@babel/traverse 7.22.7 → 8.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cache.js +7 -32
- package/lib/cache.js.map +1 -1
- package/lib/context.js +7 -14
- package/lib/context.js.map +1 -1
- package/lib/hub.js +1 -8
- package/lib/hub.js.map +1 -1
- package/lib/index.js +4847 -36
- package/lib/index.js.map +1 -1
- package/lib/path/ancestry.js +11 -26
- package/lib/path/ancestry.js.map +1 -1
- package/lib/path/comments.js +4 -12
- package/lib/path/comments.js.map +1 -1
- package/lib/path/context.js +35 -63
- package/lib/path/context.js.map +1 -1
- package/lib/path/conversion.js +18 -38
- package/lib/path/conversion.js.map +1 -1
- package/lib/path/evaluation.js +4 -11
- package/lib/path/evaluation.js.map +1 -1
- package/lib/path/family.js +19 -33
- package/lib/path/family.js.map +1 -1
- package/lib/path/index.js +45 -53
- package/lib/path/index.js.map +1 -1
- package/lib/path/inference/index.js +9 -21
- package/lib/path/inference/index.js.map +1 -1
- package/lib/path/inference/inferer-reference.js +5 -11
- package/lib/path/inference/inferer-reference.js.map +1 -1
- package/lib/path/inference/inferers.js +30 -65
- package/lib/path/inference/inferers.js.map +1 -1
- package/lib/path/inference/util.js +6 -16
- package/lib/path/inference/util.js.map +1 -1
- package/lib/path/introspection.js +20 -45
- package/lib/path/introspection.js.map +1 -1
- package/lib/path/lib/hoister.js +10 -17
- package/lib/path/lib/hoister.js.map +1 -1
- package/lib/path/lib/removal-hooks.js +1 -8
- package/lib/path/lib/removal-hooks.js.map +1 -1
- package/lib/path/lib/virtual-types-validator.js +19 -42
- package/lib/path/lib/virtual-types-validator.js.map +1 -1
- package/lib/path/lib/virtual-types.js +18 -42
- package/lib/path/lib/virtual-types.js.map +1 -1
- package/lib/path/modification.js +20 -35
- package/lib/path/modification.js.map +1 -1
- package/lib/path/removal.js +13 -27
- package/lib/path/removal.js.map +1 -1
- package/lib/path/replacement.js +25 -38
- package/lib/path/replacement.js.map +1 -1
- package/lib/scope/binding.js +10 -17
- package/lib/scope/binding.js.map +1 -1
- package/lib/scope/index.js +49 -66
- package/lib/scope/index.js.map +1 -1
- package/lib/scope/lib/renamer.js +12 -20
- package/lib/scope/lib/renamer.js.map +1 -1
- package/lib/traverse-node.js +5 -15
- package/lib/traverse-node.js.map +1 -1
- package/lib/visitors.js +16 -24
- package/lib/visitors.js.map +1 -1
- package/package.json +17 -14
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["ReferencedIdentifier","
|
1
|
+
{"version":3,"names":["ReferencedIdentifier","ReferencedMemberExpression","BindingIdentifier","Statement","Expression","Scope","Referenced","BlockScoped","Var","User","Generated","Pure","Flow","RestProperty","SpreadProperty","ExistentialTypeParam","NumericLiteralTypeAnnotation","ForAwaitStatement"],"sources":["../../../src/path/lib/virtual-types.ts"],"sourcesContent":["import type * as t from \"@babel/types\";\n\nexport interface VirtualTypeAliases {\n BindingIdentifier: t.Identifier;\n BlockScoped: t.Node;\n ExistentialTypeParam: t.ExistsTypeAnnotation;\n Expression: t.Expression;\n Flow: t.Flow | t.ImportDeclaration | t.ExportDeclaration | t.ImportSpecifier;\n ForAwaitStatement: t.ForOfStatement;\n Generated: t.Node;\n NumericLiteralTypeAnnotation: t.NumberLiteralTypeAnnotation;\n Pure: t.Node;\n Referenced: t.Node;\n ReferencedIdentifier: t.Identifier | t.JSXIdentifier;\n ReferencedMemberExpression: t.MemberExpression;\n RestProperty: t.RestElement;\n Scope: t.Scopable | t.Pattern;\n SpreadProperty: t.RestElement;\n Statement: t.Statement;\n User: t.Node;\n Var: t.VariableDeclaration;\n}\n\ntype VirtualTypeMapping = readonly (t.Node[\"type\"] | keyof t.Aliases)[] | null;\n\nexport const ReferencedIdentifier: VirtualTypeMapping = [\n \"Identifier\",\n \"JSXIdentifier\",\n] as const;\n\nexport const ReferencedMemberExpression: VirtualTypeMapping = [\n \"MemberExpression\",\n] as const;\n\nexport const BindingIdentifier: VirtualTypeMapping = [\"Identifier\"] as const;\n\nexport const Statement: VirtualTypeMapping = [\"Statement\"] as const;\n\nexport const Expression: VirtualTypeMapping = [\"Expression\"] as const;\n\nexport const Scope: VirtualTypeMapping = [\"Scopable\", \"Pattern\"] as const;\n\nexport const Referenced: VirtualTypeMapping = null;\n\nexport const BlockScoped: VirtualTypeMapping = null;\n\nexport const Var: VirtualTypeMapping = [\"VariableDeclaration\"];\n\nexport const User: VirtualTypeMapping = null;\n\nexport const Generated: VirtualTypeMapping = null;\n\nexport const Pure: VirtualTypeMapping = null;\n\nexport const Flow: VirtualTypeMapping = [\n \"Flow\",\n \"ImportDeclaration\",\n \"ExportDeclaration\",\n \"ImportSpecifier\",\n] as const;\n\n// TODO: 7.0 Backwards Compat\nexport const RestProperty: VirtualTypeMapping = [\"RestElement\"] as const;\n\nexport const SpreadProperty: VirtualTypeMapping = [\"RestElement\"] as const;\n\nexport const ExistentialTypeParam: VirtualTypeMapping = [\n \"ExistsTypeAnnotation\",\n] as const;\n\nexport const NumericLiteralTypeAnnotation: VirtualTypeMapping = [\n \"NumberLiteralTypeAnnotation\",\n] as const;\n\nexport const ForAwaitStatement: VirtualTypeMapping = [\n \"ForOfStatement\",\n] as const;\n"],"mappings":"AAyBA,OAAO,MAAMA,oBAAwC,GAAG,CACtD,YAAY,EACZ,eAAe,CACP;AAEV,OAAO,MAAMC,0BAA8C,GAAG,CAC5D,kBAAkB,CACV;AAEV,OAAO,MAAMC,iBAAqC,GAAG,CAAC,YAAY,CAAU;AAE5E,OAAO,MAAMC,SAA6B,GAAG,CAAC,WAAW,CAAU;AAEnE,OAAO,MAAMC,UAA8B,GAAG,CAAC,YAAY,CAAU;AAErE,OAAO,MAAMC,KAAyB,GAAG,CAAC,UAAU,EAAE,SAAS,CAAU;AAEzE,OAAO,MAAMC,UAA8B,GAAG,IAAI;AAElD,OAAO,MAAMC,WAA+B,GAAG,IAAI;AAEnD,OAAO,MAAMC,GAAuB,GAAG,CAAC,qBAAqB,CAAC;AAE9D,OAAO,MAAMC,IAAwB,GAAG,IAAI;AAE5C,OAAO,MAAMC,SAA6B,GAAG,IAAI;AAEjD,OAAO,MAAMC,IAAwB,GAAG,IAAI;AAE5C,OAAO,MAAMC,IAAwB,GAAG,CACtC,MAAM,EACN,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,CACT;AAGV,OAAO,MAAMC,YAAgC,GAAG,CAAC,aAAa,CAAU;AAExE,OAAO,MAAMC,cAAkC,GAAG,CAAC,aAAa,CAAU;AAE1E,OAAO,MAAMC,oBAAwC,GAAG,CACtD,sBAAsB,CACd;AAEV,OAAO,MAAMC,4BAAgD,GAAG,CAC9D,6BAA6B,CACrB;AAEV,OAAO,MAAMC,iBAAqC,GAAG,CACnD,gBAAgB,CACR"}
|
package/lib/path/modification.js
CHANGED
@@ -1,22 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
});
|
6
|
-
exports._containerInsert = _containerInsert;
|
7
|
-
exports._containerInsertAfter = _containerInsertAfter;
|
8
|
-
exports._containerInsertBefore = _containerInsertBefore;
|
9
|
-
exports._verifyNodeList = _verifyNodeList;
|
10
|
-
exports.hoist = hoist;
|
11
|
-
exports.insertAfter = insertAfter;
|
12
|
-
exports.insertBefore = insertBefore;
|
13
|
-
exports.pushContainer = pushContainer;
|
14
|
-
exports.unshiftContainer = unshiftContainer;
|
15
|
-
exports.updateSiblingKeys = updateSiblingKeys;
|
16
|
-
var _cache = require("../cache");
|
17
|
-
var _hoister = require("./lib/hoister");
|
18
|
-
var _index = require("./index");
|
19
|
-
var _t = require("@babel/types");
|
1
|
+
import { path as pathCache } from "../cache.js";
|
2
|
+
import PathHoister from "./lib/hoister.js";
|
3
|
+
import NodePath from "./index.js";
|
4
|
+
import * as _t from "@babel/types";
|
20
5
|
const {
|
21
6
|
arrowFunctionExpression,
|
22
7
|
assertExpression,
|
@@ -34,7 +19,7 @@ const {
|
|
34
19
|
isSuper,
|
35
20
|
thisExpression
|
36
21
|
} = _t;
|
37
|
-
function insertBefore(nodes_) {
|
22
|
+
export function insertBefore(nodes_) {
|
38
23
|
this._assertUnremoved();
|
39
24
|
const nodes = this._verifyNodeList(nodes_);
|
40
25
|
const {
|
@@ -57,7 +42,7 @@ function insertBefore(nodes_) {
|
|
57
42
|
throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
|
58
43
|
}
|
59
44
|
}
|
60
|
-
function _containerInsert(from, nodes) {
|
45
|
+
export function _containerInsert(from, nodes) {
|
61
46
|
this.updateSiblingKeys(from, nodes.length);
|
62
47
|
const paths = [];
|
63
48
|
this.container.splice(from, 0, ...nodes);
|
@@ -65,7 +50,7 @@ function _containerInsert(from, nodes) {
|
|
65
50
|
const to = from + i;
|
66
51
|
const path = this.getSibling(to);
|
67
52
|
paths.push(path);
|
68
|
-
if (this.context
|
53
|
+
if (this.context?.queue) {
|
69
54
|
path.pushContext(this.context);
|
70
55
|
}
|
71
56
|
}
|
@@ -79,10 +64,10 @@ function _containerInsert(from, nodes) {
|
|
79
64
|
}
|
80
65
|
return paths;
|
81
66
|
}
|
82
|
-
function _containerInsertBefore(nodes) {
|
67
|
+
export function _containerInsertBefore(nodes) {
|
83
68
|
return this._containerInsert(this.key, nodes);
|
84
69
|
}
|
85
|
-
function _containerInsertAfter(nodes) {
|
70
|
+
export function _containerInsertAfter(nodes) {
|
86
71
|
return this._containerInsert(this.key + 1, nodes);
|
87
72
|
}
|
88
73
|
const last = arr => arr[arr.length - 1];
|
@@ -96,7 +81,7 @@ function isAlmostConstantAssignment(node, scope) {
|
|
96
81
|
const blockScope = scope.getBlockParent();
|
97
82
|
return blockScope.hasOwnBinding(node.left.name) && blockScope.getOwnBinding(node.left.name).constantViolations.length <= 1;
|
98
83
|
}
|
99
|
-
function insertAfter(nodes_) {
|
84
|
+
export function insertAfter(nodes_) {
|
100
85
|
this._assertUnremoved();
|
101
86
|
if (this.isSequenceExpression()) {
|
102
87
|
return last(this.get("expressions")).insertAfter(nodes_);
|
@@ -156,16 +141,16 @@ function insertAfter(nodes_) {
|
|
156
141
|
throw new Error("We don't know what to do with this node type. " + "We were previously a Statement but we can't fit in here?");
|
157
142
|
}
|
158
143
|
}
|
159
|
-
function updateSiblingKeys(fromIndex, incrementBy) {
|
144
|
+
export function updateSiblingKeys(fromIndex, incrementBy) {
|
160
145
|
if (!this.parent) return;
|
161
|
-
const paths =
|
146
|
+
const paths = pathCache.get(this.parent);
|
162
147
|
for (const [, path] of paths) {
|
163
148
|
if (typeof path.key === "number" && path.key >= fromIndex) {
|
164
149
|
path.key += incrementBy;
|
165
150
|
}
|
166
151
|
}
|
167
152
|
}
|
168
|
-
function _verifyNodeList(nodes) {
|
153
|
+
export function _verifyNodeList(nodes) {
|
169
154
|
if (!nodes) {
|
170
155
|
return [];
|
171
156
|
}
|
@@ -181,7 +166,7 @@ function _verifyNodeList(nodes) {
|
|
181
166
|
msg = "contains a non-object node";
|
182
167
|
} else if (!node.type) {
|
183
168
|
msg = "without a type";
|
184
|
-
} else if (node instanceof
|
169
|
+
} else if (node instanceof NodePath) {
|
185
170
|
msg = "has a NodePath when it expected a raw object";
|
186
171
|
}
|
187
172
|
if (msg) {
|
@@ -191,10 +176,10 @@ function _verifyNodeList(nodes) {
|
|
191
176
|
}
|
192
177
|
return nodes;
|
193
178
|
}
|
194
|
-
function unshiftContainer(listKey, nodes) {
|
179
|
+
export function unshiftContainer(listKey, nodes) {
|
195
180
|
this._assertUnremoved();
|
196
181
|
nodes = this._verifyNodeList(nodes);
|
197
|
-
const path =
|
182
|
+
const path = NodePath.get({
|
198
183
|
parentPath: this,
|
199
184
|
parent: this.node,
|
200
185
|
container: this.node[listKey],
|
@@ -203,11 +188,11 @@ function unshiftContainer(listKey, nodes) {
|
|
203
188
|
}).setContext(this.context);
|
204
189
|
return path._containerInsertBefore(nodes);
|
205
190
|
}
|
206
|
-
function pushContainer(listKey, nodes) {
|
191
|
+
export function pushContainer(listKey, nodes) {
|
207
192
|
this._assertUnremoved();
|
208
193
|
const verifiedNodes = this._verifyNodeList(nodes);
|
209
194
|
const container = this.node[listKey];
|
210
|
-
const path =
|
195
|
+
const path = NodePath.get({
|
211
196
|
parentPath: this,
|
212
197
|
parent: this.node,
|
213
198
|
container: container,
|
@@ -216,8 +201,8 @@ function pushContainer(listKey, nodes) {
|
|
216
201
|
}).setContext(this.context);
|
217
202
|
return path.replaceWithMultiple(verifiedNodes);
|
218
203
|
}
|
219
|
-
function hoist(scope = this.scope) {
|
220
|
-
const hoister = new
|
204
|
+
export function hoist(scope = this.scope) {
|
205
|
+
const hoister = new PathHoister(this, scope);
|
221
206
|
return hoister.run();
|
222
207
|
}
|
223
208
|
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_cache","require","_hoister","_index","_t","arrowFunctionExpression","assertExpression","assignmentExpression","blockStatement","callExpression","cloneNode","expressionStatement","isAssignmentExpression","isCallExpression","isExportNamedDeclaration","isExpression","isIdentifier","isSequenceExpression","isSuper","thisExpression","insertBefore","nodes_","_assertUnremoved","nodes","_verifyNodeList","parentPath","parent","isExpressionStatement","isLabeledStatement","isExportDefaultDeclaration","isDeclaration","isNodeType","isJSXElement","isForStatement","key","node","push","replaceExpressionWithStatements","Array","isArray","container","_containerInsertBefore","isStatementOrBlock","shouldInsertCurrentNode","expression","replaceWith","unshiftContainer","Error","_containerInsert","from","updateSiblingKeys","length","paths","splice","i","to","path","getSibling","context","queue","pushContext","contexts","_getQueueContexts","setScope","debug","maybeQueue","_containerInsertAfter","last","arr","isHiddenInSequenceExpression","expressions","isAlmostConstantAssignment","scope","left","blockScope","getBlockParent","hasOwnBinding","name","getOwnBinding","constantViolations","insertAfter","get","map","isPattern","unshift","callee","isPure","isMethod","computed","temp","generateDeclaredUidIdentifier","pushContainer","fromIndex","incrementBy","getCachedPaths","hub","msg","type","NodePath","listKey","setContext","verifiedNodes","replaceWithMultiple","hoist","hoister","PathHoister","run"],"sources":["../../src/path/modification.ts"],"sourcesContent":["// This file contains methods that modify the path/node in some ways.\n\nimport { getCachedPaths } from \"../cache\";\nimport PathHoister from \"./lib/hoister\";\nimport NodePath from \"./index\";\nimport {\n arrowFunctionExpression,\n assertExpression,\n assignmentExpression,\n blockStatement,\n callExpression,\n cloneNode,\n expressionStatement,\n isAssignmentExpression,\n isCallExpression,\n isExportNamedDeclaration,\n isExpression,\n isIdentifier,\n isSequenceExpression,\n isSuper,\n thisExpression,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport type Scope from \"../scope\";\n\n/**\n * Insert the provided nodes before the current one.\n */\n\nexport function insertBefore(\n this: NodePath,\n nodes_: t.Node | t.Node[],\n): NodePath[] {\n this._assertUnremoved();\n\n const nodes = this._verifyNodeList(nodes_);\n\n const { parentPath, parent } = this;\n\n if (\n parentPath.isExpressionStatement() ||\n parentPath.isLabeledStatement() ||\n // https://github.com/babel/babel/issues/15293\n // When Babel transforms `export class String { field }`, the class properties plugin will inject the defineProperty\n // helper, which depends on the builtins e.g. String, Number, Symbol, etc. To prevent them from being shadowed by local\n // exports, the helper injector replaces the named export into `class _String { field }; export { _String as String }`,\n // with `parentPath` here changed to the moved ClassDeclaration, causing rare inconsistency between `parent` and `parentPath`.\n // Here we retrieve the parent type from the `parent` property. This is a temporary fix and we should revisit when\n // helpers should get injected.\n isExportNamedDeclaration(parent) ||\n (parentPath.isExportDefaultDeclaration() && this.isDeclaration())\n ) {\n return parentPath.insertBefore(nodes);\n } else if (\n (this.isNodeType(\"Expression\") && !this.isJSXElement()) ||\n (parentPath.isForStatement() && this.key === \"init\")\n ) {\n if (this.node) nodes.push(this.node);\n // @ts-expect-error todo(flow->ts): check that nodes is an array of statements\n return this.replaceExpressionWithStatements(nodes);\n } else if (Array.isArray(this.container)) {\n return this._containerInsertBefore(nodes);\n } else if (this.isStatementOrBlock()) {\n const node = this.node as t.Statement;\n const shouldInsertCurrentNode =\n node &&\n (!this.isExpressionStatement() ||\n (node as t.ExpressionStatement).expression != null);\n\n this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));\n return (this as NodePath<t.BlockStatement>).unshiftContainer(\n \"body\",\n // @ts-expect-error Fixme: refine nodes to t.BlockStatement[\"body\"] when this is a BlockStatement path\n nodes,\n );\n } else {\n throw new Error(\n \"We don't know what to do with this node type. \" +\n \"We were previously a Statement but we can't fit in here?\",\n );\n }\n}\n\nexport function _containerInsert<N extends t.Node>(\n this: NodePath,\n from: number,\n nodes: N[],\n): NodePath<N>[] {\n this.updateSiblingKeys(from, nodes.length);\n\n const paths: NodePath<N>[] = [];\n\n // @ts-expect-error todo(flow->ts): this.container could be a NodePath\n this.container.splice(from, 0, ...nodes);\n for (let i = 0; i < nodes.length; i++) {\n const to = from + i;\n const path = this.getSibling(to) as NodePath<N>;\n paths.push(path);\n\n if (this.context && this.context.queue) {\n path.pushContext(this.context);\n }\n }\n\n const contexts = this._getQueueContexts();\n\n for (const path of paths) {\n path.setScope();\n path.debug(\"Inserted.\");\n\n for (const context of contexts) {\n context.maybeQueue(path, true);\n }\n }\n\n return paths;\n}\n\nexport function _containerInsertBefore<N extends t.Node>(\n this: NodePath,\n nodes: N[],\n) {\n return this._containerInsert(this.key as number, nodes);\n}\n\nexport function _containerInsertAfter<N extends t.Node>(\n this: NodePath,\n nodes: N[],\n) {\n return this._containerInsert((this.key as number) + 1, nodes);\n}\n\nconst last = <T>(arr: T[]) => arr[arr.length - 1];\n\nfunction isHiddenInSequenceExpression(path: NodePath): boolean {\n return (\n isSequenceExpression(path.parent) &&\n (last(path.parent.expressions) !== path.node ||\n isHiddenInSequenceExpression(path.parentPath))\n );\n}\n\nfunction isAlmostConstantAssignment(\n node: t.Node,\n scope: Scope,\n): node is t.AssignmentExpression & { left: t.Identifier } {\n if (!isAssignmentExpression(node) || !isIdentifier(node.left)) {\n return false;\n }\n\n // Not every scope can contain variables. For example, we might be in\n // a ClassScope either in the ClassHeritage or in a computed key.\n const blockScope = scope.getBlockParent();\n\n // If the variable is defined in the current scope and only assigned here,\n // we can be sure that its value won't change.\n return (\n blockScope.hasOwnBinding(node.left.name) &&\n blockScope.getOwnBinding(node.left.name).constantViolations.length <= 1\n );\n}\n\n/**\n * Insert the provided nodes after the current one. When inserting nodes after an\n * expression, ensure that the completion record is correct by pushing the current node.\n */\n\nexport function insertAfter(\n this: NodePath,\n nodes_: t.Node | t.Node[],\n): NodePath[] {\n this._assertUnremoved();\n\n if (this.isSequenceExpression()) {\n return last(this.get(\"expressions\")).insertAfter(nodes_);\n }\n\n const nodes = this._verifyNodeList(nodes_);\n\n const { parentPath, parent } = this;\n if (\n parentPath.isExpressionStatement() ||\n parentPath.isLabeledStatement() ||\n // see insertBefore\n isExportNamedDeclaration(parent) ||\n (parentPath.isExportDefaultDeclaration() && this.isDeclaration())\n ) {\n return parentPath.insertAfter(\n nodes.map(node => {\n // Usually after an expression we can safely insert another expression:\n // A.insertAfter(B)\n // foo = A; -> foo = (A, B);\n // If A is an expression statement, it isn't safe anymore so we need to\n // convert B to an expression statement\n // A; -> A; B // No semicolon! It could break if followed by [!\n return isExpression(node) ? expressionStatement(node) : node;\n }),\n );\n } else if (\n (this.isNodeType(\"Expression\") &&\n !this.isJSXElement() &&\n !parentPath.isJSXElement()) ||\n (parentPath.isForStatement() && this.key === \"init\")\n ) {\n if (this.node) {\n const node = this.node as t.Expression | t.VariableDeclaration;\n let { scope } = this;\n\n if (scope.path.isPattern()) {\n assertExpression(node);\n\n this.replaceWith(callExpression(arrowFunctionExpression([], node), []));\n (this.get(\"callee.body\") as NodePath<t.Expression>).insertAfter(nodes);\n return [this];\n }\n\n if (isHiddenInSequenceExpression(this)) {\n nodes.unshift(node);\n }\n // We need to preserve the value of this expression.\n else if (isCallExpression(node) && isSuper(node.callee)) {\n nodes.unshift(node);\n // `super(...)` always evaluates to `this`.\n nodes.push(thisExpression());\n } else if (isAlmostConstantAssignment(node, scope)) {\n nodes.unshift(node);\n nodes.push(cloneNode(node.left));\n } else if (scope.isPure(node, true)) {\n // Insert the nodes before rather than after; it's not observable.\n nodes.push(node);\n } else {\n // Inserting after the computed key of a method should insert the\n // temporary binding in the method's parent's scope.\n if (parentPath.isMethod({ computed: true, key: node })) {\n scope = scope.parent;\n }\n const temp = scope.generateDeclaredUidIdentifier();\n nodes.unshift(\n expressionStatement(\n // @ts-expect-error todo(flow->ts): This can be a variable\n // declaration in the \"init\" of a for statement, but that's\n // invalid here.\n assignmentExpression(\"=\", cloneNode(temp), node),\n ),\n );\n nodes.push(expressionStatement(cloneNode(temp)));\n }\n }\n // @ts-expect-error todo(flow->ts): check that nodes is an array of statements\n return this.replaceExpressionWithStatements(nodes);\n } else if (Array.isArray(this.container)) {\n return this._containerInsertAfter(nodes);\n } else if (this.isStatementOrBlock()) {\n const node = this.node as t.Statement;\n const shouldInsertCurrentNode =\n node &&\n (!this.isExpressionStatement() ||\n (node as t.ExpressionStatement).expression != null);\n\n this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));\n // @ts-expect-error Fixme: refine nodes to t.BlockStatement[\"body\"] when this is a BlockStatement path\n return this.pushContainer(\"body\", nodes);\n } else {\n throw new Error(\n \"We don't know what to do with this node type. \" +\n \"We were previously a Statement but we can't fit in here?\",\n );\n }\n}\n\n/**\n * Update all sibling node paths after `fromIndex` by `incrementBy`.\n */\n\nexport function updateSiblingKeys(\n this: NodePath,\n fromIndex: number,\n incrementBy: number,\n) {\n if (!this.parent) return;\n\n const paths = getCachedPaths(this.hub, this.parent) || ([] as never[]);\n\n for (const [, path] of paths) {\n if (typeof path.key === \"number\" && path.key >= fromIndex) {\n path.key += incrementBy;\n }\n }\n}\n\nexport function _verifyNodeList<N extends t.Node>(\n this: NodePath,\n nodes: N | N[],\n): N[] {\n if (!nodes) {\n return [];\n }\n\n if (!Array.isArray(nodes)) {\n nodes = [nodes];\n }\n\n for (let i = 0; i < nodes.length; i++) {\n const node = nodes[i];\n let msg;\n\n if (!node) {\n msg = \"has falsy node\";\n } else if (typeof node !== \"object\") {\n msg = \"contains a non-object node\";\n } else if (!node.type) {\n msg = \"without a type\";\n } else if (node instanceof NodePath) {\n msg = \"has a NodePath when it expected a raw object\";\n }\n\n if (msg) {\n const type = Array.isArray(node) ? \"array\" : typeof node;\n throw new Error(\n `Node list ${msg} with the index of ${i} and type of ${type}`,\n );\n }\n }\n\n return nodes;\n}\n\nexport function unshiftContainer<N extends t.Node, K extends keyof N & string>(\n this: NodePath<N>,\n listKey: K,\n nodes: N[K] extends (infer E)[]\n ? E | E[]\n : // todo: refine to t.Node[]\n // ? E extends t.Node\n // ? E | E[]\n // : never\n never,\n) {\n // todo: NodePaths<Nodes>\n this._assertUnremoved();\n\n // @ts-expect-error fixme\n nodes = this._verifyNodeList(nodes);\n\n // get the first path and insert our nodes before it, if it doesn't exist then it\n // doesn't matter, our nodes will be inserted anyway\n const path = NodePath.get({\n parentPath: this,\n parent: this.node,\n container: this.node[listKey] as unknown as t.Node | t.Node[],\n listKey,\n key: 0,\n }).setContext(this.context);\n\n return path._containerInsertBefore(\n // @ts-expect-error typings needed to narrow down nodes as t.Node[]\n nodes,\n );\n}\n\nexport function pushContainer<N extends t.Node, K extends keyof N & string>(\n this: NodePath<N>,\n listKey: K,\n nodes: N[K] extends (infer E)[]\n ? E | E[]\n : // todo: refine to t.Node[]\n // ? E extends t.Node\n // ? E | E[]\n // : never\n never,\n) {\n this._assertUnremoved();\n\n const verifiedNodes = this._verifyNodeList(\n // @ts-expect-error refine typings\n nodes,\n );\n\n // get an invisible path that represents the last node + 1 and replace it with our\n // nodes, effectively inlining it\n\n const container = this.node[listKey];\n const path = NodePath.get({\n parentPath: this,\n parent: this.node,\n container: container as unknown as t.Node | t.Node[],\n listKey,\n // @ts-expect-error TS cannot infer that container is t.Node[]\n key: container.length,\n }).setContext(this.context);\n\n return path.replaceWithMultiple(verifiedNodes);\n}\n\n/**\n * Hoist the current node to the highest scope possible and return a UID\n * referencing it.\n */\nexport function hoist<T extends t.Node>(\n this: NodePath<T>,\n scope: Scope = this.scope,\n) {\n const hoister = new PathHoister<T>(this, scope);\n return ho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CAAC;IACxBxD,UAAU,EAAE,IAAI;IAChBC,MAAM,EAAE,IAAI,CAACS,IAAI;IACjBK,SAAS,EAAE,IAAI,CAACL,IAAI,CAACgE,OAAO,CAAiC;IAC7DA,OAAO;IACPjE,GAAG,EAAE;EACP,CAAC,CAAC,CAACkE,UAAU,CAAC,IAAI,CAAC1C,OAAO,CAAC;EAE3B,OAAOF,IAAI,CAACf,sBAAsB,CAEhClB,KACF,CAAC;AACH;AAEO,SAASoE,aAAaA,CAE3BQ,OAAU,EACV5E,KAMS,EACT;EACA,IAAI,CAACD,gBAAgB,CAAC,CAAC;EAEvB,MAAM+E,aAAa,GAAG,IAAI,CAAC7E,eAAe,CAExCD,KACF,CAAC;EAKD,MAAMiB,SAAS,GAAG,IAAI,CAACL,IAAI,CAACgE,OAAO,CAAC;EACpC,MAAM3C,IAAI,GAAG0C,cAAQ,CAACjB,GAAG,CAAC;IACxBxD,UAAU,EAAE,IAAI;IAChBC,MAAM,EAAE,IAAI,CAACS,IAAI;IACjBK,SAAS,EAAEA,SAAyC;IACpD2D,OAAO;IAEPjE,GAAG,EAAEM,SAAS,CAACW;EACjB,CAAC,CAAC,CAACiD,UAAU,CAAC,IAAI,CAAC1C,OAAO,CAAC;EAE3B,OAAOF,IAAI,CAAC8C,mBAAmB,CAACD,aAAa,CAAC;AAChD;AAMO,SAASE,KAAKA,CAEnB/B,KAAY,GAAG,IAAI,CAACA,KAAK,EACzB;EACA,MAAMgC,OAAO,GAAG,IAAIC,gBAAW,CAAI,IAAI,EAAEjC,KAAK,CAAC;EAC/C,OAAOgC,OAAO,CAACE,GAAG,CAAC,CAAC;AACtB"}
|
1
|
+
{"version":3,"names":["path","pathCache","PathHoister","NodePath","_t","arrowFunctionExpression","assertExpression","assignmentExpression","blockStatement","callExpression","cloneNode","expressionStatement","isAssignmentExpression","isCallExpression","isExportNamedDeclaration","isExpression","isIdentifier","isSequenceExpression","isSuper","thisExpression","insertBefore","nodes_","_assertUnremoved","nodes","_verifyNodeList","parentPath","parent","isExpressionStatement","isLabeledStatement","isExportDefaultDeclaration","isDeclaration","isNodeType","isJSXElement","isForStatement","key","node","push","replaceExpressionWithStatements","Array","isArray","container","_containerInsertBefore","isStatementOrBlock","shouldInsertCurrentNode","expression","replaceWith","unshiftContainer","Error","_containerInsert","from","updateSiblingKeys","length","paths","splice","i","to","getSibling","context","queue","pushContext","contexts","_getQueueContexts","setScope","debug","maybeQueue","_containerInsertAfter","last","arr","isHiddenInSequenceExpression","expressions","isAlmostConstantAssignment","scope","left","blockScope","getBlockParent","hasOwnBinding","name","getOwnBinding","constantViolations","insertAfter","get","map","isPattern","unshift","callee","isPure","isMethod","computed","temp","generateDeclaredUidIdentifier","pushContainer","fromIndex","incrementBy","msg","type","listKey","setContext","verifiedNodes","replaceWithMultiple","hoist","hoister","run"],"sources":["../../src/path/modification.ts"],"sourcesContent":["// This file contains methods that modify the path/node in some ways.\n\nimport { path as pathCache } from \"../cache\";\nimport PathHoister from \"./lib/hoister\";\nimport NodePath from \"./index\";\nimport {\n arrowFunctionExpression,\n assertExpression,\n assignmentExpression,\n blockStatement,\n callExpression,\n cloneNode,\n expressionStatement,\n isAssignmentExpression,\n isCallExpression,\n isExportNamedDeclaration,\n isExpression,\n isIdentifier,\n isSequenceExpression,\n isSuper,\n thisExpression,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport type Scope from \"../scope\";\n\n/**\n * Insert the provided nodes before the current one.\n */\n\nexport function insertBefore(\n this: NodePath,\n nodes_: t.Node | t.Node[],\n): NodePath[] {\n this._assertUnremoved();\n\n const nodes = this._verifyNodeList(nodes_);\n\n const { parentPath, parent } = this;\n\n if (\n parentPath.isExpressionStatement() ||\n parentPath.isLabeledStatement() ||\n // https://github.com/babel/babel/issues/15293\n // When Babel transforms `export class String { field }`, the class properties plugin will inject the defineProperty\n // helper, which depends on the builtins e.g. String, Number, Symbol, etc. To prevent them from being shadowed by local\n // exports, the helper injector replaces the named export into `class _String { field }; export { _String as String }`,\n // with `parentPath` here changed to the moved ClassDeclaration, causing rare inconsistency between `parent` and `parentPath`.\n // Here we retrieve the parent type from the `parent` property. This is a temporary fix and we should revisit when\n // helpers should get injected.\n isExportNamedDeclaration(parent) ||\n (parentPath.isExportDefaultDeclaration() && this.isDeclaration())\n ) {\n return parentPath.insertBefore(nodes);\n } else if (\n (this.isNodeType(\"Expression\") && !this.isJSXElement()) ||\n (parentPath.isForStatement() && this.key === \"init\")\n ) {\n if (this.node) nodes.push(this.node);\n // @ts-expect-error todo(flow->ts): check that nodes is an array of statements\n return this.replaceExpressionWithStatements(nodes);\n } else if (Array.isArray(this.container)) {\n return this._containerInsertBefore(nodes);\n } else if (this.isStatementOrBlock()) {\n const node = this.node as t.Statement;\n const shouldInsertCurrentNode =\n node &&\n (!this.isExpressionStatement() ||\n (node as t.ExpressionStatement).expression != null);\n\n this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));\n return (this as NodePath<t.BlockStatement>).unshiftContainer(\n \"body\",\n // @ts-expect-error Fixme: refine nodes to t.BlockStatement[\"body\"] when this is a BlockStatement path\n nodes,\n );\n } else {\n throw new Error(\n \"We don't know what to do with this node type. \" +\n \"We were previously a Statement but we can't fit in here?\",\n );\n }\n}\n\nexport function _containerInsert<N extends t.Node>(\n this: NodePath,\n from: number,\n nodes: N[],\n): NodePath<N>[] {\n this.updateSiblingKeys(from, nodes.length);\n\n const paths: NodePath<N>[] = [];\n\n // @ts-expect-error todo(flow->ts): this.container could be a NodePath\n this.container.splice(from, 0, ...nodes);\n for (let i = 0; i < nodes.length; i++) {\n const to = from + i;\n const path = this.getSibling(to) as NodePath<N>;\n paths.push(path);\n\n if (this.context?.queue) {\n path.pushContext(this.context);\n }\n }\n\n const contexts = this._getQueueContexts();\n\n for (const path of paths) {\n path.setScope();\n path.debug(\"Inserted.\");\n\n for (const context of contexts) {\n context.maybeQueue(path, true);\n }\n }\n\n return paths;\n}\n\nexport function _containerInsertBefore<N extends t.Node>(\n this: NodePath,\n nodes: N[],\n) {\n return this._containerInsert(this.key as number, nodes);\n}\n\nexport function _containerInsertAfter<N extends t.Node>(\n this: NodePath,\n nodes: N[],\n) {\n return this._containerInsert((this.key as number) + 1, nodes);\n}\n\nconst last = <T>(arr: T[]) => arr[arr.length - 1];\n\nfunction isHiddenInSequenceExpression(path: NodePath): boolean {\n return (\n isSequenceExpression(path.parent) &&\n (last(path.parent.expressions) !== path.node ||\n isHiddenInSequenceExpression(path.parentPath))\n );\n}\n\nfunction isAlmostConstantAssignment(\n node: t.Node,\n scope: Scope,\n): node is t.AssignmentExpression & { left: t.Identifier } {\n if (!isAssignmentExpression(node) || !isIdentifier(node.left)) {\n return false;\n }\n\n // Not every scope can contain variables. For example, we might be in\n // a ClassScope either in the ClassHeritage or in a computed key.\n const blockScope = scope.getBlockParent();\n\n // If the variable is defined in the current scope and only assigned here,\n // we can be sure that its value won't change.\n return (\n blockScope.hasOwnBinding(node.left.name) &&\n blockScope.getOwnBinding(node.left.name).constantViolations.length <= 1\n );\n}\n\n/**\n * Insert the provided nodes after the current one. When inserting nodes after an\n * expression, ensure that the completion record is correct by pushing the current node.\n */\n\nexport function insertAfter(\n this: NodePath,\n nodes_: t.Node | t.Node[],\n): NodePath[] {\n this._assertUnremoved();\n\n if (this.isSequenceExpression()) {\n return last(this.get(\"expressions\")).insertAfter(nodes_);\n }\n\n const nodes = this._verifyNodeList(nodes_);\n\n const { parentPath, parent } = this;\n if (\n parentPath.isExpressionStatement() ||\n parentPath.isLabeledStatement() ||\n // see insertBefore\n isExportNamedDeclaration(parent) ||\n (parentPath.isExportDefaultDeclaration() && this.isDeclaration())\n ) {\n return parentPath.insertAfter(\n nodes.map(node => {\n // Usually after an expression we can safely insert another expression:\n // A.insertAfter(B)\n // foo = A; -> foo = (A, B);\n // If A is an expression statement, it isn't safe anymore so we need to\n // convert B to an expression statement\n // A; -> A; B // No semicolon! It could break if followed by [!\n return isExpression(node) ? expressionStatement(node) : node;\n }),\n );\n } else if (\n (this.isNodeType(\"Expression\") &&\n !this.isJSXElement() &&\n !parentPath.isJSXElement()) ||\n (parentPath.isForStatement() && this.key === \"init\")\n ) {\n if (this.node) {\n const node = this.node as t.Expression | t.VariableDeclaration;\n let { scope } = this;\n\n if (scope.path.isPattern()) {\n assertExpression(node);\n\n this.replaceWith(callExpression(arrowFunctionExpression([], node), []));\n (this.get(\"callee.body\") as NodePath<t.Expression>).insertAfter(nodes);\n return [this];\n }\n\n if (isHiddenInSequenceExpression(this)) {\n nodes.unshift(node);\n }\n // We need to preserve the value of this expression.\n else if (isCallExpression(node) && isSuper(node.callee)) {\n nodes.unshift(node);\n // `super(...)` always evaluates to `this`.\n nodes.push(thisExpression());\n } else if (isAlmostConstantAssignment(node, scope)) {\n nodes.unshift(node);\n nodes.push(cloneNode(node.left));\n } else if (scope.isPure(node, true)) {\n // Insert the nodes before rather than after; it's not observable.\n nodes.push(node);\n } else {\n // Inserting after the computed key of a method should insert the\n // temporary binding in the method's parent's scope.\n if (parentPath.isMethod({ computed: true, key: node })) {\n scope = scope.parent;\n }\n const temp = scope.generateDeclaredUidIdentifier();\n nodes.unshift(\n expressionStatement(\n // @ts-expect-error todo(flow->ts): This can be a variable\n // declaration in the \"init\" of a for statement, but that's\n // invalid here.\n assignmentExpression(\"=\", cloneNode(temp), node),\n ),\n );\n nodes.push(expressionStatement(cloneNode(temp)));\n }\n }\n // @ts-expect-error todo(flow->ts): check that nodes is an array of statements\n return this.replaceExpressionWithStatements(nodes);\n } else if (Array.isArray(this.container)) {\n return this._containerInsertAfter(nodes);\n } else if (this.isStatementOrBlock()) {\n const node = this.node as t.Statement;\n const shouldInsertCurrentNode =\n node &&\n (!this.isExpressionStatement() ||\n (node as t.ExpressionStatement).expression != null);\n\n this.replaceWith(blockStatement(shouldInsertCurrentNode ? [node] : []));\n // @ts-expect-error Fixme: refine nodes to t.BlockStatement[\"body\"] when this is a BlockStatement path\n return this.pushContainer(\"body\", nodes);\n } else {\n throw new Error(\n \"We don't know what to do with this node type. \" +\n \"We were previously a Statement but we can't fit in here?\",\n );\n }\n}\n\n/**\n * Update all sibling node paths after `fromIndex` by `incrementBy`.\n */\n\nexport function updateSiblingKeys(\n this: NodePath,\n fromIndex: number,\n incrementBy: number,\n) {\n if (!this.parent) return;\n\n const paths = pathCache.get(this.parent);\n\n for (const [, path] of paths) {\n if (typeof path.key === \"number\" && path.key >= fromIndex) {\n path.key += incrementBy;\n }\n }\n}\n\nexport function _verifyNodeList<N extends t.Node>(\n this: NodePath,\n nodes: N | N[],\n): N[] {\n if (!nodes) {\n return [];\n }\n\n if (!Array.isArray(nodes)) {\n nodes = [nodes];\n }\n\n for (let i = 0; i < nodes.length; i++) {\n const node = nodes[i];\n let msg;\n\n if (!node) {\n msg = \"has falsy node\";\n } else if (typeof node !== \"object\") {\n msg = \"contains a non-object node\";\n } else if (!node.type) {\n msg = \"without a type\";\n } else if (node instanceof NodePath) {\n msg = \"has a NodePath when it expected a raw object\";\n }\n\n if (msg) {\n const type = Array.isArray(node) ? \"array\" : typeof node;\n throw new Error(\n `Node list ${msg} with the index of ${i} and type of ${type}`,\n );\n }\n }\n\n return nodes;\n}\n\nexport function unshiftContainer<N extends t.Node, K extends keyof N & string>(\n this: NodePath<N>,\n listKey: K,\n nodes: N[K] extends (infer E)[]\n ? E | E[]\n : // todo: refine to t.Node[]\n // ? E extends t.Node\n // ? E | E[]\n // : never\n never,\n) {\n // todo: NodePaths<Nodes>\n this._assertUnremoved();\n\n // @ts-expect-error fixme\n nodes = this._verifyNodeList(nodes);\n\n // get the first path and insert our nodes before it, if it doesn't exist then it\n // doesn't matter, our nodes will be inserted anyway\n const path = NodePath.get({\n parentPath: this,\n parent: this.node,\n container: this.node[listKey] as unknown as t.Node | t.Node[],\n listKey,\n key: 0,\n }).setContext(this.context);\n\n return path._containerInsertBefore(\n // @ts-expect-error typings needed to narrow down nodes as t.Node[]\n nodes,\n );\n}\n\nexport function pushContainer<N extends t.Node, K extends keyof N & string>(\n this: NodePath<N>,\n listKey: K,\n nodes: N[K] extends (infer E)[]\n ? E | E[]\n : // todo: refine to t.Node[]\n // ? E extends t.Node\n // ? E | E[]\n // : never\n never,\n) {\n this._assertUnremoved();\n\n const verifiedNodes = this._verifyNodeList(\n // @ts-expect-error refine typings\n nodes,\n );\n\n // get an invisible path that represents the last node + 1 and replace it with our\n // nodes, effectively inlining it\n\n const container = this.node[listKey];\n const path = NodePath.get({\n parentPath: this,\n parent: this.node,\n container: container as unknown as t.Node | t.Node[],\n listKey,\n // @ts-expect-error TS cannot infer that container is t.Node[]\n key: container.length,\n }).setContext(this.context);\n\n return path.replaceWithMultiple(verifiedNodes);\n}\n\n/**\n * Hoist the current node to the highest scope possible and return a UID\n * referencing it.\n */\nexport function hoist<T extends t.Node>(\n this: NodePath<T>,\n scope: Scope = this.scope,\n) {\n const hoister = new PathHoister<T>(this, scope);\n return hoister.run();\n}\n"],"mappings":"AAEA,SAASA,IAAI,IAAIC,SAAS,QAAQ,aAAU;AAC5C,OAAOC,WAAW,MAAM,kBAAe;AACvC,OAAOC,QAAQ,MAAM,YAAS;AAC9B,YAAAC,EAAA,MAgBO,cAAc;AAAC;EAfpBC,uBAAuB;EACvBC,gBAAgB;EAChBC,oBAAoB;EACpBC,cAAc;EACdC,cAAc;EACdC,SAAS;EACTC,mBAAmB;EACnBC,sBAAsB;EACtBC,gBAAgB;EAChBC,wBAAwB;EACxBC,YAAY;EACZC,YAAY;EACZC,oBAAoB;EACpBC,OAAO;EACPC;AAAc,IAAAf,EAAA;AAShB,OAAO,SAASgB,YAAYA,CAE1BC,MAAyB,EACb;EACZ,IAAI,CAACC,gBAAgB,CAAC,CAAC;EAEvB,MAAMC,KAAK,GAAG,IAAI,CAACC,eAAe,CAACH,MAAM,CAAC;EAE1C,MAAM;IAAEI,UAAU;IAAEC;EAAO,CAAC,GAAG,IAAI;EAEnC,IACED,UAAU,CAACE,qBAAqB,CAAC,CAAC,IAClCF,UAAU,CAACG,kBAAkB,CAAC,CAAC,IAQ/Bd,wBAA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AEM,SAAS,CAACW;EACjB,CAAC,CAAC,CAAC6C,UAAU,CAAC,IAAI,CAACvC,OAAO,CAAC;EAE3B,OAAOzD,IAAI,CAACkG,mBAAmB,CAACD,aAAa,CAAC;AAChD;AAMA,OAAO,SAASE,KAAKA,CAEnB5B,KAAY,GAAG,IAAI,CAACA,KAAK,EACzB;EACA,MAAM6B,OAAO,GAAG,IAAIlG,WAAW,CAAI,IAAI,EAAEqE,KAAK,CAAC;EAC/C,OAAO6B,OAAO,CAACC,GAAG,CAAC,CAAC;AACtB"}
|
package/lib/path/removal.js
CHANGED
@@ -1,22 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
});
|
6
|
-
exports._assertUnremoved = _assertUnremoved;
|
7
|
-
exports._callRemovalHooks = _callRemovalHooks;
|
8
|
-
exports._markRemoved = _markRemoved;
|
9
|
-
exports._remove = _remove;
|
10
|
-
exports._removeFromScope = _removeFromScope;
|
11
|
-
exports.remove = remove;
|
12
|
-
var _removalHooks = require("./lib/removal-hooks");
|
13
|
-
var _cache = require("../cache");
|
14
|
-
var _index = require("./index");
|
15
|
-
function remove() {
|
16
|
-
var _this$opts;
|
1
|
+
import { hooks } from "./lib/removal-hooks.js";
|
2
|
+
import { path as pathCache } from "../cache.js";
|
3
|
+
import { REMOVED, SHOULD_SKIP } from "./index.js";
|
4
|
+
export function remove() {
|
17
5
|
this._assertUnremoved();
|
18
6
|
this.resync();
|
19
|
-
if (!
|
7
|
+
if (!this.opts?.noScope) {
|
20
8
|
this._removeFromScope();
|
21
9
|
}
|
22
10
|
if (this._callRemovalHooks()) {
|
@@ -27,16 +15,16 @@ function remove() {
|
|
27
15
|
this._remove();
|
28
16
|
this._markRemoved();
|
29
17
|
}
|
30
|
-
function _removeFromScope() {
|
18
|
+
export function _removeFromScope() {
|
31
19
|
const bindings = this.getBindingIdentifiers();
|
32
20
|
Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
|
33
21
|
}
|
34
|
-
function _callRemovalHooks() {
|
35
|
-
for (const fn of
|
22
|
+
export function _callRemovalHooks() {
|
23
|
+
for (const fn of hooks) {
|
36
24
|
if (fn(this, this.parentPath)) return true;
|
37
25
|
}
|
38
26
|
}
|
39
|
-
function _remove() {
|
27
|
+
export function _remove() {
|
40
28
|
if (Array.isArray(this.container)) {
|
41
29
|
this.container.splice(this.key, 1);
|
42
30
|
this.updateSiblingKeys(this.key, -1);
|
@@ -44,14 +32,12 @@ function _remove() {
|
|
44
32
|
this._replaceWith(null);
|
45
33
|
}
|
46
34
|
}
|
47
|
-
function _markRemoved() {
|
48
|
-
this._traverseFlags |=
|
49
|
-
if (this.parent)
|
50
|
-
(0, _cache.getCachedPaths)(this.hub, this.parent).delete(this.node);
|
51
|
-
}
|
35
|
+
export function _markRemoved() {
|
36
|
+
this._traverseFlags |= SHOULD_SKIP | REMOVED;
|
37
|
+
if (this.parent) pathCache.get(this.parent).delete(this.node);
|
52
38
|
this.node = null;
|
53
39
|
}
|
54
|
-
function _assertUnremoved() {
|
40
|
+
export function _assertUnremoved() {
|
55
41
|
if (this.removed) {
|
56
42
|
throw this.buildCodeFrameError("NodePath has been removed so is read-only.");
|
57
43
|
}
|
package/lib/path/removal.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["
|
1
|
+
{"version":3,"names":["hooks","path","pathCache","REMOVED","SHOULD_SKIP","remove","_assertUnremoved","resync","opts","noScope","_removeFromScope","_callRemovalHooks","_markRemoved","shareCommentsWithSiblings","_remove","bindings","getBindingIdentifiers","Object","keys","forEach","name","scope","removeBinding","fn","parentPath","Array","isArray","container","splice","key","updateSiblingKeys","_replaceWith","_traverseFlags","parent","get","delete","node","removed","buildCodeFrameError"],"sources":["../../src/path/removal.ts"],"sourcesContent":["// This file contains methods responsible for removing a node.\n\nimport { hooks } from \"./lib/removal-hooks\";\nimport { path as pathCache } from \"../cache\";\nimport type NodePath from \"./index\";\nimport { REMOVED, SHOULD_SKIP } from \"./index\";\n\nexport function remove(this: NodePath) {\n this._assertUnremoved();\n\n this.resync();\n if (!this.opts?.noScope) {\n this._removeFromScope();\n }\n\n if (this._callRemovalHooks()) {\n this._markRemoved();\n return;\n }\n\n this.shareCommentsWithSiblings();\n this._remove();\n this._markRemoved();\n}\n\nexport function _removeFromScope(this: NodePath) {\n const bindings = this.getBindingIdentifiers();\n Object.keys(bindings).forEach(name => this.scope.removeBinding(name));\n}\n\nexport function _callRemovalHooks(this: NodePath) {\n for (const fn of hooks) {\n if (fn(this, this.parentPath)) return true;\n }\n}\n\nexport function _remove(this: NodePath) {\n if (Array.isArray(this.container)) {\n this.container.splice(this.key as number, 1);\n this.updateSiblingKeys(this.key as number, -1);\n } else {\n this._replaceWith(null);\n }\n}\n\nexport function _markRemoved(this: NodePath) {\n // this.shouldSkip = true; this.removed = true;\n this._traverseFlags |= SHOULD_SKIP | REMOVED;\n if (this.parent) pathCache.get(this.parent).delete(this.node);\n this.node = null;\n}\n\nexport function _assertUnremoved(this: NodePath) {\n if (this.removed) {\n throw this.buildCodeFrameError(\n \"NodePath has been removed so is read-only.\",\n );\n }\n}\n"],"mappings":"AAEA,SAASA,KAAK,QAAQ,wBAAqB;AAC3C,SAASC,IAAI,IAAIC,SAAS,QAAQ,aAAU;AAE5C,SAASC,OAAO,EAAEC,WAAW,QAAQ,YAAS;AAE9C,OAAO,SAASC,MAAMA,CAAA,EAAiB;EACrC,IAAI,CAACC,gBAAgB,CAAC,CAAC;EAEvB,IAAI,CAACC,MAAM,CAAC,CAAC;EACb,IAAI,CAAC,IAAI,CAACC,IAAI,EAAEC,OAAO,EAAE;IACvB,IAAI,CAACC,gBAAgB,CAAC,CAAC;EACzB;EAEA,IAAI,IAAI,CAACC,iBAAiB,CAAC,CAAC,EAAE;IAC5B,IAAI,CAACC,YAAY,CAAC,CAAC;IACnB;EACF;EAEA,IAAI,CAACC,yBAAyB,CAAC,CAAC;EAChC,IAAI,CAACC,OAAO,CAAC,CAAC;EACd,IAAI,CAACF,YA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|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
}
|
6
|
-
|
7
|
-
exports.replaceExpressionWithStatements = replaceExpressionWithStatements;
|
8
|
-
exports.replaceInline = replaceInline;
|
9
|
-
exports.replaceWith = replaceWith;
|
10
|
-
exports.replaceWithMultiple = replaceWithMultiple;
|
11
|
-
exports.replaceWithSourceString = replaceWithSourceString;
|
12
|
-
var _codeFrame = require("@babel/code-frame");
|
13
|
-
var _index = require("../index");
|
14
|
-
var _index2 = require("./index");
|
15
|
-
var _cache = require("../cache");
|
16
|
-
var _parser = require("@babel/parser");
|
17
|
-
var _t = require("@babel/types");
|
18
|
-
var _helperHoistVariables = require("@babel/helper-hoist-variables");
|
1
|
+
import { codeFrameColumns } from "@babel/code-frame";
|
2
|
+
import traverse from "../index.js";
|
3
|
+
import NodePath from "./index.js";
|
4
|
+
import { path as pathCache } from "../cache.js";
|
5
|
+
import { parse } from "@babel/parser";
|
6
|
+
import * as _t from "@babel/types";
|
19
7
|
const {
|
20
8
|
FUNCTION_TYPES,
|
21
9
|
arrowFunctionExpression,
|
@@ -38,13 +26,13 @@ const {
|
|
38
26
|
validate,
|
39
27
|
yieldExpression
|
40
28
|
} = _t;
|
41
|
-
|
42
|
-
|
29
|
+
import hoistVariables from "@babel/helper-hoist-variables";
|
30
|
+
export function replaceWithMultiple(nodes) {
|
43
31
|
this.resync();
|
44
32
|
nodes = this._verifyNodeList(nodes);
|
45
33
|
inheritLeadingComments(nodes[0], this.node);
|
46
34
|
inheritTrailingComments(nodes[nodes.length - 1], this.node);
|
47
|
-
|
35
|
+
pathCache.get(this.parent)?.delete(this.node);
|
48
36
|
this.node = this.container[this.key] = null;
|
49
37
|
const paths = this.insertAfter(nodes);
|
50
38
|
if (this.node) {
|
@@ -54,16 +42,16 @@ function replaceWithMultiple(nodes) {
|
|
54
42
|
}
|
55
43
|
return paths;
|
56
44
|
}
|
57
|
-
function replaceWithSourceString(replacement) {
|
45
|
+
export function replaceWithSourceString(replacement) {
|
58
46
|
this.resync();
|
59
47
|
let ast;
|
60
48
|
try {
|
61
49
|
replacement = `(${replacement})`;
|
62
|
-
ast =
|
50
|
+
ast = parse(replacement);
|
63
51
|
} catch (err) {
|
64
52
|
const loc = err.loc;
|
65
53
|
if (loc) {
|
66
|
-
err.message += " - make sure this is an expression.\n" +
|
54
|
+
err.message += " - make sure this is an expression.\n" + codeFrameColumns(replacement, {
|
67
55
|
start: {
|
68
56
|
line: loc.line,
|
69
57
|
column: loc.column + 1
|
@@ -74,15 +62,15 @@ function replaceWithSourceString(replacement) {
|
|
74
62
|
throw err;
|
75
63
|
}
|
76
64
|
const expressionAST = ast.program.body[0].expression;
|
77
|
-
|
65
|
+
traverse.removeProperties(expressionAST);
|
78
66
|
return this.replaceWith(expressionAST);
|
79
67
|
}
|
80
|
-
function replaceWith(replacementPath) {
|
68
|
+
export function replaceWith(replacementPath) {
|
81
69
|
this.resync();
|
82
70
|
if (this.removed) {
|
83
71
|
throw new Error("You can't replace this node, we've already removed it");
|
84
72
|
}
|
85
|
-
let replacement = replacementPath instanceof
|
73
|
+
let replacement = replacementPath instanceof NodePath ? replacementPath.node : replacementPath;
|
86
74
|
if (!replacement) {
|
87
75
|
throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead");
|
88
76
|
}
|
@@ -121,8 +109,7 @@ function replaceWith(replacementPath) {
|
|
121
109
|
this.requeue();
|
122
110
|
return [nodePath ? this.get(nodePath) : this];
|
123
111
|
}
|
124
|
-
function _replaceWith(node) {
|
125
|
-
var _getCachedPaths2;
|
112
|
+
export function _replaceWith(node) {
|
126
113
|
if (!this.container) {
|
127
114
|
throw new ReferenceError("Container is falsy");
|
128
115
|
}
|
@@ -131,23 +118,23 @@ function _replaceWith(node) {
|
|
131
118
|
} else {
|
132
119
|
validate(this.parent, this.key, node);
|
133
120
|
}
|
134
|
-
this.debug(`Replace with ${node
|
135
|
-
|
121
|
+
this.debug(`Replace with ${node?.type}`);
|
122
|
+
pathCache.get(this.parent)?.set(node, this).delete(this.node);
|
136
123
|
this.node = this.container[this.key] = node;
|
137
124
|
}
|
138
|
-
function replaceExpressionWithStatements(nodes) {
|
125
|
+
export function replaceExpressionWithStatements(nodes) {
|
139
126
|
this.resync();
|
140
127
|
const nodesAsSequenceExpression = toSequenceExpression(nodes, this.scope);
|
141
128
|
if (nodesAsSequenceExpression) {
|
142
129
|
return this.replaceWith(nodesAsSequenceExpression)[0].get("expressions");
|
143
130
|
}
|
144
131
|
const functionParent = this.getFunctionParent();
|
145
|
-
const isParentAsync = functionParent
|
146
|
-
const isParentGenerator = functionParent
|
132
|
+
const isParentAsync = functionParent?.is("async");
|
133
|
+
const isParentGenerator = functionParent?.is("generator");
|
147
134
|
const container = arrowFunctionExpression([], blockStatement(nodes));
|
148
135
|
this.replaceWith(callExpression(container, []));
|
149
136
|
const callee = this.get("callee");
|
150
|
-
(
|
137
|
+
hoistVariables(callee.get("body"), id => {
|
151
138
|
this.scope.push({
|
152
139
|
id
|
153
140
|
});
|
@@ -172,8 +159,8 @@ function replaceExpressionWithStatements(nodes) {
|
|
172
159
|
}
|
173
160
|
callee.arrowFunctionToExpression();
|
174
161
|
const newCallee = callee;
|
175
|
-
const needToAwaitFunction = isParentAsync &&
|
176
|
-
const needToYieldFunction = isParentGenerator &&
|
162
|
+
const needToAwaitFunction = isParentAsync && traverse.hasType(this.get("callee.body").node, "AwaitExpression", FUNCTION_TYPES);
|
163
|
+
const needToYieldFunction = isParentGenerator && traverse.hasType(this.get("callee.body").node, "YieldExpression", FUNCTION_TYPES);
|
177
164
|
if (needToAwaitFunction) {
|
178
165
|
newCallee.set("async", true);
|
179
166
|
if (!needToYieldFunction) {
|
@@ -186,7 +173,7 @@ function replaceExpressionWithStatements(nodes) {
|
|
186
173
|
}
|
187
174
|
return newCallee.get("body.body");
|
188
175
|
}
|
189
|
-
function replaceInline(nodes) {
|
176
|
+
export function replaceInline(nodes) {
|
190
177
|
this.resync();
|
191
178
|
if (Array.isArray(nodes)) {
|
192
179
|
if (Array.isArray(this.container)) {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_codeFrame","require","_index","_index2","_cache","_parser","_t","_helperHoistVariables","FUNCTION_TYPES","arrowFunctionExpression","assignmentExpression","awaitExpression","blockStatement","callExpression","cloneNode","expressionStatement","identifier","inheritLeadingComments","inheritTrailingComments","inheritsComments","isExpression","isProgram","isStatement","removeComments","returnStatement","toSequenceExpression","validate","yieldExpression","replaceWithMultiple","nodes","_getCachedPaths","resync","_verifyNodeList","node","length","getCachedPaths","hub","parent","delete","container","key","paths","insertAfter","requeue","remove","replaceWithSourceString","replacement","ast","parse","err","loc","message","codeFrameColumns","start","line","column","code","expressionAST","program","body","expression","traverse","removeProperties","replaceWith","replacementPath","removed","Error","NodePath","Array","isArray","nodePath","isNodeType","canHaveVariableDeclarationOrExpression","canSwapBetweenExpressionAndStatement","parentPath","isExportDefaultDeclaration","replaceExpressionWithStatements","oldNode","_replaceWith","type","setScope","get","_getCachedPaths2","ReferenceError","inList","debug","set","nodesAsSequenceExpression","scope","functionParent","getFunctionParent","isParentAsync","is","isParentGenerator","callee","hoistVariables","id","push","completionRecords","getCompletionRecords","path","isExpressionStatement","loop","findParent","isLoop","uid","getData","generateDeclaredUidIdentifier","pushContainer","setData","name","arrowFunctionToExpression","newCallee","needToAwaitFunction","hasType","needToYieldFunction","replaceInline","_containerInsertAfter"],"sources":["../../src/path/replacement.ts"],"sourcesContent":["// This file contains methods responsible for replacing a node with another.\n\nimport { codeFrameColumns } from \"@babel/code-frame\";\nimport traverse from \"../index\";\nimport NodePath from \"./index\";\nimport { getCachedPaths } from \"../cache\";\nimport { parse } from \"@babel/parser\";\nimport {\n FUNCTION_TYPES,\n arrowFunctionExpression,\n assignmentExpression,\n awaitExpression,\n blockStatement,\n callExpression,\n cloneNode,\n expressionStatement,\n identifier,\n inheritLeadingComments,\n inheritTrailingComments,\n inheritsComments,\n isExpression,\n isProgram,\n isStatement,\n removeComments,\n returnStatement,\n toSequenceExpression,\n validate,\n yieldExpression,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport hoistVariables from \"@babel/helper-hoist-variables\";\n\n/**\n * Replace a node with an array of multiple. This method performs the following steps:\n *\n * - Inherit the comments of first provided node with that of the current node.\n * - Insert the provided nodes after the current node.\n * - Remove the current node.\n */\n\nexport function replaceWithMultiple(\n this: NodePath,\n nodes: t.Node | t.Node[],\n): NodePath[] {\n this.resync();\n\n nodes = this._verifyNodeList(nodes);\n inheritLeadingComments(nodes[0], this.node);\n inheritTrailingComments(nodes[nodes.length - 1], this.node);\n getCachedPaths(this.hub, this.parent)?.delete(this.node);\n this.node =\n // @ts-expect-error this.key must present in this.container\n this.container[this.key] = null;\n const paths = this.insertAfter(nodes);\n\n if (this.node) {\n this.requeue();\n } else {\n this.remove();\n }\n return paths;\n}\n\n/**\n * Parse a string as an expression and replace the current node with the result.\n *\n * NOTE: This is typically not a good idea to use. Building source strings when\n * transforming ASTs is an antipattern and SHOULD NOT be encouraged. Even if it's\n * easier to use, your transforms will be extremely brittle.\n */\n\nexport function replaceWithSourceString(this: NodePath, replacement: string) {\n this.resync();\n let ast: t.File;\n\n try {\n replacement = `(${replacement})`;\n // @ts-expect-error todo: use babel-types ast typings in Babel parser\n ast = parse(replacement);\n } catch (err) {\n const loc = err.loc;\n if (loc) {\n err.message +=\n \" - make sure this is an expression.\\n\" +\n codeFrameColumns(replacement, {\n start: {\n line: loc.line,\n column: loc.column + 1,\n },\n });\n err.code = \"BABEL_REPLACE_SOURCE_ERROR\";\n }\n throw err;\n }\n\n const expressionAST = (ast.program.body[0] as t.ExpressionStatement)\n .expression;\n traverse.removeProperties(expressionAST);\n return this.replaceWith(expressionAST);\n}\n\n/**\n * Replace the current node with another.\n */\n\nexport function replaceWith<R extends t.Node>(\n this: NodePath,\n replacementPath: R | NodePath<R>,\n): [NodePath<R>] {\n this.resync();\n\n if (this.removed) {\n throw new Error(\"You can't replace this node, we've already removed it\");\n }\n\n let replacement: t.Node =\n replacementPath instanceof NodePath\n ? replacementPath.node\n : replacementPath;\n\n if (!replacement) {\n throw new Error(\n \"You passed `path.replaceWith()` a falsy node, use `path.remove()` instead\",\n );\n }\n\n if (this.node === replacement) {\n return [this as NodePath<R>];\n }\n\n if (this.isProgram() && !isProgram(replacement)) {\n throw new Error(\n \"You can only replace a Program root node with another Program node\",\n );\n }\n\n if (Array.isArray(replacement)) {\n throw new Error(\n \"Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`\",\n );\n }\n\n if (typeof replacement === \"string\") {\n throw new Error(\n \"Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`\",\n );\n }\n\n let nodePath = \"\";\n\n if (this.isNodeType(\"Statement\") && isExpression(replacement)) {\n if (\n !this.canHaveVariableDeclarationOrExpression() &&\n !this.canSwapBetweenExpressionAndStatement(replacement) &&\n !this.parentPath.isExportDefaultDeclaration()\n ) {\n // replacing a statement with an expression so wrap it in an expression statement\n replacement = expressionStatement(replacement);\n nodePath = \"expression\";\n }\n }\n\n if (this.isNodeType(\"Expression\") && isStatement(replacement)) {\n if (\n !this.canHaveVariableDeclarationOrExpression() &&\n !this.canSwapBetweenExpressionAndStatement(replacement)\n ) {\n // replacing an expression with a statement so let's explode it\n return this.replaceExpressionWithStatements([replacement]) as [\n NodePath<R>,\n ];\n }\n }\n\n const oldNode = this.node;\n if (oldNode) {\n inheritsComments(replacement, oldNode);\n removeComments(oldNode);\n }\n\n // replace the node\n this._replaceWith(replacement);\n this.type = replacement.type;\n\n // potentially create new scope\n this.setScope();\n\n // requeue for visiting\n this.requeue();\n\n return [\n nodePath ? (this.get(nodePath) as NodePath<R>) : (this as NodePath<R>),\n ];\n}\n\n/**\n * Description\n */\n\nexport function _replaceWith(this: NodePath, node: t.Node) {\n if (!this.container) {\n throw new ReferenceError(\"Container is falsy\");\n }\n\n if (this.inList) {\n // @ts-expect-error todo(flow->ts): check if validate accepts a numeric key\n validate(this.parent, this.key, [node]);\n } else {\n validate(this.parent, this.key as string, node);\n }\n\n this.debug(`Replace with ${node?.type}`);\n getCachedPaths(this.hub, this.parent)?.set(node, this).delete(this.node);\n\n this.node =\n // @ts-expect-error this.key must present in this.container\n this.container[this.key] = node;\n}\n\n/**\n * This method takes an array of statements nodes and then explodes it\n * into expressions. This method retains completion records which is\n * extremely important to retain original semantics.\n */\n\nexport function replaceExpressionWithStatements(\n this: NodePath,\n nodes: Array<t.Statement>,\n) {\n this.resync();\n\n const nodesAsSequenceExpression = toSequenceExpression(nodes, this.scope);\n\n if (nodesAsSequenceExpression) {\n return this.replaceWith(nodesAsSequenceExpression)[0].get(\"expressions\");\n }\n\n const functionParent = this.getFunctionParent();\n const isParentAsync = functionParent?.is(\"async\");\n const isParentGenerator = functionParent?.is(\"generator\");\n\n const container = arrowFunctionExpression([], blockStatement(nodes));\n\n this.replaceWith(callExpression(container, []));\n // replaceWith changes the type of \"this\", but it isn't trackable by TS\n type ThisType = NodePath<\n t.CallExpression & {\n callee: t.ArrowFunctionExpression & { body: t.BlockStatement };\n }\n >;\n\n // hoist variable declaration in do block\n // `(do { var x = 1; x;})` -> `var x; (() => { x = 1; return x; })()`\n const callee = (this as ThisType).get(\"callee\");\n hoistVariables(\n callee.get(\"body\"),\n (id: t.Identifier) => {\n this.scope.push({ id });\n },\n \"var\",\n );\n\n // add implicit returns to all ending expression statements\n const completionRecords: Array<NodePath> = (this as ThisType)\n .get(\"callee\")\n .getCompletionRecords();\n for (const path of completionRecords) {\n if (!path.isExpressionStatement()) continue;\n\n const loop = path.findParent(path => path.isLoop());\n if (loop) {\n let uid = loop.getData(\"expressionReplacementReturnUid\");\n\n if (!uid) {\n uid = callee.scope.generateDeclaredUidIdentifier(\"ret\");\n callee\n .get(\"body\")\n .pushContainer(\"body\", returnStatement(cloneNode(uid)));\n loop.setData(\"expressionReplacementReturnUid\", uid);\n } else {\n uid = identifier(uid.name);\n }\n\n path\n .get(\"expression\")\n .replaceWith(\n assignmentExpression(\"=\", cloneNode(uid), path.node.expression),\n );\n } else {\n path.replaceWith(returnStatement(path.node.expression));\n }\n }\n\n // This is an IIFE, so we don't need to worry about the noNewArrows assumption\n callee.arrowFunctionToExpression();\n // Fixme: we can not `assert this is NodePath<t.FunctionExpression>` in `arrowFunctionToExpression`\n // because it is not a class method known at compile time.\n const newCallee = callee as unknown as NodePath<t.FunctionExpression>;\n\n // (() => await xxx)() -> await (async () => await xxx)();\n const needToAwaitFunction =\n isParentAsync &&\n traverse.hasType(\n (this.get(\"callee.body\") as NodePath<t.BlockStatement>).node,\n \"AwaitExpression\",\n FUNCTION_TYPES,\n );\n const needToYieldFunction =\n isParentGenerator &&\n traverse.hasType(\n (this.get(\"callee.body\") as NodePath<t.BlockStatement>).node,\n \"YieldExpression\",\n FUNCTION_TYPES,\n );\n if (needToAwaitFunction) {\n newCallee.set(\"async\", true);\n // yield* will await the generator return result\n if (!needToYieldFunction) {\n this.replaceWith(awaitExpression((this as ThisType).node));\n }\n }\n if (needToYieldFunction) {\n newCallee.set(\"generator\", true);\n this.replaceWith(yieldExpression((this as ThisType).node, true));\n }\n\n return newCallee.get(\"body.body\");\n}\n\nexport function replaceInline(this: NodePath, nodes: t.Node | Array<t.Node>) {\n this.resync();\n\n if (Array.isArray(nodes)) {\n if (Array.isArray(this.container)) {\n nodes = this._verifyNodeList(nodes);\n const paths = this._containerInsertAfter(nodes);\n this.remove();\n return paths;\n } else {\n return this.replaceWithMultiple(nodes);\n }\n } else {\n return this.replaceWith(nodes);\n }\n}\n"],"mappings":";;;;;;;;;;;AAEA,IAAAA,UAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,OAAA,GAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AACA,IAAAI,OAAA,GAAAJ,OAAA;AACA,IAAAK,EAAA,GAAAL,OAAA;AAuBA,IAAAM,qBAAA,GAAAN,OAAA;AAA2D;EAtBzDO,cAAc;EACdC,uBAAuB;EACvBC,oBAAoB;EACp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,EAAE;IACxB,IAAIuC,KAAK,CAACC,OAAO,CAAC,IAAI,CAAC9B,SAAS,CAAC,EAAE;MACjCV,KAAK,GAAG,IAAI,CAACG,eAAe,CAACH,KAAK,CAAC;MACnC,MAAMY,KAAK,GAAG,IAAI,CAAC4E,qBAAqB,CAACxF,KAAK,CAAC;MAC/C,IAAI,CAACe,MAAM,CAAC,CAAC;MACb,OAAOH,KAAK;IACd,CAAC,MAAM;MACL,OAAO,IAAI,CAACb,mBAAmB,CAACC,KAAK,CAAC;IACxC;EACF,CAAC,MAAM;IACL,OAAO,IAAI,CAACkC,WAAW,CAAClC,KAAK,CAAC;EAChC;AACF"}
|
1
|
+
{"version":3,"names":["codeFrameColumns","traverse","NodePath","path","pathCache","parse","_t","FUNCTION_TYPES","arrowFunctionExpression","assignmentExpression","awaitExpression","blockStatement","callExpression","cloneNode","expressionStatement","identifier","inheritLeadingComments","inheritTrailingComments","inheritsComments","isExpression","isProgram","isStatement","removeComments","returnStatement","toSequenceExpression","validate","yieldExpression","hoistVariables","replaceWithMultiple","nodes","resync","_verifyNodeList","node","length","get","parent","delete","container","key","paths","insertAfter","requeue","remove","replaceWithSourceString","replacement","ast","err","loc","message","start","line","column","code","expressionAST","program","body","expression","removeProperties","replaceWith","replacementPath","removed","Error","Array","isArray","nodePath","isNodeType","canHaveVariableDeclarationOrExpression","canSwapBetweenExpressionAndStatement","parentPath","isExportDefaultDeclaration","replaceExpressionWithStatements","oldNode","_replaceWith","type","setScope","ReferenceError","inList","debug","set","nodesAsSequenceExpression","scope","functionParent","getFunctionParent","isParentAsync","is","isParentGenerator","callee","id","push","completionRecords","getCompletionRecords","isExpressionStatement","loop","findParent","isLoop","uid","getData","generateDeclaredUidIdentifier","pushContainer","setData","name","arrowFunctionToExpression","newCallee","needToAwaitFunction","hasType","needToYieldFunction","replaceInline","_containerInsertAfter"],"sources":["../../src/path/replacement.ts"],"sourcesContent":["// This file contains methods responsible for replacing a node with another.\n\nimport { codeFrameColumns } from \"@babel/code-frame\";\nimport traverse from \"../index\";\nimport NodePath from \"./index\";\nimport { path as pathCache } from \"../cache\";\nimport { parse } from \"@babel/parser\";\nimport {\n FUNCTION_TYPES,\n arrowFunctionExpression,\n assignmentExpression,\n awaitExpression,\n blockStatement,\n callExpression,\n cloneNode,\n expressionStatement,\n identifier,\n inheritLeadingComments,\n inheritTrailingComments,\n inheritsComments,\n isExpression,\n isProgram,\n isStatement,\n removeComments,\n returnStatement,\n toSequenceExpression,\n validate,\n yieldExpression,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport hoistVariables from \"@babel/helper-hoist-variables\";\n\n/**\n * Replace a node with an array of multiple. This method performs the following steps:\n *\n * - Inherit the comments of first provided node with that of the current node.\n * - Insert the provided nodes after the current node.\n * - Remove the current node.\n */\n\nexport function replaceWithMultiple(\n this: NodePath,\n nodes: t.Node | t.Node[],\n): NodePath[] {\n this.resync();\n\n nodes = this._verifyNodeList(nodes);\n inheritLeadingComments(nodes[0], this.node);\n inheritTrailingComments(nodes[nodes.length - 1], this.node);\n pathCache.get(this.parent)?.delete(this.node);\n this.node =\n // @ts-expect-error this.key must present in this.container\n this.container[this.key] = null;\n const paths = this.insertAfter(nodes);\n\n if (this.node) {\n this.requeue();\n } else {\n this.remove();\n }\n return paths;\n}\n\n/**\n * Parse a string as an expression and replace the current node with the result.\n *\n * NOTE: This is typically not a good idea to use. Building source strings when\n * transforming ASTs is an antipattern and SHOULD NOT be encouraged. Even if it's\n * easier to use, your transforms will be extremely brittle.\n */\n\nexport function replaceWithSourceString(this: NodePath, replacement: string) {\n this.resync();\n let ast: t.File;\n\n try {\n replacement = `(${replacement})`;\n // @ts-expect-error todo: use babel-types ast typings in Babel parser\n ast = parse(replacement);\n } catch (err) {\n const loc = err.loc;\n if (loc) {\n err.message +=\n \" - make sure this is an expression.\\n\" +\n codeFrameColumns(replacement, {\n start: {\n line: loc.line,\n column: loc.column + 1,\n },\n });\n err.code = \"BABEL_REPLACE_SOURCE_ERROR\";\n }\n throw err;\n }\n\n const expressionAST = (ast.program.body[0] as t.ExpressionStatement)\n .expression;\n traverse.removeProperties(expressionAST);\n return this.replaceWith(expressionAST);\n}\n\n/**\n * Replace the current node with another.\n */\n\nexport function replaceWith<R extends t.Node>(\n this: NodePath,\n replacementPath: R | NodePath<R>,\n): [NodePath<R>] {\n this.resync();\n\n if (this.removed) {\n throw new Error(\"You can't replace this node, we've already removed it\");\n }\n\n let replacement: t.Node =\n replacementPath instanceof NodePath\n ? replacementPath.node\n : replacementPath;\n\n if (!replacement) {\n throw new Error(\n \"You passed `path.replaceWith()` a falsy node, use `path.remove()` instead\",\n );\n }\n\n if (this.node === replacement) {\n return [this as NodePath<R>];\n }\n\n if (this.isProgram() && !isProgram(replacement)) {\n throw new Error(\n \"You can only replace a Program root node with another Program node\",\n );\n }\n\n if (Array.isArray(replacement)) {\n throw new Error(\n \"Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`\",\n );\n }\n\n if (typeof replacement === \"string\") {\n throw new Error(\n \"Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`\",\n );\n }\n\n let nodePath = \"\";\n\n if (this.isNodeType(\"Statement\") && isExpression(replacement)) {\n if (\n !this.canHaveVariableDeclarationOrExpression() &&\n !this.canSwapBetweenExpressionAndStatement(replacement) &&\n !this.parentPath.isExportDefaultDeclaration()\n ) {\n // replacing a statement with an expression so wrap it in an expression statement\n replacement = expressionStatement(replacement);\n nodePath = \"expression\";\n }\n }\n\n if (this.isNodeType(\"Expression\") && isStatement(replacement)) {\n if (\n !this.canHaveVariableDeclarationOrExpression() &&\n !this.canSwapBetweenExpressionAndStatement(replacement)\n ) {\n // replacing an expression with a statement so let's explode it\n return this.replaceExpressionWithStatements([replacement]) as [\n NodePath<R>,\n ];\n }\n }\n\n const oldNode = this.node;\n if (oldNode) {\n inheritsComments(replacement, oldNode);\n removeComments(oldNode);\n }\n\n // replace the node\n this._replaceWith(replacement);\n this.type = replacement.type;\n\n // potentially create new scope\n this.setScope();\n\n // requeue for visiting\n this.requeue();\n\n return [\n nodePath ? (this.get(nodePath) as NodePath<R>) : (this as NodePath<R>),\n ];\n}\n\n/**\n * Description\n */\n\nexport function _replaceWith(this: NodePath, node: t.Node) {\n if (!this.container) {\n throw new ReferenceError(\"Container is falsy\");\n }\n\n if (this.inList) {\n // @ts-expect-error todo(flow->ts): check if validate accepts a numeric key\n validate(this.parent, this.key, [node]);\n } else {\n validate(this.parent, this.key as string, node);\n }\n\n this.debug(`Replace with ${node?.type}`);\n pathCache.get(this.parent)?.set(node, this).delete(this.node);\n\n this.node =\n // @ts-expect-error this.key must present in this.container\n this.container[this.key] = node;\n}\n\n/**\n * This method takes an array of statements nodes and then explodes it\n * into expressions. This method retains completion records which is\n * extremely important to retain original semantics.\n */\n\nexport function replaceExpressionWithStatements(\n this: NodePath,\n nodes: Array<t.Statement>,\n) {\n this.resync();\n\n const nodesAsSequenceExpression = toSequenceExpression(nodes, this.scope);\n\n if (nodesAsSequenceExpression) {\n return this.replaceWith(nodesAsSequenceExpression)[0].get(\"expressions\");\n }\n\n const functionParent = this.getFunctionParent();\n const isParentAsync = functionParent?.is(\"async\");\n const isParentGenerator = functionParent?.is(\"generator\");\n\n const container = arrowFunctionExpression([], blockStatement(nodes));\n\n this.replaceWith(callExpression(container, []));\n // replaceWith changes the type of \"this\", but it isn't trackable by TS\n type ThisType = NodePath<\n t.CallExpression & {\n callee: t.ArrowFunctionExpression & { body: t.BlockStatement };\n }\n >;\n\n // hoist variable declaration in do block\n // `(do { var x = 1; x;})` -> `var x; (() => { x = 1; return x; })()`\n const callee = (this as ThisType).get(\"callee\");\n hoistVariables(\n callee.get(\"body\"),\n (id: t.Identifier) => {\n this.scope.push({ id });\n },\n \"var\",\n );\n\n // add implicit returns to all ending expression statements\n const completionRecords: Array<NodePath> = (this as ThisType)\n .get(\"callee\")\n .getCompletionRecords();\n for (const path of completionRecords) {\n if (!path.isExpressionStatement()) continue;\n\n const loop = path.findParent(path => path.isLoop());\n if (loop) {\n let uid = loop.getData(\"expressionReplacementReturnUid\");\n\n if (!uid) {\n uid = callee.scope.generateDeclaredUidIdentifier(\"ret\");\n callee\n .get(\"body\")\n .pushContainer(\"body\", returnStatement(cloneNode(uid)));\n loop.setData(\"expressionReplacementReturnUid\", uid);\n } else {\n uid = identifier(uid.name);\n }\n\n path\n .get(\"expression\")\n .replaceWith(\n assignmentExpression(\"=\", cloneNode(uid), path.node.expression),\n );\n } else {\n path.replaceWith(returnStatement(path.node.expression));\n }\n }\n\n // This is an IIFE, so we don't need to worry about the noNewArrows assumption\n callee.arrowFunctionToExpression();\n // Fixme: we can not `assert this is NodePath<t.FunctionExpression>` in `arrowFunctionToExpression`\n // because it is not a class method known at compile time.\n const newCallee = callee as unknown as NodePath<t.FunctionExpression>;\n\n // (() => await xxx)() -> await (async () => await xxx)();\n const needToAwaitFunction =\n isParentAsync &&\n traverse.hasType(\n (this.get(\"callee.body\") as NodePath<t.BlockStatement>).node,\n \"AwaitExpression\",\n FUNCTION_TYPES,\n );\n const needToYieldFunction =\n isParentGenerator &&\n traverse.hasType(\n (this.get(\"callee.body\") as NodePath<t.BlockStatement>).node,\n \"YieldExpression\",\n FUNCTION_TYPES,\n );\n if (needToAwaitFunction) {\n newCallee.set(\"async\", true);\n // yield* will await the generator return result\n if (!needToYieldFunction) {\n this.replaceWith(awaitExpression((this as ThisType).node));\n }\n }\n if (needToYieldFunction) {\n newCallee.set(\"generator\", true);\n this.replaceWith(yieldExpression((this as ThisType).node, true));\n }\n\n return newCallee.get(\"body.body\");\n}\n\nexport function replaceInline(this: NodePath, nodes: t.Node | Array<t.Node>) {\n this.resync();\n\n if (Array.isArray(nodes)) {\n if (Array.isArray(this.container)) {\n nodes = this._verifyNodeList(nodes);\n const paths = this._containerInsertAfter(nodes);\n this.remove();\n return paths;\n } else {\n return this.replaceWithMultiple(nodes);\n }\n } else {\n return this.replaceWith(nodes);\n }\n}\n"],"mappings":"AAEA,SAASA,gBAAgB,QAA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