fix(linter): fix import/no_cycle with ignoreTypes (#5995)

related https://github.com/toeverything/AFFiNE/issues/7580

`iter().all` produces `true` on an empty list.
This commit is contained in:
Boshen 2024-09-23 15:10:52 +00:00
parent f5eee723fa
commit 4771492b6a
2 changed files with 26 additions and 4 deletions

View file

@ -109,17 +109,29 @@ impl Rule for NoCycle {
.max_depth(self.max_depth)
.filter(move |(key, val): (&CompactStr, &Arc<ModuleRecord>), parent: &ModuleRecord| {
let path = &val.resolved_absolute_path;
let is_node_module = path
.components()
.any(|c| matches!(c, Component::Normal(p) if p == OsStr::new("node_modules")));
let is_type_import = !ignore_types
|| !parent
if is_node_module {
return false;
}
if ignore_types {
let import_entries = parent
.import_entries
.iter()
.filter(|entry| entry.module_request.name() == key)
.all(|entry| entry.is_type);
.collect::<Vec<_>>();
if !import_entries.is_empty()
&& import_entries.iter().all(|entry| entry.is_type)
{
return false;
}
}
is_node_module || is_type_import
true
})
.event(|event, (key, val), _| match event {
ModuleGraphVisitorEvent::Enter => {
@ -242,6 +254,7 @@ fn test() {
// (r#"require(["./es6/depth-one"], d1 => {})"#, Some(json!([{"amd":true}]))),
// (r#"define(["./es6/depth-one"], d1 => {})"#, Some(json!([{"amd":true}]))),
(r#"import { foo } from "./es6/depth-one-reexport""#, None),
(r#"import { foo } from "./es6/depth-one-reexport""#, Some(json!([{"ignoreTypes":true}]))),
(r#"import { foo } from "./es6/depth-two""#, None),
(r#"import { foo } from "./es6/depth-two""#, Some(json!([{"maxDepth":2}]))),
// (r#"const { foo } = require("./es6/depth-two")"#, Some(json!([{"commonjs":true}]))),

View file

@ -19,6 +19,15 @@ source: crates/oxc_linter/src/tester.rs
-> ./es6/depth-one-reexport - fixtures/import/cycles/es6/depth-one-reexport.js
-> ../depth-zero - fixtures/import/cycles/depth-zero.js
⚠ eslint-plugin-import(no-cycle): Dependency cycle detected
╭─[cycles/depth-zero.js:1:21]
1 │ import { foo } from "./es6/depth-one-reexport"
· ──────────────────────────
╰────
help: These paths form a cycle:
-> ./es6/depth-one-reexport - fixtures/import/cycles/es6/depth-one-reexport.js
-> ../depth-zero - fixtures/import/cycles/depth-zero.js
⚠ eslint-plugin-import(no-cycle): Dependency cycle detected
╭─[cycles/depth-zero.js:1:21]
1 │ import { foo } from "./es6/depth-two"