Skip to content

Commit

Permalink
fix(linter/no-unused-vars): type specifier not deleted for type imp…
Browse files Browse the repository at this point in the history
…orts (#5029)

fixes a bug in eslint/no-unused-vars where, when unused type imports were deleted, the `type` modifier was not.
  • Loading branch information
DonIsaac committed Aug 21, 2024
1 parent c30e2e9 commit 5a55dcf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ impl NoUnusedVars {
if specifiers.len() == 1 {
return fixer.delete(import).dangerously();
}
let span = symbol.span();
let span = specifiers
.iter()
.find(|specifier| symbol == specifier)
.map_or_else(|| symbol.span(), GetSpan::span);
let text_after = fixer.source_text()[(span.end as usize)..].chars();
let span = span.expand_right(count_whitespace_or_commas(text_after));

Expand Down
8 changes: 7 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_unused_vars/symbol.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_ast::ast::VariableDeclarator;
use oxc_ast::ast::{ImportDeclarationSpecifier, VariableDeclarator};
use std::{cell::OnceCell, fmt};

use oxc_ast::{
Expand Down Expand Up @@ -257,6 +257,12 @@ impl<'a> PartialEq<AssignmentTarget<'a>> for Symbol<'_, 'a> {
}
}

impl<'s, 'a> PartialEq<ImportDeclarationSpecifier<'a>> for Symbol<'s, 'a> {
fn eq(&self, import: &ImportDeclarationSpecifier<'a>) -> bool {
self == import.local()
}
}

impl<'s, 'a, T> PartialEq<&T> for Symbol<'s, 'a>
where
Symbol<'s, 'a>: PartialEq<T>,
Expand Down
19 changes: 19 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 @@ -533,6 +533,25 @@ fn test_imports() {
None,
FixKind::DangerousSuggestion,
),
// type imports
(
"import { type foo, bar } from './foo'; bar();",
"import { bar } from './foo'; bar();",
None,
FixKind::DangerousSuggestion,
),
(
"import { foo, type bar, baz } from './foo'; foo(baz);",
"import { foo, baz } from './foo'; foo(baz);",
None,
FixKind::DangerousSuggestion,
),
(
"import foo, { type bar } from './foo'; foo();",
"import foo, { } from './foo'; foo();",
None,
FixKind::DangerousSuggestion,
),
];

Tester::new(NoUnusedVars::NAME, pass, fail)
Expand Down

0 comments on commit 5a55dcf

Please sign in to comment.