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,52 +159,59 @@ 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;
let (function_declaration_symbol_id, function_body, reporter_span) =
match node.kind() {
AstKind::Function(function) => {
if function.is_typescript_syntax() {
return;
}
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;
}
}
}
// NOTE: function.body will always be some here because of
// checks in `is_typescript_syntax`
let Some(function_body) = &function.body else { return };
if let Some(binding_ident) = get_function_like_declaration(node, ctx) {
(
binding_ident.symbol_id.get().unwrap(),
function_body,
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 {
let Some(symbol_id) = function_id.symbol_id.get() else {
return;
};
(symbol_id, function_body, function_id.span())
} else {
return;
}
}
if function.is_typescript_syntax() {
return;
}
// NOTE: function.body will always be some here because of
// checks in `is_typescript_syntax`
let Some(function_body) = &function.body else { return };
if let Some(binding_ident) = get_function_like_declaration(node, ctx) {
(
binding_ident.symbol_id.get().unwrap(),
function_body,
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 {
let Some(symbol_id) = function_id.symbol_id.get() else {
AstKind::ArrowFunctionExpression(arrow_function) if self.check_arrow_functions => {
let Some(binding_ident) = get_function_like_declaration(node, ctx) else {
return;
};
let Some(symbol_id) = binding_ident.symbol_id.get() else {
return;
};
(symbol_id, function_body, function_id.span())
} else {
return;
}
}
AstKind::ArrowFunctionExpression(arrow_function) if self.check_arrow_functions => {
let Some(binding_ident) = get_function_like_declaration(node, ctx) else {
return;
};
let Some(symbol_id) = binding_ident.symbol_id.get() else {
return;
};
(symbol_id, &arrow_function.body, binding_ident.span())
}
_ => return,
};
(symbol_id, &arrow_function.body, binding_ident.span())
}
_ => return,
};
// if the function is declared at the root scope, we don't need to check anything
if ctx.symbols().get_scope_id(function_declaration_symbol_id)
@ -588,6 +595,17 @@ fn test() {
("module.exports = function foo() {};", None),
("module.exports.foo = 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),
];
@ -626,6 +644,14 @@ fn test() {
),
("function foo() { function bar() { return <JSX/>; } }", 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.
(
"function doFoo(foo) {

View file

@ -42,6 +42,15 @@ 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: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.
╭─[consistent_function_scoping.tsx:2:26]
1 │ function doFoo(foo) {