Skip to content

Commit cf235ec

Browse files
committed
perf(linter): switch if !matches!(node.kind(), ...) to match block
1 parent 46ec4b6 commit cf235ec

File tree

6 files changed

+49
-13
lines changed

6 files changed

+49
-13
lines changed

crates/oxc_linter/src/generated/rule_runner_impls.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ impl RuleRunner for crate::rules::eslint::no_loss_of_precision::NoLossOfPrecisio
484484
}
485485

486486
impl RuleRunner for crate::rules::eslint::no_magic_numbers::NoMagicNumbers {
487-
const NODE_TYPES: Option<&AstTypesBitset> = None;
487+
const NODE_TYPES: Option<&AstTypesBitset> =
488+
Some(&AstTypesBitset::from_types(&[AstType::BigIntLiteral, AstType::NumericLiteral]));
488489
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
489490
}
490491

@@ -2307,7 +2308,8 @@ impl RuleRunner for crate::rules::react::react_in_jsx_scope::ReactInJsxScope {
23072308
}
23082309

23092310
impl RuleRunner for crate::rules::react::require_render_return::RequireRenderReturn {
2310-
const NODE_TYPES: Option<&AstTypesBitset> = None;
2311+
const NODE_TYPES: Option<&AstTypesBitset> =
2312+
Some(&AstTypesBitset::from_types(&[AstType::ArrowFunctionExpression, AstType::Function]));
23112313
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
23122314
}
23132315

@@ -2939,7 +2941,8 @@ impl RuleRunner for crate::rules::unicorn::empty_brace_spaces::EmptyBraceSpaces
29392941
}
29402942

29412943
impl RuleRunner for crate::rules::unicorn::error_message::ErrorMessage {
2942-
const NODE_TYPES: Option<&AstTypesBitset> = None;
2944+
const NODE_TYPES: Option<&AstTypesBitset> =
2945+
Some(&AstTypesBitset::from_types(&[AstType::CallExpression, AstType::NewExpression]));
29432946
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
29442947
}
29452948

@@ -3270,7 +3273,8 @@ impl RuleRunner
32703273
}
32713274

32723275
impl RuleRunner for crate::rules::unicorn::no_useless_spread::NoUselessSpread {
3273-
const NODE_TYPES: Option<&AstTypesBitset> = None;
3276+
const NODE_TYPES: Option<&AstTypesBitset> =
3277+
Some(&AstTypesBitset::from_types(&[AstType::ArrayExpression, AstType::ObjectExpression]));
32743278
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
32753279
}
32763280

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,9 @@ impl Rule for NoMagicNumbers {
289289
}
290290

291291
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
292-
if !matches!(node.kind(), AstKind::NumericLiteral(_) | AstKind::BigIntLiteral(_)) {
293-
return;
292+
match node.kind() {
293+
AstKind::NumericLiteral(_) | AstKind::BigIntLiteral(_) => {}
294+
_ => return,
294295
}
295296

296297
let nodes = ctx.nodes();

crates/oxc_linter/src/rules/react/require_render_return.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ declare_oxc_lint!(
7070

7171
impl Rule for RequireRenderReturn {
7272
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
73-
if !matches!(node.kind(), AstKind::ArrowFunctionExpression(_) | AstKind::Function(_)) {
74-
return;
73+
match node.kind() {
74+
AstKind::ArrowFunctionExpression(_) | AstKind::Function(_) => {}
75+
_ => return,
7576
}
77+
7678
let parent = ctx.nodes().parent_node(node.id());
7779
if !is_render_fn(parent) {
7880
return;

crates/oxc_linter/src/rules/typescript/explicit_function_return_type.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,6 @@ fn check_return_statements<'a>(statements: &'a [Statement<'a>]) -> bool {
722722
* ```
723723
*/
724724
fn is_property_of_object_with_type(node: &AstNode, ctx: &LintContext) -> bool {
725-
if !matches!(node.kind(), AstKind::ObjectProperty(_)) {
726-
return false;
727-
}
728725
if !matches!(node.kind(), AstKind::ObjectProperty(_)) {
729726
return false;
730727
}

crates/oxc_linter/src/rules/unicorn/no_useless_spread/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ declare_oxc_lint!(
145145

146146
impl Rule for NoUselessSpread {
147147
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
148-
if !matches!(node.kind(), AstKind::ArrayExpression(_) | AstKind::ObjectExpression(_)) {
149-
return;
148+
match node.kind() {
149+
AstKind::ArrayExpression(_) | AstKind::ObjectExpression(_) => {}
150+
_ => return,
150151
}
151152

152153
if check_useless_spread_in_list(node, ctx) {

tasks/linter_codegen/src/early_diverge_detector.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ impl EarlyDivergeDetector {
3434
}
3535
return Some(detector.node_types);
3636
}
37+
// Stand-alone `match node.kind() { ... }` that diverges
38+
else if let Stmt::Expr(Expr::Match(match_expr), _) = stmt
39+
&& is_node_kind_call(&match_expr.expr)
40+
{
41+
let mut detector = Self { node_types: NodeTypeSet::new() };
42+
let result = detector.extract_variants_from_diverging_match_expr(match_expr);
43+
if result == CollectionResult::Incomplete || detector.node_types.is_empty() {
44+
return None;
45+
}
46+
return Some(detector.node_types);
47+
}
3748

3849
None
3950
}
@@ -72,6 +83,26 @@ impl EarlyDivergeDetector {
7283
}
7384
CollectionResult::Incomplete
7485
}
86+
Pat::Or(or) => {
87+
let mut overall_result = CollectionResult::Complete;
88+
for case in &or.cases {
89+
let result = match case {
90+
Pat::TupleStruct(ts) => {
91+
if let Some(variant) = astkind_variant_from_path(&ts.path) {
92+
self.node_types.insert(variant);
93+
CollectionResult::Complete
94+
} else {
95+
CollectionResult::Incomplete
96+
}
97+
}
98+
_ => CollectionResult::Incomplete,
99+
};
100+
if result == CollectionResult::Incomplete {
101+
overall_result = CollectionResult::Incomplete;
102+
}
103+
}
104+
overall_result
105+
}
75106
_ => CollectionResult::Incomplete,
76107
}
77108
}

0 commit comments

Comments
 (0)