Skip to content

Commit

Permalink
Add support for wrapped propTypes to require-default-props
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinsoftware committed Jun 24, 2017
1 parent 61b65a0 commit 4df159b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/rules/require-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,25 @@ module.exports = {
category: 'Best Practices'
},

schema: []
schema: [{
type: 'object',
properties: {
propWrapperFunctions: {
type: 'array',
items: {
type: 'string'
},
uniqueItems: true
}
},
additionalProperties: false
}]
},

create: Components.detect(function(context, components, utils) {
var sourceCode = context.getSourceCode();
var configuration = context.options[0] || {};
var propWrapperFunctions = new Set(configuration.propWrapperFunctions || []);

/**
* Get properties name
Expand Down Expand Up @@ -105,6 +119,13 @@ module.exports = {
if (node.type === 'Identifier') {
return findVariableByName(node.name);
}
if (
node.type === 'CallExpression' &&
propWrapperFunctions.has(node.callee.name) &&
node.arguments && node.arguments[0]
) {
return node.arguments[0];
}

return node;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/require-default-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,23 @@ ruleTester.run('require-default-props', rule, {
column: 3
}]
},
{
code: [
'function MyStatelessComponent({ foo, bar }) {',
' return <div>{foo}{bar}</div>;',
'}',
'MyStatelessComponent.propTypes = forbidExtraProps({',
' foo: PropTypes.string,',
' bar: PropTypes.string.isRequired',
'});'
].join('\n'),
errors: [{
message: 'propType "foo" is not required, but has no corresponding defaultProp declaration.',
line: 5,
column: 3
}],
options: [{propWrapperFunctions: ['forbidExtraProps']}]
},
{
code: [
'function MyStatelessComponent({ foo, bar }) {',
Expand Down

0 comments on commit 4df159b

Please sign in to comment.