Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_analyze): noUnusedLabels #4096

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/rome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ define_dategories! {
"lint/nursery/noStringCaseMismatch": "https://docs.rome.tools/lint/rules/noStringCaseMismatch",
"lint/nursery/noUnreachableSuper": "https://rome.tools/docs/lint/rules/noUnreachableSuper",
"lint/nursery/noUnsafeFinally": "https://docs.rome.tools/lint/rules/noUnsafeFinally",
"lint/nursery/noUnusedLabels": "https://docs.rome.tools/lint/rules/noUnusedLabels",
"lint/nursery/noUselessSwitchCase": "https://docs.rome.tools/lint/rules/noUselessSwitchCase",
"lint/nursery/noVar": "https://docs.rome.tools/lint/rules/noVar",
"lint/nursery/noVoidTypeReturn": "https://docs.rome.tools/lint/rules/noVoidTypeReturn",
Expand Down
3 changes: 2 additions & 1 deletion crates/rome_js_analyze/src/analyzers/nursery.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

178 changes: 178 additions & 0 deletions crates/rome_js_analyze/src/analyzers/nursery/no_unused_labels.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
use rome_analyze::context::RuleContext;
use rome_analyze::{
declare_rule, ActionCategory, AddVisitor, Phases, QueryMatch, Queryable, Rule, RuleDiagnostic,
ServiceBag, Visitor, VisitorContext,
};
use rome_console::markup;
use rome_diagnostics::Applicability;
use rome_js_syntax::{
JsBreakStatement, JsContinueStatement, JsLabeledStatement, JsLanguage, TextRange, WalkEvent,
};

use rome_rowan::{AstNode, BatchMutationExt, Language, NodeOrToken, SyntaxNode};
use rustc_hash::FxHashMap;

use crate::control_flow::AnyJsControlFlowRoot;
use crate::JsRuleAction;

declare_rule! {
/// Disallow unused labels.
///
/// Labels that are declared and never used are most likely an error due to incomplete refactoring.
///
/// Source: https://eslint.org/docs/latest/rules/no-unused-labels
///
/// ## Examples
///
/// ### Invalid
///
/// ```cjs,expect_diagnostic
/// LOOP: for (const x of xs) {
/// if (x > 0) {
/// break;
/// }
/// f(x);
/// }
/// ```
///
/// ### Valid
///
/// ```cjs
/// LOOP: for (const x of xs) {
/// if (x > 0) {
/// break LOOP;
/// }
/// f(x);
/// }
/// ```
///
pub(crate) NoUnusedLabels {
version: "next",
name: "noUnusedLabels",
recommended: true,
}
}

#[derive(Default)]
struct UnusedLabelVisitor {
root_id: usize,
// Key = (root_id, label)
labels: FxHashMap<(usize, String), JsLabeledStatement>,
}

impl UnusedLabelVisitor {
fn insert(&mut self, label: String, label_stmt: JsLabeledStatement) {
self.labels.insert((self.root_id, label), label_stmt);
}

fn remove(&mut self, label: String) -> Option<JsLabeledStatement> {
self.labels.remove(&(self.root_id, label))
}
}

impl Visitor for UnusedLabelVisitor {
type Language = JsLanguage;

fn visit(
&mut self,
event: &WalkEvent<SyntaxNode<Self::Language>>,
mut ctx: VisitorContext<Self::Language>,
) {
match event {
WalkEvent::Enter(node) => {
if AnyJsControlFlowRoot::cast_ref(node).is_some() {
self.root_id += 1;
} else if let Some(label_stmt) = JsLabeledStatement::cast_ref(node) {
if let Ok(label_tok) = label_stmt.label_token() {
self.insert(label_tok.text_trimmed().to_owned(), label_stmt);
}
} else if let Some(break_stmt) = JsBreakStatement::cast_ref(node) {
if let Some(label_tok) = break_stmt.label_token() {
self.remove(label_tok.text_trimmed().to_owned());
}
} else if let Some(continue_stmt) = JsContinueStatement::cast_ref(node) {
if let Some(label_tok) = continue_stmt.label_token() {
self.remove(label_tok.text_trimmed().to_owned());
}
}
}
WalkEvent::Leave(node) => {
if AnyJsControlFlowRoot::cast_ref(node).is_some() {
self.root_id -= 1;
} else if let Some(stmt_label) = JsLabeledStatement::cast_ref(node) {
if let Ok(label_tok) = stmt_label.label_token() {
let result = self.remove(label_tok.text_trimmed().to_owned());
if let Some(label_stmt) = result {
ctx.match_query(UnusedLabel(label_stmt));
}
}
}
}
}
}
}

pub(crate) struct UnusedLabel(JsLabeledStatement);

impl QueryMatch for UnusedLabel {
fn text_range(&self) -> TextRange {
self.0.range()
}
}

impl Queryable for UnusedLabel {
type Input = Self;
type Language = JsLanguage;
type Output = JsLabeledStatement;
type Services = ();

fn build_visitor(
analyzer: &mut impl AddVisitor<Self::Language>,
_: &<Self::Language as Language>::Root,
) {
analyzer.add_visitor(Phases::Syntax, UnusedLabelVisitor::default);
}

// Extract the output object from the input type
fn unwrap_match(_: &ServiceBag, query: &Self::Input) -> Self::Output {
query.0.clone()
}
}

impl Rule for NoUnusedLabels {
type Query = UnusedLabel;
type State = ();
type Signals = Option<Self::State>;
type Options = ();

fn run(_: &RuleContext<Self>) -> Option<Self::State> {
Some(())
}

fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> {
let unused_label = ctx.query();
Some(RuleDiagnostic::new(
rule_category!(),
unused_label.label_token().ok()?.text_trimmed_range(),
markup! {
"Unused "<Emphasis>"label"</Emphasis>"."
},
))
}

fn action(ctx: &RuleContext<Self>, _: &Self::State) -> Option<JsRuleAction> {
let unused_label = ctx.query();
let body = unused_label.body().ok()?;
let mut mutation = ctx.root().begin();
mutation.replace_element(
NodeOrToken::Node(unused_label.syntax().to_owned()),
NodeOrToken::Node(body.syntax().to_owned()),
);
Some(JsRuleAction {
category: ActionCategory::QuickFix,
applicability: Applicability::MaybeIncorrect,
message: markup! {"Remove the unused "<Emphasis>"label"</Emphasis>"."}.to_owned(),
mutation,
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*before*/ A /*inner*/: /*after*/ var foo = 0;

B: {
foo();
}

C: for (let i = 0; i < 10; ++i) {
foo();
}

D: var foo = 0;

E: {
foo();
bar();
}

F: for (var i = 0; i < 10; ++i) {
foo();
if (a) break;
bar();
}

G: for (var i = 0; i < 10; ++i) {
foo();
if (a) continue;
bar();
}

A: for (var i = 0; i < 10; ++i) {
H: break A;
}

I: {
var I = 0;
console.log(I);
}

J: /* comment */ foo;

K /* comment */: foo;

L: {
function f() {
L: {
break L;
}
}
}

M: {
class X {
static {
M: {
break M;
}
}

method() {
M: {
break M;
}
}
}
}

/*
* Below is fatal errors.
* "A: break B",
* "A: function foo() { break A; }",
* "A: class Foo { foo() { break A; } }",
* "A: { A: { break A; } }"
*/
Loading