mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
fix(linter/unicorn): consistent-function-scoping false positive on assignment expression (#5312)
fixes #5159 and any other named function assigned to a property like: ```js const foo = {}; foo.bar = function fooBar() {} ``` --------- Co-authored-by: Don Isaac <donald.isaac@gmail.com> Co-authored-by: Cameron Clark <cameron.clark@hey.com>
This commit is contained in:
parent
f81e8a126e
commit
11b93af0d1
2 changed files with 21 additions and 2 deletions
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue