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

fix(linter): improve prefer_namespace_keyword rule #4751

Merged
merged 10 commits into from
Aug 9, 2024
41 changes: 36 additions & 5 deletions crates/oxc_linter/src/rules/typescript/prefer_namespace_keyword.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc_ast::{
ast::{TSModuleDeclarationKind, TSModuleDeclarationName},
ast::{TSModuleDeclaration, TSModuleDeclarationKind, TSModuleDeclarationName},
AstKind,
};
use oxc_diagnostics::OxcDiagnostic;
Expand Down Expand Up @@ -34,16 +34,32 @@ declare_oxc_lint!(
fix
);

fn is_nest_module(node: &AstNode, ctx: &LintContext<'_>) -> bool {
ctx.nodes().parent_node(node.id()).map_or(false, |parent_node| is_valid_module(parent_node))
}

fn is_valid_module(node: &AstNode) -> bool {
matches!(node.kind(), AstKind::TSModuleDeclaration(module) if !is_invalid_module(module))
}

fn is_invalid_module(module: &TSModuleDeclaration) -> bool {
module.id.is_string_literal()
|| !matches!(module.id, TSModuleDeclarationName::Identifier(_))
|| module.kind != TSModuleDeclarationKind::Module
}

impl Rule for PreferNamespaceKeyword {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let AstKind::TSModuleDeclaration(module) = node.kind() else { return };
if module.id.is_string_literal()
|| !matches!(module.id, TSModuleDeclarationName::Identifier(_))
|| module.kind != TSModuleDeclarationKind::Module
{

if is_invalid_module(module) {
rzvxa marked this conversation as resolved.
Show resolved Hide resolved
return;
}

if is_nest_module(node, ctx) {
return ctx.diagnostic(prefer_namespace_keyword_diagnostic(module.span));
rzvxa marked this conversation as resolved.
Show resolved Hide resolved
}

ctx.diagnostic_with_fix(prefer_namespace_keyword_diagnostic(module.span), |fixer| {
let span_size = u32::try_from("module".len()).unwrap_or(6);
let span_start = if module.declare {
Expand Down Expand Up @@ -74,6 +90,7 @@ fn test() {

let fail = vec![
"module foo {}",
"module A.B {}",
"declare module foo {}",
"
declare module foo {
Expand All @@ -88,6 +105,20 @@ fn test() {

let fix = vec![
("module foo {}", "namespace foo {}", None),
("module A.B {}", "namespace A.B {}", None),
(
"
module A {
module B {}
}
rzvxa marked this conversation as resolved.
Show resolved Hide resolved
",
rzvxa marked this conversation as resolved.
Show resolved Hide resolved
"
namespace A {
namespace B {}
}
",
None,
),
("declare module foo {}", "declare namespace foo {}", None),
(
"
Expand Down
13 changes: 13 additions & 0 deletions crates/oxc_linter/src/snapshots/prefer_namespace_keyword.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ source: crates/oxc_linter/src/tester.rs
╰────
help: Replace `module` with `namespace`.

⚠ typescript-eslint(prefer-namespace-keyword): Use 'namespace' instead of 'module' to declare custom TypeScript modules.
╭─[prefer_namespace_keyword.tsx:1:1]
1 │ module A.B {}
· ─────────────
╰────
help: Replace `module` with `namespace`.

⚠ typescript-eslint(prefer-namespace-keyword): Use 'namespace' instead of 'module' to declare custom TypeScript modules.
╭─[prefer_namespace_keyword.tsx:1:10]
1 │ module A.B {}
· ────
╰────

⚠ typescript-eslint(prefer-namespace-keyword): Use 'namespace' instead of 'module' to declare custom TypeScript modules.
╭─[prefer_namespace_keyword.tsx:1:1]
1 │ declare module foo {}
Expand Down