diff --git a/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs b/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs index 8679382c9..a250fe518 100644 --- a/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs +++ b/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs @@ -160,6 +160,10 @@ impl Rule for ConsistentFunctionScoping { fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { let (function_declaration_symbol_id, function_body, reporter_span) = match node.kind() { AstKind::Function(function) => { + if let Some(AstKind::AssignmentExpression(_)) = ctx.nodes().parent_kind(node.id()) { + return; + } + if function.is_typescript_syntax() { return; } @@ -172,8 +176,12 @@ impl Rule for ConsistentFunctionScoping { ( binding_ident.symbol_id.get().unwrap(), function_body, - // 8 for "function" - Span::sized(function.span.start, 8), + function + .id + .as_ref() + .map_or(Span::sized(function.span.start, 8), |func_binding_ident| { + func_binding_ident.span + }), ) } else if let Some(function_id) = &function.id { (function_id.symbol_id.get().unwrap(), function_body, function_id.span()) @@ -573,6 +581,9 @@ fn test() { ("t.throws(() => receiveString(function a() {}), {})", None), ("function test () { t.throws(() => receiveString(function a() {}), {}) }", None), ("function foo() { let x = new Bar(function b() {}) }", None), + ("module.exports = function foo() {};", None), + ("module.exports.foo = function foo() {};", None), + ("foo.bar.func = function foo() {};", None), ]; let fail = vec![ @@ -804,6 +815,7 @@ fn test() { // ("const doFoo = () => bar => bar;", None), ("function foo() { const bar = async () => {} }", None), ("function doFoo() { const doBar = function(bar) { return bar; }; }", None), + ("function outer() { const inner = function inner() {}; }", None), ]; Tester::new(ConsistentFunctionScoping::NAME, pass, fail).test_and_snapshot(); diff --git a/crates/oxc_linter/src/snapshots/consistent_function_scoping.snap b/crates/oxc_linter/src/snapshots/consistent_function_scoping.snap index d8d6c5025..7dd3f0396 100644 --- a/crates/oxc_linter/src/snapshots/consistent_function_scoping.snap +++ b/crates/oxc_linter/src/snapshots/consistent_function_scoping.snap @@ -394,3 +394,10 @@ source: crates/oxc_linter/src/tester.rs · ──────── ╰──── help: Move this function to the outer scope. + + ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. + ╭─[consistent_function_scoping.tsx:1:43] + 1 │ function outer() { const inner = function inner() {}; } + · ───── + ╰──── + help: Move this function to the outer scope.