| 1 |
import gensync, { type Handler } from "gensync"; |
| 2 |
|
| 3 |
import loadConfig from "./config"; |
| 4 |
import type { InputOptions, ResolvedConfig } from "./config"; |
| 5 |
import { run } from "./transformation"; |
| 6 |
import type * as t from "@babel/types"; |
| 7 |
|
| 8 |
import { beginHiddenCallStack } from "./errors/rewrite-stack-trace"; |
| 9 |
|
| 10 |
import type { FileResult, FileResultCallback } from "./transformation"; |
| 11 |
type AstRoot = t.File | t.Program; |
| 12 |
|
| 13 |
type TransformFromAst = { |
| 14 |
(ast: AstRoot, code: string, callback: FileResultCallback): void; |
| 15 |
( |
| 16 |
ast: AstRoot, |
| 17 |
code: string, |
| 18 |
opts: InputOptions | undefined | null, |
| 19 |
callback: FileResultCallback, |
| 20 |
): void; |
| 21 |
(ast: AstRoot, code: string, opts?: InputOptions | null): FileResult | null; |
| 22 |
}; |
| 23 |
|
| 24 |
const transformFromAstRunner = gensync(function* ( |
| 25 |
ast: AstRoot, |
| 26 |
code: string, |
| 27 |
opts: InputOptions | undefined | null, |
| 28 |
): Handler<FileResult | null> { |
| 29 |
const config: ResolvedConfig | null = yield* loadConfig(opts); |
| 30 |
if (config === null) return null; |
| 31 |
|
| 32 |
if (!ast) throw new Error("No AST given"); |
| 33 |
|
| 34 |
return yield* run(config, code, ast); |
| 35 |
}); |
| 36 |
|
| 37 |
export const transformFromAst: TransformFromAst = function transformFromAst( |
| 38 |
ast, |
| 39 |
code, |
| 40 |
optsOrCallback?: InputOptions | null | undefined | FileResultCallback, |
| 41 |
maybeCallback?: FileResultCallback, |
| 42 |
) { |
| 43 |
let opts: InputOptions | undefined | null; |
| 44 |
let callback: FileResultCallback | undefined; |
| 45 |
if (typeof optsOrCallback === "function") { |
| 46 |
callback = optsOrCallback; |
| 47 |
opts = undefined; |
| 48 |
} else { |
| 49 |
opts = optsOrCallback; |
| 50 |
callback = maybeCallback; |
| 51 |
} |
| 52 |
|
| 53 |
if (callback === undefined) { |
| 54 |
if (process.env.BABEL_8_BREAKING) { |
| 55 |
throw new Error( |
| 56 |
"Starting from Babel 8.0.0, the 'transformFromAst' function expects a callback. If you need to call it synchronously, please use 'transformFromAstSync'.", |
| 57 |
); |
| 58 |
} else { |
| 59 |
// console.warn( |
| 60 |
// "Starting from Babel 8.0.0, the 'transformFromAst' function will expect a callback. If you need to call it synchronously, please use 'transformFromAstSync'.", |
| 61 |
// ); |
| 62 |
return beginHiddenCallStack(transformFromAstRunner.sync)(ast, code, opts); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
beginHiddenCallStack(transformFromAstRunner.errback)( |
| 67 |
ast, |
| 68 |
code, |
| 69 |
opts, |
| 70 |
callback, |
| 71 |
); |
| 72 |
}; |
| 73 |
|
| 74 |
export function transformFromAstSync( |
| 75 |
...args: Parameters<typeof transformFromAstRunner.sync> |
| 76 |
) { |
| 77 |
return beginHiddenCallStack(transformFromAstRunner.sync)(...args); |
| 78 |
} |
| 79 |
|
| 80 |
export function transformFromAstAsync( |
| 81 |
...args: Parameters<typeof transformFromAstRunner.async> |
| 82 |
) { |
| 83 |
return beginHiddenCallStack(transformFromAstRunner.async)(...args); |
| 84 |
} |