fix(linter): fix some cases on ``AssignmentExpression` for `unicorn/consistent-function-scoping`` (#5675)

This is to fix the cases mentioned in the comment section of #5365.

In short, it will report these as PASS test cases:

```js
let inner;

function foo1() {
  inner = function() {}
}
function foo2() {
  inner = function() {}
}
```

```js
let inner;

function outer() {
  inner = function() {}
}
```

And report these below as FAIL test cases:

```js
let inner;

function foo1() {
  inner = function smth() {}
}
function foo2() {
  inner = function bar() {}
}
```

```js
let inner;

function outer() {
  inner = function inner() {}
}
```

The case below was already done in #5312 but mentioned in #5365. It is a
FAIL case as well:
```js
function outer() {
  const inner = function inner() {}
}
```

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Don Isaac <donald.isaac@gmail.com>
This commit is contained in:
Arian94 2024-09-16 07:39:59 +03:30 committed by GitHub
parent dc10eaf047
commit 737ba1d9d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 77 additions and 42 deletions

View file

@ -159,15 +159,24 @@ impl Rule for ConsistentFunctionScoping {
} }
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let (function_declaration_symbol_id, function_body, reporter_span) = match node.kind() { let (function_declaration_symbol_id, function_body, reporter_span) =
match node.kind() {
AstKind::Function(function) => { AstKind::Function(function) => {
if let Some(AstKind::AssignmentExpression(_)) = ctx.nodes().parent_kind(node.id()) { if function.is_typescript_syntax() {
return; return;
} }
if function.is_typescript_syntax() { if let Some(func_scope_id) = function.scope_id.get() {
if let Some(parent_scope_id) = ctx.scopes().get_parent_id(func_scope_id) {
// Example: const foo = function bar() {};
// The bar function scope id is 1. In order to ignore this rule,
// its parent's scope id (in this case `foo`'s scope id is 0 and is equal to root scope id)
// should be considered.
if parent_scope_id == ctx.scopes().root_scope_id() {
return; return;
} }
}
}
// NOTE: function.body will always be some here because of // NOTE: function.body will always be some here because of
// checks in `is_typescript_syntax` // checks in `is_typescript_syntax`
@ -177,12 +186,10 @@ impl Rule for ConsistentFunctionScoping {
( (
binding_ident.symbol_id.get().unwrap(), binding_ident.symbol_id.get().unwrap(),
function_body, function_body,
function function.id.as_ref().map_or(
.id Span::sized(function.span.start, 8),
.as_ref() |func_binding_ident| func_binding_ident.span,
.map_or(Span::sized(function.span.start, 8), |func_binding_ident| { ),
func_binding_ident.span
}),
) )
} else if let Some(function_id) = &function.id { } else if let Some(function_id) = &function.id {
let Some(symbol_id) = function_id.symbol_id.get() else { let Some(symbol_id) = function_id.symbol_id.get() else {
@ -588,6 +595,17 @@ fn test() {
("module.exports = function foo() {};", None), ("module.exports = function foo() {};", None),
("module.exports.foo = function foo() {};", None), ("module.exports.foo = function foo() {};", None),
("foo.bar.func = function foo() {};", None), ("foo.bar.func = function foo() {};", None),
(
"let inner;
function foo1() {
inner = function() {}
}
function foo2() {
inner = function() {}
}",
None,
),
("if(f) function f(){}", None), ("if(f) function f(){}", None),
]; ];
@ -626,6 +644,14 @@ fn test() {
), ),
("function foo() { function bar() { return <JSX/>; } }", None), ("function foo() { function bar() { return <JSX/>; } }", None),
("function doFoo(Foo) { const doBar = () => arguments; return doBar(); };", None), ("function doFoo(Foo) { const doBar = () => arguments; return doBar(); };", None),
(
"let inner;
function outer() {
inner = function inner() {}
}",
None,
),
// end of cases that eslint-plugin-unicorn passes, but we fail. // end of cases that eslint-plugin-unicorn passes, but we fail.
( (
"function doFoo(foo) { "function doFoo(foo) {

View file

@ -42,6 +42,15 @@ source: crates/oxc_linter/src/tester.rs
╰──── ╰────
help: Move this function to the outer scope. 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:4:34]
3 │ function outer() {
4 │ inner = function inner() {}
· ─────
5 │ }
╰────
help: Move this function to the outer scope.
⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope.
╭─[consistent_function_scoping.tsx:2:26] ╭─[consistent_function_scoping.tsx:2:26]
1 │ function doFoo(foo) { 1 │ function doFoo(foo) {