| 1 | // This file contains methods responsible for replacing a node with another. |
| 2 | |
| 3 | import { codeFrameColumnsscip-typescript npm @babel/code-frame 7.18.6 src/`index.ts`/codeFrameColumns(). } from "@babel/code-frame"scip-typescript npm @babel/code-frame 7.18.6 src/`index.ts`/; |
| 4 | import traversescip-typescript npm @babel/traverse 7.20.13 src/`index.ts`/traverse(). from "../index"scip-typescript npm @babel/traverse 7.20.13 src/`index.ts`/; |
| 5 | import NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath# from "./index"scip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/; |
| 6 | import { pathscip-typescript npm @babel/traverse 7.20.13 src/`cache.ts`/path. as pathCachescip-typescript npm @babel/traverse 7.20.13 src/`cache.ts`/path. } from "../cache"scip-typescript npm @babel/traverse 7.20.13 src/`cache.ts`/; |
| 7 | import { parsescip-typescript npm @babel/parser 7.20.15 src/`index.ts`/parse(). } from "@babel/parser"scip-typescript npm @babel/parser 7.20.15 src/`index.ts`/; |
| 8 | import { |
| 9 | FUNCTION_TYPESscip-typescript npm @babel/types 7.20.7 src/constants/generated/`index.ts`/FUNCTION_TYPES., |
| 10 | arrowFunctionExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/arrowFunctionExpression()., |
| 11 | assignmentExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/assignmentExpression()., |
| 12 | awaitExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/awaitExpression()., |
| 13 | blockStatementscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/blockStatement()., |
| 14 | callExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/callExpression()., |
| 15 | cloneNodescip-typescript npm @babel/types 7.20.7 src/clone/`cloneNode.ts`/cloneNode()., |
| 16 | expressionStatementscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/expressionStatement()., |
| 17 | identifierscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/identifier()., |
| 18 | inheritLeadingCommentsscip-typescript npm @babel/types 7.20.7 src/comments/`inheritLeadingComments.ts`/inheritLeadingComments()., |
| 19 | inheritTrailingCommentsscip-typescript npm @babel/types 7.20.7 src/comments/`inheritTrailingComments.ts`/inheritTrailingComments()., |
| 20 | inheritsCommentsscip-typescript npm @babel/types 7.20.7 src/comments/`inheritsComments.ts`/inheritsComments()., |
| 21 | isExpressionscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isExpression()., |
| 22 | isProgramscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isProgram()., |
| 23 | isStatementscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isStatement()., |
| 24 | removeCommentsscip-typescript npm @babel/types 7.20.7 src/comments/`removeComments.ts`/removeComments()., |
| 25 | returnStatementscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/returnStatement()., |
| 26 | toSequenceExpressionscip-typescript npm @babel/types 7.20.7 src/converters/`toSequenceExpression.ts`/toSequenceExpression()., |
| 27 | validatescip-typescript npm @babel/types 7.20.7 src/validators/`validate.ts`/validate()., |
| 28 | yieldExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/yieldExpression()., |
| 29 | } from "@babel/types"scip-typescript npm @babel/types 7.20.7 src/`index.ts`/; |
| 30 | import type * as tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/ from "@babel/types"scip-typescript npm @babel/types 7.20.7 src/`index.ts`/; |
| 31 | import hoistVariablesscip-typescript npm @babel/helper-hoist-variables 7.18.6 src/`index.ts`/hoistVariables(). from "@babel/helper-hoist-variables"scip-typescript npm @babel/helper-hoist-variables 7.18.6 src/`index.ts`/; |
| 32 | |
| 33 | /** |
| 34 | * Replace a node with an array of multiple. This method performs the following steps: |
| 35 | * |
| 36 | * - Inherit the comments of first provided node with that of the current node. |
| 37 | * - Insert the provided nodes after the current node. |
| 38 | * - Remove the current node. |
| 39 | */ |
| 40 | |
| 41 | export function replaceWithMultiplescip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithMultiple().( |
| 42 | thisscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithMultiple().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, |
| 43 | nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithMultiple().(nodes): tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Nodescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Node# | tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Nodescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Node#[], |
| 44 | ): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#[] { |
| 45 | this.resyncscip-typescript npm @babel/traverse 7.20.13 src/path/`context.ts`/resync().(); |
| 46 | |
| 47 | nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithMultiple().(nodes) = this._verifyNodeListscip-typescript npm @babel/traverse 7.20.13 src/path/`modification.ts`/_verifyNodeList().(nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithMultiple().(nodes)); |
| 48 | inheritLeadingCommentsscip-typescript npm @babel/types 7.20.7 src/comments/`inheritLeadingComments.ts`/inheritLeadingComments().(nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithMultiple().(nodes)[0], this.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.); |
| 49 | inheritTrailingCommentsscip-typescript npm @babel/types 7.20.7 src/comments/`inheritTrailingComments.ts`/inheritTrailingComments().(nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithMultiple().(nodes)[nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithMultiple().(nodes).lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. - 1], this.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.); |
| 50 | pathCachescip-typescript npm @babel/traverse 7.20.13 src/`cache.ts`/path..getscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/WeakMap#get().(this.parentscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parent.)?.delete(this.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.); |
| 51 | this.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node. = |
| 52 | // @ts-expect-error this.key must present in this.container |
| 53 | this.containerscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#container.[this.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key.] = null; |
| 54 | const paths = this.insertAfterscip-typescript npm @babel/traverse 7.20.13 src/path/`modification.ts`/insertAfter().(nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithMultiple().(nodes)); |
| 55 | |
| 56 | if (this.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.) { |
| 57 | this.requeuescip-typescript npm @babel/traverse 7.20.13 src/path/`context.ts`/requeue().(); |
| 58 | } else { |
| 59 | this.removescip-typescript npm @babel/traverse 7.20.13 src/path/`removal.ts`/remove().(); |
| 60 | } |
| 61 | return paths; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Parse a string as an expression and replace the current node with the result. |
| 66 | * |
| 67 | * NOTE: This is typically not a good idea to use. Building source strings when |
| 68 | * transforming ASTs is an antipattern and SHOULD NOT be encouraged. Even if it's |
| 69 | * easier to use, your transforms will be extremely brittle. |
| 70 | */ |
| 71 | |
| 72 | export function replaceWithSourceStringscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithSourceString().(thisscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithSourceString().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, replacementscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithSourceString().(replacement): string) { |
| 73 | this.resyncscip-typescript npm @babel/traverse 7.20.13 src/path/`context.ts`/resync().(); |
| 74 | let ast: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Filescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/File#; |
| 75 | |
| 76 | try { |
| 77 | replacementscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithSourceString().(replacement) = `(${replacementscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithSourceString().(replacement)})`; |
| 78 | // @ts-expect-error todo: use babel-types ast typings in Babel parser |
| 79 | ast = parsescip-typescript npm @babel/parser 7.20.15 src/`index.ts`/parse().(replacementscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithSourceString().(replacement)); |
| 80 | } catch (err) { |
| 81 | const loc = err.loc; |
| 82 | if (loc) { |
| 83 | err.message += |
| 84 | " - make sure this is an expression.\n" + |
| 85 | codeFrameColumnsscip-typescript npm @babel/code-frame 7.18.6 src/`index.ts`/codeFrameColumns().(replacementscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithSourceString().(replacement), { |
| 86 | startscip-typescript npm @babel/code-frame 7.18.6 src/`index.ts`/NodeLocation#typeLiteral1:start.: { |
| 87 | linescip-typescript npm @babel/code-frame 7.18.6 src/`index.ts`/Location#typeLiteral0:line.: loc.line, |
| 88 | columnscip-typescript npm @babel/code-frame 7.18.6 src/`index.ts`/Location#typeLiteral0:column.: loc.column + 1, |
| 89 | }, |
| 90 | }); |
| 91 | err.code = "BABEL_REPLACE_SOURCE_ERROR"; |
| 92 | } |
| 93 | throw err; |
| 94 | } |
| 95 | |
| 96 | const expressionAST = (ast.programscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/File#program..bodyscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Program#body.[0] as tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.ExpressionStatementscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ExpressionStatement#) |
| 97 | .expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ExpressionStatement#expression.; |
| 98 | traversescip-typescript npm @babel/traverse 7.20.13 src/`index.ts`/traverse()..removeProperties(expressionAST); |
| 99 | return this.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(expressionAST); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Replace the current node with another. |
| 104 | */ |
| 105 | |
| 106 | export function replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().<Rscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().[R] extends tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Nodescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Node#>( |
| 107 | thisscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, |
| 108 | replacementPathscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(replacementPath): Rscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().[R] | NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<Rscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().[R]>, |
| 109 | ): [NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<Rscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().[R]>] { |
| 110 | this.resyncscip-typescript npm @babel/traverse 7.20.13 src/path/`context.ts`/resync().(); |
| 111 | |
| 112 | if (this.removedscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#`<get>removed`().) { |
| 113 | throw new Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#("You can't replace this node, we've already removed it"); |
| 114 | } |
| 115 | |
| 116 | let replacement: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Nodescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Node# = |
| 117 | replacementPathscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(replacementPath) instanceof NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath# |
| 118 | ? replacementPathscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(replacementPath).nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node. |
| 119 | : replacementPathscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(replacementPath); |
| 120 | |
| 121 | if (!replacement) { |
| 122 | throw new Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#( |
| 123 | "You passed `path.replaceWith()` a falsy node, use `path.remove()` instead", |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | if (this.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node. === replacement) { |
| 128 | return [this as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<Rscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().[R]>]; |
| 129 | } |
| 130 | |
| 131 | if (this.isProgramscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isProgram().() && !isProgramscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isProgram().(replacement)) { |
| 132 | throw new Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#( |
| 133 | "You can only replace a Program root node with another Program node", |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | if (Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#.isArrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/ArrayConstructor#isArray().(replacement)) { |
| 138 | throw new Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#( |
| 139 | "Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`", |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | if (typeof replacement === "string") { |
| 144 | throw new Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#( |
| 145 | "Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`", |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | let nodePath = ""; |
| 150 | |
| 151 | if (this.isNodeTypescip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/isNodeType().("Statement") && isExpressionscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isExpression().(replacement)) { |
| 152 | if ( |
| 153 | !this.canHaveVariableDeclarationOrExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/canHaveVariableDeclarationOrExpression().() && |
| 154 | !this.canSwapBetweenExpressionAndStatementscip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/canSwapBetweenExpressionAndStatement().(replacement) && |
| 155 | !this.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath..isExportDefaultDeclarationscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isExportDefaultDeclaration().() |
| 156 | ) { |
| 157 | // replacing a statement with an expression so wrap it in an expression statement |
| 158 | replacement = expressionStatementscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/expressionStatement().(replacement); |
| 159 | nodePath = "expression"; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (this.isNodeTypescip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/isNodeType().("Expression") && isStatementscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isStatement().(replacement)) { |
| 164 | if ( |
| 165 | !this.canHaveVariableDeclarationOrExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/canHaveVariableDeclarationOrExpression().() && |
| 166 | !this.canSwapBetweenExpressionAndStatementscip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/canSwapBetweenExpressionAndStatement().(replacement) |
| 167 | ) { |
| 168 | // replacing an expression with a statement so let's explode it |
| 169 | return this.replaceExpressionWithStatementsscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceExpressionWithStatements().([replacement]) as [ |
| 170 | NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<Rscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().[R]>, |
| 171 | ]; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | const oldNode = this.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.; |
| 176 | if (oldNode) { |
| 177 | inheritsCommentsscip-typescript npm @babel/types 7.20.7 src/comments/`inheritsComments.ts`/inheritsComments().(replacement, oldNode); |
| 178 | removeCommentsscip-typescript npm @babel/types 7.20.7 src/comments/`removeComments.ts`/removeComments().(oldNode); |
| 179 | } |
| 180 | |
| 181 | // replace the node |
| 182 | this._replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/_replaceWith().(replacement); |
| 183 | this.typescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#type. = replacement.typescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ArrayExpression#type.; |
| 184 | |
| 185 | // potentially create new scope |
| 186 | this.setScopescip-typescript npm @babel/traverse 7.20.13 src/path/`context.ts`/setScope().(); |
| 187 | |
| 188 | // requeue for visiting |
| 189 | this.requeuescip-typescript npm @babel/traverse 7.20.13 src/path/`context.ts`/requeue().(); |
| 190 | |
| 191 | return [ |
| 192 | nodePath ? (this.get(nodePath) as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<Rscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().[R]>) : (this as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<Rscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().[R]>), |
| 193 | ]; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Description |
| 198 | */ |
| 199 | |
| 200 | export function _replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/_replaceWith().(thisscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/_replaceWith().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, nodescip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/_replaceWith().(node): tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Nodescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Node#) { |
| 201 | if (!this.containerscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#container.) { |
| 202 | throw new ReferenceErrorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/ReferenceError#("Container is falsy"); |
| 203 | } |
| 204 | |
| 205 | if (this.inListscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#`<get>inList`().) { |
| 206 | // @ts-expect-error todo(flow->ts): check if validate accepts a numeric key |
| 207 | validatescip-typescript npm @babel/types 7.20.7 src/validators/`validate.ts`/validate().(this.parentscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parent., this.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key., [nodescip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/_replaceWith().(node)]); |
| 208 | } else { |
| 209 | validatescip-typescript npm @babel/types 7.20.7 src/validators/`validate.ts`/validate().(this.parentscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parent., this.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key. as string, nodescip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/_replaceWith().(node)); |
| 210 | } |
| 211 | |
| 212 | this.debugscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#debug().(`Replace with ${nodescip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/_replaceWith().(node)?.typescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ArrayExpression#type.}`); |
| 213 | pathCachescip-typescript npm @babel/traverse 7.20.13 src/`cache.ts`/path..getscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/WeakMap#get().(this.parentscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parent.)?.set(nodescip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/_replaceWith().(node), this).delete(this.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.); |
| 214 | |
| 215 | this.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node. = |
| 216 | // @ts-expect-error this.key must present in this.container |
| 217 | this.containerscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#container.[this.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key.] = nodescip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/_replaceWith().(node); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * This method takes an array of statements nodes and then explodes it |
| 222 | * into expressions. This method retains completion records which is |
| 223 | * extremely important to retain original semantics. |
| 224 | */ |
| 225 | |
| 226 | export function replaceExpressionWithStatementsscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceExpressionWithStatements().( |
| 227 | thisscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceExpressionWithStatements().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, |
| 228 | nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceExpressionWithStatements().(nodes): Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Statementscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Statement#>, |
| 229 | ) { |
| 230 | this.resyncscip-typescript npm @babel/traverse 7.20.13 src/path/`context.ts`/resync().(); |
| 231 | |
| 232 | const nodesAsSequenceExpression = toSequenceExpressionscip-typescript npm @babel/types 7.20.7 src/converters/`toSequenceExpression.ts`/toSequenceExpression().(nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceExpressionWithStatements().(nodes), this.scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope.); |
| 233 | |
| 234 | if (nodesAsSequenceExpression) { |
| 235 | return this.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(nodesAsSequenceExpression)[0].get("expressions"); |
| 236 | } |
| 237 | |
| 238 | const functionParent = this.getFunctionParentscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/getFunctionParent().(); |
| 239 | const isParentAsync = functionParent?.isscip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/is.("async"); |
| 240 | const isParentGenerator = functionParent?.isscip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/is.("generator"); |
| 241 | |
| 242 | const container = arrowFunctionExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/arrowFunctionExpression().([], blockStatementscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/blockStatement().(nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceExpressionWithStatements().(nodes))); |
| 243 | |
| 244 | this.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(callExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/callExpression().(container, [])); |
| 245 | // replaceWith changes the type of "this", but it isn't trackable by TS |
| 246 | type ThisType = NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#< |
| 247 | tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.CallExpressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/CallExpression# & { |
| 248 | callee: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.ArrowFunctionExpressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ArrowFunctionExpression# & { body: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.BlockStatementscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BlockStatement# }; |
| 249 | } |
| 250 | >; |
| 251 | |
| 252 | // hoist variable declaration in do block |
| 253 | // `(do { var x = 1; x;})` -> `var x; (() => { x = 1; return x; })()` |
| 254 | const callee = (this as ThisType).get("callee"); |
| 255 | hoistVariablesscip-typescript npm @babel/helper-hoist-variables 7.18.6 src/`index.ts`/hoistVariables().( |
| 256 | callee.get("body"), |
| 257 | (id: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Identifierscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#) => { |
| 258 | this.scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope..pushscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#push().({ idscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#push().(opts)typeLiteral191:id. }); |
| 259 | }, |
| 260 | "var", |
| 261 | ); |
| 262 | |
| 263 | // add implicit returns to all ending expression statements |
| 264 | const completionRecords: Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#<NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#> = (this as ThisType) |
| 265 | .get("callee") |
| 266 | .getCompletionRecordsscip-typescript npm @babel/traverse 7.20.13 src/path/`family.ts`/getCompletionRecords().(); |
| 267 | for (const path of completionRecords) { |
| 268 | if (!path.isExpressionStatementscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isExpressionStatement().()) continue; |
| 269 | |
| 270 | const loop = path.findParentscip-typescript npm @babel/traverse 7.20.13 src/path/`ancestry.ts`/findParent().(path => path.isLoopscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isLoop().()); |
| 271 | if (loop) { |
| 272 | let uid = loop.getDatascip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#getData().("expressionReplacementReturnUid"); |
| 273 | |
| 274 | if (!uid) { |
| 275 | uid = callee.scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope..generateDeclaredUidIdentifierscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#generateDeclaredUidIdentifier().("ret"); |
| 276 | callee |
| 277 | .get("body") |
| 278 | .pushContainerscip-typescript npm @babel/traverse 7.20.13 src/path/`modification.ts`/pushContainer().("body", returnStatementscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/returnStatement().(cloneNodescip-typescript npm @babel/types 7.20.7 src/clone/`cloneNode.ts`/cloneNode().(uid))); |
| 279 | loop.setDatascip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#setData().("expressionReplacementReturnUid", uid); |
| 280 | } else { |
| 281 | uid = identifierscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/identifier().(uid.name); |
| 282 | } |
| 283 | |
| 284 | path |
| 285 | .get("expression") |
| 286 | .replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().( |
| 287 | assignmentExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/assignmentExpression().("=", cloneNodescip-typescript npm @babel/types 7.20.7 src/clone/`cloneNode.ts`/cloneNode().(uid), path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ExpressionStatement#expression.), |
| 288 | ); |
| 289 | } else { |
| 290 | path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(returnStatementscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/returnStatement().(path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ExpressionStatement#expression.)); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // This is an IIFE, so we don't need to worry about the noNewArrows assumption |
| 295 | callee.arrowFunctionToExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/`conversion.ts`/arrowFunctionToExpression().(); |
| 296 | // Fixme: we can not `assert this is NodePath<t.FunctionExpression>` in `arrowFunctionToExpression` |
| 297 | // because it is not a class method known at compile time. |
| 298 | const newCallee = callee as unknown as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.FunctionExpressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/FunctionExpression#>; |
| 299 | |
| 300 | // (() => await xxx)() -> await (async () => await xxx)(); |
| 301 | const needToAwaitFunction = |
| 302 | isParentAsync && |
| 303 | traversescip-typescript npm @babel/traverse 7.20.13 src/`index.ts`/traverse()..hasType( |
| 304 | (this.get("callee.body") as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.BlockStatementscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BlockStatement#>).nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node., |
| 305 | "AwaitExpression", |
| 306 | FUNCTION_TYPESscip-typescript npm @babel/types 7.20.7 src/constants/generated/`index.ts`/FUNCTION_TYPES., |
| 307 | ); |
| 308 | const needToYieldFunction = |
| 309 | isParentGenerator && |
| 310 | traversescip-typescript npm @babel/traverse 7.20.13 src/`index.ts`/traverse()..hasType( |
| 311 | (this.get("callee.body") as NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.BlockStatementscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BlockStatement#>).nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node., |
| 312 | "YieldExpression", |
| 313 | FUNCTION_TYPESscip-typescript npm @babel/types 7.20.7 src/constants/generated/`index.ts`/FUNCTION_TYPES., |
| 314 | ); |
| 315 | if (needToAwaitFunction) { |
| 316 | newCallee.setscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#set().("async", true); |
| 317 | // yield* will await the generator return result |
| 318 | if (!needToYieldFunction) { |
| 319 | this.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(awaitExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/awaitExpression().((this as ThisType).nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.)); |
| 320 | } |
| 321 | } |
| 322 | if (needToYieldFunction) { |
| 323 | newCallee.setscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#set().("generator", true); |
| 324 | this.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(yieldExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/yieldExpression().((this as ThisType).nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node., true)); |
| 325 | } |
| 326 | |
| 327 | return newCallee.get("body.body"); |
| 328 | } |
| 329 | |
| 330 | export function replaceInlinescip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceInline().(thisscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceInline().(this): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceInline().(nodes): tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Nodescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Node# | Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Nodescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Node#>) { |
| 331 | this.resyncscip-typescript npm @babel/traverse 7.20.13 src/path/`context.ts`/resync().(); |
| 332 | |
| 333 | if (Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#.isArrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/ArrayConstructor#isArray().(nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceInline().(nodes))) { |
| 334 | if (Arrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#.isArrayscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/ArrayConstructor#isArray().(this.containerscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#container.)) { |
| 335 | nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceInline().(nodes) = this._verifyNodeListscip-typescript npm @babel/traverse 7.20.13 src/path/`modification.ts`/_verifyNodeList().(nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceInline().(nodes)); |
| 336 | const paths = this._containerInsertAfterscip-typescript npm @babel/traverse 7.20.13 src/path/`modification.ts`/_containerInsertAfter().(nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceInline().(nodes)); |
| 337 | this.removescip-typescript npm @babel/traverse 7.20.13 src/path/`removal.ts`/remove().(); |
| 338 | return paths; |
| 339 | } else { |
| 340 | return this.replaceWithMultiplescip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWithMultiple().(nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceInline().(nodes)); |
| 341 | } |
| 342 | } else { |
| 343 | return this.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(nodesscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceInline().(nodes)); |
| 344 | } |
| 345 | } |