This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rome_js_analyze):
noUnusedVariable
rule (#2987)
* no unused variables lint rule
- Loading branch information
Showing
13 changed files
with
906 additions
and
90 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
183 changes: 183 additions & 0 deletions
183
crates/rome_js_analyze/src/semantic_analyzers/js/no_unused_variables.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,183 @@ | ||
use crate::{semantic_services::Semantic, utils::batch::JsBatchMutation, JsRuleAction}; | ||
use rome_analyze::{ | ||
context::RuleContext, declare_rule, ActionCategory, Rule, RuleCategory, RuleDiagnostic, | ||
}; | ||
use rome_console::markup; | ||
use rome_diagnostics::Applicability; | ||
use rome_js_semantic::{AllReferencesExtensions, SemanticScopeExtensions}; | ||
use rome_js_syntax::{ | ||
JsFormalParameter, JsFunctionDeclaration, JsIdentifierBinding, JsSyntaxKind, | ||
JsVariableDeclarator, | ||
}; | ||
use rome_rowan::{AstNode, BatchMutationExt}; | ||
|
||
declare_rule! { | ||
/// Disallow unused variables. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// const a = 4; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// let a = 4; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// function foo() { | ||
/// }; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// function foo(myVar) { | ||
/// console.log('foo'); | ||
/// } | ||
/// foo(); | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// const foo = () => { | ||
/// }; | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// function foo() { | ||
/// foo(); | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// const foo = () => { | ||
/// foo(); | ||
/// console.log(this); | ||
/// }; | ||
/// ``` | ||
/// | ||
/// # Valid | ||
/// | ||
/// ```js | ||
/// function foo(b) { | ||
/// console.log(b) | ||
/// }; | ||
/// foo(); | ||
/// ``` | ||
pub(crate) NoUnusedVariables { | ||
version: "0.9.0", | ||
name: "noUnusedVariables", | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for NoUnusedVariables { | ||
const CATEGORY: RuleCategory = RuleCategory::Lint; | ||
|
||
type Query = Semantic<JsIdentifierBinding>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { | ||
let binding = ctx.query(); | ||
let model = ctx.model(); | ||
|
||
let all_references = binding.all_references(model); | ||
|
||
if all_references.count() == 0 { | ||
Some(()) | ||
} else { | ||
// We need to check if all uses of this binding are somehow recursive | ||
|
||
let function_declaration_scope = binding | ||
.parent::<JsFunctionDeclaration>() | ||
.map(|declaration| declaration.scope(model)); | ||
|
||
let declarator = binding.parent::<JsVariableDeclarator>(); | ||
|
||
let mut references_outside = 0; | ||
for r in binding.all_references(model) { | ||
let reference_scope = r.scope(); | ||
|
||
// If this binding is a function, and all its references are "inside" this | ||
// function, we can safely say that this function is not used | ||
if function_declaration_scope | ||
.as_ref() | ||
.map(|s| s.is_ancestor_of(&reference_scope)) | ||
.unwrap_or(false) | ||
{ | ||
continue; | ||
} | ||
|
||
// Another possibility is if all its references are "inside" the same declaration | ||
if let Some(declarator) = declarator.as_ref() { | ||
let node = declarator.syntax(); | ||
if r.node().ancestors().any(|n| n == *node) { | ||
continue; | ||
} | ||
} | ||
|
||
references_outside += 1; | ||
break; | ||
} | ||
|
||
if references_outside == 0 { | ||
Some(()) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> { | ||
let binding = ctx.query(); | ||
|
||
let symbol_type = match binding.syntax().parent().unwrap().kind() { | ||
JsSyntaxKind::JS_FORMAL_PARAMETER => "parameter", | ||
JsSyntaxKind::JS_FUNCTION_DECLARATION => "function", | ||
_ => "variable", | ||
}; | ||
|
||
let diag = RuleDiagnostic::warning( | ||
binding.syntax().text_trimmed_range(), | ||
markup! { | ||
"This " {symbol_type} " is unused." | ||
}, | ||
); | ||
|
||
let diag = diag.footer_note( | ||
markup! {"Unused variables usually are result of incomplete refactoring, typos and other source of bugs."}, | ||
); | ||
|
||
Some(diag) | ||
} | ||
|
||
fn action(ctx: &RuleContext<Self>, _: &Self::State) -> Option<JsRuleAction> { | ||
let root = ctx.root(); | ||
let binding = ctx.query(); | ||
|
||
let mut batch = root.begin(); | ||
|
||
// If this is a function, remove the whole declaration | ||
if let Some(declaration) = binding.parent::<JsFunctionDeclaration>() { | ||
batch.remove_node(declaration) | ||
} else if let Some(variable_declarator) = binding.parent::<JsVariableDeclarator>() { | ||
batch.remove_js_variable_declarator(&variable_declarator); | ||
} else if let Some(formal_parameter) = binding.parent::<JsFormalParameter>() { | ||
batch.remove_js_formal_parameter(&formal_parameter); | ||
} | ||
|
||
let symbol_type = match binding.syntax().parent().unwrap().kind() { | ||
JsSyntaxKind::JS_FORMAL_PARAMETER => "parameter", | ||
JsSyntaxKind::JS_FUNCTION_DECLARATION => "function", | ||
_ => "variable", | ||
}; | ||
|
||
Some(JsRuleAction { | ||
category: ActionCategory::QuickFix, | ||
applicability: Applicability::Unspecified, | ||
message: markup! { "Remove this " {symbol_type} "." }.to_owned(), | ||
mutation: batch, | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.