Skip to content

Commit

Permalink
fix(linter/no-unused-vars): do not delete function expressions when f…
Browse files Browse the repository at this point in the history
…ixing
  • Loading branch information
DonIsaac committed Aug 12, 2024
1 parent 0d48c22 commit 5fc4653
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use oxc_ast::{ast::VariableDeclarator, AstKind};
use oxc_ast::{
ast::{Expression, VariableDeclarator},
AstKind,
};
use oxc_semantic::{AstNode, AstNodeId};
use oxc_span::CompactStr;
use regex::Regex;
Expand All @@ -20,6 +23,10 @@ impl NoUnusedVars {
decl: &VariableDeclarator<'a>,
decl_id: AstNodeId,
) -> RuleFix<'a> {
if decl.init.as_ref().is_some_and(Expression::is_function) {
return fixer.noop();
}

let Some(AstKind::VariableDeclaration(declaration)) =
symbol.nodes().parent_node(decl_id).map(AstNode::kind)
else {
Expand Down
23 changes: 23 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ fn test_vars_simple() {
None,
FixKind::DangerousSuggestion,
),
// function expressions do not get changed
(r"const foo = () => {}", r"const foo = () => {}", None, FixKind::DangerousSuggestion),
(
r"const foo = function() {}",
r"const foo = function() {}",
None,
FixKind::DangerousSuggestion,
),
(
r"const foo = function foo() {}",
r"const foo = function foo() {}",
None,
FixKind::DangerousSuggestion,
),
// vars with references get renamed
("let x = 1; x = 2;", "let _x = 1; _x = 2;", None, FixKind::DangerousFix),
(
Expand Down Expand Up @@ -279,8 +293,10 @@ fn test_vars_destructure() {
];

let fix = vec![
// single destructure
("const { a } = obj;", "", None, FixKind::DangerousSuggestion),
("const [a] = arr;", "", None, FixKind::DangerousSuggestion),
// multi destructure
(
"const { a, b } = obj; f(b)",
"const { b } = obj; f(b)",
Expand Down Expand Up @@ -313,6 +329,13 @@ fn test_vars_destructure() {
None,
FixKind::DangerousSuggestion,
),
// multi destructure with rename
(
"const { a: foo, b: bar } = obj; f(bar)",
"const { b: bar } = obj; f(bar)",
None,
FixKind::DangerousSuggestion,
),
// renaming
// (
// "let a = 1; a = 2;",
Expand Down

0 comments on commit 5fc4653

Please sign in to comment.