Skip to content

Commit d9a751d

Browse files
committed
perf(linter): add codegen support for is_member_expression_kind in match
1 parent eb52529 commit d9a751d

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

crates/oxc_linter/src/generated/rule_runner_impls.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,13 @@ impl RuleRunner for crate::rules::eslint::no_new::NoNew {
530530
}
531531

532532
impl RuleRunner for crate::rules::eslint::no_new_func::NoNewFunc {
533-
const NODE_TYPES: Option<&AstTypesBitset> = None;
533+
const NODE_TYPES: Option<&AstTypesBitset> = Some(&AstTypesBitset::from_types(&[
534+
AstType::CallExpression,
535+
AstType::ComputedMemberExpression,
536+
AstType::NewExpression,
537+
AstType::PrivateFieldExpression,
538+
AstType::StaticMemberExpression,
539+
]));
534540
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
535541
}
536542

@@ -1069,7 +1075,12 @@ impl RuleRunner for crate::rules::import::no_anonymous_default_export::NoAnonymo
10691075
}
10701076

10711077
impl RuleRunner for crate::rules::import::no_commonjs::NoCommonjs {
1072-
const NODE_TYPES: Option<&AstTypesBitset> = None;
1078+
const NODE_TYPES: Option<&AstTypesBitset> = Some(&AstTypesBitset::from_types(&[
1079+
AstType::CallExpression,
1080+
AstType::ComputedMemberExpression,
1081+
AstType::PrivateFieldExpression,
1082+
AstType::StaticMemberExpression,
1083+
]));
10731084
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
10741085
}
10751086

@@ -2274,7 +2285,12 @@ impl RuleRunner for crate::rules::react::no_set_state::NoSetState {
22742285
}
22752286

22762287
impl RuleRunner for crate::rules::react::no_string_refs::NoStringRefs {
2277-
const NODE_TYPES: Option<&AstTypesBitset> = None;
2288+
const NODE_TYPES: Option<&AstTypesBitset> = Some(&AstTypesBitset::from_types(&[
2289+
AstType::ComputedMemberExpression,
2290+
AstType::JSXAttribute,
2291+
AstType::PrivateFieldExpression,
2292+
AstType::StaticMemberExpression,
2293+
]));
22782294
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
22792295
}
22802296

@@ -3487,7 +3503,13 @@ impl RuleRunner for crate::rules::unicorn::prefer_node_protocol::PreferNodeProto
34873503
}
34883504

34893505
impl RuleRunner for crate::rules::unicorn::prefer_number_properties::PreferNumberProperties {
3490-
const NODE_TYPES: Option<&AstTypesBitset> = None;
3506+
const NODE_TYPES: Option<&AstTypesBitset> = Some(&AstTypesBitset::from_types(&[
3507+
AstType::CallExpression,
3508+
AstType::ComputedMemberExpression,
3509+
AstType::IdentifierReference,
3510+
AstType::PrivateFieldExpression,
3511+
AstType::StaticMemberExpression,
3512+
]));
34913513
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
34923514
}
34933515

tasks/linter_codegen/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn detect_top_level_node_types(
131131
return Some(node_types);
132132
}
133133

134-
let node_types = MatchDetector::from_run_func(run_func);
134+
let node_types = MatchDetector::from_run_func(run_func, rule_runner_data);
135135
if let Some(node_types) = node_types
136136
&& !node_types.is_empty()
137137
{

tasks/linter_codegen/src/match_detector.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
use syn::{Arm, Expr, Pat, Stmt};
22

33
use crate::{
4-
CollectionResult,
4+
CollectionResult, RuleRunnerData,
55
node_type_set::NodeTypeSet,
66
utils::{astkind_variant_from_path, is_node_kind_call},
77
};
88

99
/// Detects top-level `match node.kind() { ... }` patterns in the `run` method.
10-
pub struct MatchDetector {
10+
pub struct MatchDetector<'a> {
1111
node_types: NodeTypeSet,
12+
rule_runner_data: &'a RuleRunnerData,
1213
}
1314

14-
impl MatchDetector {
15-
pub fn from_run_func(run_func: &syn::ImplItemFn) -> Option<NodeTypeSet> {
15+
impl<'a> MatchDetector<'a> {
16+
pub fn from_run_func(
17+
run_func: &syn::ImplItemFn,
18+
rule_runner_data: &'a RuleRunnerData,
19+
) -> Option<NodeTypeSet> {
1620
// Only consider when the body's only statement is `match node.kind() { ... }`
1721
let block = &run_func.block;
1822
if block.stmts.len() != 1 {
@@ -28,7 +32,7 @@ impl MatchDetector {
2832
return None;
2933
}
3034

31-
let mut detector = Self { node_types: NodeTypeSet::new() };
35+
let mut detector = Self { node_types: NodeTypeSet::new(), rule_runner_data };
3236
let result = detector.extract_variants_from_match_expr(match_expr);
3337
if detector.node_types.is_empty() || result == CollectionResult::Incomplete {
3438
return None;
@@ -82,6 +86,23 @@ impl MatchDetector {
8286
CollectionResult::Incomplete
8387
}
8488
}
89+
Pat::Ident(ident) => {
90+
if ident.subpat.is_some() {
91+
return CollectionResult::Incomplete;
92+
}
93+
// Look for a `member_expr if member_expr.is_member_expression_kind() => {` arm
94+
if let Some((_, guard_expr)) = &arm.guard
95+
&& let Expr::MethodCall(method_call) = &**guard_expr
96+
&& method_call.method == "is_member_expression_kind"
97+
&& method_call.args.is_empty()
98+
{
99+
// We have a match, so we can add MemberExpression to the set of node types
100+
self.node_types.extend(self.rule_runner_data.member_expression_kinds.clone());
101+
CollectionResult::Complete
102+
} else {
103+
CollectionResult::Incomplete
104+
}
105+
}
85106
_ => CollectionResult::Incomplete,
86107
}
87108
}

0 commit comments

Comments
 (0)