Skip to content

Commit

Permalink
Update: reporting location for semi-spacing (refs #12334) (#13285)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
anikethsaha committed May 22, 2020
1 parent e3e4c41 commit a195141
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 35 deletions.
40 changes: 32 additions & 8 deletions lib/rules/semi-spacing.js
Expand Up @@ -117,33 +117,50 @@ 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)) {
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]]);
}
});
}
} else {
if (requireSpaceBefore) {
const loc = token.loc;

context.report({
node,
loc: location,
loc,
messageId: "missingWhitespaceBefore",
fix(fixer) {
return fixer.insertTextBefore(token, " ");
Expand All @@ -155,22 +172,29 @@ 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]]);
}
});
}
} else {
if (requireSpaceAfter) {
const loc = token.loc;

context.report({
node,
loc: location,
loc,
messageId: "missingWhitespaceAfter",
fix(fixer) {
return fixer.insertTextAfter(token, " ");
Expand Down

0 comments on commit a195141

Please sign in to comment.