@babel/traverse 7.20.0 → 7.20.5

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.

Potentially problematic release.


This version of @babel/traverse might be problematic. Click here for more details.

Files changed (57) hide show
  1. package/lib/cache.js +0 -3
  2. package/lib/cache.js.map +1 -1
  3. package/lib/context.js +7 -21
  4. package/lib/context.js.map +1 -1
  5. package/lib/hub.js +0 -6
  6. package/lib/hub.js.map +1 -1
  7. package/lib/index.js +3 -21
  8. package/lib/index.js.map +1 -1
  9. package/lib/path/ancestry.js +3 -23
  10. package/lib/path/ancestry.js.map +1 -1
  11. package/lib/path/comments.js +0 -4
  12. package/lib/path/comments.js.map +1 -1
  13. package/lib/path/context.js +14 -54
  14. package/lib/path/context.js.map +1 -1
  15. package/lib/path/conversion.js +23 -78
  16. package/lib/path/conversion.js.map +1 -1
  17. package/lib/path/evaluation.js +9 -85
  18. package/lib/path/evaluation.js.map +1 -1
  19. package/lib/path/family.js +6 -73
  20. package/lib/path/family.js.map +1 -1
  21. package/lib/path/index.js +2 -61
  22. package/lib/path/index.js.map +1 -1
  23. package/lib/path/inference/index.js +2 -27
  24. package/lib/path/inference/index.js.map +1 -1
  25. package/lib/path/inference/inferer-reference.js +10 -31
  26. package/lib/path/inference/inferer-reference.js.map +1 -1
  27. package/lib/path/inference/inferers.js +4 -44
  28. package/lib/path/inference/inferers.js.map +1 -1
  29. package/lib/path/inference/util.js +0 -4
  30. package/lib/path/inference/util.js.map +1 -1
  31. package/lib/path/introspection.js +17 -59
  32. package/lib/path/introspection.js.map +1 -1
  33. package/lib/path/lib/hoister.js +7 -23
  34. package/lib/path/lib/hoister.js.map +1 -1
  35. package/lib/path/lib/removal-hooks.js +7 -3
  36. package/lib/path/lib/removal-hooks.js.map +1 -1
  37. package/lib/path/lib/virtual-types-validator.js +0 -23
  38. package/lib/path/lib/virtual-types-validator.js.map +1 -1
  39. package/lib/path/lib/virtual-types.js +1 -0
  40. package/lib/path/lib/virtual-types.js.map +1 -1
  41. package/lib/path/modification.js +12 -47
  42. package/lib/path/modification.js.map +1 -1
  43. package/lib/path/removal.js +0 -16
  44. package/lib/path/removal.js.map +1 -1
  45. package/lib/path/replacement.js +8 -50
  46. package/lib/path/replacement.js.map +1 -1
  47. package/lib/scope/binding.js +2 -14
  48. package/lib/scope/binding.js.map +1 -1
  49. package/lib/scope/index.js +19 -184
  50. package/lib/scope/index.js.map +1 -1
  51. package/lib/scope/lib/renamer.js +2 -25
  52. package/lib/scope/lib/renamer.js.map +1 -1
  53. package/lib/traverse-node.js +0 -7
  54. package/lib/traverse-node.js.map +1 -1
  55. package/lib/visitors.js +13 -44
  56. package/lib/visitors.js.map +1 -1
  57. package/package.json +4 -4
@@ -1 +1 @@
1
- {"version":3,"names":["arrowFunctionExpression","assertExpression","assignmentExpression","blockStatement","callExpression","cloneNode","expressionStatement","isAssignmentExpression","isCallExpression","isExpression","isIdentifier","isSequenceExpression","isSuper","thisExpression","insertBefore","nodes_","_assertUnremoved","nodes","_verifyNodeList","parentPath","isExpressionStatement","isLabeledStatement","isExportNamedDeclaration","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","parent","expressions","isAlmostConstantAssignment","scope","left","blockScope","getBlockParent","hasOwnBinding","name","getOwnBinding","constantViolations","insertAfter","get","map","isPattern","unshift","callee","isPure","isMethod","computed","temp","generateDeclaredUidIdentifier","pushContainer","fromIndex","incrementBy","pathCache","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 { 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 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 } = this;\n\n if (\n parentPath.isExpressionStatement() ||\n parentPath.isLabeledStatement() ||\n parentPath.isExportNamedDeclaration() ||\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 } = this;\n if (\n parentPath.isExpressionStatement() ||\n parentPath.isLabeledStatement() ||\n parentPath.isExportNamedDeclaration() ||\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 // declaraion 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 for (const [, path] of paths) {\n if (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;;AACA;;AACA;;AACA;;;EACEA,uB;EACAC,gB;EACAC,oB;EACAC,c;EACAC,c;EACAC,S;EACAC,mB;EACAC,sB;EACAC,gB;EACAC,Y;EACAC,Y;EACAC,oB;EACAC,O;EACAC;;;AASK,SAASC,YAAT,CAELC,MAFK,EAGO;EACZ,KAAKC,gBAAL;;EAEA,MAAMC,KAAK,GAAG,KAAKC,eAAL,CAAqBH,MAArB,CAAd;;EAEA,MAAM;IAAEI;EAAF,IA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GAAG0C,cAAA,CAAShB,GAAT,CAAa;IACxBzD,UAAU,EAAE,IADY;IAExB6C,MAAM,EAAE,KAAKnC,IAFW;IAGxBK,SAAS,EAAE,KAAKL,IAAL,CAAUgE,OAAV,CAHa;IAIxBA,OAJwB;IAKxBjE,GAAG,EAAE;EALmB,CAAb,EAMVkE,UANU,CAMC,KAAK1C,OANN,CAAb;;EAQA,OAAOF,IAAI,CAACf,sBAAL,CAELlB,KAFK,CAAP;AAID;;AAEM,SAASqE,aAAT,CAELO,OAFK,EAGL5E,KAHK,EAUL;EACA,KAAKD,gBAAL;;EAEA,MAAM+E,aAAa,GAAG,KAAK7E,eAAL,CAEpBD,KAFoB,CAAtB;;EAQA,MAAMiB,SAAS,GAAG,KAAKL,IAAL,CAAUgE,OAAV,CAAlB;;EACA,MAAM3C,IAAI,GAAG0C,cAAA,CAAShB,GAAT,CAAa;IACxBzD,UAAU,EAAE,IADY;IAExB6C,MAAM,EAAE,KAAKnC,IAFW;IAGxBK,SAAS,EAAEA,SAHa;IAIxB2D,OAJwB;IAMxBjE,GAAG,EAAEM,SAAS,CAACW;EANS,CAAb,EAOViD,UAPU,CAOC,KAAK1C,OAPN,CAAb;;EASA,OAAOF,IAAI,CAAC8C,mBAAL,CAAyBD,aAAzB,CAAP;AACD;;AAMM,SAASE,KAAT,CAEL9B,KAAY,GAAG,KAAKA,KAFf,EAGL;EACA,MAAM+B,OAAO,GAAG,IAAIC,gBAAJ,CAAmB,IAAnB,EAAyBhC,KAAzB,CAAhB;EACA,OAAO+B,OAAO,CAACE,GAAR,EAAP;AACD"}
1
+ {"version":3,"names":["arrowFunctionExpression","assertExpression","assignmentExpression","blockStatement","callExpression","cloneNode","expressionStatement","isAssignmentExpression","isCallExpression","isExpression","isIdentifier","isSequenceExpression","isSuper","thisExpression","insertBefore","nodes_","_assertUnremoved","nodes","_verifyNodeList","parentPath","isExpressionStatement","isLabeledStatement","isExportNamedDeclaration","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","parent","expressions","isAlmostConstantAssignment","scope","left","blockScope","getBlockParent","hasOwnBinding","name","getOwnBinding","constantViolations","insertAfter","get","map","isPattern","unshift","callee","isPure","isMethod","computed","temp","generateDeclaredUidIdentifier","pushContainer","fromIndex","incrementBy","pathCache","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 { 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 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 } = this;\n\n if (\n parentPath.isExpressionStatement() ||\n parentPath.isLabeledStatement() ||\n parentPath.isExportNamedDeclaration() ||\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 } = this;\n if (\n parentPath.isExpressionStatement() ||\n parentPath.isLabeledStatement() ||\n parentPath.isExportNamedDeclaration() ||\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 // declaraion 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 for (const [, path] of paths) {\n if (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;AACA;AACA;AACA;AAesB;EAdpBA,uBAAuB;EACvBC,gBAAgB;EAChBC,oBAAoB;EACpBC,cAAc;EACdC,cAAc;EACdC,SAAS;EACTC,mBAAmB;EACnBC,sBAAsB;EACtBC,gBAAgB;EAChBC,YAAY;EACZC,YAAY;EACZC,oBAAoB;EACpBC,OAAO;EACPC;AAAc;;AAST,SAASC,YAAY,CAE1BC,MAAyB,EACb;EACZ,IAAI,CAACC,gBAAgB,EAAE;EAEvB,MAAMC,KAAK,GAAG,IAAI,CAACC,eAAe,CAACH,MAAM,CAAC;EAE1C,MAAM;IAAEI;EAAW,CAAC,GAAG,IAAI;EAE3B,IACEA,UAAU,CAACC,qBAAqB,EAAE,IAClCD,UAAU,CAACE,kBAAkB,EAAE,IAC/BF,UAAU,CAACG,wBAAwB,EAAE,IACpCH,UAAU,CAACI,0BAA0B,EAAE,IAAI,IAAI,CAACC,aAAa,EAAG,EACjE;IACA,OAAOL,UAAU,CAACL,YAAY,CAACG,KAAK,CAAC;EACvC,CAAC,MAAM,IACJ,IAAI,CAACQ,UAAU,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAACC,YAAY,EAAE,IACrDP,UAAU,CAACQ,cAAc,EAAE,IAAI,IAAI,CAACC,GAAG,KAAK,MAAO,EACpD;IACA,IAAI,IAAI,CAACC,IAAI,EAAEZ,KAAK,CAACa,IAAI,CAAC,IAAI,CAACD,IAAI,CAAC;IAEpC,OAAO,IAAI,CAACE,+BAA+B,CAACd,KAAK,CAAC;EACpD,CAAC,MAAM,IAAIe,KAAK,CAACC,OAAO,CAAC,IAAI,CAACC,SAAS,CAAC,EAAE;IACxC,OAAO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BAAsB;EAEhClB,KAAK,CACN;AACH;AAEO,SAASqE,aAAa,CAE3BO,OAAU,EACV5E,KAMS,EACT;EACA,IAAI,CAACD,gBAAgB,EAAE;EAEvB,MAAM+E,aAAa,GAAG,IAAI,CAAC7E,eAAe;EAExCD,KAAK,CACN;;EAKD,MAAMiB,SAAS,GAAG,IAAI,CAACL,IAAI,CAACgE,OAAO,CAAC;EACpC,MAAM3C,IAAI,GAAG0C,cAAQ,CAAChB,GAAG,CAAC;IACxBzD,UAAU,EAAE,IAAI;IAChB6C,MAAM,EAAE,IAAI,CAACnC,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,KAAK,CAEnB9B,KAAY,GAAG,IAAI,CAACA,KAAK,EACzB;EACA,MAAM+B,OAAO,GAAG,IAAIC,gBAAW,CAAI,IAAI,EAAEhC,KAAK,CAAC;EAC/C,OAAO+B,OAAO,CAACE,GAAG,EAAE;AACtB"}
@@ -9,48 +9,34 @@ exports._markRemoved = _markRemoved;
9
9
  exports._remove = _remove;
10
10
  exports._removeFromScope = _removeFromScope;
11
11
  exports.remove = remove;
12
-
13
12
  var _removalHooks = require("./lib/removal-hooks");
14
-
15
13
  var _cache = require("../cache");
16
-
17
14
  var _index = require("./index");
18
15
 
19
16
  function remove() {
20
17
  var _this$opts;
21
-
22
18
  this._assertUnremoved();
23
-
24
19
  this.resync();
25
-
26
20
  if (!((_this$opts = this.opts) != null && _this$opts.noScope)) {
27
21
  this._removeFromScope();
28
22
  }
29
-
30
23
  if (this._callRemovalHooks()) {
31
24
  this._markRemoved();
32
-
33
25
  return;
34
26
  }
35
-
36
27
  this.shareCommentsWithSiblings();
37
-
38
28
  this._remove();
39
-
40
29
  this._markRemoved();
41
30
  }
42
-
43
31
  function _removeFromScope() {
44
32
  const bindings = this.getBindingIdentifiers();
45
33
  Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
46
34
  }
47
-
48
35
  function _callRemovalHooks() {
49
36
  for (const fn of _removalHooks.hooks) {
50
37
  if (fn(this, this.parentPath)) return true;
51
38
  }
52
39
  }
53
-
54
40
  function _remove() {
55
41
  if (Array.isArray(this.container)) {
56
42
  this.container.splice(this.key, 1);
@@ -59,13 +45,11 @@ function _remove() {
59
45
  this._replaceWith(null);
60
46
  }
61
47
  }
62
-
63
48
  function _markRemoved() {
64
49
  this._traverseFlags |= _index.SHOULD_SKIP | _index.REMOVED;
65
50
  if (this.parent) _cache.path.get(this.parent).delete(this.node);
66
51
  this.node = null;
67
52
  }
68
-
69
53
  function _assertUnremoved() {
70
54
  if (this.removed) {
71
55
  throw this.buildCodeFrameError("NodePath has been removed so is read-only.");
@@ -1 +1 @@
1
- {"version":3,"names":["remove","_assertUnremoved","resync","opts","noScope","_removeFromScope","_callRemovalHooks","_markRemoved","shareCommentsWithSiblings","_remove","bindings","getBindingIdentifiers","Object","keys","forEach","name","scope","removeBinding","fn","hooks","parentPath","Array","isArray","container","splice","key","updateSiblingKeys","_replaceWith","_traverseFlags","SHOULD_SKIP","REMOVED","parent","pathCache","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;;AACA;;AAEA;;AAEO,SAASA,MAAT,GAAgC;EAAA;;EACrC,KAAKC,gBAAL;;EAEA,KAAKC,MAAL;;EACA,IAAI,gBAAC,KAAKC,IAAN,aAAC,WAAWC,OAAZ,CAAJ,EAAyB;IACvB,KAAKC,gBAAL;EACD;;EAED,IAAI,KAAKC,iBAAL,EAAJ,EAA8B;IAC5B,KAAKC,YAAL;;IACA;EACD;;EAED,KAAKC,yBAAL;;EACA,KAAKC,OAAL;;EACA,KAAKF,YAAL;AACD;;AAEM,SAASF,gBAAT,GAA0C;EAC/C,MAAMK,QAAQ,GAAG,KAAKC,qBAAL,EAAjB;EACAC,MAAM,CAACC,IAAP,CAAYH,QAAZ,EAAsBI,OAAtB,CAA8BC,IAAI,IAAI,KAAKC,KAAL,CAAWC,aAAX,CAAyBF,IAAzB,CAAtC;AACD;;AAEM,SAAST,iBAAT,GAA2C;EAChD,KAAK,MAAMY,EAAX,IAAiBC,mBAAjB,EAAwB;IACtB,IAAID,EAAE,CAAC,IAAD,EAAO,KAAKE,UAAZ,CAAN,EAA+B,OAAO,IAAP;EAChC;AACF;;AAEM,SAASX,OAAT,GAAiC;EACtC,IAAIY,KAAK,CAACC,OAAN,CAAc,KAAKC,SAAnB,CAAJ,EAAmC;IACjC,KAAKA,SAAL,CAAeC,MAAf,CAAsB,KAAKC,GAA3B,EAA0C,CAA1C;IACA,KAAKC,iBAAL,CAAuB,KAAKD,GAA5B,EAA2C,CAAC,CAA5C;EACD,CAHD,MAGO;IACL,KAAKE,YAAL,CAAkB,IAAlB;EACD;AACF;;AAEM,SAASpB,YAAT,GAAsC;EAE3C,KAAKqB,cAAL,IAAuBC,kBAAA,GAAcC,cAArC;EACA,IAAI,KAAKC,MAAT,EAAiBC,WAAA,CAAUC,GAAV,CAAc,KAAKF,MAAnB,EAA2BG,MAA3B,CAAkC,KAAKC,IAAvC;EACjB,KAAKA,IAAL,GAAY,IAAZ;AACD;;AAEM,SAASlC,gBAAT,GAA0C;EAC/C,IAAI,KAAKmC,OAAT,EAAkB;IAChB,MAAM,KAAKC,mBAAL,CACJ,4CADI,CAAN;EAGD;AACF"}
1
+ {"version":3,"names":["remove","_assertUnremoved","resync","opts","noScope","_removeFromScope","_callRemovalHooks","_markRemoved","shareCommentsWithSiblings","_remove","bindings","getBindingIdentifiers","Object","keys","forEach","name","scope","removeBinding","fn","hooks","parentPath","Array","isArray","container","splice","key","updateSiblingKeys","_replaceWith","_traverseFlags","SHOULD_SKIP","REMOVED","parent","pathCache","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;AACA;AAEA;;AAEO,SAASA,MAAM,GAAiB;EAAA;EACrC,IAAI,CAACC,gBAAgB,EAAE;EAEvB,IAAI,CAACC,MAAM,EAAE;EACb,IAAI,gBAAC,IAAI,CAACC,IAAI,aAAT,WAAWC,OAAO,GAAE;IACvB,IAAI,CAACC,gBAAgB,EAAE;EACzB;EAEA,IAAI,IAAI,CAACC,iBAAiB,EAAE,EAAE;IAC5B,IAAI,CAACC,YAAY,EAAE;IACnB;EACF;EAEA,IAAI,CAACC,yBAAyB,EAAE;EAChC,IAAI,CAACC,OAAO,EAAE;EACd,IAAI,CAACF,YAAY,EAAE;AACrB;AAEO,SAASF,gBAAgB,GAAiB;EAC/C,MAAMK,QAAQ,GAAG,IAAI,CAACC,qBAAqB,EAAE;EAC7CC,MAAM,CAACC,IAAI,CAACH,QAAQ,CAAC,CAACI,OAAO,CAACC,IAAI,IAAI,IAAI,CAACC,KAAK,CAACC,aAAa,CAACF,IAAI,CAAC,CAAC;AACvE;AAEO,SAAST,iBAAiB,GAAiB;EAChD,KAAK,MAAMY,EAAE,IAAIC,mBAAK,EAAE;IACtB,IAAID,EAAE,CAAC,IAAI,EAAE,IAAI,CAACE,UAAU,CAAC,EAAE,OAAO,IAAI;EAC5C;AACF;AAEO,SAASX,OAAO,GAAiB;EACtC,IAAIY,KAAK,CAACC,OAAO,CAAC,IAAI,CAACC,SAAS,CAAC,EAAE;IACjC,IAAI,CAACA,SAAS,CAACC,MAAM,CAAC,IAAI,CAACC,GAAG,EAAY,CAAC,CAAC;IAC5C,IAAI,CAACC,iBAAiB,CAAC,IAAI,CAACD,GAAG,EAAY,CAAC,CAAC,CAAC;EAChD,CAAC,MAAM;IACL,IAAI,CAACE,YAAY,CAAC,IAAI,CAAC;EACzB;AACF;AAEO,SAASpB,YAAY,GAAiB;EAE3C,IAAI,CAACqB,cAAc,IAAIC,kBAAW,GAAGC,cAAO;EAC5C,IAAI,IAAI,CAACC,MAAM,EAAEC,WAAS,CAACC,GAAG,CAAC,IAAI,CAACF,MAAM,CAAC,CAACG,MAAM,CAAC,IAAI,CAACC,IAAI,CAAC;EAC7D,IAAI,CAACA,IAAI,GAAG,IAAI;AAClB;AAEO,SAASlC,gBAAgB,GAAiB;EAC/C,IAAI,IAAI,CAACmC,OAAO,EAAE;IAChB,MAAM,IAAI,CAACC,mBAAmB,CAC5B,4CAA4C,CAC7C;EACH;AACF"}
@@ -9,21 +9,13 @@ exports.replaceInline = replaceInline;
9
9
  exports.replaceWith = replaceWith;
10
10
  exports.replaceWithMultiple = replaceWithMultiple;
11
11
  exports.replaceWithSourceString = replaceWithSourceString;
12
-
13
12
  var _codeFrame = require("@babel/code-frame");
14
-
15
13
  var _index = require("../index");
16
-
17
14
  var _index2 = require("./index");
18
-
19
15
  var _cache = require("../cache");
20
-
21
16
  var _parser = require("@babel/parser");
22
-
23
17
  var _t = require("@babel/types");
24
-
25
18
  var _helperHoistVariables = require("@babel/helper-hoist-variables");
26
-
27
19
  const {
28
20
  FUNCTION_TYPES,
29
21
  arrowFunctionExpression,
@@ -49,34 +41,30 @@ const {
49
41
 
50
42
  function replaceWithMultiple(nodes) {
51
43
  var _pathCache$get;
52
-
53
44
  this.resync();
54
45
  nodes = this._verifyNodeList(nodes);
55
46
  inheritLeadingComments(nodes[0], this.node);
56
47
  inheritTrailingComments(nodes[nodes.length - 1], this.node);
57
48
  (_pathCache$get = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get.delete(this.node);
58
- this.node = this.container[this.key] = null;
49
+ this.node =
50
+ this.container[this.key] = null;
59
51
  const paths = this.insertAfter(nodes);
60
-
61
52
  if (this.node) {
62
53
  this.requeue();
63
54
  } else {
64
55
  this.remove();
65
56
  }
66
-
67
57
  return paths;
68
58
  }
69
59
 
70
60
  function replaceWithSourceString(replacement) {
71
61
  this.resync();
72
62
  let ast;
73
-
74
63
  try {
75
64
  replacement = `(${replacement})`;
76
65
  ast = (0, _parser.parse)(replacement);
77
66
  } catch (err) {
78
67
  const loc = err.loc;
79
-
80
68
  if (loc) {
81
69
  err.message += " - make sure this is an expression.\n" + (0, _codeFrame.codeFrameColumns)(replacement, {
82
70
  start: {
@@ -86,122 +74,102 @@ function replaceWithSourceString(replacement) {
86
74
  });
87
75
  err.code = "BABEL_REPLACE_SOURCE_ERROR";
88
76
  }
89
-
90
77
  throw err;
91
78
  }
92
-
93
79
  const expressionAST = ast.program.body[0].expression;
94
-
95
80
  _index.default.removeProperties(expressionAST);
96
-
97
81
  return this.replaceWith(expressionAST);
98
82
  }
99
83
 
100
84
  function replaceWith(replacementPath) {
101
85
  this.resync();
102
-
103
86
  if (this.removed) {
104
87
  throw new Error("You can't replace this node, we've already removed it");
105
88
  }
106
-
107
89
  let replacement = replacementPath instanceof _index2.default ? replacementPath.node : replacementPath;
108
-
109
90
  if (!replacement) {
110
91
  throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead");
111
92
  }
112
-
113
93
  if (this.node === replacement) {
114
94
  return [this];
115
95
  }
116
-
117
96
  if (this.isProgram() && !isProgram(replacement)) {
118
97
  throw new Error("You can only replace a Program root node with another Program node");
119
98
  }
120
-
121
99
  if (Array.isArray(replacement)) {
122
100
  throw new Error("Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`");
123
101
  }
124
-
125
102
  if (typeof replacement === "string") {
126
103
  throw new Error("Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`");
127
104
  }
128
-
129
105
  let nodePath = "";
130
-
131
106
  if (this.isNodeType("Statement") && isExpression(replacement)) {
132
107
  if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration()) {
133
108
  replacement = expressionStatement(replacement);
134
109
  nodePath = "expression";
135
110
  }
136
111
  }
137
-
138
112
  if (this.isNodeType("Expression") && isStatement(replacement)) {
139
113
  if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement)) {
140
114
  return this.replaceExpressionWithStatements([replacement]);
141
115
  }
142
116
  }
143
-
144
117
  const oldNode = this.node;
145
-
146
118
  if (oldNode) {
147
119
  inheritsComments(replacement, oldNode);
148
120
  removeComments(oldNode);
149
121
  }
150
122
 
151
123
  this._replaceWith(replacement);
152
-
153
124
  this.type = replacement.type;
125
+
154
126
  this.setScope();
127
+
155
128
  this.requeue();
156
129
  return [nodePath ? this.get(nodePath) : this];
157
130
  }
158
131
 
159
132
  function _replaceWith(node) {
160
133
  var _pathCache$get2;
161
-
162
134
  if (!this.container) {
163
135
  throw new ReferenceError("Container is falsy");
164
136
  }
165
-
166
137
  if (this.inList) {
167
138
  validate(this.parent, this.key, [node]);
168
139
  } else {
169
140
  validate(this.parent, this.key, node);
170
141
  }
171
-
172
142
  this.debug(`Replace with ${node == null ? void 0 : node.type}`);
173
143
  (_pathCache$get2 = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get2.set(node, this).delete(this.node);
174
- this.node = this.container[this.key] = node;
144
+ this.node =
145
+ this.container[this.key] = node;
175
146
  }
176
147
 
177
148
  function replaceExpressionWithStatements(nodes) {
178
149
  this.resync();
179
150
  const nodesAsSequenceExpression = toSequenceExpression(nodes, this.scope);
180
-
181
151
  if (nodesAsSequenceExpression) {
182
152
  return this.replaceWith(nodesAsSequenceExpression)[0].get("expressions");
183
153
  }
184
-
185
154
  const functionParent = this.getFunctionParent();
186
155
  const isParentAsync = functionParent == null ? void 0 : functionParent.is("async");
187
156
  const isParentGenerator = functionParent == null ? void 0 : functionParent.is("generator");
188
157
  const container = arrowFunctionExpression([], blockStatement(nodes));
189
158
  this.replaceWith(callExpression(container, []));
159
+
190
160
  const callee = this.get("callee");
191
161
  (0, _helperHoistVariables.default)(callee.get("body"), id => {
192
162
  this.scope.push({
193
163
  id
194
164
  });
195
165
  }, "var");
196
- const completionRecords = this.get("callee").getCompletionRecords();
197
166
 
167
+ const completionRecords = this.get("callee").getCompletionRecords();
198
168
  for (const path of completionRecords) {
199
169
  if (!path.isExpressionStatement()) continue;
200
170
  const loop = path.findParent(path => path.isLoop());
201
-
202
171
  if (loop) {
203
172
  let uid = loop.getData("expressionReplacementReturnUid");
204
-
205
173
  if (!uid) {
206
174
  uid = callee.scope.generateDeclaredUidIdentifier("ret");
207
175
  callee.get("body").pushContainer("body", returnStatement(cloneNode(uid)));
@@ -209,7 +177,6 @@ function replaceExpressionWithStatements(nodes) {
209
177
  } else {
210
178
  uid = identifier(uid.name);
211
179
  }
212
-
213
180
  path.get("expression").replaceWith(assignmentExpression("=", cloneNode(uid), path.node.expression));
214
181
  } else {
215
182
  path.replaceWith(returnStatement(path.node.expression));
@@ -220,34 +187,25 @@ function replaceExpressionWithStatements(nodes) {
220
187
  const newCallee = callee;
221
188
 
222
189
  const needToAwaitFunction = isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", FUNCTION_TYPES);
223
-
224
190
  const needToYieldFunction = isParentGenerator && _index.default.hasType(this.get("callee.body").node, "YieldExpression", FUNCTION_TYPES);
225
-
226
191
  if (needToAwaitFunction) {
227
192
  newCallee.set("async", true);
228
-
229
193
  if (!needToYieldFunction) {
230
194
  this.replaceWith(awaitExpression(this.node));
231
195
  }
232
196
  }
233
-
234
197
  if (needToYieldFunction) {
235
198
  newCallee.set("generator", true);
236
199
  this.replaceWith(yieldExpression(this.node, true));
237
200
  }
238
-
239
201
  return newCallee.get("body.body");
240
202
  }
241
-
242
203
  function replaceInline(nodes) {
243
204
  this.resync();
244
-
245
205
  if (Array.isArray(nodes)) {
246
206
  if (Array.isArray(this.container)) {
247
207
  nodes = this._verifyNodeList(nodes);
248
-
249
208
  const paths = this._containerInsertAfter(nodes);
250
-
251
209
  this.remove();
252
210
  return paths;
253
211
  } else {
@@ -1 +1 @@
1
- {"version":3,"names":["FUNCTION_TYPES","arrowFunctionExpression","assignmentExpression","awaitExpression","blockStatement","callExpression","cloneNode","expressionStatement","identifier","inheritLeadingComments","inheritTrailingComments","inheritsComments","isExpression","isProgram","isStatement","removeComments","returnStatement","toSequenceExpression","validate","yieldExpression","replaceWithMultiple","nodes","resync","_verifyNodeList","node","length","pathCache","get","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","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 { 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;;AACA;;AACA;;AACA;;AACA;;AACA;;AAuBA;;;EAtBEA,c;EACAC,uB;EACAC,oB;EACAC,e;EACAC,c;EACAC,c;EACAC,S;EACAC,mB;E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AAS,CAAC1B,GAAV,CAAc,OAAd,EAAuB,IAAvB;;IAEA,IAAI,CAAC6B,mBAAL,EAA0B;MACxB,KAAKlD,WAAL,CAAiBnD,eAAe,CAAE,IAAD,CAAmBqB,IAApB,CAAhC;IACD;EACF;;EACD,IAAIgF,mBAAJ,EAAyB;IACvBH,SAAS,CAAC1B,GAAV,CAAc,WAAd,EAA2B,IAA3B;IACA,KAAKrB,WAAL,CAAiBnC,eAAe,CAAE,IAAD,CAAmBK,IAApB,EAA0B,IAA1B,CAAhC;EACD;;EAED,OAAO6E,SAAS,CAAC1E,GAAV,CAAc,WAAd,CAAP;AACD;;AAEM,SAAS8E,aAAT,CAAuCpF,KAAvC,EAAsE;EAC3E,KAAKC,MAAL;;EAEA,IAAIqC,KAAK,CAACC,OAAN,CAAcvC,KAAd,CAAJ,EAA0B;IACxB,IAAIsC,KAAK,CAACC,OAAN,CAAc,KAAK9B,SAAnB,CAAJ,EAAmC;MACjCT,KAAK,GAAG,KAAKE,eAAL,CAAqBF,KAArB,CAAR;;MACA,MAAMW,KAAK,GAAG,KAAK0E,qBAAL,CAA2BrF,KAA3B,CAAd;;MACA,KAAKc,MAAL;MACA,OAAOH,KAAP;IACD,CALD,MAKO;MACL,OAAO,KAAKZ,mBAAL,CAAyBC,KAAzB,CAAP;IACD;EACF,CATD,MASO;IACL,OAAO,KAAKiC,WAAL,CAAiBjC,KAAjB,CAAP;EACD;AACF"}
1
+ {"version":3,"names":["FUNCTION_TYPES","arrowFunctionExpression","assignmentExpression","awaitExpression","blockStatement","callExpression","cloneNode","expressionStatement","identifier","inheritLeadingComments","inheritTrailingComments","inheritsComments","isExpression","isProgram","isStatement","removeComments","returnStatement","toSequenceExpression","validate","yieldExpression","replaceWithMultiple","nodes","resync","_verifyNodeList","node","length","pathCache","get","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","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 { 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;AACA;AACA;AACA;AACA;AACA;AAuBA;AAA2D;EAtBzDA,cAAc;EACdC,uBAAuB;EACvBC,oBAAoB;EACpBC,eAAe;EACfC,cAAc;EACdC,cAAc;EACdC,SAAS;EACTC,mBAAmB;EACnBC,UAAU;EACVC,sBAAsB;EACtBC,uBAAuB;EACvBC,gBAAgB;EAChBC,YAAY;EACZC,SAAS;EACTC,WAAW;EACXC,cAAc;EACdC,eAAe;EACfC,oBAAoB;EACpBC,QAAQ;EACRC;AAAe;;AAaV,SAASC,mBAAmB,CAEjCC,KAAwB,EACZ;EAAA;EACZ,IAAI,CAACC,MAAM,EAAE;EAEbD,KAAK,GAAG,IAAI,CAACE,eAAe,CAACF,KAAK,CAAC;EACnCZ,sBAAsB,CAACY,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,CAACG,IAAI,CAAC;EAC3Cd,uBAAuB,CAACW,KAAK,CAACA,KAAK,CAACI,MAAM,GAAG,CAAC,CAAC,EAAE,IAAI,CAACD,IAAI,CAAC;EAC3D,kBAAAE,WAAS,CAACC,GAAG,CAAC,IAAI,CAACC,MAAM,CAAC,qBAA1B,eAA4BC,MAAM,CAAC,IAAI,CAACL,IAAI,CAAC;EAC7C,IAAI,CAACA,IAAI;EA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;EACA,IAAIgF,mBAAmB,EAAE;IACvBH,SAAS,CAAC1B,GAAG,CAAC,WAAW,EAAE,IAAI,CAAC;IAChC,IAAI,CAACrB,WAAW,CAACnC,eAAe,CAAE,IAAI,CAAcK,IAAI,EAAE,IAAI,CAAC,CAAC;EAClE;EAEA,OAAO6E,SAAS,CAAC1E,GAAG,CAAC,WAAW,CAAC;AACnC;AAEO,SAAS8E,aAAa,CAAiBpF,KAA6B,EAAE;EAC3E,IAAI,CAACC,MAAM,EAAE;EAEb,IAAIqC,KAAK,CAACC,OAAO,CAACvC,KAAK,CAAC,EAAE;IACxB,IAAIsC,KAAK,CAACC,OAAO,CAAC,IAAI,CAAC9B,SAAS,CAAC,EAAE;MACjCT,KAAK,GAAG,IAAI,CAACE,eAAe,CAACF,KAAK,CAAC;MACnC,MAAMW,KAAK,GAAG,IAAI,CAAC0E,qBAAqB,CAACrF,KAAK,CAAC;MAC/C,IAAI,CAACc,MAAM,EAAE;MACb,OAAOH,KAAK;IACd,CAAC,MAAM;MACL,OAAO,IAAI,CAACZ,mBAAmB,CAACC,KAAK,CAAC;IACxC;EACF,CAAC,MAAM;IACL,OAAO,IAAI,CAACiC,WAAW,CAACjC,KAAK,CAAC;EAChC;AACF"}
@@ -25,27 +25,23 @@ class Binding {
25
25
  this.scope = scope;
26
26
  this.path = path;
27
27
  this.kind = kind;
28
-
29
- if ((kind === "var" || kind === "hoisted") && isDeclaredInLoop(path || (() => {
28
+ if ((kind === "var" || kind === "hoisted") &&
29
+ isDeclaredInLoop(path || (() => {
30
30
  throw new Error("Internal Babel error: unreachable ");
31
31
  })())) {
32
32
  this.reassign(path);
33
33
  }
34
-
35
34
  this.clearValue();
36
35
  }
37
-
38
36
  deoptValue() {
39
37
  this.clearValue();
40
38
  this.hasDeoptedValue = true;
41
39
  }
42
-
43
40
  setValue(value) {
44
41
  if (this.hasDeoptedValue) return;
45
42
  this.hasValue = true;
46
43
  this.value = value;
47
44
  }
48
-
49
45
  clearValue() {
50
46
  this.hasDeoptedValue = false;
51
47
  this.hasValue = false;
@@ -54,11 +50,9 @@ class Binding {
54
50
 
55
51
  reassign(path) {
56
52
  this.constant = false;
57
-
58
53
  if (this.constantViolations.indexOf(path) !== -1) {
59
54
  return;
60
55
  }
61
-
62
56
  this.constantViolations.push(path);
63
57
  }
64
58
 
@@ -66,7 +60,6 @@ class Binding {
66
60
  if (this.referencePaths.indexOf(path) !== -1) {
67
61
  return;
68
62
  }
69
-
70
63
  this.referenced = true;
71
64
  this.references++;
72
65
  this.referencePaths.push(path);
@@ -76,11 +69,8 @@ class Binding {
76
69
  this.references--;
77
70
  this.referenced = !!this.references;
78
71
  }
79
-
80
72
  }
81
-
82
73
  exports.default = Binding;
83
-
84
74
  function isDeclaredInLoop(path) {
85
75
  for (let {
86
76
  parentPath,
@@ -90,12 +80,10 @@ function isDeclaredInLoop(path) {
90
80
  key
91
81
  } = parentPath)) {
92
82
  if (parentPath.isFunctionParent()) return false;
93
-
94
83
  if (parentPath.isWhile() || parentPath.isForXStatement() || parentPath.isForStatement() && key === "body") {
95
84
  return true;
96
85
  }
97
86
  }
98
-
99
87
  return false;
100
88
  }
101
89
 
@@ -1 +1 @@
1
- {"version":3,"names":["Binding","constructor","identifier","scope","path","kind","constantViolations","constant","referencePaths","referenced","references","isDeclaredInLoop","Error","reassign","clearValue","deoptValue","hasDeoptedValue","setValue","value","hasValue","indexOf","push","reference","dereference","parentPath","key","isFunctionParent","isWhile","isForXStatement","isForStatement"],"sources":["../../src/scope/binding.ts"],"sourcesContent":["import type NodePath from \"../path\";\nimport type * as t from \"@babel/types\";\nimport type Scope from \"./index\";\n\nexport type BindingKind =\n | \"var\" /* var declarator */\n | \"let\" /* let declarator, class declaration id, catch clause parameters */\n | \"const\" /* const/using declarator */\n | \"module\" /* import specifiers */\n | \"hoisted\" /* function declaration id */\n | \"param\" /* function declaration parameters */\n | \"local\" /* function expression id, class expression id */\n | \"unknown\"; /* export specifiers */\n/**\n * This class is responsible for a binding inside of a scope.\n *\n * It tracks the following:\n *\n * * Node path.\n * * Amount of times referenced by other nodes.\n * * Paths to nodes that reassign or modify this binding.\n * * The kind of binding. (Is it a parameter, declaration etc)\n */\n\nexport default class Binding {\n identifier: t.Identifier;\n scope: Scope;\n path: NodePath;\n kind: BindingKind;\n\n constructor({\n identifier,\n scope,\n path,\n kind,\n }: {\n identifier: t.Identifier;\n scope: Scope;\n path: NodePath;\n kind: BindingKind;\n }) {\n this.identifier = identifier;\n this.scope = scope;\n this.path = path;\n this.kind = kind;\n\n if (\n (kind === \"var\" || kind === \"hoisted\") &&\n // https://github.com/rollup/rollup/issues/4654\n // Rollup removes the path argument from this call. Add an\n // unreachable IIFE (that rollup doesn't know is unreachable)\n // with side effects, to prevent it from messing up with arguments.\n // You can reproduce this with\n // BABEL_8_BREAKING=true make prepublish-build\n isDeclaredInLoop(\n path ||\n (() => {\n throw new Error(\"Internal Babel error: unreachable \");\n })(),\n )\n ) {\n this.reassign(path);\n }\n\n this.clearValue();\n }\n\n constantViolations: Array<NodePath> = [];\n constant: boolean = true;\n\n referencePaths: Array<NodePath> = [];\n referenced: boolean = false;\n references: number = 0;\n\n declare hasDeoptedValue: boolean;\n declare hasValue: boolean;\n declare value: any;\n\n deoptValue() {\n this.clearValue();\n this.hasDeoptedValue = true;\n }\n\n setValue(value: any) {\n if (this.hasDeoptedValue) return;\n this.hasValue = true;\n this.value = value;\n }\n\n clearValue() {\n this.hasDeoptedValue = false;\n this.hasValue = false;\n this.value = null;\n }\n\n /**\n * Register a constant violation with the provided `path`.\n */\n\n reassign(path: NodePath) {\n this.constant = false;\n if (this.constantViolations.indexOf(path) !== -1) {\n return;\n }\n this.constantViolations.push(path);\n }\n\n /**\n * Increment the amount of references to this binding.\n */\n\n reference(path: NodePath) {\n if (this.referencePaths.indexOf(path) !== -1) {\n return;\n }\n this.referenced = true;\n this.references++;\n this.referencePaths.push(path);\n }\n\n /**\n * Decrement the amount of references to this binding.\n */\n\n dereference() {\n this.references--;\n this.referenced = !!this.references;\n }\n}\n\nfunction isDeclaredInLoop(path: NodePath) {\n for (\n let { parentPath, key } = path;\n parentPath;\n { parentPath, key } = parentPath\n ) {\n if (parentPath.isFunctionParent()) return false;\n if (\n parentPath.isWhile() ||\n parentPath.isForXStatement() ||\n (parentPath.isForStatement() && key === \"body\")\n ) {\n return true;\n }\n }\n return false;\n}\n"],"mappings":";;;;;;;AAwBe,MAAMA,OAAN,CAAc;EAM3BC,WAAW,CAAC;IACVC,UADU;IAEVC,KAFU;IAGVC,IAHU;IAIVC;EAJU,CAAD,EAUR;IAAA,KAfHH,UAeG;IAAA,KAdHC,KAcG;IAAA,KAbHC,IAaG;IAAA,KAZHC,IAYG;IAAA,KA2BHC,kBA3BG,GA2BmC,EA3BnC;IAAA,KA4BHC,QA5BG,GA4BiB,IA5BjB;IAAA,KA8BHC,cA9BG,GA8B+B,EA9B/B;IAAA,KA+BHC,UA/BG,GA+BmB,KA/BnB;IAAA,KAgCHC,UAhCG,GAgCkB,CAhClB;IACD,KAAKR,UAAL,GAAkBA,UAAlB;IACA,KAAKC,KAAL,GAAaA,KAAb;IACA,KAAKC,IAAL,GAAYA,IAAZ;IACA,KAAKC,IAAL,GAAYA,IAAZ;;IAEA,IACE,CAACA,IAAI,KAAK,KAAT,IAAkBA,IAAI,KAAK,SAA5B,KAOAM,gBAAgB,CACdP,IAAI,IACF,CAAC,MAAM;MACL,MAAM,IAAIQ,KAAJ,CAAU,oCAAV,CAAN;IACD,CAFD,GAFY,CARlB,EAcE;MACA,KAAKC,QAAL,CAAcT,IAAd;IACD;;IAED,KAAKU,UAAL;EACD;;EAaDC,UAAU,GAAG;IACX,KAAKD,UAAL;IACA,KAAKE,eAAL,GAAuB,IAAvB;EACD;;EAEDC,QAAQ,CAACC,KAAD,EAAa;IACnB,IAAI,KAAKF,eAAT,EAA0B;IAC1B,KAAKG,QAAL,GAAgB,IAAhB;IACA,KAAKD,KAAL,GAAaA,KAAb;EACD;;EAEDJ,UAAU,GAAG;IACX,KAAKE,eAAL,GAAuB,KAAvB;IACA,KAAKG,QAAL,GAAgB,KAAhB;IACA,KAAKD,KAAL,GAAa,IAAb;EACD;;EAMDL,QAAQ,CAACT,IAAD,EAAiB;IACvB,KAAKG,QAAL,GAAgB,KAAhB;;IACA,IAAI,KAAKD,kBAAL,CAAwBc,OAAxB,CAAgChB,IAAhC,MAA0C,CAAC,CAA/C,EAAkD;MAChD;IACD;;IACD,KAAKE,kBAAL,CAAwBe,IAAxB,CAA6BjB,IAA7B;EACD;;EAMDkB,SAAS,CAAClB,IAAD,EAAiB;IACxB,IAAI,KAAKI,cAAL,CAAoBY,OAApB,CAA4BhB,IAA5B,MAAsC,CAAC,CAA3C,EAA8C;MAC5C;IACD;;IACD,KAAKK,UAAL,GAAkB,IAAlB;IACA,KAAKC,UAAL;IACA,KAAKF,cAAL,CAAoBa,IAApB,CAAyBjB,IAAzB;EACD;;EAMDmB,WAAW,GAAG;IACZ,KAAKb,UAAL;IACA,KAAKD,UAAL,GAAkB,CAAC,CAAC,KAAKC,UAAzB;EACD;;AAvG0B;;;;AA0G7B,SAASC,gBAAT,CAA0BP,IAA1B,EAA0C;EACxC,KACE,IAAI;IAAEoB,UAAF;IAAcC;EAAd,IAAsBrB,IAD5B,EAEEoB,UAFF,GAGE;IAAEA,UAAF;IAAcC;EAAd,IAAsBD,UAHxB,GAIE;IACA,IAAIA,UAAU,CAACE,gBAAX,EAAJ,EAAmC,OAAO,KAAP;;IACnC,IACEF,UAAU,CAACG,OAAX,MACAH,UAAU,CAACI,eAAX,EADA,IAECJ,UAAU,CAACK,cAAX,MAA+BJ,GAAG,KAAK,MAH1C,EAIE;MACA,OAAO,IAAP;IACD;EACF;;EACD,OAAO,KAAP;AACD"}
1
+ {"version":3,"names":["Binding","constructor","identifier","scope","path","kind","constantViolations","constant","referencePaths","referenced","references","isDeclaredInLoop","Error","reassign","clearValue","deoptValue","hasDeoptedValue","setValue","value","hasValue","indexOf","push","reference","dereference","parentPath","key","isFunctionParent","isWhile","isForXStatement","isForStatement"],"sources":["../../src/scope/binding.ts"],"sourcesContent":["import type NodePath from \"../path\";\nimport type * as t from \"@babel/types\";\nimport type Scope from \"./index\";\n\nexport type BindingKind =\n | \"var\" /* var declarator */\n | \"let\" /* let declarator, class declaration id, catch clause parameters */\n | \"const\" /* const/using declarator */\n | \"module\" /* import specifiers */\n | \"hoisted\" /* function declaration id */\n | \"param\" /* function declaration parameters */\n | \"local\" /* function expression id, class expression id */\n | \"unknown\"; /* export specifiers */\n/**\n * This class is responsible for a binding inside of a scope.\n *\n * It tracks the following:\n *\n * * Node path.\n * * Amount of times referenced by other nodes.\n * * Paths to nodes that reassign or modify this binding.\n * * The kind of binding. (Is it a parameter, declaration etc)\n */\n\nexport default class Binding {\n identifier: t.Identifier;\n scope: Scope;\n path: NodePath;\n kind: BindingKind;\n\n constructor({\n identifier,\n scope,\n path,\n kind,\n }: {\n identifier: t.Identifier;\n scope: Scope;\n path: NodePath;\n kind: BindingKind;\n }) {\n this.identifier = identifier;\n this.scope = scope;\n this.path = path;\n this.kind = kind;\n\n if (\n (kind === \"var\" || kind === \"hoisted\") &&\n // https://github.com/rollup/rollup/issues/4654\n // Rollup removes the path argument from this call. Add an\n // unreachable IIFE (that rollup doesn't know is unreachable)\n // with side effects, to prevent it from messing up with arguments.\n // You can reproduce this with\n // BABEL_8_BREAKING=true make prepublish-build\n isDeclaredInLoop(\n path ||\n (() => {\n throw new Error(\"Internal Babel error: unreachable \");\n })(),\n )\n ) {\n this.reassign(path);\n }\n\n this.clearValue();\n }\n\n constantViolations: Array<NodePath> = [];\n constant: boolean = true;\n\n referencePaths: Array<NodePath> = [];\n referenced: boolean = false;\n references: number = 0;\n\n declare hasDeoptedValue: boolean;\n declare hasValue: boolean;\n declare value: any;\n\n deoptValue() {\n this.clearValue();\n this.hasDeoptedValue = true;\n }\n\n setValue(value: any) {\n if (this.hasDeoptedValue) return;\n this.hasValue = true;\n this.value = value;\n }\n\n clearValue() {\n this.hasDeoptedValue = false;\n this.hasValue = false;\n this.value = null;\n }\n\n /**\n * Register a constant violation with the provided `path`.\n */\n\n reassign(path: NodePath) {\n this.constant = false;\n if (this.constantViolations.indexOf(path) !== -1) {\n return;\n }\n this.constantViolations.push(path);\n }\n\n /**\n * Increment the amount of references to this binding.\n */\n\n reference(path: NodePath) {\n if (this.referencePaths.indexOf(path) !== -1) {\n return;\n }\n this.referenced = true;\n this.references++;\n this.referencePaths.push(path);\n }\n\n /**\n * Decrement the amount of references to this binding.\n */\n\n dereference() {\n this.references--;\n this.referenced = !!this.references;\n }\n}\n\nfunction isDeclaredInLoop(path: NodePath) {\n for (\n let { parentPath, key } = path;\n parentPath;\n { parentPath, key } = parentPath\n ) {\n if (parentPath.isFunctionParent()) return false;\n if (\n parentPath.isWhile() ||\n parentPath.isForXStatement() ||\n (parentPath.isForStatement() && key === \"body\")\n ) {\n return true;\n }\n }\n return false;\n}\n"],"mappings":";;;;;;;AAwBe,MAAMA,OAAO,CAAC;EAM3BC,WAAW,CAAC;IACVC,UAAU;IACVC,KAAK;IACLC,IAAI;IACJC;EAMF,CAAC,EAAE;IAAA,KAfHH,UAAU;IAAA,KACVC,KAAK;IAAA,KACLC,IAAI;IAAA,KACJC,IAAI;IAAA,KAuCJC,kBAAkB,GAAoB,EAAE;IAAA,KACxCC,QAAQ,GAAY,IAAI;IAAA,KAExBC,cAAc,GAAoB,EAAE;IAAA,KACpCC,UAAU,GAAY,KAAK;IAAA,KAC3BC,UAAU,GAAW,CAAC;IA/BpB,IAAI,CAACR,UAAU,GAAGA,UAAU;IAC5B,IAAI,CAACC,KAAK,GAAGA,KAAK;IAClB,IAAI,CAACC,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACC,IAAI,GAAGA,IAAI;IAEhB,IACE,CAACA,IAAI,KAAK,KAAK,IAAIA,IAAI,KAAK,SAAS;IAOrCM,gBAAgB,CACdP,IAAI,IACF,CAAC,MAAM;MACL,MAAM,IAAIQ,KAAK,CAAC,oCAAoC,CAAC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