Skip to content

Commit

Permalink
[Fix] sort-prop-types: avoid repeated warnings of the same node/reason
Browse files Browse the repository at this point in the history
Fixes #519
  • Loading branch information
ljharb committed Feb 2, 2022
1 parent a13593d commit f8b4aa3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [`no-invalid-html-attribute`]: allow 'shortcut icon' on `link` ([#3174][] @Primajin)
* [`prefer-exact-props`] improve performance for `Identifier` visitor ([#3190][] @meowtec)
* `propTypes`: Handle TSTypeReference in no-unused-prop-type ([#3195][] @niik)
* [`sort-prop-types`]: avoid repeated warnings of the same node/reason ([#519][] @ljharb)

### Changed
* [readme] change [`jsx-runtime`] link from branch to sha ([#3160][] @tatsushitoji)
Expand All @@ -39,6 +40,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
[#3133]: https://github.com/yannickcr/eslint-plugin-react/pull/3133
[#2921]: https://github.com/yannickcr/eslint-plugin-react/pull/2921
[#2753]: https://github.com/yannickcr/eslint-plugin-react/pull/2753
[#519]: https://github.com/yannickcr/eslint-plugin-react/issues/519

## [7.28.0] - 2021.12.22

Expand Down
35 changes: 24 additions & 11 deletions lib/rules/sort-prop-types.js
Expand Up @@ -118,6 +118,10 @@ module.exports = {
// );
// }

const callbackPropsLastSeen = new WeakSet();
const requiredPropsFirstSeen = new WeakSet();
const propsNotSortedSeen = new WeakSet();

declarations.reduce((prev, curr, idx, decls) => {
if (curr.type === 'ExperimentalSpreadProperty' || curr.type === 'SpreadElement') {
return decls[idx + 1];
Expand All @@ -142,10 +146,13 @@ module.exports = {
}
if (!previousIsRequired && currentIsRequired) {
// Encountered a non-required prop after a required prop
report(context, messages.requiredPropsFirst, 'requiredPropsFirst', {
node: curr,
// fix
});
if (!requiredPropsFirstSeen.has(curr)) {
requiredPropsFirstSeen.add(curr);
report(context, messages.requiredPropsFirst, 'requiredPropsFirst', {
node: curr,
// fix
});
}
return curr;
}
}
Expand All @@ -157,19 +164,25 @@ module.exports = {
}
if (previousIsCallback && !currentIsCallback) {
// Encountered a non-callback prop after a callback prop
report(context, messages.callbackPropsLast, 'callbackPropsLast', {
node: prev,
// fix
});
if (!callbackPropsLastSeen.has(prev)) {
callbackPropsLastSeen.add(prev);
report(context, messages.callbackPropsLast, 'callbackPropsLast', {
node: prev,
// fix
});
}
return prev;
}
}

if (!noSortAlphabetically && currentPropName < prevPropName) {
report(context, messages.propsNotSorted, 'propsNotSorted', {
node: curr,
if (!propsNotSortedSeen.has(curr)) {
propsNotSortedSeen.add(curr);
report(context, messages.propsNotSorted, 'propsNotSorted', {
node: curr,
// fix
});
});
}
return prev;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/lib/rules/sort-prop-types.js
Expand Up @@ -1860,5 +1860,24 @@ ruleTester.run('sort-prop-types', rule, {
},
],
},
{
code: `
var Component = React.createClass({
propTypes: {
onChange: React.PropTypes.func,
a: React.PropTypes.string,
c: React.PropTypes.string,
b: React.PropTypes.string,
}
});
`,
options: [{ callbacksLast: true }],
errors: [
{
messageId: 'callbackPropsLast',
line: 4,
},
],
},
]),
});

0 comments on commit f8b4aa3

Please sign in to comment.