@@ -42,6 +42,9 @@ module.exports = {
42
42
const propWrapperFunctions = new Set ( context . settings . propWrapperFunctions || [ ] ) ;
43
43
const configuration = context . options [ 0 ] || { } ;
44
44
const forbidDefaultForRequired = configuration . forbidDefaultForRequired || false ;
45
+ // Used to track the type annotations in scope.
46
+ // Necessary because babel's scopes do not track type annotations.
47
+ let stack = null ;
45
48
46
49
/**
47
50
* Try to resolve the node passed in to a variable in the current scope. If the node passed in is not
@@ -64,6 +67,22 @@ module.exports = {
64
67
return node ;
65
68
}
66
69
70
+ /**
71
+ * Helper for accessing the current scope in the stack.
72
+ * @param {string } key The name of the identifier to access. If omitted, returns the full scope.
73
+ * @param {ASTNode } value If provided sets the new value for the identifier.
74
+ * @returns {Object|ASTNode } Either the whole scope or the ASTNode associated with the given identifier.
75
+ */
76
+ function typeScope ( key , value ) {
77
+ if ( arguments . length === 0 ) {
78
+ return stack [ stack . length - 1 ] ;
79
+ } else if ( arguments . length === 1 ) {
80
+ return stack [ stack . length - 1 ] [ key ] ;
81
+ }
82
+ stack [ stack . length - 1 ] [ key ] = value ;
83
+ return value ;
84
+ }
85
+
67
86
/**
68
87
* Tries to find the definition of a GenericTypeAnnotation in the current scope.
69
88
* @param {ASTNode } node The node GenericTypeAnnotation node to resolve.
@@ -74,7 +93,7 @@ module.exports = {
74
93
return null ;
75
94
}
76
95
77
- return variableUtil . findVariableByName ( context , node . id . name ) ;
96
+ return variableUtil . findVariableByName ( context , node . id . name ) || typeScope ( node . id . name ) ;
78
97
}
79
98
80
99
function resolveUnionTypeAnnotation ( node ) {
@@ -563,6 +582,22 @@ module.exports = {
563
582
} ) ;
564
583
} ,
565
584
585
+ TypeAlias : function ( node ) {
586
+ typeScope ( node . id . name , node . right ) ;
587
+ } ,
588
+
589
+ Program : function ( ) {
590
+ stack = [ { } ] ;
591
+ } ,
592
+
593
+ BlockStatement : function ( ) {
594
+ stack . push ( Object . create ( typeScope ( ) ) ) ;
595
+ } ,
596
+
597
+ 'BlockStatement:exit' : function ( ) {
598
+ stack . pop ( ) ;
599
+ } ,
600
+
566
601
// e.g.:
567
602
// type HelloProps = {
568
603
// foo?: string
@@ -588,6 +623,7 @@ module.exports = {
588
623
FunctionExpression : handleStatelessComponent ,
589
624
590
625
'Program:exit' : function ( ) {
626
+ stack = null ;
591
627
const list = components . list ( ) ;
592
628
593
629
for ( const component in list ) {
0 commit comments