From b3330be4930a8c4d2f9d65b599d73d6e94cf9ba2 Mon Sep 17 00:00:00 2001 From: Anix Date: Mon, 11 May 2020 08:38:47 +0000 Subject: [PATCH 1/7] Update: Changed reporting location (refs #12334) --- lib/rules/curly.js | 100 ++++++++++++- tests/lib/rules/curly.js | 301 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 393 insertions(+), 8 deletions(-) diff --git a/lib/rules/curly.js b/lib/rules/curly.js index 29f00c0ad0b..70ca42e7e6d 100644 --- a/lib/rules/curly.js +++ b/lib/rules/curly.js @@ -309,6 +309,98 @@ module.exports = { hasUnsafeIf(statement) && isFollowedByElseKeyword(node); } + /** + * Examples: + * + * The following location should be reported + * + * if (foo) bar() + * ^ + * + * while(foo) bar(); + * ^ + * + * for(;;) bar(); + * ^ + * + * for (var foo of bar) console.log(foo) + * ^ + * + * for (var foo in bar) console.log(foo) + * ^ + * + * Get the location of missing parent location where it should have been placed. + * @param {ASTNode} node the node to check. + * @param {string} name name to check. + * @returns {Object} Position object. + */ + function getMissingLoc(node, name) { + switch (name) { + case "else": + return getElseKeyword(node).loc.start; + case "if": + return astUtils.getNextLocation(sourceCode, node.test.loc.end); + case "while": + return astUtils.getNextLocation(sourceCode, node.test.loc.end); + case "for": + return node.body.loc.start; + case "for-of": + return node.body.loc.start; + case "do": + return node.body.loc.start; + case "for-in": + return node.body.loc.start; + default: + return node.loc; + } + } + + /** + * Examples: + * + * The following location should be reported + * + * if (true) foo(); else { baz(); } + * ^ + * + * for (;;) { foo(); } + * ^ + * + * while (bar) { foo(); } + * ^ + * + * do{foo();} while(bar); + * ^ + * + * for (var foo of bar) {console.log(foo)} + * ^ + * + * Get the location of unexpected parens. mostly the starting parens `{` + * @param {ASTNode} node the node to check. + * @param {string} name name to check. + * @returns {Object} Position object. + */ + function getUnexpectedParenLoc(node, name) { + switch (name) { + case "else": + return getElseKeyword(node).loc.start; + case "if": + return node.consequent.loc.start; + case "while": + return node.body.loc.start; + case "for": + return node.body.loc.start; + case "for-of": + return node.body.loc.start; + case "do": + return node.body.loc.start; + case "for-in": + return node.body.loc.start; + default: + return node.loc; + } + } + /** * Prepares to check the body of a node to see if it's a block statement. * @param {ASTNode} node The node to report if there's a problem. @@ -359,9 +451,11 @@ module.exports = { check() { if (this.expected !== null && this.expected !== this.actual) { if (this.expected) { + const loc = getMissingLoc(node, name); + context.report({ node, - loc: (name !== "else" ? node : getElseKeyword(node)).loc.start, + loc, messageId: opts && opts.condition ? "missingCurlyAfterCondition" : "missingCurlyAfter", data: { name @@ -369,9 +463,11 @@ module.exports = { fix: fixer => fixer.replaceText(body, `{${sourceCode.getText(body)}}`) }); } else { + const loc = getUnexpectedParenLoc(node, name); + context.report({ node, - loc: (name !== "else" ? node : getElseKeyword(node)).loc.start, + loc, messageId: opts && opts.condition ? "unexpectedCurlyAfterCondition" : "unexpectedCurlyAfter", data: { name diff --git a/tests/lib/rules/curly.js b/tests/lib/rules/curly.js index 1d5ee8c6e4f..bc30daedf36 100644 --- a/tests/lib/rules/curly.js +++ b/tests/lib/rules/curly.js @@ -429,7 +429,22 @@ ruleTester.run("curly", rule, { { messageId: "missingCurlyAfterCondition", data: { name: "if" }, - type: "IfStatement" + type: "IfStatement", + line: 1, + column: 9 + } + ] + }, + { + code: "if (foo) \n bar()", + output: "if (foo) \n {bar()}", + errors: [ + { + messageId: "missingCurlyAfterCondition", + data: { name: "if" }, + type: "IfStatement", + line: 1, + column: 9 } ] }, @@ -462,7 +477,22 @@ ruleTester.run("curly", rule, { { messageId: "missingCurlyAfterCondition", data: { name: "while" }, - type: "WhileStatement" + type: "WhileStatement", + line: 1, + column: 12 + } + ] + }, + { + code: "while (foo) \n bar()", + output: "while (foo) \n {bar()}", + errors: [ + { + messageId: "missingCurlyAfterCondition", + data: { name: "while" }, + type: "WhileStatement", + line: 1, + column: 12 } ] }, @@ -507,7 +537,81 @@ ruleTester.run("curly", rule, { { messageId: "missingCurlyAfter", data: { name: "for-of" }, - type: "ForOfStatement" + type: "ForOfStatement", + line: 1, + column: 22 + } + ] + }, + { + code: "for (var foo of bar) \n console.log(foo)", + output: "for (var foo of bar) \n {console.log(foo)}", + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "missingCurlyAfter", + data: { name: "for-of" }, + type: "ForOfStatement", + line: 2, + column: 2 + } + ] + }, + { + code: "for (a;;) console.log(foo)", + output: "for (a;;) {console.log(foo)}", + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "missingCurlyAfterCondition", + data: { name: "for" }, + type: "ForStatement", + line: 1, + column: 11 + } + ] + }, + { + code: "for (a;;) \n console.log(foo)", + output: "for (a;;) \n {console.log(foo)}", + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "missingCurlyAfterCondition", + data: { name: "for" }, + type: "ForStatement", + line: 2, + column: 2 + } + ] + }, + { + code: "for (var foo of bar) {console.log(foo)}", + output: "for (var foo of bar) console.log(foo)", + options: ["multi"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "unexpectedCurlyAfter", + data: { name: "for-of" }, + type: "ForOfStatement", + line: 1, + column: 22 + } + ] + }, + { + code: "do{foo();} while(bar);", + output: "do foo(); while(bar);", + options: ["multi"], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "unexpectedCurlyAfter", + data: { name: "do" }, + type: "DoWhileStatement", + line: 1, + column: 3 } ] }, @@ -706,6 +810,20 @@ ruleTester.run("curly", rule, { } ] }, + { + code: "do foo(); while (bar)", + output: "do {foo();} while (bar)", + options: ["all"], + errors: [ + { + messageId: "missingCurlyAfter", + data: { name: "do" }, + type: "DoWhileStatement", + line: 1, + column: 4 + } + ] + }, { code: "do \n foo(); \n while (bar)", output: "do \n {foo();} \n while (bar)", @@ -714,7 +832,23 @@ ruleTester.run("curly", rule, { { messageId: "missingCurlyAfter", data: { name: "do" }, - type: "DoWhileStatement" + type: "DoWhileStatement", + line: 2, + column: 2 + } + ] + }, + { + code: "for (var foo in bar) {console.log(foo)}", + output: "for (var foo in bar) console.log(foo)", + options: ["multi"], + errors: [ + { + messageId: "unexpectedCurlyAfter", + data: { name: "for-in" }, + type: "ForInStatement", + line: 1, + column: 22 } ] }, @@ -878,6 +1012,34 @@ ruleTester.run("curly", rule, { } ] }, + { + code: "for (var foo in bar) if (foo) console.log(1); else console.log(2);", + output: "for (var foo in bar) {if (foo) console.log(1); else console.log(2);}", + options: ["all"], + errors: [ + { + line: 1, + column: 22, + messageId: "missingCurlyAfter", + data: { name: "for-in" }, + type: "ForInStatement" + }, + { + line: 1, + column: 30, + messageId: "missingCurlyAfterCondition", + data: { name: "if" }, + type: "IfStatement" + }, + { + line: 1, + column: 47, + messageId: "missingCurlyAfter", + data: { name: "else" }, + type: "IfStatement" + } + ] + }, { code: "for (var foo in bar) \n if (foo) console.log(1); \n else console.log(2);", output: "for (var foo in bar) \n {if (foo) console.log(1); \n else console.log(2);}", @@ -886,7 +1048,9 @@ ruleTester.run("curly", rule, { { messageId: "missingCurlyAfter", data: { name: "for-in" }, - type: "ForInStatement" + type: "ForInStatement", + line: 2, + column: 2 } ] }, @@ -960,7 +1124,9 @@ ruleTester.run("curly", rule, { { messageId: "unexpectedCurlyAfter", data: { name: "else" }, - type: "IfStatement" + type: "IfStatement", + line: 1, + column: 18 } ] }, @@ -993,6 +1159,62 @@ ruleTester.run("curly", rule, { } ] }, + { + code: "do\n{foo();} while (bar)", + output: "do\nfoo(); while (bar)", + options: ["multi"], + errors: [ + { + messageId: "unexpectedCurlyAfter", + data: { name: "do" }, + type: "DoWhileStatement", + line: 2, + column: 1 + } + ] + }, + { + code: "while (bar) { foo(); }", + output: "while (bar) foo(); ", + options: ["multi"], + errors: [ + { + messageId: "unexpectedCurlyAfterCondition", + data: { name: "while" }, + type: "WhileStatement", + line: 1, + column: 13 + } + ] + }, + { + code: "while (bar) \n{\n foo(); }", + output: "while (bar) \n\n foo(); ", + options: ["multi"], + errors: [ + { + messageId: "unexpectedCurlyAfterCondition", + data: { name: "while" }, + type: "WhileStatement", + line: 2, + column: 1 + } + ] + }, + { + code: "for (;;) { foo(); }", + output: "for (;;) foo(); ", + options: ["multi"], + errors: [ + { + messageId: "unexpectedCurlyAfterCondition", + data: { name: "for" }, + type: "ForStatement", + line: 1, + column: 10 + } + ] + }, { code: "do{[1, 2, 3].map(bar);} while (bar)", output: "do[1, 2, 3].map(bar); while (bar)", @@ -1267,6 +1489,73 @@ ruleTester.run("curly", rule, { options: ["multi", "consistent"], errors: [{ messageId: "missingCurlyAfter", data: { name: "else" }, type: "IfStatement" }] }, + { + code: "if (a) { while (cond) if (b) foo() } ", + output: "if (a) while (cond) if (b) foo() ", + options: ["multi", "consistent"], + errors: [{ + messageId: "unexpectedCurlyAfterCondition", + line: 1, + column: 8, + data: { name: "if" }, + type: "IfStatement" + }] + }, + { + code: "if(a) { if (b) foo(); } if (c) bar(); else if(foo){bar();}", + output: "if(a) if (b) foo(); if (c) bar(); else if(foo)bar();", + options: ["multi-or-nest"], + errors: [{ + line: 1, + column: 7, + type: "IfStatement", + data: { name: "if" }, + messageId: "unexpectedCurlyAfterCondition" + }, + { + line: 1, + column: 51, + type: "IfStatement", + data: { name: "if" }, + messageId: "unexpectedCurlyAfterCondition" + }] + }, + { + code: "if (true) [1, 2, 3]\n.bar()", + output: "if (true) {[1, 2, 3]\n.bar()}", + options: ["multi-line"], + errors: [{ + line: 1, + column: 10, + data: { name: "if" }, + type: "IfStatement", + messageId: "missingCurlyAfterCondition" + }] + }, + { + code: "for(\n;\n;\n) {foo()}", + output: "for(\n;\n;\n) foo()", + options: ["multi"], + errors: [{ + line: 4, + column: 3, + data: { name: "for" }, + type: "ForStatement", + messageId: "unexpectedCurlyAfterCondition" + }] + }, + { + code: "for(\n;\n;\n) \nfoo()\n", + output: "for(\n;\n;\n) \n{foo()}\n", + options: ["multi-line"], + errors: [{ + line: 5, + column: 1, + data: { name: "for" }, + type: "ForStatement", + messageId: "missingCurlyAfterCondition" + }] + }, { /** From 457afc7f6d051c02b8ec3593fb53a5b212581a74 Mon Sep 17 00:00:00 2001 From: Anix Date: Tue, 12 May 2020 06:49:10 +0000 Subject: [PATCH 2/7] Update: abstraction for location function --- lib/rules/curly.js | 80 +++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 50 deletions(-) diff --git a/lib/rules/curly.js b/lib/rules/curly.js index 70ca42e7e6d..09b1ae32a3f 100644 --- a/lib/rules/curly.js +++ b/lib/rules/curly.js @@ -10,6 +10,12 @@ const astUtils = require("./utils/ast-utils"); +//------------------------------------------------------------------------------ +// Helper +//------------------------------------------------------------------------------ + +const blockStartLoc = new Set(["for", "for-of", "do", "for-in"]); + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -310,7 +316,7 @@ module.exports = { } /** - * Examples: + * Missing curly brace reporting location examples: * * The following location should be reported * @@ -329,34 +335,7 @@ module.exports = { * for (var foo in bar) console.log(foo) * ^ * - * Get the location of missing parent location where it should have been placed. - * @param {ASTNode} node the node to check. - * @param {string} name name to check. - * @returns {Object} Position object. - */ - function getMissingLoc(node, name) { - switch (name) { - case "else": - return getElseKeyword(node).loc.start; - case "if": - return astUtils.getNextLocation(sourceCode, node.test.loc.end); - case "while": - return astUtils.getNextLocation(sourceCode, node.test.loc.end); - case "for": - return node.body.loc.start; - case "for-of": - return node.body.loc.start; - case "do": - return node.body.loc.start; - case "for-in": - return node.body.loc.start; - default: - return node.loc; - } - } - - /** - * Examples: + * Unexpected curly brace reporting location examples: * * The following location should be reported * @@ -375,30 +354,31 @@ module.exports = { * for (var foo of bar) {console.log(foo)} * ^ * - * Get the location of unexpected parens. mostly the starting parens `{` + * Get the location of missing or non missing curly brace. * @param {ASTNode} node the node to check. * @param {string} name name to check. + * @param {boolean} isMissing is curly brace missing. * @returns {Object} Position object. */ - function getUnexpectedParenLoc(node, name) { - switch (name) { - case "else": - return getElseKeyword(node).loc.start; - case "if": - return node.consequent.loc.start; - case "while": - return node.body.loc.start; - case "for": - return node.body.loc.start; - case "for-of": - return node.body.loc.start; - case "do": - return node.body.loc.start; - case "for-in": - return node.body.loc.start; - default: - return node.loc; + function getReportLoc(node, name, isMissing) { + + if (name === "else") { + return getElseKeyword(node).loc.start; + } + + if (blockStartLoc.has(name)) { + return node.body.loc.start; } + + if (name === "if") { + return isMissing ? astUtils.getNextLocation(sourceCode, node.test.loc.end) : node.consequent.loc.start; + } + + if (name === "while") { + return isMissing ? astUtils.getNextLocation(sourceCode, node.test.loc.end) : node.body.loc.start; + } + + return node.loc; } /** @@ -451,7 +431,7 @@ module.exports = { check() { if (this.expected !== null && this.expected !== this.actual) { if (this.expected) { - const loc = getMissingLoc(node, name); + const loc = getReportLoc(node, name, true); context.report({ node, @@ -463,7 +443,7 @@ module.exports = { fix: fixer => fixer.replaceText(body, `{${sourceCode.getText(body)}}`) }); } else { - const loc = getUnexpectedParenLoc(node, name); + const loc = getReportLoc(node, name, false); context.report({ node, From 5daaf9b3f5dff58816dcf0674332658bca132903 Mon Sep 17 00:00:00 2001 From: Anix Date: Fri, 15 May 2020 06:01:19 +0000 Subject: [PATCH 3/7] Update: location for unexpected and missing curly brace --- lib/rules/curly.js | 62 +++++++++++++---- tests/lib/rules/curly.js | 143 ++++++++++++++++++++++++++++++++------- 2 files changed, 168 insertions(+), 37 deletions(-) diff --git a/lib/rules/curly.js b/lib/rules/curly.js index 09b1ae32a3f..e8064d37e39 100644 --- a/lib/rules/curly.js +++ b/lib/rules/curly.js @@ -16,6 +16,22 @@ const astUtils = require("./utils/ast-utils"); const blockStartLoc = new Set(["for", "for-of", "do", "for-in"]); +/** + * Returns location of given position with `end` position next to `start` position. + * @param {Position} position position whose very next needs to be created. + * @returns {Location} the new location. + */ +function createNextLocation(position) { + + return { + start: position, + end: { + line: position.line, + column: position.column + 1 + } + }; +} + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -321,38 +337,41 @@ module.exports = { * The following location should be reported * * if (foo) bar() - * ^ + * ^^ * * while(foo) bar(); - * ^ + * ^^ + * + * do foo() while(bar) + * ^^^ * * for(;;) bar(); - * ^ + * ^^ * * for (var foo of bar) console.log(foo) - * ^ + * ^^ * * for (var foo in bar) console.log(foo) - * ^ + * ^^ * * Unexpected curly brace reporting location examples: * * The following location should be reported * * if (true) foo(); else { baz(); } - * ^ + * ^^ * * for (;;) { foo(); } - * ^ + * ^^ * * while (bar) { foo(); } - * ^ + * ^^ * * do{foo();} while(bar); - * ^ + * ^^ * * for (var foo of bar) {console.log(foo)} - * ^ + * ^^ * * Get the location of missing or non missing curly brace. * @param {ASTNode} node the node to check. @@ -366,16 +385,33 @@ module.exports = { return getElseKeyword(node).loc.start; } + if (isMissing) { + + if (name === "for" || name === "do") { + const token = sourceCode.getTokenBefore(node.body); + + return token.loc; + } + + if (name === "for-of" || name === "for-in") { + const token = sourceCode.getTokenAfter(node.right); + + return token.loc; + } + + } + + if (blockStartLoc.has(name)) { - return node.body.loc.start; + return createNextLocation(node.body.loc.start); } if (name === "if") { - return isMissing ? astUtils.getNextLocation(sourceCode, node.test.loc.end) : node.consequent.loc.start; + return isMissing ? createNextLocation(node.test.loc.end) : createNextLocation(node.consequent.loc.start); } if (name === "while") { - return isMissing ? astUtils.getNextLocation(sourceCode, node.test.loc.end) : node.body.loc.start; + return isMissing ? createNextLocation(astUtils.getNextLocation(sourceCode, node.test.loc.end)) : createNextLocation(node.body.loc.start); } return node.loc; diff --git a/tests/lib/rules/curly.js b/tests/lib/rules/curly.js index bc30daedf36..acad42d89ab 100644 --- a/tests/lib/rules/curly.js +++ b/tests/lib/rules/curly.js @@ -431,7 +431,9 @@ ruleTester.run("curly", rule, { data: { name: "if" }, type: "IfStatement", line: 1, - column: 9 + column: 8, + endColumn: 9, + endLine: 1 } ] }, @@ -444,7 +446,9 @@ ruleTester.run("curly", rule, { data: { name: "if" }, type: "IfStatement", line: 1, - column: 9 + column: 8, + endColumn: 9, + endLine: 1 } ] }, @@ -479,7 +483,9 @@ ruleTester.run("curly", rule, { data: { name: "while" }, type: "WhileStatement", line: 1, - column: 12 + column: 12, + endLine: 1, + endColumn: 13 } ] }, @@ -503,7 +509,26 @@ ruleTester.run("curly", rule, { { messageId: "missingCurlyAfter", data: { name: "do" }, - type: "DoWhileStatement" + type: "DoWhileStatement", + line: 1, + column: 1, + endColumn: 3, + endLine: 1 + } + ] + }, + { + code: "do \n bar(); while (foo)", + output: "do \n {bar();} while (foo)", + errors: [ + { + messageId: "missingCurlyAfter", + data: { name: "do" }, + type: "DoWhileStatement", + line: 1, + column: 1, + endColumn: 3, + endLine: 1 } ] }, @@ -539,7 +564,9 @@ ruleTester.run("curly", rule, { data: { name: "for-of" }, type: "ForOfStatement", line: 1, - column: 22 + column: 20, + endLine: 1, + endColumn: 21 } ] }, @@ -552,8 +579,10 @@ ruleTester.run("curly", rule, { messageId: "missingCurlyAfter", data: { name: "for-of" }, type: "ForOfStatement", - line: 2, - column: 2 + line: 1, + column: 20, + endColumn: 21, + endLine: 1 } ] }, @@ -567,7 +596,9 @@ ruleTester.run("curly", rule, { data: { name: "for" }, type: "ForStatement", line: 1, - column: 11 + column: 9, + endLine: 1, + endColumn: 10 } ] }, @@ -580,8 +611,10 @@ ruleTester.run("curly", rule, { messageId: "missingCurlyAfterCondition", data: { name: "for" }, type: "ForStatement", - line: 2, - column: 2 + line: 1, + column: 9, + endLine: 1, + endColumn: 10 } ] }, @@ -596,7 +629,9 @@ ruleTester.run("curly", rule, { data: { name: "for-of" }, type: "ForOfStatement", line: 1, - column: 22 + column: 22, + endLine: 1, + endColumn: 23 } ] }, @@ -611,7 +646,9 @@ ruleTester.run("curly", rule, { data: { name: "do" }, type: "DoWhileStatement", line: 1, - column: 3 + column: 3, + endColumn: 4, + endLine: 1 } ] }, @@ -623,7 +660,26 @@ ruleTester.run("curly", rule, { { messageId: "unexpectedCurlyAfterCondition", data: { name: "for" }, - type: "ForStatement" + type: "ForStatement", + line: 1, + column: 13, + endColumn: 14, + endLine: 1 + } + ] + }, + { + code: "for (;foo;) \n bar() ", + output: "for (;foo;) \n {bar()} ", + errors: [ + { + data: { name: "for" }, + line: 1, + column: 11, + type: "ForStatement", + messageId: "missingCurlyAfterCondition", + endLine: 1, + endColumn: 12 } ] }, @@ -758,7 +814,26 @@ ruleTester.run("curly", rule, { { messageId: "missingCurlyAfterCondition", data: { name: "if" }, - type: "IfStatement" + type: "IfStatement", + line: 1, + endColumn: 9, + endLine: 1, + column: 8 + } + ] + }, + { + code: "if (foo) baz()", + output: "if (foo) {baz()}", + errors: [ + { + messageId: "missingCurlyAfterCondition", + data: { name: "if" }, + type: "IfStatement", + line: 1, + endColumn: 9, + endLine: 1, + column: 8 } ] }, @@ -820,7 +895,9 @@ ruleTester.run("curly", rule, { data: { name: "do" }, type: "DoWhileStatement", line: 1, - column: 4 + column: 1, + endColumn: 3, + endLine: 1 } ] }, @@ -833,8 +910,10 @@ ruleTester.run("curly", rule, { messageId: "missingCurlyAfter", data: { name: "do" }, type: "DoWhileStatement", - line: 2, - column: 2 + line: 1, + column: 1, + endColumn: 3, + endLine: 1 } ] }, @@ -885,7 +964,11 @@ ruleTester.run("curly", rule, { { messageId: "missingCurlyAfter", data: { name: "for-of" }, - type: "ForOfStatement" + type: "ForOfStatement", + line: 1, + column: 20, + endLine: 1, + endColumn: 21 } ] }, @@ -1019,14 +1102,18 @@ ruleTester.run("curly", rule, { errors: [ { line: 1, - column: 22, + column: 20, + endLine: 1, + endColumn: 21, messageId: "missingCurlyAfter", data: { name: "for-in" }, type: "ForInStatement" }, { line: 1, - column: 30, + column: 29, + endColumn: 30, + endLine: 1, messageId: "missingCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" @@ -1049,8 +1136,10 @@ ruleTester.run("curly", rule, { messageId: "missingCurlyAfter", data: { name: "for-in" }, type: "ForInStatement", - line: 2, - column: 2 + line: 1, + column: 20, + endLine: 1, + endColumn: 21 } ] }, @@ -1526,7 +1615,9 @@ ruleTester.run("curly", rule, { options: ["multi-line"], errors: [{ line: 1, - column: 10, + column: 9, + endLine: 1, + endColumn: 10, data: { name: "if" }, type: "IfStatement", messageId: "missingCurlyAfterCondition" @@ -1538,6 +1629,8 @@ ruleTester.run("curly", rule, { options: ["multi"], errors: [{ line: 4, + endLine: 4, + endColumn: 4, column: 3, data: { name: "for" }, type: "ForStatement", @@ -1549,8 +1642,10 @@ ruleTester.run("curly", rule, { output: "for(\n;\n;\n) \n{foo()}\n", options: ["multi-line"], errors: [{ - line: 5, + line: 4, column: 1, + endLine: 4, + endColumn: 2, data: { name: "for" }, type: "ForStatement", messageId: "missingCurlyAfterCondition" From d6c1765f087c60c2d8cdb17ef3cc56cba1cbd01e Mon Sep 17 00:00:00 2001 From: Anix Date: Fri, 15 May 2020 08:07:58 +0000 Subject: [PATCH 4/7] Update: added end position for else --- lib/rules/curly.js | 8 ++++- tests/lib/rules/curly.js | 72 ++++++++++++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 14 deletions(-) diff --git a/lib/rules/curly.js b/lib/rules/curly.js index e8064d37e39..dca9c8ab363 100644 --- a/lib/rules/curly.js +++ b/lib/rules/curly.js @@ -354,6 +354,9 @@ module.exports = { * for (var foo in bar) console.log(foo) * ^^ * + * if (foo) {bar()} else bar(1); + * ^^^^ + * * Unexpected curly brace reporting location examples: * * The following location should be reported @@ -373,6 +376,9 @@ module.exports = { * for (var foo of bar) {console.log(foo)} * ^^ * + * if (foo) bar() else {bar(1)} + * ^^^^ + * * Get the location of missing or non missing curly brace. * @param {ASTNode} node the node to check. * @param {string} name name to check. @@ -382,7 +388,7 @@ module.exports = { function getReportLoc(node, name, isMissing) { if (name === "else") { - return getElseKeyword(node).loc.start; + return getElseKeyword(node).loc; } if (isMissing) { diff --git a/tests/lib/rules/curly.js b/tests/lib/rules/curly.js index acad42d89ab..7de534da662 100644 --- a/tests/lib/rules/curly.js +++ b/tests/lib/rules/curly.js @@ -1387,19 +1387,19 @@ ruleTester.run("curly", rule, { }, { code: - "if (a) {\n" + - " while (b) {\n" + - " c();\n" + - " d();\n" + - " }\n" + - "} else e();", + "if (a) {\n" + + " while (b) {\n" + + " c();\n" + + " d();\n" + + " }\n" + + "} else e();", output: - "if (a) \n" + - " while (b) {\n" + - " c();\n" + - " d();\n" + - " }\n" + - " else e();", + "if (a) \n" + + " while (b) {\n" + + " c();\n" + + " d();\n" + + " }\n" + + " else e();", options: ["multi"], errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }] }, @@ -1576,7 +1576,53 @@ ruleTester.run("curly", rule, { code: "if (a) { while (cond) if (b) foo() } else bar();", output: "if (a) { while (cond) if (b) foo() } else {bar();}", options: ["multi", "consistent"], - errors: [{ messageId: "missingCurlyAfter", data: { name: "else" }, type: "IfStatement" }] + errors: [ + { + messageId: "missingCurlyAfter", + data: { name: "else" }, + type: "IfStatement", + line: 1, + column: 38, + endColumn: 42, + endLine: 1 + } + ] + }, + { + code: "if (a) while (cond) if (b) foo() \nelse\n {bar();}", + output: "if (a) while (cond) if (b) foo() \nelse\n bar();", + options: ["multi", "consistent"], + errors: [ + { + messageId: "unexpectedCurlyAfter", + data: { name: "else" }, + type: "IfStatement", + line: 2, + column: 1, + endColumn: 5, + endLine: 2 + } + ] + }, + { + code: "if (a) foo() \nelse\n bar();", + output: "if (a) {foo()} \nelse\n {bar();}", + errors: [{ + line: 1, + column: 6, + type: "IfStatement", + messageId: "missingCurlyAfterCondition", + endLine: 1, + endColumn: 7 + }, + { + line: 2, + column: 1, + type: "IfStatement", + messageId: "missingCurlyAfter", + endLine: 2, + endColumn: 5 + }] }, { code: "if (a) { while (cond) if (b) foo() } ", From e03f220e78543f855d3f298ad5837033f8bd961c Mon Sep 17 00:00:00 2001 From: Anix Date: Fri, 15 May 2020 08:43:11 +0000 Subject: [PATCH 5/7] Update: change loc for else unexpected curly brace --- lib/rules/curly.js | 7 +++++++ tests/lib/rules/curly.js | 16 ++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/rules/curly.js b/lib/rules/curly.js index dca9c8ab363..c00d1d362e7 100644 --- a/lib/rules/curly.js +++ b/lib/rules/curly.js @@ -388,6 +388,13 @@ module.exports = { function getReportLoc(node, name, isMissing) { if (name === "else") { + + if (!isMissing) { + const token = sourceCode.getTokenAfter(getElseKeyword(node)); + + return token.loc; + } + return getElseKeyword(node).loc; } diff --git a/tests/lib/rules/curly.js b/tests/lib/rules/curly.js index 7de534da662..1572e55e45d 100644 --- a/tests/lib/rules/curly.js +++ b/tests/lib/rules/curly.js @@ -777,7 +777,9 @@ ruleTester.run("curly", rule, { data: { name: "else" }, type: "IfStatement", line: 6, - column: 3 + column: 8, + endLine: 6, + endColumn: 9 } ] }, @@ -1215,7 +1217,9 @@ ruleTester.run("curly", rule, { data: { name: "else" }, type: "IfStatement", line: 1, - column: 18 + column: 23, + endColumn: 24, + endLine: 1 } ] }, @@ -1597,10 +1601,10 @@ ruleTester.run("curly", rule, { messageId: "unexpectedCurlyAfter", data: { name: "else" }, type: "IfStatement", - line: 2, - column: 1, - endColumn: 5, - endLine: 2 + line: 3, + column: 2, + endColumn: 3, + endLine: 3 } ] }, From fd31c692c4d9a584863845f392c5d126c7ecfe87 Mon Sep 17 00:00:00 2001 From: Anix Date: Tue, 7 Jul 2020 13:42:10 +0000 Subject: [PATCH 6/7] Update: changed reporting location --- lib/rules/curly.js | 46 ++++---- tests/lib/rules/curly.js | 225 +++++++++++++++++++++++---------------- 2 files changed, 159 insertions(+), 112 deletions(-) diff --git a/lib/rules/curly.js b/lib/rules/curly.js index c00d1d362e7..2bd0750119f 100644 --- a/lib/rules/curly.js +++ b/lib/rules/curly.js @@ -337,22 +337,22 @@ module.exports = { * The following location should be reported * * if (foo) bar() - * ^^ + * ^^^ * * while(foo) bar(); - * ^^ + * ^^^ * * do foo() while(bar) * ^^^ * * for(;;) bar(); - * ^^ + * ^^^ * * for (var foo of bar) console.log(foo) - * ^^ + * ^^^ * * for (var foo in bar) console.log(foo) - * ^^ + * ^^^ * * if (foo) {bar()} else bar(1); * ^^^^ @@ -362,19 +362,19 @@ module.exports = { * The following location should be reported * * if (true) foo(); else { baz(); } - * ^^ + * ^ * * for (;;) { foo(); } - * ^^ + * ^ * * while (bar) { foo(); } - * ^^ + * ^ * * do{foo();} while(bar); - * ^^ + * ^ * * for (var foo of bar) {console.log(foo)} - * ^^ + * ^ * * if (foo) bar() else {bar(1)} * ^^^^ @@ -386,7 +386,6 @@ module.exports = { * @returns {Object} Position object. */ function getReportLoc(node, name, isMissing) { - if (name === "else") { if (!isMissing) { @@ -403,28 +402,39 @@ module.exports = { if (name === "for" || name === "do") { const token = sourceCode.getTokenBefore(node.body); - return token.loc; + return { + start: token.loc.end, + end: node.body.loc.start + }; } if (name === "for-of" || name === "for-in") { - const token = sourceCode.getTokenAfter(node.right); - - return token.loc; + return { + start: node.right.loc.end, + end: node.body.loc.start + }; } } - if (blockStartLoc.has(name)) { return createNextLocation(node.body.loc.start); } if (name === "if") { - return isMissing ? createNextLocation(node.test.loc.end) : createNextLocation(node.consequent.loc.start); + return isMissing + ? { + start: node.test.loc.end, + end: node.consequent.loc.start + } + : createNextLocation(node.consequent.loc.start); } if (name === "while") { - return isMissing ? createNextLocation(astUtils.getNextLocation(sourceCode, node.test.loc.end)) : createNextLocation(node.body.loc.start); + return isMissing ? { + start: node.test.loc.end, + end: node.body.loc.start + } : createNextLocation(node.body.loc.start); } return node.loc; diff --git a/tests/lib/rules/curly.js b/tests/lib/rules/curly.js index 1572e55e45d..cf588083762 100644 --- a/tests/lib/rules/curly.js +++ b/tests/lib/rules/curly.js @@ -432,8 +432,8 @@ ruleTester.run("curly", rule, { type: "IfStatement", line: 1, column: 8, - endColumn: 9, - endLine: 1 + endLine: 1, + endColumn: 10 } ] }, @@ -447,8 +447,8 @@ ruleTester.run("curly", rule, { type: "IfStatement", line: 1, column: 8, - endColumn: 9, - endLine: 1 + endLine: 2, + endColumn: 2 } ] }, @@ -483,7 +483,7 @@ ruleTester.run("curly", rule, { data: { name: "while" }, type: "WhileStatement", line: 1, - column: 12, + column: 11, endLine: 1, endColumn: 13 } @@ -498,7 +498,9 @@ ruleTester.run("curly", rule, { data: { name: "while" }, type: "WhileStatement", line: 1, - column: 12 + column: 11, + endLine: 2, + endColumn: 2 } ] }, @@ -511,9 +513,9 @@ ruleTester.run("curly", rule, { data: { name: "do" }, type: "DoWhileStatement", line: 1, - column: 1, - endColumn: 3, - endLine: 1 + column: 3, + endLine: 1, + endColumn: 4 } ] }, @@ -526,9 +528,9 @@ ruleTester.run("curly", rule, { data: { name: "do" }, type: "DoWhileStatement", line: 1, - column: 1, - endColumn: 3, - endLine: 1 + column: 3, + endLine: 2, + endColumn: 2 } ] }, @@ -566,7 +568,7 @@ ruleTester.run("curly", rule, { line: 1, column: 20, endLine: 1, - endColumn: 21 + endColumn: 22 } ] }, @@ -581,8 +583,8 @@ ruleTester.run("curly", rule, { type: "ForOfStatement", line: 1, column: 20, - endColumn: 21, - endLine: 1 + endLine: 2, + endColumn: 2 } ] }, @@ -596,9 +598,9 @@ ruleTester.run("curly", rule, { data: { name: "for" }, type: "ForStatement", line: 1, - column: 9, + column: 10, endLine: 1, - endColumn: 10 + endColumn: 11 } ] }, @@ -612,9 +614,9 @@ ruleTester.run("curly", rule, { data: { name: "for" }, type: "ForStatement", line: 1, - column: 9, - endLine: 1, - endColumn: 10 + column: 10, + endLine: 2, + endColumn: 2 } ] }, @@ -647,8 +649,8 @@ ruleTester.run("curly", rule, { type: "DoWhileStatement", line: 1, column: 3, - endColumn: 4, - endLine: 1 + endLine: 1, + endColumn: 4 } ] }, @@ -663,8 +665,8 @@ ruleTester.run("curly", rule, { type: "ForStatement", line: 1, column: 13, - endColumn: 14, - endLine: 1 + endLine: 1, + endColumn: 14 } ] }, @@ -674,12 +676,12 @@ ruleTester.run("curly", rule, { errors: [ { data: { name: "for" }, - line: 1, - column: 11, type: "ForStatement", messageId: "missingCurlyAfterCondition", - endLine: 1, - endColumn: 12 + line: 1, + column: 12, + endLine: 2, + endColumn: 2 } ] }, @@ -691,7 +693,11 @@ ruleTester.run("curly", rule, { { messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, - type: "IfStatement" + type: "IfStatement", + line: 1, + column: 10, + endLine: 1, + endColumn: 11 } ] }, @@ -703,7 +709,11 @@ ruleTester.run("curly", rule, { { messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, - type: "WhileStatement" + type: "WhileStatement", + line: 1, + column: 13, + endLine: 1, + endColumn: 14 } ] }, @@ -791,7 +801,11 @@ ruleTester.run("curly", rule, { { messageId: "unexpectedCurlyAfter", data: { name: "for-in" }, - type: "ForInStatement" + type: "ForInStatement", + line: 1, + column: 22, + endLine: 1, + endColumn: 23 } ] }, @@ -818,9 +832,9 @@ ruleTester.run("curly", rule, { data: { name: "if" }, type: "IfStatement", line: 1, - endColumn: 9, - endLine: 1, - column: 8 + column: 8, + endLine: 2, + endColumn: 2 } ] }, @@ -833,9 +847,9 @@ ruleTester.run("curly", rule, { data: { name: "if" }, type: "IfStatement", line: 1, - endColumn: 9, + column: 8, endLine: 1, - column: 8 + endColumn: 10 } ] }, @@ -897,9 +911,9 @@ ruleTester.run("curly", rule, { data: { name: "do" }, type: "DoWhileStatement", line: 1, - column: 1, - endColumn: 3, - endLine: 1 + column: 3, + endLine: 1, + endColumn: 4 } ] }, @@ -913,9 +927,9 @@ ruleTester.run("curly", rule, { data: { name: "do" }, type: "DoWhileStatement", line: 1, - column: 1, - endColumn: 3, - endLine: 1 + column: 3, + endLine: 2, + endColumn: 2 } ] }, @@ -929,7 +943,9 @@ ruleTester.run("curly", rule, { data: { name: "for-in" }, type: "ForInStatement", line: 1, - column: 22 + column: 22, + endLine: 1, + endColumn: 23 } ] }, @@ -969,8 +985,8 @@ ruleTester.run("curly", rule, { type: "ForOfStatement", line: 1, column: 20, - endLine: 1, - endColumn: 21 + endLine: 2, + endColumn: 2 } ] }, @@ -1093,7 +1109,11 @@ ruleTester.run("curly", rule, { { messageId: "unexpectedCurlyAfterCondition", data: { name: "for" }, - type: "ForStatement" + type: "ForStatement", + line: 1, + column: 27, + endLine: 1, + endColumn: 28 } ] }, @@ -1103,29 +1123,31 @@ ruleTester.run("curly", rule, { options: ["all"], errors: [ { + messageId: "missingCurlyAfter", + data: { name: "for-in" }, + type: "ForInStatement", line: 1, column: 20, endLine: 1, - endColumn: 21, - messageId: "missingCurlyAfter", - data: { name: "for-in" }, - type: "ForInStatement" + endColumn: 22 }, { + messageId: "missingCurlyAfterCondition", + data: { name: "if" }, + type: "IfStatement", line: 1, column: 29, - endColumn: 30, endLine: 1, - messageId: "missingCurlyAfterCondition", - data: { name: "if" }, - type: "IfStatement" + endColumn: 31 }, { - line: 1, - column: 47, messageId: "missingCurlyAfter", data: { name: "else" }, - type: "IfStatement" + type: "IfStatement", + line: 1, + column: 47, + endLine: 1, + endColumn: 51 } ] }, @@ -1140,8 +1162,8 @@ ruleTester.run("curly", rule, { type: "ForInStatement", line: 1, column: 20, - endLine: 1, - endColumn: 21 + endLine: 2, + endColumn: 2 } ] }, @@ -1262,7 +1284,9 @@ ruleTester.run("curly", rule, { data: { name: "do" }, type: "DoWhileStatement", line: 2, - column: 1 + column: 1, + endLine: 2, + endColumn: 2 } ] }, @@ -1276,7 +1300,9 @@ ruleTester.run("curly", rule, { data: { name: "while" }, type: "WhileStatement", line: 1, - column: 13 + column: 13, + endLine: 1, + endColumn: 14 } ] }, @@ -1290,7 +1316,9 @@ ruleTester.run("curly", rule, { data: { name: "while" }, type: "WhileStatement", line: 2, - column: 1 + column: 1, + endLine: 2, + endColumn: 2 } ] }, @@ -1304,7 +1332,9 @@ ruleTester.run("curly", rule, { data: { name: "for" }, type: "ForStatement", line: 1, - column: 10 + column: 10, + endLine: 1, + endColumn: 11 } ] }, @@ -1587,8 +1617,8 @@ ruleTester.run("curly", rule, { type: "IfStatement", line: 1, column: 38, - endColumn: 42, - endLine: 1 + endLine: 1, + endColumn: 42 } ] }, @@ -1603,8 +1633,8 @@ ruleTester.run("curly", rule, { type: "IfStatement", line: 3, column: 2, - endColumn: 3, - endLine: 3 + endLine: 3, + endColumn: 3 } ] }, @@ -1612,18 +1642,18 @@ ruleTester.run("curly", rule, { code: "if (a) foo() \nelse\n bar();", output: "if (a) {foo()} \nelse\n {bar();}", errors: [{ - line: 1, - column: 6, type: "IfStatement", messageId: "missingCurlyAfterCondition", + line: 1, + column: 6, endLine: 1, - endColumn: 7 + endColumn: 8 }, { - line: 2, - column: 1, type: "IfStatement", messageId: "missingCurlyAfter", + line: 2, + column: 1, endLine: 2, endColumn: 5 }] @@ -1634,10 +1664,12 @@ ruleTester.run("curly", rule, { options: ["multi", "consistent"], errors: [{ messageId: "unexpectedCurlyAfterCondition", + data: { name: "if" }, + type: "IfStatement", line: 1, column: 8, - data: { name: "if" }, - type: "IfStatement" + endLine: 1, + endColumn: 9 }] }, { @@ -1645,18 +1677,23 @@ ruleTester.run("curly", rule, { output: "if(a) if (b) foo(); if (c) bar(); else if(foo)bar();", options: ["multi-or-nest"], errors: [{ - line: 1, - column: 7, type: "IfStatement", data: { name: "if" }, - messageId: "unexpectedCurlyAfterCondition" + messageId: "unexpectedCurlyAfterCondition", + line: 1, + column: 7, + endLine: 1, + endColumn: 8 }, { - line: 1, - column: 51, + type: "IfStatement", data: { name: "if" }, - messageId: "unexpectedCurlyAfterCondition" + messageId: "unexpectedCurlyAfterCondition", + line: 1, + column: 51, + endLine: 1, + endColumn: 52 }] }, { @@ -1664,13 +1701,13 @@ ruleTester.run("curly", rule, { output: "if (true) {[1, 2, 3]\n.bar()}", options: ["multi-line"], errors: [{ + data: { name: "if" }, + type: "IfStatement", + messageId: "missingCurlyAfterCondition", line: 1, column: 9, endLine: 1, - endColumn: 10, - data: { name: "if" }, - type: "IfStatement", - messageId: "missingCurlyAfterCondition" + endColumn: 11 }] }, { @@ -1678,13 +1715,13 @@ ruleTester.run("curly", rule, { output: "for(\n;\n;\n) foo()", options: ["multi"], errors: [{ - line: 4, - endLine: 4, - endColumn: 4, - column: 3, data: { name: "for" }, type: "ForStatement", - messageId: "unexpectedCurlyAfterCondition" + messageId: "unexpectedCurlyAfterCondition", + line: 4, + column: 3, + endLine: 4, + endColumn: 4 }] }, { @@ -1692,13 +1729,13 @@ ruleTester.run("curly", rule, { output: "for(\n;\n;\n) \n{foo()}\n", options: ["multi-line"], errors: [{ - line: 4, - column: 1, - endLine: 4, - endColumn: 2, data: { name: "for" }, type: "ForStatement", - messageId: "missingCurlyAfterCondition" + messageId: "missingCurlyAfterCondition", + line: 4, + column: 2, + endLine: 5, + endColumn: 1 }] }, { From 1808ce470f4ba1ab29255c1c569bc99df2b0ec1f Mon Sep 17 00:00:00 2001 From: Anix Date: Wed, 29 Jul 2020 16:24:43 +0000 Subject: [PATCH 7/7] Update: changed the start loc for missing --- lib/rules/curly.js | 18 +++---------- tests/lib/rules/curly.js | 57 +++++++++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/lib/rules/curly.js b/lib/rules/curly.js index 2bd0750119f..966b9e65fa6 100644 --- a/lib/rules/curly.js +++ b/lib/rules/curly.js @@ -387,7 +387,6 @@ module.exports = { */ function getReportLoc(node, name, isMissing) { if (name === "else") { - if (!isMissing) { const token = sourceCode.getTokenAfter(getElseKeyword(node)); @@ -397,27 +396,16 @@ module.exports = { return getElseKeyword(node).loc; } - if (isMissing) { - - if (name === "for" || name === "do") { + if (blockStartLoc.has(name)) { + if (isMissing) { const token = sourceCode.getTokenBefore(node.body); return { - start: token.loc.end, + start: token.loc.start, end: node.body.loc.start }; } - if (name === "for-of" || name === "for-in") { - return { - start: node.right.loc.end, - end: node.body.loc.start - }; - } - - } - - if (blockStartLoc.has(name)) { return createNextLocation(node.body.loc.start); } diff --git a/tests/lib/rules/curly.js b/tests/lib/rules/curly.js index cf588083762..26c601754f8 100644 --- a/tests/lib/rules/curly.js +++ b/tests/lib/rules/curly.js @@ -513,7 +513,7 @@ ruleTester.run("curly", rule, { data: { name: "do" }, type: "DoWhileStatement", line: 1, - column: 3, + column: 1, endLine: 1, endColumn: 4 } @@ -528,7 +528,7 @@ ruleTester.run("curly", rule, { data: { name: "do" }, type: "DoWhileStatement", line: 1, - column: 3, + column: 1, endLine: 2, endColumn: 2 } @@ -598,7 +598,7 @@ ruleTester.run("curly", rule, { data: { name: "for" }, type: "ForStatement", line: 1, - column: 10, + column: 9, endLine: 1, endColumn: 11 } @@ -614,7 +614,7 @@ ruleTester.run("curly", rule, { data: { name: "for" }, type: "ForStatement", line: 1, - column: 10, + column: 9, endLine: 2, endColumn: 2 } @@ -679,7 +679,7 @@ ruleTester.run("curly", rule, { type: "ForStatement", messageId: "missingCurlyAfterCondition", line: 1, - column: 12, + column: 11, endLine: 2, endColumn: 2 } @@ -911,7 +911,7 @@ ruleTester.run("curly", rule, { data: { name: "do" }, type: "DoWhileStatement", line: 1, - column: 3, + column: 1, endLine: 1, endColumn: 4 } @@ -927,7 +927,7 @@ ruleTester.run("curly", rule, { data: { name: "do" }, type: "DoWhileStatement", line: 1, - column: 3, + column: 1, endLine: 2, endColumn: 2 } @@ -1686,7 +1686,6 @@ ruleTester.run("curly", rule, { endColumn: 8 }, { - type: "IfStatement", data: { name: "if" }, messageId: "unexpectedCurlyAfterCondition", @@ -1733,7 +1732,7 @@ ruleTester.run("curly", rule, { type: "ForStatement", messageId: "missingCurlyAfterCondition", line: 4, - column: 2, + column: 1, endLine: 5, endColumn: 1 }] @@ -1753,6 +1752,46 @@ ruleTester.run("curly", rule, { { messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }, { messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" } ] + }, + { + code: "for(;;)foo()\n", + output: "for(;;){foo()}\n", + errors: [{ + data: { name: "for" }, + type: "ForStatement", + messageId: "missingCurlyAfterCondition", + line: 1, + column: 7, + endLine: 1, + endColumn: 8 + }] + }, + { + code: "for(var \ni \n in \n z)foo()\n", + output: "for(var \ni \n in \n z){foo()}\n", + errors: [{ + data: { name: "for-in" }, + type: "ForInStatement", + messageId: "missingCurlyAfter", + line: 4, + column: 3, + endLine: 4, + endColumn: 4 + }] + }, + { + code: "for(var i of \n z)\nfoo()\n", + output: "for(var i of \n z)\n{foo()}\n", + parserOptions: { ecmaVersion: 6 }, + errors: [{ + data: { name: "for-of" }, + type: "ForOfStatement", + messageId: "missingCurlyAfter", + line: 2, + column: 3, + endLine: 3, + endColumn: 1 + }] } ] });