Skip to content

Commit

Permalink
fix(linter): rule: no-restricted-imports support option patterns with…
Browse files Browse the repository at this point in the history
… group key
  • Loading branch information
Sysix committed Dec 20, 2024
1 parent 32f9050 commit 6b6d508
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ cow-utils = { workspace = true }
dashmap = { workspace = true }
fast-glob = { workspace = true }
globset = { workspace = true }
ignore = { workspace = true }
itertools = { workspace = true }
json-strip-comments = { workspace = true }
language-tags = { workspace = true }
Expand Down
30 changes: 30 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use oxc_span::{CompactStr, Span};
use rustc_hash::FxHashMap;
use serde::Deserialize;
use serde_json::Value;
use ignore::gitignore::GitignoreBuilder;

use crate::{
context::LintContext,
Expand Down Expand Up @@ -217,6 +218,35 @@ impl Rule for NoRestrictedImports {
}
}

for pattern in &self.patterns {
let mut builder = GitignoreBuilder::new("/");

for group in &pattern.group {
let _ = builder.add_line(None, group.as_str());
}

let Ok(gitignore) = builder.build() else {
continue;
};

for entry in &module_record.import_entries {
let source = entry.module_request.name();
let span = entry.module_request.span();

let matched = gitignore.matched(source, true);
println!("{:?}", matched);

// ToDo: better whitelist handling
if matched.is_whitelist() {
break;
}

if !matched.is_none() {
no_restricted_imports_diagnostic(ctx, span, pattern.message.clone(), source);
}
}
}

for path in &self.paths {
for entry in &module_record.import_entries {
let source = entry.module_request.name();
Expand Down

0 comments on commit 6b6d508

Please sign in to comment.