fix(linter/no-unused-vars): type specifier not deleted for type imports (#5029)

fixes a bug in eslint/no-unused-vars where, when unused type imports were deleted, the `type` modifier was not.
This commit is contained in:
DonIsaac 2024-08-21 00:11:45 +00:00
parent c30e2e9cce
commit 5a55dcf39b
3 changed files with 30 additions and 2 deletions

View file

@ -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));

View file

@ -1,4 +1,4 @@
use oxc_ast::ast::VariableDeclarator;
use oxc_ast::ast::{ImportDeclarationSpecifier, VariableDeclarator};
use std::{cell::OnceCell, fmt};
use oxc_ast::{
@ -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>,

View file

@ -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)