Skip to content

Commit

Permalink
[Fix] jsx-curly-spacing problem in new tests, fixes #1414
Browse files Browse the repository at this point in the history
Before this, spaces before/after braces when there should be none resulted in two overlapping fixes for the full brace content.
And so they required two passes. But the tester does only one pass, so only one white space was fixed and the test failed.

By including comments in the range calculation for the fix, the range is now properly limited, making it possible to have start and end fixes in one pass.
  • Loading branch information
s-h-a-d-o-w committed Feb 16, 2018
1 parent 36a2008 commit 9219e52
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/rules/jsx-curly-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ module.exports = {
message: `There should be no space after '${token.value}'`,
fix: function(fixer) {
const nextToken = sourceCode.getTokenAfter(token);
const nextComment = sourceCode.getCommentsAfter(token);

// Take comments into consideration to narrow the fix range to what is actually affected. (See #1414)
if (nextComment.length > 0) {
return fixByTrimmingWhitespace(fixer, token.range[1], Math.min(nextToken.range[0], nextComment[0].start), 'start');
}

return fixByTrimmingWhitespace(fixer, token.range[1], nextToken.range[0], 'start');
}
});
Expand All @@ -244,6 +251,13 @@ module.exports = {
message: `There should be no space before '${token.value}'`,
fix: function(fixer) {
const previousToken = sourceCode.getTokenBefore(token);
const previousComment = sourceCode.getCommentsBefore(token);

// Take comments into consideration to narrow the fix range to what is actually affected. (See #1414)
if (previousComment.length > 0) {
return fixByTrimmingWhitespace(fixer, Math.max(previousToken.range[1], previousComment[0].end), token.range[0], 'end');
}

return fixByTrimmingWhitespace(fixer, previousToken.range[1], token.range[0], 'end');
}
});
Expand Down

0 comments on commit 9219e52

Please sign in to comment.