diff --git a/lib/rules/no-var.js b/lib/rules/no-var.js index e9f191b39f2..a3c750f9011 100644 --- a/lib/rules/no-var.js +++ b/lib/rules/no-var.js @@ -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 //------------------------------------------------------------------------------ @@ -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. * @@ -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; } diff --git a/tests/lib/rules/no-var.js b/tests/lib/rules/no-var.js index b47bf9097e9..d7576bfe3ce 100644 --- a/tests/lib/rules/no-var.js +++ b/tests/lib/rules/no-var.js @@ -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."] } ] });