Skip to content
Permalink
Browse files

Fix: Curly rule incorrectly flagging lexical declarations (fixes #11663

…) (#11675)

* Fix: Curly rule incorrectly flagging lexical declarations (fixes #11663)

* Add invalid var single statement test case
  • Loading branch information...
BRKurek authored and ilyavolodin committed May 11, 2019
1 parent f42d0af commit 1a3a88df2f952c34631d8e1d83de47178826fce0
Showing with 52 additions and 1 deletion.
  1. +20 −1 lib/rules/curly.js
  2. +32 −0 tests/lib/rules/curly.js
@@ -103,6 +103,20 @@ module.exports = {
return first.loc.start.line === last.loc.end.line;
}

/**
* Determines if the given node is a lexical declaration (let, const, function, or class)
* @param {ASTNode} node The node to check
* @returns {boolean} True if the node is a lexical declaration
* @private
*/
function isLexicalDeclaration(node) {
if (node.type === "VariableDeclaration") {
return node.kind === "const" || node.kind === "let";
}

return node.type === "FunctionDeclaration" || node.type === "ClassDeclaration";
}

/**
* Checks if the given token is an `else` token or not.
*
@@ -238,8 +252,13 @@ module.exports = {
} else if (multiOrNest) {
if (hasBlock && body.body.length === 1 && isOneLiner(body.body[0])) {
const leadingComments = sourceCode.getCommentsBefore(body.body[0]);
const isLexDef = isLexicalDeclaration(body.body[0]);

expected = leadingComments.length > 0;
if (isLexDef) {
expected = true;
} else {
expected = leadingComments.length > 0;
}
} else if (!isOneLiner(body)) {
expected = true;
}
@@ -164,6 +164,26 @@ ruleTester.run("curly", rule, {
options: ["multi-or-nest"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "if (foo) { \n const bar = 'baz'; \n }",
options: ["multi-or-nest"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "if (foo) { \n let bar = 'baz'; \n }",
options: ["multi-or-nest"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "if (foo) { \n function bar() {} \n }",
options: ["multi-or-nest"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "if (foo) { \n class bar {} \n }",
options: ["multi-or-nest"],
parserOptions: { ecmaVersion: 6 }
},

// https://github.com/eslint/eslint/issues/3856
{
@@ -596,6 +616,18 @@ ruleTester.run("curly", rule, {
}
]
},
{
code: "if (foo) { \n var bar = 'baz'; \n }",
output: "if (foo) \n var bar = 'baz'; \n ",
options: ["multi-or-nest"],
errors: [
{
messageId: "unexpectedCurlyAfterCondition",
data: { name: "if" },
type: "IfStatement"
}
]
},
{
code: "while (true) { \n doSomething(); \n }",
output: "while (true) \n doSomething(); \n ",

0 comments on commit 1a3a88d

Please sign in to comment.
You can’t perform that action at this time.