Skip to content

Commit 8ff6f2c

Browse files
committedAug 25, 2024·
fix(linter/no-unused-vars): panic on UsingDeclarations (#5206)
Closes #5202
1 parent 982bd6e commit 8ff6f2c

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed
 

‎crates/oxc_linter/src/rules/eslint/no_unused_vars/fixers/fix_vars.rs

+24-10
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,26 @@ impl NoUnusedVars {
3434
return fixer.noop();
3535
}
3636

37-
let Some(AstKind::VariableDeclaration(declaration)) =
38-
symbol.nodes().parent_node(decl_id).map(AstNode::kind)
39-
else {
40-
panic!("VariableDeclarator nodes should always be direct children of VariableDeclaration nodes");
37+
let Some(parent) = symbol.nodes().parent_node(decl_id).map(AstNode::kind) else {
38+
#[cfg(debug_assertions)]
39+
panic!("VariableDeclarator nodes should always have a parent node");
40+
#[cfg(not(debug_assertions))]
41+
return fixer.noop();
42+
};
43+
let (span, declarations) = match parent {
44+
AstKind::VariableDeclaration(decl) => (decl.span, &decl.declarations),
45+
AstKind::UsingDeclaration(decl) => {
46+
if decl.is_await {
47+
return fixer.noop();
48+
}
49+
(decl.span, &decl.declarations)
50+
}
51+
_ => {
52+
#[cfg(debug_assertions)]
53+
panic!("VariableDeclarator nodes should always be direct children of VariableDeclaration or UsingDeclaration nodes");
54+
#[cfg(not(debug_assertions))]
55+
return fixer.noop();
56+
}
4157
};
4258

4359
// `true` even if references aren't considered a usage.
@@ -48,18 +64,16 @@ impl NoUnusedVars {
4864
// for `let x = 1;` or `const { x } = obj; the whole declaration can
4965
// be removed, but for `const { x, y } = obj;` or `let x = 1, y = 2`
5066
// we need to keep the other declarations
51-
let has_neighbors = declaration.declarations.len() > 1;
52-
debug_assert!(!declaration.declarations.is_empty());
67+
let has_neighbors = declarations.len() > 1;
68+
debug_assert!(!declarations.is_empty());
5369
let binding_info = symbol.get_binding_info(&decl.id.kind);
5470

5571
match binding_info {
5672
BindingInfo::SingleDestructure | BindingInfo::NotDestructure => {
5773
if has_neighbors {
58-
return symbol
59-
.delete_from_list(fixer, &declaration.declarations, decl)
60-
.dangerously();
74+
return symbol.delete_from_list(fixer, declarations, decl).dangerously();
6175
}
62-
return fixer.delete(declaration).dangerously();
76+
return fixer.delete_range(span).dangerously();
6377
}
6478
BindingInfo::MultiDestructure(mut span, is_object, is_last) => {
6579
let source_after = &fixer.source_text()[(span.end as usize)..];

‎crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs

+10
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,16 @@ fn test_vars_catch() {
370370
.test_and_snapshot();
371371
}
372372

373+
#[test]
374+
fn test_vars_using() {
375+
let pass = vec![("using a = 1; console.log(a)", None)];
376+
377+
let fail = vec![("using a = 1;", None)];
378+
379+
Tester::new(NoUnusedVars::NAME, pass, fail)
380+
.with_snapshot_suffix("oxc-vars-using")
381+
.test_and_snapshot();
382+
}
373383
#[test]
374384
fn test_functions() {
375385
let pass = vec![
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: crates/oxc_linter/src/tester.rs
3+
---
4+
eslint(no-unused-vars): Variable 'a' is declared but never used.
5+
╭─[no_unused_vars.tsx:1:7]
6+
1using a = 1;
7+
· ┬
8+
· ╰── 'a' is declared here
9+
╰────
10+
help: Consider removing this declaration.

0 commit comments

Comments
 (0)
Please sign in to comment.