-
-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): start import fixer for eslint/no-unused-vars (#4849)
- Loading branch information
Showing
4 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
crates/oxc_linter/src/rules/eslint/no_unused_vars/fixers/fix_imports.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use oxc_ast::ast::ImportDeclaration; | ||
use oxc_span::GetSpan; | ||
|
||
use super::{count_whitespace_or_commas, NoUnusedVars, Symbol}; | ||
use crate::fixer::{RuleFix, RuleFixer}; | ||
|
||
impl NoUnusedVars { | ||
#[allow(clippy::unused_self)] | ||
pub(in super::super) fn remove_unused_import_declaration<'a>( | ||
&self, | ||
fixer: RuleFixer<'_, 'a>, | ||
symbol: &Symbol<'_, 'a>, | ||
import: &ImportDeclaration<'a>, | ||
) -> RuleFix<'a> { | ||
let specifiers = import | ||
.specifiers | ||
.as_ref() | ||
.expect("Found an unused variable in an ImportDeclaration with no specifiers. This should be impossible."); | ||
|
||
debug_assert!( | ||
!specifiers.is_empty(), | ||
"Found an unused variable in an ImportDeclaration with no specifiers. This should be impossible." | ||
); | ||
|
||
if specifiers.len() == 1 { | ||
return fixer.delete(import).dangerously(); | ||
} | ||
let span = symbol.span(); | ||
let text_after = fixer.source_text()[(span.end as usize)..].chars(); | ||
let span = span.expand_right(count_whitespace_or_commas(text_after)); | ||
|
||
fixer.delete_range(span).dangerously() | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
crates/oxc_linter/src/rules/eslint/no_unused_vars/fixers/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod fix_imports; | ||
mod fix_symbol; | ||
mod fix_vars; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters