Skip to content

Commit

Permalink
fix(linter): fix import/no_cycle with ignoreTypes (#5995)
Browse files Browse the repository at this point in the history
related toeverything/AFFiNE#7580

`iter().all` produces `true` on an empty list.
  • Loading branch information
Boshen committed Sep 23, 2024
1 parent f5eee72 commit 4771492
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
21 changes: 17 additions & 4 deletions crates/oxc_linter/src/rules/import/no_cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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}]))),
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_linter/src/snapshots/no_cycle.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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]
1import { 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]
1import { foo } from "./es6/depth-two"
Expand Down

0 comments on commit 4771492

Please sign in to comment.