1
+ import type * as Babel from '@babel/core' ;
2
+ import type { types as t } from '@babel/core' ;
3
+ import type { NodePath } from '@babel/core' ;
4
+ import { CallIdentifierExpression , CallStatementPath } from './babel-type-helpers' ;
5
+
6
+ export interface Options {
7
+ module : boolean | undefined ;
8
+ global : string | undefined ;
9
+ assertPredicateIndex : number | undefined ;
10
+ isDebug : boolean ;
11
+ }
12
+
13
+ interface MacroExpressionOpts {
14
+ validate ?: ( expression : CallIdentifierExpression , args : t . CallExpression [ 'arguments' ] ) => void ;
15
+ buildConsoleAPI ?: (
16
+ expression : CallIdentifierExpression ,
17
+ args : t . CallExpression [ 'arguments' ]
18
+ ) => t . CallExpression ;
19
+ consoleAPI ?: t . Identifier ;
20
+ predicate ?: (
21
+ expression : CallIdentifierExpression ,
22
+ args : t . CallExpression [ 'arguments' ]
23
+ ) => t . CallExpression [ 'arguments' ] [ number ] | undefined ;
24
+ }
25
+
1
26
export default class Builder {
2
- constructor ( t , options ) {
3
- this . t = t ;
27
+ private module : boolean | undefined ;
28
+ private global : string | undefined ;
29
+ private assertPredicateIndex : number | undefined ;
30
+ private isDebug : boolean ;
31
+
32
+ private expressions : [ CallStatementPath , ( debugIdentifier : t . Expression ) => t . Expression ] [ ] = [ ] ;
33
+
34
+ constructor (
35
+ readonly t : typeof Babel . types ,
36
+ options : Options
37
+ ) {
4
38
this . module = options . module ;
5
39
this . global = options . global ;
6
40
this . assertPredicateIndex = options . assertPredicateIndex ;
7
41
this . isDebug = options . isDebug ;
8
- this . expressions = [ ] ;
9
42
}
10
43
11
44
/**
@@ -25,11 +58,12 @@ export default class Builder {
25
58
*
26
59
* ($DEBUG && $GLOBAL_NS.assert($PREDICATE, $MESSAGE));
27
60
*/
28
- assert ( path ) {
29
- let predicate ;
30
- if ( this . assertPredicateIndex !== undefined ) {
61
+ assert ( path : CallStatementPath ) {
62
+ let predicate : MacroExpressionOpts [ 'predicate' ] ;
63
+ const index = this . assertPredicateIndex ;
64
+ if ( index !== undefined ) {
31
65
predicate = ( expression , args ) => {
32
- return args [ this . assertPredicateIndex ] ;
66
+ return args [ index ] ;
33
67
} ;
34
68
}
35
69
@@ -55,7 +89,7 @@ export default class Builder {
55
89
*
56
90
* ($DEBUG && $GLOBAL_NS.warn($MESSAGE));
57
91
*/
58
- warn ( path ) {
92
+ warn ( path : CallStatementPath ) {
59
93
this . _createMacroExpression ( path ) ;
60
94
}
61
95
@@ -76,13 +110,11 @@ export default class Builder {
76
110
*
77
111
* ($DEBUG && $GLOBAL_NS.log($MESSAGE));
78
112
*/
79
- log ( path ) {
113
+ log ( path : CallStatementPath ) {
80
114
this . _createMacroExpression ( path ) ;
81
115
}
82
116
83
- _createMacroExpression ( path , _options ) {
84
- let options = _options || { } ;
85
-
117
+ _createMacroExpression ( path : CallStatementPath , options : MacroExpressionOpts = { } ) {
86
118
let t = this . t ;
87
119
let expression = path . node . expression ;
88
120
let callee = expression . callee ;
@@ -103,10 +135,13 @@ export default class Builder {
103
135
callExpression = this . _createConsoleAPI ( options . consoleAPI || callee , args ) ;
104
136
}
105
137
106
- let prefixedIdentifiers = [ ] ;
138
+ let prefixedIdentifiers : t . Expression [ ] = [ ] ;
107
139
108
140
if ( options . predicate ) {
109
141
let predicate = options . predicate ( expression , args ) || t . identifier ( 'false' ) ;
142
+ if ( ! this . t . isExpression ( predicate ) ) {
143
+ throw new Error ( `bug: this doesn't support ${ predicate . type } ` ) ;
144
+ }
110
145
let negatedPredicate = t . unaryExpression ( '!' , t . parenthesizedExpression ( predicate ) ) ;
111
146
prefixedIdentifiers . push ( negatedPredicate ) ;
112
147
}
@@ -142,7 +177,7 @@ export default class Builder {
142
177
*
143
178
* ($DEBUG && $PREDICATE && $GLOBAL_NS.deprecate($MESSAGE, $PREDICATE, { $ID, $URL, $UNTIL }));
144
179
*/
145
- deprecate ( path ) {
180
+ deprecate ( path : CallStatementPath ) {
146
181
this . _createMacroExpression ( path , {
147
182
predicate : ( expression , args ) => args [ 1 ] ,
148
183
@@ -157,8 +192,14 @@ export default class Builder {
157
192
158
193
if (
159
194
meta &&
195
+ this . t . isObjectExpression ( meta ) &&
160
196
meta . properties &&
161
- ! meta . properties . some ( ( prop ) => prop . key . name === 'id' || prop . key . value === 'id' )
197
+ ! meta . properties . some (
198
+ ( prop ) =>
199
+ this . t . isObjectProperty ( prop ) &&
200
+ ( ( this . t . isIdentifier ( prop . key ) && prop . key . name === 'id' ) ||
201
+ ( this . t . isStringLiteral ( prop . key ) && prop . key . value === 'id' ) )
202
+ )
162
203
) {
163
204
throw new ReferenceError ( `deprecate's meta information requires an "id" field.` ) ;
164
205
}
@@ -180,24 +221,27 @@ export default class Builder {
180
221
}
181
222
}
182
223
183
- _getIdentifiers ( args ) {
184
- return args . filter ( ( arg ) => this . t . isIdentifier ( arg ) ) ;
185
- }
186
-
187
- _createGlobalExternalHelper ( identifier , args , ns ) {
224
+ _createGlobalExternalHelper (
225
+ identifier : t . Identifier ,
226
+ args : t . CallExpression [ 'arguments' ] ,
227
+ ns : string
228
+ ) {
188
229
let t = this . t ;
189
230
return t . callExpression ( t . memberExpression ( t . identifier ( ns ) , identifier ) , args ) ;
190
231
}
191
232
192
- _createConsoleAPI ( identifier , args ) {
233
+ _createConsoleAPI ( identifier : t . Identifier , args : t . CallExpression [ 'arguments' ] ) {
193
234
let t = this . t ;
194
235
return t . callExpression ( t . memberExpression ( t . identifier ( 'console' ) , identifier ) , args ) ;
195
236
}
196
237
197
- _buildLogicalExpressions ( identifiers , callExpression ) {
238
+ _buildLogicalExpressions (
239
+ identifiers : t . Expression [ ] ,
240
+ callExpression : t . Expression
241
+ ) : ( debugIdentifier : t . Expression ) => t . Expression {
198
242
let t = this . t ;
199
243
200
- return ( debugIdentifier ) => {
244
+ return ( debugIdentifier : t . Expression ) => {
201
245
identifiers . unshift ( debugIdentifier ) ;
202
246
identifiers . push ( callExpression ) ;
203
247
let logicalExpressions ;
@@ -212,7 +256,7 @@ export default class Builder {
212
256
}
213
257
}
214
258
215
- return logicalExpressions ;
259
+ return logicalExpressions ! ;
216
260
} ;
217
261
}
218
- } ;
262
+ }
0 commit comments