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 f829c63 commit 9aa76d3
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 72 deletions.
144 changes: 72 additions & 72 deletions crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,40 +789,40 @@ fn test() {
// }]
// }])),
// ),
// (
// r#"import withPatterns from "foo/bar";"#,
// Some(
// serde_json::json!([{ "patterns": [{ "regex": "foo/(?!bar)", "message": "foo is forbidden, use bar instead" }] }]),
// ),
// ),
// (
// "import withPatternsCaseSensitive from 'foo';",
// Some(serde_json::json!([{
// "patterns": [{
// "regex": "FOO",
// "message": "foo is forbidden, use bar instead",
// "caseSensitive": true
// }]
// }])),
// ),
// (
// "import Foo from '../../my/relative-module';",
// Some(serde_json::json!([{
// "patterns": [{
// "regex": "my/relative-module",
// "importNamePattern": "^Foo"
// }]
// }])),
// ),
// (
// "import { Bar } from '../../my/relative-module';",
// Some(serde_json::json!([{
// "patterns": [{
// "regex": "my/relative-module",
// "importNamePattern": "^Foo"
// }]
// }])),
// ),
(
r#"import withPatterns from "foo/bar";"#,
Some(
serde_json::json!([{ "patterns": [{ "regex": "foo/(?!bar)", "message": "foo is forbidden, use bar instead" }] }]),
),
),
(
"import withPatternsCaseSensitive from 'foo';",
Some(serde_json::json!([{
"patterns": [{
"regex": "FOO",
"message": "foo is forbidden, use bar instead",
"caseSensitive": true
}]
}])),
),
(
"import Foo from '../../my/relative-module';",
Some(serde_json::json!([{
"patterns": [{
"regex": "my/relative-module",
"importNamePattern": "^Foo"
}]
}])),
),
(
"import { Bar } from '../../my/relative-module';",
Some(serde_json::json!([{
"patterns": [{
"regex": "my/relative-module",
"importNamePattern": "^Foo"
}]
}])),
),
];

let fail = vec![
Expand Down Expand Up @@ -1633,25 +1633,25 @@ fn test() {
}]
}])),
),
// (
// r#"import { AllowedObject, DisallowedObject } from "foo";"#,
// Some(serde_json::json!([{
// "patterns": [{
// "group": ["foo"],
// "allowImportNames": ["AllowedObject"]
// }]
// }])),
// ),
// (
// r#"import { AllowedObject, DisallowedObject } from "foo";"#,
// Some(serde_json::json!([{
// "patterns": [{
// "group": ["foo"],
// "allowImportNames": ["AllowedObject"],
// "message": r#"Only "AllowedObject" is allowed to be imported from "foo"."#
// }]
// }])),
// ),
(
r#"import { AllowedObject, DisallowedObject } from "foo";"#,
Some(serde_json::json!([{
"patterns": [{
"group": ["foo"],
"allowImportNames": ["AllowedObject"]
}]
}])),
),
(
r#"import { AllowedObject, DisallowedObject } from "foo";"#,
Some(serde_json::json!([{
"patterns": [{
"group": ["foo"],
"allowImportNames": ["AllowedObject"],
"message": r#"Only "AllowedObject" is allowed to be imported from "foo"."#
}]
}])),
),
(
r#"import * as AllowedObject from "foo";"#,
Some(serde_json::json!([{
Expand All @@ -1671,25 +1671,25 @@ fn test() {
}]
}])),
),
// (
// r#"import * as AllowedObject from "foo/bar";"#,
// Some(serde_json::json!([{
// "patterns": [{
// "group": ["foo/*"],
// "allowImportNames": ["AllowedObject"]
// }]
// }])),
// ),
// (
// r#"import * as AllowedObject from "foo/bar";"#,
// Some(serde_json::json!([{
// "patterns": [{
// "group": ["foo/*"],
// "allowImportNames": ["AllowedObject"],
// "message": r#"Only "AllowedObject" is allowed to be imported from "foo"."#
// }]
// }])),
// ),
(
r#"import * as AllowedObject from "foo/bar";"#,
Some(serde_json::json!([{
"patterns": [{
"group": ["foo/*"],
"allowImportNames": ["AllowedObject"]
}]
}])),
),
(
r#"import * as AllowedObject from "foo/bar";"#,
Some(serde_json::json!([{
"patterns": [{
"group": ["foo/*"],
"allowImportNames": ["AllowedObject"],
"message": r#"Only "AllowedObject" is allowed to be imported from "foo"."#
}]
}])),
),
// (
// r#"import * as AllowedObject from "foo/bar";"#,
// Some(serde_json::json!([{
Expand Down
28 changes: 28 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 @@ -597,6 +597,20 @@ snapshot_kind: text
╰────
help: Remove the import statement.

eslint(no-restricted-imports): 'foo' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:49]
1import { AllowedObject, DisallowedObject } from "foo";
· ─────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): Only "AllowedObject" is allowed to be imported from "foo".
╭─[no_restricted_imports.tsx:1:49]
1import { AllowedObject, DisallowedObject } from "foo";
· ─────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): 'foo' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:32]
1import * as AllowedObject from "foo";
Expand All @@ -610,3 +624,17 @@ snapshot_kind: text
· ─────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): 'foo/bar' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:32]
1import * as AllowedObject from "foo/bar";
· ─────────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): Only "AllowedObject" is allowed to be imported from "foo".
╭─[no_restricted_imports.tsx:1:32]
1import * as AllowedObject from "foo/bar";
· ─────────
╰────
help: Remove the import statement.

0 comments on commit 9aa76d3

Please sign in to comment.