Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions crates/oxc_linter/src/rules/typescript/no_namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ impl Rule for NoNamespace {
}
}

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

let declaration_code = declaration.span.source_text(ctx.source_text());

let span = match declaration.kind {
TSModuleDeclarationKind::Global => None, // handled above
TSModuleDeclarationKind::Module => declaration_code
.find("module")
.map(|i| Span::sized(declaration.span.start + i as u32, 6)),
TSModuleDeclarationKind::Namespace => declaration_code
.find("namespace")
.map(|i| Span::sized(declaration.span.start + i as u32, 9)),
TSModuleDeclarationKind::Module => ctx
.find_next_token_from(declaration.span.start, "module")
.map(|i| Span::sized(declaration.span.start + i, 6)),
TSModuleDeclarationKind::Namespace => ctx
.find_next_token_from(declaration.span.start, "namespace")
.map(|i| Span::sized(declaration.span.start + i, 9)),
};
if let Some(span) = span {
ctx.diagnostic(no_namespace_diagnostic(span));
Expand Down Expand Up @@ -381,6 +378,8 @@ fn test() {
}",
Some(serde_json::json!([{ "allowDeclarations": true }])),
),
("declare /* module */ module foo {}", None),
("declare /* namespace */ namespace foo {}", None),
];

Tester::new(NoNamespace::NAME, NoNamespace::PLUGIN, pass, fail).test_and_snapshot();
Expand Down
14 changes: 14 additions & 0 deletions crates/oxc_linter/src/snapshots/typescript_no_namespace.snap
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,17 @@ source: crates/oxc_linter/src/tester.rs
3 │ export declare namespace C {}
╰────
help: Replace the namespace with an ES2015 module or use `declare module`

⚠ typescript-eslint(no-namespace): ES2015 module syntax is preferred over namespaces.
╭─[no_namespace.tsx:1:22]
1 │ declare /* module */ module foo {}
· ──────
╰────
help: Replace the namespace with an ES2015 module or use `declare module`

⚠ typescript-eslint(no-namespace): ES2015 module syntax is preferred over namespaces.
╭─[no_namespace.tsx:1:25]
1 │ declare /* namespace */ namespace foo {}
· ─────────
╰────
help: Replace the namespace with an ES2015 module or use `declare module`
Loading