From 5e564b977dddefb66507cf333d18de1e56b45184 Mon Sep 17 00:00:00 2001 From: Arian Pourarian Date: Thu, 29 Aug 2024 00:37:07 +0330 Subject: [PATCH 1/4] fix false scoping error on assignment expr --- .../src/rules/unicorn/consistent_function_scoping.rs | 7 +++++++ 1 file changed, 7 insertions(+) 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 8f99875ed9735..b8e253b617ee9 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; } @@ -568,6 +572,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![ From bc23ccb2c32f5c148f267325492add7b9bf66c31 Mon Sep 17 00:00:00 2001 From: Arian94 Date: Sat, 31 Aug 2024 10:53:20 +0330 Subject: [PATCH 2/4] add new fail test - improve error logging text accordingly --- .../src/rules/unicorn/consistent_function_scoping.rs | 12 ++++++++++-- .../src/snapshots/consistent_function_scoping.snap | 7 +++++++ 2 files changed, 17 insertions(+), 2 deletions(-) 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 b8e253b617ee9..383a526e4bc64 100644 --- a/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs +++ b/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs @@ -176,8 +176,15 @@ 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() + .unwrap_or(&oxc_ast::ast::BindingIdentifier { + span: Span::sized(function.span.start, 8), // 8 for "function" + name: oxc_span::Atom::empty(), + symbol_id: core::cell::Cell::new(None), + }) + .span, ) } else if let Some(function_id) = &function.id { (function_id.symbol_id.get().unwrap(), function_body, function_id.span()) @@ -806,6 +813,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 d8d6c502511f4..7dd3f0396f95a 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. From b2a552b35b1acb0f6962520bbcdb6705980f4aed Mon Sep 17 00:00:00 2001 From: Arian Pourarian Date: Sat, 31 Aug 2024 19:24:32 +0330 Subject: [PATCH 3/4] refactor: use map to get func name span --- .../src/rules/unicorn/consistent_function_scoping.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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 383a526e4bc64..01df436465eb4 100644 --- a/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs +++ b/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs @@ -179,12 +179,10 @@ impl Rule for ConsistentFunctionScoping { function .id .as_ref() - .unwrap_or(&oxc_ast::ast::BindingIdentifier { - span: Span::sized(function.span.start, 8), // 8 for "function" - name: oxc_span::Atom::empty(), - symbol_id: core::cell::Cell::new(None), - }) - .span, + .map(|func_binding_ident| func_binding_ident.span) + .unwrap_or( + Span::sized(function.span.start, 8), // 8 for "function" + ), ) } else if let Some(function_id) = &function.id { (function_id.symbol_id.get().unwrap(), function_body, function_id.span()) From 0f68456add20fe735a114cef9d15c4f45236d573 Mon Sep 17 00:00:00 2001 From: Cameron Clark Date: Sat, 31 Aug 2024 19:32:47 +0100 Subject: [PATCH 4/4] fix: fix clippy error --- .../src/rules/unicorn/consistent_function_scoping.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) 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 f14d4b6c0c996..a250fe518e9ac 100644 --- a/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs +++ b/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs @@ -179,10 +179,9 @@ impl Rule for ConsistentFunctionScoping { function .id .as_ref() - .map(|func_binding_ident| func_binding_ident.span) - .unwrap_or( - Span::sized(function.span.start, 8), // 8 for "function" - ), + .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())