@@ -30,7 +30,7 @@ const generator = /** @type {typeof import('@babel/generator')['default']} */ (
30
30
/**
31
31
* Options for {@link generateCode} (no source map generated)
32
32
*
33
- * @typedef GenerateAstOptionsWithoutSourceMap
33
+ * @typedef GenerateAstOptions
34
34
* @property {string } [source]
35
35
* @property {undefined } [sourceUrl] - This should be undefined or otherwise not provided
36
36
* @internal
@@ -40,62 +40,70 @@ const generator = /** @type {typeof import('@babel/generator')['default']} */ (
40
40
* The result of {@link generate}; depends on whether a `sourceUrl` was
41
41
* provided to the options.
42
42
*
43
- * @template {string|undefined} [SourceUrl=undefined]
44
- * @typedef {{code: string, map: SourceUrl extends string ? string : never} } TransformedResult
43
+ * @typedef {{code: string, map: undefined} } TransformedResult
45
44
* @internal
46
45
*/
47
46
48
47
/**
49
- * Generates new code from a Babel AST; returns code and source map
48
+ * The result of {@link generate}; depends on whether a `sourceUrl` was
49
+ * provided to the options.
50
50
*
51
- * @callback GenerateAstWithSourceMap
51
+ * @typedef {TransformedResult & { map: NonNullable<import('@babel/generator').GeneratorResult['map']>} } TransformedResultWithSourceMap
52
+ * @internal
53
+ */
54
+
55
+ /**
56
+ * Generates new code from a Babel AST; returns code and source map
57
+ *@overload
52
58
* @param {import('@babel/types').File } ast - Babel "File" AST
53
59
* @param {GenerateAstOptionsWithSourceMap } options - Options for the transform
54
- * @returns {TransformedResult<string> }
60
+ * @returns {TransformedResultWithSourceMap }
55
61
* @internal
56
62
*/
57
63
58
64
/**
59
65
* Generates new code from a Babel AST; returns code only
60
- *
61
- * @callback GenerateAstWithoutSourceMap
66
+ *@overload
62
67
* @param {import('@babel/types').File } ast - Babel "File" AST
63
- * @param {GenerateAstOptionsWithoutSourceMap } [options] - Options for the transform
64
- * @returns {TransformedResult<undefined> }
68
+ * @param {GenerateAstOptions } [options] - Options for the transform
69
+ * @returns {TransformedResult }
65
70
* @internal
66
71
*/
67
- export const generate =
68
- /** @type {GenerateAstWithSourceMap & GenerateAstWithoutSourceMap } */ (
69
- ( ast , options ) => {
70
- // TODO Use options?.sourceUrl when resolved:
71
- // https://github.com/Agoric/agoric-sdk/issues/8671
72
- const sourceUrl = options ? options . sourceUrl : undefined ;
73
- const inputSourceMap =
74
- options && 'sourceMap' in options ? options . sourceMap : undefined ;
75
- const source = options ? options . source : undefined ;
76
- const result = generator (
77
- ast ,
78
- {
79
- sourceFileName : sourceUrl ,
80
- sourceMaps : Boolean ( sourceUrl ) ,
81
- // @ts -expect-error Property missing on versioned types
82
- inputSourceMap,
83
- retainLines : true ,
84
- ...( source === undefined
85
- ? { }
86
- : { experimental_preserveFormat : true } ) ,
87
- } ,
88
- source ,
89
- ) ;
90
72
91
- if ( sourceUrl ) {
92
- return {
93
- code : result . code ,
94
- map : result . map ,
95
- } ;
96
- }
97
- return {
98
- code : result . code ,
99
- } ;
100
- }
73
+ /**
74
+ * Generates new code from a Babel AST; returns code only
75
+ *
76
+ * @param {import('@babel/types').File } ast - Babel "File" AST
77
+ * @param {GenerateAstOptions } [options] - Options for the transform
78
+ * @internal
79
+ */
80
+ export const generate = ( ast , options ) => {
81
+ // TODO Use options?.sourceUrl when resolved:
82
+ // https://github.com/Agoric/agoric-sdk/issues/8671
83
+ const sourceUrl = options ? options . sourceUrl : undefined ;
84
+ const inputSourceMap =
85
+ options && 'sourceMap' in options ? options . sourceMap : undefined ;
86
+ const source = options ? options . source : undefined ;
87
+ const result = generator (
88
+ ast ,
89
+ {
90
+ sourceFileName : sourceUrl ,
91
+ sourceMaps : Boolean ( sourceUrl ) ,
92
+ // @ts -expect-error Property missing on versioned types
93
+ inputSourceMap,
94
+ retainLines : true ,
95
+ ...( source === undefined ? { } : { experimental_preserveFormat : true } ) ,
96
+ } ,
97
+ source ,
101
98
) ;
99
+
100
+ if ( sourceUrl ) {
101
+ return {
102
+ code : result . code ,
103
+ map : result . map ,
104
+ } ;
105
+ }
106
+ return {
107
+ code : result . code ,
108
+ } ;
109
+ } ;
0 commit comments