From 9aa76d39a8f321f2aa6ec74da30d7a0c8a920f9d Mon Sep 17 00:00:00 2001 From: Sysix Date: Fri, 20 Dec 2024 22:21:07 +0100 Subject: [PATCH] fix(linter): rule: no-restricted-imports support option patterns with group key --- .../src/rules/eslint/no_restricted_imports.rs | 144 +++++++++--------- .../eslint_no_restricted_imports.snap | 28 ++++ 2 files changed, 100 insertions(+), 72 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs b/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs index aa34748e4aefc..14e830c2382d6 100644 --- a/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs +++ b/crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs @@ -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![ @@ -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!([{ @@ -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!([{ diff --git a/crates/oxc_linter/src/snapshots/eslint_no_restricted_imports.snap b/crates/oxc_linter/src/snapshots/eslint_no_restricted_imports.snap index 50cd1c37e49b0..972b0993fbdef 100644 --- a/crates/oxc_linter/src/snapshots/eslint_no_restricted_imports.snap +++ b/crates/oxc_linter/src/snapshots/eslint_no_restricted_imports.snap @@ -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] + 1 │ import { 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] + 1 │ import { 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] 1 │ import * as AllowedObject from "foo"; @@ -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] + 1 │ import * 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] + 1 │ import * as AllowedObject from "foo/bar"; + · ───────── + ╰──── + help: Remove the import statement.