| 1 | import assert from "assert"; |
| 2 | import { |
| 3 | callExpression, |
| 4 | cloneNode, |
| 5 | expressionStatement, |
| 6 | identifier, |
| 7 | importDeclaration, |
| 8 | importDefaultSpecifier, |
| 9 | importNamespaceSpecifier, |
| 10 | importSpecifier, |
| 11 | memberExpression, |
| 12 | stringLiteral, |
| 13 | variableDeclaration, |
| 14 | variableDeclarator, |
| 15 | } from "@babel/types"; |
| 16 | import type * as t from "@babel/types"; |
| 17 | import type { Scope } from "@babel/traverse"; |
| 18 | import type { File } from "@babel/core"; |
| 19 | |
| 20 | /** |
| 21 | * A class to track and accumulate mutations to the AST that will eventually |
| 22 | * output a new require/import statement list. |
| 23 | */ |
| 24 | export default class ImportBuilder { |
| 25 | private _statements: t.Statement[] = []; |
| 26 | private _resultName: t.Identifier | t.MemberExpression = null; |
| 27 | |
| 28 | declare _scope: Scope; |
| 29 | declare _hub: File["hub"]; |
| 30 | private _importedSource: string; |
| 31 | |
| 32 | constructor(importedSource: string, scope: Scope, hub: File["hub"]) { |
| 33 | this._scope = scope; |
| 34 | this._hub = hub; |
| 35 | this._importedSource = importedSource; |
| 36 | } |
| 37 | |
| 38 | done() { |
| 39 | return { |
| 40 | statements: this._statements, |
| 41 | resultName: this._resultName, |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | import() { |
| 46 | this._statements.push( |
| 47 | importDeclaration([], stringLiteral(this._importedSource)), |
| 48 | ); |
| 49 | return this; |
| 50 | } |
| 51 | |
| 52 | require() { |
| 53 | this._statements.push( |
| 54 | expressionStatement( |
| 55 | callExpression(identifier("require"), [ |
| 56 | stringLiteral(this._importedSource), |
| 57 | ]), |
| 58 | ), |
| 59 | ); |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | namespace(name = "namespace") { |
| 64 | const local = this._scope.generateUidIdentifier(name); |
| 65 | |
| 66 | const statement = this._statements[this._statements.length - 1]; |
| 67 | assert(statement.type === "ImportDeclaration"); |
| 68 | assert(statement.specifiers.length === 0); |
| 69 | statement.specifiers = [importNamespaceSpecifier(local)]; |
| 70 | this._resultName = cloneNode(local); |
| 71 | return this; |
| 72 | } |
| 73 | default(name: string) { |
| 74 | const id = this._scope.generateUidIdentifier(name); |
| 75 | const statement = this._statements[this._statements.length - 1]; |
| 76 | assert(statement.type === "ImportDeclaration"); |
| 77 | assert(statement.specifiers.length === 0); |
| 78 | statement.specifiers = [importDefaultSpecifier(id)]; |
| 79 | this._resultName = cloneNode(id); |
| 80 | return this; |
| 81 | } |
| 82 | named(name: string, importName: string) { |
| 83 | if (importName === "default") return this.default(name); |
| 84 | |
| 85 | const id = this._scope.generateUidIdentifier(name); |
| 86 | const statement = this._statements[this._statements.length - 1]; |
| 87 | assert(statement.type === "ImportDeclaration"); |
| 88 | assert(statement.specifiers.length === 0); |
| 89 | statement.specifiers = [importSpecifier(id, identifier(importName))]; |
| 90 | this._resultName = cloneNode(id); |
| 91 | return this; |
| 92 | } |
| 93 | |
| 94 | var(name: string) { |
| 95 | const id = this._scope.generateUidIdentifier(name); |
| 96 | let statement = this._statements[this._statements.length - 1]; |
| 97 | if (statement.type !== "ExpressionStatement") { |
| 98 | assert(this._resultName); |
| 99 | statement = expressionStatement(this._resultName); |
| 100 | this._statements.push(statement); |
| 101 | } |
| 102 | this._statements[this._statements.length - 1] = variableDeclaration("var", [ |
| 103 | variableDeclarator(id, statement.expression), |
| 104 | ]); |
| 105 | this._resultName = cloneNode(id); |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | defaultInterop() { |
| 110 | return this._interop(this._hub.addHelper("interopRequireDefault")); |
| 111 | } |
| 112 | wildcardInterop() { |
| 113 | return this._interop(this._hub.addHelper("interopRequireWildcard")); |
| 114 | } |
| 115 | |
| 116 | _interop(callee: t.Expression) { |
| 117 | const statement = this._statements[this._statements.length - 1]; |
| 118 | if (statement.type === "ExpressionStatement") { |
| 119 | statement.expression = callExpression(callee, [statement.expression]); |
| 120 | } else if (statement.type === "VariableDeclaration") { |
| 121 | assert(statement.declarations.length === 1); |
| 122 | statement.declarations[0].init = callExpression(callee, [ |
| 123 | statement.declarations[0].init, |
| 124 | ]); |
| 125 | } else { |
| 126 | assert.fail("Unexpected type."); |
| 127 | } |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | prop(name: string) { |
| 132 | const statement = this._statements[this._statements.length - 1]; |
| 133 | if (statement.type === "ExpressionStatement") { |
| 134 | statement.expression = memberExpression( |
| 135 | statement.expression, |
| 136 | identifier(name), |
| 137 | ); |
| 138 | } else if (statement.type === "VariableDeclaration") { |
| 139 | assert(statement.declarations.length === 1); |
| 140 | statement.declarations[0].init = memberExpression( |
| 141 | statement.declarations[0].init, |
| 142 | identifier(name), |
| 143 | ); |
| 144 | } else { |
| 145 | assert.fail("Unexpected type:" + statement.type); |
| 146 | } |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | read(name: string) { |
| 151 | this._resultName = memberExpression(this._resultName, identifier(name)); |
| 152 | } |
| 153 | } |