Skip to content

Add support for Flow IntersectionTypeAnnotation to prop-types and no-unused-prop-types #1404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions lib/rules/default-props-match-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,47 @@ module.exports = {
}));
}

/**
* Handles Props defined in IntersectionTypeAnnotation nodes
* e.g. type Props = PropsA & PropsB
* @param {ASTNode} intersectionTypeAnnotation ObjectExpression node.
* @returns {Object[]}
*/
function getPropertiesFromIntersectionTypeAnnotationNode(annotation) {
return annotation.types.reduce((properties, type) => {
annotation = resolveGenericTypeAnnotation(type);

if (annotation && annotation.id) {
annotation = findVariableByName(annotation.id.name);
}

return properties.concat(annotation.properties);
}, []);
}

/**
* Extracts a PropType from a TypeAnnotation node.
* @param {ASTNode} node TypeAnnotation node.
* @returns {Object[]} Array of PropType object representations, to be consumed by `addPropTypesToComponent`.
*/
function getPropTypesFromTypeAnnotation(node) {
let properties;
let properties = [];

switch (node.typeAnnotation.type) {
case 'GenericTypeAnnotation':
let annotation = resolveGenericTypeAnnotation(node.typeAnnotation);

if (annotation && annotation.id) {
annotation = findVariableByName(annotation.id.name);
if (annotation && annotation.type === 'IntersectionTypeAnnotation') {
properties = getPropertiesFromIntersectionTypeAnnotationNode(annotation);
} else {
if (annotation && annotation.id) {
annotation = findVariableByName(annotation.id.name);
}

properties = annotation ? (annotation.properties || []) : [];
}

properties = annotation ? (annotation.properties || []) : [];

break;

case 'UnionTypeAnnotation':
Expand Down Expand Up @@ -314,7 +338,6 @@ module.exports = {
if (!component) {
return;
}

addPropTypesToComponent(component, getPropTypesFromTypeAnnotation(node.typeAnnotation, context));
}

Expand Down
20 changes: 20 additions & 0 deletions lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,26 @@ module.exports = {
return;
}
break;
case 'IntersectionTypeAnnotation':
propTypes.types.forEach(annotation => {
const propsType = typeScope(annotation.id.name);
iterateProperties(propsType.properties, (key, value) => {
if (!value) {
ignorePropsValidation = true;
return;
}

let types = buildTypeAnnotationDeclarationTypes(value, key);
if (types === true) {
types = {};
}
types.fullName = key;
types.name = key;
types.node = value;
declaredPropTypes.push(types);
});
});
break;
case null:
break;
default:
Expand Down
13 changes: 12 additions & 1 deletion lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const Components = require('../util/Components');
const variable = require('../util/variable');
const annotations = require('../util/annotations');
const versionUtil = require('../util/version');

// ------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -794,6 +793,18 @@ module.exports = {
return;
}
break;
case 'IntersectionTypeAnnotation':
propTypes.types.forEach(annotation => {
const propsType = typeScope(annotation.id.name);
iterateProperties(propsType.properties, (key, value) => {
if (!value) {
ignorePropsValidation = true;
return;
}
declaredPropTypes[key] = buildTypeAnnotationDeclarationTypes(value);
});
});
break;
case null:
break;
default:
Expand Down
48 changes: 48 additions & 0 deletions tests/lib/rules/default-props-match-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,26 @@ ruleTester.run('default-props-match-prop-types', rule, {
].join('\n'),
parser: 'babel-eslint'
},
{
code: `
type PropsA = { foo?: string };
type PropsB = { bar?: string, fooBar: string };
type Props = PropsA & PropsB;

class Bar extends React.Component {
props: Props;
static defaultProps = {
foo: "foo",
bar: "bar",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case where a defaultProp is omitted for a ? prop type, and there's no error? Alternatively, you could just remove the defaultProp for "bar" here.

}

render() {
return <div>{this.props.foo} - {this.props.bar}</div>
}
}
`,
parser: 'babel-eslint'
},
{
code: [
'import type Props from "fake";',
Expand Down Expand Up @@ -1509,6 +1529,34 @@ ruleTester.run('default-props-match-prop-types', rule, {
column: 36
}
]
},
{
code: `
type PropsA = { foo: string };
type PropsB = { bar: string };
type Props = PropsA & PropsB;

class Bar extends React.Component {
props: Props;
static defaultProps = {
fooBar: "fooBar",
foo: "foo",
}

render() {
return <div>{this.props.foo} - {this.props.bar}</div>
}
}
`,
parser: 'babel-eslint',
errors: [
{
message: 'defaultProp "fooBar" has no corresponding propTypes declaration.'
},
{
message: 'defaultProp "foo" defined for isRequired propType.'
}
]
}
]
});
33 changes: 33 additions & 0 deletions tests/lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,21 @@ ruleTester.run('no-unused-prop-types', rule, {
'}'
].join('\n'),
parser: 'babel-eslint'
}, {
code: `
type PropsA = { a: string }
type PropsB = { b: string }
type Props = PropsA & PropsB;

class MyComponent extends React.Component {
props: Props;

render() {
return <div>{this.props.a} - {this.props.b}</div>
}
}
`,
parser: 'babel-eslint'
}, {
code: [
'import type Props from "fake";',
Expand Down Expand Up @@ -2637,6 +2652,24 @@ ruleTester.run('no-unused-prop-types', rule, {
errors: [
{message: '\'unused\' PropType is defined but prop is never used'}
]
}, {
code: `
type PropsA = { a: string }
type PropsB = { b: string }
type Props = PropsA & PropsB;

class MyComponent extends React.Component {
props: Props;

render() {
return <div>{this.props.a}</div>
}
}
`,
parser: 'babel-eslint',
errors: [
{message: '\'b\' PropType is defined but prop is never used'}
]
}, {
code: [
'class Hello extends React.Component {',
Expand Down
34 changes: 34 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1669,7 +1669,23 @@ ruleTester.run('prop-types', rule, {
`,
settings: {react: {flowVersion: '0.53'}},
parser: 'babel-eslint'
}, {
code: `
type PropsA = { foo: string };
type PropsB = { bar: string };
type Props = PropsA & PropsB;

class Bar extends React.Component {
props: Props;

render() {
return <div>{this.props.foo} - {this.props.bar}</div>
}
}
`,
parser: 'babel-eslint'
},

// issue #1288
`function Foo() {
const props = {}
Expand Down Expand Up @@ -3256,6 +3272,24 @@ ruleTester.run('prop-types', rule, {
type: 'Identifier'
}],
parser: 'babel-eslint'
}, {
code: `
type PropsA = {foo: string };
type PropsB = { bar: string };
type Props = PropsA & PropsB;

class MyComponent extends React.Component {
props: Props;

render() {
return <div>{this.props.foo} - {this.props.bar} - {this.props.fooBar}</div>
}
}
`,
parser: 'babel-eslint',
errors: [{
message: '\'fooBar\' is missing in props validation'
}]
}
]
});