@@ -61,7 +61,7 @@ module.exports = Components.detect(function(context, components, utils) {
61
61
* @param {ASTNode } node The AST node being checked.
62
62
* @returns {Boolean } True if the node is a type annotated props declaration, false if not.
63
63
*/
64
- function isAnnotatedPropsDeclaration ( node ) {
64
+ function isAnnotatedClassPropsDeclaration ( node ) {
65
65
if ( node && node . type === 'ClassProperty' ) {
66
66
var tokens = context . getFirstTokens ( node , 2 ) ;
67
67
if (
@@ -76,6 +76,26 @@ module.exports = Components.detect(function(context, components, utils) {
76
76
return false ;
77
77
}
78
78
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
+
79
99
/**
80
100
* Checks if we are declaring a prop
81
101
* @param {ASTNode } node The AST node being checked.
@@ -719,13 +739,33 @@ module.exports = Components.detect(function(context, components, utils) {
719
739
}
720
740
}
721
741
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
+
722
762
// --------------------------------------------------------------------------
723
763
// Public
724
764
// --------------------------------------------------------------------------
725
765
726
766
return {
727
767
ClassProperty : function ( node ) {
728
- if ( isAnnotatedPropsDeclaration ( node ) ) {
768
+ if ( isAnnotatedClassPropsDeclaration ( node ) ) {
729
769
markPropTypesAsDeclared ( node , resolveTypeAnnotation ( node ) ) ;
730
770
} else if ( isPropTypesDeclaration ( node ) ) {
731
771
markPropTypesAsDeclared ( node , node . value ) ;
@@ -745,11 +785,11 @@ module.exports = Components.detect(function(context, components, utils) {
745
785
markPropTypesAsUsed ( node ) ;
746
786
} ,
747
787
748
- FunctionDeclaration : markDestructuredFunctionArgumentsAsUsed ,
788
+ FunctionDeclaration : handleStatelessComponent ,
749
789
750
- ArrowFunctionExpression : markDestructuredFunctionArgumentsAsUsed ,
790
+ ArrowFunctionExpression : handleStatelessComponent ,
751
791
752
- FunctionExpression : markDestructuredFunctionArgumentsAsUsed ,
792
+ FunctionExpression : handleStatelessComponent ,
753
793
754
794
MemberExpression : function ( node ) {
755
795
var type ;
0 commit comments