Skip to content

Commit 516d14e

Browse files
committed
fix(linter/no-namespace): skip comments when searching for token (#15716)
1 parent b5aaace commit 516d14e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

crates/oxc_linter/src/rules/typescript/no_namespace.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ impl Rule for NoNamespace {
133133
}
134134
}
135135

136-
#[expect(clippy::cast_possible_truncation)]
137136
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
138137
let AstKind::TSModuleDeclaration(declaration) = node.kind() else {
139138
return;
@@ -156,16 +155,14 @@ impl Rule for NoNamespace {
156155
return;
157156
}
158157

159-
let declaration_code = declaration.span.source_text(ctx.source_text());
160-
161158
let span = match declaration.kind {
162159
TSModuleDeclarationKind::Global => None, // handled above
163-
TSModuleDeclarationKind::Module => declaration_code
164-
.find("module")
165-
.map(|i| Span::sized(declaration.span.start + i as u32, 6)),
166-
TSModuleDeclarationKind::Namespace => declaration_code
167-
.find("namespace")
168-
.map(|i| Span::sized(declaration.span.start + i as u32, 9)),
160+
TSModuleDeclarationKind::Module => ctx
161+
.find_next_token_from(declaration.span.start, "module")
162+
.map(|i| Span::sized(declaration.span.start + i, 6)),
163+
TSModuleDeclarationKind::Namespace => ctx
164+
.find_next_token_from(declaration.span.start, "namespace")
165+
.map(|i| Span::sized(declaration.span.start + i, 9)),
169166
};
170167
if let Some(span) = span {
171168
ctx.diagnostic(no_namespace_diagnostic(span));
@@ -381,6 +378,8 @@ fn test() {
381378
}",
382379
Some(serde_json::json!([{ "allowDeclarations": true }])),
383380
),
381+
("declare /* module */ module foo {}", None),
382+
("declare /* namespace */ namespace foo {}", None),
384383
];
385384

386385
Tester::new(NoNamespace::NAME, NoNamespace::PLUGIN, pass, fail).test_and_snapshot();

crates/oxc_linter/src/snapshots/typescript_no_namespace.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,17 @@ source: crates/oxc_linter/src/tester.rs
276276
3export declare namespace C {}
277277
╰────
278278
help: Replace the namespace with an ES2015 module or use `declare module`
279+
280+
typescript-eslint(no-namespace): ES2015 module syntax is preferred over namespaces.
281+
╭─[no_namespace.tsx:1:22]
282+
1declare /* module */ module foo {}
283+
· ──────
284+
╰────
285+
help: Replace the namespace with an ES2015 module or use `declare module`
286+
287+
typescript-eslint(no-namespace): ES2015 module syntax is preferred over namespaces.
288+
╭─[no_namespace.tsx:1:25]
289+
1declare /* namespace */ namespace foo {}
290+
· ─────────
291+
╰────
292+
help: Replace the namespace with an ES2015 module or use `declare module`

0 commit comments

Comments
 (0)