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

[Fix] jsx-props-no-multi-spaces: fix a false positive for beside comments #2878

Merged
merged 1 commit into from Dec 18, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -20,8 +20,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
* [`jsx-max-depth`]: avoid crash with childless jsx child ([#2869][] @ljharb, @AriPerkkio)
* [`no-unknown-property`]: avoid crash with prop named with Object.prototype key ([#2879][] @ljharb, @AriPerkkio)
* [`prop-types`]: default argument does not count as props-types declaration ([#2877][] @golopot)
* [`jsx-props-no-multi-spaces`]: fix a false positive for beside comments ([#2878][] @golopot)

[#2879]: https://github.com/yannickcr/eslint-plugin-react/issues/2879
[#2878]: https://github.com/yannickcr/eslint-plugin-react/pull/2878
[#2877]: https://github.com/yannickcr/eslint-plugin-react/pull/2877
[#2875]: https://github.com/yannickcr/eslint-plugin-react/issues/2875
[#2871]: https://github.com/yannickcr/eslint-plugin-react/issues/2871
Expand Down
32 changes: 13 additions & 19 deletions lib/rules/jsx-props-no-multi-spaces.js
Expand Up @@ -39,30 +39,24 @@ module.exports = {
}
}

function hasEmptyLines(node, leadingLineCount) {
const allComments = sourceCode.getCommentsBefore(node);
let commentLines = 0;

if (allComments.length > 0) {
allComments.forEach((comment) => {
const start = comment.loc.start.line;
const end = comment.loc.end.line;

commentLines += (end - start + 1);
});

return commentLines !== leadingLineCount;
// First and second must be adjacent nodes
function hasEmptyLines(first, second) {
const comments = sourceCode.getCommentsBefore(second);
const nodes = [].concat(first, comments, second);

for (let i = 1; i < nodes.length; i += 1) {
const prev = nodes[i - 1];
const curr = nodes[i];
if (curr.loc.start.line - prev.loc.end.line >= 2) {
return true;
}
}

return true;
return false;
}

function checkSpacing(prev, node) {
const prevNodeEndLine = prev.loc.end.line;
const currNodeStartLine = node.loc.start.line;
const leadingLineCount = currNodeStartLine - prevNodeEndLine - 1;

if (leadingLineCount > 0 && hasEmptyLines(node, leadingLineCount)) {
if (hasEmptyLines(prev, node)) {
context.report({
node,
message: `Expected no line gap between “${getPropName(prev)}” and “${getPropName(node)}”`
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/jsx-props-no-multi-spaces.js
Expand Up @@ -108,6 +108,14 @@ ruleTester.run('jsx-props-no-multi-spaces', rule, {
type="button"
/>
`
}, {
code: `
<App
foo="Some button" // comment
// comment
bar=""
/>
`
}, {
code: `
<button
Expand Down