@@ -28,10 +28,14 @@ export class BabelAstFactory implements AstFactory<
2828 t . Expression | t . SpreadElement ,
2929 t . TSType
3030> {
31+ private readonly typesEnabled : boolean ;
32+
3133 constructor (
3234 /** The absolute path to the source file being compiled. */
33- private sourceUrl : string ,
34- ) { }
35+ private sourcePath : string ,
36+ ) {
37+ this . typesEnabled = sourcePath . endsWith ( '.ts' ) || sourcePath . endsWith ( '.mts' ) ;
38+ }
3539
3640 attachComments ( statement : t . Statement | t . Expression , leadingComments : LeadingComment [ ] ) : void {
3741 // We must process the comments in reverse because `t.addComment()` will add new ones in front.
@@ -112,7 +116,7 @@ export class BabelAstFactory implements AstFactory<
112116 assert ( body , t . isBlockStatement , 'a block' ) ;
113117 return t . functionDeclaration (
114118 t . identifier ( functionName ) ,
115- parameters . map ( ( param ) => identifierWithType ( param . name , param . type ) ) ,
119+ parameters . map ( ( param ) => this . identifierWithType ( param . name , param . type ) ) ,
116120 body ,
117121 ) ;
118122 }
@@ -125,7 +129,7 @@ export class BabelAstFactory implements AstFactory<
125129 assert ( body , t . isBlockStatement , 'a block' ) ;
126130 }
127131 return t . arrowFunctionExpression (
128- parameters . map ( ( param ) => identifierWithType ( param . name , param . type ) ) ,
132+ parameters . map ( ( param ) => this . identifierWithType ( param . name , param . type ) ) ,
129133 body ,
130134 ) ;
131135 }
@@ -139,7 +143,7 @@ export class BabelAstFactory implements AstFactory<
139143 const name = functionName !== null ? t . identifier ( functionName ) : null ;
140144 return t . functionExpression (
141145 name ,
142- parameters . map ( ( param ) => identifierWithType ( param . name , param . type ) ) ,
146+ parameters . map ( ( param ) => this . identifierWithType ( param . name , param . type ) ) ,
143147 body ,
144148 ) ;
145149 }
@@ -234,7 +238,7 @@ export class BabelAstFactory implements AstFactory<
234238 type : t . TSType | null ,
235239 ) : t . Statement {
236240 return t . variableDeclaration ( variableType , [
237- t . variableDeclarator ( identifierWithType ( variableName , type ) , initializer ) ,
241+ t . variableDeclarator ( this . identifierWithType ( variableName , type ) , initializer ) ,
238242 ] ) ;
239243 }
240244
@@ -253,7 +257,7 @@ export class BabelAstFactory implements AstFactory<
253257 // Add in the filename so that we can map to external template files.
254258 // Note that Babel gets confused if you specify a filename when it is the original source
255259 // file. This happens when the template is inline, in which case just use `undefined`.
256- filename : sourceMapRange . url !== this . sourceUrl ? sourceMapRange . url : undefined ,
260+ filename : sourceMapRange . url !== this . sourcePath ? sourceMapRange . url : undefined ,
257261 start : {
258262 line : sourceMapRange . start . line + 1 , // lines are 1-based in Babel.
259263 column : sourceMapRange . start . column ,
@@ -301,7 +305,7 @@ export class BabelAstFactory implements AstFactory<
301305 }
302306
303307 createMapType ( valueType : t . TSType ) : t . TSType {
304- const keySignature = identifierWithType ( 'key' , this . createBuiltInType ( 'string' ) ) ;
308+ const keySignature = this . identifierWithType ( 'key' , this . createBuiltInType ( 'string' ) ) ;
305309 return t . tsTypeLiteral ( [ t . tsIndexSignature ( [ keySignature ] , t . tsTypeAnnotation ( valueType ) ) ] ) ;
306310 }
307311
@@ -311,6 +315,16 @@ export class BabelAstFactory implements AstFactory<
311315 }
312316 throw new Error ( 'Attempting to transplant a type node from a non-Babel AST: ' + type ) ;
313317 }
318+
319+ private identifierWithType ( name : string , type : t . TSType | null ) : t . Identifier {
320+ const node = t . identifier ( name ) ;
321+
322+ if ( this . typesEnabled && type != null ) {
323+ node . typeAnnotation = t . tsTypeAnnotation ( type ) ;
324+ }
325+
326+ return node ;
327+ }
314328}
315329
316330function getEntityTypeFromExpression ( expression : t . Expression ) : t . Identifier | t . TSQualifiedName {
@@ -333,16 +347,6 @@ function getEntityTypeFromExpression(expression: t.Expression): t.Identifier | t
333347 throw new Error ( `Unsupported expression for type reference: ${ expression . type } ` ) ;
334348}
335349
336- function identifierWithType ( name : string , type : t . TSType | null ) : t . Identifier {
337- const node = t . identifier ( name ) ;
338-
339- if ( type !== null ) {
340- node . typeAnnotation = t . tsTypeAnnotation ( type ) ;
341- }
342-
343- return node ;
344- }
345-
346350function isLExpression ( expr : t . Expression ) : expr is Extract < t . LVal , t . Expression > {
347351 // Some LVal types are not expressions, which prevents us from using `t.isLVal()`
348352 // directly with `assert()`.
0 commit comments