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 all 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
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;
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 +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