Skip to content

Commit

Permalink
Fix: prevent fixing to incorrect code (fixes eslint#11069)
Browse files Browse the repository at this point in the history
  • Loading branch information
captain-yossarian committed Nov 11, 2018
1 parent e18c827 commit 9a8d8d6
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/rules/no-else-return.js
Expand Up @@ -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
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -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 !== ";") {
Expand Down

0 comments on commit 9a8d8d6

Please sign in to comment.