Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: no-var should not fix variables named 'let' (fixes #11830) #11832

Merged
merged 1 commit into from Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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."]
}
]
});