Skip to content

Commit 801bb10

Browse files
committed
perf(linter): refactor no-labels to use top-level match on node kind (#14688)
Using a single `match node.kind()` should be a bit faster and simpler, and also allows node type analysis.
1 parent 1217411 commit 801bb10

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

crates/oxc_linter/src/generated/rule_runner_impls.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,11 @@ impl RuleRunner for crate::rules::eslint::no_label_var::NoLabelVar {
465465
}
466466

467467
impl RuleRunner for crate::rules::eslint::no_labels::NoLabels {
468-
const NODE_TYPES: Option<&AstTypesBitset> = None;
468+
const NODE_TYPES: Option<&AstTypesBitset> = Some(&AstTypesBitset::from_types(&[
469+
AstType::BreakStatement,
470+
AstType::ContinueStatement,
471+
AstType::LabeledStatement,
472+
]));
469473
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
470474
}
471475

crates/oxc_linter/src/rules/eslint/no_labels.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,33 +141,34 @@ impl Rule for NoLabels {
141141
}
142142

143143
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
144-
if let AstKind::LabeledStatement(labeled_stmt) = node.kind()
145-
&& !self.is_allowed(&labeled_stmt.body)
146-
{
147-
let label_span = labeled_stmt.label.span;
148-
ctx.diagnostic(no_labels_diagnostic("Labeled statement is not allowed", label_span));
149-
}
150-
151-
if let AstKind::BreakStatement(break_stmt) = node.kind() {
152-
let Some(label) = &break_stmt.label else { return };
153-
154-
if !self.is_allowed_in_break_or_continue(label, node.id(), ctx) {
144+
match node.kind() {
145+
AstKind::LabeledStatement(labeled_stmt) if !self.is_allowed(&labeled_stmt.body) => {
155146
ctx.diagnostic(no_labels_diagnostic(
156-
"Label in break statement is not allowed",
157-
label.span,
147+
"Labeled statement is not allowed",
148+
labeled_stmt.label.span,
158149
));
159150
}
160-
}
161-
162-
if let AstKind::ContinueStatement(cont_stmt) = node.kind() {
163-
let Some(label) = &cont_stmt.label else { return };
164-
165-
if !self.is_allowed_in_break_or_continue(label, node.id(), ctx) {
166-
ctx.diagnostic(no_labels_diagnostic(
167-
"Label in continue statement is not allowed",
168-
label.span,
169-
));
151+
AstKind::BreakStatement(break_stmt) => {
152+
if let Some(label) = &break_stmt.label
153+
&& !self.is_allowed_in_break_or_continue(label, node.id(), ctx)
154+
{
155+
ctx.diagnostic(no_labels_diagnostic(
156+
"Label in break statement is not allowed",
157+
label.span,
158+
));
159+
}
160+
}
161+
AstKind::ContinueStatement(cont_stmt) => {
162+
if let Some(label) = &cont_stmt.label
163+
&& !self.is_allowed_in_break_or_continue(label, node.id(), ctx)
164+
{
165+
ctx.diagnostic(no_labels_diagnostic(
166+
"Label in continue statement is not allowed",
167+
label.span,
168+
));
169+
}
170170
}
171+
_ => {}
171172
}
172173
}
173174
}

0 commit comments

Comments
 (0)