summaryrefslogtreecommitdiff
path: root/node_modules/@babel/traverse/lib/scope/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/@babel/traverse/lib/scope/index.js')
-rw-r--r--node_modules/@babel/traverse/lib/scope/index.js99
1 files changed, 79 insertions, 20 deletions
diff --git a/node_modules/@babel/traverse/lib/scope/index.js b/node_modules/@babel/traverse/lib/scope/index.js
index ff56a2a5..f0c9748c 100644
--- a/node_modules/@babel/traverse/lib/scope/index.js
+++ b/node_modules/@babel/traverse/lib/scope/index.js
@@ -14,6 +14,7 @@ var _cache = require("../cache.js");
var _visitors = require("../visitors.js");
const {
NOT_LOCAL_BINDING,
+ assignmentExpression,
callExpression,
cloneNode,
getBindingIdentifiers,
@@ -45,6 +46,7 @@ const {
isThisExpression,
isUnaryExpression,
isVariableDeclaration,
+ expressionStatement,
matchesPattern,
memberExpression,
numericLiteral,
@@ -58,7 +60,8 @@ const {
isMetaProperty,
isPrivateName,
isExportDeclaration,
- buildUndefinedNode
+ buildUndefinedNode,
+ sequenceExpression
} = _t;
function gatherNodeParts(node, parts) {
switch (node == null ? void 0 : node.type) {
@@ -268,13 +271,13 @@ const collectorVisitor = {
for (const param of params) {
path.scope.registerBinding("param", param);
}
- if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
+ if (path.isFunctionExpression() && path.node.id && !path.node.id[NOT_LOCAL_BINDING]) {
path.scope.registerBinding("local", path.get("id"), path);
}
},
ClassExpression(path) {
- if (path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
- path.scope.registerBinding("local", path);
+ if (path.node.id && !path.node.id[NOT_LOCAL_BINDING]) {
+ path.scope.registerBinding("local", path.get("id"), path);
}
},
TSTypeAnnotation(path) {
@@ -314,10 +317,11 @@ class Scope {
let parent,
path = this.path;
do {
+ var _path;
const shouldSkip = path.key === "key" || path.listKey === "decorators";
path = path.parentPath;
if (shouldSkip && path.isMethod()) path = path.parentPath;
- if (path && path.isScope()) parent = path;
+ if ((_path = path) != null && _path.isScope()) parent = path;
} while (path && !parent);
return (_parent = parent) == null ? void 0 : _parent.scope;
}
@@ -341,7 +345,7 @@ class Scope {
return identifier(this.generateUid(name));
}
generateUid(name = "temp") {
- name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
+ name = toIdentifier(name).replace(/^_+/, "").replace(/\d+$/g, "");
let uid;
let i = 1;
do {
@@ -514,7 +518,7 @@ class Scope {
return buildUndefinedNode();
}
registerConstantViolation(path) {
- const ids = path.getBindingIdentifiers();
+ const ids = path.getAssignmentIdentifiers();
for (const name of Object.keys(ids)) {
var _this$getBinding;
(_this$getBinding = this.getBinding(name)) == null || _this$getBinding.reassign(path);
@@ -540,7 +544,7 @@ class Scope {
this.checkBlockScopedCollisions(local, kind, name, id);
}
if (local) {
- this.registerConstantViolation(bindingPath);
+ local.reassign(bindingPath);
} else {
this.bindings[name] = new _binding.default({
identifier: id,
@@ -700,7 +704,7 @@ class Scope {
path.traverse(collectorVisitor, state);
this.crawling = false;
for (const path of state.assignments) {
- const ids = path.getBindingIdentifiers();
+ const ids = path.getAssignmentIdentifiers();
for (const name of Object.keys(ids)) {
if (path.scope.getBinding(name)) continue;
programParent.addGlobal(ids[name]);
@@ -856,18 +860,24 @@ class Scope {
return !!this.getOwnBinding(name);
}
hasBinding(name, opts) {
- var _opts, _opts2, _opts3;
if (!name) return false;
- if (this.hasOwnBinding(name)) return true;
- {
- if (typeof opts === "boolean") opts = {
- noGlobals: opts
- };
- }
- if (this.parentHasBinding(name, opts)) return true;
- if (!((_opts = opts) != null && _opts.noUids) && this.hasUid(name)) return true;
- if (!((_opts2 = opts) != null && _opts2.noGlobals) && Scope.globals.includes(name)) return true;
- if (!((_opts3 = opts) != null && _opts3.noGlobals) && Scope.contextVariables.includes(name)) return true;
+ let scope = this;
+ do {
+ if (scope.hasOwnBinding(name)) {
+ return true;
+ }
+ } while (scope = scope.parent);
+ let noGlobals;
+ let noUids;
+ if (typeof opts === "object") {
+ noGlobals = opts.noGlobals;
+ noUids = opts.noUids;
+ } else if (typeof opts === "boolean") {
+ noGlobals = opts;
+ }
+ if (!noUids && this.hasUid(name)) return true;
+ if (!noGlobals && Scope.globals.includes(name)) return true;
+ if (!noGlobals && Scope.contextVariables.includes(name)) return true;
return false;
}
parentHasBinding(name, opts) {
@@ -895,6 +905,55 @@ class Scope {
}
} while (scope = scope.parent);
}
+ hoistVariables(emit = id => this.push({
+ id
+ })) {
+ this.crawl();
+ const seen = new Set();
+ for (const name of Object.keys(this.bindings)) {
+ const binding = this.bindings[name];
+ if (!binding) continue;
+ const {
+ path
+ } = binding;
+ if (!path.isVariableDeclarator()) continue;
+ const {
+ parent,
+ parentPath
+ } = path;
+ if (parent.kind !== "var" || seen.has(parent)) continue;
+ seen.add(path.parent);
+ let firstId;
+ const init = [];
+ for (const decl of parent.declarations) {
+ var _firstId;
+ (_firstId = firstId) != null ? _firstId : firstId = decl.id;
+ if (decl.init) {
+ init.push(assignmentExpression("=", decl.id, decl.init));
+ }
+ const ids = Object.keys(getBindingIdentifiers(decl, false, true, true));
+ for (const name of ids) {
+ emit(identifier(name), decl.init != null);
+ }
+ }
+ if (parentPath.parentPath.isFor({
+ left: parent
+ })) {
+ parentPath.replaceWith(firstId);
+ } else if (init.length === 0) {
+ parentPath.remove();
+ } else {
+ const expr = init.length === 1 ? init[0] : sequenceExpression(init);
+ if (parentPath.parentPath.isForStatement({
+ init: parent
+ })) {
+ parentPath.replaceWith(expr);
+ } else {
+ parentPath.replaceWith(expressionStatement(expr));
+ }
+ }
+ }
+ }
}
exports.default = Scope;
Scope.globals = Object.keys(_globals.builtin);