Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linter): implement no-unused-labels #282

Merged
merged 4 commits into from
Apr 16, 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/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ oxc_macros::declare_all_lint_rules! {
no_constant_condition,
no_compare_neg_zero,
no_unsafe_negation,
no_unused_labels,
no_bitwise,
deepscan::uninvoked_array_callback,
deepscan::bad_bitwise_operator,
Expand Down
90 changes: 90 additions & 0 deletions crates/oxc_linter/src/rules/no_unused_labels.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use oxc_ast::{AstKind, Atom, Span};
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;

use crate::{context::LintContext, fixer::Fix, rule::Rule, AstNode};

#[derive(Debug, Error, Diagnostic)]
#[error("eslint(no-unused-labels): Disallow unused labels")]
#[diagnostic(severity(warning), help("'{0}:' is defined but never used."))]
struct NoUnusedLabelsDiagnostic(Atom, #[label] pub Span);

#[derive(Debug, Default, Clone)]
pub struct NoUnusedLabels;

declare_oxc_lint!(
/// ### What it does
///
/// Disallow unused labels
///
///
/// ### Why is this bad?
///
/// Labels that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring.
///
/// ### Example
/// ```javascript
/// OUTER_LOOP:
/// for (const student of students) {
/// if (checkScores(student.scores)) {
/// continue;
/// }
/// doSomething(student);
/// }
/// ```
NoUnusedLabels,
correctness
);

impl Rule for NoUnusedLabels {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if AstKind::Root == node.get().kind() {
for id in ctx.semantic().unused_labels() {
let node = ctx.semantic().nodes()[*id];
if let AstKind::LabeledStatement(stmt) = node.kind() {
// TODO: Ignore fix where comments exist between label and statement
// e.g. A: /* Comment */ function foo(){}
ctx.diagnostic_with_fix(
NoUnusedLabelsDiagnostic(stmt.label.name.clone(), stmt.label.span),
|| Fix::delete(stmt.label.span),
);
}
}
}
}
}

#[test]
fn test() {
use crate::tester::Tester;

let pass = vec![
("A: break A;", None),
("A: { foo(); break A; bar(); }", None),
("A: if (a) { foo(); if (b) break A; bar(); }", None),
("A: for (var i = 0; i < 10; ++i) { foo(); if (a) break A; bar(); }", None),
("A: for (var i = 0; i < 10; ++i) { foo(); if (a) continue A; bar(); }", None),
(
"A: { B: break B; C: for (var i = 0; i < 10; ++i) { foo(); if (a) break A; if (c) continue C; bar(); } }",
None,
),
("A: { var A = 0; console.log(A); break A; console.log(A); }", None),
];

let fail = vec![
("A: var foo = 0;", None),
("A: { foo(); bar(); }", None),
("A: if (a) { foo(); bar(); }", None),
("A: for (var i = 0; i < 10; ++i) { foo(); if (a) break; bar(); }", None),
("A: for (var i = 0; i < 10; ++i) { foo(); if (a) continue; bar(); }", None),
("A: for (var i = 0; i < 10; ++i) { B: break A; }", None),
("A: { var A = 0; console.log(A); }", None),
("A: /* comment */ foo", None),
("A /* comment */: foo", None),
];

Tester::new(NoUnusedLabels::NAME, pass, fail).test_and_snapshot();
}
68 changes: 68 additions & 0 deletions crates/oxc_linter/src/snapshots/no_unused_labels.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
source: crates/oxc_linter/src/tester.rs
expression: no_unused_labels
---

⚠ eslint(no-unused-labels): Disallow unused labels
╭─[no_unused_labels.tsx:1:1]
1 │ A: var foo = 0;
· ─
╰────
help: 'A:' is defined but never used.

⚠ eslint(no-unused-labels): Disallow unused labels
╭─[no_unused_labels.tsx:1:1]
1 │ A: { foo(); bar(); }
· ─
╰────
help: 'A:' is defined but never used.

⚠ eslint(no-unused-labels): Disallow unused labels
╭─[no_unused_labels.tsx:1:1]
1 │ A: if (a) { foo(); bar(); }
· ─
╰────
help: 'A:' is defined but never used.

⚠ eslint(no-unused-labels): Disallow unused labels
╭─[no_unused_labels.tsx:1:1]
1 │ A: for (var i = 0; i < 10; ++i) { foo(); if (a) break; bar(); }
· ─
╰────
help: 'A:' is defined but never used.

⚠ eslint(no-unused-labels): Disallow unused labels
╭─[no_unused_labels.tsx:1:1]
1 │ A: for (var i = 0; i < 10; ++i) { foo(); if (a) continue; bar(); }
· ─
╰────
help: 'A:' is defined but never used.

⚠ eslint(no-unused-labels): Disallow unused labels
╭─[no_unused_labels.tsx:1:1]
1 │ A: for (var i = 0; i < 10; ++i) { B: break A; }
· ─
╰────
help: 'B:' is defined but never used.

⚠ eslint(no-unused-labels): Disallow unused labels
╭─[no_unused_labels.tsx:1:1]
1 │ A: { var A = 0; console.log(A); }
· ─
╰────
help: 'A:' is defined but never used.

⚠ eslint(no-unused-labels): Disallow unused labels
╭─[no_unused_labels.tsx:1:1]
1 │ A: /* comment */ foo
· ─
╰────
help: 'A:' is defined but never used.

⚠ eslint(no-unused-labels): Disallow unused labels
╭─[no_unused_labels.tsx:1:1]
1 │ A /* comment */: foo
· ─
╰────
help: 'A:' is defined but never used.

48 changes: 48 additions & 0 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ use crate::{
Semantic,
};

pub struct LabeledScope<'a> {
name: &'a str,
used: bool,
parent: usize,
}

struct UnusedLabels<'a> {
scopes: Vec<LabeledScope<'a>>,
curr_scope: usize,
labels: Vec<AstNodeId>,
}

pub struct SemanticBuilder<'a> {
pub source_text: &'a str,

Expand All @@ -44,6 +56,7 @@ pub struct SemanticBuilder<'a> {

with_module_record_builder: bool,
pub module_record_builder: ModuleRecordBuilder,
unused_labels: UnusedLabels<'a>,

jsdoc: JSDocBuilder<'a>,

Expand Down Expand Up @@ -76,6 +89,7 @@ impl<'a> SemanticBuilder<'a> {
symbols: SymbolTableBuilder::default(),
with_module_record_builder: false,
module_record_builder: ModuleRecordBuilder::default(),
unused_labels: UnusedLabels { scopes: vec![], curr_scope: 0, labels: vec![] },
jsdoc: JSDocBuilder::new(source_text, trivias),
check_syntax_error: false,
}
Expand Down Expand Up @@ -122,6 +136,7 @@ impl<'a> SemanticBuilder<'a> {
symbols,
module_record,
jsdoc: self.jsdoc.build(),
unused_labels: self.unused_labels.labels,
};
SemanticBuilderReturn { semantic, errors: self.errors.into_inner() }
}
Expand Down Expand Up @@ -295,6 +310,32 @@ impl<'a> SemanticBuilder<'a> {
AstKind::JSXElementName(elem) => {
self.reference_jsx_element_name(elem);
}
AstKind::LabeledStatement(stmt) => {
self.unused_labels.scopes.push(LabeledScope {
name: stmt.label.name.as_str(),
used: false,
parent: self.unused_labels.curr_scope,
});
self.unused_labels.curr_scope = self.unused_labels.scopes.len() - 1;
}
AstKind::ContinueStatement(stmt) => {
if let Some(label) = &stmt.label {
let scope =
self.unused_labels.scopes.iter_mut().rev().find(|x| x.name == label.name);
if let Some(scope) = scope {
scope.used = true;
}
}
}
AstKind::BreakStatement(stmt) => {
if let Some(label) = &stmt.label {
let scope =
self.unused_labels.scopes.iter_mut().rev().find(|x| x.name == label.name);
if let Some(scope) = scope {
scope.used = true;
}
CarrotzRule123 marked this conversation as resolved.
Show resolved Hide resolved
}
}
_ => {}
}
}
Expand All @@ -308,6 +349,13 @@ impl<'a> SemanticBuilder<'a> {
AstKind::ModuleDeclaration(decl) => {
self.current_symbol_flags -= Self::symbol_flag_from_module_declaration(decl);
}
AstKind::LabeledStatement(_) => {
let scope = &self.unused_labels.scopes[self.unused_labels.curr_scope];
if !scope.used {
self.unused_labels.labels.push(self.current_node_id);
}
self.unused_labels.curr_scope = scope.parent;
}
_ => {}
}
}
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub struct Semantic<'a> {
module_record: ModuleRecord,

jsdoc: JSDoc<'a>,

unused_labels: Vec<AstNodeId>,
}

impl<'a> Semantic<'a> {
Expand Down Expand Up @@ -84,6 +86,11 @@ impl<'a> Semantic<'a> {
&self.symbols
}

#[must_use]
pub fn unused_labels(&self) -> &Vec<AstNodeId> {
&self.unused_labels
}

#[must_use]
pub fn is_unresolved_reference(&self, node_id: AstNodeId) -> bool {
let reference_node = &self.nodes()[node_id];
Expand Down