diff --git a/crates/oxc_linter/src/rules/eslint/no_inner_declarations.rs b/crates/oxc_linter/src/rules/eslint/no_inner_declarations.rs index 98be0f3c0..999dd9f3e 100644 --- a/crates/oxc_linter/src/rules/eslint/no_inner_declarations.rs +++ b/crates/oxc_linter/src/rules/eslint/no_inner_declarations.rs @@ -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)); } } diff --git a/crates/oxc_linter/src/snapshots/no_inner_declarations.snap b/crates/oxc_linter/src/snapshots/no_inner_declarations.snap index 1d397a2f1..d39f2b5da 100644 --- a/crates/oxc_linter/src/snapshots/no_inner_declarations.snap +++ b/crates/oxc_linter/src/snapshots/no_inner_declarations.snap @@ -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