Skip to content

Commit 989634a

Browse files
committed
fix(linter/no-inner-declaration): false negative with for loops (#11692)
fixes #11634
1 parent 219adcc commit 989634a

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

crates/oxc_linter/src/rules/eslint/no_inner_declarations.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use oxc_ast::AstKind;
22
use oxc_diagnostics::OxcDiagnostic;
33
use oxc_macros::declare_oxc_lint;
4-
use oxc_span::{GetSpan, Span};
4+
use oxc_span::Span;
55

66
use crate::{AstNode, context::LintContext, rule::Rule};
77

@@ -90,17 +90,6 @@ impl Rule for NoInnerDeclarations {
9090
| AstKind::StaticBlock(_)
9191
| AstKind::ExportNamedDeclaration(_)
9292
| AstKind::ExportDefaultDeclaration(_) => return,
93-
AstKind::ForStatement(for_stmt) => {
94-
if for_stmt.init.as_ref().is_some_and(|init| init.span() == kind.span()) {
95-
return;
96-
}
97-
}
98-
AstKind::ForInStatement(for_stmt) if for_stmt.left.span() == kind.span() => {
99-
return;
100-
}
101-
AstKind::ForOfStatement(for_stmt) if for_stmt.left.span() == kind.span() => {
102-
return;
103-
}
10493
_ => {}
10594
}
10695

@@ -167,9 +156,6 @@ fn test() {
167156
("class C { method() { var x; } }", Some(serde_json::json!(["both"]))),
168157
("class C { static { function foo() {} } }", Some(serde_json::json!(["both"]))),
169158
("class C { static { var x; } }", Some(serde_json::json!(["both"]))),
170-
("for (var x in {}) {}", Some(serde_json::json!(["both"]))),
171-
("for (var x of []) {}", Some(serde_json::json!(["both"]))),
172-
("for (var x = 1; a < 10; a++) {}", Some(serde_json::json!(["both"]))),
173159
("for (const x in {}) { let y = 5; }", Some(serde_json::json!(["both"]))),
174160
("for (const x of []) { let y = 5; }", Some(serde_json::json!(["both"]))),
175161
("for (const x = 1; a < 10; a++) { let y = 5; }", Some(serde_json::json!(["both"]))),
@@ -214,6 +200,9 @@ fn test() {
214200
("for (const x in {}) var y = 5;", Some(serde_json::json!(["both"]))),
215201
("for (const x of []) var y = 5;", Some(serde_json::json!(["both"]))),
216202
("for (const x = 1; a < 10; a++) var y = 5;", Some(serde_json::json!(["both"]))),
203+
("for (var x in {}) {}", Some(serde_json::json!(["both"]))),
204+
("for (var x of []) {}", Some(serde_json::json!(["both"]))),
205+
("for (var x = 1; a < 10; a++) {}", Some(serde_json::json!(["both"]))),
217206
];
218207

219208
Tester::new(NoInnerDeclarations::NAME, NoInnerDeclarations::PLUGIN, pass, fail)

crates/oxc_linter/src/snapshots/eslint_no_inner_declarations.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,24 @@ source: crates/oxc_linter/src/tester.rs
196196
· ───
197197
╰────
198198
help: Move variable declaration to program root
199+
200+
eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
201+
╭─[no_inner_declarations.tsx:1:6]
202+
1for (var x in {}) {}
203+
· ───
204+
╰────
205+
help: Move variable declaration to program root
206+
207+
eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
208+
╭─[no_inner_declarations.tsx:1:6]
209+
1for (var x of []) {}
210+
· ───
211+
╰────
212+
help: Move variable declaration to program root
213+
214+
eslint(no-inner-declarations): Variable or `function` declarations are not allowed in nested blocks
215+
╭─[no_inner_declarations.tsx:1:6]
216+
1for (var x = 1; a < 10; a++) {}
217+
· ───
218+
╰────
219+
help: Move variable declaration to program root

0 commit comments

Comments
 (0)