mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
perf(linter/eslint/no-const-assign): batch by declaration (#28356)
Check declaration kinds once and process each bound name without repeated parent lookups. Reduce fixture callbacks from 37,499 to 31,363. --- <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
1ec9991cb2
commit
d65531f41c
4 files changed
+38
-9
No files matched your search
+1
-1
@@ -550,7 +550,7 @@ impl RuleRunner for crate::rules::eslint::no_console::NoConsole {
|
||||
|
||||
impl RuleRunner for crate::rules::eslint::no_const_assign::NoConstAssign {
|
||||
const NODE_TYPES: Option<&AstTypesBitset> =
|
||||
Some(&AstTypesBitset::from_types(&[AstType::VariableDeclarator]));
|
||||
Some(&AstTypesBitset::from_types(&[AstType::VariableDeclaration]));
|
||||
const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ use oxc_macros::declare_oxc_lint;
|
||||
use oxc_semantic::{AstNode, SymbolId};
|
||||
use oxc_span::Span;
|
||||
|
||||
use crate::{ast_util::variable_declaration_kind, context::LintContext, rule::Rule};
|
||||
use crate::{context::LintContext, rule::Rule};
|
||||
|
||||
fn no_const_assign_diagnostic(name: &str, decl_span: Span, assign_span: Span) -> OxcDiagnostic {
|
||||
OxcDiagnostic::warn(format!("Unexpected re-assignment of `const` variable {name}."))
|
||||
@@ -61,12 +61,11 @@ declare_oxc_lint!(
|
||||
|
||||
impl Rule for NoConstAssign {
|
||||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
|
||||
let AstKind::VariableDeclarator(decl) = node.kind() else { return };
|
||||
let Some(kind) = variable_declaration_kind(decl, ctx.nodes()) else {
|
||||
return;
|
||||
};
|
||||
if kind.is_const() || kind.is_using() {
|
||||
decl.id.bound_names(&mut |ident| check_symbol_id(ident.symbol_id(), ctx));
|
||||
let AstKind::VariableDeclaration(declaration) = node.kind() else { return };
|
||||
if declaration.kind.is_const() || declaration.kind.is_using() {
|
||||
for declarator in &declaration.declarations {
|
||||
declarator.id.bound_names(&mut |ident| check_symbol_id(ident.symbol_id(), ctx));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,6 +111,7 @@ fn test() {
|
||||
"using x = foo(); bar(x);",
|
||||
"await using x = foo(); bar(x);",
|
||||
"type t = (a, ...b) => void",
|
||||
"let a = 1, b = 2; a = 3; b = 4;",
|
||||
];
|
||||
|
||||
let fail = vec![
|
||||
@@ -142,6 +142,8 @@ fn test() {
|
||||
"await using x = foo(); [x = baz, y] = bar();",
|
||||
"using x = foo(); ({a: x} = bar());",
|
||||
"await using x = foo(); ({a: x = baz} = bar());",
|
||||
"const a = 1, {b} = source; a = 2; b = 3;",
|
||||
"using a = foo(), b = bar(); b = baz();",
|
||||
];
|
||||
|
||||
Tester::new(NoConstAssign::NAME, NoConstAssign::PLUGIN, pass, fail).test_and_snapshot();
|
||||
|
||||
@@ -226,3 +226,30 @@ source: crates/oxc_linter/src/tester.rs
|
||||
· ╰── x is declared here as `const`.
|
||||
╰────
|
||||
help: Use `let` instead of `const` if you need to reassign this variable.
|
||||
|
||||
⚠ eslint(no-const-assign): Unexpected re-assignment of `const` variable a.
|
||||
╭─[no_const_assign.tsx:1:7]
|
||||
1 │ const a = 1, {b} = source; a = 2; b = 3;
|
||||
· ┬ ┬
|
||||
· │ ╰── a is re-assigned here.
|
||||
· ╰── a is declared here as `const`.
|
||||
╰────
|
||||
help: Use `let` instead of `const` if you need to reassign this variable.
|
||||
|
||||
⚠ eslint(no-const-assign): Unexpected re-assignment of `const` variable b.
|
||||
╭─[no_const_assign.tsx:1:15]
|
||||
1 │ const a = 1, {b} = source; a = 2; b = 3;
|
||||
· ┬ ┬
|
||||
· │ ╰── b is re-assigned here.
|
||||
· ╰── b is declared here as `const`.
|
||||
╰────
|
||||
help: Use `let` instead of `const` if you need to reassign this variable.
|
||||
|
||||
⚠ eslint(no-const-assign): Unexpected re-assignment of `const` variable b.
|
||||
╭─[no_const_assign.tsx:1:18]
|
||||
1 │ using a = foo(), b = bar(); b = baz();
|
||||
· ┬ ┬
|
||||
· │ ╰── b is re-assigned here.
|
||||
· ╰── b is declared here as `const`.
|
||||
╰────
|
||||
help: Use `let` instead of `const` if you need to reassign this variable.
|
||||
@@ -42,7 +42,7 @@ eslint/no-class-assign | 300
|
||||
eslint/no-compare-neg-zero | 9106
|
||||
eslint/no-cond-assign | 31699
|
||||
eslint/no-console | 95297
|
||||
eslint/no-const-assign | 37499
|
||||
eslint/no-const-assign | 31363
|
||||
eslint/no-constant-binary-expression | 32723
|
||||
eslint/no-constant-condition | 18694
|
||||
eslint/no-constructor-return | 14199
|
||||
|
||||
Reference in new issue
Block a user