Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linter/import) support check re-export in named #2769

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 14 additions & 6 deletions crates/oxc_linter/src/rules/import/named.rs
Expand Up @@ -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(),
Expand Down Expand Up @@ -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'",
Expand Down Expand Up @@ -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')",
Expand All @@ -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'",
];
Expand Down
18 changes: 4 additions & 14 deletions crates/oxc_linter/src/rules/import/namespace.rs
Expand Up @@ -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;
}
Expand Down
21 changes: 0 additions & 21 deletions crates/oxc_linter/src/snapshots/named.snap
Expand Up @@ -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'
Expand Down