Skip to content

Commit

Permalink
Fix: no-var should not fix variables named 'let' (fixes #11830) (#11832)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and mysticatea committed Jun 18, 2019
1 parent 4d75956 commit 1b5661a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/rules/no-var.js
Expand Up @@ -174,6 +174,17 @@ function hasReferenceInTDZ(node) {
};
}

/**
* Checks whether a given variable has name that is allowed for 'var' declarations,
* but disallowed for `let` declarations.
*
* @param {eslint-scope.Variable} variable The variable to check.
* @returns {boolean} `true` if the variable has a disallowed name.
*/
function hasNameDisallowedForLetDeclarations(variable) {
return variable.name === "let";
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -223,6 +234,7 @@ module.exports = {
* - A variable might be used before it is assigned within a loop.
* - A variable might be used in TDZ.
* - A variable is declared in statement position (e.g. a single-line `IfStatement`)
* - A variable has name that is disallowed for `let` declarations.
*
* ## A variable is declared on a SwitchCase node.
*
Expand Down Expand Up @@ -271,7 +283,8 @@ module.exports = {
node.declarations.some(hasSelfReferenceInTDZ) ||
variables.some(isGlobal) ||
variables.some(isRedeclared) ||
variables.some(isUsedFromOutsideOf(scopeNode))
variables.some(isUsedFromOutsideOf(scopeNode)) ||
variables.some(hasNameDisallowedForLetDeclarations)
) {
return false;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/no-var.js
Expand Up @@ -307,6 +307,18 @@ ruleTester.run("no-var", rule, {
parser: require.resolve("../../fixtures/parsers/typescript-parsers/declare-var"),
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: ["Unexpected var, use let or const instead."]
},

// https://github.com/eslint/eslint/issues/11830
{
code: "function foo() { var let; }",
output: null,
errors: ["Unexpected var, use let or const instead."]
},
{
code: "function foo() { var { let } = {}; }",
output: null,
errors: ["Unexpected var, use let or const instead."]
}
]
});

0 comments on commit 1b5661a

Please sign in to comment.