Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion 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 crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ convert_case = { workspace = true }
cow-utils = { workspace = true }
fast-glob = { workspace = true }
globset = { workspace = true }
ignore = { workspace = true }
indexmap = { workspace = true, features = ["rayon"] }
itertools = { workspace = true }
javascript-globals = { workspace = true }
Expand Down
58 changes: 39 additions & 19 deletions crates/oxc_linter/src/rules/eslint/no_restricted_imports.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use ignore::gitignore::GitignoreBuilder;
use std::borrow::Cow;

use globset::GlobBuilder;
use lazy_regex::Regex;
use oxc_ast::{
AstKind,
Expand All @@ -10,7 +12,6 @@ use oxc_span::{CompactStr, Span};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Deserializer, de::Error};
use serde_json::Value;
use std::borrow::Cow;

use crate::{
ModuleRecord,
Expand Down Expand Up @@ -856,30 +857,37 @@ impl RestrictedPattern {
return GlobResult::None;
};

let mut builder = GitignoreBuilder::new("");
// returns always OK, will be fixed in the next version
let _ = builder.case_insensitive(!self.case_sensitive.unwrap_or(false));
let case_insensitive = !self.case_sensitive.unwrap_or(false);

for group in groups {
// returns always OK
let _ = builder.add_line(None, group.as_str());
}
let mut decision = GlobResult::None;

let Ok(gitignore) = builder.build() else {
return GlobResult::None;
};
for raw_pat in groups {
let (negated, pat) = match raw_pat.strip_prefix('!') {
Some(rest) => (true, rest),
None => (false, raw_pat.as_str()),
};

let matched = gitignore.matched(name, false);
// roughly based on https://github.com/BurntSushi/ripgrep/blob/6dfaec03e830892e787686917509c17860456db1/crates/ignore/src/gitignore.rs#L436-L516
let mut pat = pat.to_string();

if matched.is_whitelist() {
return GlobResult::Whitelist;
}
if !pat.starts_with('/') && !pat.chars().any(|c| c == '/') && (!pat.starts_with("**")) {
pat = format!("**/{pat}");
}

if matched.is_none() {
return GlobResult::None;
let Ok(glob) = GlobBuilder::new(&pat)
.case_insensitive(case_insensitive)
.build()
.map(|g| g.compile_matcher())
else {
continue;
};

if glob.is_match(name) {
decision = if negated { GlobResult::Whitelist } else { GlobResult::Found };
}
}

GlobResult::Found
decision
}

fn get_regex_result(&self, name: &str) -> bool {
Expand Down Expand Up @@ -1762,6 +1770,12 @@ fn test() {
}]
}])),
),
(
r#"import a from "./index.mjs";"#,
Some(
serde_json::json!([{ "patterns": [{ "group": ["[@a-z]*", "!.*/**"], "message": "foo is forbidden, use bar instead" }] }]),
),
),
];

let pass_typescript = vec![
Expand Down Expand Up @@ -2984,6 +2998,12 @@ fn test() {
}]
}])),
),
(
r#"import {x} from "foo"; import {x2} from "./index.mjs"; import {x3} from "index";"#,
Some(
serde_json::json!([{ "patterns": [{ "group": ["[@a-z]*", "!.*/**","./index.mjs"], "message": "foo is forbidden, use bar instead" }] }]),
),
),
// (
// "
// // error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,27 @@ source: crates/oxc_linter/src/tester.rs
╰────
help: foo is forbidden, use bar instead

⚠ eslint(no-restricted-imports): 'foo' import is restricted from being used by a pattern.
╭─[no_restricted_imports.tsx:1:1]
1 │ import {x} from "foo"; import {x2} from "./index.mjs"; import {x3} from "index";
· ──────────────────────
╰────
help: foo is forbidden, use bar instead

⚠ eslint(no-restricted-imports): './index.mjs' import is restricted from being used by a pattern.
╭─[no_restricted_imports.tsx:1:24]
1 │ import {x} from "foo"; import {x2} from "./index.mjs"; import {x3} from "index";
· ───────────────────────────────
╰────
help: foo is forbidden, use bar instead

⚠ eslint(no-restricted-imports): 'index' import is restricted from being used by a pattern.
╭─[no_restricted_imports.tsx:1:56]
1 │ import {x} from "foo"; import {x2} from "./index.mjs"; import {x3} from "index";
· ─────────────────────────
╰────
help: foo is forbidden, use bar instead

⚠ eslint(no-restricted-imports): 'import1' import is restricted from being used.
╭─[no_restricted_imports.tsx:1:1]
1 │ import foo from 'import1';
Expand Down
Loading