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
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]
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"
Expand Down