|
| 1 | +use oxc_ast::{ |
| 2 | + AstKind, |
| 3 | + ast::{ExportNamedDeclaration, ImportDeclaration, ImportDeclarationSpecifier}, |
| 4 | +}; |
| 5 | +use oxc_diagnostics::OxcDiagnostic; |
| 6 | +use oxc_macros::declare_oxc_lint; |
| 7 | +use oxc_span::Span; |
| 8 | + |
| 9 | +use crate::{ |
| 10 | + AstNode, |
| 11 | + context::LintContext, |
| 12 | + fixer::{RuleFix, RuleFixer}, |
| 13 | + rule::Rule, |
| 14 | +}; |
| 15 | + |
| 16 | +fn require_module_specifiers_diagnostic(span: Span, statement_type: &str) -> OxcDiagnostic { |
| 17 | + OxcDiagnostic::warn(format!("Empty {} specifier is not allowed", statement_type)) |
| 18 | + .with_help("Remove empty braces") |
| 19 | + .with_label(span) |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Debug, Default, Clone)] |
| 23 | +pub struct RequireModuleSpecifiers; |
| 24 | + |
| 25 | +declare_oxc_lint!( |
| 26 | + /// ### What it does |
| 27 | + /// |
| 28 | + /// Enforce non-empty specifier list in `import` and `export` statements. |
| 29 | + /// |
| 30 | + /// ### Why is this bad? |
| 31 | + /// |
| 32 | + /// Empty import/export specifiers add no value and can be confusing. |
| 33 | + /// If you want to import a module for side effects, use `import 'module'` instead. |
| 34 | + /// |
| 35 | + /// ### Examples |
| 36 | + /// |
| 37 | + /// Examples of **incorrect** code for this rule: |
| 38 | + /// ```js |
| 39 | + /// import {} from 'foo'; |
| 40 | + /// import foo, {} from 'foo'; |
| 41 | + /// export {} from 'foo'; |
| 42 | + /// export {}; |
| 43 | + /// ``` |
| 44 | + /// |
| 45 | + /// Examples of **correct** code for this rule: |
| 46 | + /// ```js |
| 47 | + /// import 'foo'; |
| 48 | + /// import foo from 'foo'; |
| 49 | + /// ``` |
| 50 | + RequireModuleSpecifiers, |
| 51 | + unicorn, |
| 52 | + correctness, |
| 53 | + fix |
| 54 | +); |
| 55 | + |
| 56 | +impl Rule for RequireModuleSpecifiers { |
| 57 | + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { |
| 58 | + match node.kind() { |
| 59 | + AstKind::ImportDeclaration(import_decl) => { |
| 60 | + let Some(span) = find_empty_braces_in_import(ctx, import_decl) else { |
| 61 | + return; |
| 62 | + }; |
| 63 | + ctx.diagnostic_with_fix( |
| 64 | + require_module_specifiers_diagnostic(span, "import"), |
| 65 | + |fixer| fix_import(fixer, import_decl), |
| 66 | + ); |
| 67 | + } |
| 68 | + AstKind::ExportNamedDeclaration(export_decl) => { |
| 69 | + if export_decl.declaration.is_none() && export_decl.specifiers.is_empty() { |
| 70 | + let span = |
| 71 | + find_empty_braces_in_export(ctx, export_decl).unwrap_or(export_decl.span); |
| 72 | + ctx.diagnostic(require_module_specifiers_diagnostic(span, "export")); |
| 73 | + } |
| 74 | + } |
| 75 | + _ => {} |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Finds empty braces `{}` in the given text and returns their span |
| 81 | +fn find_empty_braces_in_text(text: &str, base_span: Span) -> Option<Span> { |
| 82 | + let open_brace = text.find('{')?; |
| 83 | + let close_brace = text[open_brace + 1..].find('}')?; |
| 84 | + |
| 85 | + // Check if braces contain only whitespace |
| 86 | + if !text[open_brace + 1..open_brace + 1 + close_brace].trim().is_empty() { |
| 87 | + return None; |
| 88 | + } |
| 89 | + |
| 90 | + // Calculate absolute positions |
| 91 | + let start = base_span.start + u32::try_from(open_brace).ok()?; |
| 92 | + let end = start + u32::try_from(close_brace + 1).ok()? + 1; // +2 for '{' and '}' |
| 93 | + Some(Span::new(start, end)) |
| 94 | +} |
| 95 | + |
| 96 | +fn find_empty_braces_in_import( |
| 97 | + ctx: &LintContext<'_>, |
| 98 | + import_decl: &ImportDeclaration<'_>, |
| 99 | +) -> Option<Span> { |
| 100 | + // Side-effect imports don't have specifiers |
| 101 | + let specifiers = import_decl.specifiers.as_ref()?; |
| 102 | + |
| 103 | + // Check for patterns that could have empty braces |
| 104 | + let could_have_empty_braces = matches!( |
| 105 | + specifiers.as_slice(), |
| 106 | + [] | [ImportDeclarationSpecifier::ImportDefaultSpecifier(_)] |
| 107 | + ); |
| 108 | + |
| 109 | + if !could_have_empty_braces { |
| 110 | + return None; |
| 111 | + } |
| 112 | + |
| 113 | + let import_text = ctx.source_range(import_decl.span); |
| 114 | + find_empty_braces_in_text(import_text, import_decl.span) |
| 115 | +} |
| 116 | + |
| 117 | +fn find_empty_braces_in_export( |
| 118 | + ctx: &LintContext<'_>, |
| 119 | + export_decl: &ExportNamedDeclaration<'_>, |
| 120 | +) -> Option<Span> { |
| 121 | + let export_text = ctx.source_range(export_decl.span); |
| 122 | + find_empty_braces_in_text(export_text, export_decl.span) |
| 123 | +} |
| 124 | + |
| 125 | +fn fix_import<'a>(fixer: RuleFixer<'_, 'a>, import_decl: &ImportDeclaration<'a>) -> RuleFix<'a> { |
| 126 | + let import_text = fixer.source_range(import_decl.span); |
| 127 | + |
| 128 | + // Only fix `import foo, {} from 'bar'` - safe removal of empty braces |
| 129 | + let Some(comma_pos) = import_text.find(',') else { |
| 130 | + return fixer.noop(); // Don't fix `import {} from 'foo'` - changes behavior |
| 131 | + }; |
| 132 | + let Some(from_pos) = import_text[comma_pos..].find("from") else { |
| 133 | + return fixer.noop(); |
| 134 | + }; |
| 135 | + |
| 136 | + // Remove empty braces: "import foo, {} from 'bar'" -> "import foo from 'bar'" |
| 137 | + let default_part = &import_text[..comma_pos]; |
| 138 | + let from_part = &import_text[comma_pos + from_pos..]; |
| 139 | + fixer.replace(import_decl.span, format!("{default_part} {from_part}")) |
| 140 | +} |
| 141 | + |
| 142 | +#[test] |
| 143 | +fn test() { |
| 144 | + use crate::tester::Tester; |
| 145 | + |
| 146 | + let pass = vec![ |
| 147 | + r#"import "foo""#, |
| 148 | + r#"import foo from "foo""#, |
| 149 | + r#"import * as foo from "foo""#, |
| 150 | + r#"import {foo} from "foo""#, |
| 151 | + r#"import foo,{bar} from "foo""#, |
| 152 | + r#"import type foo from "foo""#, |
| 153 | + r#"import type foo,{bar} from "foo""#, |
| 154 | + r#"import foo,{type bar} from "foo""#, |
| 155 | + "const foo = 1; |
| 156 | + export {foo};", |
| 157 | + r#"export {foo} from "foo""#, |
| 158 | + r#"export * as foo from "foo""#, |
| 159 | + r"export type {Foo}", |
| 160 | + r"export type foo = Foo", |
| 161 | + r#"export type {foo} from "foo""#, |
| 162 | + r#"export type * as foo from "foo""#, |
| 163 | + "export const foo = 1", |
| 164 | + "export function foo() {}", |
| 165 | + "export class foo {}", |
| 166 | + "export const {} = foo", |
| 167 | + "export const [] = foo", |
| 168 | + ]; |
| 169 | + |
| 170 | + let fail = vec![ |
| 171 | + r#"import {} from "foo";"#, |
| 172 | + r#"import{}from"foo";"#, |
| 173 | + r#"import { |
| 174 | + } from "foo";"#, |
| 175 | + r#"import foo, {} from "foo";"#, |
| 176 | + r#"import foo,{}from "foo";"#, |
| 177 | + r#"import foo, { |
| 178 | + } from "foo";"#, |
| 179 | + r#"import foo,{}/* comment */from "foo";"#, |
| 180 | + r#"import type {} from "foo""#, |
| 181 | + r#"import type{}from"foo""#, |
| 182 | + r#"import type foo, {} from "foo""#, |
| 183 | + r#"import type foo,{}from "foo""#, |
| 184 | + "export {}", |
| 185 | + r#"export {} from "foo";"#, |
| 186 | + r#"export{}from"foo";"#, |
| 187 | + r#"export { |
| 188 | + } from "foo";"#, |
| 189 | + r#"export {} from "foo" with {type: "json"};"#, |
| 190 | + r"export type{}", |
| 191 | + r#"export type {} from "foo""#, |
| 192 | + ]; |
| 193 | + |
| 194 | + let fix = vec![ |
| 195 | + (r#"import foo, {} from "foo";"#, r#"import foo from "foo";"#), |
| 196 | + (r#"import foo,{} from "foo";"#, r#"import foo from "foo";"#), |
| 197 | + ]; |
| 198 | + |
| 199 | + Tester::new(RequireModuleSpecifiers::NAME, RequireModuleSpecifiers::PLUGIN, pass, fail) |
| 200 | + .expect_fix(fix) |
| 201 | + .test_and_snapshot(); |
| 202 | +} |
0 commit comments