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 2911309 commit f829c63
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 87 deletions.
174 changes: 87 additions & 87 deletions crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,38 +833,38 @@ fn test() {
r#"import withPaths from "foo/bar";"#,
Some(serde_json::json!([{ "paths": ["foo/bar"] }])),
),
(
r#"import withPatterns from "foo/bar";"#,
Some(serde_json::json!([{ "patterns": ["foo"] }])),
),
(
r#"import withPatterns from "foo/bar";"#,
Some(serde_json::json!([{ "patterns": ["bar"] }])),
),
// (
// r#"import withPatterns from "foo/baz";"#,
// Some(
// serde_json::json!([{ "patterns": [{ "group": ["foo/*", "!foo/bar"], "message": "foo is forbidden, use foo/bar instead" }] }]),
// ),
// ),
// (
// r#"import withPatterns from "foo/baz";"#,
// Some(
// serde_json::json!([{ "patterns": [{ "group": ["foo/bar", "foo/baz"], "message": "some foo subimports are restricted" }] }]),
// ),
// r#"import withPatterns from "foo/bar";"#,
// Some(serde_json::json!([{ "patterns": ["foo"] }])),
// ),
// (
// r#"import withPatterns from "foo/bar";"#,
// Some(serde_json::json!([{ "patterns": [{ "group": ["foo/bar"] }] }])),
// Some(serde_json::json!([{ "patterns": ["bar"] }])),
// ),
(
r#"import withPatterns from "foo/baz";"#,
Some(
serde_json::json!([{ "patterns": [{ "group": ["foo/*", "!foo/bar"], "message": "foo is forbidden, use foo/bar instead" }] }]),
),
),
(
r#"import withPatterns from "foo/baz";"#,
Some(
serde_json::json!([{ "patterns": [{ "group": ["foo/bar", "foo/baz"], "message": "some foo subimports are restricted" }] }]),
),
),
(
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"] }] }])),
// ),
// (
// r#"import withGitignores from "foo/bar";"#,
// Some(serde_json::json!([{ "patterns": ["foo/*", "!foo/baz"] }])),
// ),
(
r#"import withGitignores from "foo/bar";"#,
Some(serde_json::json!([{ "patterns": ["foo/*", "!foo/baz"] }])),
),
(r#"export * from "fs";"#, Some(serde_json::json!(["fs"]))),
(r#"export * as ns from "fs";"#, Some(serde_json::json!(["fs"]))),
(r#"export {a} from "fs";"#, Some(serde_json::json!(["fs"]))),
Expand Down Expand Up @@ -1336,76 +1336,76 @@ fn test() {
"import relativeWithPaths from '../foo';",
Some(serde_json::json!([{ "paths": ["../foo"] }])),
),
// (
// "import relativeWithPatterns from '../foo';",
// Some(serde_json::json!([{ "patterns": ["../foo"] }])),
// ),
(
"import relativeWithPatterns from '../foo';",
Some(serde_json::json!([{ "patterns": ["../foo"] }])),
),
("import absolute from '/foo';", Some(serde_json::json!(["/foo"]))),
("import absoluteWithPaths from '/foo';", Some(serde_json::json!([{ "paths": ["/foo"] }]))),
// (
// "import absoluteWithPatterns from '/foo';",
// Some(serde_json::json!([{ "patterns": ["foo"] }])),
// ),
(
"import absoluteWithPatterns from '/foo';",
Some(serde_json::json!([{ "patterns": ["foo"] }])),
),
// (
// "import absoluteWithPatterns from '#foo/bar';",
// Some(serde_json::json!([{ "patterns": ["\\#foo"] }])),
// ),
// (
// "import { Foo } from '../../my/relative-module';",
// Some(serde_json::json!([{
// "patterns": [{
// "group": ["**/my/relative-module"],
// "importNames": ["Foo"]
// }]
// }])),
// ),
// (
// "import { Foo, Bar } from '../../my/relative-module';",
// Some(serde_json::json!([{
// "patterns": [{
// "group": ["**/my/relative-module"],
// "importNames": ["Foo", "Bar"],
// "message": "Import from @/utils instead."
// }]
// }])),
// ),
// (
// "import * as All from '../../my/relative-module';",
// Some(serde_json::json!([{
// "patterns": [{
// "group": ["**/my/relative-module"],
// "importNames": ["Foo"]
// }]
// }])),
// ),
// (
// "import * as AllWithCustomMessage from '../../my/relative-module';",
// Some(serde_json::json!([{
// "patterns": [{
// "group": ["**/my/relative-module"],
// "importNames": ["Foo"],
// "message": "Import from @/utils instead."
// }]
// }])),
// ),
// (
// "import def, * as ns from 'mod';",
// Some(serde_json::json!([{
// "patterns": [{
// "group": ["mod"],
// "importNames": ["default"]
// }]
// }])),
// ),
// (
// "import Foo from 'mod';",
// Some(serde_json::json!([{
// "patterns": [{
// "group": ["mod"],
// "importNames": ["default"]
// }]
// }])),
// ),
(
"import { Foo } from '../../my/relative-module';",
Some(serde_json::json!([{
"patterns": [{
"group": ["**/my/relative-module"],
"importNames": ["Foo"]
}]
}])),
),
(
"import { Foo, Bar } from '../../my/relative-module';",
Some(serde_json::json!([{
"patterns": [{
"group": ["**/my/relative-module"],
"importNames": ["Foo", "Bar"],
"message": "Import from @/utils instead."
}]
}])),
),
(
"import * as All from '../../my/relative-module';",
Some(serde_json::json!([{
"patterns": [{
"group": ["**/my/relative-module"],
"importNames": ["Foo"]
}]
}])),
),
(
"import * as AllWithCustomMessage from '../../my/relative-module';",
Some(serde_json::json!([{
"patterns": [{
"group": ["**/my/relative-module"],
"importNames": ["Foo"],
"message": "Import from @/utils instead."
}]
}])),
),
(
"import def, * as ns from 'mod';",
Some(serde_json::json!([{
"patterns": [{
"group": ["mod"],
"importNames": ["default"]
}]
}])),
),
(
"import Foo from 'mod';",
Some(serde_json::json!([{
"patterns": [{
"group": ["mod"],
"importNames": ["default"]
}]
}])),
),
// (
// "import { Foo } from 'foo';",
// Some(serde_json::json!([{
Expand Down
98 changes: 98 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 @@ -30,6 +30,34 @@ snapshot_kind: text
╰────
help: Remove the import statement.

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

eslint(no-restricted-imports): some foo subimports are restricted
╭─[no_restricted_imports.tsx:1:26]
1import withPatterns from "foo/baz";
· ─────────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): 'foo/bar' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:26]
1import withPatterns from "foo/bar";
· ─────────
╰────
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";
· ─────────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): 'fs' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:1]
1export * from "fs";
Expand Down Expand Up @@ -471,6 +499,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:34]
1import relativeWithPatterns from '../foo';
· ────────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): '/foo' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:22]
1import absolute from '/foo';
Expand All @@ -485,6 +520,69 @@ snapshot_kind: text
╰────
help: Remove the import statement.

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

eslint(no-restricted-imports): '../../my/relative-module' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:21]
1import { Foo } from '../../my/relative-module';
· ──────────────────────────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): Import from @/utils instead.
╭─[no_restricted_imports.tsx:1:26]
1import { Foo, Bar } from '../../my/relative-module';
· ──────────────────────────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): Import from @/utils instead.
╭─[no_restricted_imports.tsx:1:26]
1import { Foo, Bar } from '../../my/relative-module';
· ──────────────────────────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): '../../my/relative-module' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:22]
1import * as All from '../../my/relative-module';
· ──────────────────────────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): Import from @/utils instead.
╭─[no_restricted_imports.tsx:1:39]
1import * as AllWithCustomMessage from '../../my/relative-module';
· ──────────────────────────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): 'mod' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:26]
1import def, * as ns from 'mod';
· ─────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): 'mod' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:26]
1import def, * as ns from 'mod';
· ─────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): 'mod' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:17]
1import Foo from 'mod';
· ─────
╰────
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";
Expand Down

0 comments on commit f829c63

Please sign in to comment.