Skip to content

Commit

Permalink
Add support for wrapped propTypes to sort-prop-types
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinsoftware committed Jun 24, 2017
1 parent b6e3a2f commit b25d791
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = {
var requiredFirst = configuration.requiredFirst || false;
var callbacksLast = configuration.callbacksLast || false;
var ignoreCase = configuration.ignoreCase || false;
var propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);

/**
* Checks if node is `propTypes` declaration
Expand Down Expand Up @@ -144,8 +145,23 @@ module.exports = {

return {
ClassProperty: function(node) {
if (isPropTypesDeclaration(node) && node.value && node.value.type === 'ObjectExpression') {
checkSorted(node.value.properties);
if (!isPropTypesDeclaration(node)) {
return;
}
switch (node.value && node.value.type) {
case 'ObjectExpression':
checkSorted(node.value.properties);
break;
case 'CallExpression':
if (
propWrapperFunctions.has(node.value.callee.name) &&
node.value.arguments && node.value.arguments[0]
) {
checkSorted(node.value.arguments[0].properties);
}
break;
default:
break;
}
},

Expand All @@ -156,6 +172,14 @@ module.exports = {
var right = node.parent.right;
var declarations;
switch (right && right.type) {
case 'CallExpression':
if (
propWrapperFunctions.has(right.callee.name) &&
right.arguments && right.arguments[0]
) {
declarations = right.arguments[0].properties;
}
break;
case 'ObjectExpression':
declarations = right.properties;
break;
Expand Down
44 changes: 44 additions & 0 deletions tests/lib/rules/sort-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,24 @@ ruleTester.run('sort-prop-types', rule, {
].join('\n'),
parser: 'babel-eslint',
errors: 2
}, {
code: [
'class Component extends React.Component {',
' static propTypes = forbidExtraProps({',
' z: PropTypes.any,',
' y: PropTypes.any,',
' a: PropTypes.any',
' });',
' render() {',
' return <div />;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
settings: {
propWrapperFunctions: ['forbidExtraProps']
},
errors: 2
}, {
code: [
'var First = createReactClass({',
Expand Down Expand Up @@ -519,6 +537,32 @@ ruleTester.run('sort-prop-types', rule, {
column: 5,
type: 'Property'
}]
}, {
code: [
'class First extends React.Component {',
' render() {',
' return <div />;',
' }',
'}',
'First.propTypes = forbidExtraProps({',
' a: PropTypes.any,',
' z: PropTypes.string,',
' onFoo: PropTypes.func,',
' onBar: PropTypes.func',
'});'
].join('\n'),
options: [{
callbacksLast: true
}],
settings: {
propWrapperFunctions: ['forbidExtraProps']
},
errors: [{
message: ERROR_MESSAGE,
line: 10,
column: 5,
type: 'Property'
}]
}, {
code: [
'var First = createReactClass({',
Expand Down

0 comments on commit b25d791

Please sign in to comment.