Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linter/no-unused-vars): function expression in implicit arrow function return #5155

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/allowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ impl<'s, 'a> Symbol<'s, 'a> {
AstKind::AssignmentExpression(assignment) if assignment.right.span().contains_inclusive(self.span()) => {
return self != &assignment.left;
}
AstKind::ExpressionStatement(_) => {
// implicit return in arrow function expression
let Some(AstKind::FunctionBody(body)) = self.nodes().parent_kind(parent.id()) else {
return false;
};
return body.span.contains_inclusive(self.span()) && body.statements.len() == 1 && !self.get_snippet(body.span).starts_with('{')
}
_ => {
parent.kind().debug_name();
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,15 @@ fn test_functions() {
};
});
",
"const foo = () => function bar() { }\nfoo()",
"module.exports.foo = () => function bar() { }"
];

let fail = vec!["function foo() {}", "function foo() { foo() }"];
let fail = vec![
"function foo() {}",
"function foo() { foo() }",
"const foo = () => { function bar() { } }\nfoo()",
];

let fix = vec![
// function declarations are never removed
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_linter/src/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ source: crates/oxc_linter/src/tester.rs
· ╰── 'foo' is declared here
╰────
help: Consider removing this declaration.

⚠ eslint(no-unused-vars): Variable 'bar' is declared but never used.
╭─[no_unused_vars.tsx:1:30]
1 │ const foo = () => { function bar() { } }
· ─┬─
· ╰── 'bar' is declared here
2 │ foo()
╰────
help: Consider removing this declaration.