Skip to content

Commit a195141

Browse files
authored
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
1 parent e3e4c41 commit a195141

2 files changed

Lines changed: 221 additions & 35 deletions

File tree

lib/rules/semi-spacing.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,33 +117,50 @@ module.exports = {
117117
}
118118

119119
/**
120+
* Report location example :
121+
*
122+
* for unexpected space `before`
123+
*
124+
* var a = 'b' ;
125+
* ^^^
126+
*
127+
* for unexpected space `after`
128+
*
129+
* var a = 'b'; c = 10;
130+
* ^^
131+
*
120132
* Reports if the given token has invalid spacing.
121133
* @param {Token} token The semicolon token to check.
122134
* @param {ASTNode} node The corresponding node of the token.
123135
* @returns {void}
124136
*/
125137
function checkSemicolonSpacing(token, node) {
126138
if (astUtils.isSemicolonToken(token)) {
127-
const location = token.loc.start;
128-
129139
if (hasLeadingSpace(token)) {
130140
if (!requireSpaceBefore) {
141+
const tokenBefore = sourceCode.getTokenBefore(token);
142+
const loc = {
143+
start: tokenBefore.loc.end,
144+
end: token.loc.start
145+
};
146+
131147
context.report({
132148
node,
133-
loc: location,
149+
loc,
134150
messageId: "unexpectedWhitespaceBefore",
135151
fix(fixer) {
136-
const tokenBefore = sourceCode.getTokenBefore(token);
137152

138153
return fixer.removeRange([tokenBefore.range[1], token.range[0]]);
139154
}
140155
});
141156
}
142157
} else {
143158
if (requireSpaceBefore) {
159+
const loc = token.loc;
160+
144161
context.report({
145162
node,
146-
loc: location,
163+
loc,
147164
messageId: "missingWhitespaceBefore",
148165
fix(fixer) {
149166
return fixer.insertTextBefore(token, " ");
@@ -155,22 +172,29 @@ module.exports = {
155172
if (!isFirstTokenInCurrentLine(token) && !isLastTokenInCurrentLine(token) && !isBeforeClosingParen(token)) {
156173
if (hasTrailingSpace(token)) {
157174
if (!requireSpaceAfter) {
175+
const tokenAfter = sourceCode.getTokenAfter(token);
176+
const loc = {
177+
start: token.loc.end,
178+
end: tokenAfter.loc.start
179+
};
180+
158181
context.report({
159182
node,
160-
loc: location,
183+
loc,
161184
messageId: "unexpectedWhitespaceAfter",
162185
fix(fixer) {
163-
const tokenAfter = sourceCode.getTokenAfter(token);
164186

165187
return fixer.removeRange([token.range[1], tokenAfter.range[0]]);
166188
}
167189
});
168190
}
169191
} else {
170192
if (requireSpaceAfter) {
193+
const loc = token.loc;
194+
171195
context.report({
172196
node,
173-
loc: location,
197+
loc,
174198
messageId: "missingWhitespaceAfter",
175199
fix(fixer) {
176200
return fixer.insertTextAfter(token, " ");

0 commit comments

Comments
 (0)