fix(linter): improve the span for no-inner-declarations

This commit is contained in:
Boshen 2023-07-28 13:18:28 +08:00
parent e4020d6fb2
commit f5c9908550
2 changed files with 33 additions and 28 deletions

View file

@ -4,7 +4,7 @@ use oxc_diagnostics::{
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_span::Span;
use crate::{context::LintContext, rule::Rule, AstNode};
@ -64,12 +64,17 @@ impl Rule for NoInnerDeclarations {
}
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
match node.kind() {
let span = match node.kind() {
AstKind::VariableDeclaration(decl)
if decl.kind.is_var() && self.config == NoInnerDeclarationsConfig::Both => {}
AstKind::Function(func) if func.is_function_declaration() => {}
if decl.kind.is_var() && self.config == NoInnerDeclarationsConfig::Both =>
{
Span::new(decl.span.start, decl.span.start + 3) // 3 for "var".len()
}
AstKind::Function(func) if func.is_function_declaration() => {
Span::new(func.span.start, func.span.start + 8) // 8 for "function".len()
}
_ => return,
}
};
let mut parent = ctx.nodes().parent_node(node.id());
if let Some(parent_node) = parent {
@ -120,7 +125,7 @@ impl Rule for NoInnerDeclarations {
}
}
ctx.diagnostic(NoInnerDeclarationsDiagnostic(decl_type, body, node.kind().span()));
ctx.diagnostic(NoInnerDeclarationsDiagnostic(decl_type, body, span));
}
}

View file

@ -5,154 +5,154 @@ expression: no_inner_declarations
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ if (test) { function doSomething() { } }
· ──────────────────────────
· ────────
╰────
help: Move function declaration to program root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ if (foo) var a;
· ──────
· ───
╰────
help: Move variable declaration to program root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ if (foo) /* some comments */ var a;
· ──────
· ───
╰────
help: Move variable declaration to program root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ if (foo){ function f(){ if(bar){ var a; } } }
· ─────────────────────────────────
· ────────
╰────
help: Move function declaration to program root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ if (foo){ function f(){ if(bar){ var a; } } }
· ──────
· ───
╰────
help: Move variable declaration to function body root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ if (foo) function f(){ if(bar) var a; }
· ──────────────────────────────
· ────────
╰────
help: Move function declaration to program root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ if (foo) function f(){ if(bar) var a; }
· ──────
· ───
╰────
help: Move variable declaration to function body root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ if (foo) { var fn = function(){} }
· ─────────────────────
· ───
╰────
help: Move variable declaration to program root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ if (foo) function f(){}
· ──────────────
· ────────
╰────
help: Move function declaration to program root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ function bar() { if (foo) function f(){}; }
· ──────────────
· ────────
╰────
help: Move function declaration to function body root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ function bar() { if (foo) var a; }
· ──────
· ───
╰────
help: Move variable declaration to function body root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ if (foo){ var a; }
· ──────
· ───
╰────
help: Move variable declaration to program root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ function doSomething() { do { function somethingElse() { } } while (test); }
· ────────────────────────────
· ────────
╰────
help: Move function declaration to function body root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ (function() { if (test) { function doSomething() { } } }());
· ──────────────────────────
· ────────
╰────
help: Move function declaration to function body root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ while (test) { var foo; }
· ────────
· ───
╰────
help: Move variable declaration to program root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ function doSomething() { if (test) { var foo = 42; } }
· ─────────────
· ───
╰────
help: Move variable declaration to function body root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ (function() { if (test) { var foo; } }());
· ────────
· ───
╰────
help: Move variable declaration to function body root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ const doSomething = () => { if (test) { var foo = 42; } }
· ─────────────
· ───
╰────
help: Move variable declaration to program root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ class C { method() { if(test) { var foo; } } }
· ────────
· ───
╰────
help: Move variable declaration to function body root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ class C { static { if (test) { function foo() {} } } }
· ─────────────────
· ────────
╰────
help: Move function declaration to class static block body root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ class C { static { if (test) { var foo; } } }
· ────────
· ───
╰────
help: Move variable declaration to class static block body root
⚠ eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
╭─[no_inner_declarations.tsx:1:1]
1 │ class C { static { if (test) { if (anotherTest) { var foo; } } } }
· ────────
· ───
╰────
help: Move variable declaration to class static block body root