Skip to content

Commit

Permalink
Add support for TypeA & { ... }
Browse files Browse the repository at this point in the history
  • Loading branch information
Joachim Seminck committed Sep 6, 2017
1 parent a771125 commit 20f75ee
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 22 deletions.
56 changes: 34 additions & 22 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,20 @@ module.exports = {
});
}

function declarePropTypesForObjectTypeAnnotation(propTypes, declaredPropTypes) {
let ignorePropsValidation = false;

iterateProperties(propTypes.properties, (key, value) => {
if (!value) {
ignorePropsValidation = true;
return;
}
declaredPropTypes[key] = buildTypeAnnotationDeclarationTypes(value);
});

return ignorePropsValidation;
}

/**
* Marks all props found inside IntersectionTypeAnnotation as declared.
* Since InterSectionTypeAnnotations can be nested, this handles recursively.
Expand All @@ -723,22 +737,26 @@ module.exports = {
let ignorePropsValidation = false;

propTypes.types.forEach(annotation => {
const typeNode = typeScope(annotation.id.name);
if (!typeNode) {
ignorePropsValidation = true;
return;
}

if (typeNode.type === 'IntersectionTypeAnnotation') {
ignorePropsValidation = declarePropTypesForIntersectionTypeAnnotation(typeNode, declaredPropTypes);
if (annotation.type === 'ObjectTypeAnnotation') {
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(annotation, declaredPropTypes);
} else {
iterateProperties(typeNode.properties, (key, value) => {
if (!value) {
ignorePropsValidation = true;
return;
}
declaredPropTypes[key] = buildTypeAnnotationDeclarationTypes(value);
});
const typeNode = typeScope(annotation.id.name);
if (!typeNode) {
ignorePropsValidation = true;
return;
}

if (typeNode.type === 'IntersectionTypeAnnotation') {
ignorePropsValidation = declarePropTypesForIntersectionTypeAnnotation(typeNode, declaredPropTypes);
} else {
iterateProperties(typeNode.properties, (key, value) => {
if (!value) {
ignorePropsValidation = true;
return;
}
declaredPropTypes[key] = buildTypeAnnotationDeclarationTypes(value);
});
}
}
});

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

switch (propTypes && propTypes.type) {
case 'ObjectTypeAnnotation':
iterateProperties(propTypes.properties, (key, value) => {
if (!value) {
ignorePropsValidation = true;
return;
}
declaredPropTypes[key] = buildTypeAnnotationDeclarationTypes(value);
});
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(propTypes, declaredPropTypes);
break;
case 'ObjectExpression':
iterateProperties(propTypes.properties, (key, value) => {
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,23 @@ ruleTester.run('prop-types', rule, {
}
`,
parser: 'babel-eslint'
}, {
code: `
type PropsA = { bar: string };
type PropsB = { zap: string };
type Props = PropsA & {
baz: string
};
class Bar extends React.Component {
props: Props & PropsB;
render() {
return <div>{this.props.bar} - {this.props.zap} - {this.props.baz}</div>
}
}
`,
parser: 'babel-eslint'
}, {
code: `
type Props = { foo: string }
Expand Down Expand Up @@ -3426,6 +3443,26 @@ ruleTester.run('prop-types', rule, {
errors: [{
message: '\'fooBar\' is missing in props validation'
}]
}, {
code: `
type PropsB = { bar: string };
type PropsC = { zap: string };
type Props = PropsB & {
baz: string
};
class Bar extends React.Component {
props: Props & PropsC;
render() {
return <div>{this.props.bar} - {this.props.baz} - {this.props.fooBar}</div>
}
}
`,
errors: [{
message: '\'fooBar\' is missing in props validation'
}],
parser: 'babel-eslint'
}
]
});

0 comments on commit 20f75ee

Please sign in to comment.