Skip to content

Commit b92decf

Browse files
authored
perf: skip no-redeclare scan for single-declaration variables (#21365)
* perf: skip no-redeclare scan for single-declaration variables findVariablesInScope ran the iterateDeclarations generator and a rest destructure for every variable, including every configured global. A variable with fewer than two declarations cannot be reported, so count them up front and skip the generator when there is at most one. * refactor: count no-redeclare declarations in getDeclarations * docs: clarify no-redeclare optimization comment
1 parent 66ac583 commit b92decf

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

‎lib/rules/no-redeclare.js‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,36 +52,47 @@ module.exports = {
5252
const sourceCode = context.sourceCode;
5353

5454
/**
55-
* Iterate declarations of a given variable.
56-
* @param {escope.variable} variable The variable object to iterate declarations.
57-
* @returns {IterableIterator<{type:string,node:ASTNode,loc:SourceLocation}>} The declarations.
55+
* Get declarations of a given variable.
56+
* @param {escope.variable} variable The variable object to get declarations of.
57+
* @returns {Array<{type:string,node:ASTNode,loc:SourceLocation}>|null} The declarations, or `null` if there are fewer than two.
5858
*/
59-
function* iterateDeclarations(variable) {
60-
if (
59+
function getDeclarations(variable) {
60+
const isBuiltin =
6161
builtinGlobals &&
6262
(variable.eslintImplicitGlobalSetting === "readonly" ||
63-
variable.eslintImplicitGlobalSetting === "writable")
64-
) {
65-
yield { type: "builtin" };
63+
variable.eslintImplicitGlobalSetting === "writable");
64+
65+
const count =
66+
(isBuiltin ? 1 : 0) +
67+
variable.identifiers.length +
68+
(variable.eslintExplicitGlobalComments?.length ?? 0);
69+
70+
// Every configured global lands here, so avoid allocating for variables that are not redeclared
71+
if (count < 2) {
72+
return null;
6673
}
6774

75+
const declarations = isBuiltin ? [{ type: "builtin" }] : [];
76+
6877
for (const id of variable.identifiers) {
69-
yield { type: "syntax", node: id, loc: id.loc };
78+
declarations.push({ type: "syntax", node: id, loc: id.loc });
7079
}
7180

7281
if (variable.eslintExplicitGlobalComments) {
7382
for (const comment of variable.eslintExplicitGlobalComments) {
74-
yield {
83+
declarations.push({
7584
type: "comment",
7685
node: comment,
7786
loc: astUtils.getNameLocationInGlobalDirectiveComment(
7887
sourceCode,
7988
comment,
8089
variable.name,
8190
),
82-
};
91+
});
8392
}
8493
}
94+
95+
return declarations;
8596
}
8697

8798
/**
@@ -92,13 +103,14 @@ module.exports = {
92103
*/
93104
function findVariablesInScope(scope) {
94105
for (const variable of scope.variables) {
95-
const [declaration, ...extraDeclarations] =
96-
iterateDeclarations(variable);
106+
const declarations = getDeclarations(variable);
97107

98-
if (extraDeclarations.length === 0) {
108+
if (!declarations) {
99109
continue;
100110
}
101111

112+
const [declaration, ...extraDeclarations] = declarations;
113+
102114
/*
103115
* If the type of a declaration is different from the type of
104116
* the first declaration, it shows the location of the first

‎tests/lib/rules/no-redeclare.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,5 +737,20 @@ ruleTester.run("no-redeclare", rule, {
737737
},
738738
],
739739
},
740+
{
741+
code: "/*globals a */ var a;",
742+
options: [{ builtinGlobals: true }],
743+
languageOptions: { globals: { a: "off" } },
744+
errors: [
745+
{
746+
message:
747+
"'a' is already defined by a variable declaration.",
748+
line: 1,
749+
column: 11,
750+
endLine: 1,
751+
endColumn: 12,
752+
},
753+
],
754+
},
740755
],
741756
});

0 commit comments

Comments
 (0)