From e5d76184575389a7df1da1ee6893016763100df2 Mon Sep 17 00:00:00 2001 From: Boshen Date: Sun, 20 Aug 2023 18:11:12 +0800 Subject: [PATCH] fix(linter): reduce the span of no-namespace to the keyword --- .../src/rules/eslint/no_extra_boolean_cast.rs | 2 +- .../src/rules/typescript/no_namespace.rs | 10 +- .../src/snapshots/no_namespace.snap | 273 +++++++----------- 3 files changed, 117 insertions(+), 168 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_extra_boolean_cast.rs b/crates/oxc_linter/src/rules/eslint/no_extra_boolean_cast.rs index cf675b130..e59039d00 100644 --- a/crates/oxc_linter/src/rules/eslint/no_extra_boolean_cast.rs +++ b/crates/oxc_linter/src/rules/eslint/no_extra_boolean_cast.rs @@ -77,7 +77,7 @@ impl Rule for NoExtraBooleanCast { let Some(parent) = get_real_parent(node, ctx) else { return }; if matches!(parent.kind(), AstKind::UnaryExpression(p) if p.operator == UnaryOperator::LogicalNot && is_flagged_ctx(parent, ctx, self.enforce_for_logical_operands)) { - ctx.diagnostic(NoExtraDoubleNegationCastDiagnostic(unary.span)); + ctx.diagnostic(NoExtraDoubleNegationCastDiagnostic(parent.kind().span())); } } _ => {} diff --git a/crates/oxc_linter/src/rules/typescript/no_namespace.rs b/crates/oxc_linter/src/rules/typescript/no_namespace.rs index 6e19223a3..63bb2a289 100644 --- a/crates/oxc_linter/src/rules/typescript/no_namespace.rs +++ b/crates/oxc_linter/src/rules/typescript/no_namespace.rs @@ -80,7 +80,15 @@ impl Rule for NoNamespace { return; } - ctx.diagnostic(NoNamespaceDiagnostic(declaration.span)); + let start = declaration.span.start; + let span = Span::new(start, declaration.span.start + 6); // "module".len() + let modifier = span.source_text(ctx.source_text()); + let span = if modifier == "module" { + span + } else { + Span::new(start, declaration.span.start + 9) // "namespace".len() + }; + ctx.diagnostic(NoNamespaceDiagnostic(span)); } } diff --git a/crates/oxc_linter/src/snapshots/no_namespace.snap b/crates/oxc_linter/src/snapshots/no_namespace.snap index ecbd7f893..658e62347 100644 --- a/crates/oxc_linter/src/snapshots/no_namespace.snap +++ b/crates/oxc_linter/src/snapshots/no_namespace.snap @@ -1,356 +1,297 @@ --- source: crates/oxc_linter/src/tester.rs -assertion_line: 101 expression: no_namespace --- ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] 1 │ module foo {} - · ───────────── + · ────── ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] 1 │ namespace foo {} - · ──────────────── + · ───────── ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] 1 │ module foo {} - · ───────────── + · ────── ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] 1 │ namespace foo {} - · ──────────────── + · ───────── ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] 1 │ module foo {} - · ───────────── + · ────── ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] 1 │ namespace foo {} - · ──────────────── + · ───────── ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] 1 │ declare module foo {} - · ───────────── + · ────── ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] 1 │ declare namespace foo {} - · ──────────────── + · ───────── ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] 1 │ declare module foo {} - · ───────────── + · ────── ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] 1 │ declare namespace foo {} - · ──────────────── + · ───────── ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] 1 │ namespace Foo.Bar {} - · ──────────────────── + · ───────── ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ namespace Foo.Bar { - 3 │ │ namespace Baz.Bas { - 4 │ │ interface X {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ namespace Foo.Bar { + · ───────── + 3 │ namespace Baz.Bas { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:2:1] - 2 │ namespace Foo.Bar { - 3 │ ╭─▶ namespace Baz.Bas { - 4 │ │ interface X {} - 5 │ ╰─▶ } - 6 │ } + 2 │ namespace Foo.Bar { + 3 │ namespace Baz.Bas { + · ───────── + 4 │ interface X {} ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ namespace A { - 3 │ │ namespace B { - 4 │ │ declare namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ namespace A { + · ───────── + 3 │ namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:2:1] - 2 │ namespace A { - 3 │ ╭─▶ namespace B { - 4 │ │ declare namespace C {} - 5 │ ╰─▶ } - 6 │ } + 2 │ namespace A { + 3 │ namespace B { + · ───────── + 4 │ declare namespace C {} ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ namespace A { - 3 │ │ namespace B { - 4 │ │ export declare namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ namespace A { + · ───────── + 3 │ namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:2:1] - 2 │ namespace A { - 3 │ ╭─▶ namespace B { - 4 │ │ export declare namespace C {} - 5 │ ╰─▶ } - 6 │ } + 2 │ namespace A { + 3 │ namespace B { + · ───────── + 4 │ export declare namespace C {} ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ namespace A { - 3 │ │ declare namespace B { - 4 │ │ namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ namespace A { + · ───────── + 3 │ declare namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ namespace A { - 3 │ │ export declare namespace B { - 4 │ │ namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ namespace A { + · ───────── + 3 │ export declare namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ namespace A { - 3 │ │ export declare namespace B { - 4 │ │ declare namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ namespace A { + · ───────── + 3 │ export declare namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ namespace A { - 3 │ │ export declare namespace B { - 4 │ │ export declare namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ namespace A { + · ───────── + 3 │ export declare namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ namespace A { - 3 │ │ declare namespace B { - 4 │ │ export declare namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ namespace A { + · ───────── + 3 │ declare namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ namespace A { - 3 │ │ export namespace B { - 4 │ │ export declare namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ namespace A { + · ───────── + 3 │ export namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:2:1] - 2 │ namespace A { - 3 │ ╭─▶ export namespace B { - 4 │ │ export declare namespace C {} - 5 │ ╰─▶ } - 6 │ } + 2 │ namespace A { + 3 │ export namespace B { + · ───────── + 4 │ export declare namespace C {} ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ export namespace A { - 3 │ │ namespace B { - 4 │ │ declare namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ export namespace A { + · ───────── + 3 │ namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:2:1] - 2 │ export namespace A { - 3 │ ╭─▶ namespace B { - 4 │ │ declare namespace C {} - 5 │ ╰─▶ } - 6 │ } + 2 │ export namespace A { + 3 │ namespace B { + · ───────── + 4 │ declare namespace C {} ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ export namespace A { - 3 │ │ namespace B { - 4 │ │ export declare namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ export namespace A { + · ───────── + 3 │ namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:2:1] - 2 │ export namespace A { - 3 │ ╭─▶ namespace B { - 4 │ │ export declare namespace C {} - 5 │ ╰─▶ } - 6 │ } + 2 │ export namespace A { + 3 │ namespace B { + · ───────── + 4 │ export declare namespace C {} ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ export namespace A { - 3 │ │ declare namespace B { - 4 │ │ namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ export namespace A { + · ───────── + 3 │ declare namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ export namespace A { - 3 │ │ export declare namespace B { - 4 │ │ namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ export namespace A { + · ───────── + 3 │ export declare namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ export namespace A { - 3 │ │ export declare namespace B { - 4 │ │ declare namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ export namespace A { + · ───────── + 3 │ export declare namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ export namespace A { - 3 │ │ export declare namespace B { - 4 │ │ export declare namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ export namespace A { + · ───────── + 3 │ export declare namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ export namespace A { - 3 │ │ declare namespace B { - 4 │ │ export declare namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ export namespace A { + · ───────── + 3 │ declare namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:1:1] - 1 │ - 2 │ ╭─▶ export namespace A { - 3 │ │ export namespace B { - 4 │ │ export declare namespace C {} - 5 │ │ } - 6 │ ╰─▶ } - 7 │ + 1 │ + 2 │ export namespace A { + · ───────── + 3 │ export namespace B { ╰──── help: Replace the namespace with an ES2015 module ⚠ ES2015 module syntax is preferred over namespaces. ╭─[no_namespace.tsx:2:1] - 2 │ export namespace A { - 3 │ ╭─▶ export namespace B { - 4 │ │ export declare namespace C {} - 5 │ ╰─▶ } - 6 │ } + 2 │ export namespace A { + 3 │ export namespace B { + · ───────── + 4 │ export declare namespace C {} ╰──── help: Replace the namespace with an ES2015 module