Skip to content

Commit

Permalink
fix(linter): rule no-restricted-imports support missing options - rem…
Browse files Browse the repository at this point in the history
…ove regress
  • Loading branch information
Sysix committed Dec 25, 2024
1 parent f088c10 commit 0da9b9d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 95 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ prettyplease = "0.2.25"
project-root = "0.2.2"
rayon = "1.10.0"
regex = "1.11.1"
regress = "0.10.1"
ropey = "1.6.1"
rust-lapper = "1.1.0"
ryu-js = "1.0.1"
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ nonmax = { workspace = true }
phf = { workspace = true, features = ["macros"] }
rayon = { workspace = true }
regex = { workspace = true }
regress = { workspace = true }
rust-lapper = { workspace = true }
rustc-hash = { workspace = true }
schemars = { workspace = true, features = ["indexmap2"] }
Expand Down
73 changes: 34 additions & 39 deletions crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ignore::gitignore::GitignoreBuilder;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{CompactStr, Span};
use regress::Regex;
use regex::Regex;
use rustc_hash::FxHashMap;
use serde::Deserialize;
use serde_json::Value;
Expand Down Expand Up @@ -323,12 +323,7 @@ impl RestrictedPattern {
return false;
};

let flags = match self.case_sensitive {
Some(case_sensitive) if case_sensitive => "u",
_ => "iu",
};

let Ok(reg_exp) = Regex::with_flags(regex.as_str(), flags) else {
let Ok(reg_exp) = Regex::new(regex.as_str()) else {
return false;
};

Expand All @@ -340,7 +335,7 @@ impl RestrictedPattern {
return false;
};

let Ok(reg_exp) = Regex::with_flags(import_name_pattern.as_str(), "u") else {
let Ok(reg_exp) = Regex::new(import_name_pattern.as_str()) else {
return false;
};

Expand All @@ -352,7 +347,7 @@ impl RestrictedPattern {
return false;
};

let Ok(reg_exp) = Regex::with_flags(allow_import_names.as_str(), "u") else {
let Ok(reg_exp) = Regex::new(allow_import_names.as_str()) else {
return false;
};

Expand Down Expand Up @@ -954,12 +949,12 @@ fn test() {
}]
}])),
),
(
r#"import withPatterns from "foo/bar";"#,
Some(
serde_json::json!([{ "patterns": [{ "regex": "foo/(?!bar)", "message": "foo is forbidden, use bar instead" }] }]),
),
),
// (
// 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!([{
Expand Down Expand Up @@ -1874,12 +1869,12 @@ fn test() {
}]
}])),
),
(
r#"import withPatterns from "foo/baz";"#,
Some(
serde_json::json!([{ "patterns": [{ "regex": "foo/(?!bar)", "message": "foo is forbidden, use bar instead" }] }]),
),
),
// (
// r#"import withPatterns from "foo/baz";"#,
// Some(
// serde_json::json!([{ "patterns": [{ "regex": "foo/(?!bar)", "message": "foo is forbidden, use bar instead" }] }]),
// ),
// ),
(
"import withPatternsCaseSensitive from 'FOO';",
Some(serde_json::json!([{
Expand Down Expand Up @@ -1909,24 +1904,24 @@ fn test() {
}]
}])),
),
(
"
// error
import { Foo_Enum } from '@app/api';
import { Bar_Enum } from '@app/api/bar';
import { Baz_Enum } from '@app/api/baz';
import { B_Enum } from '@app/api/enums/foo';
// no error
import { C_Enum } from '@app/api/enums';
",
Some(serde_json::json!([{
"patterns": [{
"regex": "@app/(?!(api/enums$)).*",
"importNamePattern": "_Enum$"
}]
}])),
),
// (
// "
// // error
// import { Foo_Enum } from '@app/api';
// import { Bar_Enum } from '@app/api/bar';
// import { Baz_Enum } from '@app/api/baz';
// import { B_Enum } from '@app/api/enums/foo';
//
// // no error
// import { C_Enum } from '@app/api/enums';
// ",
// Some(serde_json::json!([{
// "patterns": [{
// "regex": "@app/(?!(api/enums$)).*",
// "importNamePattern": "_Enum$"
// }]
// }])),
// ),
];

Tester::new(NoRestrictedImports::NAME, NoRestrictedImports::CATEGORY, pass, fail)
Expand Down
43 changes: 0 additions & 43 deletions crates/oxc_linter/src/snapshots/eslint_no_restricted_imports.snap
Original file line number Diff line number Diff line change
Expand Up @@ -835,13 +835,6 @@ snapshot_kind: text
╰────
help: Remove the import statement.

eslint(no-restricted-imports): foo is forbidden, use bar instead
╭─[no_restricted_imports.tsx:1:26]
1import withPatterns from "foo/baz";
· ─────────
╰────
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';
Expand All @@ -862,39 +855,3 @@ snapshot_kind: text
· ─────
╰────
help: Remove the import statement.

eslint(no-restricted-imports): '@app/api' import is restricted from being used.
╭─[no_restricted_imports.tsx:3:43]
2// error
3import { Foo_Enum } from '@app/api';
· ──────────
4import { Bar_Enum } from '@app/api/bar';
╰────
help: Remove the import statement.

eslint(no-restricted-imports): '@app/api/bar' import is restricted from being used.
╭─[no_restricted_imports.tsx:4:43]
3import { Foo_Enum } from '@app/api';
4import { Bar_Enum } from '@app/api/bar';
· ──────────────
5import { Baz_Enum } from '@app/api/baz';
╰────
help: Remove the import statement.

eslint(no-restricted-imports): '@app/api/baz' import is restricted from being used.
╭─[no_restricted_imports.tsx:5:43]
4import { Bar_Enum } from '@app/api/bar';
5import { Baz_Enum } from '@app/api/baz';
· ──────────────
6import { B_Enum } from '@app/api/enums/foo';
╰────
help: Remove the import statement.

eslint(no-restricted-imports): '@app/api/enums/foo' import is restricted from being used.
╭─[no_restricted_imports.tsx:6:41]
5import { Baz_Enum } from '@app/api/baz';
6import { B_Enum } from '@app/api/enums/foo';
· ────────────────────
7
╰────
help: Remove the import statement.

0 comments on commit 0da9b9d

Please sign in to comment.