mirror of
https://github.com/danbulant/oxc
synced 2026-05-22 13:48:55 +00:00
fix(linter): improve the fixer of prefer-namespace-keyword (#6230)
closes #6204
This commit is contained in:
parent
a089e19cf9
commit
ea28ee9eda
2 changed files with 21 additions and 21 deletions
|
|
@ -4,7 +4,7 @@ use oxc_ast::{
|
||||||
};
|
};
|
||||||
use oxc_diagnostics::OxcDiagnostic;
|
use oxc_diagnostics::OxcDiagnostic;
|
||||||
use oxc_macros::declare_oxc_lint;
|
use oxc_macros::declare_oxc_lint;
|
||||||
use oxc_span::Span;
|
use oxc_span::{GetSpan, Span};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
context::{ContextHost, LintContext},
|
context::{ContextHost, LintContext},
|
||||||
|
|
@ -38,19 +38,8 @@ declare_oxc_lint!(
|
||||||
fix
|
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 {
|
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
|
&& module.kind == TSModuleDeclarationKind::Module
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,18 +47,18 @@ impl Rule for PreferNamespaceKeyword {
|
||||||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
|
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
|
||||||
let AstKind::TSModuleDeclaration(module) = node.kind() else { return };
|
let AstKind::TSModuleDeclaration(module) = node.kind() else { return };
|
||||||
|
|
||||||
if !is_valid_module(module) || is_nest_module(node, ctx) {
|
if !is_valid_module(module) {
|
||||||
return;
|
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| {
|
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 = module.span.start + u32::try_from(offset).unwrap();
|
||||||
let span_start = if module.declare {
|
fixer.replace(Span::sized(span_start, 6), "namespace")
|
||||||
module.span.start + u32::try_from("declare ".len()).unwrap_or(8)
|
|
||||||
} else {
|
|
||||||
module.span.start
|
|
||||||
};
|
|
||||||
fixer.replace(Span::sized(span_start, span_size), "namespace")
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -88,6 +77,7 @@ fn test() {
|
||||||
"namespace foo {}",
|
"namespace foo {}",
|
||||||
"declare namespace foo {}",
|
"declare namespace foo {}",
|
||||||
"declare global {}",
|
"declare global {}",
|
||||||
|
"module''.s",
|
||||||
];
|
];
|
||||||
|
|
||||||
let fail = vec![
|
let fail = vec![
|
||||||
|
|
@ -104,6 +94,7 @@ fn test() {
|
||||||
module foo {}
|
module foo {}
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
|
"module foo.'a'",
|
||||||
];
|
];
|
||||||
|
|
||||||
let fix = vec![
|
let fix = vec![
|
||||||
|
|
@ -136,6 +127,8 @@ fn test() {
|
||||||
",
|
",
|
||||||
None,
|
None,
|
||||||
),
|
),
|
||||||
|
("module foo.'a'", "namespace foo.'a'", None),
|
||||||
];
|
];
|
||||||
|
|
||||||
Tester::new(PreferNamespaceKeyword::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
|
Tester::new(PreferNamespaceKeyword::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,3 +49,10 @@ source: crates/oxc_linter/src/tester.rs
|
||||||
4 │ }
|
4 │ }
|
||||||
╰────
|
╰────
|
||||||
help: Replace `module` with `namespace`.
|
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]
|
||||||
|
1 │ module foo.'a'
|
||||||
|
· ──────────────
|
||||||
|
╰────
|
||||||
|
help: Replace `module` with `namespace`.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue