mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
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:
parent
f5eee723fa
commit
4771492b6a
2 changed files with 26 additions and 4 deletions
|
|
@ -109,17 +109,29 @@ impl Rule for NoCycle {
|
||||||
.max_depth(self.max_depth)
|
.max_depth(self.max_depth)
|
||||||
.filter(move |(key, val): (&CompactStr, &Arc<ModuleRecord>), parent: &ModuleRecord| {
|
.filter(move |(key, val): (&CompactStr, &Arc<ModuleRecord>), parent: &ModuleRecord| {
|
||||||
let path = &val.resolved_absolute_path;
|
let path = &val.resolved_absolute_path;
|
||||||
|
|
||||||
let is_node_module = path
|
let is_node_module = path
|
||||||
.components()
|
.components()
|
||||||
.any(|c| matches!(c, Component::Normal(p) if p == OsStr::new("node_modules")));
|
.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
|
.import_entries
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|entry| entry.module_request.name() == key)
|
.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 {
|
.event(|event, (key, val), _| match event {
|
||||||
ModuleGraphVisitorEvent::Enter => {
|
ModuleGraphVisitorEvent::Enter => {
|
||||||
|
|
@ -242,6 +254,7 @@ fn test() {
|
||||||
// (r#"require(["./es6/depth-one"], d1 => {})"#, Some(json!([{"amd":true}]))),
|
// (r#"require(["./es6/depth-one"], d1 => {})"#, Some(json!([{"amd":true}]))),
|
||||||
// (r#"define(["./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""#, 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""#, None),
|
||||||
(r#"import { foo } from "./es6/depth-two""#, Some(json!([{"maxDepth":2}]))),
|
(r#"import { foo } from "./es6/depth-two""#, Some(json!([{"maxDepth":2}]))),
|
||||||
// (r#"const { foo } = require("./es6/depth-two")"#, Some(json!([{"commonjs":true}]))),
|
// (r#"const { foo } = require("./es6/depth-two")"#, Some(json!([{"commonjs":true}]))),
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,15 @@ source: crates/oxc_linter/src/tester.rs
|
||||||
-> ./es6/depth-one-reexport - fixtures/import/cycles/es6/depth-one-reexport.js
|
-> ./es6/depth-one-reexport - fixtures/import/cycles/es6/depth-one-reexport.js
|
||||||
-> ../depth-zero - fixtures/import/cycles/depth-zero.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
|
⚠ eslint-plugin-import(no-cycle): Dependency cycle detected
|
||||||
╭─[cycles/depth-zero.js:1:21]
|
╭─[cycles/depth-zero.js:1:21]
|
||||||
1 │ import { foo } from "./es6/depth-two"
|
1 │ import { foo } from "./es6/depth-two"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue