Skip to content

Commit b25d791

Browse files
Add support for wrapped propTypes to sort-prop-types
1 parent b6e3a2f commit b25d791

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

lib/rules/sort-prop-types.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = {
4040
var requiredFirst = configuration.requiredFirst || false;
4141
var callbacksLast = configuration.callbacksLast || false;
4242
var ignoreCase = configuration.ignoreCase || false;
43+
var propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
4344

4445
/**
4546
* Checks if node is `propTypes` declaration
@@ -144,8 +145,23 @@ module.exports = {
144145

145146
return {
146147
ClassProperty: function(node) {
147-
if (isPropTypesDeclaration(node) && node.value && node.value.type === 'ObjectExpression') {
148-
checkSorted(node.value.properties);
148+
if (!isPropTypesDeclaration(node)) {
149+
return;
150+
}
151+
switch (node.value && node.value.type) {
152+
case 'ObjectExpression':
153+
checkSorted(node.value.properties);
154+
break;
155+
case 'CallExpression':
156+
if (
157+
propWrapperFunctions.has(node.value.callee.name) &&
158+
node.value.arguments && node.value.arguments[0]
159+
) {
160+
checkSorted(node.value.arguments[0].properties);
161+
}
162+
break;
163+
default:
164+
break;
149165
}
150166
},
151167

@@ -156,6 +172,14 @@ module.exports = {
156172
var right = node.parent.right;
157173
var declarations;
158174
switch (right && right.type) {
175+
case 'CallExpression':
176+
if (
177+
propWrapperFunctions.has(right.callee.name) &&
178+
right.arguments && right.arguments[0]
179+
) {
180+
declarations = right.arguments[0].properties;
181+
}
182+
break;
159183
case 'ObjectExpression':
160184
declarations = right.properties;
161185
break;

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,24 @@ ruleTester.run('sort-prop-types', rule, {
449449
].join('\n'),
450450
parser: 'babel-eslint',
451451
errors: 2
452+
}, {
453+
code: [
454+
'class Component extends React.Component {',
455+
' static propTypes = forbidExtraProps({',
456+
' z: PropTypes.any,',
457+
' y: PropTypes.any,',
458+
' a: PropTypes.any',
459+
' });',
460+
' render() {',
461+
' return <div />;',
462+
' }',
463+
'}'
464+
].join('\n'),
465+
parser: 'babel-eslint',
466+
settings: {
467+
propWrapperFunctions: ['forbidExtraProps']
468+
},
469+
errors: 2
452470
}, {
453471
code: [
454472
'var First = createReactClass({',
@@ -519,6 +537,32 @@ ruleTester.run('sort-prop-types', rule, {
519537
column: 5,
520538
type: 'Property'
521539
}]
540+
}, {
541+
code: [
542+
'class First extends React.Component {',
543+
' render() {',
544+
' return <div />;',
545+
' }',
546+
'}',
547+
'First.propTypes = forbidExtraProps({',
548+
' a: PropTypes.any,',
549+
' z: PropTypes.string,',
550+
' onFoo: PropTypes.func,',
551+
' onBar: PropTypes.func',
552+
'});'
553+
].join('\n'),
554+
options: [{
555+
callbacksLast: true
556+
}],
557+
settings: {
558+
propWrapperFunctions: ['forbidExtraProps']
559+
},
560+
errors: [{
561+
message: ERROR_MESSAGE,
562+
line: 10,
563+
column: 5,
564+
type: 'Property'
565+
}]
522566
}, {
523567
code: [
524568
'var First = createReactClass({',

0 commit comments

Comments
 (0)