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
24 changes: 9 additions & 15 deletions crates/oxc_linter/src/rules/typescript/no_namespace.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use oxc_ast::{
AstKind,
ast::{TSModuleDeclarationKind, TSModuleDeclarationName},
};
use oxc_ast::{AstKind, ast::TSModuleDeclarationName};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
Expand Down Expand Up @@ -141,6 +138,8 @@ impl Rule for NoNamespace {
return;
}

// Ignore nested `TSModuleDeclaration`s
// e.g. the 2 inner `TSModuleDeclaration`s in `module A.B.C {}`
if let AstKind::TSModuleDeclaration(_) = ctx.nodes().parent_kind(node.id()) {
return;
}
Expand All @@ -151,17 +150,12 @@ impl Rule for NoNamespace {
return;
}

let span = match declaration.kind {
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));
}
let keyword = declaration.kind.as_str();
let mut span_start = declaration.span.start;
span_start += ctx.find_next_token_from(span_start, keyword).unwrap();
#[expect(clippy::cast_possible_truncation)]
let span = Span::sized(span_start, keyword.len() as u32);
ctx.diagnostic(no_namespace_diagnostic(span));
}

fn should_run(&self, ctx: &ContextHost) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@ impl Rule for PreferNamespaceKeyword {
return;
}

let Some(offset) = ctx.find_next_token_from(module.span.start, "module") else { return };

ctx.diagnostic_with_fix(prefer_namespace_keyword_diagnostic(module.span), |fixer| {
let span_start = module.span.start + offset;
let mut span_start = module.span.start;
span_start += ctx.find_next_token_from(span_start, "module").unwrap();
fixer.replace(Span::sized(span_start, 6), "namespace")
});
}
Expand Down
Loading