Skip to content

Commit 1b5661a

Browse files
mdjermanovicmysticatea
authored andcommitted
Fix: no-var should not fix variables named 'let' (fixes #11830) (#11832)
1 parent 4d75956 commit 1b5661a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/rules/no-var.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ function hasReferenceInTDZ(node) {
174174
};
175175
}
176176

177+
/**
178+
* Checks whether a given variable has name that is allowed for 'var' declarations,
179+
* but disallowed for `let` declarations.
180+
*
181+
* @param {eslint-scope.Variable} variable The variable to check.
182+
* @returns {boolean} `true` if the variable has a disallowed name.
183+
*/
184+
function hasNameDisallowedForLetDeclarations(variable) {
185+
return variable.name === "let";
186+
}
187+
177188
//------------------------------------------------------------------------------
178189
// Rule Definition
179190
//------------------------------------------------------------------------------
@@ -223,6 +234,7 @@ module.exports = {
223234
* - A variable might be used before it is assigned within a loop.
224235
* - A variable might be used in TDZ.
225236
* - A variable is declared in statement position (e.g. a single-line `IfStatement`)
237+
* - A variable has name that is disallowed for `let` declarations.
226238
*
227239
* ## A variable is declared on a SwitchCase node.
228240
*
@@ -271,7 +283,8 @@ module.exports = {
271283
node.declarations.some(hasSelfReferenceInTDZ) ||
272284
variables.some(isGlobal) ||
273285
variables.some(isRedeclared) ||
274-
variables.some(isUsedFromOutsideOf(scopeNode))
286+
variables.some(isUsedFromOutsideOf(scopeNode)) ||
287+
variables.some(hasNameDisallowedForLetDeclarations)
275288
) {
276289
return false;
277290
}

tests/lib/rules/no-var.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,18 @@ ruleTester.run("no-var", rule, {
307307
parser: require.resolve("../../fixtures/parsers/typescript-parsers/declare-var"),
308308
parserOptions: { ecmaVersion: 6, sourceType: "module" },
309309
errors: ["Unexpected var, use let or const instead."]
310+
},
311+
312+
// https://github.com/eslint/eslint/issues/11830
313+
{
314+
code: "function foo() { var let; }",
315+
output: null,
316+
errors: ["Unexpected var, use let or const instead."]
317+
},
318+
{
319+
code: "function foo() { var { let } = {}; }",
320+
output: null,
321+
errors: ["Unexpected var, use let or const instead."]
310322
}
311323
]
312324
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy