Skip to content

Commit 20f75ee

Browse files
author
Joachim Seminck
committed
Add support for TypeA & { ... }
1 parent a771125 commit 20f75ee

File tree

2 files changed

+71
-22
lines changed

2 files changed

+71
-22
lines changed

lib/rules/prop-types.js

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,20 @@ module.exports = {
710710
});
711711
}
712712

713+
function declarePropTypesForObjectTypeAnnotation(propTypes, declaredPropTypes) {
714+
let ignorePropsValidation = false;
715+
716+
iterateProperties(propTypes.properties, (key, value) => {
717+
if (!value) {
718+
ignorePropsValidation = true;
719+
return;
720+
}
721+
declaredPropTypes[key] = buildTypeAnnotationDeclarationTypes(value);
722+
});
723+
724+
return ignorePropsValidation;
725+
}
726+
713727
/**
714728
* Marks all props found inside IntersectionTypeAnnotation as declared.
715729
* Since InterSectionTypeAnnotations can be nested, this handles recursively.
@@ -723,22 +737,26 @@ module.exports = {
723737
let ignorePropsValidation = false;
724738

725739
propTypes.types.forEach(annotation => {
726-
const typeNode = typeScope(annotation.id.name);
727-
if (!typeNode) {
728-
ignorePropsValidation = true;
729-
return;
730-
}
731-
732-
if (typeNode.type === 'IntersectionTypeAnnotation') {
733-
ignorePropsValidation = declarePropTypesForIntersectionTypeAnnotation(typeNode, declaredPropTypes);
740+
if (annotation.type === 'ObjectTypeAnnotation') {
741+
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(annotation, declaredPropTypes);
734742
} else {
735-
iterateProperties(typeNode.properties, (key, value) => {
736-
if (!value) {
737-
ignorePropsValidation = true;
738-
return;
739-
}
740-
declaredPropTypes[key] = buildTypeAnnotationDeclarationTypes(value);
741-
});
743+
const typeNode = typeScope(annotation.id.name);
744+
if (!typeNode) {
745+
ignorePropsValidation = true;
746+
return;
747+
}
748+
749+
if (typeNode.type === 'IntersectionTypeAnnotation') {
750+
ignorePropsValidation = declarePropTypesForIntersectionTypeAnnotation(typeNode, declaredPropTypes);
751+
} else {
752+
iterateProperties(typeNode.properties, (key, value) => {
753+
if (!value) {
754+
ignorePropsValidation = true;
755+
return;
756+
}
757+
declaredPropTypes[key] = buildTypeAnnotationDeclarationTypes(value);
758+
});
759+
}
742760
}
743761
});
744762

@@ -761,13 +779,7 @@ module.exports = {
761779

762780
switch (propTypes && propTypes.type) {
763781
case 'ObjectTypeAnnotation':
764-
iterateProperties(propTypes.properties, (key, value) => {
765-
if (!value) {
766-
ignorePropsValidation = true;
767-
return;
768-
}
769-
declaredPropTypes[key] = buildTypeAnnotationDeclarationTypes(value);
770-
});
782+
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(propTypes, declaredPropTypes);
771783
break;
772784
case 'ObjectExpression':
773785
iterateProperties(propTypes.properties, (key, value) => {

tests/lib/rules/prop-types.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,23 @@ ruleTester.run('prop-types', rule, {
17161716
}
17171717
`,
17181718
parser: 'babel-eslint'
1719+
}, {
1720+
code: `
1721+
type PropsA = { bar: string };
1722+
type PropsB = { zap: string };
1723+
type Props = PropsA & {
1724+
baz: string
1725+
};
1726+
1727+
class Bar extends React.Component {
1728+
props: Props & PropsB;
1729+
1730+
render() {
1731+
return <div>{this.props.bar} - {this.props.zap} - {this.props.baz}</div>
1732+
}
1733+
}
1734+
`,
1735+
parser: 'babel-eslint'
17191736
}, {
17201737
code: `
17211738
type Props = { foo: string }
@@ -3426,6 +3443,26 @@ ruleTester.run('prop-types', rule, {
34263443
errors: [{
34273444
message: '\'fooBar\' is missing in props validation'
34283445
}]
3446+
}, {
3447+
code: `
3448+
type PropsB = { bar: string };
3449+
type PropsC = { zap: string };
3450+
type Props = PropsB & {
3451+
baz: string
3452+
};
3453+
3454+
class Bar extends React.Component {
3455+
props: Props & PropsC;
3456+
3457+
render() {
3458+
return <div>{this.props.bar} - {this.props.baz} - {this.props.fooBar}</div>
3459+
}
3460+
}
3461+
`,
3462+
errors: [{
3463+
message: '\'fooBar\' is missing in props validation'
3464+
}],
3465+
parser: 'babel-eslint'
34293466
}
34303467
]
34313468
});

0 commit comments

Comments
 (0)