@babel/traverse 7.20.10 → 7.20.13
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.
- package/lib/context.js +2 -14
- package/lib/context.js.map +1 -1
- package/lib/index.js +1 -4
- package/lib/index.js.map +1 -1
- package/lib/path/ancestry.js +0 -19
- package/lib/path/ancestry.js.map +1 -1
- package/lib/path/comments.js +0 -2
- package/lib/path/comments.js.map +1 -1
- package/lib/path/context.js +5 -21
- package/lib/path/context.js.map +1 -1
- package/lib/path/conversion.js +6 -30
- package/lib/path/conversion.js.map +1 -1
- package/lib/path/evaluation.js +14 -20
- package/lib/path/evaluation.js.map +1 -1
- package/lib/path/family.js +3 -9
- package/lib/path/family.js.map +1 -1
- package/lib/path/index.js +1 -6
- package/lib/path/index.js.map +1 -1
- package/lib/path/inference/index.js +1 -9
- package/lib/path/inference/index.js.map +1 -1
- package/lib/path/inference/inferer-reference.js +1 -13
- package/lib/path/inference/inferer-reference.js.map +1 -1
- package/lib/path/inference/inferers.js +2 -5
- package/lib/path/inference/inferers.js.map +1 -1
- package/lib/path/introspection.js +1 -45
- package/lib/path/introspection.js.map +1 -1
- package/lib/path/lib/hoister.js +1 -22
- package/lib/path/lib/hoister.js.map +1 -1
- package/lib/path/lib/removal-hooks.js +1 -7
- package/lib/path/lib/removal-hooks.js.map +1 -1
- package/lib/path/lib/virtual-types-validator.js +0 -2
- package/lib/path/lib/virtual-types-validator.js.map +1 -1
- package/lib/path/lib/virtual-types.js +0 -1
- package/lib/path/lib/virtual-types.js.map +1 -1
- package/lib/path/modification.js +7 -24
- package/lib/path/modification.js.map +1 -1
- package/lib/path/removal.js +0 -1
- package/lib/path/removal.js.map +1 -1
- package/lib/path/replacement.js +2 -16
- package/lib/path/replacement.js.map +1 -1
- package/lib/scope/binding.js +1 -6
- package/lib/scope/binding.js.map +1 -1
- package/lib/scope/index.js +9 -53
- package/lib/scope/index.js.map +1 -1
- package/lib/scope/lib/renamer.js +4 -8
- package/lib/scope/lib/renamer.js.map +1 -1
- package/lib/visitors.js +2 -23
- package/lib/visitors.js.map +1 -1
- package/package.json +2 -2
package/lib/path/replacement.js
CHANGED
@@ -38,7 +38,6 @@ const {
|
|
38
38
|
validate,
|
39
39
|
yieldExpression
|
40
40
|
} = _t;
|
41
|
-
|
42
41
|
function replaceWithMultiple(nodes) {
|
43
42
|
var _pathCache$get;
|
44
43
|
this.resync();
|
@@ -46,8 +45,7 @@ function replaceWithMultiple(nodes) {
|
|
46
45
|
inheritLeadingComments(nodes[0], this.node);
|
47
46
|
inheritTrailingComments(nodes[nodes.length - 1], this.node);
|
48
47
|
(_pathCache$get = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get.delete(this.node);
|
49
|
-
this.node =
|
50
|
-
this.container[this.key] = null;
|
48
|
+
this.node = this.container[this.key] = null;
|
51
49
|
const paths = this.insertAfter(nodes);
|
52
50
|
if (this.node) {
|
53
51
|
this.requeue();
|
@@ -56,7 +54,6 @@ function replaceWithMultiple(nodes) {
|
|
56
54
|
}
|
57
55
|
return paths;
|
58
56
|
}
|
59
|
-
|
60
57
|
function replaceWithSourceString(replacement) {
|
61
58
|
this.resync();
|
62
59
|
let ast;
|
@@ -80,7 +77,6 @@ function replaceWithSourceString(replacement) {
|
|
80
77
|
_index.default.removeProperties(expressionAST);
|
81
78
|
return this.replaceWith(expressionAST);
|
82
79
|
}
|
83
|
-
|
84
80
|
function replaceWith(replacementPath) {
|
85
81
|
this.resync();
|
86
82
|
if (this.removed) {
|
@@ -119,16 +115,12 @@ function replaceWith(replacementPath) {
|
|
119
115
|
inheritsComments(replacement, oldNode);
|
120
116
|
removeComments(oldNode);
|
121
117
|
}
|
122
|
-
|
123
118
|
this._replaceWith(replacement);
|
124
119
|
this.type = replacement.type;
|
125
|
-
|
126
120
|
this.setScope();
|
127
|
-
|
128
121
|
this.requeue();
|
129
122
|
return [nodePath ? this.get(nodePath) : this];
|
130
123
|
}
|
131
|
-
|
132
124
|
function _replaceWith(node) {
|
133
125
|
var _pathCache$get2;
|
134
126
|
if (!this.container) {
|
@@ -141,10 +133,8 @@ function _replaceWith(node) {
|
|
141
133
|
}
|
142
134
|
this.debug(`Replace with ${node == null ? void 0 : node.type}`);
|
143
135
|
(_pathCache$get2 = _cache.path.get(this.parent)) == null ? void 0 : _pathCache$get2.set(node, this).delete(this.node);
|
144
|
-
this.node =
|
145
|
-
this.container[this.key] = node;
|
136
|
+
this.node = this.container[this.key] = node;
|
146
137
|
}
|
147
|
-
|
148
138
|
function replaceExpressionWithStatements(nodes) {
|
149
139
|
this.resync();
|
150
140
|
const nodesAsSequenceExpression = toSequenceExpression(nodes, this.scope);
|
@@ -156,14 +146,12 @@ function replaceExpressionWithStatements(nodes) {
|
|
156
146
|
const isParentGenerator = functionParent == null ? void 0 : functionParent.is("generator");
|
157
147
|
const container = arrowFunctionExpression([], blockStatement(nodes));
|
158
148
|
this.replaceWith(callExpression(container, []));
|
159
|
-
|
160
149
|
const callee = this.get("callee");
|
161
150
|
(0, _helperHoistVariables.default)(callee.get("body"), id => {
|
162
151
|
this.scope.push({
|
163
152
|
id
|
164
153
|
});
|
165
154
|
}, "var");
|
166
|
-
|
167
155
|
const completionRecords = this.get("callee").getCompletionRecords();
|
168
156
|
for (const path of completionRecords) {
|
169
157
|
if (!path.isExpressionStatement()) continue;
|
@@ -182,10 +170,8 @@ function replaceExpressionWithStatements(nodes) {
|
|
182
170
|
path.replaceWith(returnStatement(path.node.expression));
|
183
171
|
}
|
184
172
|
}
|
185
|
-
|
186
173
|
callee.arrowFunctionToExpression();
|
187
174
|
const newCallee = callee;
|
188
|
-
|
189
175
|
const needToAwaitFunction = isParentAsync && _index.default.hasType(this.get("callee.body").node, "AwaitExpression", FUNCTION_TYPES);
|
190
176
|
const needToYieldFunction = isParentGenerator && _index.default.hasType(this.get("callee.body").node, "YieldExpression", FUNCTION_TYPES);
|
191
177
|
if (needToAwaitFunction) {
|
@@ -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;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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ACf;EACH,MAAMwG,mBAAmB,GACvBtB,iBAAiB,IACjB9B,cAAQ,CAACmD,OAAO,CACb,IAAI,CAAC5E,GAAG,CAAC,aAAa,CAAC,CAAgCH,IAAI,EAC5D,iBAAiB,EACjBxB,cAAc,CACf;EACH,IAAIsG,mBAAmB,EAAE;IACvBD,SAAS,CAAC1B,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC;IAE5B,IAAI,CAAC6B,mBAAmB,EAAE;MACxB,IAAI,CAAClD,WAAW,CAACnD,eAAe,CAAE,IAAI,CAAcqB,IAAI,CAAC,CAAC;IAC5D;EACF;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"}
|
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,I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EAC5D,iBAAiB,EACjBxB,cAAc,CACf;EACH,IAAIsG,mBAAmB,EAAE;IACvBD,SAAS,CAAC1B,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC;IAE5B,IAAI,CAAC6B,mBAAmB,EAAE;MACxB,IAAI,CAAClD,WAAW,CAACnD,eAAe,CAAE,IAAI,CAAcqB,IAAI,CAAC,CAAC;IAC5D;EACF;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"}
|
package/lib/scope/binding.js
CHANGED
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
|
-
|
8
7
|
class Binding {
|
9
8
|
constructor({
|
10
9
|
identifier,
|
@@ -25,8 +24,7 @@ class Binding {
|
|
25
24
|
this.scope = scope;
|
26
25
|
this.path = path;
|
27
26
|
this.kind = kind;
|
28
|
-
if ((kind === "var" || kind === "hoisted") &&
|
29
|
-
isDeclaredInLoop(path || (() => {
|
27
|
+
if ((kind === "var" || kind === "hoisted") && isDeclaredInLoop(path || (() => {
|
30
28
|
throw new Error("Internal Babel error: unreachable ");
|
31
29
|
})())) {
|
32
30
|
this.reassign(path);
|
@@ -47,7 +45,6 @@ class Binding {
|
|
47
45
|
this.hasValue = false;
|
48
46
|
this.value = null;
|
49
47
|
}
|
50
|
-
|
51
48
|
reassign(path) {
|
52
49
|
this.constant = false;
|
53
50
|
if (this.constantViolations.indexOf(path) !== -1) {
|
@@ -55,7 +52,6 @@ class Binding {
|
|
55
52
|
}
|
56
53
|
this.constantViolations.push(path);
|
57
54
|
}
|
58
|
-
|
59
55
|
reference(path) {
|
60
56
|
if (this.referencePaths.indexOf(path) !== -1) {
|
61
57
|
return;
|
@@ -64,7 +60,6 @@ class Binding {
|
|
64
60
|
this.references++;
|
65
61
|
this.referencePaths.push(path);
|
66
62
|
}
|
67
|
-
|
68
63
|
dereference() {
|
69
64
|
this.references--;
|
70
65
|
this.referenced = !!this.references;
|
package/lib/scope/binding.js.map
CHANGED
@@ -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":"
|
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,GAAo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,OAAO,CAAChB,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE;MAC5C;IACF;IACA,IAAI,CAACK,UAAU,GAAG,IAAI;IACtB,IAAI,CAACC,UAAU,EAAE;IACjB,IAAI,CAACF,cAAc,CAACa,IAAI,CAACjB,IAAI,CAAC;EAChC;EAMAmB,WAAW,GAAG;IACZ,IAAI,CAACb,UAAU,EAAE;IACjB,IAAI,CAACD,UAAU,GAAG,CAAC,CAAC,IAAI,CAACC,UAAU;EACrC;AACF;AAAC;AAED,SAASC,gBAAgB,CAACP,IAAc,EAAE;EACxC,KACE,IAAI;IAAEoB,UAAU;IAAEC;EAAI,CAAC,GAAGrB,IAAI,EAC9BoB,UAAU,GACV;IAAEA,UAAU;IAAEC;EAAI,CAAC,GAAGD,UAAU,GAChC;IACA,IAAIA,UAAU,CAACE,gBAAgB,EAAE,EAAE,OAAO,KAAK;IAC/C,IACEF,UAAU,CAACG,OAAO,EAAE,IACpBH,UAAU,CAACI,eAAe,EAAE,IAC3BJ,UAAU,CAACK,cAAc,EAAE,IAAIJ,GAAG,KAAK,MAAO,EAC/C;MACA,OAAO,IAAI;IACb;EACF;EACA,OAAO,KAAK;AACd"}
|
package/lib/scope/index.js
CHANGED
@@ -170,7 +170,6 @@ function gatherNodeParts(node, parts) {
|
|
170
170
|
break;
|
171
171
|
}
|
172
172
|
}
|
173
|
-
|
174
173
|
const collectorVisitor = {
|
175
174
|
ForStatement(path) {
|
176
175
|
const declar = path.get("init");
|
@@ -184,11 +183,8 @@ const collectorVisitor = {
|
|
184
183
|
},
|
185
184
|
Declaration(path) {
|
186
185
|
if (path.isBlockScoped()) return;
|
187
|
-
|
188
186
|
if (path.isImportDeclaration()) return;
|
189
|
-
|
190
187
|
if (path.isExportDeclaration()) return;
|
191
|
-
|
192
188
|
const parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
|
193
189
|
parent.registerDeclaration(path);
|
194
190
|
},
|
@@ -203,8 +199,7 @@ const collectorVisitor = {
|
|
203
199
|
const left = path.get("left");
|
204
200
|
if (left.isPattern() || left.isIdentifier()) {
|
205
201
|
state.constantViolations.push(path);
|
206
|
-
}
|
207
|
-
else if (left.isVar()) {
|
202
|
+
} else if (left.isVar()) {
|
208
203
|
const {
|
209
204
|
scope
|
210
205
|
} = path;
|
@@ -254,7 +249,6 @@ const collectorVisitor = {
|
|
254
249
|
if (scope.path === path) scope = scope.parent;
|
255
250
|
const parent = scope.getBlockParent();
|
256
251
|
parent.registerDeclaration(path);
|
257
|
-
|
258
252
|
if (path.isClassDeclaration() && path.node.id) {
|
259
253
|
const id = path.node.id;
|
260
254
|
const name = id.name;
|
@@ -269,15 +263,12 @@ const collectorVisitor = {
|
|
269
263
|
for (const param of params) {
|
270
264
|
path.scope.registerBinding("param", param);
|
271
265
|
}
|
272
|
-
|
273
|
-
if (path.isFunctionExpression() && path.has("id") &&
|
274
|
-
!path.get("id").node[NOT_LOCAL_BINDING]) {
|
266
|
+
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
275
267
|
path.scope.registerBinding("local", path.get("id"), path);
|
276
268
|
}
|
277
269
|
},
|
278
270
|
ClassExpression(path) {
|
279
|
-
if (path.has("id") &&
|
280
|
-
!path.get("id").node[NOT_LOCAL_BINDING]) {
|
271
|
+
if (path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
281
272
|
path.scope.registerBinding("local", path);
|
282
273
|
}
|
283
274
|
}
|
@@ -310,7 +301,6 @@ class Scope {
|
|
310
301
|
this.labels = new Map();
|
311
302
|
this.inited = false;
|
312
303
|
}
|
313
|
-
|
314
304
|
get parent() {
|
315
305
|
var _parent;
|
316
306
|
let parent,
|
@@ -332,7 +322,6 @@ class Scope {
|
|
332
322
|
traverse(node, opts, state) {
|
333
323
|
(0, _index.default)(node, opts, this, state, this.path);
|
334
324
|
}
|
335
|
-
|
336
325
|
generateDeclaredUidIdentifier(name) {
|
337
326
|
const id = this.generateUidIdentifier(name);
|
338
327
|
this.push({
|
@@ -340,11 +329,9 @@ class Scope {
|
|
340
329
|
});
|
341
330
|
return cloneNode(id);
|
342
331
|
}
|
343
|
-
|
344
332
|
generateUidIdentifier(name) {
|
345
333
|
return identifier(this.generateUid(name));
|
346
334
|
}
|
347
|
-
|
348
335
|
generateUid(name = "temp") {
|
349
336
|
name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
|
350
337
|
let uid;
|
@@ -358,7 +345,6 @@ class Scope {
|
|
358
345
|
program.uids[uid] = true;
|
359
346
|
return uid;
|
360
347
|
}
|
361
|
-
|
362
348
|
_generateUid(name, i) {
|
363
349
|
let id = name;
|
364
350
|
if (i > 1) id += i;
|
@@ -371,11 +357,9 @@ class Scope {
|
|
371
357
|
id = id.replace(/^_/, "") || defaultName || "ref";
|
372
358
|
return this.generateUid(id.slice(0, 20));
|
373
359
|
}
|
374
|
-
|
375
360
|
generateUidIdentifierBasedOnNode(node, defaultName) {
|
376
361
|
return identifier(this.generateUidBasedOnNode(node, defaultName));
|
377
362
|
}
|
378
|
-
|
379
363
|
isStatic(node) {
|
380
364
|
if (isThisExpression(node) || isSuper(node) || isTopicReference(node)) {
|
381
365
|
return true;
|
@@ -390,7 +374,6 @@ class Scope {
|
|
390
374
|
}
|
391
375
|
return false;
|
392
376
|
}
|
393
|
-
|
394
377
|
maybeGenerateMemoised(node, dontPush) {
|
395
378
|
if (this.isStatic(node)) {
|
396
379
|
return null;
|
@@ -407,23 +390,20 @@ class Scope {
|
|
407
390
|
}
|
408
391
|
checkBlockScopedCollisions(local, kind, name, id) {
|
409
392
|
if (kind === "param") return;
|
410
|
-
|
411
393
|
if (local.kind === "local") return;
|
412
|
-
const duplicate =
|
413
|
-
kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" ||
|
414
|
-
local.kind === "param" && kind === "const";
|
394
|
+
const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && kind === "const";
|
415
395
|
if (duplicate) {
|
416
396
|
throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
|
417
397
|
}
|
418
398
|
}
|
419
|
-
rename(oldName, newName
|
399
|
+
rename(oldName, newName) {
|
420
400
|
const binding = this.getBinding(oldName);
|
421
401
|
if (binding) {
|
422
|
-
newName
|
423
|
-
|
402
|
+
newName || (newName = this.generateUidIdentifier(oldName).name);
|
403
|
+
const renamer = new _renamer.default(binding, oldName, newName);
|
404
|
+
return renamer.rename(arguments[2]);
|
424
405
|
}
|
425
406
|
}
|
426
|
-
|
427
407
|
_renameFromMap(map, oldName, newName, value) {
|
428
408
|
if (map[oldName]) {
|
429
409
|
map[newName] = value;
|
@@ -448,7 +428,6 @@ class Scope {
|
|
448
428
|
} while (scope = scope.parent);
|
449
429
|
console.log(sep);
|
450
430
|
}
|
451
|
-
|
452
431
|
toArray(node, i, arrayLikeIsIterable) {
|
453
432
|
if (isIdentifier(node)) {
|
454
433
|
const binding = this.getBinding(node.name);
|
@@ -470,7 +449,6 @@ class Scope {
|
|
470
449
|
helperName = "toConsumableArray";
|
471
450
|
} else if (typeof i === "number") {
|
472
451
|
args.push(numericLiteral(i));
|
473
|
-
|
474
452
|
helperName = "slicedToArray";
|
475
453
|
} else {
|
476
454
|
helperName = "toArray";
|
@@ -479,7 +457,6 @@ class Scope {
|
|
479
457
|
args.unshift(this.hub.addHelper(helperName));
|
480
458
|
helperName = "maybeArrayLike";
|
481
459
|
}
|
482
|
-
|
483
460
|
return callExpression(this.hub.addHelper(helperName), args);
|
484
461
|
}
|
485
462
|
hasLabel(name) {
|
@@ -552,7 +529,6 @@ class Scope {
|
|
552
529
|
if (local.identifier === id) continue;
|
553
530
|
this.checkBlockScopedCollisions(local, kind, name, id);
|
554
531
|
}
|
555
|
-
|
556
532
|
if (local) {
|
557
533
|
this.registerConstantViolation(bindingPath);
|
558
534
|
} else {
|
@@ -652,11 +628,9 @@ class Scope {
|
|
652
628
|
return isPureish(node);
|
653
629
|
}
|
654
630
|
}
|
655
|
-
|
656
631
|
setData(key, val) {
|
657
632
|
return this.data[key] = val;
|
658
633
|
}
|
659
|
-
|
660
634
|
getData(key) {
|
661
635
|
let scope = this;
|
662
636
|
do {
|
@@ -664,7 +638,6 @@ class Scope {
|
|
664
638
|
if (data != null) return data;
|
665
639
|
} while (scope = scope.parent);
|
666
640
|
}
|
667
|
-
|
668
641
|
removeData(key) {
|
669
642
|
let scope = this;
|
670
643
|
do {
|
@@ -706,17 +679,14 @@ class Scope {
|
|
706
679
|
}
|
707
680
|
path.traverse(collectorVisitor, state);
|
708
681
|
this.crawling = false;
|
709
|
-
|
710
682
|
for (const path of state.assignments) {
|
711
683
|
const ids = path.getBindingIdentifiers();
|
712
684
|
for (const name of Object.keys(ids)) {
|
713
685
|
if (path.scope.getBinding(name)) continue;
|
714
686
|
programParent.addGlobal(ids[name]);
|
715
687
|
}
|
716
|
-
|
717
688
|
path.scope.registerConstantViolation(path);
|
718
689
|
}
|
719
|
-
|
720
690
|
for (const ref of state.references) {
|
721
691
|
const binding = ref.scope.getBinding(ref.node.name);
|
722
692
|
if (binding) {
|
@@ -725,7 +695,6 @@ class Scope {
|
|
725
695
|
programParent.addGlobal(ref.node);
|
726
696
|
}
|
727
697
|
}
|
728
|
-
|
729
698
|
for (const path of state.constantViolations) {
|
730
699
|
path.scope.registerConstantViolation(path);
|
731
700
|
}
|
@@ -759,7 +728,6 @@ class Scope {
|
|
759
728
|
const len = declarPath.node.declarations.push(declarator);
|
760
729
|
path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
|
761
730
|
}
|
762
|
-
|
763
731
|
getProgramParent() {
|
764
732
|
let scope = this;
|
765
733
|
do {
|
@@ -769,7 +737,6 @@ class Scope {
|
|
769
737
|
} while (scope = scope.parent);
|
770
738
|
throw new Error("Couldn't find a Program");
|
771
739
|
}
|
772
|
-
|
773
740
|
getFunctionParent() {
|
774
741
|
let scope = this;
|
775
742
|
do {
|
@@ -779,7 +746,6 @@ class Scope {
|
|
779
746
|
} while (scope = scope.parent);
|
780
747
|
return null;
|
781
748
|
}
|
782
|
-
|
783
749
|
getBlockParent() {
|
784
750
|
let scope = this;
|
785
751
|
do {
|
@@ -789,7 +755,6 @@ class Scope {
|
|
789
755
|
} while (scope = scope.parent);
|
790
756
|
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
791
757
|
}
|
792
|
-
|
793
758
|
getPatternParent() {
|
794
759
|
let scope = this;
|
795
760
|
do {
|
@@ -799,7 +764,6 @@ class Scope {
|
|
799
764
|
} while (scope = scope.parent.parent);
|
800
765
|
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
801
766
|
}
|
802
|
-
|
803
767
|
getAllBindings() {
|
804
768
|
const ids = Object.create(null);
|
805
769
|
let scope = this;
|
@@ -813,7 +777,6 @@ class Scope {
|
|
813
777
|
} while (scope);
|
814
778
|
return ids;
|
815
779
|
}
|
816
|
-
|
817
780
|
getAllBindingsOfKind(...kinds) {
|
818
781
|
const ids = Object.create(null);
|
819
782
|
for (const kind of kinds) {
|
@@ -838,9 +801,7 @@ class Scope {
|
|
838
801
|
const binding = scope.getOwnBinding(name);
|
839
802
|
if (binding) {
|
840
803
|
var _previousPath;
|
841
|
-
|
842
|
-
if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {
|
843
|
-
} else {
|
804
|
+
if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {} else {
|
844
805
|
return binding;
|
845
806
|
}
|
846
807
|
} else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
|
@@ -852,12 +813,10 @@ class Scope {
|
|
852
813
|
getOwnBinding(name) {
|
853
814
|
return this.bindings[name];
|
854
815
|
}
|
855
|
-
|
856
816
|
getBindingIdentifier(name) {
|
857
817
|
var _this$getBinding;
|
858
818
|
return (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.identifier;
|
859
819
|
}
|
860
|
-
|
861
820
|
getOwnBindingIdentifier(name) {
|
862
821
|
const binding = this.bindings[name];
|
863
822
|
return binding == null ? void 0 : binding.identifier;
|
@@ -865,7 +824,6 @@ class Scope {
|
|
865
824
|
hasOwnBinding(name) {
|
866
825
|
return !!this.getOwnBinding(name);
|
867
826
|
}
|
868
|
-
|
869
827
|
hasBinding(name, opts) {
|
870
828
|
var _opts, _opts2, _opts3;
|
871
829
|
if (!name) return false;
|
@@ -885,7 +843,6 @@ class Scope {
|
|
885
843
|
var _this$parent;
|
886
844
|
return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, opts);
|
887
845
|
}
|
888
|
-
|
889
846
|
moveBindingTo(name, scope) {
|
890
847
|
const info = this.getBinding(name);
|
891
848
|
if (info) {
|
@@ -900,7 +857,6 @@ class Scope {
|
|
900
857
|
removeBinding(name) {
|
901
858
|
var _this$getBinding2;
|
902
859
|
(_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.scope.removeOwnBinding(name);
|
903
|
-
|
904
860
|
let scope = this;
|
905
861
|
do {
|
906
862
|
if (scope.uids[name]) {
|