Skip to content

Commit df17cd4

Browse files
authored
Merge pull request #1563 from justinanastos/fix/prop-types-union-flowtype-1468
Fix crash when using Unions in flow propTypes
2 parents 6829c5c + dd10851 commit df17cd4

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

lib/rules/no-unused-prop-types.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,10 @@ module.exports = {
780780
return declarePropTypesForObjectTypeAnnotation(annotation, declaredPropTypes);
781781
}
782782

783+
if (annotation.type === 'UnionTypeAnnotation') {
784+
return true;
785+
}
786+
783787
const typeNode = typeScope(annotation.id.name);
784788

785789
if (!typeNode) {

lib/rules/prop-types.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,10 @@ module.exports = {
726726
return declarePropTypesForObjectTypeAnnotation(annotation, declaredPropTypes);
727727
}
728728

729+
if (annotation.type === 'UnionTypeAnnotation') {
730+
return true;
731+
}
732+
729733
const typeNode = typeScope(annotation.id.name);
730734

731735
if (!typeNode) {

tests/lib/rules/no-unused-prop-types.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,28 @@ ruleTester.run('no-unused-prop-types', rule, {
10971097
'}'
10981098
].join('\n'),
10991099
parser: 'babel-eslint'
1100+
}, {
1101+
code: [
1102+
'type PropsUnionA = {',
1103+
' a: string,',
1104+
' b?: void,',
1105+
'};',
1106+
'type PropsUnionB = {',
1107+
' a?: void,',
1108+
' b: string,',
1109+
'};',
1110+
'type Props = {',
1111+
' name: string,',
1112+
'} & (PropsUnionA | PropsUnionB);',
1113+
'class Hello extends React.Component {',
1114+
' props: Props;',
1115+
' render() {',
1116+
' const {name} = this.props;',
1117+
' return name;',
1118+
' }',
1119+
'}'
1120+
].join('\n'),
1121+
parser: 'babel-eslint'
11001122
}, {
11011123
code: [
11021124
'Card.propTypes = {',

tests/lib/rules/prop-types.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,28 @@ ruleTester.run('prop-types', rule, {
11291129
'}'
11301130
].join('\n'),
11311131
parser: 'babel-eslint'
1132+
}, {
1133+
code: [
1134+
'type PropsUnionA = {',
1135+
' a: string,',
1136+
' b?: void,',
1137+
'};',
1138+
'type PropsUnionB = {',
1139+
' a?: void,',
1140+
' b: string,',
1141+
'};',
1142+
'type Props = {',
1143+
' name: string,',
1144+
'} & (PropsUnionA | PropsUnionB);',
1145+
'class Hello extends React.Component {',
1146+
' props: Props;',
1147+
' render() {',
1148+
' const {name} = this.props;',
1149+
' return name;',
1150+
' }',
1151+
'}'
1152+
].join('\n'),
1153+
parser: 'babel-eslint'
11321154
}, {
11331155
code: [
11341156
'Card.propTypes = {',

0 commit comments

Comments
 (0)