Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: reporting location for semi-spacing (refs #12334) #13285

Merged
merged 6 commits into from May 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 35 additions & 7 deletions lib/rules/semi-spacing.js
Expand Up @@ -117,33 +117,51 @@ 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.
* @returns {void}
*/
function checkSemicolonSpacing(token, node) {
if (astUtils.isSemicolonToken(token)) {
const location = token.loc.start;

if (hasLeadingSpace(token)) {
const tokenBefore = sourceCode.getTokenBefore(token);
const loc = {
start: tokenBefore.loc.end,
end: token.loc.start
};

anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
if (!requireSpaceBefore) {
context.report({
node,
loc: location,
loc,
messageId: "unexpectedWhitespaceBefore",
fix(fixer) {
const tokenBefore = sourceCode.getTokenBefore(token);

return fixer.removeRange([tokenBefore.range[1], token.range[0]]);
}
});
}
} else {
if (requireSpaceBefore) {
const loc = token.loc;
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved

context.report({
node,
loc: location,
loc,
messageId: "missingWhitespaceBefore",
fix(fixer) {
return fixer.insertTextBefore(token, " ");
Expand All @@ -155,22 +173,32 @@ 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: {
line: tokenAfter.loc.start.line,
column: tokenAfter.loc.start.column - 1
}
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
};

context.report({
node,
loc: location,
loc,
messageId: "unexpectedWhitespaceAfter",
fix(fixer) {
const tokenAfter = sourceCode.getTokenAfter(token);

return fixer.removeRange([token.range[1], tokenAfter.range[0]]);
}
});
}
} else {
if (requireSpaceAfter) {
const loc = token.loc;

context.report({
node,
loc: location,
loc,
messageId: "missingWhitespaceAfter",
fix(fixer) {
return fixer.insertTextAfter(token, " ");
Expand Down
156 changes: 135 additions & 21 deletions tests/lib/rules/semi-spacing.js
Expand Up @@ -58,67 +58,90 @@ 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 }
]
},
{
Expand All @@ -136,7 +159,31 @@ ruleTester.run("semi-spacing", rule, {
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,
endColumn: 13,
endLine: 1
}
]
},
{
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,
endColumn: 15,
endLine: 1
}
]
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
},
{
code: "for (var i = 0;i < 10;i++) {}",
Expand All @@ -160,8 +207,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 }
]
},
{
Expand All @@ -170,7 +217,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 }
]
},
{
Expand All @@ -179,7 +226,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 }
]
},
{
Expand All @@ -188,7 +235,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 }
]
},
{
Expand All @@ -197,7 +244,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 }
]
},
{
Expand All @@ -206,7 +253,74 @@ 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,
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,
endColumn: 30
}
]
},
{
code: "for(a ; ; );",
output: "for(a;; );",
options: [{ before: false, after: false }],
parserOptions: { ecmaVersion: 6 },
errors: [{
line: 1,
column: 6,
type: "ForStatement",
messageId: "unexpectedWhitespaceBefore",
endLine: 1,
endColumn: 7
},
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
{
line: 1,
column: 8,
type: "ForStatement",
messageId: "unexpectedWhitespaceAfter",
endLine: 1,
endColumn: 8
}]
},
{
code: "for(a ; \n ; );",
output: "for(a; \n ; );",
options: [{ before: false, after: false }],
parserOptions: { ecmaVersion: 6 },
errors: [{
line: 1,
column: 6,
type: "ForStatement",
messageId: "unexpectedWhitespaceBefore",
endLine: 1,
endColumn: 7
}
]
}
]
Expand Down