diff --git a/lib/rules/no-else-return.js b/lib/rules/no-else-return.js index eebdec76e0e..44b5454c808 100644 --- a/lib/rules/no-else-return.js +++ b/lib/rules/no-else-return.js @@ -12,6 +12,20 @@ const astUtils = require("../util/ast-utils"); const FixTracker = require("../util/fix-tracker"); +//------------------------------------------------------------------------------ +// Helpers +//------------------------------------------------------------------------------ + +/** + * @param {Token} token - The token to check + * @returns {boolean} `true` if keyword let or const exist + */ +function isVariableDeclaration(token) { + const variableDeclaration = token.value === "let" || token.value === "const"; + + return token.type === "Keyword" && variableDeclaration; +} + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -88,6 +102,17 @@ module.exports = { } const endToken = sourceCode.getLastToken(node); + + /** + * If else block includes block scope local variable declaration [let or const], + * then it is not safe to remove else keyword [issue 11069] + */ + const isDeclarationInside = sourceCode.getTokensBetween(startToken, endToken).findIndex(isVariableDeclaration) > -1; + + if (isDeclarationInside) { + return null; + } + const lastTokenOfElseBlock = sourceCode.getTokenBefore(endToken); if (lastTokenOfElseBlock.value !== ";") {