@@ -31,6 +31,16 @@ const STRIP_KEYS = new Set([
3131 "directive"
3232] ) ;
3333
34+ /** Which Babel AST shape to emit. Babel 8 keeps `typeArguments`,
35+ * `ImportExpression`, Identifier type-parameter names and `TSEnumBody`;
36+ * Babel 7 wants the older forms. Set for the length of one `toBabelFile`. */
37+ let babel8 = false ;
38+
39+ /** The key Babel expects for a node's type arguments. */
40+ function typeArgsKey ( ) : string {
41+ return babel8 ? "typeArguments" : "typeParameters" ;
42+ }
43+
3444function isNode ( value : unknown ) : value is EsNode {
3545 return (
3646 typeof value === "object" &&
@@ -57,19 +67,32 @@ function convertValue(value: unknown): unknown {
5767}
5868
5969/** Shallow-map every kept key, renaming typescript-eslint's `typeArguments`
60- * to Babel 7's `typeParameters` along the way. */
70+ * to Babel 7's `typeParameters` along the way (Babel 8 keeps the name) . */
6171function convertGeneric ( node : EsNode , overrideType ?: string ) : EsNode {
6272 const out : Record < string , unknown > = { type : overrideType ?? node . type } ;
6373 copyLoc ( node , out ) ;
6474 for ( const key of Object . keys ( node ) ) {
6575 if ( STRIP_KEYS . has ( key ) ) continue ;
6676 const outKey =
67- key === "typeArguments" && node . typeParameters === undefined ? "typeParameters" : key ;
77+ ! babel8 && key === "typeArguments" && node . typeParameters === undefined
78+ ? "typeParameters"
79+ : key ;
6880 out [ outKey ] = convertValue ( node [ key ] ) ;
6981 }
7082 return out as EsNode ;
7183}
7284
85+ /** Convert a node whose type arguments typescript-eslint spells
86+ * `typeParameters`, moving them to the key the target Babel expects. */
87+ function convertTypeArgsHolder ( node : EsNode , type : string ) : EsNode {
88+ const out = convertGeneric ( node , type ) as Record < string , unknown > ;
89+ const args = out . typeParameters ?? out . typeArguments ;
90+ delete out . typeParameters ;
91+ delete out . typeArguments ;
92+ if ( args !== undefined ) out [ typeArgsKey ( ) ] = args ;
93+ return out as EsNode ;
94+ }
95+
7396function convertLiteral ( node : EsNode ) : EsNode {
7497 const out : Record < string , unknown > = { } ;
7598 copyLoc ( node , out ) ;
@@ -162,7 +185,7 @@ function convertChainLink(node: EsNode): EsNode {
162185 } ;
163186 copyLoc ( node , out ) ;
164187 if ( node . typeParameters !== undefined || node . typeArguments !== undefined ) {
165- out . typeParameters = convertValue ( node . typeParameters ?? node . typeArguments ) ;
188+ out [ typeArgsKey ( ) ] = convertValue ( node . typeParameters ?? node . typeArguments ) ;
166189 }
167190 return out as EsNode ;
168191 }
@@ -245,6 +268,9 @@ function convertNode(node: EsNode): EsNode {
245268 } ;
246269 copyLoc ( node , out ) ;
247270 convertFunctionParts ( fn , out ) ;
271+ if ( node . typeParameters != null && out . typeParameters === undefined ) {
272+ out . typeParameters = convertValue ( node . typeParameters ) ;
273+ }
248274 if ( node . accessibility !== undefined ) out . accessibility = node . accessibility ;
249275 if ( node . override !== undefined ) out . override = node . override ;
250276 if ( node . optional !== undefined ) out . optional = node . optional ;
@@ -287,10 +313,22 @@ function convertNode(node: EsNode): EsNode {
287313 return convertChainLink ( node . expression as EsNode ) ;
288314
289315 case "ImportExpression" : {
316+ // `@tsrx/core` puts the options object in `arguments[0]`; acorn uses `options`.
317+ const options = node . options ?? ( node . arguments as EsNode [ ] | undefined ) ?. [ 0 ] ;
318+ if ( babel8 ) {
319+ const out : Record < string , unknown > = {
320+ type : "ImportExpression" ,
321+ source : convertValue ( node . source ) ,
322+ options : options != null ? convertValue ( options ) : null
323+ } ;
324+ if ( node . phase != null ) out . phase = node . phase ;
325+ copyLoc ( node , out ) ;
326+ return out as EsNode ;
327+ }
290328 const importCallee : Record < string , unknown > = { type : "Import" } ;
291329 copyLoc ( node , importCallee ) ;
292330 const args : unknown [ ] = [ convertValue ( node . source ) ] ;
293- if ( node . options != null ) args . push ( convertValue ( node . options ) ) ;
331+ if ( options != null ) args . push ( convertValue ( options ) ) ;
294332 const out : Record < string , unknown > = {
295333 type : "CallExpression" ,
296334 callee : importCallee ,
@@ -308,13 +346,72 @@ function convertNode(node: EsNode): EsNode {
308346 }
309347
310348 case "TSTypeParameter" : {
311- const out = convertGeneric ( node ) ;
312- if ( isNode ( out . name ) ) out . name = ( out . name as unknown as { name : string } ) . name ;
313- return out ;
349+ const out = convertGeneric ( node ) as Record < string , unknown > ;
350+ if ( babel8 ) {
351+ if ( typeof out . name === "string" ) {
352+ const id : Record < string , unknown > = { type : "Identifier" , name : out . name } ;
353+ copyLoc ( node , id ) ;
354+ out . name = id ;
355+ }
356+ } else if ( isNode ( out . name ) ) out . name = ( out . name as unknown as { name : string } ) . name ;
357+ return out as EsNode ;
314358 }
315359
316360 case "TSInterfaceHeritage" :
317- return convertGeneric ( node , "TSExpressionWithTypeArguments" ) ;
361+ return convertTypeArgsHolder (
362+ node ,
363+ babel8 ? "TSInterfaceHeritage" : "TSExpressionWithTypeArguments"
364+ ) ;
365+
366+ // `implements` entries arrive in the pre-split typescript-eslint shape.
367+ case "TSExpressionWithTypeArguments" :
368+ case "TSClassImplements" :
369+ return convertTypeArgsHolder (
370+ node ,
371+ babel8 ? "TSClassImplements" : "TSExpressionWithTypeArguments"
372+ ) ;
373+
374+ case "ClassDeclaration" :
375+ case "ClassExpression" : {
376+ const out = convertGeneric ( node ) as Record < string , unknown > ;
377+ const superArgs = out . superTypeParameters ?? out . superTypeArguments ;
378+ delete out . superTypeParameters ;
379+ delete out . superTypeArguments ;
380+ if ( superArgs !== undefined ) {
381+ out [ babel8 ? "superTypeArguments" : "superTypeParameters" ] = superArgs ;
382+ }
383+ return out as EsNode ;
384+ }
385+
386+ case "TSInterfaceDeclaration" : {
387+ const out = convertGeneric ( node ) as Record < string , unknown > ;
388+ // Interface heritage can arrive in the class-implements shape.
389+ if ( babel8 && Array . isArray ( out . extends ) ) {
390+ for ( const heritage of out . extends as Record < string , unknown > [ ] ) {
391+ heritage . type = "TSInterfaceHeritage" ;
392+ }
393+ }
394+ return out as EsNode ;
395+ }
396+
397+ case "TSImportType" : {
398+ const out = convertGeneric ( node ) as Record < string , unknown > ;
399+ if ( babel8 && out . argument !== undefined ) {
400+ out . source = out . argument ;
401+ delete out . argument ;
402+ }
403+ return out as EsNode ;
404+ }
405+
406+ case "TSEnumDeclaration" : {
407+ const out = convertGeneric ( node ) as Record < string , unknown > ;
408+ if ( babel8 && out . body === undefined ) {
409+ const body : Record < string , unknown > = { type : "TSEnumBody" , members : out . members } ;
410+ delete out . members ;
411+ out . body = body ;
412+ }
413+ return out as EsNode ;
414+ }
318415
319416 case "TSAbstractMethodDefinition" :
320417 case "TSAbstractPropertyDefinition" :
@@ -337,13 +434,18 @@ function convertNode(node: EsNode): EsNode {
337434 }
338435}
339436
340- export function toBabelFile ( program : EsNode ) : t . File {
341- const file : Record < string , unknown > = {
342- type : "File" ,
343- program : convertNode ( program ) ,
344- comments : [ ] ,
345- tokens : [ ]
346- } ;
347- copyLoc ( program , file ) ;
348- return file as unknown as t . File ;
437+ export function toBabelFile ( program : EsNode , babelMajor = 7 ) : t . File {
438+ babel8 = babelMajor >= 8 ;
439+ try {
440+ const file : Record < string , unknown > = {
441+ type : "File" ,
442+ program : convertNode ( program ) ,
443+ comments : [ ] ,
444+ tokens : [ ]
445+ } ;
446+ copyLoc ( program , file ) ;
447+ return file as unknown as t . File ;
448+ } finally {
449+ babel8 = false ;
450+ }
349451}
0 commit comments