-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): add the import/no-named-default rule
- Loading branch information
1 parent
ee26b44
commit 5fd4df6
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use oxc_diagnostics::OxcDiagnostic; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::Span; | ||
|
||
use crate::{context::LintContext, module_record::ImportImportName, rule::Rule}; | ||
|
||
fn no_named_default_diagnostic(span: Span) -> OxcDiagnostic { | ||
OxcDiagnostic::warn("Replace default import with named import.") | ||
.with_help("Forbid named default exports.") | ||
.with_label(span) | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoNamedDefault; | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// Reports use of a default export as a locally named import. | ||
/// | ||
/// ### Why is this bad? | ||
/// Rationale: the syntax exists to import default exports expressively, let's use it. | ||
/// | ||
/// ### Examples | ||
/// | ||
/// Examples of **incorrect** code for this rule: | ||
/// ```js | ||
/// // message: Using exported name 'bar' as identifier for default export. | ||
/// import { default as foo } from './foo.js'; | ||
/// import { default as foo, bar } from './foo.js'; | ||
/// ``` | ||
/// | ||
/// Examples of **correct** code for this rule: | ||
/// ```js | ||
/// import foo from './foo.js'; | ||
/// import foo, { bar } from './foo.js'; | ||
/// ``` | ||
NoNamedDefault, | ||
style, | ||
pending // TODO: describe fix capabilities. Remove if no fix can be done, | ||
// keep at 'pending' if you think one could be added but don't know how. | ||
// Options are 'fix', 'fix_dangerous', 'suggestion', and 'conditional_fix_suggestion' | ||
); | ||
|
||
impl Rule for NoNamedDefault { | ||
fn run_once(&self, ctx: &LintContext) { | ||
ctx.module_record().import_entries.iter().for_each(|entry| { | ||
let ImportImportName::Name(import_name) = &entry.import_name else { | ||
return; | ||
}; | ||
if import_name.name() == "default" && !entry.is_type { | ||
ctx.diagnostic(no_named_default_diagnostic(entry.module_request.span())); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![ | ||
r#"import bar from "./bar";"#, | ||
r#"import bar, { foo } from "./bar";"#, | ||
r#"import { type default as Foo } from "./bar";"#, | ||
]; | ||
|
||
let fail = vec![ | ||
r#"import { default as bar } from "./bar";"#, | ||
r#"import { foo, default as bar } from "./bar";"#, | ||
r#"import { "default" as bar } from "./bar";"#, | ||
]; | ||
|
||
Tester::new(NoNamedDefault::NAME, NoNamedDefault::CATEGORY, pass, fail).test_and_snapshot(); | ||
} |
24 changes: 24 additions & 0 deletions
24
crates/oxc_linter/src/snapshots/import_no_named_default.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
snapshot_kind: text | ||
--- | ||
⚠ eslint-plugin-import(no-named-default): Replace default import with named import. | ||
╭─[no_named_default.tsx:1:32] | ||
1 │ import { default as bar } from "./bar"; | ||
· ─────── | ||
╰──── | ||
help: Forbid named default exports. | ||
|
||
⚠ eslint-plugin-import(no-named-default): Replace default import with named import. | ||
╭─[no_named_default.tsx:1:37] | ||
1 │ import { foo, default as bar } from "./bar"; | ||
· ─────── | ||
╰──── | ||
help: Forbid named default exports. | ||
|
||
⚠ eslint-plugin-import(no-named-default): Replace default import with named import. | ||
╭─[no_named_default.tsx:1:34] | ||
1 │ import { "default" as bar } from "./bar"; | ||
· ─────── | ||
╰──── | ||
help: Forbid named default exports. |