From b60bdf1ff7cb9f9298ce05048bbd01c336309493 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 26 Jul 2024 00:14:59 +0000 Subject: [PATCH] perf(linter): `no_shadow_restricted_names` only look up name in hashmap once (#4472) `eslint(no_shadow_restricted_names)` lint rule emits a diagnostic for every declaration of a symbol with a restricted name. Currently for a var which has redeclarations, the var name is looked up in hash map of restricted names repeatedly for each redeclaration. This PR changes that to only do a single hashmap lookup. Also, if the var name is `undefined`, skip looking it up in hash map, because we already know it's a restricted name. --- .../eslint/no_shadow_restricted_names.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_shadow_restricted_names.rs b/crates/oxc_linter/src/rules/eslint/no_shadow_restricted_names.rs index e00c19700f847..6218dd5ec0035 100644 --- a/crates/oxc_linter/src/rules/eslint/no_shadow_restricted_names.rs +++ b/crates/oxc_linter/src/rules/eslint/no_shadow_restricted_names.rs @@ -36,18 +36,15 @@ declare_oxc_lint!( correctness ); -#[inline] -fn check_and_diagnostic(s: &str, span: Span, ctx: &LintContext) { - if PRE_DEFINE_VAR.contains_key(s) { - ctx.diagnostic(no_shadow_restricted_names_diagnostic(s, span)); - } -} - impl Rule for NoShadowRestrictedNames { fn run_once(&self, ctx: &LintContext<'_>) { ctx.symbols().iter().for_each(|symbol_id| { let name = ctx.symbols().get_name(symbol_id); + if !PRE_DEFINE_VAR.contains_key(name) { + return; + } + if name == "undefined" { // Allow to declare `undefined` variable but not allow to assign value to it. let node_id = ctx.semantic().symbols().get_declaration(symbol_id); @@ -63,9 +60,11 @@ impl Rule for NoShadowRestrictedNames { } } - check_and_diagnostic(name, ctx.symbols().get_span(symbol_id), ctx); - for span in ctx.symbols().get_redeclarations(symbol_id) { - check_and_diagnostic(name, *span, ctx); + let span = ctx.symbols().get_span(symbol_id); + ctx.diagnostic(no_shadow_restricted_names_diagnostic(name, span)); + + for &span in ctx.symbols().get_redeclarations(symbol_id) { + ctx.diagnostic(no_shadow_restricted_names_diagnostic(name, span)); } }); }