|
| 1 | +// Import Third-party Dependencies |
| 2 | +import type { ESTree } from "meriyah"; |
| 3 | + |
| 4 | +// Import Internal Dependencies |
| 5 | +import { VirtualVariableIdentifier } from "./VirtualVariableIdentifier.ts"; |
| 6 | + |
| 7 | +export interface SplitResult { |
| 8 | + /** |
| 9 | + * A virtual variable name that replaces the target |
| 10 | + */ |
| 11 | + virtualIdentifier: string; |
| 12 | + /** |
| 13 | + * Virtual variable declaration: |
| 14 | + * require: const __virtual_require_0__ = require("xxx") |
| 15 | + * new: const __virtual_new_0__ = new Foo("xxx") |
| 16 | + * Can be walked with standard ESTree walkers. |
| 17 | + */ |
| 18 | + virtualDeclaration: ESTree.VariableDeclaration; |
| 19 | + /** |
| 20 | + * The rebuilt expression with require() replaced by the virtual identifier. |
| 21 | + * For `require("x").spawn("y")`, this would be `__virtual_require_0__.spawn("y")` |
| 22 | + * For `(new Foo()).bar()`, this would be `__virtual_new_0__.bar()` |
| 23 | + * Can be walked with standard ESTree walkers. |
| 24 | + */ |
| 25 | + rebuildExpression: ESTree.Node | null; |
| 26 | +} |
| 27 | + |
| 28 | +export class Inlined { |
| 29 | + static buildSplitResult( |
| 30 | + node: ESTree.Node, |
| 31 | + target: ESTree.Expression, |
| 32 | + identifier: string |
| 33 | + ): SplitResult { |
| 34 | + const virtualIdentifier = VirtualVariableIdentifier.generate( |
| 35 | + identifier, |
| 36 | + node.loc |
| 37 | + ); |
| 38 | + |
| 39 | + return { |
| 40 | + virtualIdentifier, |
| 41 | + virtualDeclaration: { |
| 42 | + type: "VariableDeclaration", |
| 43 | + kind: "const", |
| 44 | + declarations: [ |
| 45 | + { |
| 46 | + type: "VariableDeclarator", |
| 47 | + id: { |
| 48 | + type: "Identifier", |
| 49 | + name: virtualIdentifier |
| 50 | + }, |
| 51 | + init: target |
| 52 | + } |
| 53 | + ] |
| 54 | + |
| 55 | + }, |
| 56 | + rebuildExpression: Inlined.#rebuildWithVirtualIdentifier( |
| 57 | + node, |
| 58 | + target, |
| 59 | + virtualIdentifier |
| 60 | + ) |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + static #rebuildWithVirtualIdentifier( |
| 65 | + node: ESTree.Node, |
| 66 | + target: ESTree.Node, |
| 67 | + virtualIdentifier: string |
| 68 | + ): ESTree.Node | null { |
| 69 | + if (node === target) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + const virtualId: ESTree.Identifier = { |
| 74 | + type: "Identifier", |
| 75 | + name: virtualIdentifier |
| 76 | + }; |
| 77 | + |
| 78 | + return Inlined.#cloneAndReplace( |
| 79 | + node, |
| 80 | + target, |
| 81 | + virtualId |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + static #cloneAndReplace( |
| 86 | + node: ESTree.Node, |
| 87 | + target: ESTree.Node, |
| 88 | + replacement: ESTree.Identifier |
| 89 | + ): ESTree.Node { |
| 90 | + if (node === target) { |
| 91 | + return replacement; |
| 92 | + } |
| 93 | + |
| 94 | + if (node.type === "CallExpression") { |
| 95 | + const callee = Inlined.#cloneAndReplace( |
| 96 | + node.callee, |
| 97 | + target, |
| 98 | + replacement |
| 99 | + ) as ESTree.Expression; |
| 100 | + |
| 101 | + const args = node.arguments.map( |
| 102 | + (arg) => Inlined.#cloneAndReplace(arg, target, replacement) |
| 103 | + ) as ESTree.Expression[]; |
| 104 | + |
| 105 | + return { |
| 106 | + ...node, |
| 107 | + callee, |
| 108 | + arguments: args |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + if (node.type === "MemberExpression") { |
| 113 | + return { |
| 114 | + ...node, |
| 115 | + object: Inlined.#cloneAndReplace( |
| 116 | + node.object, |
| 117 | + target, |
| 118 | + replacement |
| 119 | + ) as ESTree.Expression |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + return node; |
| 124 | + } |
| 125 | +} |
0 commit comments