Skip to content

Commit 412f44c

Browse files
Add support for wrapped propTypes to default-props-match-prop-types
1 parent eda7e1d commit 412f44c

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/rules/default-props-match-prop-types.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = {
3636
create: Components.detect(function(context, components, utils) {
3737
const configuration = context.options[0] || {};
3838
const allowRequiredDefaults = configuration.allowRequiredDefaults || false;
39+
var propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
3940

4041
/**
4142
* Get properties name
@@ -113,7 +114,13 @@ module.exports = {
113114
if (node.type === 'Identifier') {
114115
return findVariableByName(node.name);
115116
}
116-
117+
if (
118+
node.type === 'CallExpression' &&
119+
propWrapperFunctions.has(node.callee.name) &&
120+
node.arguments && node.arguments[0]
121+
) {
122+
return node.arguments[0];
123+
}
117124
return node;
118125
}
119126

tests/lib/rules/default-props-match-prop-types.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,28 @@ ruleTester.run('default-props-match-prop-types', rule, {
761761
column: 3
762762
}]
763763
},
764+
{
765+
code: [
766+
'function MyStatelessComponent({ foo, bar }) {',
767+
' return <div>{foo}{bar}</div>;',
768+
'}',
769+
'MyStatelessComponent.propTypes = forbidExtraProps({',
770+
' foo: React.PropTypes.string,',
771+
' bar: React.PropTypes.string.isRequired',
772+
'})',
773+
'MyStatelessComponent.defaultProps = {',
774+
' baz: "baz"',
775+
'};'
776+
].join('\n'),
777+
settings: {
778+
propWrapperFunctions: ['forbidExtraProps']
779+
},
780+
errors: [{
781+
message: 'defaultProp "baz" has no corresponding propTypes declaration.',
782+
line: 9,
783+
column: 3
784+
}]
785+
},
764786
{
765787
code: [
766788
'function MyStatelessComponent({ foo, bar }) {',
@@ -1219,6 +1241,25 @@ ruleTester.run('default-props-match-prop-types', rule, {
12191241
column: 5
12201242
}]
12211243
},
1244+
{
1245+
code: [
1246+
'function MyStatelessComponent({ foo, bar }) {',
1247+
' return <div>{foo}{bar}</div>;',
1248+
'}',
1249+
'MyStatelessComponent.propTypes = {',
1250+
' foo: React.PropTypes.string,',
1251+
' bar: React.PropTypes.string.isRequired',
1252+
'};',
1253+
'MyStatelessComponent.defaultProps = {',
1254+
' baz: "baz"',
1255+
'};'
1256+
].join('\n'),
1257+
errors: [{
1258+
message: 'defaultProp "baz" has no corresponding propTypes declaration.',
1259+
line: 9,
1260+
column: 3
1261+
}]
1262+
},
12221263
{
12231264
code: [
12241265
'class Greeting extends React.Component {',

0 commit comments

Comments
 (0)