Skip to content

Commit 0e70a3a

Browse files
committed
Fix #2358 - Recognize props wrapped in flow $ReadOnly<> utility type
1 parent 60d502d commit 0e70a3a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/util/propTypes.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,16 @@ module.exports = function propTypesInstructions(context, components, utils) {
545545
case 'IntersectionTypeAnnotation':
546546
ignorePropsValidation = declarePropTypesForIntersectionTypeAnnotation(propTypes, declaredPropTypes);
547547
break;
548+
case 'GenericTypeAnnotation':
549+
if (propTypes.id.name === '$ReadOnly') {
550+
ignorePropsValidation = declarePropTypesForObjectTypeAnnotation(
551+
propTypes.typeParameters.params[0],
552+
declaredPropTypes
553+
);
554+
} else {
555+
ignorePropsValidation = true;
556+
}
557+
break;
548558
case null:
549559
break;
550560
default:

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,17 @@ ruleTester.run('no-unused-prop-types', rule, {
27102710
};
27112711
`,
27122712
parser: parsers.BABEL_ESLINT
2713+
}, {
2714+
code: [
2715+
'type Props = $ReadOnly<{foo: number}>;',
2716+
'class Hello extends React.Component {',
2717+
' props: Props;',
2718+
' render () {',
2719+
' return <div>{this.props.foo}</div>;',
2720+
' }',
2721+
'}'
2722+
].join('\n'),
2723+
parser: parsers.BABEL_ESLINT
27132724
}, {
27142725
// issue #933
27152726
code: `
@@ -3656,6 +3667,20 @@ ruleTester.run('no-unused-prop-types', rule, {
36563667
errors: [
36573668
{message: '\'unused\' PropType is defined but prop is never used'}
36583669
]
3670+
}, {
3671+
code: [
3672+
'type Props = $ReadOnly<{unused: Object;}>;',
3673+
'class Hello extends React.Component {',
3674+
' props: Props;',
3675+
' render () {',
3676+
' return <div>Hello {this.props.firstname}</div>;',
3677+
' }',
3678+
'}'
3679+
].join('\n'),
3680+
parser: parsers.BABEL_ESLINT,
3681+
errors: [
3682+
{message: '\'unused\' PropType is defined but prop is never used'}
3683+
]
36593684
}, {
36603685
code: `
36613686
type PropsA = { a: string }

0 commit comments

Comments
 (0)