@@ -61,7 +61,7 @@ module.exports = Components.detect(function(context, components, utils) {
6161 * @param {ASTNode } node The AST node being checked.
6262 * @returns {Boolean } True if the node is a type annotated props declaration, false if not.
6363 */
64- function isAnnotatedPropsDeclaration ( node ) {
64+ function isAnnotatedClassPropsDeclaration ( node ) {
6565 if ( node && node . type === 'ClassProperty' ) {
6666 var tokens = context . getFirstTokens ( node , 2 ) ;
6767 if (
@@ -76,6 +76,26 @@ module.exports = Components.detect(function(context, components, utils) {
7676 return false ;
7777 }
7878
79+ /**
80+ * Checks if we are declaring a `props` argument with a flow type annotation.
81+ * @param {ASTNode } node The AST node being checked.
82+ * @returns {Boolean } True if the node is a type annotated props declaration, false if not.
83+ */
84+ function isAnnotatedFunctionPropsDeclaration ( node ) {
85+ if ( node && node . params && node . params . length ) {
86+ var tokens = context . getFirstTokens ( node . params [ 0 ] , 2 ) ;
87+ if (
88+ node . params [ 0 ] . typeAnnotation && (
89+ tokens [ 0 ] . value === 'props' ||
90+ ( tokens [ 1 ] && tokens [ 1 ] . value === 'props' )
91+ )
92+ ) {
93+ return true ;
94+ }
95+ }
96+ return false ;
97+ }
98+
7999 /**
80100 * Checks if we are declaring a prop
81101 * @param {ASTNode } node The AST node being checked.
@@ -719,13 +739,33 @@ module.exports = Components.detect(function(context, components, utils) {
719739 }
720740 }
721741
742+ /**
743+ * @param {ASTNode } node We expect either an ArrowFunctionExpression,
744+ * FunctionDeclaration, or FunctionExpression
745+ */
746+ function markAnnotatedFunctionArgumentsAsDeclared ( node ) {
747+ if ( ! node . params || ! node . params . length || ! isAnnotatedFunctionPropsDeclaration ( node ) ) {
748+ return ;
749+ }
750+ markPropTypesAsDeclared ( node , resolveTypeAnnotation ( node . params [ 0 ] ) ) ;
751+ }
752+
753+ /**
754+ * @param {ASTNode } node We expect either an ArrowFunctionExpression,
755+ * FunctionDeclaration, or FunctionExpression
756+ */
757+ function handleStatelessComponent ( node ) {
758+ markDestructuredFunctionArgumentsAsUsed ( node ) ;
759+ markAnnotatedFunctionArgumentsAsDeclared ( node ) ;
760+ }
761+
722762 // --------------------------------------------------------------------------
723763 // Public
724764 // --------------------------------------------------------------------------
725765
726766 return {
727767 ClassProperty : function ( node ) {
728- if ( isAnnotatedPropsDeclaration ( node ) ) {
768+ if ( isAnnotatedClassPropsDeclaration ( node ) ) {
729769 markPropTypesAsDeclared ( node , resolveTypeAnnotation ( node ) ) ;
730770 } else if ( isPropTypesDeclaration ( node ) ) {
731771 markPropTypesAsDeclared ( node , node . value ) ;
@@ -745,11 +785,11 @@ module.exports = Components.detect(function(context, components, utils) {
745785 markPropTypesAsUsed ( node ) ;
746786 } ,
747787
748- FunctionDeclaration : markDestructuredFunctionArgumentsAsUsed ,
788+ FunctionDeclaration : handleStatelessComponent ,
749789
750- ArrowFunctionExpression : markDestructuredFunctionArgumentsAsUsed ,
790+ ArrowFunctionExpression : handleStatelessComponent ,
751791
752- FunctionExpression : markDestructuredFunctionArgumentsAsUsed ,
792+ FunctionExpression : handleStatelessComponent ,
753793
754794 MemberExpression : function ( node ) {
755795 var type ;
0 commit comments