@babel/traverse 8.0.0-alpha.2 → 8.0.0-alpha.3

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.
Files changed (59) hide show
  1. package/lib/index.js +132 -130
  2. package/lib/index.js.map +1 -1
  3. package/package.json +11 -11
  4. package/lib/cache.js +0 -28
  5. package/lib/cache.js.map +0 -1
  6. package/lib/context.js +0 -108
  7. package/lib/context.js.map +0 -1
  8. package/lib/hub.js +0 -12
  9. package/lib/hub.js.map +0 -1
  10. package/lib/path/ancestry.js +0 -126
  11. package/lib/path/ancestry.js.map +0 -1
  12. package/lib/path/comments.js +0 -46
  13. package/lib/path/comments.js.map +0 -1
  14. package/lib/path/context.js +0 -194
  15. package/lib/path/context.js.map +0 -1
  16. package/lib/path/conversion.js +0 -453
  17. package/lib/path/conversion.js.map +0 -1
  18. package/lib/path/evaluation.js +0 -333
  19. package/lib/path/evaluation.js.map +0 -1
  20. package/lib/path/family.js +0 -322
  21. package/lib/path/family.js.map +0 -1
  22. package/lib/path/index.js +0 -181
  23. package/lib/path/index.js.map +0 -1
  24. package/lib/path/inference/index.js +0 -137
  25. package/lib/path/inference/index.js.map +0 -1
  26. package/lib/path/inference/inferer-reference.js +0 -145
  27. package/lib/path/inference/inferer-reference.js.map +0 -1
  28. package/lib/path/inference/inferers.js +0 -172
  29. package/lib/path/inference/inferers.js.map +0 -1
  30. package/lib/path/inference/util.js +0 -20
  31. package/lib/path/inference/util.js.map +0 -1
  32. package/lib/path/introspection.js +0 -360
  33. package/lib/path/introspection.js.map +0 -1
  34. package/lib/path/lib/hoister.js +0 -164
  35. package/lib/path/lib/hoister.js.map +0 -1
  36. package/lib/path/lib/removal-hooks.js +0 -31
  37. package/lib/path/lib/removal-hooks.js.map +0 -1
  38. package/lib/path/lib/virtual-types-validator.js +0 -133
  39. package/lib/path/lib/virtual-types-validator.js.map +0 -1
  40. package/lib/path/lib/virtual-types.js +0 -20
  41. package/lib/path/lib/virtual-types.js.map +0 -1
  42. package/lib/path/modification.js +0 -209
  43. package/lib/path/modification.js.map +0 -1
  44. package/lib/path/removal.js +0 -48
  45. package/lib/path/removal.js.map +0 -1
  46. package/lib/path/replacement.js +0 -192
  47. package/lib/path/replacement.js.map +0 -1
  48. package/lib/scope/binding.js +0 -78
  49. package/lib/scope/binding.js.map +0 -1
  50. package/lib/scope/index.js +0 -870
  51. package/lib/scope/index.js.map +0 -1
  52. package/lib/scope/lib/renamer.js +0 -105
  53. package/lib/scope/lib/renamer.js.map +0 -1
  54. package/lib/traverse-node.js +0 -23
  55. package/lib/traverse-node.js.map +0 -1
  56. package/lib/types.js +0 -3
  57. package/lib/types.js.map +0 -1
  58. package/lib/visitors.js +0 -210
  59. package/lib/visitors.js.map +0 -1
@@ -1,192 +0,0 @@
1
- import { codeFrameColumns } from "@babel/code-frame";
2
- import traverse from "../index.js";
3
- import NodePath from "./index.js";
4
- import { getCachedPaths } from "../cache.js";
5
- import { parse } from "@babel/parser";
6
- import * as _t from "@babel/types";
7
- const {
8
- FUNCTION_TYPES,
9
- arrowFunctionExpression,
10
- assignmentExpression,
11
- awaitExpression,
12
- blockStatement,
13
- callExpression,
14
- cloneNode,
15
- expressionStatement,
16
- identifier,
17
- inheritLeadingComments,
18
- inheritTrailingComments,
19
- inheritsComments,
20
- isExpression,
21
- isProgram,
22
- isStatement,
23
- removeComments,
24
- returnStatement,
25
- toSequenceExpression,
26
- validate,
27
- yieldExpression
28
- } = _t;
29
- import hoistVariables from "@babel/helper-hoist-variables";
30
- export function replaceWithMultiple(nodes) {
31
- this.resync();
32
- nodes = this._verifyNodeList(nodes);
33
- inheritLeadingComments(nodes[0], this.node);
34
- inheritTrailingComments(nodes[nodes.length - 1], this.node);
35
- getCachedPaths(this.hub, this.parent)?.delete(this.node);
36
- this.node = this.container[this.key] = null;
37
- const paths = this.insertAfter(nodes);
38
- if (this.node) {
39
- this.requeue();
40
- } else {
41
- this.remove();
42
- }
43
- return paths;
44
- }
45
- export function replaceWithSourceString(replacement) {
46
- this.resync();
47
- let ast;
48
- try {
49
- replacement = `(${replacement})`;
50
- ast = parse(replacement);
51
- } catch (err) {
52
- const loc = err.loc;
53
- if (loc) {
54
- err.message += " - make sure this is an expression.\n" + codeFrameColumns(replacement, {
55
- start: {
56
- line: loc.line,
57
- column: loc.column + 1
58
- }
59
- });
60
- err.code = "BABEL_REPLACE_SOURCE_ERROR";
61
- }
62
- throw err;
63
- }
64
- const expressionAST = ast.program.body[0].expression;
65
- traverse.removeProperties(expressionAST);
66
- return this.replaceWith(expressionAST);
67
- }
68
- export function replaceWith(replacementPath) {
69
- this.resync();
70
- if (this.removed) {
71
- throw new Error("You can't replace this node, we've already removed it");
72
- }
73
- let replacement = replacementPath instanceof NodePath ? replacementPath.node : replacementPath;
74
- if (!replacement) {
75
- throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead");
76
- }
77
- if (this.node === replacement) {
78
- return [this];
79
- }
80
- if (this.isProgram() && !isProgram(replacement)) {
81
- throw new Error("You can only replace a Program root node with another Program node");
82
- }
83
- if (Array.isArray(replacement)) {
84
- throw new Error("Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`");
85
- }
86
- if (typeof replacement === "string") {
87
- throw new Error("Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`");
88
- }
89
- let nodePath = "";
90
- if (this.isNodeType("Statement") && isExpression(replacement)) {
91
- if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement) && !this.parentPath.isExportDefaultDeclaration()) {
92
- replacement = expressionStatement(replacement);
93
- nodePath = "expression";
94
- }
95
- }
96
- if (this.isNodeType("Expression") && isStatement(replacement)) {
97
- if (!this.canHaveVariableDeclarationOrExpression() && !this.canSwapBetweenExpressionAndStatement(replacement)) {
98
- return this.replaceExpressionWithStatements([replacement]);
99
- }
100
- }
101
- const oldNode = this.node;
102
- if (oldNode) {
103
- inheritsComments(replacement, oldNode);
104
- removeComments(oldNode);
105
- }
106
- this._replaceWith(replacement);
107
- this.type = replacement.type;
108
- this.setScope();
109
- this.requeue();
110
- return [nodePath ? this.get(nodePath) : this];
111
- }
112
- export function _replaceWith(node) {
113
- if (!this.container) {
114
- throw new ReferenceError("Container is falsy");
115
- }
116
- if (this.inList) {
117
- validate(this.parent, this.key, [node]);
118
- } else {
119
- validate(this.parent, this.key, node);
120
- }
121
- this.debug(`Replace with ${node?.type}`);
122
- getCachedPaths(this.hub, this.parent)?.set(node, this).delete(this.node);
123
- this.node = this.container[this.key] = node;
124
- }
125
- export function replaceExpressionWithStatements(nodes) {
126
- this.resync();
127
- const nodesAsSequenceExpression = toSequenceExpression(nodes, this.scope);
128
- if (nodesAsSequenceExpression) {
129
- return this.replaceWith(nodesAsSequenceExpression)[0].get("expressions");
130
- }
131
- const functionParent = this.getFunctionParent();
132
- const isParentAsync = functionParent?.is("async");
133
- const isParentGenerator = functionParent?.is("generator");
134
- const container = arrowFunctionExpression([], blockStatement(nodes));
135
- this.replaceWith(callExpression(container, []));
136
- const callee = this.get("callee");
137
- hoistVariables(callee.get("body"), id => {
138
- this.scope.push({
139
- id
140
- });
141
- }, "var");
142
- const completionRecords = this.get("callee").getCompletionRecords();
143
- for (const path of completionRecords) {
144
- if (!path.isExpressionStatement()) continue;
145
- const loop = path.findParent(path => path.isLoop());
146
- if (loop) {
147
- let uid = loop.getData("expressionReplacementReturnUid");
148
- if (!uid) {
149
- uid = callee.scope.generateDeclaredUidIdentifier("ret");
150
- callee.get("body").pushContainer("body", returnStatement(cloneNode(uid)));
151
- loop.setData("expressionReplacementReturnUid", uid);
152
- } else {
153
- uid = identifier(uid.name);
154
- }
155
- path.get("expression").replaceWith(assignmentExpression("=", cloneNode(uid), path.node.expression));
156
- } else {
157
- path.replaceWith(returnStatement(path.node.expression));
158
- }
159
- }
160
- callee.arrowFunctionToExpression();
161
- const newCallee = callee;
162
- const needToAwaitFunction = isParentAsync && traverse.hasType(this.get("callee.body").node, "AwaitExpression", FUNCTION_TYPES);
163
- const needToYieldFunction = isParentGenerator && traverse.hasType(this.get("callee.body").node, "YieldExpression", FUNCTION_TYPES);
164
- if (needToAwaitFunction) {
165
- newCallee.set("async", true);
166
- if (!needToYieldFunction) {
167
- this.replaceWith(awaitExpression(this.node));
168
- }
169
- }
170
- if (needToYieldFunction) {
171
- newCallee.set("generator", true);
172
- this.replaceWith(yieldExpression(this.node, true));
173
- }
174
- return newCallee.get("body.body");
175
- }
176
- export function replaceInline(nodes) {
177
- this.resync();
178
- if (Array.isArray(nodes)) {
179
- if (Array.isArray(this.container)) {
180
- nodes = this._verifyNodeList(nodes);
181
- const paths = this._containerInsertAfter(nodes);
182
- this.remove();
183
- return paths;
184
- } else {
185
- return this.replaceWithMultiple(nodes);
186
- }
187
- } else {
188
- return this.replaceWith(nodes);
189
- }
190
- }
191
-
192
- //# sourceMappingURL=replacement.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["codeFrameColumns","traverse","NodePath","getCachedPaths","parse","_t","FUNCTION_TYPES","arrowFunctionExpression","assignmentExpression","awaitExpression","blockStatement","callExpression","cloneNode","expressionStatement","identifier","inheritLeadingComments","inheritTrailingComments","inheritsComments","isExpression","isProgram","isStatement","removeComments","returnStatement","toSequenceExpression","validate","yieldExpression","hoistVariables","replaceWithMultiple","nodes","resync","_verifyNodeList","node","length","hub","parent","delete","container","key","paths","insertAfter","requeue","remove","replaceWithSourceString","replacement","ast","err","loc","message","start","line","column","code","expressionAST","program","body","expression","removeProperties","replaceWith","replacementPath","removed","Error","Array","isArray","nodePath","isNodeType","canHaveVariableDeclarationOrExpression","canSwapBetweenExpressionAndStatement","parentPath","isExportDefaultDeclaration","replaceExpressionWithStatements","oldNode","_replaceWith","type","setScope","get","ReferenceError","inList","debug","set","nodesAsSequenceExpression","scope","functionParent","getFunctionParent","isParentAsync","is","isParentGenerator","callee","id","push","completionRecords","getCompletionRecords","path","isExpressionStatement","loop","findParent","isLoop","uid","getData","generateDeclaredUidIdentifier","pushContainer","setData","name","arrowFunctionToExpression","newCallee","needToAwaitFunction","hasType","needToYieldFunction","replaceInline","_containerInsertAfter"],"sources":["../../src/path/replacement.ts"],"sourcesContent":["// This file contains methods responsible for replacing a node with another.\n\nimport { codeFrameColumns } from \"@babel/code-frame\";\nimport traverse from \"../index\";\nimport NodePath from \"./index\";\nimport { getCachedPaths } from \"../cache\";\nimport { parse } from \"@babel/parser\";\nimport {\n FUNCTION_TYPES,\n arrowFunctionExpression,\n assignmentExpression,\n awaitExpression,\n blockStatement,\n callExpression,\n cloneNode,\n expressionStatement,\n identifier,\n inheritLeadingComments,\n inheritTrailingComments,\n inheritsComments,\n isExpression,\n isProgram,\n isStatement,\n removeComments,\n returnStatement,\n toSequenceExpression,\n validate,\n yieldExpression,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport hoistVariables from \"@babel/helper-hoist-variables\";\n\n/**\n * Replace a node with an array of multiple. This method performs the following steps:\n *\n * - Inherit the comments of first provided node with that of the current node.\n * - Insert the provided nodes after the current node.\n * - Remove the current node.\n */\n\nexport function replaceWithMultiple(\n this: NodePath,\n nodes: t.Node | t.Node[],\n): NodePath[] {\n this.resync();\n\n nodes = this._verifyNodeList(nodes);\n inheritLeadingComments(nodes[0], this.node);\n inheritTrailingComments(nodes[nodes.length - 1], this.node);\n getCachedPaths(this.hub, this.parent)?.delete(this.node);\n this.node =\n // @ts-expect-error this.key must present in this.container\n this.container[this.key] = null;\n const paths = this.insertAfter(nodes);\n\n if (this.node) {\n this.requeue();\n } else {\n this.remove();\n }\n return paths;\n}\n\n/**\n * Parse a string as an expression and replace the current node with the result.\n *\n * NOTE: This is typically not a good idea to use. Building source strings when\n * transforming ASTs is an antipattern and SHOULD NOT be encouraged. Even if it's\n * easier to use, your transforms will be extremely brittle.\n */\n\nexport function replaceWithSourceString(this: NodePath, replacement: string) {\n this.resync();\n let ast: t.File;\n\n try {\n replacement = `(${replacement})`;\n // @ts-expect-error todo: use babel-types ast typings in Babel parser\n ast = parse(replacement);\n } catch (err) {\n const loc = err.loc;\n if (loc) {\n err.message +=\n \" - make sure this is an expression.\\n\" +\n codeFrameColumns(replacement, {\n start: {\n line: loc.line,\n column: loc.column + 1,\n },\n });\n err.code = \"BABEL_REPLACE_SOURCE_ERROR\";\n }\n throw err;\n }\n\n const expressionAST = (ast.program.body[0] as t.ExpressionStatement)\n .expression;\n traverse.removeProperties(expressionAST);\n return this.replaceWith(expressionAST);\n}\n\n/**\n * Replace the current node with another.\n */\n\nexport function replaceWith<R extends t.Node>(\n this: NodePath,\n replacementPath: R | NodePath<R>,\n): [NodePath<R>] {\n this.resync();\n\n if (this.removed) {\n throw new Error(\"You can't replace this node, we've already removed it\");\n }\n\n let replacement: t.Node =\n replacementPath instanceof NodePath\n ? replacementPath.node\n : replacementPath;\n\n if (!replacement) {\n throw new Error(\n \"You passed `path.replaceWith()` a falsy node, use `path.remove()` instead\",\n );\n }\n\n if (this.node === replacement) {\n return [this as NodePath<R>];\n }\n\n if (this.isProgram() && !isProgram(replacement)) {\n throw new Error(\n \"You can only replace a Program root node with another Program node\",\n );\n }\n\n if (Array.isArray(replacement)) {\n throw new Error(\n \"Don't use `path.replaceWith()` with an array of nodes, use `path.replaceWithMultiple()`\",\n );\n }\n\n if (typeof replacement === \"string\") {\n throw new Error(\n \"Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`\",\n );\n }\n\n let nodePath = \"\";\n\n if (this.isNodeType(\"Statement\") && isExpression(replacement)) {\n if (\n !this.canHaveVariableDeclarationOrExpression() &&\n !this.canSwapBetweenExpressionAndStatement(replacement) &&\n !this.parentPath.isExportDefaultDeclaration()\n ) {\n // replacing a statement with an expression so wrap it in an expression statement\n replacement = expressionStatement(replacement);\n nodePath = \"expression\";\n }\n }\n\n if (this.isNodeType(\"Expression\") && isStatement(replacement)) {\n if (\n !this.canHaveVariableDeclarationOrExpression() &&\n !this.canSwapBetweenExpressionAndStatement(replacement)\n ) {\n // replacing an expression with a statement so let's explode it\n return this.replaceExpressionWithStatements([replacement]) as [\n NodePath<R>,\n ];\n }\n }\n\n const oldNode = this.node;\n if (oldNode) {\n inheritsComments(replacement, oldNode);\n removeComments(oldNode);\n }\n\n // replace the node\n this._replaceWith(replacement);\n this.type = replacement.type;\n\n // potentially create new scope\n this.setScope();\n\n // requeue for visiting\n this.requeue();\n\n return [\n nodePath ? (this.get(nodePath) as NodePath<R>) : (this as NodePath<R>),\n ];\n}\n\n/**\n * Description\n */\n\nexport function _replaceWith(this: NodePath, node: t.Node) {\n if (!this.container) {\n throw new ReferenceError(\"Container is falsy\");\n }\n\n if (this.inList) {\n // @ts-expect-error todo(flow->ts): check if validate accepts a numeric key\n validate(this.parent, this.key, [node]);\n } else {\n validate(this.parent, this.key as string, node);\n }\n\n this.debug(`Replace with ${node?.type}`);\n getCachedPaths(this.hub, this.parent)?.set(node, this).delete(this.node);\n\n this.node =\n // @ts-expect-error this.key must present in this.container\n this.container[this.key] = node;\n}\n\n/**\n * This method takes an array of statements nodes and then explodes it\n * into expressions. This method retains completion records which is\n * extremely important to retain original semantics.\n */\n\nexport function replaceExpressionWithStatements(\n this: NodePath,\n nodes: Array<t.Statement>,\n) {\n this.resync();\n\n const nodesAsSequenceExpression = toSequenceExpression(nodes, this.scope);\n\n if (nodesAsSequenceExpression) {\n return this.replaceWith(nodesAsSequenceExpression)[0].get(\"expressions\");\n }\n\n const functionParent = this.getFunctionParent();\n const isParentAsync = functionParent?.is(\"async\");\n const isParentGenerator = functionParent?.is(\"generator\");\n\n const container = arrowFunctionExpression([], blockStatement(nodes));\n\n this.replaceWith(callExpression(container, []));\n // replaceWith changes the type of \"this\", but it isn't trackable by TS\n type ThisType = NodePath<\n t.CallExpression & {\n callee: t.ArrowFunctionExpression & { body: t.BlockStatement };\n }\n >;\n\n // hoist variable declaration in do block\n // `(do { var x = 1; x;})` -> `var x; (() => { x = 1; return x; })()`\n const callee = (this as ThisType).get(\"callee\");\n hoistVariables(\n callee.get(\"body\"),\n (id: t.Identifier) => {\n this.scope.push({ id });\n },\n \"var\",\n );\n\n // add implicit returns to all ending expression statements\n const completionRecords: Array<NodePath> = (this as ThisType)\n .get(\"callee\")\n .getCompletionRecords();\n for (const path of completionRecords) {\n if (!path.isExpressionStatement()) continue;\n\n const loop = path.findParent(path => path.isLoop());\n if (loop) {\n let uid = loop.getData(\"expressionReplacementReturnUid\");\n\n if (!uid) {\n uid = callee.scope.generateDeclaredUidIdentifier(\"ret\");\n callee\n .get(\"body\")\n .pushContainer(\"body\", returnStatement(cloneNode(uid)));\n loop.setData(\"expressionReplacementReturnUid\", uid);\n } else {\n uid = identifier(uid.name);\n }\n\n path\n .get(\"expression\")\n .replaceWith(\n assignmentExpression(\"=\", cloneNode(uid), path.node.expression),\n );\n } else {\n path.replaceWith(returnStatement(path.node.expression));\n }\n }\n\n // This is an IIFE, so we don't need to worry about the noNewArrows assumption\n callee.arrowFunctionToExpression();\n // Fixme: we can not `assert this is NodePath<t.FunctionExpression>` in `arrowFunctionToExpression`\n // because it is not a class method known at compile time.\n const newCallee = callee as unknown as NodePath<t.FunctionExpression>;\n\n // (() => await xxx)() -> await (async () => await xxx)();\n const needToAwaitFunction =\n isParentAsync &&\n traverse.hasType(\n (this.get(\"callee.body\") as NodePath<t.BlockStatement>).node,\n \"AwaitExpression\",\n FUNCTION_TYPES,\n );\n const needToYieldFunction =\n isParentGenerator &&\n traverse.hasType(\n (this.get(\"callee.body\") as NodePath<t.BlockStatement>).node,\n \"YieldExpression\",\n FUNCTION_TYPES,\n );\n if (needToAwaitFunction) {\n newCallee.set(\"async\", true);\n // yield* will await the generator return result\n if (!needToYieldFunction) {\n this.replaceWith(awaitExpression((this as ThisType).node));\n }\n }\n if (needToYieldFunction) {\n newCallee.set(\"generator\", true);\n this.replaceWith(yieldExpression((this as ThisType).node, true));\n }\n\n return newCallee.get(\"body.body\");\n}\n\nexport function replaceInline(this: NodePath, nodes: t.Node | Array<t.Node>) {\n this.resync();\n\n if (Array.isArray(nodes)) {\n if (Array.isArray(this.container)) {\n nodes = this._verifyNodeList(nodes);\n const paths = this._containerInsertAfter(nodes);\n this.remove();\n return paths;\n } else {\n return this.replaceWithMultiple(nodes);\n }\n } else {\n return this.replaceWith(nodes);\n }\n}\n"],"mappings":"AAEA,SAASA,gBAAgB,QAAQ,mBAAmB;AACpD,OAAOC,QAAQ,MAAM,aAAU;AAC/B,OAAOC,QAAQ,MAAM,YAAS;AAC9B,SAASC,cAAc,QAAQ,aAAU;AACzC,SAASC,KAAK,QAAQ,eAAe;AACrC,YAAAC,EAAA,MAqBO,cAAc;AAAC;EApBpBC,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,IAAApB,EAAA;AAGjB,OAAOqB,cAAc,MAAM,+BAA+B;AAU1D,OAAO,SAASC,mBAAmBA,CAEjCC,KAAwB,EACZ;EACZ,IAAI,CAACC,MAAM,CAAC,CAAC;EAEbD,KAAK,GAAG,IAAI,CAACE,eAAe,CAACF,KAAK,CAAC;EACnCb,sBAAsB,CAACa,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,CAACG,IAAI,CAAC;EAC3Cf,uBAAuB,CAACY,KAAK,CAACA,KAAK,CAACI,MAAM,GAAG,CAAC,CAAC,EAAE,IAAI,CAACD,IAAI,CAAC;EAC3D5B,cAAc,CAAC,IAAI,CAAC8B,GAAG,EAAE,IAAI,CAACC,MAAM,CAAC,EAAEC,MAAM,CAAC,IAAI,CAACJ,IAAI,CAAC;EACxD,IAAI,CAACA,IAAI,GAEP,IAAI,CAACK,SAAS,CAAC,IAAI,CAACC,GAAG,CAAC,GAAG,IAAI;EACjC,MAAMC,KAAK,GAAG,IAAI,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AC,WAAW,EAAE,IAAI,CAAC;IAChC,IAAI,CAACrB,WAAW,CAAChC,eAAe,CAAE,IAAI,CAAcM,IAAI,EAAE,IAAI,CAAC,CAAC;EAClE;EAEA,OAAOwE,SAAS,CAAC7B,GAAG,CAAC,WAAW,CAAC;AACnC;AAEA,OAAO,SAASiC,aAAaA,CAAiB/E,KAA6B,EAAE;EAC3E,IAAI,CAACC,MAAM,CAAC,CAAC;EAEb,IAAIgC,KAAK,CAACC,OAAO,CAAClC,KAAK,CAAC,EAAE;IACxB,IAAIiC,KAAK,CAACC,OAAO,CAAC,IAAI,CAAC1B,SAAS,CAAC,EAAE;MACjCR,KAAK,GAAG,IAAI,CAACE,eAAe,CAACF,KAAK,CAAC;MACnC,MAAMU,KAAK,GAAG,IAAI,CAACsE,qBAAqB,CAAChF,KAAK,CAAC;MAC/C,IAAI,CAACa,MAAM,CAAC,CAAC;MACb,OAAOH,KAAK;IACd,CAAC,MAAM;MACL,OAAO,IAAI,CAACX,mBAAmB,CAACC,KAAK,CAAC;IACxC;EACF,CAAC,MAAM;IACL,OAAO,IAAI,CAAC6B,WAAW,CAAC7B,KAAK,CAAC;EAChC;AACF"}
@@ -1,78 +0,0 @@
1
- export default class Binding {
2
- identifier;
3
- scope;
4
- path;
5
- kind;
6
- constructor({
7
- identifier,
8
- scope,
9
- path,
10
- kind
11
- }) {
12
- this.identifier = identifier;
13
- this.scope = scope;
14
- this.path = path;
15
- this.kind = kind;
16
- if ((kind === "var" || kind === "hoisted") && isDeclaredInLoop(path || (() => {
17
- throw new Error("Internal Babel error: unreachable ");
18
- })())) {
19
- this.reassign(path);
20
- }
21
- this.clearValue();
22
- }
23
- constantViolations = [];
24
- constant = true;
25
- referencePaths = [];
26
- referenced = false;
27
- references = 0;
28
- deoptValue() {
29
- this.clearValue();
30
- this.hasDeoptedValue = true;
31
- }
32
- setValue(value) {
33
- if (this.hasDeoptedValue) return;
34
- this.hasValue = true;
35
- this.value = value;
36
- }
37
- clearValue() {
38
- this.hasDeoptedValue = false;
39
- this.hasValue = false;
40
- this.value = null;
41
- }
42
- reassign(path) {
43
- this.constant = false;
44
- if (this.constantViolations.indexOf(path) !== -1) {
45
- return;
46
- }
47
- this.constantViolations.push(path);
48
- }
49
- reference(path) {
50
- if (this.referencePaths.indexOf(path) !== -1) {
51
- return;
52
- }
53
- this.referenced = true;
54
- this.references++;
55
- this.referencePaths.push(path);
56
- }
57
- dereference() {
58
- this.references--;
59
- this.referenced = !!this.references;
60
- }
61
- }
62
- function isDeclaredInLoop(path) {
63
- for (let {
64
- parentPath,
65
- key
66
- } = path; parentPath; ({
67
- parentPath,
68
- key
69
- } = parentPath)) {
70
- if (parentPath.isFunctionParent()) return false;
71
- if (parentPath.isWhile() || parentPath.isForXStatement() || parentPath.isForStatement() && key === "body") {
72
- return true;
73
- }
74
- }
75
- return false;
76
- }
77
-
78
- //# sourceMappingURL=binding.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["Binding","identifier","scope","path","kind","constructor","isDeclaredInLoop","Error","reassign","clearValue","constantViolations","constant","referencePaths","referenced","references","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":"AAwBA,eAAe,MAAMA,OAAO,CAAC;EAC3BC,UAAU;EACVC,KAAK;EACLC,IAAI;EACJC,IAAI;EAEJC,WAAWA,CAAC;IACVJ,UAAU;IACVC,KAAK;IACLC,IAAI;IACJC;EAMF,CAAC,EAAE;IACD,IAAI,CAACH,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,KAOrCE,gBAAgB,CACdH,IAAI,IACF,CAAC,MAAM;MACL,MAAM,IAAII,KAAK,CAAC,oCAAoC,CAAC;IACvD,CAAC,EAAE,CACP,CAAC,EACD;MACA,IAAI,CAACC,QAAQ,CAACL,IAAI,CAAC;IACrB;IAEA,IAAI,CAACM,UAAU,CAAC,CAAC;EACnB;EAEAC,kBAAkB,GAAoB,EAAE;EACxCC,QAAQ,GAAY,IAAI;EAExBC,cAAc,GAAoB,EAAE;EACpCC,UAAU,GAAY,KAAK;EAC3BC,UAAU,GAAW,CAAC;EAMtBC,UAAUA,CAAA,EAAG;IACX,IAAI,CAACN,UAAU,CAAC,CAAC;IACjB,IAAI,CAACO,eAAe,GAAG,IAAI;EAC7B;EAEAC,QAAQA,CAACC,KAAU,EAAE;IACnB,IAAI,IAAI,CAACF,eAAe,EAAE;IAC1B,IAAI,CAACG,QAAQ,GAAG,IAAI;IACpB,IAAI,CAACD,KAAK,GAAGA,KAAK;EACpB;EAEAT,UAAUA,CAAA,EAAG;IACX,IAAI,CAACO,eAAe,GAAG,KAAK;IAC5B,IAAI,CAACG,QAAQ,GAAG,KAAK;IACrB,IAAI,CAACD,KAAK,GAAG,IAAI;EACnB;EAMAV,QAAQA,CAACL,IAAc,EAAE;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