Skip to content

Commit 9f7cbde

Browse files
committed
fix(linter): consistent-function-scoping false positive with hoisted var declarations
The rule was incorrectly starting from the scope containing the variable binding instead of the function's own scope. This caused false positives when var declarations were hoisted to a parent scope while the function captured variables from its lexical scope. Fixes the issue by using the function's own scope to determine parent scopes.
1 parent 85a34ce commit 9f7cbde

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,13 @@ impl Rule for ConsistentFunctionScoping {
181181
}
182182

183183
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
184-
let (function_declaration_symbol_id, function_name, function_body, reporter_span) =
184+
let (
185+
function_declaration_symbol_id,
186+
function_name,
187+
function_body,
188+
reporter_span,
189+
function_scope_id,
190+
) =
185191
match node.kind() {
186192
AstKind::Function(function) => {
187193
if function.is_typescript_syntax() {
@@ -215,13 +221,15 @@ impl Rule for ConsistentFunctionScoping {
215221
Span::sized(function.span.start, 8),
216222
|func_binding_ident| func_binding_ident.span,
217223
),
224+
func_scope_id,
218225
)
219226
} else if let Some(function_id) = &function.id {
220227
(
221228
function_id.symbol_id(),
222229
Some(function_id.name),
223230
function_body,
224231
function_id.span(),
232+
func_scope_id,
225233
)
226234
} else {
227235
return;
@@ -237,6 +245,7 @@ impl Rule for ConsistentFunctionScoping {
237245
Some(binding_ident.name),
238246
&arrow_function.body,
239247
binding_ident.span(),
248+
arrow_function.scope_id(),
240249
)
241250
}
242251
_ => return,
@@ -273,10 +282,8 @@ impl Rule for ConsistentFunctionScoping {
273282
}
274283

275284
let parent_scope_ids = {
276-
let mut current_scope_id =
277-
ctx.scoping().symbol_scope_id(function_declaration_symbol_id);
285+
let mut current_scope_id = function_scope_id;
278286
let mut parent_scope_ids = FxHashSet::default();
279-
parent_scope_ids.insert(current_scope_id);
280287
while let Some(parent_scope_id) = ctx.scoping().scope_parent_id(current_scope_id) {
281288
parent_scope_ids.insert(parent_scope_id);
282289
current_scope_id = parent_scope_id;
@@ -749,6 +756,7 @@ fn test() {
749756
",
750757
None,
751758
),
759+
("function outer() { { let x; var inner = () => x; } return inner; }", None),
752760
];
753761

754762
let fail = vec![

0 commit comments

Comments
 (0)