diff --git a/lib/util/annotations.js b/lib/util/annotations.js index 3161381b5b..d52fb214f1 100644 --- a/lib/util/annotations.js +++ b/lib/util/annotations.js @@ -17,9 +17,11 @@ function isAnnotatedFunctionPropsDeclaration(node, context) { return false; } - const tokens = context.getFirstTokens(node.params[0], 2); - const isAnnotated = node.params[0].typeAnnotation; - const isDestructuredProps = node.params[0].type === 'ObjectPattern'; + const typeNode = node.params[0].type === 'AssignmentPattern' ? node.params[0].left : node.params[0]; + + const tokens = context.getFirstTokens(typeNode, 2); + const isAnnotated = typeNode.typeAnnotation; + const isDestructuredProps = typeNode.type === 'ObjectPattern'; const isProps = tokens[0].value === 'props' || (tokens[1] && tokens[1].value === 'props'); return (isAnnotated && (isDestructuredProps || isProps)); diff --git a/lib/util/propTypes.js b/lib/util/propTypes.js index b38eaf0dcc..be8eb6501f 100644 --- a/lib/util/propTypes.js +++ b/lib/util/propTypes.js @@ -200,7 +200,7 @@ module.exports = function propTypesInstructions(context, components, utils) { * @returns {ASTNode} The resolved type annotation for the node. */ function resolveTypeAnnotation(node) { - let annotation = node.typeAnnotation || node; + let annotation = (node.left && node.left.typeAnnotation) || node.typeAnnotation || node; while (annotation && (annotation.type === 'TypeAnnotation' || annotation.type === 'NullableTypeAnnotation')) { annotation = annotation.typeAnnotation; } diff --git a/tests/lib/rules/prop-types.js b/tests/lib/rules/prop-types.js index 301a6f78a7..6685103fa1 100755 --- a/tests/lib/rules/prop-types.js +++ b/tests/lib/rules/prop-types.js @@ -2336,6 +2336,19 @@ ruleTester.run('prop-types', rule, { foo: PropTypes.number, } ` + }, + { + // issue #2298 + code: ` + type Props = {| + firstname?: string + |}; + + function Hello({ firstname = 'John' }: Props = {}) { + return
Hello {firstname}
+ } + `, + parser: parsers.BABEL_ESLINT } ], @@ -4621,6 +4634,22 @@ ruleTester.run('prop-types', rule, { errors: [{ message: '\'initialValues\' is missing in props validation' }] + }, + { + // issue #2298 + code: ` + type Props = {| + firstname?: string + |}; + + function Hello({ firstname = 'John', lastname = 'Doe' }: Props = {}) { + return
Hello {firstname} {lastname}
+ } + `, + parser: parsers.BABEL_ESLINT, + errors: [{ + message: '\'lastname\' is missing in props validation' + }] } ] });