Skip to content

Commit 2fabd1f

Browse files
committed
Add support to IntersectionTypeAnnotation for no-unused-prop-types
1 parent 2ef79a4 commit 2fabd1f

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,26 @@ module.exports = {
736736
return;
737737
}
738738
break;
739+
case 'IntersectionTypeAnnotation':
740+
propTypes.types.forEach(annotation => {
741+
const propsType = typeScope(annotation.id.name);
742+
iterateProperties(propsType.properties, (key, value) => {
743+
if (!value) {
744+
ignorePropsValidation = true;
745+
return;
746+
}
747+
748+
let types = buildTypeAnnotationDeclarationTypes(value, key);
749+
if (types === true) {
750+
types = {};
751+
}
752+
types.fullName = key;
753+
types.name = key;
754+
types.node = value;
755+
declaredPropTypes.push(types);
756+
});
757+
});
758+
break;
739759
case null:
740760
break;
741761
default:

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,21 @@ ruleTester.run('no-unused-prop-types', rule, {
824824
'}'
825825
].join('\n'),
826826
parser: 'babel-eslint'
827+
}, {
828+
code: `
829+
type PropsA = { a: string }
830+
type PropsB = { b: string }
831+
type Props = PropsA & PropsB;
832+
833+
class MyComponent extends React.Component {
834+
props: Props;
835+
836+
render() {
837+
return <div>{this.props.a} - {this.props.b}</div>
838+
}
839+
}
840+
`,
841+
parser: 'babel-eslint'
827842
}, {
828843
code: [
829844
'import type Props from "fake";',
@@ -2637,6 +2652,24 @@ ruleTester.run('no-unused-prop-types', rule, {
26372652
errors: [
26382653
{message: '\'unused\' PropType is defined but prop is never used'}
26392654
]
2655+
}, {
2656+
code: `
2657+
type PropsA = { a: string }
2658+
type PropsB = { b: string }
2659+
type Props = PropsA & PropsB;
2660+
2661+
class MyComponent extends React.Component {
2662+
props: Props;
2663+
2664+
render() {
2665+
return <div>{this.props.a}</div>
2666+
}
2667+
}
2668+
`,
2669+
parser: 'babel-eslint',
2670+
errors: [
2671+
{message: '\'b\' PropType is defined but prop is never used'}
2672+
]
26402673
}, {
26412674
code: [
26422675
'class Hello extends React.Component {',

tests/lib/rules/prop-types.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3280,17 +3280,11 @@ ruleTester.run('prop-types', rule, {
32803280
parser: 'babel-eslint'
32813281
}, {
32823282
code: `
3283-
type PropsA = {
3284-
foo: string,
3285-
};
3286-
3287-
type PropsB = {
3288-
bar: string,
3289-
};
3290-
3283+
type PropsA = {foo: string };
3284+
type PropsB = { bar: string };
32913285
type Props = PropsA & PropsB;
32923286
3293-
class Bar extends React.Component {
3287+
class MyComponent extends React.Component {
32943288
props: Props;
32953289
32963290
render() {

0 commit comments

Comments
 (0)