diff options
Diffstat (limited to 'tools/eslint/lib/rules/curly.js')
-rw-r--r-- | tools/eslint/lib/rules/curly.js | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/tools/eslint/lib/rules/curly.js b/tools/eslint/lib/rules/curly.js index 801552d69e..cd15b2d935 100644 --- a/tools/eslint/lib/rules/curly.js +++ b/tools/eslint/lib/rules/curly.js @@ -76,7 +76,7 @@ module.exports = { function isCollapsedOneLiner(node) { const before = sourceCode.getTokenBefore(node); const last = sourceCode.getLastToken(node); - const lastExcludingSemicolon = last.type === "Punctuator" && last.value === ";" ? sourceCode.getTokenBefore(last) : last; + const lastExcludingSemicolon = astUtils.isSemicolonToken(last) ? sourceCode.getTokenBefore(last) : last; return before.loc.start.line === lastExcludingSemicolon.loc.end.line; } @@ -95,18 +95,22 @@ module.exports = { } /** + * Checks if the given token is an `else` token or not. + * + * @param {Token} token - The token to check. + * @returns {boolean} `true` if the token is an `else` token. + */ + function isElseKeywordToken(token) { + return token.value === "else" && token.type === "Keyword"; + } + + /** * Gets the `else` keyword token of a given `IfStatement` node. * @param {ASTNode} node - A `IfStatement` node to get. * @returns {Token} The `else` keyword token. */ function getElseKeyword(node) { - let token = sourceCode.getTokenAfter(node.consequent); - - while (token.type !== "Keyword" || token.value !== "else") { - token = sourceCode.getTokenAfter(token); - } - - return token; + return node.alternate && sourceCode.getFirstTokenBetween(node.consequent, node.alternate, isElseKeywordToken); } /** @@ -170,7 +174,7 @@ module.exports = { const tokenAfter = sourceCode.getTokenAfter(closingBracket); const lastBlockNode = sourceCode.getNodeByRangeIndex(tokenBefore.range[0]); - if (tokenBefore.value === ";") { + if (astUtils.isSemicolonToken(tokenBefore)) { // If the last statement already has a semicolon, don't add another one. return false; |