Skip to content

Commit ea28ee9

Browse files
authoredOct 2, 2024··
fix(linter): improve the fixer of prefer-namespace-keyword (#6230)
closes #6204
1 parent a089e19 commit ea28ee9

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed
 

‎crates/oxc_linter/src/rules/typescript/prefer_namespace_keyword.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use oxc_ast::{
44
};
55
use oxc_diagnostics::OxcDiagnostic;
66
use oxc_macros::declare_oxc_lint;
7-
use oxc_span::Span;
7+
use oxc_span::{GetSpan, Span};
88

99
use crate::{
1010
context::{ContextHost, LintContext},
@@ -38,38 +38,27 @@ declare_oxc_lint!(
3838
fix
3939
);
4040

41-
fn is_nest_module(node: &AstNode, ctx: &LintContext<'_>) -> bool {
42-
ctx.nodes()
43-
.parent_node(node.id())
44-
.map_or(false, |parent_node| is_valid_module_node(parent_node))
45-
}
46-
47-
fn is_valid_module_node(node: &AstNode) -> bool {
48-
matches!(node.kind(), AstKind::TSModuleDeclaration(module) if is_valid_module(module))
49-
}
50-
5141
fn is_valid_module(module: &TSModuleDeclaration) -> bool {
52-
!module.id.is_string_literal()
53-
&& matches!(module.id, TSModuleDeclarationName::Identifier(_))
42+
matches!(module.id, TSModuleDeclarationName::Identifier(_))
5443
&& module.kind == TSModuleDeclarationKind::Module
5544
}
5645

5746
impl Rule for PreferNamespaceKeyword {
5847
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
5948
let AstKind::TSModuleDeclaration(module) = node.kind() else { return };
6049

61-
if !is_valid_module(module) || is_nest_module(node, ctx) {
50+
if !is_valid_module(module) {
6251
return;
6352
}
6453

54+
let token = ctx.source_range(Span::new(module.span.start, module.id.span().start));
55+
let Some(offset) = token.find("module") else {
56+
return;
57+
};
58+
6559
ctx.diagnostic_with_fix(prefer_namespace_keyword_diagnostic(module.span), |fixer| {
66-
let span_size = u32::try_from("module".len()).unwrap_or(6);
67-
let span_start = if module.declare {
68-
module.span.start + u32::try_from("declare ".len()).unwrap_or(8)
69-
} else {
70-
module.span.start
71-
};
72-
fixer.replace(Span::sized(span_start, span_size), "namespace")
60+
let span_start = module.span.start + u32::try_from(offset).unwrap();
61+
fixer.replace(Span::sized(span_start, 6), "namespace")
7362
});
7463
}
7564

@@ -88,6 +77,7 @@ fn test() {
8877
"namespace foo {}",
8978
"declare namespace foo {}",
9079
"declare global {}",
80+
"module''.s",
9181
];
9282

9383
let fail = vec![
@@ -104,6 +94,7 @@ fn test() {
10494
module foo {}
10595
}
10696
",
97+
"module foo.'a'",
10798
];
10899

109100
let fix = vec![
@@ -136,6 +127,8 @@ fn test() {
136127
",
137128
None,
138129
),
130+
("module foo.'a'", "namespace foo.'a'", None),
139131
];
132+
140133
Tester::new(PreferNamespaceKeyword::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
141134
}

‎crates/oxc_linter/src/snapshots/prefer_namespace_keyword.snap

+7
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,10 @@ source: crates/oxc_linter/src/tester.rs
4949
4 │ }
5050
╰────
5151
help: Replace `module` with `namespace`.
52+
53+
typescript-eslint(prefer-namespace-keyword): Use 'namespace' instead of 'module' to declare custom TypeScript modules.
54+
╭─[prefer_namespace_keyword.tsx:1:1]
55+
1module foo.'a'
56+
· ──────────────
57+
╰────
58+
help: Replace `module` with `namespace`.

0 commit comments

Comments
 (0)
Please sign in to comment.