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 9aa76d3 commit 5f8d010
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
31 changes: 17 additions & 14 deletions crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct RestrictedPattern {
group: Vec<CompactStr>,
import_names: Option<Box<[CompactStr]>>,
allow_import_names: Option<Box<[CompactStr]>>,
case_sensitive: Option<bool>,
message: Option<CompactStr>,
}

Expand Down Expand Up @@ -153,6 +154,7 @@ fn add_configuration_patterns_from_string(paths: &mut Vec<RestrictedPattern>, mo
group: vec![CompactStr::new(module_name)],
import_names: None,
allow_import_names: None,
case_sensitive: None,
message: None,
});
}
Expand Down Expand Up @@ -341,6 +343,7 @@ impl NoRestrictedImports {
}

let mut builder = GitignoreBuilder::new("/");
let _ = builder.case_insensitive(!pattern.case_sensitive.unwrap_or(false));

for group in &pattern.group {
let _ = builder.add_line(None, group.as_str());
Expand Down Expand Up @@ -857,10 +860,10 @@ fn test() {
r#"import withPatterns from "foo/bar";"#,
Some(serde_json::json!([{ "patterns": [{ "group": ["foo/bar"] }] }])),
),
// (
// "import withPatternsCaseInsensitive from 'foo';",
// Some(serde_json::json!([{ "patterns": [{ "group": ["FOO"] }] }])),
// ),
(
"import withPatternsCaseInsensitive from 'foo';",
Some(serde_json::json!([{ "patterns": [{ "group": ["FOO"] }] }])),
),
(
r#"import withGitignores from "foo/bar";"#,
Some(serde_json::json!([{ "patterns": ["foo/*", "!foo/baz"] }])),
Expand Down Expand Up @@ -1734,16 +1737,16 @@ fn test() {
// }]
// }])),
// ),
// (
// "import withPatternsCaseSensitive from 'foo';",
// Some(serde_json::json!([{
// "patterns": [{
// "group": ["FOO"],
// "message": "foo is forbidden, use bar instead",
// "caseSensitive": false
// }]
// }])),
// ),
(
"import withPatternsCaseSensitive from 'foo';",
Some(serde_json::json!([{
"patterns": [{
"group": ["FOO"],
"message": "foo is forbidden, use bar instead",
"caseSensitive": false
}]
}])),
),
// (
// "
// // error
Expand Down
14 changes: 14 additions & 0 deletions crates/oxc_linter/src/snapshots/eslint_no_restricted_imports.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ snapshot_kind: text
╰────
help: Remove the import statement.

eslint(no-restricted-imports): 'foo' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:41]
1import withPatternsCaseInsensitive from 'foo';
· ─────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): 'foo/bar' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:28]
1import withGitignores from "foo/bar";
Expand Down Expand Up @@ -638,3 +645,10 @@ snapshot_kind: text
· ─────────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): foo is forbidden, use bar instead
╭─[no_restricted_imports.tsx:1:39]
1import withPatternsCaseSensitive from 'foo';
· ─────
╰────
help: Remove the import statement.

0 comments on commit 5f8d010

Please sign in to comment.