123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- "use strict";
- exports.__esModule = true;
- exports.is = undefined;
- var _getIterator2 = require("babel-runtime/core-js/get-iterator");
- var _getIterator3 = _interopRequireDefault(_getIterator2);
- exports.matchesPattern = matchesPattern;
- exports.has = has;
- exports.isStatic = isStatic;
- exports.isnt = isnt;
- exports.equals = equals;
- exports.isNodeType = isNodeType;
- exports.canHaveVariableDeclarationOrExpression = canHaveVariableDeclarationOrExpression;
- exports.canSwapBetweenExpressionAndStatement = canSwapBetweenExpressionAndStatement;
- exports.isCompletionRecord = isCompletionRecord;
- exports.isStatementOrBlock = isStatementOrBlock;
- exports.referencesImport = referencesImport;
- exports.getSource = getSource;
- exports.willIMaybeExecuteBefore = willIMaybeExecuteBefore;
- exports._guessExecutionStatusRelativeTo = _guessExecutionStatusRelativeTo;
- exports._guessExecutionStatusRelativeToDifferentFunctions = _guessExecutionStatusRelativeToDifferentFunctions;
- exports.resolve = resolve;
- exports._resolve = _resolve;
- var _includes = require("lodash/includes");
- var _includes2 = _interopRequireDefault(_includes);
- var _babelTypes = require("babel-types");
- var t = _interopRequireWildcard(_babelTypes);
- 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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- function matchesPattern(pattern, allowPartial) {
- if (!this.isMemberExpression()) return false;
- var parts = pattern.split(".");
- var search = [this.node];
- var i = 0;
- function matches(name) {
- var part = parts[i];
- return part === "*" || name === part;
- }
- while (search.length) {
- var node = search.shift();
- if (allowPartial && i === parts.length) {
- return true;
- }
- if (t.isIdentifier(node)) {
- if (!matches(node.name)) return false;
- } else if (t.isLiteral(node)) {
- if (!matches(node.value)) return false;
- } else if (t.isMemberExpression(node)) {
- if (node.computed && !t.isLiteral(node.property)) {
- return false;
- } else {
- search.unshift(node.property);
- search.unshift(node.object);
- continue;
- }
- } else if (t.isThisExpression(node)) {
- if (!matches("this")) return false;
- } else {
- return false;
- }
- if (++i > parts.length) {
- return false;
- }
- }
- return i === parts.length;
- }
- function has(key) {
- var val = this.node && this.node[key];
- if (val && Array.isArray(val)) {
- return !!val.length;
- } else {
- return !!val;
- }
- }
- function isStatic() {
- return this.scope.isStatic(this.node);
- }
- var is = exports.is = has;
- function isnt(key) {
- return !this.has(key);
- }
- function equals(key, value) {
- return this.node[key] === value;
- }
- function isNodeType(type) {
- return t.isType(this.type, type);
- }
- function canHaveVariableDeclarationOrExpression() {
- return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
- }
- function canSwapBetweenExpressionAndStatement(replacement) {
- if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
- return false;
- }
- if (this.isExpression()) {
- return t.isBlockStatement(replacement);
- } else if (this.isBlockStatement()) {
- return t.isExpression(replacement);
- }
- return false;
- }
- function isCompletionRecord(allowInsideFunction) {
- var path = this;
- var first = true;
- do {
- var container = path.container;
- if (path.isFunction() && !first) {
- return !!allowInsideFunction;
- }
- first = false;
- if (Array.isArray(container) && path.key !== container.length - 1) {
- return false;
- }
- } while ((path = path.parentPath) && !path.isProgram());
- return true;
- }
- function isStatementOrBlock() {
- if (this.parentPath.isLabeledStatement() || t.isBlockStatement(this.container)) {
- return false;
- } else {
- return (0, _includes2.default)(t.STATEMENT_OR_BLOCK_KEYS, this.key);
- }
- }
- function referencesImport(moduleSource, importName) {
- if (!this.isReferencedIdentifier()) return false;
- var binding = this.scope.getBinding(this.node.name);
- if (!binding || binding.kind !== "module") return false;
- var path = binding.path;
- var parent = path.parentPath;
- if (!parent.isImportDeclaration()) return false;
- if (parent.node.source.value === moduleSource) {
- if (!importName) return true;
- } else {
- return false;
- }
- if (path.isImportDefaultSpecifier() && importName === "default") {
- return true;
- }
- if (path.isImportNamespaceSpecifier() && importName === "*") {
- return true;
- }
- if (path.isImportSpecifier() && path.node.imported.name === importName) {
- return true;
- }
- return false;
- }
- function getSource() {
- var node = this.node;
- if (node.end) {
- return this.hub.file.code.slice(node.start, node.end);
- } else {
- return "";
- }
- }
- function willIMaybeExecuteBefore(target) {
- return this._guessExecutionStatusRelativeTo(target) !== "after";
- }
- function _guessExecutionStatusRelativeTo(target) {
- var targetFuncParent = target.scope.getFunctionParent();
- var selfFuncParent = this.scope.getFunctionParent();
- if (targetFuncParent.node !== selfFuncParent.node) {
- var status = this._guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent);
- if (status) {
- return status;
- } else {
- target = targetFuncParent.path;
- }
- }
- var targetPaths = target.getAncestry();
- if (targetPaths.indexOf(this) >= 0) return "after";
- var selfPaths = this.getAncestry();
- var commonPath = void 0;
- var targetIndex = void 0;
- var selfIndex = void 0;
- for (selfIndex = 0; selfIndex < selfPaths.length; selfIndex++) {
- var selfPath = selfPaths[selfIndex];
- targetIndex = targetPaths.indexOf(selfPath);
- if (targetIndex >= 0) {
- commonPath = selfPath;
- break;
- }
- }
- if (!commonPath) {
- return "before";
- }
- var targetRelationship = targetPaths[targetIndex - 1];
- var selfRelationship = selfPaths[selfIndex - 1];
- if (!targetRelationship || !selfRelationship) {
- return "before";
- }
- if (targetRelationship.listKey && targetRelationship.container === selfRelationship.container) {
- return targetRelationship.key > selfRelationship.key ? "before" : "after";
- }
- var targetKeyPosition = t.VISITOR_KEYS[targetRelationship.type].indexOf(targetRelationship.key);
- var selfKeyPosition = t.VISITOR_KEYS[selfRelationship.type].indexOf(selfRelationship.key);
- return targetKeyPosition > selfKeyPosition ? "before" : "after";
- }
- function _guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent) {
- var targetFuncPath = targetFuncParent.path;
- if (!targetFuncPath.isFunctionDeclaration()) return;
- var binding = targetFuncPath.scope.getBinding(targetFuncPath.node.id.name);
- if (!binding.references) return "before";
- var referencePaths = binding.referencePaths;
- for (var _iterator = referencePaths, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
- var _ref;
- if (_isArray) {
- if (_i >= _iterator.length) break;
- _ref = _iterator[_i++];
- } else {
- _i = _iterator.next();
- if (_i.done) break;
- _ref = _i.value;
- }
- var path = _ref;
- if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
- return;
- }
- }
- var allStatus = void 0;
- for (var _iterator2 = referencePaths, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
- var _ref2;
- if (_isArray2) {
- if (_i2 >= _iterator2.length) break;
- _ref2 = _iterator2[_i2++];
- } else {
- _i2 = _iterator2.next();
- if (_i2.done) break;
- _ref2 = _i2.value;
- }
- var _path = _ref2;
- var childOfFunction = !!_path.find(function (path) {
- return path.node === targetFuncPath.node;
- });
- if (childOfFunction) continue;
- var status = this._guessExecutionStatusRelativeTo(_path);
- if (allStatus) {
- if (allStatus !== status) return;
- } else {
- allStatus = status;
- }
- }
- return allStatus;
- }
- function resolve(dangerous, resolved) {
- return this._resolve(dangerous, resolved) || this;
- }
- function _resolve(dangerous, resolved) {
- if (resolved && resolved.indexOf(this) >= 0) return;
- resolved = resolved || [];
- resolved.push(this);
- if (this.isVariableDeclarator()) {
- if (this.get("id").isIdentifier()) {
- return this.get("init").resolve(dangerous, resolved);
- } else {}
- } else if (this.isReferencedIdentifier()) {
- var binding = this.scope.getBinding(this.node.name);
- if (!binding) return;
- if (!binding.constant) return;
- if (binding.kind === "module") return;
- if (binding.path !== this) {
- var ret = binding.path.resolve(dangerous, resolved);
- if (this.find(function (parent) {
- return parent.node === ret.node;
- })) return;
- return ret;
- }
- } else if (this.isTypeCastExpression()) {
- return this.get("expression").resolve(dangerous, resolved);
- } else if (dangerous && this.isMemberExpression()) {
- var targetKey = this.toComputedKey();
- if (!t.isLiteral(targetKey)) return;
- var targetName = targetKey.value;
- var target = this.get("object").resolve(dangerous, resolved);
- if (target.isObjectExpression()) {
- var props = target.get("properties");
- for (var _iterator3 = props, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : (0, _getIterator3.default)(_iterator3);;) {
- var _ref3;
- if (_isArray3) {
- if (_i3 >= _iterator3.length) break;
- _ref3 = _iterator3[_i3++];
- } else {
- _i3 = _iterator3.next();
- if (_i3.done) break;
- _ref3 = _i3.value;
- }
- var prop = _ref3;
- if (!prop.isProperty()) continue;
- var key = prop.get("key");
- var match = prop.isnt("computed") && key.isIdentifier({ name: targetName });
- match = match || key.isLiteral({ value: targetName });
- if (match) return prop.get("value").resolve(dangerous, resolved);
- }
- } else if (target.isArrayExpression() && !isNaN(+targetName)) {
- var elems = target.get("elements");
- var elem = elems[targetName];
- if (elem) return elem.resolve(dangerous, resolved);
- }
- }
- }
|