Skip to content

Commit f6a8676

Browse files
committed
Add support for IntersectionTypeAnnotation
1 parent e2f6460 commit f6a8676

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lib/rules/prop-types.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
// https://github.com/yannickcr/eslint-plugin-react/issues/7
99

1010
const has = require('has');
11+
const util = require('util');
1112
const Components = require('../util/Components');
1213
const variable = require('../util/variable');
1314
const annotations = require('../util/annotations');
1415
const versionUtil = require('../util/version');
15-
1616
// ------------------------------------------------------------------------------
1717
// Constants
1818
// ------------------------------------------------------------------------------
@@ -794,6 +794,18 @@ module.exports = {
794794
return;
795795
}
796796
break;
797+
case 'IntersectionTypeAnnotation':
798+
propTypes.types.forEach(annotation => {
799+
const propsType = typeScope(annotation.id.name);
800+
iterateProperties(propsType.properties, (key, value) => {
801+
if (!value) {
802+
ignorePropsValidation = true;
803+
return;
804+
}
805+
declaredPropTypes[key] = buildTypeAnnotationDeclarationTypes(value);
806+
});
807+
});
808+
break;
797809
case null:
798810
break;
799811
default:

tests/lib/rules/prop-types.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,29 @@ ruleTester.run('prop-types', rule, {
16691669
`,
16701670
settings: {react: {flowVersion: '0.53'}},
16711671
parser: 'babel-eslint'
1672+
}, {
1673+
code: `
1674+
type PropsA = {
1675+
foo: string,
1676+
};
1677+
1678+
type PropsB = {
1679+
bar: string,
1680+
};
1681+
1682+
type Props = PropsA & PropsB;
1683+
1684+
class Bar extends React.Component {
1685+
props: Props;
1686+
1687+
render() {
1688+
return <div>{this.props.foo} - {this.props.bar}</div>
1689+
}
1690+
}
1691+
`,
1692+
parser: 'babel-eslint'
16721693
},
1694+
16731695
// issue #1288
16741696
`function Foo() {
16751697
const props = {}
@@ -3256,6 +3278,30 @@ ruleTester.run('prop-types', rule, {
32563278
type: 'Identifier'
32573279
}],
32583280
parser: 'babel-eslint'
3281+
}, {
3282+
code: `
3283+
type PropsA = {
3284+
foo: string,
3285+
};
3286+
3287+
type PropsB = {
3288+
bar: string,
3289+
};
3290+
3291+
type Props = PropsA & PropsB;
3292+
3293+
class Bar extends React.Component {
3294+
props: Props;
3295+
3296+
render() {
3297+
return <div>{this.props.foo} - {this.props.bar} - {this.props.fooBar}</div>
3298+
}
3299+
}
3300+
`,
3301+
parser: 'babel-eslint',
3302+
errors: [{
3303+
message: '\'fooBar\' is missing in props validation'
3304+
}]
32593305
}
32603306
]
32613307
});

0 commit comments

Comments
 (0)