Skip to content

Commit e58a0b0

Browse files
committed
fix(linter): panic in unicorn/consistent-function-scoping (#11772)
1 parent 81ef443 commit e58a0b0

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ fn consistent_function_scoping(
2121
parent_scope_kind: Option<&'static str>,
2222
function_name: Option<Atom<'_>>,
2323
) -> OxcDiagnostic {
24-
debug_assert!(Some(fn_span) != parent_scope_span);
2524
let function_label = if let Some(name) = function_name {
2625
format!("Function `{name}` does not capture any variables from its parent scope")
2726
} else {
@@ -375,7 +374,7 @@ fn get_short_span_for_fn_scope(
375374

376375
let scope_id =
377376
match ctx.nodes().parent_kind(ctx.scoping().symbol_declaration(function_symbol_id)) {
378-
Some(AstKind::AssignmentExpression(_)) => {
377+
Some(AstKind::AssignmentExpression(_) | AstKind::ObjectProperty(_)) => {
379378
ctx.scoping().scope_parent_id(scope_id).unwrap_or(scope_id)
380379
}
381380
_ => scope_id,
@@ -988,6 +987,10 @@ fn test() {
988987
"export namespace Foo { export function outer() { const inner = function inner() {}; } }",
989988
None,
990989
),
990+
(
991+
"jest.mock('@kbn/i18n-react', () => { return { I18nProvider: function MockI18nProvider() { }, }; });",
992+
None,
993+
),
991994
];
992995

993996
Tester::new(ConsistentFunctionScoping::NAME, ConsistentFunctionScoping::PLUGIN, pass, fail)

crates/oxc_linter/src/snapshots/unicorn_consistent_function_scoping.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,10 @@ source: crates/oxc_linter/src/tester.rs
541541
· ╰── Outer scope where this function is defined
542542
╰────
543543
help: Move `inner` to the outer scope to avoid recreating it on every call.
544+
545+
⚠ eslint-plugin-unicorn(consistent-function-scoping): Function `MockI18nProvider` does not capture any variables from its parent scope
546+
╭─[consistent_function_scoping.tsx:1:70]
547+
1 │ jest.mock('@kbn/i18n-react', () => { return { I18nProvider: function MockI18nProvider() { }, }; });
548+
· ────────────────
549+
╰────
550+
help: Move `MockI18nProvider` to the outer scope to avoid recreating it on every call.

0 commit comments

Comments
 (0)