Skip to content

Commit f8b4aa3

Browse files
committed
[Fix] sort-prop-types: avoid repeated warnings of the same node/reason
Fixes #519
1 parent a13593d commit f8b4aa3

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1616
* [`no-invalid-html-attribute`]: allow 'shortcut icon' on `link` ([#3174][] @Primajin)
1717
* [`prefer-exact-props`] improve performance for `Identifier` visitor ([#3190][] @meowtec)
1818
* `propTypes`: Handle TSTypeReference in no-unused-prop-type ([#3195][] @niik)
19+
* [`sort-prop-types`]: avoid repeated warnings of the same node/reason ([#519][] @ljharb)
1920

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

4345
## [7.28.0] - 2021.12.22
4446

lib/rules/sort-prop-types.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ module.exports = {
118118
// );
119119
// }
120120

121+
const callbackPropsLastSeen = new WeakSet();
122+
const requiredPropsFirstSeen = new WeakSet();
123+
const propsNotSortedSeen = new WeakSet();
124+
121125
declarations.reduce((prev, curr, idx, decls) => {
122126
if (curr.type === 'ExperimentalSpreadProperty' || curr.type === 'SpreadElement') {
123127
return decls[idx + 1];
@@ -142,10 +146,13 @@ module.exports = {
142146
}
143147
if (!previousIsRequired && currentIsRequired) {
144148
// Encountered a non-required prop after a required prop
145-
report(context, messages.requiredPropsFirst, 'requiredPropsFirst', {
146-
node: curr,
147-
// fix
148-
});
149+
if (!requiredPropsFirstSeen.has(curr)) {
150+
requiredPropsFirstSeen.add(curr);
151+
report(context, messages.requiredPropsFirst, 'requiredPropsFirst', {
152+
node: curr,
153+
// fix
154+
});
155+
}
149156
return curr;
150157
}
151158
}
@@ -157,19 +164,25 @@ module.exports = {
157164
}
158165
if (previousIsCallback && !currentIsCallback) {
159166
// Encountered a non-callback prop after a callback prop
160-
report(context, messages.callbackPropsLast, 'callbackPropsLast', {
161-
node: prev,
162-
// fix
163-
});
167+
if (!callbackPropsLastSeen.has(prev)) {
168+
callbackPropsLastSeen.add(prev);
169+
report(context, messages.callbackPropsLast, 'callbackPropsLast', {
170+
node: prev,
171+
// fix
172+
});
173+
}
164174
return prev;
165175
}
166176
}
167177

168178
if (!noSortAlphabetically && currentPropName < prevPropName) {
169-
report(context, messages.propsNotSorted, 'propsNotSorted', {
170-
node: curr,
179+
if (!propsNotSortedSeen.has(curr)) {
180+
propsNotSortedSeen.add(curr);
181+
report(context, messages.propsNotSorted, 'propsNotSorted', {
182+
node: curr,
171183
// fix
172-
});
184+
});
185+
}
173186
return prev;
174187
}
175188

tests/lib/rules/sort-prop-types.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,5 +1860,24 @@ ruleTester.run('sort-prop-types', rule, {
18601860
},
18611861
],
18621862
},
1863+
{
1864+
code: `
1865+
var Component = React.createClass({
1866+
propTypes: {
1867+
onChange: React.PropTypes.func,
1868+
a: React.PropTypes.string,
1869+
c: React.PropTypes.string,
1870+
b: React.PropTypes.string,
1871+
}
1872+
});
1873+
`,
1874+
options: [{ callbacksLast: true }],
1875+
errors: [
1876+
{
1877+
messageId: 'callbackPropsLast',
1878+
line: 4,
1879+
},
1880+
],
1881+
},
18631882
]),
18641883
});

0 commit comments

Comments
 (0)