@babel/traverse 7.25.3 → 7.25.6
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/context.js +4 -3
- package/lib/context.js.map +1 -1
- package/lib/path/context.js +10 -7
- package/lib/path/context.js.map +1 -1
- package/lib/path/conversion.js +2 -1
- package/lib/path/conversion.js.map +1 -1
- package/lib/path/index.js +19 -18
- package/lib/path/index.js.map +1 -1
- package/lib/path/introspection.js +18 -18
- package/lib/path/introspection.js.map +1 -1
- package/lib/path/lib/hoister.js.map +1 -1
- package/lib/path/modification.js +8 -7
- package/lib/path/modification.js.map +1 -1
- package/lib/path/removal.js +4 -2
- package/lib/path/removal.js.map +1 -1
- package/lib/path/replacement.js +9 -8
- package/lib/path/replacement.js.map +1 -1
- package/lib/scope/index.js +2 -2
- package/lib/scope/index.js.map +1 -1
- package/lib/scope/lib/renamer.js +16 -3
- package/lib/scope/lib/renamer.js.map +1 -1
- package/lib/visitors.js +1 -1
- package/package.json +4 -4
- package/tsconfig.json +29 -0
- package/tsconfig.tsbuildinfo +1 -0
package/lib/path/modification.js
CHANGED
@@ -7,7 +7,6 @@ exports._containerInsert = _containerInsert;
|
|
7
7
|
exports._containerInsertAfter = _containerInsertAfter;
|
8
8
|
exports._containerInsertBefore = _containerInsertBefore;
|
9
9
|
exports._verifyNodeList = _verifyNodeList;
|
10
|
-
exports.hoist = hoist;
|
11
10
|
exports.insertAfter = insertAfter;
|
12
11
|
exports.insertBefore = insertBefore;
|
13
12
|
exports.pushContainer = pushContainer;
|
@@ -60,7 +59,7 @@ function insertBefore(nodes_) {
|
|
60
59
|
}
|
61
60
|
}
|
62
61
|
function _containerInsert(from, nodes) {
|
63
|
-
|
62
|
+
updateSiblingKeys.call(this, from, nodes.length);
|
64
63
|
const paths = [];
|
65
64
|
this.container.splice(from, 0, ...nodes);
|
66
65
|
for (let i = 0; i < nodes.length; i++) {
|
@@ -69,12 +68,12 @@ function _containerInsert(from, nodes) {
|
|
69
68
|
const path = this.getSibling(to);
|
70
69
|
paths.push(path);
|
71
70
|
if ((_this$context = this.context) != null && _this$context.queue) {
|
72
|
-
|
71
|
+
_context.pushContext.call(path, this.context);
|
73
72
|
}
|
74
73
|
}
|
75
74
|
const contexts = _context._getQueueContexts.call(this);
|
76
75
|
for (const path of paths) {
|
77
|
-
|
76
|
+
_context.setScope.call(path);
|
78
77
|
path.debug("Inserted.");
|
79
78
|
for (const context of contexts) {
|
80
79
|
context.maybeQueue(path, true);
|
@@ -220,9 +219,11 @@ function pushContainer(listKey, nodes) {
|
|
220
219
|
}).setContext(this.context);
|
221
220
|
return path.replaceWithMultiple(verifiedNodes);
|
222
221
|
}
|
223
|
-
|
224
|
-
|
225
|
-
|
222
|
+
{
|
223
|
+
exports.hoist = function hoist(scope = this.scope) {
|
224
|
+
const hoister = new _hoister.default(this, scope);
|
225
|
+
return hoister.run();
|
226
|
+
};
|
226
227
|
}
|
227
228
|
|
228
229
|
//# sourceMappingURL=modification.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_cache","require","_hoister","_index","_context","_removal","_t","arrowFunctionExpression","assertExpression","assignmentExpression","blockStatement","callExpression","cloneNode","expressionStatement","isAssignmentExpression","isCallExpression","isExportNamedDeclaration","isExpression","isIdentifier","isSequenceExpression","isSuper","thisExpression","insertBefore","nodes_","_assertUnremoved","call","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","_this$context","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","self","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.ts\";\nimport PathHoister from \"./lib/hoister.ts\";\nimport NodePath from \"./index.ts\";\nimport { _getQueueContexts } from \"./context.ts\";\nimport { _assertUnremoved } from \"./removal.ts\";\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/index.ts\";\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 _assertUnremoved.call(this);\n\n const nodes = _verifyNodeList.call(this, 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 _containerInsertBefore.call(this, 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 = _getQueueContexts.call(this);\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 _containerInsert.call(this, this.key as number, nodes);\n}\n\nexport function _containerInsertAfter<N extends t.Node>(\n this: NodePath,\n nodes: N[],\n) {\n return _containerInsert.call(this, (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 _assertUnremoved.call(this);\n\n if (this.isSequenceExpression()) {\n return last(this.get(\"expressions\")).insertAfter(nodes_);\n }\n\n const nodes = _verifyNodeList.call(this, 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 const self = this as NodePath<t.Expression | t.VariableDeclaration>;\n if (self.node) {\n const node = self.node;\n let { scope } = this;\n\n if (scope.path.isPattern()) {\n assertExpression(node);\n\n self.replaceWith(callExpression(arrowFunctionExpression([], node), []));\n (self.get(\"callee.body\") as NodePath<t.Expression>).insertAfter(nodes);\n return [self];\n }\n\n if (isHiddenInSequenceExpression(self)) {\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 _containerInsertAfter.call(this, 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 _assertUnremoved.call(this);\n\n // @ts-expect-error fixme\n nodes = _verifyNodeList.call(this, 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 as N)[listKey] as unknown as t.Node | t.Node[],\n listKey,\n key: 0,\n }).setContext(this.context);\n\n return _containerInsertBefore.call(\n path,\n // @ts-expect-error typings needed to narrow down nodes as t.Node[]\n nodes,\n );\n}\n\nexport function pushContainer<\n P extends NodePath,\n K extends string & keyof P[\"node\"],\n>(\n this: P,\n listKey: K,\n nodes: P[\"node\"][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 _assertUnremoved.call(this);\n\n const verifiedNodes = _verifyNodeList.call(\n this,\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 as P[\"node\"])[listKey] as t.Node[];\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 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,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AACA,IAAAG,QAAA,GAAAH,OAAA;AACA,IAAAI,QAAA,GAAAJ,OAAA;AACA,IAAAK,EAAA,GAAAL,OAAA;AAgBsB;EAfpBM,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;AAST,SAASgB,YAAYA,CAE1BC,MAAyB,EACb;EACZC,yBAAgB,CAACC,IAAI,CAAC,IAAI,CAAC;EAE3B,MAAMC,KAAK,GAAGC,eAAe,CAACF,IAAI,CAAC,IAAI,EAAEF,MAAM,CAAC;EAEhD,MAAM;IAAEK,UAAU;IAAEC;EAAO,CAAC,GAAG,IAAI;EAEnC,IACED,UAAU,CAACE,qBAAqB,CAAC,CAAC,IAClCF,UAAU,CAACG,kBAAkB,CAAC,CAAC,IAQ/Bf,wBAAwB,CAACa,MAAM,CAAC,IAC/BD,UAAU,CAACI,0BAA0B,CAAC,CAAC,IAAI,IAA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IACxBzD,UAAU,EAAE,IAAI;IAChBC,MAAM,EAAE,IAAI,CAACS,IAAI;IACjBK,SAAS,EAAEA,SAAyC;IACpD6D,OAAO;IACPnE,GAAG,EAAEM,SAAS,CAACW;EACjB,CAAC,CAAC,CAACmD,UAAU,CAAC,IAAI,CAAC3C,OAAO,CAAC;EAE3B,OAAOF,IAAI,CAAC+C,mBAAmB,CAACD,aAAa,CAAC;AAChD;AAMO,SAASE,KAAKA,CAEnBhC,KAAY,GAAG,IAAI,CAACA,KAAK,EACzB;EACA,MAAMiC,OAAO,GAAG,IAAIC,gBAAW,CAAI,IAAI,EAAElC,KAAK,CAAC;EAC/C,OAAOiC,OAAO,CAACE,GAAG,CAAC,CAAC;AACtB","ignoreList":[]}
|
1
|
+
{"version":3,"names":["_cache","require","_hoister","_index","_context","_removal","_t","arrowFunctionExpression","assertExpression","assignmentExpression","blockStatement","callExpression","cloneNode","expressionStatement","isAssignmentExpression","isCallExpression","isExportNamedDeclaration","isExpression","isIdentifier","isSequenceExpression","isSuper","thisExpression","insertBefore","nodes_","_assertUnremoved","call","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","_this$context","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","self","isPattern","unshift","callee","isPure","isMethod","computed","temp","generateDeclaredUidIdentifier","pushContainer","fromIndex","incrementBy","getCachedPaths","hub","msg","type","NodePath","listKey","setContext","verifiedNodes","replaceWithMultiple","exports","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.ts\";\nimport PathHoister from \"./lib/hoister.ts\";\nimport NodePath from \"./index.ts\";\nimport { _getQueueContexts, pushContext, setScope } from \"./context.ts\";\nimport { _assertUnremoved } from \"./removal.ts\";\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/index.ts\";\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 _assertUnremoved.call(this);\n\n const nodes = _verifyNodeList.call(this, 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 _containerInsertBefore.call(this, 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 updateSiblingKeys.call(this, 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 pushContext.call(path, this.context);\n }\n }\n\n const contexts = _getQueueContexts.call(this);\n\n for (const path of paths) {\n setScope.call(path);\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 _containerInsert.call(this, this.key as number, nodes);\n}\n\nexport function _containerInsertAfter<N extends t.Node>(\n this: NodePath,\n nodes: N[],\n) {\n return _containerInsert.call(this, (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 _assertUnremoved.call(this);\n\n if (this.isSequenceExpression()) {\n return last(this.get(\"expressions\")).insertAfter(nodes_);\n }\n\n const nodes = _verifyNodeList.call(this, 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 const self = this as NodePath<t.Expression | t.VariableDeclaration>;\n if (self.node) {\n const node = self.node;\n let { scope } = this;\n\n if (scope.path.isPattern()) {\n assertExpression(node);\n\n self.replaceWith(callExpression(arrowFunctionExpression([], node), []));\n (self.get(\"callee.body\") as NodePath<t.Expression>).insertAfter(nodes);\n return [self];\n }\n\n if (isHiddenInSequenceExpression(self)) {\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 _containerInsertAfter.call(this, 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 _assertUnremoved.call(this);\n\n // @ts-expect-error fixme\n nodes = _verifyNodeList.call(this, 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 as N)[listKey] as unknown as t.Node | t.Node[],\n listKey,\n key: 0,\n }).setContext(this.context);\n\n return _containerInsertBefore.call(\n path,\n // @ts-expect-error typings needed to narrow down nodes as t.Node[]\n nodes,\n );\n}\n\nexport function pushContainer<\n P extends NodePath,\n K extends string & keyof P[\"node\"],\n>(\n this: P,\n listKey: K,\n nodes: P[\"node\"][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 _assertUnremoved.call(this);\n\n const verifiedNodes = _verifyNodeList.call(\n this,\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 as P[\"node\"])[listKey] as t.Node[];\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 key: container.length,\n }).setContext(this.context);\n\n return path.replaceWithMultiple(verifiedNodes);\n}\n\nif (!process.env.BABEL_8_BREAKING && !USE_ESM) {\n /**\n * Hoist the current node to the highest scope possible and return a UID\n * referencing it.\n */\n // eslint-disable-next-line no-restricted-globals\n exports.hoist = 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}\n"],"mappings":";;;;;;;;;;;;;;AAEA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AACA,IAAAG,QAAA,GAAAH,OAAA;AACA,IAAAI,QAAA,GAAAJ,OAAA;AACA,IAAAK,EAAA,GAAAL,OAAA;AAgBsB;EAfpBM,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;AAST,SAASgB,YAAYA,CAE1BC,MAAyB,EACb;EACZC,yBAAgB,CAACC,IAAI,CAAC,IAAI,CAAC;EAE3B,MAAMC,KAAK,GAAGC,eAAe,CAACF,IAAI,CAAC,IAAI,EAAEF,MAAM,CAAC;EAEhD,MAAM;IAAEK,UAAU;IAAEC;EAAO,CAAC,GAAG,IAAI;EAEnC,IACED,UAAU,CAACE,qBAAqB,CAAC,CAAC,IAClCF,UAAU,CAACG,kBAAkB,CAAC,CAAC,IAQ/Bf,wBAAwB,CAACa,MAAM,CAAC,IAC/BD,UAAU,CAACI,0BAA0B,CAAC,CAAC,IAAI,IAAI,CAACC,aAAa,CAAC,CAAE,EACjE;IACA,OAAOL,UAAU,CAACN,YAAY,CAACI,KAAK,CAAC;EACvC,CA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,IAAI,CAACS,IAAI;IACjBK,SAAS,EAAEA,SAAyC;IACpD6D,OAAO;IACPnE,GAAG,EAAEM,SAAS,CAACW;EACjB,CAAC,CAAC,CAACmD,UAAU,CAAC,IAAI,CAAC3C,OAAO,CAAC;EAE3B,OAAOF,IAAI,CAAC+C,mBAAmB,CAACD,aAAa,CAAC;AAChD;AAE+C;EAM7CE,OAAO,CAACC,KAAK,GAAG,SAASA,KAAKA,CAE5BjC,KAAY,GAAG,IAAI,CAACA,KAAK,EACzB;IACA,MAAMkC,OAAO,GAAG,IAAIC,gBAAW,CAAI,IAAI,EAAEnC,KAAK,CAAC;IAC/C,OAAOkC,OAAO,CAACE,GAAG,CAAC,CAAC;EACtB,CAAC;AACH","ignoreList":[]}
|
package/lib/path/removal.js
CHANGED
@@ -14,13 +14,15 @@ var _cache = require("../cache.js");
|
|
14
14
|
var _replacement = require("./replacement.js");
|
15
15
|
var _index = require("./index.js");
|
16
16
|
var _t = require("@babel/types");
|
17
|
+
var _modification = require("./modification.js");
|
18
|
+
var _context = require("./context.js");
|
17
19
|
const {
|
18
20
|
getBindingIdentifiers
|
19
21
|
} = _t;
|
20
22
|
function remove() {
|
21
23
|
var _this$opts;
|
22
24
|
_assertUnremoved.call(this);
|
23
|
-
|
25
|
+
_context.resync.call(this);
|
24
26
|
if (_callRemovalHooks.call(this)) {
|
25
27
|
_markRemoved.call(this);
|
26
28
|
return;
|
@@ -46,7 +48,7 @@ function _callRemovalHooks() {
|
|
46
48
|
function _remove() {
|
47
49
|
if (Array.isArray(this.container)) {
|
48
50
|
this.container.splice(this.key, 1);
|
49
|
-
|
51
|
+
_modification.updateSiblingKeys.call(this, this.key, -1);
|
50
52
|
} else {
|
51
53
|
_replacement._replaceWith.call(this, null);
|
52
54
|
}
|
package/lib/path/removal.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_removalHooks","require","_cache","_replacement","_index","_t","getBindingIdentifiers","remove","_this$opts","_assertUnremoved","call","resync","_callRemovalHooks","_markRemoved","opts","noScope","_removeFromScope","shareCommentsWithSiblings","_remove","bindings","node","Object","keys","forEach","name","scope","removeBinding","parentPath","fn","hooks","Array","isArray","container","splice","key","updateSiblingKeys","_replaceWith","_traverseFlags","SHOULD_SKIP","REMOVED","parent","getCachedPaths","hub","delete","removed","buildCodeFrameError"],"sources":["../../src/path/removal.ts"],"sourcesContent":["// This file contains methods responsible for removing a node.\n\nimport { hooks } from \"./lib/removal-hooks.ts\";\nimport { getCachedPaths } from \"../cache.ts\";\nimport { _replaceWith } from \"./replacement.ts\";\nimport type NodePath from \"./index.ts\";\nimport { REMOVED, SHOULD_SKIP } from \"./index.ts\";\nimport { getBindingIdentifiers } from \"@babel/types\";\n\nexport function remove(this: NodePath) {\n _assertUnremoved.call(this);\n\n
|
1
|
+
{"version":3,"names":["_removalHooks","require","_cache","_replacement","_index","_t","_modification","_context","getBindingIdentifiers","remove","_this$opts","_assertUnremoved","call","resync","_callRemovalHooks","_markRemoved","opts","noScope","_removeFromScope","shareCommentsWithSiblings","_remove","bindings","node","Object","keys","forEach","name","scope","removeBinding","parentPath","fn","hooks","Array","isArray","container","splice","key","updateSiblingKeys","_replaceWith","_traverseFlags","SHOULD_SKIP","REMOVED","parent","getCachedPaths","hub","delete","removed","buildCodeFrameError"],"sources":["../../src/path/removal.ts"],"sourcesContent":["// This file contains methods responsible for removing a node.\n\nimport { hooks } from \"./lib/removal-hooks.ts\";\nimport { getCachedPaths } from \"../cache.ts\";\nimport { _replaceWith } from \"./replacement.ts\";\nimport type NodePath from \"./index.ts\";\nimport { REMOVED, SHOULD_SKIP } from \"./index.ts\";\nimport { getBindingIdentifiers } from \"@babel/types\";\nimport { updateSiblingKeys } from \"./modification.ts\";\nimport { resync } from \"./context.ts\";\n\nexport function remove(this: NodePath) {\n _assertUnremoved.call(this);\n\n resync.call(this);\n\n if (_callRemovalHooks.call(this)) {\n _markRemoved.call(this);\n return;\n }\n\n if (!this.opts?.noScope) {\n _removeFromScope.call(this);\n }\n\n this.shareCommentsWithSiblings();\n _remove.call(this);\n _markRemoved.call(this);\n}\n\nexport function _removeFromScope(this: NodePath) {\n const bindings = getBindingIdentifiers(this.node, false, false, true);\n Object.keys(bindings).forEach(name => this.scope.removeBinding(name));\n}\n\nexport function _callRemovalHooks(this: NodePath) {\n if (this.parentPath) {\n for (const fn of hooks) {\n if (fn(this, this.parentPath)) return true;\n }\n }\n}\n\nexport function _remove(this: NodePath) {\n if (Array.isArray(this.container)) {\n this.container.splice(this.key as number, 1);\n updateSiblingKeys.call(this, this.key as number, -1);\n } else {\n _replaceWith.call(this, 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) {\n getCachedPaths(this.hub, this.parent).delete(this.node);\n }\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,IAAAA,aAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,YAAA,GAAAF,OAAA;AAEA,IAAAG,MAAA,GAAAH,OAAA;AACA,IAAAI,EAAA,GAAAJ,OAAA;AACA,IAAAK,aAAA,GAAAL,OAAA;AACA,IAAAM,QAAA,GAAAN,OAAA;AAAsC;EAF7BO;AAAqB,IAAAH,EAAA;AAIvB,SAASI,MAAMA,CAAA,EAAiB;EAAA,IAAAC,UAAA;EACrCC,gBAAgB,CAACC,IAAI,CAAC,IAAI,CAAC;EAE3BC,eAAM,CAACD,IAAI,CAAC,IAAI,CAAC;EAEjB,IAAIE,iBAAiB,CAACF,IAAI,CAAC,IAAI,CAAC,EAAE;IAChCG,YAAY,CAACH,IAAI,CAAC,IAAI,CAAC;IACvB;EACF;EAEA,IAAI,GAAAF,UAAA,GAAC,IAAI,CAACM,IAAI,aAATN,UAAA,CAAW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,CAAC,CAACG,MAAM,CAAC,IAAI,CAACvB,IAAI,CAAC;EACzD;EACA,IAAI,CAACA,IAAI,GAAG,IAAI;AAClB;AAEO,SAASX,gBAAgBA,CAAA,EAAiB;EAC/C,IAAI,IAAI,CAACmC,OAAO,EAAE;IAChB,MAAM,IAAI,CAACC,mBAAmB,CAC5B,4CACF,CAAC;EACH;AACF","ignoreList":[]}
|
package/lib/path/replacement.js
CHANGED
@@ -16,6 +16,7 @@ var _cache = require("../cache.js");
|
|
16
16
|
var _modification = require("./modification.js");
|
17
17
|
var _parser = require("@babel/parser");
|
18
18
|
var _t = require("@babel/types");
|
19
|
+
var _context = require("./context.js");
|
19
20
|
const {
|
20
21
|
FUNCTION_TYPES,
|
21
22
|
arrowFunctionExpression,
|
@@ -48,7 +49,7 @@ const {
|
|
48
49
|
} = _t;
|
49
50
|
function replaceWithMultiple(nodes) {
|
50
51
|
var _getCachedPaths;
|
51
|
-
|
52
|
+
_context.resync.call(this);
|
52
53
|
nodes = _modification._verifyNodeList.call(this, nodes);
|
53
54
|
inheritLeadingComments(nodes[0], this.node);
|
54
55
|
inheritTrailingComments(nodes[nodes.length - 1], this.node);
|
@@ -63,7 +64,7 @@ function replaceWithMultiple(nodes) {
|
|
63
64
|
return paths;
|
64
65
|
}
|
65
66
|
function replaceWithSourceString(replacement) {
|
66
|
-
|
67
|
+
_context.resync.call(this);
|
67
68
|
let ast;
|
68
69
|
try {
|
69
70
|
replacement = `(${replacement})`;
|
@@ -86,7 +87,7 @@ function replaceWithSourceString(replacement) {
|
|
86
87
|
return this.replaceWith(expressionAST);
|
87
88
|
}
|
88
89
|
function replaceWith(replacementPath) {
|
89
|
-
|
90
|
+
_context.resync.call(this);
|
90
91
|
if (this.removed) {
|
91
92
|
throw new Error("You can't replace this node, we've already removed it");
|
92
93
|
}
|
@@ -125,7 +126,7 @@ function replaceWith(replacementPath) {
|
|
125
126
|
}
|
126
127
|
_replaceWith.call(this, replacement);
|
127
128
|
this.type = replacement.type;
|
128
|
-
|
129
|
+
_context.setScope.call(this);
|
129
130
|
this.requeue();
|
130
131
|
return [nodePath ? this.get(nodePath) : this];
|
131
132
|
}
|
@@ -144,7 +145,7 @@ function _replaceWith(node) {
|
|
144
145
|
this.node = this.container[this.key] = node;
|
145
146
|
}
|
146
147
|
function replaceExpressionWithStatements(nodes) {
|
147
|
-
|
148
|
+
_context.resync.call(this);
|
148
149
|
const declars = [];
|
149
150
|
const nodesAsSingleExpression = gatherSequenceExpressions(nodes, declars);
|
150
151
|
if (nodesAsSingleExpression) {
|
@@ -154,8 +155,8 @@ function replaceExpressionWithStatements(nodes) {
|
|
154
155
|
return this.replaceWith(nodesAsSingleExpression)[0].get("expressions");
|
155
156
|
}
|
156
157
|
const functionParent = this.getFunctionParent();
|
157
|
-
const isParentAsync = functionParent == null ? void 0 : functionParent.
|
158
|
-
const isParentGenerator = functionParent == null ? void 0 : functionParent.
|
158
|
+
const isParentAsync = functionParent == null ? void 0 : functionParent.node.async;
|
159
|
+
const isParentGenerator = functionParent == null ? void 0 : functionParent.node.generator;
|
159
160
|
const container = arrowFunctionExpression([], blockStatement(nodes));
|
160
161
|
this.replaceWith(callExpression(container, []));
|
161
162
|
const callee = this.get("callee");
|
@@ -244,7 +245,7 @@ function gatherSequenceExpressions(nodes, declars) {
|
|
244
245
|
}
|
245
246
|
}
|
246
247
|
function replaceInline(nodes) {
|
247
|
-
|
248
|
+
_context.resync.call(this);
|
248
249
|
if (Array.isArray(nodes)) {
|
249
250
|
if (Array.isArray(this.container)) {
|
250
251
|
nodes = _modification._verifyNodeList.call(this, nodes);
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_codeFrame","require","_index","_index2","_cache","_modification","_parser","_t","FUNCTION_TYPES","arrowFunctionExpression","assignmentExpression","awaitExpression","blockStatement","buildUndefinedNode","callExpression","cloneNode","conditionalExpression","expressionStatement","getBindingIdentifiers","identifier","inheritLeadingComments","inheritTrailingComments","inheritsComments","isBlockStatement","isEmptyStatement","isExpression","isExpressionStatement","isIfStatement","isProgram","isStatement","isVariableDeclaration","removeComments","returnStatement","sequenceExpression","validate","yieldExpression","replaceWithMultiple","nodes","_getCachedPaths","resync","_verifyNodeList","call","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","declars","nodesAsSingleExpression","gatherSequenceExpressions","id","scope","push","functionParent","getFunctionParent","isParentAsync","is","isParentGenerator","callee","hoistVariables","completionRecords","getCompletionRecords","path","loop","findParent","isLoop","uid","getData","generateDeclaredUidIdentifier","pushContainer","setData","name","arrowFunctionToExpression","newCallee","needToAwaitFunction","hasType","needToYieldFunction","exprs","ensureLastUndefined","kind","declar","declarations","bindings","Object","keys","init","consequent","alternate","test","indexOf","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.ts\";\nimport NodePath from \"./index.ts\";\nimport { getCachedPaths } from \"../cache.ts\";\nimport { _verifyNodeList, _containerInsertAfter } from \"./modification.ts\";\nimport { parse } from \"@babel/parser\";\nimport {\n FUNCTION_TYPES,\n arrowFunctionExpression,\n assignmentExpression,\n awaitExpression,\n blockStatement,\n buildUndefinedNode,\n callExpression,\n cloneNode,\n conditionalExpression,\n expressionStatement,\n getBindingIdentifiers,\n identifier,\n inheritLeadingComments,\n inheritTrailingComments,\n inheritsComments,\n isBlockStatement,\n isEmptyStatement,\n isExpression,\n isExpressionStatement,\n isIfStatement,\n isProgram,\n isStatement,\n isVariableDeclaration,\n removeComments,\n returnStatement,\n sequenceExpression,\n validate,\n yieldExpression,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\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 = _verifyNodeList.call(this, 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 */\nexport function replaceWith<R extends t.Node>(\n this: NodePath,\n replacementPath: R,\n): [NodePath<R>];\nexport function replaceWith<R extends NodePath>(\n this: NodePath,\n replacementPath: R,\n): [R];\nexport function replaceWith(\n this: NodePath,\n replacementPath: t.Node | NodePath,\n): [NodePath] {\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];\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 [NodePath];\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 _replaceWith.call(this, 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 [nodePath ? this.get(nodePath) : this];\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 declars: t.Identifier[] = [];\n const nodesAsSingleExpression = gatherSequenceExpressions(nodes, declars);\n if (nodesAsSingleExpression) {\n for (const id of declars) this.scope.push({ id });\n return this.replaceWith(nodesAsSingleExpression)[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 callee.get(\"body\").scope.hoistVariables(id => this.scope.push({ id }));\n\n // add implicit returns to all ending expression statements\n const completionRecords: Array<NodePath> = callee.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\nfunction gatherSequenceExpressions(\n nodes: ReadonlyArray<t.Node>,\n declars: Array<t.Identifier>,\n) {\n const exprs: t.Expression[] = [];\n let ensureLastUndefined = true;\n\n for (const node of nodes) {\n // if we encounter emptyStatement before a non-emptyStatement\n // we want to disregard that\n if (!isEmptyStatement(node)) {\n ensureLastUndefined = false;\n }\n\n if (isExpression(node)) {\n exprs.push(node);\n } else if (isExpressionStatement(node)) {\n exprs.push(node.expression);\n } else if (isVariableDeclaration(node)) {\n if (node.kind !== \"var\") return; // bailed\n\n for (const declar of node.declarations) {\n const bindings = getBindingIdentifiers(declar);\n for (const key of Object.keys(bindings)) {\n declars.push(cloneNode(bindings[key]));\n }\n\n if (declar.init) {\n exprs.push(assignmentExpression(\"=\", declar.id, declar.init));\n }\n }\n\n ensureLastUndefined = true;\n } else if (isIfStatement(node)) {\n const consequent = node.consequent\n ? gatherSequenceExpressions([node.consequent], declars)\n : buildUndefinedNode();\n const alternate = node.alternate\n ? gatherSequenceExpressions([node.alternate], declars)\n : buildUndefinedNode();\n if (!consequent || !alternate) return; // bailed\n\n exprs.push(conditionalExpression(node.test, consequent, alternate));\n } else if (isBlockStatement(node)) {\n const body = gatherSequenceExpressions(node.body, declars);\n if (!body) return; // bailed\n\n exprs.push(body);\n } else if (isEmptyStatement(node)) {\n // empty statement so ensure the last item is undefined if we're last\n // checks if emptyStatement is first\n if (nodes.indexOf(node) === 0) {\n ensureLastUndefined = true;\n }\n } else {\n // bailed, we can't turn this statement into an expression\n return;\n }\n }\n\n if (ensureLastUndefined) exprs.push(buildUndefinedNode());\n\n if (exprs.length === 1) {\n return exprs[0];\n } else {\n return sequenceExpression(exprs);\n }\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 = _verifyNodeList.call(this, nodes);\n const paths = _containerInsertAfter.call(this, 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,aAAA,GAAAJ,OAAA;AACA,IAAAK,OAAA,GAAAL,OAAA;AACA,IAAAM,EAAA,GAAAN,OAAA;AA6BsB;EA5BpBO,cAAc;EACdC,uBAAuB;EACvBC,oBAAoB;EACpBC,eAAe;EACfC,cAAc;EACdC,kBAAkB;EAClBC,cAAc;EACdC,SAAS;EACTC,qBAAqB;EACrBC,mBAAmB;EACnBC,qBAAqB;EACrBC,UAAU;EACVC,sBAAsB;EACtBC,uBAAuB;EACvBC,gBAAgB;EAChBC,g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,EAAEJ,KAAK,CAAC;MACzC,MAAMa,KAAK,GAAG0F,mCAAqB,CAACnG,IAAI,CAAC,IAAI,EAAEJ,KAAK,CAAC;MACrD,IAAI,CAACgB,MAAM,CAAC,CAAC;MACb,OAAOH,KAAK;IACd,CAAC,MAAM;MACL,OAAO,IAAI,CAACd,mBAAmB,CAACC,KAAK,CAAC;IACxC;EACF,CAAC,MAAM;IACL,OAAO,IAAI,CAACmC,WAAW,CAACnC,KAAK,CAAC;EAChC;AACF","ignoreList":[]}
|
1
|
+
{"version":3,"names":["_codeFrame","require","_index","_index2","_cache","_modification","_parser","_t","_context","FUNCTION_TYPES","arrowFunctionExpression","assignmentExpression","awaitExpression","blockStatement","buildUndefinedNode","callExpression","cloneNode","conditionalExpression","expressionStatement","getBindingIdentifiers","identifier","inheritLeadingComments","inheritTrailingComments","inheritsComments","isBlockStatement","isEmptyStatement","isExpression","isExpressionStatement","isIfStatement","isProgram","isStatement","isVariableDeclaration","removeComments","returnStatement","sequenceExpression","validate","yieldExpression","replaceWithMultiple","nodes","_getCachedPaths","resync","call","_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","declars","nodesAsSingleExpression","gatherSequenceExpressions","id","scope","push","functionParent","getFunctionParent","isParentAsync","async","isParentGenerator","generator","callee","hoistVariables","completionRecords","getCompletionRecords","path","loop","findParent","isLoop","uid","getData","generateDeclaredUidIdentifier","pushContainer","setData","name","arrowFunctionToExpression","newCallee","needToAwaitFunction","hasType","needToYieldFunction","exprs","ensureLastUndefined","kind","declar","declarations","bindings","Object","keys","init","consequent","alternate","test","indexOf","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.ts\";\nimport NodePath from \"./index.ts\";\nimport { getCachedPaths } from \"../cache.ts\";\nimport { _verifyNodeList, _containerInsertAfter } from \"./modification.ts\";\nimport { parse } from \"@babel/parser\";\nimport {\n FUNCTION_TYPES,\n arrowFunctionExpression,\n assignmentExpression,\n awaitExpression,\n blockStatement,\n buildUndefinedNode,\n callExpression,\n cloneNode,\n conditionalExpression,\n expressionStatement,\n getBindingIdentifiers,\n identifier,\n inheritLeadingComments,\n inheritTrailingComments,\n inheritsComments,\n isBlockStatement,\n isEmptyStatement,\n isExpression,\n isExpressionStatement,\n isIfStatement,\n isProgram,\n isStatement,\n isVariableDeclaration,\n removeComments,\n returnStatement,\n sequenceExpression,\n validate,\n yieldExpression,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport { resync, setScope } from \"./context.ts\";\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 resync.call(this);\n\n nodes = _verifyNodeList.call(this, 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 resync.call(this);\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 */\nexport function replaceWith<R extends t.Node>(\n this: NodePath,\n replacementPath: R,\n): [NodePath<R>];\nexport function replaceWith<R extends NodePath>(\n this: NodePath,\n replacementPath: R,\n): [R];\nexport function replaceWith(\n this: NodePath,\n replacementPath: t.Node | NodePath,\n): [NodePath] {\n resync.call(this);\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];\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 [NodePath];\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 _replaceWith.call(this, replacement);\n this.type = replacement.type;\n\n // potentially create new scope\n setScope.call(this);\n\n // requeue for visiting\n this.requeue();\n\n return [nodePath ? this.get(nodePath) : this];\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 resync.call(this);\n\n const declars: t.Identifier[] = [];\n const nodesAsSingleExpression = gatherSequenceExpressions(nodes, declars);\n if (nodesAsSingleExpression) {\n for (const id of declars) this.scope.push({ id });\n return this.replaceWith(nodesAsSingleExpression)[0].get(\"expressions\");\n }\n\n const functionParent = this.getFunctionParent();\n const isParentAsync = functionParent?.node.async;\n const isParentGenerator = functionParent?.node.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 callee.get(\"body\").scope.hoistVariables(id => this.scope.push({ id }));\n\n // add implicit returns to all ending expression statements\n const completionRecords: Array<NodePath> = callee.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\nfunction gatherSequenceExpressions(\n nodes: ReadonlyArray<t.Node>,\n declars: Array<t.Identifier>,\n) {\n const exprs: t.Expression[] = [];\n let ensureLastUndefined = true;\n\n for (const node of nodes) {\n // if we encounter emptyStatement before a non-emptyStatement\n // we want to disregard that\n if (!isEmptyStatement(node)) {\n ensureLastUndefined = false;\n }\n\n if (isExpression(node)) {\n exprs.push(node);\n } else if (isExpressionStatement(node)) {\n exprs.push(node.expression);\n } else if (isVariableDeclaration(node)) {\n if (node.kind !== \"var\") return; // bailed\n\n for (const declar of node.declarations) {\n const bindings = getBindingIdentifiers(declar);\n for (const key of Object.keys(bindings)) {\n declars.push(cloneNode(bindings[key]));\n }\n\n if (declar.init) {\n exprs.push(assignmentExpression(\"=\", declar.id, declar.init));\n }\n }\n\n ensureLastUndefined = true;\n } else if (isIfStatement(node)) {\n const consequent = node.consequent\n ? gatherSequenceExpressions([node.consequent], declars)\n : buildUndefinedNode();\n const alternate = node.alternate\n ? gatherSequenceExpressions([node.alternate], declars)\n : buildUndefinedNode();\n if (!consequent || !alternate) return; // bailed\n\n exprs.push(conditionalExpression(node.test, consequent, alternate));\n } else if (isBlockStatement(node)) {\n const body = gatherSequenceExpressions(node.body, declars);\n if (!body) return; // bailed\n\n exprs.push(body);\n } else if (isEmptyStatement(node)) {\n // empty statement so ensure the last item is undefined if we're last\n // checks if emptyStatement is first\n if (nodes.indexOf(node) === 0) {\n ensureLastUndefined = true;\n }\n } else {\n // bailed, we can't turn this statement into an expression\n return;\n }\n }\n\n if (ensureLastUndefined) exprs.push(buildUndefinedNode());\n\n if (exprs.length === 1) {\n return exprs[0];\n } else {\n return sequenceExpression(exprs);\n }\n}\n\nexport function replaceInline(this: NodePath, nodes: t.Node | Array<t.Node>) {\n resync.call(this);\n\n if (Array.isArray(nodes)) {\n if (Array.isArray(this.container)) {\n nodes = _verifyNodeList.call(this, nodes);\n const paths = _containerInsertAfter.call(this, 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,aAAA,GAAAJ,OAAA;AACA,IAAAK,OAAA,GAAAL,OAAA;AACA,IAAAM,EAAA,GAAAN,OAAA;AA+BA,IAAAO,QAAA,GAAAP,OAAA;AAAgD;EA9B9CQ,cAAc;EACdC,uBAAuB;EACvBC,oBAAoB;EACpBC,eAAe;EACfC,cAAc;EACdC,kBAAkB;EAClBC,cAAc;EACdC,SAAS;EACTC,qBAAqB;EACrBC,mBAAmB;EACnBC,qBAAqB;EACrBC,UAAU;EACVC,sBAAsB;EACtBC,uBAAuB;EACvBC,gBAAgB;EAChBC,gBAAgB;EAChBC,gBAAgB;EAChBC,YAAY;EACZC,qBAAqB;EACrBC,aAAa;EACbC,SAAS;EACTC,WAAW;EACXC,qBAAqB;EACrBC,cAAc;EACdC,eAAe;EACfC,kBAAkB;EAClBC,QAAQ;EACRC;AAAe,IAAA7B,EAAA;AAaV,SAAS8B,mBAAmBA,CAEjCC,KAAwB,EACZ;EAAA,IAAAC,eAAA;EACZC,eAAM,CAACC,IAAI,CAAC,IAAI,CAAC;EAEjBH,KAAK,GAAGI,6BAAe,CAACD,IAAI,CAAC,IAAI,EAAEH,KAAK,CAAC;EACzCjB,sBAAsB,CAACiB,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,CAACK,IAAI,CAAC;EAC3CrB,uBAAuB,CAACgB,KAAK,CAACA,KAAK,CAACM,MAAM,GAAG,CAAC,CAAC,EAAE,IAAI,CAACD,IAAI,CAAC;EAC3D,CAAAJ,eAAA,OAAAM,qBAAc,EAAC,IAAI,CAACC,GAAG,EAAE,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AAI,CAAC,EAAE;MAGjC,IAAIL,KAAK,CAACsG,OAAO,CAACjG,IAAI,CAAC,KAAK,CAAC,EAAE;QAC7BsF,mBAAmB,GAAG,IAAI;MAC5B;IACF,CAAC,MAAM;MAEL;IACF;EACF;EAEA,IAAIA,mBAAmB,EAAED,KAAK,CAAC1B,IAAI,CAACxF,kBAAkB,CAAC,CAAC,CAAC;EAEzD,IAAIkH,KAAK,CAACpF,MAAM,KAAK,CAAC,EAAE;IACtB,OAAOoF,KAAK,CAAC,CAAC,CAAC;EACjB,CAAC,MAAM;IACL,OAAO9F,kBAAkB,CAAC8F,KAAK,CAAC;EAClC;AACF;AAEO,SAASa,aAAaA,CAAiBvG,KAA6B,EAAE;EAC3EE,eAAM,CAACC,IAAI,CAAC,IAAI,CAAC;EAEjB,IAAIqC,KAAK,CAACC,OAAO,CAACzC,KAAK,CAAC,EAAE;IACxB,IAAIwC,KAAK,CAACC,OAAO,CAAC,IAAI,CAAC9B,SAAS,CAAC,EAAE;MACjCX,KAAK,GAAGI,6BAAe,CAACD,IAAI,CAAC,IAAI,EAAEH,KAAK,CAAC;MACzC,MAAMa,KAAK,GAAG2F,mCAAqB,CAACrG,IAAI,CAAC,IAAI,EAAEH,KAAK,CAAC;MACrD,IAAI,CAACgB,MAAM,CAAC,CAAC;MACb,OAAOH,KAAK;IACd,CAAC,MAAM;MACL,OAAO,IAAI,CAACd,mBAAmB,CAACC,KAAK,CAAC;IACxC;EACF,CAAC,MAAM;IACL,OAAO,IAAI,CAACmC,WAAW,CAACnC,KAAK,CAAC;EAChC;AACF","ignoreList":[]}
|
package/lib/scope/index.js
CHANGED
@@ -271,12 +271,12 @@ const collectorVisitor = {
|
|
271
271
|
for (const param of params) {
|
272
272
|
path.scope.registerBinding("param", param);
|
273
273
|
}
|
274
|
-
if (path.isFunctionExpression() && path.
|
274
|
+
if (path.isFunctionExpression() && path.node.id && !path.node.id[NOT_LOCAL_BINDING]) {
|
275
275
|
path.scope.registerBinding("local", path.get("id"), path);
|
276
276
|
}
|
277
277
|
},
|
278
278
|
ClassExpression(path) {
|
279
|
-
if (path.
|
279
|
+
if (path.node.id && !path.node.id[NOT_LOCAL_BINDING]) {
|
280
280
|
path.scope.registerBinding("local", path.get("id"), path);
|
281
281
|
}
|
282
282
|
},
|