Skip to content

Commit

Permalink
fix(linter): improve the fixer of prefer-namespace-keyword (#6230)
Browse files Browse the repository at this point in the history
closes #6204
  • Loading branch information
shulaoda authored Oct 2, 2024
1 parent a089e19 commit ea28ee9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
35 changes: 14 additions & 21 deletions crates/oxc_linter/src/rules/typescript/prefer_namespace_keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use oxc_ast::{
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_span::{GetSpan, Span};

use crate::{
context::{ContextHost, LintContext},
Expand Down Expand Up @@ -38,38 +38,27 @@ 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_node(parent_node))
}

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

fn is_valid_module(module: &TSModuleDeclaration) -> bool {
!module.id.is_string_literal()
&& matches!(module.id, TSModuleDeclarationName::Identifier(_))
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 !is_valid_module(module) || is_nest_module(node, ctx) {
if !is_valid_module(module) {
return;
}

let token = ctx.source_range(Span::new(module.span.start, module.id.span().start));
let Some(offset) = token.find("module") else {
return;
};

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 {
module.span.start + u32::try_from("declare ".len()).unwrap_or(8)
} else {
module.span.start
};
fixer.replace(Span::sized(span_start, span_size), "namespace")
let span_start = module.span.start + u32::try_from(offset).unwrap();
fixer.replace(Span::sized(span_start, 6), "namespace")
});
}

Expand All @@ -88,6 +77,7 @@ fn test() {
"namespace foo {}",
"declare namespace foo {}",
"declare global {}",
"module''.s",
];

let fail = vec![
Expand All @@ -104,6 +94,7 @@ fn test() {
module foo {}
}
",
"module foo.'a'",
];

let fix = vec![
Expand Down Expand Up @@ -136,6 +127,8 @@ fn test() {
",
None,
),
("module foo.'a'", "namespace foo.'a'", None),
];

Tester::new(PreferNamespaceKeyword::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}
7 changes: 7 additions & 0 deletions crates/oxc_linter/src/snapshots/prefer_namespace_keyword.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ source: crates/oxc_linter/src/tester.rs
4 │ }
╰────
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]
1module foo.'a'
· ──────────────
╰────
help: Replace `module` with `namespace`.

0 comments on commit ea28ee9

Please sign in to comment.