File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,12 @@ var finders = require('./finders'),
5
5
flowDoctrine = require ( '../flow_doctrine' ) ,
6
6
t = require ( 'babel-types' ) ;
7
7
8
+ var constTypeMapping = {
9
+ 'BooleanLiteral' : { type : 'BooleanTypeAnnotation' } ,
10
+ 'NumericLiteral' : { type : 'NumberTypeAnnotation' } ,
11
+ 'StringLiteral' : { type : 'StringTypeAnnotation' }
12
+ } ;
13
+
8
14
/**
9
15
* Infers type tags by using Flow type annotations
10
16
*
@@ -27,6 +33,9 @@ module.exports = function () {
27
33
switch ( n . type ) {
28
34
case 'VariableDeclarator' :
29
35
type = n . id . typeAnnotation ;
36
+ if ( ! type && comment . kind === 'constant' ) {
37
+ type = constTypeMapping [ n . init . type ] ;
38
+ }
30
39
break ;
31
40
case 'ClassProperty' :
32
41
type = n . typeAnnotation ;
Original file line number Diff line number Diff line change @@ -107,5 +107,29 @@ test('inferType', function (t) {
107
107
type : 'NameExpression'
108
108
} ) ;
109
109
110
+ t . deepEqual ( evaluate (
111
+ '/** */' +
112
+ 'const x = 42;'
113
+ ) . type , {
114
+ name : 'number' ,
115
+ type : 'NameExpression'
116
+ } ) ;
117
+
118
+ t . deepEqual ( evaluate (
119
+ '/** */' +
120
+ 'const x = "abc";'
121
+ ) . type , {
122
+ name : 'string' ,
123
+ type : 'NameExpression'
124
+ } ) ;
125
+
126
+ t . deepEqual ( evaluate (
127
+ '/** */' +
128
+ 'const x = true;'
129
+ ) . type , {
130
+ name : 'boolean' ,
131
+ type : 'NameExpression'
132
+ } ) ;
133
+
110
134
t . end ( ) ;
111
135
} ) ;
You can’t perform that action at this time.
0 commit comments