Skip to content

Commit 763382f

Browse files
committed
Fix prop-types on annotated components (fixes #729)
1 parent 3fdd030 commit 763382f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/rules/prop-types.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,12 @@ module.exports = {
118118
function isAnnotatedFunctionPropsDeclaration(node) {
119119
if (node && node.params && node.params.length) {
120120
var tokens = context.getFirstTokens(node.params[0], 2);
121-
var isAnnotated = node.params[0].typeAnnotation;
121+
var isAnnotated = (
122+
node.params[0].typeAnnotation &&
123+
node.params[0].typeAnnotation.typeAnnotation &&
124+
node.params[0].typeAnnotation.typeAnnotation.id &&
125+
node.params[0].typeAnnotation.typeAnnotation.id.name === 'Props'
126+
);
122127
var isDestructuredProps = node.params[0].type === 'ObjectPattern';
123128
var isProps = tokens[0].value === 'props' || (tokens[1] && tokens[1].value === 'props');
124129
if (isAnnotated && (isDestructuredProps || isProps)) {

tests/lib/rules/prop-types.js

+14
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,20 @@ ruleTester.run('prop-types', rule, {
12801280
'}'
12811281
].join('\n'),
12821282
parser: 'babel-eslint'
1283+
}, {
1284+
code: [
1285+
'type Target = { target: EventTarget }',
1286+
'class MyComponent extends Component {',
1287+
' static propTypes = {',
1288+
' children: PropTypes.any,',
1289+
' }',
1290+
' handler({ target }: Target) {}',
1291+
' render() {',
1292+
' return <div>{this.props.children}</div>;',
1293+
' }',
1294+
'}'
1295+
].join('\n'),
1296+
parser: 'babel-eslint'
12831297
}
12841298
],
12851299

0 commit comments

Comments
 (0)