From 328c558e2ff438593025a9e9dc597b18e7b7b624 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Wed, 20 Mar 2024 12:00:30 +0800 Subject: [PATCH] feat(linter/import) support check re-export in named --- crates/oxc_linter/src/rules/import/named.rs | 20 ++++++++++++------ .../oxc_linter/src/rules/import/namespace.rs | 18 ++++------------ crates/oxc_linter/src/snapshots/named.snap | 21 ------------------- 3 files changed, 18 insertions(+), 41 deletions(-) diff --git a/crates/oxc_linter/src/rules/import/named.rs b/crates/oxc_linter/src/rules/import/named.rs index 65ff862ea171..a821a31695b1 100644 --- a/crates/oxc_linter/src/rules/import/named.rs +++ b/crates/oxc_linter/src/rules/import/named.rs @@ -58,6 +58,15 @@ impl Rule for Named { if remote_module_record.exported_bindings.contains_key(import_name.name()) { continue; } + // check re-export + if remote_module_record + .exported_bindings_from_star_export + .iter() + .any(|entry| entry.value().contains(import_name.name())) + { + continue; + } + ctx.diagnostic(NamedDiagnostic( import_name.name().to_string(), specifier.to_string(), @@ -106,14 +115,13 @@ fn test() { "import { destructingAssign } from './named-exports'", "import { destructingRenamedAssign } from './named-exports'", "import { ActionTypes } from './qc'", - // TODO: export * - // "import {a, b, c, d} from './re-export'", - // "import {a, b, c} from './re-export-common-star'", + "import {a, b, c, d} from './re-export'", // "import {RuleTester} from './re-export-node_modules'", - // "import { jsxFoo } from './jsx/AnotherComponent'", + "import { jsxFoo } from './jsx/AnotherComponent'", "import {a, b, d} from './common'; // eslint-disable-line named", "import { foo, bar } from './re-export-names'", // TODO: module.exports + // "import {a, b, c} from './re-export-common-star'", // "import { foo, bar } from './common'", // ignore core modules by default "import { foo } from 'crypto'", @@ -150,7 +158,7 @@ fn test() { // "import {a, b, d} from './common'", // settings: { 'import/ignore': ['bar'] }, // "import { baz } from './bar'", - // "import { common } from './re-export-default'", + "import { common } from './re-export-default'", // "const { destructuredProp } = require('./named-exports')", // "let { arrayKeyProp } = require('./named-exports')", // "const { deepProp } = require('./named-exports')", @@ -164,7 +172,7 @@ fn test() { "import { 'foo' as foo } from './bar'", "import { 'foo' as foo } from './empty-module'", // export all - // "import { foo } from './export-all'", + "import { foo } from './export-all'", // TypeScript export assignment "import x from './typescript-export-assign-object'", ]; diff --git a/crates/oxc_linter/src/rules/import/namespace.rs b/crates/oxc_linter/src/rules/import/namespace.rs index 26cad69c4cc2..0e058148a1f7 100644 --- a/crates/oxc_linter/src/rules/import/namespace.rs +++ b/crates/oxc_linter/src/rules/import/namespace.rs @@ -61,20 +61,10 @@ impl Rule for Namespace { let check_binding_exported = |name: &str, span| { if module.exported_bindings.contains_key(name) - || module.star_export_entries.iter().any(|entry| { - entry.module_request.as_ref().is_some_and(|name_span| { - module - .exported_bindings_from_star_export - .get( - &module - .loaded_modules - .get(name_span.name()) - .unwrap() - .resolved_absolute_path, - ) - .is_some_and(|bindings| bindings.contains(&CompactStr::from(name))) - }) - }) + || module + .exported_bindings_from_star_export + .iter() + .any(|entry| entry.value().contains(&CompactStr::from(name))) { return; } diff --git a/crates/oxc_linter/src/snapshots/named.snap b/crates/oxc_linter/src/snapshots/named.snap index 07920b08e812..37f5a5c28629 100644 --- a/crates/oxc_linter/src/snapshots/named.snap +++ b/crates/oxc_linter/src/snapshots/named.snap @@ -51,27 +51,6 @@ expression: named ╰──── help: does "./qc" have the export "ActionTypes1"? - ⚠ eslint-plugin-import(named): named import "a" not found - ╭─[index.js:1:9] - 1 │ import {a, b, c, d, e} from './re-export' - · ─ - ╰──── - help: does "./re-export" have the export "a"? - - ⚠ eslint-plugin-import(named): named import "b" not found - ╭─[index.js:1:12] - 1 │ import {a, b, c, d, e} from './re-export' - · ─ - ╰──── - help: does "./re-export" have the export "b"? - - ⚠ eslint-plugin-import(named): named import "d" not found - ╭─[index.js:1:18] - 1 │ import {a, b, c, d, e} from './re-export' - · ─ - ╰──── - help: does "./re-export" have the export "d"? - ⚠ eslint-plugin-import(named): named import "e" not found ╭─[index.js:1:21] 1 │ import {a, b, c, d, e} from './re-export'