test/dummy/node_modules/@babel/traverse/lib/scope/index.js in disco_app-0.18.1 vs test/dummy/node_modules/@babel/traverse/lib/scope/index.js in disco_app-0.18.2
- old
+ new
@@ -3,273 +3,131 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
-var _renamer = require("./lib/renamer");
+var _includes = _interopRequireDefault(require("lodash/includes"));
-var _index = require("../index");
+var _repeat = _interopRequireDefault(require("lodash/repeat"));
-var _binding = require("./binding");
+var _renamer = _interopRequireDefault(require("./lib/renamer"));
-var _globals = require("globals");
+var _index = _interopRequireDefault(require("../index"));
-var _t = require("@babel/types");
+var _defaults = _interopRequireDefault(require("lodash/defaults"));
-var _cache = require("../cache");
+var _binding = _interopRequireDefault(require("./binding"));
-const {
- NOT_LOCAL_BINDING,
- callExpression,
- cloneNode,
- getBindingIdentifiers,
- identifier,
- isArrayExpression,
- isBinary,
- isClass,
- isClassBody,
- isClassDeclaration,
- isExportAllDeclaration,
- isExportDefaultDeclaration,
- isExportNamedDeclaration,
- isFunctionDeclaration,
- isIdentifier,
- isImportDeclaration,
- isLiteral,
- isMethod,
- isModuleDeclaration,
- isModuleSpecifier,
- isObjectExpression,
- isProperty,
- isPureish,
- isSuper,
- isTaggedTemplateExpression,
- isTemplateLiteral,
- isThisExpression,
- isUnaryExpression,
- isVariableDeclaration,
- matchesPattern,
- memberExpression,
- numericLiteral,
- toIdentifier,
- unaryExpression,
- variableDeclaration,
- variableDeclarator
-} = _t;
+var _globals = _interopRequireDefault(require("globals"));
-function gatherNodeParts(node, parts) {
- switch (node == null ? void 0 : node.type) {
- default:
- if (isModuleDeclaration(node)) {
- if ((isExportAllDeclaration(node) || isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.source) {
- gatherNodeParts(node.source, parts);
- } else if ((isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.specifiers && node.specifiers.length) {
- for (const e of node.specifiers) gatherNodeParts(e, parts);
- } else if ((isExportDefaultDeclaration(node) || isExportNamedDeclaration(node)) && node.declaration) {
- gatherNodeParts(node.declaration, parts);
- }
- } else if (isModuleSpecifier(node)) {
- gatherNodeParts(node.local, parts);
- } else if (isLiteral(node)) {
- parts.push(node.value);
- }
+var t = _interopRequireWildcard(require("@babel/types"));
- break;
+var _cache = require("../cache");
- case "MemberExpression":
- case "OptionalMemberExpression":
- case "JSXMemberExpression":
- gatherNodeParts(node.object, parts);
- gatherNodeParts(node.property, parts);
- break;
+function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
- case "Identifier":
- case "JSXIdentifier":
- parts.push(node.name);
- break;
+function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
- case "CallExpression":
- case "OptionalCallExpression":
- case "NewExpression":
- gatherNodeParts(node.callee, parts);
- break;
+function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- case "ObjectExpression":
- case "ObjectPattern":
- for (const e of node.properties) {
- gatherNodeParts(e, parts);
+function gatherNodeParts(node, parts) {
+ if (t.isModuleDeclaration(node)) {
+ if (node.source) {
+ gatherNodeParts(node.source, parts);
+ } else if (node.specifiers && node.specifiers.length) {
+ for (const specifier of node.specifiers) {
+ gatherNodeParts(specifier, parts);
}
-
- break;
-
- case "SpreadElement":
- case "RestElement":
- gatherNodeParts(node.argument, parts);
- break;
-
- case "ObjectProperty":
- case "ObjectMethod":
- case "ClassProperty":
- case "ClassMethod":
- case "ClassPrivateProperty":
- case "ClassPrivateMethod":
- gatherNodeParts(node.key, parts);
- break;
-
- case "ThisExpression":
- parts.push("this");
- break;
-
- case "Super":
- parts.push("super");
- break;
-
- case "Import":
- parts.push("import");
- break;
-
- case "DoExpression":
- parts.push("do");
- break;
-
- case "YieldExpression":
- parts.push("yield");
- gatherNodeParts(node.argument, parts);
- break;
-
- case "AwaitExpression":
- parts.push("await");
- gatherNodeParts(node.argument, parts);
- break;
-
- case "AssignmentExpression":
- gatherNodeParts(node.left, parts);
- break;
-
- case "VariableDeclarator":
- gatherNodeParts(node.id, parts);
- break;
-
- case "FunctionExpression":
- case "FunctionDeclaration":
- case "ClassExpression":
- case "ClassDeclaration":
- gatherNodeParts(node.id, parts);
- break;
-
- case "PrivateName":
- gatherNodeParts(node.id, parts);
- break;
-
- case "ParenthesizedExpression":
- gatherNodeParts(node.expression, parts);
- break;
-
- case "UnaryExpression":
- case "UpdateExpression":
- gatherNodeParts(node.argument, parts);
- break;
-
- case "MetaProperty":
- gatherNodeParts(node.meta, parts);
- gatherNodeParts(node.property, parts);
- break;
-
- case "JSXElement":
- gatherNodeParts(node.openingElement, parts);
- break;
-
- case "JSXOpeningElement":
- parts.push(node.name);
- break;
-
- case "JSXFragment":
- gatherNodeParts(node.openingFragment, parts);
- break;
-
- case "JSXOpeningFragment":
- parts.push("Fragment");
- break;
-
- case "JSXNamespacedName":
- gatherNodeParts(node.namespace, parts);
- gatherNodeParts(node.name, parts);
- break;
+ } else if (node.declaration) {
+ gatherNodeParts(node.declaration, parts);
+ }
+ } else if (t.isModuleSpecifier(node)) {
+ gatherNodeParts(node.local, parts);
+ } else if (t.isMemberExpression(node)) {
+ gatherNodeParts(node.object, parts);
+ gatherNodeParts(node.property, parts);
+ } else if (t.isIdentifier(node)) {
+ parts.push(node.name);
+ } else if (t.isLiteral(node)) {
+ parts.push(node.value);
+ } else if (t.isCallExpression(node)) {
+ gatherNodeParts(node.callee, parts);
+ } else if (t.isObjectExpression(node) || t.isObjectPattern(node)) {
+ for (const prop of node.properties) {
+ gatherNodeParts(prop.key || prop.argument, parts);
+ }
+ } else if (t.isPrivateName(node)) {
+ gatherNodeParts(node.id, parts);
+ } else if (t.isThisExpression(node)) {
+ parts.push("this");
+ } else if (t.isSuper(node)) {
+ parts.push("super");
}
}
const collectorVisitor = {
- ForStatement(path) {
- const declar = path.get("init");
+ For(path) {
+ for (const key of t.FOR_INIT_KEYS) {
+ const declar = path.get(key);
- if (declar.isVar()) {
- const {
- scope
- } = path;
- const parentScope = scope.getFunctionParent() || scope.getProgramParent();
- parentScope.registerBinding("var", declar);
+ if (declar.isVar()) {
+ const parentScope = path.scope.getFunctionParent() || path.scope.getProgramParent();
+ parentScope.registerBinding("var", declar);
+ }
}
},
Declaration(path) {
if (path.isBlockScoped()) return;
- if (path.isImportDeclaration()) return;
- if (path.isExportDeclaration()) return;
+
+ if (path.isExportDeclaration() && path.get("declaration").isDeclaration()) {
+ return;
+ }
+
const parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
parent.registerDeclaration(path);
},
- ImportDeclaration(path) {
- const parent = path.scope.getBlockParent();
- parent.registerDeclaration(path);
- },
-
ReferencedIdentifier(path, state) {
state.references.push(path);
},
ForXStatement(path, state) {
const left = path.get("left");
if (left.isPattern() || left.isIdentifier()) {
state.constantViolations.push(path);
- } else if (left.isVar()) {
- const {
- scope
- } = path;
- const parentScope = scope.getFunctionParent() || scope.getProgramParent();
- parentScope.registerBinding("var", left);
}
},
ExportDeclaration: {
exit(path) {
const {
node,
scope
} = path;
- if (isExportAllDeclaration(node)) return;
const declar = node.declaration;
- if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) {
+ if (t.isClassDeclaration(declar) || t.isFunctionDeclaration(declar)) {
const id = declar.id;
if (!id) return;
const binding = scope.getBinding(id.name);
- binding == null ? void 0 : binding.reference(path);
- } else if (isVariableDeclaration(declar)) {
+ if (binding) binding.reference(path);
+ } else if (t.isVariableDeclaration(declar)) {
for (const decl of declar.declarations) {
- for (const name of Object.keys(getBindingIdentifiers(decl))) {
+ for (const name of Object.keys(t.getBindingIdentifiers(decl))) {
const binding = scope.getBinding(name);
- binding == null ? void 0 : binding.reference(path);
+ if (binding) binding.reference(path);
}
}
}
}
},
LabeledStatement(path) {
+ path.scope.getProgramParent().addGlobal(path.node);
path.scope.getBlockParent().registerDeclaration(path);
},
AssignmentExpression(path, state) {
state.assignments.push(path);
@@ -286,91 +144,56 @@
},
BlockScoped(path) {
let scope = path.scope;
if (scope.path === path) scope = scope.parent;
- const parent = scope.getBlockParent();
- parent.registerDeclaration(path);
-
- if (path.isClassDeclaration() && path.node.id) {
- const id = path.node.id;
- const name = id.name;
- path.scope.bindings[name] = path.scope.parent.getBinding(name);
- }
+ scope.getBlockParent().registerDeclaration(path);
},
- CatchClause(path) {
- path.scope.registerBinding("let", path);
+ ClassDeclaration(path) {
+ const id = path.node.id;
+ if (!id) return;
+ const name = id.name;
+ path.scope.bindings[name] = path.scope.getBinding(name);
},
- Function(path) {
- if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
- path.scope.registerBinding("local", path.get("id"), path);
- }
+ Block(path) {
+ const paths = path.get("body");
- const params = path.get("params");
-
- for (const param of params) {
- path.scope.registerBinding("param", param);
+ for (const bodyPath of paths) {
+ if (bodyPath.isFunctionDeclaration()) {
+ path.scope.getBlockParent().registerDeclaration(bodyPath);
+ }
}
- },
-
- ClassExpression(path) {
- if (path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
- path.scope.registerBinding("local", path);
- }
}
};
let uid = 0;
class Scope {
constructor(path) {
- this.uid = void 0;
- this.path = void 0;
- this.block = void 0;
- this.labels = void 0;
- this.inited = void 0;
- this.bindings = void 0;
- this.references = void 0;
- this.globals = void 0;
- this.uids = void 0;
- this.data = void 0;
- this.crawling = void 0;
const {
node
} = path;
const cached = _cache.scope.get(node);
- if ((cached == null ? void 0 : cached.path) === path) {
+ if (cached && cached.path === path) {
return cached;
}
_cache.scope.set(node, this);
this.uid = uid++;
this.block = node;
this.path = path;
this.labels = new Map();
- this.inited = false;
}
get parent() {
- var _parent;
-
- let parent,
- path = this.path;
-
- do {
- const isKey = path.key === "key";
- path = path.parentPath;
- if (isKey && path.isMethod()) path = path.parentPath;
- if (path && path.isScope()) parent = path;
- } while (path && !parent);
-
- return (_parent = parent) == null ? void 0 : _parent.scope;
+ const parent = this.path.findParent(p => p.isScope());
+ return parent && parent.scope;
}
get parentBlock() {
return this.path.parent;
}
@@ -386,21 +209,21 @@
generateDeclaredUidIdentifier(name) {
const id = this.generateUidIdentifier(name);
this.push({
id
});
- return cloneNode(id);
+ return t.cloneNode(id);
}
generateUidIdentifier(name) {
- return identifier(this.generateUid(name));
+ return t.identifier(this.generateUid(name));
}
generateUid(name = "temp") {
- name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
+ name = t.toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
let uid;
- let i = 1;
+ let i = 0;
do {
uid = this._generateUid(name, i);
i++;
} while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
@@ -415,28 +238,38 @@
let id = name;
if (i > 1) id += i;
return `_${id}`;
}
- generateUidBasedOnNode(node, defaultName) {
+ generateUidBasedOnNode(parent, defaultName) {
+ let node = parent;
+
+ if (t.isAssignmentExpression(parent)) {
+ node = parent.left;
+ } else if (t.isVariableDeclarator(parent)) {
+ node = parent.id;
+ } else if (t.isObjectProperty(node) || t.isObjectMethod(node)) {
+ node = node.key;
+ }
+
const parts = [];
gatherNodeParts(node, parts);
let id = parts.join("$");
id = id.replace(/^_/, "") || defaultName || "ref";
return this.generateUid(id.slice(0, 20));
}
- generateUidIdentifierBasedOnNode(node, defaultName) {
- return identifier(this.generateUidBasedOnNode(node, defaultName));
+ generateUidIdentifierBasedOnNode(parent, defaultName) {
+ return t.identifier(this.generateUidBasedOnNode(parent, defaultName));
}
isStatic(node) {
- if (isThisExpression(node) || isSuper(node)) {
+ if (t.isThisExpression(node) || t.isSuper(node)) {
return true;
}
- if (isIdentifier(node)) {
+ if (t.isIdentifier(node)) {
const binding = this.getBinding(node.name);
if (binding) {
return binding.constant;
} else {
@@ -455,11 +288,11 @@
if (!dontPush) {
this.push({
id
});
- return cloneNode(id);
+ return t.cloneNode(id);
}
return id;
}
}
@@ -489,11 +322,11 @@
map[oldName] = null;
}
}
dump() {
- const sep = "-".repeat(60);
+ const sep = (0, _repeat.default)("-", 60);
console.log(sep);
let scope = this;
do {
console.log("#", scope.block.type);
@@ -510,47 +343,42 @@
} while (scope = scope.parent);
console.log(sep);
}
- toArray(node, i, arrayLikeIsIterable) {
- if (isIdentifier(node)) {
+ toArray(node, i) {
+ if (t.isIdentifier(node)) {
const binding = this.getBinding(node.name);
- if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
+ if (binding && binding.constant && binding.path.isGenericType("Array")) {
return node;
}
}
- if (isArrayExpression(node)) {
+ if (t.isArrayExpression(node)) {
return node;
}
- if (isIdentifier(node, {
+ if (t.isIdentifier(node, {
name: "arguments"
})) {
- return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
+ return t.callExpression(t.memberExpression(t.memberExpression(t.memberExpression(t.identifier("Array"), t.identifier("prototype")), t.identifier("slice")), t.identifier("call")), [node]);
}
let helperName;
const args = [node];
if (i === true) {
helperName = "toConsumableArray";
} else if (i) {
- args.push(numericLiteral(i));
+ args.push(t.numericLiteral(i));
helperName = "slicedToArray";
} else {
helperName = "toArray";
}
- if (arrayLikeIsIterable) {
- args.unshift(this.hub.addHelper(helperName));
- helperName = "maybeArrayLike";
- }
-
- return callExpression(this.hub.addHelper(helperName), args);
+ return t.callExpression(this.hub.addHelper(helperName), args);
}
hasLabel(name) {
return !!this.getLabel(name);
}
@@ -592,11 +420,11 @@
this.registerBinding("unknown", path);
}
}
buildUndefinedNode() {
- return unaryExpression("void", numericLiteral(0), true);
+ return t.unaryExpression("void", t.numericLiteral(0), true);
}
registerConstantViolation(path) {
const ids = path.getBindingIdentifiers();
@@ -621,20 +449,20 @@
const parent = this.getProgramParent();
const ids = path.getOuterBindingIdentifiers(true);
for (const name of Object.keys(ids)) {
- parent.references[name] = true;
-
for (const id of ids[name]) {
const local = this.getOwnBinding(name);
if (local) {
if (local.identifier === id) continue;
this.checkBlockScopedCollisions(local, kind, name, id);
}
+ parent.references[name] = true;
+
if (local) {
this.registerConstantViolation(bindingPath);
} else {
this.bindings[name] = new _binding.default({
identifier: id,
@@ -670,64 +498,70 @@
return false;
}
hasReference(name) {
- return !!this.getProgramParent().references[name];
+ let scope = this;
+
+ do {
+ if (scope.references[name]) return true;
+ } while (scope = scope.parent);
+
+ return false;
}
isPure(node, constantsOnly) {
- if (isIdentifier(node)) {
+ if (t.isIdentifier(node)) {
const binding = this.getBinding(node.name);
if (!binding) return false;
if (constantsOnly) return binding.constant;
return true;
- } else if (isClass(node)) {
+ } else if (t.isClass(node)) {
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
return false;
}
return this.isPure(node.body, constantsOnly);
- } else if (isClassBody(node)) {
+ } else if (t.isClassBody(node)) {
for (const method of node.body) {
if (!this.isPure(method, constantsOnly)) return false;
}
return true;
- } else if (isBinary(node)) {
+ } else if (t.isBinary(node)) {
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
- } else if (isArrayExpression(node)) {
+ } else if (t.isArrayExpression(node)) {
for (const elem of node.elements) {
if (!this.isPure(elem, constantsOnly)) return false;
}
return true;
- } else if (isObjectExpression(node)) {
+ } else if (t.isObjectExpression(node)) {
for (const prop of node.properties) {
if (!this.isPure(prop, constantsOnly)) return false;
}
return true;
- } else if (isMethod(node)) {
+ } else if (t.isClassMethod(node)) {
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
if (node.kind === "get" || node.kind === "set") return false;
return true;
- } else if (isProperty(node)) {
+ } else if (t.isProperty(node)) {
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
return this.isPure(node.value, constantsOnly);
- } else if (isUnaryExpression(node)) {
+ } else if (t.isUnaryExpression(node)) {
return this.isPure(node.argument, constantsOnly);
- } else if (isTaggedTemplateExpression(node)) {
- return matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
- } else if (isTemplateLiteral(node)) {
+ } else if (t.isTaggedTemplateExpression(node)) {
+ return t.matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
+ } else if (t.isTemplateLiteral(node)) {
for (const expression of node.expressions) {
if (!this.isPure(expression, constantsOnly)) return false;
}
return true;
} else {
- return isPureish(node);
+ return t.isPureish(node);
}
}
setData(key, val) {
return this.data[key] = val;
@@ -750,54 +584,70 @@
if (data != null) scope.data[key] = null;
} while (scope = scope.parent);
}
init() {
- if (!this.inited) {
- this.inited = true;
- this.crawl();
- }
+ if (!this.references) this.crawl();
}
crawl() {
const path = this.path;
this.references = Object.create(null);
this.bindings = Object.create(null);
this.globals = Object.create(null);
this.uids = Object.create(null);
this.data = Object.create(null);
- const programParent = this.getProgramParent();
- if (programParent.crawling) return;
- const state = {
- references: [],
- constantViolations: [],
- assignments: []
- };
- this.crawling = true;
- if (path.type !== "Program" && collectorVisitor._exploded) {
- for (const visit of collectorVisitor.enter) {
- visit(path, state);
+ if (path.isLoop()) {
+ for (const key of t.FOR_INIT_KEYS) {
+ const node = path.get(key);
+ if (node.isBlockScoped()) this.registerBinding(node.node.kind, node);
}
+ }
- const typeVisitors = collectorVisitor[path.type];
+ if (path.isFunctionExpression() && path.has("id")) {
+ if (!path.get("id").node[t.NOT_LOCAL_BINDING]) {
+ this.registerBinding("local", path.get("id"), path);
+ }
+ }
- if (typeVisitors) {
- for (const visit of typeVisitors.enter) {
- visit(path, state);
- }
+ if (path.isClassExpression() && path.has("id")) {
+ if (!path.get("id").node[t.NOT_LOCAL_BINDING]) {
+ this.registerBinding("local", path);
}
}
+ if (path.isFunction()) {
+ const params = path.get("params");
+
+ for (const param of params) {
+ this.registerBinding("param", param);
+ }
+ }
+
+ if (path.isCatchClause()) {
+ this.registerBinding("let", path);
+ }
+
+ const parent = this.getProgramParent();
+ if (parent.crawling) return;
+ const state = {
+ references: [],
+ constantViolations: [],
+ assignments: []
+ };
+ this.crawling = true;
path.traverse(collectorVisitor, state);
this.crawling = false;
for (const path of state.assignments) {
const ids = path.getBindingIdentifiers();
+ let programParent;
for (const name of Object.keys(ids)) {
if (path.scope.getBinding(name)) continue;
+ programParent = programParent || path.scope.getProgramParent();
programParent.addGlobal(ids[name]);
}
path.scope.registerConstantViolation(path);
}
@@ -806,11 +656,11 @@
const binding = ref.scope.getBinding(ref.node.name);
if (binding) {
binding.reference(ref);
} else {
- programParent.addGlobal(ref.node);
+ ref.scope.getProgramParent().addGlobal(ref.node);
}
}
for (const path of state.constantViolations) {
path.scope.registerConstantViolation(path);
@@ -838,17 +688,17 @@
const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
const dataKey = `declaration:${kind}:${blockHoist}`;
let declarPath = !unique && path.getData(dataKey);
if (!declarPath) {
- const declar = variableDeclaration(kind, []);
+ const declar = t.variableDeclaration(kind, []);
declar._blockHoist = blockHoist;
[declarPath] = path.unshiftContainer("body", [declar]);
if (!unique) path.setData(dataKey, declarPath);
}
- const declarator = variableDeclarator(opts.id, opts.init);
+ const declarator = t.variableDeclarator(opts.id, opts.init);
declarPath.node.declarations.push(declarator);
this.registerBinding(kind, declarPath.get("declarations").pop());
}
getProgramParent() {
@@ -890,26 +740,21 @@
getAllBindings() {
const ids = Object.create(null);
let scope = this;
do {
- for (const key of Object.keys(scope.bindings)) {
- if (key in ids === false) {
- ids[key] = scope.bindings[key];
- }
- }
-
+ (0, _defaults.default)(ids, scope.bindings);
scope = scope.parent;
} while (scope);
return ids;
}
- getAllBindingsOfKind(...kinds) {
+ getAllBindingsOfKind() {
const ids = Object.create(null);
- for (const kind of kinds) {
+ for (const kind of arguments) {
let scope = this;
do {
for (const name of Object.keys(scope.bindings)) {
const binding = scope.bindings[name];
@@ -927,40 +772,29 @@
return this.getBindingIdentifier(name) === node;
}
getBinding(name) {
let scope = this;
- let previousPath;
do {
const binding = scope.getOwnBinding(name);
-
- if (binding) {
- var _previousPath;
-
- if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param") {} else {
- return binding;
- }
- }
-
- previousPath = scope.path;
+ if (binding) return binding;
} while (scope = scope.parent);
}
getOwnBinding(name) {
return this.bindings[name];
}
getBindingIdentifier(name) {
- var _this$getBinding;
-
- return (_this$getBinding = this.getBinding(name)) == null ? void 0 : _this$getBinding.identifier;
+ const info = this.getBinding(name);
+ return info && info.identifier;
}
getOwnBindingIdentifier(name) {
const binding = this.bindings[name];
- return binding == null ? void 0 : binding.identifier;
+ return binding && binding.identifier;
}
hasOwnBinding(name) {
return !!this.getOwnBinding(name);
}
@@ -968,19 +802,17 @@
hasBinding(name, noGlobals) {
if (!name) return false;
if (this.hasOwnBinding(name)) return true;
if (this.parentHasBinding(name, noGlobals)) return true;
if (this.hasUid(name)) return true;
- if (!noGlobals && Scope.globals.includes(name)) return true;
- if (!noGlobals && Scope.contextVariables.includes(name)) return true;
+ if (!noGlobals && (0, _includes.default)(Scope.globals, name)) return true;
+ if (!noGlobals && (0, _includes.default)(Scope.contextVariables, name)) return true;
return false;
}
parentHasBinding(name, noGlobals) {
- var _this$parent;
-
- return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, noGlobals);
+ return this.parent && this.parent.hasBinding(name, noGlobals);
}
moveBindingTo(name, scope) {
const info = this.getBinding(name);
@@ -994,13 +826,16 @@
removeOwnBinding(name) {
delete this.bindings[name];
}
removeBinding(name) {
- var _this$getBinding2;
+ const info = this.getBinding(name);
- (_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.scope.removeOwnBinding(name);
+ if (info) {
+ info.scope.removeOwnBinding(name);
+ }
+
let scope = this;
do {
if (scope.uids[name]) {
scope.uids[name] = false;
@@ -1009,7 +844,7 @@
}
}
exports.default = Scope;
-Scope.globals = Object.keys(_globals.builtin);
+Scope.globals = Object.keys(_globals.default.builtin);
Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
\ No newline at end of file