Skip to content

Commit 0b2a18b

Browse files
committed
Fix sort-prop-types crash with spread operator (fixes #478)
1 parent d54f1a4 commit 0b2a18b

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

docs/rules/sort-prop-types.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ Some developers prefer to sort propTypes declarations alphabetically to be able
44

55
## Rule Details
66

7-
This rule checks all components and verifies that all propTypes declarations are sorted alphabetically.
8-
The default configuration of the rule is case-sensitive.
9-
This rule is off by default.
7+
This rule checks all components and verifies that all propTypes declarations are sorted alphabetically. A spread attribute resets the verification. The default configuration of the rule is case-sensitive.
108

119
The following patterns are considered warnings:
1210

lib/rules/sort-prop-types.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
module.exports = function(context) {
1111

12+
var sourceCode = context.getSourceCode();
1213
var configuration = context.options[0] || {};
1314
var requiredFirst = configuration.requiredFirst || false;
1415
var callbacksLast = configuration.callbacksLast || false;
@@ -36,11 +37,11 @@ module.exports = function(context) {
3637
}
3738

3839
function getKey(node) {
39-
return node.key.type === 'Identifier' ? node.key.name : node.key.value;
40+
return sourceCode.getText(node.key || node.argument);
4041
}
4142

4243
function getValueName(node) {
43-
return node.value.property && node.value.property.name;
44+
return node.type === 'Property' && node.value.property && node.value.property.name;
4445
}
4546

4647
function isCallbackPropName(propName) {
@@ -57,7 +58,11 @@ module.exports = function(context) {
5758
* @returns {void}
5859
*/
5960
function checkSorted(declarations) {
60-
declarations.reduce(function(prev, curr) {
61+
declarations.reduce(function(prev, curr, idx, decls) {
62+
if (/SpreadProperty$/.test(curr.type)) {
63+
return decls[idx + 1];
64+
}
65+
6166
var prevPropName = getKey(prev);
6267
var currentPropName = getKey(curr);
6368
var previousIsRequired = isRequiredProp(prev);

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,17 @@ ruleTester.run('sort-prop-types', rule, {
312312
callbacksLast: true
313313
}],
314314
parserOptions: parserOptions
315+
}, {
316+
code: [
317+
'export default class ClassWithSpreadInPropTypes extends BaseClass {',
318+
' static propTypes = {',
319+
' b: PropTypes.string,',
320+
' ...c.propTypes,',
321+
' a: PropTypes.string',
322+
' }',
323+
'}'
324+
].join('\n'),
325+
parser: 'babel-eslint'
315326
}],
316327

317328
invalid: [{
@@ -595,5 +606,23 @@ ruleTester.run('sort-prop-types', rule, {
595606
column: 5,
596607
type: 'Property'
597608
}]
609+
}, {
610+
code: [
611+
'export default class ClassWithSpreadInPropTypes extends BaseClass {',
612+
' static propTypes = {',
613+
' b: PropTypes.string,',
614+
' ...a.propTypes,',
615+
' d: PropTypes.string,',
616+
' c: PropTypes.string',
617+
' }',
618+
'}'
619+
].join('\n'),
620+
parser: 'babel-eslint',
621+
errors: [{
622+
message: 'Prop types declarations should be sorted alphabetically',
623+
line: 6,
624+
column: 5,
625+
type: 'Property'
626+
}]
598627
}]
599628
});

0 commit comments

Comments
 (0)