mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
perf(linter/unicorn/prefer-set-size): narrow AST dispatch (#28364)
Start from spreads and calls rather than every static member expression, retaining conversion, binding, and comment checks. Reduce fixture callbacks from 76,337 to 63,060. --- <sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
1 parent
a777fa6552
commit
a86158be60
3 files changed
+22
-4
No files matched your search
+1
-1
@@ -3920,7 +3920,7 @@ impl RuleRunner for crate::rules::unicorn::prefer_set_has::PreferSetHas {
|
|||||||
|
|
||||||
impl RuleRunner for crate::rules::unicorn::prefer_set_size::PreferSetSize {
|
impl RuleRunner for crate::rules::unicorn::prefer_set_size::PreferSetSize {
|
||||||
const NODE_TYPES: Option<&AstTypesBitset> =
|
const NODE_TYPES: Option<&AstTypesBitset> =
|
||||||
Some(&AstTypesBitset::from_types(&[AstType::StaticMemberExpression]));
|
Some(&AstTypesBitset::from_types(&[AstType::CallExpression, AstType::SpreadElement]));
|
||||||
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
|
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use oxc_span::{GetSpan, Span};
|
|||||||
use crate::{
|
use crate::{
|
||||||
AstNode,
|
AstNode,
|
||||||
ast_util::variable_declaration_kind,
|
ast_util::variable_declaration_kind,
|
||||||
ast_util::{get_declaration_of_variable, is_method_call},
|
ast_util::{get_declaration_of_variable, is_method_call, outermost_paren_parent},
|
||||||
context::LintContext,
|
context::LintContext,
|
||||||
fixer::Fix,
|
fixer::Fix,
|
||||||
rule::Rule,
|
rule::Rule,
|
||||||
@@ -57,7 +57,21 @@ declare_oxc_lint!(
|
|||||||
|
|
||||||
impl Rule for PreferSetSize {
|
impl Rule for PreferSetSize {
|
||||||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
|
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
|
||||||
let AstKind::StaticMemberExpression(member_expr) = node.kind() else {
|
let conversion_node = match node.kind() {
|
||||||
|
AstKind::SpreadElement(_) => {
|
||||||
|
let parent = ctx.nodes().parent_node(node.id());
|
||||||
|
if !matches!(parent.kind(), AstKind::ArrayExpression(_)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
parent
|
||||||
|
}
|
||||||
|
AstKind::CallExpression(_) => node,
|
||||||
|
_ => return,
|
||||||
|
};
|
||||||
|
let Some(parent) = outermost_paren_parent(conversion_node, ctx) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let AstKind::StaticMemberExpression(member_expr) = parent.kind() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -165,6 +179,10 @@ fn test() {
|
|||||||
|
|
||||||
let pass = vec![
|
let pass = vec![
|
||||||
"new Set(foo).size",
|
"new Set(foo).size",
|
||||||
|
"[...new Set(array), ...new Set(other)].length",
|
||||||
|
"foo(...new Set(array)).length",
|
||||||
|
"({...new Set(array)}).length",
|
||||||
|
"[...[...new Set(array)]].length",
|
||||||
"for (const foo of bar) console.log([...foo].length)",
|
"for (const foo of bar) console.log([...foo].length)",
|
||||||
"[...new Set(array), foo].length",
|
"[...new Set(array), foo].length",
|
||||||
"[foo, ...new Set(array), ].length",
|
"[foo, ...new Set(array), ].length",
|
||||||
|
|||||||
@@ -627,7 +627,7 @@ unicorn/prefer-reflect-apply | 62606
|
|||||||
unicorn/prefer-regexp-test | 62606
|
unicorn/prefer-regexp-test | 62606
|
||||||
unicorn/prefer-response-static-json | 1421
|
unicorn/prefer-response-static-json | 1421
|
||||||
unicorn/prefer-set-has | 37499
|
unicorn/prefer-set-has | 37499
|
||||||
unicorn/prefer-set-size | 76337
|
unicorn/prefer-set-size | 63060
|
||||||
unicorn/prefer-single-call | 24802
|
unicorn/prefer-single-call | 24802
|
||||||
unicorn/prefer-spread | 62606
|
unicorn/prefer-spread | 62606
|
||||||
unicorn/prefer-string-raw | 62460
|
unicorn/prefer-string-raw | 62460
|
||||||
|
|||||||
Reference in new issue
Block a user