From a19514193a42f4f00732559ff828b33a6ec9d7c5 Mon Sep 17 00:00:00 2001 From: Anix Date: Sat, 23 May 2020 05:01:53 +0530 Subject: [PATCH] Update: reporting location for semi-spacing (refs #12334) (#13285) * Update: reporting location for unexpected space (refs #12334) * Update: both start and end for missing semi spacing * Update: changed end loc for UnexpectedWhitespace * Update: changed loc.end anf tests errors object ordering * Chore: test refactore * Chore: refactoring codebase --- lib/rules/semi-spacing.js | 40 ++++-- tests/lib/rules/semi-spacing.js | 216 ++++++++++++++++++++++++++++---- 2 files changed, 221 insertions(+), 35 deletions(-) diff --git a/lib/rules/semi-spacing.js b/lib/rules/semi-spacing.js index 92948533d27..936e7661ef4 100644 --- a/lib/rules/semi-spacing.js +++ b/lib/rules/semi-spacing.js @@ -117,6 +117,18 @@ module.exports = { } /** + * Report location example : + * + * for unexpected space `before` + * + * var a = 'b' ; + * ^^^ + * + * for unexpected space `after` + * + * var a = 'b'; c = 10; + * ^^ + * * Reports if the given token has invalid spacing. * @param {Token} token The semicolon token to check. * @param {ASTNode} node The corresponding node of the token. @@ -124,16 +136,19 @@ module.exports = { */ function checkSemicolonSpacing(token, node) { if (astUtils.isSemicolonToken(token)) { - const location = token.loc.start; - if (hasLeadingSpace(token)) { if (!requireSpaceBefore) { + const tokenBefore = sourceCode.getTokenBefore(token); + const loc = { + start: tokenBefore.loc.end, + end: token.loc.start + }; + context.report({ node, - loc: location, + loc, messageId: "unexpectedWhitespaceBefore", fix(fixer) { - const tokenBefore = sourceCode.getTokenBefore(token); return fixer.removeRange([tokenBefore.range[1], token.range[0]]); } @@ -141,9 +156,11 @@ module.exports = { } } else { if (requireSpaceBefore) { + const loc = token.loc; + context.report({ node, - loc: location, + loc, messageId: "missingWhitespaceBefore", fix(fixer) { return fixer.insertTextBefore(token, " "); @@ -155,12 +172,17 @@ module.exports = { if (!isFirstTokenInCurrentLine(token) && !isLastTokenInCurrentLine(token) && !isBeforeClosingParen(token)) { if (hasTrailingSpace(token)) { if (!requireSpaceAfter) { + const tokenAfter = sourceCode.getTokenAfter(token); + const loc = { + start: token.loc.end, + end: tokenAfter.loc.start + }; + context.report({ node, - loc: location, + loc, messageId: "unexpectedWhitespaceAfter", fix(fixer) { - const tokenAfter = sourceCode.getTokenAfter(token); return fixer.removeRange([token.range[1], tokenAfter.range[0]]); } @@ -168,9 +190,11 @@ module.exports = { } } else { if (requireSpaceAfter) { + const loc = token.loc; + context.report({ node, - loc: location, + loc, messageId: "missingWhitespaceAfter", fix(fixer) { return fixer.insertTextAfter(token, " "); diff --git a/tests/lib/rules/semi-spacing.js b/tests/lib/rules/semi-spacing.js index c1098df6f8e..6582a5ca690 100644 --- a/tests/lib/rules/semi-spacing.js +++ b/tests/lib/rules/semi-spacing.js @@ -58,92 +58,171 @@ ruleTester.run("semi-spacing", rule, { { code: "for ( var i = 0;i < results.length; ) {}", options: [{ after: false }] } ], invalid: [ + { + code: "var a = 'b' ;", + output: "var a = 'b';", + errors: [ + { + messageId: "unexpectedWhitespaceBefore", + type: "VariableDeclaration", + line: 1, + column: 12, + endLine: 1, + endColumn: 14 + } + ] + }, { code: "var a = 'b' ;", output: "var a = 'b';", - errors: [{ messageId: "unexpectedWhitespaceBefore", type: "VariableDeclaration", line: 1, column: 13 }] + errors: [ + { + messageId: "unexpectedWhitespaceBefore", + type: "VariableDeclaration", + line: 1, + column: 12, + endLine: 1, + endColumn: 13 + } + ] }, { code: "var a = 'b',\nc = 'd' ;", output: "var a = 'b',\nc = 'd';", - errors: [{ messageId: "unexpectedWhitespaceBefore", type: "VariableDeclaration", line: 2, column: 9 }] + errors: [{ messageId: "unexpectedWhitespaceBefore", type: "VariableDeclaration", line: 2, column: 8 }] }, { code: "var a = function() {} ;", output: "var a = function() {};", - errors: [{ messageId: "unexpectedWhitespaceBefore", type: "VariableDeclaration", line: 1, column: 23 }] + errors: [{ messageId: "unexpectedWhitespaceBefore", type: "VariableDeclaration", line: 1, column: 22 }] }, { code: "var a = function() {\n} ;", output: "var a = function() {\n};", - errors: [{ messageId: "unexpectedWhitespaceBefore", type: "VariableDeclaration", line: 2, column: 3 }] + errors: [{ messageId: "unexpectedWhitespaceBefore", type: "VariableDeclaration", line: 2, column: 2 }] }, { code: "/^a$/.test('b') ;", output: "/^a$/.test('b');", - errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ExpressionStatement", line: 1, column: 17 }] + errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ExpressionStatement", line: 1, column: 16 }] }, { code: ";(function(){}()) ;", output: ";(function(){}());", - errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ExpressionStatement", line: 1, column: 19 }] + errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ExpressionStatement", line: 1, column: 18 }] }, { code: "while (true) { break ; }", output: "while (true) { break; }", - errors: [{ messageId: "unexpectedWhitespaceBefore", type: "BreakStatement", line: 1, column: 22 }] + errors: [{ messageId: "unexpectedWhitespaceBefore", type: "BreakStatement", line: 1, column: 21 }] }, { code: "while (true) { continue ; }", output: "while (true) { continue; }", - errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ContinueStatement", line: 1, column: 25 }] + errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ContinueStatement", line: 1, column: 24 }] }, { code: "debugger ;", output: "debugger;", - errors: [{ messageId: "unexpectedWhitespaceBefore", type: "DebuggerStatement", line: 1, column: 10 }] + errors: [{ messageId: "unexpectedWhitespaceBefore", type: "DebuggerStatement", line: 1, column: 9 }] }, { code: "function foo() { return ; }", output: "function foo() { return; }", - errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ReturnStatement", line: 1, column: 25 }] + errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ReturnStatement", line: 1, column: 24 }] }, { code: "throw new Error('foo') ;", output: "throw new Error('foo');", - errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ThrowStatement", line: 1, column: 24 }] + errors: [{ messageId: "unexpectedWhitespaceBefore", type: "ThrowStatement", line: 1, column: 23 }] }, { code: "for (var i = 0 ; i < 10 ; i++) {}", output: "for (var i = 0; i < 10; i++) {}", errors: [ - { messageId: "unexpectedWhitespaceBefore", type: "ForStatement", line: 1, column: 16 }, - { messageId: "unexpectedWhitespaceBefore", type: "ForStatement", line: 1, column: 25 } + { messageId: "unexpectedWhitespaceBefore", type: "ForStatement", line: 1, column: 15 }, + { messageId: "unexpectedWhitespaceBefore", type: "ForStatement", line: 1, column: 24 } ] }, { code: "var a = 'b';c = 'd';", output: "var a = 'b'; c = 'd';", - errors: [{ messageId: "missingWhitespaceAfter", type: "VariableDeclaration", line: 1, column: 12 }] + errors: [ + { + messageId: "missingWhitespaceAfter", + type: "VariableDeclaration", + line: 1, + column: 12, + endLine: 1, + endColumn: 13 + } + ] }, { code: "var a = 'b';", output: "var a = 'b' ;", options: [{ before: true, after: true }], - errors: [{ messageId: "missingWhitespaceBefore", type: "VariableDeclaration", line: 1, column: 12 }] + errors: [ + { + messageId: "missingWhitespaceBefore", + type: "VariableDeclaration", + line: 1, + column: 12, + endLine: 1, + endColumn: 13 + } + ] }, { code: "var a = 'b'; c = 'd';", output: "var a = 'b';c = 'd';", options: [{ before: false, after: false }], - errors: [{ messageId: "unexpectedWhitespaceAfter", type: "VariableDeclaration", line: 1, column: 12 }] + errors: [ + { + messageId: "unexpectedWhitespaceAfter", + type: "VariableDeclaration", + line: 1, + column: 13, + endLine: 1, + endColumn: 14 + } + ] + }, + { + code: "var a = 'b'; c = 'd';", + output: "var a = 'b';c = 'd';", + options: [{ before: false, after: false }], + errors: [ + { + messageId: "unexpectedWhitespaceAfter", + type: "VariableDeclaration", + line: 1, + column: 13, + endLine: 1, + endColumn: 16 + } + ] }, { code: "for (var i = 0;i < 10;i++) {}", output: "for (var i = 0; i < 10; i++) {}", errors: [ - { messageId: "missingWhitespaceAfter", type: "ForStatement", line: 1, column: 15 }, - { messageId: "missingWhitespaceAfter", type: "ForStatement", line: 1, column: 22 } + { + messageId: "missingWhitespaceAfter", + type: "ForStatement", + line: 1, + column: 15, + endLine: 1, + endColumn: 16 + }, + { + messageId: "missingWhitespaceAfter", + type: "ForStatement", + line: 1, + column: 22, + endLine: 1, + endColumn: 23 + } ] }, { @@ -151,8 +230,22 @@ ruleTester.run("semi-spacing", rule, { output: "for (var i = 0 ; i < 10 ; i++) {}", options: [{ before: true, after: true }], errors: [ - { messageId: "missingWhitespaceBefore", type: "ForStatement", line: 1, column: 15 }, - { messageId: "missingWhitespaceBefore", type: "ForStatement", line: 1, column: 23 } + { + messageId: "missingWhitespaceBefore", + type: "ForStatement", + line: 1, + column: 15, + endLine: 1, + endColumn: 16 + }, + { + messageId: "missingWhitespaceBefore", + type: "ForStatement", + line: 1, + column: 23, + endLine: 1, + endColumn: 24 + } ] }, { @@ -160,8 +253,8 @@ ruleTester.run("semi-spacing", rule, { output: "for (var i = 0;i < 10;i++) {}", options: [{ before: false, after: false }], errors: [ - { messageId: "unexpectedWhitespaceAfter", type: "ForStatement", line: 1, column: 15 }, - { messageId: "unexpectedWhitespaceAfter", type: "ForStatement", line: 1, column: 23 } + { messageId: "unexpectedWhitespaceAfter", type: "ForStatement", line: 1, column: 16 }, + { messageId: "unexpectedWhitespaceAfter", type: "ForStatement", line: 1, column: 24 } ] }, { @@ -170,7 +263,7 @@ ruleTester.run("semi-spacing", rule, { options: [{ before: false, after: true }], parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ - { messageId: "unexpectedWhitespaceBefore", type: "ImportDeclaration", line: 1, column: 23 } + { messageId: "unexpectedWhitespaceBefore", type: "ImportDeclaration", line: 1, column: 22 } ] }, { @@ -179,7 +272,7 @@ ruleTester.run("semi-spacing", rule, { options: [{ before: false, after: true }], parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ - { messageId: "unexpectedWhitespaceBefore", type: "ImportDeclaration", line: 1, column: 28 } + { messageId: "unexpectedWhitespaceBefore", type: "ImportDeclaration", line: 1, column: 27 } ] }, { @@ -188,7 +281,7 @@ ruleTester.run("semi-spacing", rule, { options: [{ before: false, after: true }], parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ - { messageId: "unexpectedWhitespaceBefore", type: "ExportNamedDeclaration", line: 1, column: 27 } + { messageId: "unexpectedWhitespaceBefore", type: "ExportNamedDeclaration", line: 1, column: 26 } ] }, { @@ -197,7 +290,7 @@ ruleTester.run("semi-spacing", rule, { options: [{ before: false, after: true }], parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ - { messageId: "unexpectedWhitespaceBefore", type: "ExportAllDeclaration", line: 1, column: 21 } + { messageId: "unexpectedWhitespaceBefore", type: "ExportAllDeclaration", line: 1, column: 20 } ] }, { @@ -206,7 +299,76 @@ ruleTester.run("semi-spacing", rule, { options: [{ before: false, after: true }], parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [ - { messageId: "unexpectedWhitespaceBefore", type: "ExportDefaultDeclaration", line: 1, column: 20 } + { messageId: "unexpectedWhitespaceBefore", type: "ExportDefaultDeclaration", line: 1, column: 19 } + ] + }, + { + code: "while(foo) {continue ;}", + output: "while(foo) {continue;}", + options: [{ before: false, after: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "unexpectedWhitespaceBefore", + type: "ContinueStatement", + line: 1, + column: 21, + endLine: 1, + endColumn: 24 + } + ] + }, + { + code: "if(foo) {throw new Error() ; }", + output: "if(foo) {throw new Error(); }", + options: [{ before: false, after: false }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "unexpectedWhitespaceBefore", + type: "ThrowStatement", + line: 1, + column: 27, + endLine: 1, + endColumn: 30 + } + ] + }, + { + code: "for(a ; ; );", + output: "for(a;; );", + options: [{ before: false, after: false }], + parserOptions: { ecmaVersion: 6 }, + errors: [{ + type: "ForStatement", + messageId: "unexpectedWhitespaceBefore", + line: 1, + column: 6, + endLine: 1, + endColumn: 7 + }, + { + type: "ForStatement", + messageId: "unexpectedWhitespaceAfter", + line: 1, + column: 8, + endLine: 1, + endColumn: 9 + }] + }, + { + code: "for(a ; \n ; );", + output: "for(a; \n ; );", + options: [{ before: false, after: false }], + parserOptions: { ecmaVersion: 6 }, + errors: [{ + type: "ForStatement", + messageId: "unexpectedWhitespaceBefore", + line: 1, + column: 6, + endLine: 1, + endColumn: 7 + } ] } ]