@@ -41,6 +41,9 @@ module.exports = {
41
41
const configuration = context . options [ 0 ] || { } ;
42
42
const allowRequiredDefaults = configuration . allowRequiredDefaults || false ;
43
43
const propWrapperFunctions = new Set ( context . settings . propWrapperFunctions || [ ] ) ;
44
+ // Used to track the type annotations in scope.
45
+ // Necessary because babel's scopes do not track type annotations.
46
+ let stack = null ;
44
47
45
48
/**
46
49
* Try to resolve the node passed in to a variable in the current scope. If the node passed in is not
@@ -62,6 +65,22 @@ module.exports = {
62
65
return node ;
63
66
}
64
67
68
+ /**
69
+ * Helper for accessing the current scope in the stack.
70
+ * @param {string } key The name of the identifier to access. If omitted, returns the full scope.
71
+ * @param {ASTNode } value If provided sets the new value for the identifier.
72
+ * @returns {Object|ASTNode } Either the whole scope or the ASTNode associated with the given identifier.
73
+ */
74
+ function typeScope ( key , value ) {
75
+ if ( arguments . length === 0 ) {
76
+ return stack [ stack . length - 1 ] ;
77
+ } else if ( arguments . length === 1 ) {
78
+ return stack [ stack . length - 1 ] [ key ] ;
79
+ }
80
+ stack [ stack . length - 1 ] [ key ] = value ;
81
+ return value ;
82
+ }
83
+
65
84
/**
66
85
* Tries to find the definition of a GenericTypeAnnotation in the current scope.
67
86
* @param {ASTNode } node The node GenericTypeAnnotation node to resolve.
@@ -72,7 +91,7 @@ module.exports = {
72
91
return null ;
73
92
}
74
93
75
- return variableUtil . findVariableByName ( context , node . id . name ) ;
94
+ return variableUtil . findVariableByName ( context , node . id . name ) || typeScope ( node . id . name ) ;
76
95
}
77
96
78
97
function resolveUnionTypeAnnotation ( node ) {
@@ -551,12 +570,29 @@ module.exports = {
551
570
} ) ;
552
571
} ,
553
572
573
+ TypeAlias : function ( node ) {
574
+ typeScope ( node . id . name , node . right ) ;
575
+ } ,
576
+
577
+ Program : function ( ) {
578
+ stack = [ { } ] ;
579
+ } ,
580
+
581
+ BlockStatement : function ( ) {
582
+ stack . push ( Object . create ( typeScope ( ) ) ) ;
583
+ } ,
584
+
585
+ 'BlockStatement:exit' : function ( ) {
586
+ stack . pop ( ) ;
587
+ } ,
588
+
554
589
// Check for type annotations in stateless components
555
590
FunctionDeclaration : handleStatelessComponent ,
556
591
ArrowFunctionExpression : handleStatelessComponent ,
557
592
FunctionExpression : handleStatelessComponent ,
558
593
559
594
'Program:exit' : function ( ) {
595
+ stack = null ;
560
596
const list = components . list ( ) ;
561
597
562
598
for ( const component in list ) {
0 commit comments