2023-12-18 13:12:25 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.default = void 0;
|
|
|
|
|
|
|
|
function _includes() {
|
2024-01-16 21:26:16 +08:00
|
|
|
var data = _interopRequireDefault(require("lodash/includes"));
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_includes = function _includes() {
|
2023-12-18 13:12:25 +08:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _repeat() {
|
2024-01-16 21:26:16 +08:00
|
|
|
var data = _interopRequireDefault(require("lodash/repeat"));
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_repeat = function _repeat() {
|
2023-12-18 13:12:25 +08:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
var _renamer = _interopRequireDefault(require("./lib/renamer"));
|
|
|
|
|
|
|
|
var _index = _interopRequireDefault(require("../index"));
|
|
|
|
|
|
|
|
function _defaults() {
|
2024-01-16 21:26:16 +08:00
|
|
|
var data = _interopRequireDefault(require("lodash/defaults"));
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_defaults = function _defaults() {
|
2023-12-18 13:12:25 +08:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var _binding2 = _interopRequireDefault(require("./binding"));
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
function _globals() {
|
2024-01-16 21:26:16 +08:00
|
|
|
var data = _interopRequireDefault(require("globals"));
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_globals = function _globals() {
|
2023-12-18 13:12:25 +08:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function t() {
|
2024-01-16 21:26:16 +08:00
|
|
|
var data = _interopRequireWildcard(require("@babel/types"));
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
t = function t() {
|
2023-12-18 13:12:25 +08:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
var _cache = require("../cache");
|
|
|
|
|
|
|
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
|
|
|
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
|
|
|
|
|
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
|
|
|
2023-12-18 13:12:25 +08:00
|
|
|
function gatherNodeParts(node, parts) {
|
|
|
|
if (t().isModuleDeclaration(node)) {
|
|
|
|
if (node.source) {
|
|
|
|
gatherNodeParts(node.source, parts);
|
|
|
|
} else if (node.specifiers && node.specifiers.length) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var _arr = node.specifiers;
|
|
|
|
|
|
|
|
for (var _i = 0; _i < _arr.length; _i++) {
|
|
|
|
var specifier = _arr[_i];
|
2023-12-18 13:12:25 +08:00
|
|
|
gatherNodeParts(specifier, parts);
|
|
|
|
}
|
|
|
|
} 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)) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var _arr2 = node.properties;
|
|
|
|
|
|
|
|
for (var _i2 = 0; _i2 < _arr2.length; _i2++) {
|
|
|
|
var prop = _arr2[_i2];
|
2023-12-18 13:12:25 +08:00
|
|
|
gatherNodeParts(prop.key || prop.argument, parts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var collectorVisitor = {
|
|
|
|
For: function For(path) {
|
|
|
|
var _arr3 = t().FOR_INIT_KEYS;
|
|
|
|
|
|
|
|
for (var _i3 = 0; _i3 < _arr3.length; _i3++) {
|
|
|
|
var key = _arr3[_i3];
|
|
|
|
var declar = path.get(key);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (declar.isVar()) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var parentScope = path.scope.getFunctionParent() || path.scope.getProgramParent();
|
2023-12-18 13:12:25 +08:00
|
|
|
parentScope.registerBinding("var", declar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2024-01-16 21:26:16 +08:00
|
|
|
Declaration: function Declaration(path) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (path.isBlockScoped()) return;
|
|
|
|
|
|
|
|
if (path.isExportDeclaration() && path.get("declaration").isDeclaration()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
|
2023-12-18 13:12:25 +08:00
|
|
|
parent.registerDeclaration(path);
|
|
|
|
},
|
2024-01-16 21:26:16 +08:00
|
|
|
ReferencedIdentifier: function ReferencedIdentifier(path, state) {
|
2023-12-18 13:12:25 +08:00
|
|
|
state.references.push(path);
|
|
|
|
},
|
2024-01-16 21:26:16 +08:00
|
|
|
ForXStatement: function ForXStatement(path, state) {
|
|
|
|
var left = path.get("left");
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (left.isPattern() || left.isIdentifier()) {
|
|
|
|
state.constantViolations.push(path);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ExportDeclaration: {
|
2024-01-16 21:26:16 +08:00
|
|
|
exit: function exit(path) {
|
|
|
|
var node = path.node,
|
|
|
|
scope = path.scope;
|
|
|
|
var declar = node.declaration;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (t().isClassDeclaration(declar) || t().isFunctionDeclaration(declar)) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var _id = declar.id;
|
|
|
|
if (!_id) return;
|
|
|
|
var binding = scope.getBinding(_id.name);
|
2023-12-18 13:12:25 +08:00
|
|
|
if (binding) binding.reference(path);
|
|
|
|
} else if (t().isVariableDeclaration(declar)) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var _arr4 = declar.declarations;
|
|
|
|
|
|
|
|
for (var _i4 = 0; _i4 < _arr4.length; _i4++) {
|
|
|
|
var decl = _arr4[_i4];
|
|
|
|
var ids = t().getBindingIdentifiers(decl);
|
|
|
|
|
|
|
|
for (var name in ids) {
|
|
|
|
var _binding = scope.getBinding(name);
|
|
|
|
|
|
|
|
if (_binding) _binding.reference(path);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2024-01-16 21:26:16 +08:00
|
|
|
LabeledStatement: function LabeledStatement(path) {
|
2023-12-18 13:12:25 +08:00
|
|
|
path.scope.getProgramParent().addGlobal(path.node);
|
|
|
|
path.scope.getBlockParent().registerDeclaration(path);
|
|
|
|
},
|
2024-01-16 21:26:16 +08:00
|
|
|
AssignmentExpression: function AssignmentExpression(path, state) {
|
2023-12-18 13:12:25 +08:00
|
|
|
state.assignments.push(path);
|
|
|
|
},
|
2024-01-16 21:26:16 +08:00
|
|
|
UpdateExpression: function UpdateExpression(path, state) {
|
2023-12-18 13:12:25 +08:00
|
|
|
state.constantViolations.push(path);
|
|
|
|
},
|
2024-01-16 21:26:16 +08:00
|
|
|
UnaryExpression: function UnaryExpression(path, state) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (path.node.operator === "delete") {
|
|
|
|
state.constantViolations.push(path);
|
|
|
|
}
|
|
|
|
},
|
2024-01-16 21:26:16 +08:00
|
|
|
BlockScoped: function BlockScoped(path) {
|
|
|
|
var scope = path.scope;
|
2023-12-18 13:12:25 +08:00
|
|
|
if (scope.path === path) scope = scope.parent;
|
|
|
|
scope.getBlockParent().registerDeclaration(path);
|
|
|
|
},
|
2024-01-16 21:26:16 +08:00
|
|
|
ClassDeclaration: function ClassDeclaration(path) {
|
|
|
|
var id = path.node.id;
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!id) return;
|
2024-01-16 21:26:16 +08:00
|
|
|
var name = id.name;
|
2023-12-18 13:12:25 +08:00
|
|
|
path.scope.bindings[name] = path.scope.getBinding(name);
|
|
|
|
},
|
2024-01-16 21:26:16 +08:00
|
|
|
Block: function Block(path) {
|
|
|
|
var paths = path.get("body");
|
|
|
|
var _arr5 = paths;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var _i5 = 0; _i5 < _arr5.length; _i5++) {
|
|
|
|
var bodyPath = _arr5[_i5];
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (bodyPath.isFunctionDeclaration()) {
|
|
|
|
path.scope.getBlockParent().registerDeclaration(bodyPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
var uid = 0;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var Scope = function () {
|
|
|
|
function Scope(path) {
|
|
|
|
var node = path.node;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var cached = _cache.scope.get(node);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var _proto = Scope.prototype;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.traverse = function traverse(node, opts, state) {
|
2023-12-18 13:12:25 +08:00
|
|
|
(0, _index.default)(node, opts, this, state, this.path);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.generateDeclaredUidIdentifier = function generateDeclaredUidIdentifier(name) {
|
|
|
|
var id = this.generateUidIdentifier(name);
|
2023-12-18 13:12:25 +08:00
|
|
|
this.push({
|
2024-01-16 21:26:16 +08:00
|
|
|
id: id
|
2023-12-18 13:12:25 +08:00
|
|
|
});
|
|
|
|
return t().cloneNode(id);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.generateUidIdentifier = function generateUidIdentifier(name) {
|
2023-12-18 13:12:25 +08:00
|
|
|
return t().identifier(this.generateUid(name));
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto.generateUid = function generateUid(name) {
|
|
|
|
if (name === void 0) {
|
|
|
|
name = "temp";
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
name = t().toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
|
2024-01-16 21:26:16 +08:00
|
|
|
var uid;
|
|
|
|
var i = 0;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
uid = this._generateUid(name, i);
|
|
|
|
i++;
|
|
|
|
} while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var program = this.getProgramParent();
|
2023-12-18 13:12:25 +08:00
|
|
|
program.references[uid] = true;
|
|
|
|
program.uids[uid] = true;
|
|
|
|
return uid;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._generateUid = function _generateUid(name, i) {
|
|
|
|
var id = name;
|
2023-12-18 13:12:25 +08:00
|
|
|
if (i > 1) id += i;
|
2024-01-16 21:26:16 +08:00
|
|
|
return "_" + id;
|
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.generateUidBasedOnNode = function generateUidBasedOnNode(parent, defaultName) {
|
|
|
|
var node = parent;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var parts = [];
|
2023-12-18 13:12:25 +08:00
|
|
|
gatherNodeParts(node, parts);
|
2024-01-16 21:26:16 +08:00
|
|
|
var id = parts.join("$");
|
2023-12-18 13:12:25 +08:00
|
|
|
id = id.replace(/^_/, "") || defaultName || "ref";
|
|
|
|
return this.generateUid(id.slice(0, 20));
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.generateUidIdentifierBasedOnNode = function generateUidIdentifierBasedOnNode(parent, defaultName) {
|
2023-12-18 13:12:25 +08:00
|
|
|
return t().identifier(this.generateUidBasedOnNode(parent, defaultName));
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.isStatic = function isStatic(node) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (t().isThisExpression(node) || t().isSuper(node)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t().isIdentifier(node)) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var binding = this.getBinding(node.name);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (binding) {
|
|
|
|
return binding.constant;
|
|
|
|
} else {
|
|
|
|
return this.hasBinding(node.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.maybeGenerateMemoised = function maybeGenerateMemoised(node, dontPush) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (this.isStatic(node)) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2024-01-16 21:26:16 +08:00
|
|
|
var _id2 = this.generateUidIdentifierBasedOnNode(node);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (!dontPush) {
|
|
|
|
this.push({
|
2024-01-16 21:26:16 +08:00
|
|
|
id: _id2
|
2023-12-18 13:12:25 +08:00
|
|
|
});
|
2024-01-16 21:26:16 +08:00
|
|
|
return t().cloneNode(_id2);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
return _id2;
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.checkBlockScopedCollisions = function checkBlockScopedCollisions(local, kind, name, id) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (kind === "param") return;
|
|
|
|
if (local.kind === "local") return;
|
2024-01-16 21:26:16 +08:00
|
|
|
if (kind === "hoisted" && local.kind === "let") return;
|
|
|
|
var duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && (kind === "let" || kind === "const");
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (duplicate) {
|
2024-01-16 21:26:16 +08:00
|
|
|
throw this.hub.file.buildCodeFrameError(id, "Duplicate declaration \"" + name + "\"", TypeError);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.rename = function rename(oldName, newName, block) {
|
|
|
|
var binding = this.getBinding(oldName);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (binding) {
|
|
|
|
newName = newName || this.generateUidIdentifier(oldName).name;
|
|
|
|
return new _renamer.default(binding, oldName, newName).rename(block);
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto._renameFromMap = function _renameFromMap(map, oldName, newName, value) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (map[oldName]) {
|
|
|
|
map[newName] = value;
|
|
|
|
map[oldName] = null;
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.dump = function dump() {
|
|
|
|
var sep = (0, _repeat().default)("-", 60);
|
2023-12-18 13:12:25 +08:00
|
|
|
console.log(sep);
|
2024-01-16 21:26:16 +08:00
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
console.log("#", scope.block.type);
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var name in scope.bindings) {
|
|
|
|
var binding = scope.bindings[name];
|
2023-12-18 13:12:25 +08:00
|
|
|
console.log(" -", name, {
|
|
|
|
constant: binding.constant,
|
|
|
|
references: binding.references,
|
|
|
|
violations: binding.constantViolations.length,
|
|
|
|
kind: binding.kind
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} while (scope = scope.parent);
|
|
|
|
|
|
|
|
console.log(sep);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto.toArray = function toArray(node, i) {
|
|
|
|
var file = this.hub.file;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (t().isIdentifier(node)) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var binding = this.getBinding(node.name);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (binding && binding.constant && binding.path.isGenericType("Array")) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t().isArrayExpression(node)) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t().isIdentifier(node, {
|
|
|
|
name: "arguments"
|
|
|
|
})) {
|
|
|
|
return t().callExpression(t().memberExpression(t().memberExpression(t().memberExpression(t().identifier("Array"), t().identifier("prototype")), t().identifier("slice")), t().identifier("call")), [node]);
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var helperName;
|
|
|
|
var args = [node];
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (i === true) {
|
|
|
|
helperName = "toConsumableArray";
|
|
|
|
} else if (i) {
|
|
|
|
args.push(t().numericLiteral(i));
|
|
|
|
helperName = "slicedToArray";
|
|
|
|
} else {
|
|
|
|
helperName = "toArray";
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
return t().callExpression(file.addHelper(helperName), args);
|
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.hasLabel = function hasLabel(name) {
|
2023-12-18 13:12:25 +08:00
|
|
|
return !!this.getLabel(name);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.getLabel = function getLabel(name) {
|
2023-12-18 13:12:25 +08:00
|
|
|
return this.labels.get(name);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.registerLabel = function registerLabel(path) {
|
2023-12-18 13:12:25 +08:00
|
|
|
this.labels.set(path.node.label.name, path);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto.registerDeclaration = function registerDeclaration(path) {
|
|
|
|
if (path.isFlow()) return;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (path.isLabeledStatement()) {
|
|
|
|
this.registerLabel(path);
|
|
|
|
} else if (path.isFunctionDeclaration()) {
|
|
|
|
this.registerBinding("hoisted", path.get("id"), path);
|
|
|
|
} else if (path.isVariableDeclaration()) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var declarations = path.get("declarations");
|
|
|
|
var _arr6 = declarations;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var _i6 = 0; _i6 < _arr6.length; _i6++) {
|
|
|
|
var declar = _arr6[_i6];
|
2023-12-18 13:12:25 +08:00
|
|
|
this.registerBinding(path.node.kind, declar);
|
|
|
|
}
|
|
|
|
} else if (path.isClassDeclaration()) {
|
|
|
|
this.registerBinding("let", path);
|
|
|
|
} else if (path.isImportDeclaration()) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var specifiers = path.get("specifiers");
|
|
|
|
var _arr7 = specifiers;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var _i7 = 0; _i7 < _arr7.length; _i7++) {
|
|
|
|
var specifier = _arr7[_i7];
|
2023-12-18 13:12:25 +08:00
|
|
|
this.registerBinding("module", specifier);
|
|
|
|
}
|
|
|
|
} else if (path.isExportDeclaration()) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var _declar = path.get("declaration");
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
if (_declar.isClassDeclaration() || _declar.isFunctionDeclaration() || _declar.isVariableDeclaration()) {
|
|
|
|
this.registerDeclaration(_declar);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.registerBinding("unknown", path);
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.buildUndefinedNode = function buildUndefinedNode() {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (this.hasBinding("undefined")) {
|
|
|
|
return t().unaryExpression("void", t().numericLiteral(0), true);
|
|
|
|
} else {
|
|
|
|
return t().identifier("undefined");
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.registerConstantViolation = function registerConstantViolation(path) {
|
|
|
|
var ids = path.getBindingIdentifiers();
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var name in ids) {
|
|
|
|
var binding = this.getBinding(name);
|
2023-12-18 13:12:25 +08:00
|
|
|
if (binding) binding.reassign(path);
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_proto.registerBinding = function registerBinding(kind, path, bindingPath) {
|
|
|
|
if (bindingPath === void 0) {
|
|
|
|
bindingPath = path;
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (!kind) throw new ReferenceError("no `kind`");
|
|
|
|
|
|
|
|
if (path.isVariableDeclaration()) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var declarators = path.get("declarations");
|
|
|
|
|
|
|
|
for (var _iterator = declarators, _isArray = Array.isArray(_iterator), _i8 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
|
|
|
var _ref;
|
|
|
|
|
|
|
|
if (_isArray) {
|
|
|
|
if (_i8 >= _iterator.length) break;
|
|
|
|
_ref = _iterator[_i8++];
|
|
|
|
} else {
|
|
|
|
_i8 = _iterator.next();
|
|
|
|
if (_i8.done) break;
|
|
|
|
_ref = _i8.value;
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var declar = _ref;
|
2023-12-18 13:12:25 +08:00
|
|
|
this.registerBinding(kind, declar);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var parent = this.getProgramParent();
|
|
|
|
var ids = path.getBindingIdentifiers(true);
|
|
|
|
|
|
|
|
for (var name in ids) {
|
|
|
|
var _arr8 = ids[name];
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var _i9 = 0; _i9 < _arr8.length; _i9++) {
|
|
|
|
var _id3 = _arr8[_i9];
|
|
|
|
var local = this.getOwnBinding(name);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (local) {
|
2024-01-16 21:26:16 +08:00
|
|
|
if (local.identifier === _id3) continue;
|
|
|
|
this.checkBlockScopedCollisions(local, kind, name, _id3);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
parent.references[name] = true;
|
|
|
|
|
|
|
|
if (local) {
|
|
|
|
this.registerConstantViolation(bindingPath);
|
|
|
|
} else {
|
2024-01-16 21:26:16 +08:00
|
|
|
this.bindings[name] = new _binding2.default({
|
|
|
|
identifier: _id3,
|
2023-12-18 13:12:25 +08:00
|
|
|
scope: this,
|
|
|
|
path: bindingPath,
|
|
|
|
kind: kind
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.addGlobal = function addGlobal(node) {
|
2023-12-18 13:12:25 +08:00
|
|
|
this.globals[node.name] = node;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.hasUid = function hasUid(name) {
|
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (scope.uids[name]) return true;
|
|
|
|
} while (scope = scope.parent);
|
|
|
|
|
|
|
|
return false;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.hasGlobal = function hasGlobal(name) {
|
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (scope.globals[name]) return true;
|
|
|
|
} while (scope = scope.parent);
|
|
|
|
|
|
|
|
return false;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.hasReference = function hasReference(name) {
|
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (scope.references[name]) return true;
|
|
|
|
} while (scope = scope.parent);
|
|
|
|
|
|
|
|
return false;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.isPure = function isPure(node, constantsOnly) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (t().isIdentifier(node)) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var binding = this.getBinding(node.name);
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!binding) return false;
|
|
|
|
if (constantsOnly) return binding.constant;
|
|
|
|
return true;
|
|
|
|
} else if (t().isClass(node)) {
|
|
|
|
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.isPure(node.body, constantsOnly);
|
|
|
|
} else if (t().isClassBody(node)) {
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var _iterator2 = node.body, _isArray2 = Array.isArray(_iterator2), _i10 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
|
|
|
|
var _ref2;
|
|
|
|
|
|
|
|
if (_isArray2) {
|
|
|
|
if (_i10 >= _iterator2.length) break;
|
|
|
|
_ref2 = _iterator2[_i10++];
|
|
|
|
} else {
|
|
|
|
_i10 = _iterator2.next();
|
|
|
|
if (_i10.done) break;
|
|
|
|
_ref2 = _i10.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var method = _ref2;
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!this.isPure(method, constantsOnly)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else if (t().isBinary(node)) {
|
|
|
|
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
|
|
|
|
} else if (t().isArrayExpression(node)) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var _arr9 = node.elements;
|
|
|
|
|
|
|
|
for (var _i11 = 0; _i11 < _arr9.length; _i11++) {
|
|
|
|
var elem = _arr9[_i11];
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!this.isPure(elem, constantsOnly)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else if (t().isObjectExpression(node)) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var _arr10 = node.properties;
|
|
|
|
|
|
|
|
for (var _i12 = 0; _i12 < _arr10.length; _i12++) {
|
|
|
|
var prop = _arr10[_i12];
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!this.isPure(prop, constantsOnly)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} 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;
|
2024-01-16 21:26:16 +08:00
|
|
|
} else if (t().isClassProperty(node) || t().isObjectProperty(node)) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
|
|
|
return this.isPure(node.value, constantsOnly);
|
|
|
|
} else if (t().isUnaryExpression(node)) {
|
|
|
|
return this.isPure(node.argument, constantsOnly);
|
|
|
|
} 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)) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var _arr11 = node.expressions;
|
|
|
|
|
|
|
|
for (var _i13 = 0; _i13 < _arr11.length; _i13++) {
|
|
|
|
var expression = _arr11[_i13];
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!this.isPure(expression, constantsOnly)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return t().isPureish(node);
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.setData = function setData(key, val) {
|
2023-12-18 13:12:25 +08:00
|
|
|
return this.data[key] = val;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.getData = function getData(key) {
|
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
2024-01-16 21:26:16 +08:00
|
|
|
var data = scope.data[key];
|
2023-12-18 13:12:25 +08:00
|
|
|
if (data != null) return data;
|
|
|
|
} while (scope = scope.parent);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.removeData = function removeData(key) {
|
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
2024-01-16 21:26:16 +08:00
|
|
|
var data = scope.data[key];
|
2023-12-18 13:12:25 +08:00
|
|
|
if (data != null) scope.data[key] = null;
|
|
|
|
} while (scope = scope.parent);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.init = function init() {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!this.references) this.crawl();
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.crawl = function crawl() {
|
|
|
|
var path = this.path;
|
2023-12-18 13:12:25 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
if (path.isLoop()) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var _arr12 = t().FOR_INIT_KEYS;
|
|
|
|
|
|
|
|
for (var _i14 = 0; _i14 < _arr12.length; _i14++) {
|
|
|
|
var key = _arr12[_i14];
|
|
|
|
var node = path.get(key);
|
2023-12-18 13:12:25 +08:00
|
|
|
if (node.isBlockScoped()) this.registerBinding(node.node.kind, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.isFunctionExpression() && path.has("id")) {
|
|
|
|
if (!path.get("id").node[t().NOT_LOCAL_BINDING]) {
|
|
|
|
this.registerBinding("local", path.get("id"), path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.isClassExpression() && path.has("id")) {
|
|
|
|
if (!path.get("id").node[t().NOT_LOCAL_BINDING]) {
|
|
|
|
this.registerBinding("local", path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.isFunction()) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var params = path.get("params");
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var _iterator3 = params, _isArray3 = Array.isArray(_iterator3), _i15 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
|
|
|
|
var _ref3;
|
|
|
|
|
|
|
|
if (_isArray3) {
|
|
|
|
if (_i15 >= _iterator3.length) break;
|
|
|
|
_ref3 = _iterator3[_i15++];
|
|
|
|
} else {
|
|
|
|
_i15 = _iterator3.next();
|
|
|
|
if (_i15.done) break;
|
|
|
|
_ref3 = _i15.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var param = _ref3;
|
2023-12-18 13:12:25 +08:00
|
|
|
this.registerBinding("param", param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.isCatchClause()) {
|
|
|
|
this.registerBinding("let", path);
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var parent = this.getProgramParent();
|
2023-12-18 13:12:25 +08:00
|
|
|
if (parent.crawling) return;
|
2024-01-16 21:26:16 +08:00
|
|
|
var state = {
|
2023-12-18 13:12:25 +08:00
|
|
|
references: [],
|
|
|
|
constantViolations: [],
|
|
|
|
assignments: []
|
|
|
|
};
|
|
|
|
this.crawling = true;
|
|
|
|
path.traverse(collectorVisitor, state);
|
|
|
|
this.crawling = false;
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var _iterator4 = state.assignments, _isArray4 = Array.isArray(_iterator4), _i16 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
|
|
|
|
var _ref4;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
if (_isArray4) {
|
|
|
|
if (_i16 >= _iterator4.length) break;
|
|
|
|
_ref4 = _iterator4[_i16++];
|
|
|
|
} else {
|
|
|
|
_i16 = _iterator4.next();
|
|
|
|
if (_i16.done) break;
|
|
|
|
_ref4 = _i16.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var _path = _ref4;
|
|
|
|
|
|
|
|
var ids = _path.getBindingIdentifiers();
|
|
|
|
|
|
|
|
var programParent = void 0;
|
|
|
|
|
|
|
|
for (var name in ids) {
|
|
|
|
if (_path.scope.getBinding(name)) continue;
|
|
|
|
programParent = programParent || _path.scope.getProgramParent();
|
2023-12-18 13:12:25 +08:00
|
|
|
programParent.addGlobal(ids[name]);
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_path.scope.registerConstantViolation(_path);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var _iterator5 = state.references, _isArray5 = Array.isArray(_iterator5), _i17 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
|
|
|
|
var _ref5;
|
|
|
|
|
|
|
|
if (_isArray5) {
|
|
|
|
if (_i17 >= _iterator5.length) break;
|
|
|
|
_ref5 = _iterator5[_i17++];
|
|
|
|
} else {
|
|
|
|
_i17 = _iterator5.next();
|
|
|
|
if (_i17.done) break;
|
|
|
|
_ref5 = _i17.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ref = _ref5;
|
|
|
|
var binding = ref.scope.getBinding(ref.node.name);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (binding) {
|
|
|
|
binding.reference(ref);
|
|
|
|
} else {
|
|
|
|
ref.scope.getProgramParent().addGlobal(ref.node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var _iterator6 = state.constantViolations, _isArray6 = Array.isArray(_iterator6), _i18 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
|
|
|
|
var _ref6;
|
|
|
|
|
|
|
|
if (_isArray6) {
|
|
|
|
if (_i18 >= _iterator6.length) break;
|
|
|
|
_ref6 = _iterator6[_i18++];
|
|
|
|
} else {
|
|
|
|
_i18 = _iterator6.next();
|
|
|
|
if (_i18.done) break;
|
|
|
|
_ref6 = _i18.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var _path2 = _ref6;
|
|
|
|
|
|
|
|
_path2.scope.registerConstantViolation(_path2);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.push = function push(opts) {
|
|
|
|
var path = this.path;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (!path.isBlockStatement() && !path.isProgram()) {
|
|
|
|
path = this.getBlockParent().path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.isSwitchStatement()) {
|
|
|
|
path = (this.getFunctionParent() || this.getProgramParent()).path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
|
|
|
|
path.ensureBlock();
|
|
|
|
path = path.get("body");
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var unique = opts.unique;
|
|
|
|
var kind = opts.kind || "var";
|
|
|
|
var blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
|
|
|
|
var dataKey = "declaration:" + kind + ":" + blockHoist;
|
|
|
|
var declarPath = !unique && path.getData(dataKey);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (!declarPath) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var declar = t().variableDeclaration(kind, []);
|
2023-12-18 13:12:25 +08:00
|
|
|
declar._blockHoist = blockHoist;
|
2024-01-16 21:26:16 +08:00
|
|
|
|
|
|
|
var _path$unshiftContaine = path.unshiftContainer("body", [declar]);
|
|
|
|
|
|
|
|
declarPath = _path$unshiftContaine[0];
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!unique) path.setData(dataKey, declarPath);
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var declarator = t().variableDeclarator(opts.id, opts.init);
|
2023-12-18 13:12:25 +08:00
|
|
|
declarPath.node.declarations.push(declarator);
|
|
|
|
this.registerBinding(kind, declarPath.get("declarations").pop());
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.getProgramParent = function getProgramParent() {
|
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (scope.path.isProgram()) {
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
} while (scope = scope.parent);
|
|
|
|
|
|
|
|
throw new Error("Couldn't find a Program");
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.getFunctionParent = function getFunctionParent() {
|
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (scope.path.isFunctionParent()) {
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
} while (scope = scope.parent);
|
|
|
|
|
|
|
|
return null;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.getBlockParent = function getBlockParent() {
|
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (scope.path.isBlockParent()) {
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
} while (scope = scope.parent);
|
|
|
|
|
|
|
|
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.getAllBindings = function getAllBindings() {
|
|
|
|
var ids = Object.create(null);
|
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
(0, _defaults().default)(ids, scope.bindings);
|
|
|
|
scope = scope.parent;
|
|
|
|
} while (scope);
|
|
|
|
|
|
|
|
return ids;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.getAllBindingsOfKind = function getAllBindingsOfKind() {
|
|
|
|
var ids = Object.create(null);
|
|
|
|
var _arr13 = arguments;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var _i19 = 0; _i19 < _arr13.length; _i19++) {
|
|
|
|
var kind = _arr13[_i19];
|
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
2024-01-16 21:26:16 +08:00
|
|
|
for (var name in scope.bindings) {
|
|
|
|
var binding = scope.bindings[name];
|
2023-12-18 13:12:25 +08:00
|
|
|
if (binding.kind === kind) ids[name] = binding;
|
|
|
|
}
|
|
|
|
|
|
|
|
scope = scope.parent;
|
|
|
|
} while (scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.bindingIdentifierEquals = function bindingIdentifierEquals(name, node) {
|
2023-12-18 13:12:25 +08:00
|
|
|
return this.getBindingIdentifier(name) === node;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.getBinding = function getBinding(name) {
|
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
2024-01-16 21:26:16 +08:00
|
|
|
var binding = scope.getOwnBinding(name);
|
2023-12-18 13:12:25 +08:00
|
|
|
if (binding) return binding;
|
|
|
|
} while (scope = scope.parent);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.getOwnBinding = function getOwnBinding(name) {
|
2023-12-18 13:12:25 +08:00
|
|
|
return this.bindings[name];
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.getBindingIdentifier = function getBindingIdentifier(name) {
|
|
|
|
var info = this.getBinding(name);
|
2023-12-18 13:12:25 +08:00
|
|
|
return info && info.identifier;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.getOwnBindingIdentifier = function getOwnBindingIdentifier(name) {
|
|
|
|
var binding = this.bindings[name];
|
2023-12-18 13:12:25 +08:00
|
|
|
return binding && binding.identifier;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.hasOwnBinding = function hasOwnBinding(name) {
|
2023-12-18 13:12:25 +08:00
|
|
|
return !!this.getOwnBinding(name);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.hasBinding = function hasBinding(name, noGlobals) {
|
2023-12-18 13:12:25 +08:00
|
|
|
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 && (0, _includes().default)(Scope.globals, name)) return true;
|
|
|
|
if (!noGlobals && (0, _includes().default)(Scope.contextVariables, name)) return true;
|
|
|
|
return false;
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.parentHasBinding = function parentHasBinding(name, noGlobals) {
|
2023-12-18 13:12:25 +08:00
|
|
|
return this.parent && this.parent.hasBinding(name, noGlobals);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.moveBindingTo = function moveBindingTo(name, scope) {
|
|
|
|
var info = this.getBinding(name);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (info) {
|
|
|
|
info.scope.removeOwnBinding(name);
|
|
|
|
info.scope = scope;
|
|
|
|
scope.bindings[name] = info;
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.removeOwnBinding = function removeOwnBinding(name) {
|
2023-12-18 13:12:25 +08:00
|
|
|
delete this.bindings[name];
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_proto.removeBinding = function removeBinding(name) {
|
|
|
|
var info = this.getBinding(name);
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
if (info) {
|
|
|
|
info.scope.removeOwnBinding(name);
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var scope = this;
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (scope.uids[name]) {
|
|
|
|
scope.uids[name] = false;
|
|
|
|
}
|
|
|
|
} while (scope = scope.parent);
|
2024-01-16 21:26:16 +08:00
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
_createClass(Scope, [{
|
|
|
|
key: "parent",
|
|
|
|
get: function get() {
|
|
|
|
var parent = this.path.findParent(function (p) {
|
|
|
|
return p.isScope();
|
|
|
|
});
|
|
|
|
return parent && parent.scope;
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
key: "parentBlock",
|
|
|
|
get: function get() {
|
|
|
|
return this.path.parent;
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
key: "hub",
|
|
|
|
get: function get() {
|
|
|
|
return this.path.hub;
|
|
|
|
}
|
|
|
|
}]);
|
|
|
|
|
|
|
|
return Scope;
|
|
|
|
}();
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
exports.default = Scope;
|
|
|
|
Scope.globals = Object.keys(_globals().default.builtin);
|
|
|
|
Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
|