lib/condenser/processors/node_modules/@babel/traverse/lib/path/introspection.js in condenser-0.0.8 vs lib/condenser/processors/node_modules/@babel/traverse/lib/path/introspection.js in condenser-0.0.9

- old
+ new

@@ -22,36 +22,22 @@ exports._resolve = _resolve; exports.isConstantExpression = isConstantExpression; exports.isInStrictMode = isInStrictMode; exports.is = void 0; -function _includes() { - const data = _interopRequireDefault(require("lodash/includes")); +var _includes = _interopRequireDefault(require("lodash/includes")); - _includes = function () { - return data; - }; +var t = _interopRequireWildcard(require("@babel/types")); - return data; -} +function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; } -function t() { - const data = _interopRequireWildcard(require("@babel/types")); +function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; 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; } - t = function () { - return data; - }; - - return data; -} - -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 }; } function matchesPattern(pattern, allowPartial) { - return t().matchesPattern(this.node, pattern, allowPartial); + return t.matchesPattern(this.node, pattern, allowPartial); } function has(key) { const val = this.node && this.node[key]; @@ -76,11 +62,11 @@ function equals(key, value) { return this.node[key] === value; } function isNodeType(type) { - return t().isType(this.type, type); + return t.isType(this.type, type); } function canHaveVariableDeclarationOrExpression() { return (this.key === "init" || this.key === "left") && this.parentPath.isFor(); } @@ -89,13 +75,13 @@ if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) { return false; } if (this.isExpression()) { - return t().isBlockStatement(replacement); + return t.isBlockStatement(replacement); } else if (this.isBlockStatement()) { - return t().isExpression(replacement); + return t.isExpression(replacement); } return false; } @@ -119,14 +105,14 @@ return true; } function isStatementOrBlock() { - if (this.parentPath.isLabeledStatement() || t().isBlockStatement(this.container)) { + if (this.parentPath.isLabeledStatement() || t.isBlockStatement(this.container)) { return false; } else { - return (0, _includes().default)(t().STATEMENT_OR_BLOCK_KEYS, this.key); + return (0, _includes.default)(t.STATEMENT_OR_BLOCK_KEYS, this.key); } } function referencesImport(moduleSource, importName) { if (!this.isReferencedIdentifier()) return false; @@ -170,85 +156,151 @@ function willIMaybeExecuteBefore(target) { return this._guessExecutionStatusRelativeTo(target) !== "after"; } -function _guessExecutionStatusRelativeTo(target) { - const targetFuncParent = target.scope.getFunctionParent() || target.scope.getProgramParent(); - const selfFuncParent = this.scope.getFunctionParent() || target.scope.getProgramParent(); +function getOuterFunction(path) { + return (path.scope.getFunctionParent() || path.scope.getProgramParent()).path; +} - if (targetFuncParent.node !== selfFuncParent.node) { - const status = this._guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent); +function isExecutionUncertain(type, key) { + switch (type) { + case "LogicalExpression": + return key === "right"; - if (status) { - return status; - } else { - target = targetFuncParent.path; + case "ConditionalExpression": + case "IfStatement": + return key === "consequent" || key === "alternate"; + + case "WhileStatement": + case "DoWhileStatement": + case "ForInStatement": + case "ForOfStatement": + return key === "body"; + + case "ForStatement": + return key === "body" || key === "update"; + + case "SwitchStatement": + return key === "cases"; + + case "TryStatement": + return key === "handler"; + + case "AssignmentPattern": + return key === "right"; + + case "OptionalMemberExpression": + return key === "property"; + + case "OptionalCallExpression": + return key === "arguments"; + + default: + return false; + } +} + +function isExecutionUncertainInList(paths, maxIndex) { + for (let i = 0; i < maxIndex; i++) { + const path = paths[i]; + + if (isExecutionUncertain(path.parent.type, path.parentKey)) { + return true; } } - const targetPaths = target.getAncestry(); - if (targetPaths.indexOf(this) >= 0) return "after"; - const selfPaths = this.getAncestry(); + return false; +} + +function _guessExecutionStatusRelativeTo(target) { + const funcParent = { + this: getOuterFunction(this), + target: getOuterFunction(target) + }; + + if (funcParent.target.node !== funcParent.this.node) { + return this._guessExecutionStatusRelativeToDifferentFunctions(funcParent.target); + } + + const paths = { + target: target.getAncestry(), + this: this.getAncestry() + }; + if (paths.target.indexOf(this) >= 0) return "after"; + if (paths.this.indexOf(target) >= 0) return "before"; let commonPath; - let targetIndex; - let selfIndex; + const commonIndex = { + target: 0, + this: 0 + }; - for (selfIndex = 0; selfIndex < selfPaths.length; selfIndex++) { - const selfPath = selfPaths[selfIndex]; - targetIndex = targetPaths.indexOf(selfPath); + while (!commonPath && commonIndex.this < paths.this.length) { + const path = paths.this[commonIndex.this]; + commonIndex.target = paths.target.indexOf(path); - if (targetIndex >= 0) { - commonPath = selfPath; - break; + if (commonIndex.target >= 0) { + commonPath = path; + } else { + commonIndex.this++; } } if (!commonPath) { - return "before"; + throw new Error("Internal Babel error - The two compared nodes" + " don't appear to belong to the same program."); } - const targetRelationship = targetPaths[targetIndex - 1]; - const selfRelationship = selfPaths[selfIndex - 1]; - - if (!targetRelationship || !selfRelationship) { - return "before"; + if (isExecutionUncertainInList(paths.this, commonIndex.this - 1) || isExecutionUncertainInList(paths.target, commonIndex.target - 1)) { + return "unknown"; } - if (targetRelationship.listKey && targetRelationship.container === selfRelationship.container) { - return targetRelationship.key > selfRelationship.key ? "before" : "after"; + const divergence = { + this: paths.this[commonIndex.this - 1], + target: paths.target[commonIndex.target - 1] + }; + + if (divergence.target.listKey && divergence.this.listKey && divergence.target.container === divergence.this.container) { + return divergence.target.key > divergence.this.key ? "before" : "after"; } - const keys = t().VISITOR_KEYS[commonPath.type]; - const targetKeyPosition = keys.indexOf(targetRelationship.key); - const selfKeyPosition = keys.indexOf(selfRelationship.key); - return targetKeyPosition > selfKeyPosition ? "before" : "after"; + const keys = t.VISITOR_KEYS[commonPath.type]; + const keyPosition = { + this: keys.indexOf(divergence.this.parentKey), + target: keys.indexOf(divergence.target.parentKey) + }; + return keyPosition.target > keyPosition.this ? "before" : "after"; } -function _guessExecutionStatusRelativeToDifferentFunctions(targetFuncParent) { - const targetFuncPath = targetFuncParent.path; - if (!targetFuncPath.isFunctionDeclaration()) return; - const binding = targetFuncPath.scope.getBinding(targetFuncPath.node.id.name); - if (!binding.references) return "before"; - const referencePaths = binding.referencePaths; +const executionOrderCheckedNodes = new WeakSet(); - for (const path of referencePaths) { - if (path.key !== "callee" || !path.parentPath.isCallExpression()) { - return; - } +function _guessExecutionStatusRelativeToDifferentFunctions(target) { + if (!target.isFunctionDeclaration() || target.parentPath.isExportDeclaration()) { + return "unknown"; } + const binding = target.scope.getBinding(target.node.id.name); + if (!binding.references) return "before"; + const referencePaths = binding.referencePaths; let allStatus; for (const path of referencePaths) { - const childOfFunction = !!path.find(path => path.node === targetFuncPath.node); + const childOfFunction = !!path.find(path => path.node === target.node); if (childOfFunction) continue; + if (path.key !== "callee" || !path.parentPath.isCallExpression()) { + return "unknown"; + } + + if (executionOrderCheckedNodes.has(path.node)) continue; + executionOrderCheckedNodes.add(path.node); + const status = this._guessExecutionStatusRelativeTo(path); - if (allStatus) { - if (allStatus !== status) return; + executionOrderCheckedNodes.delete(path.node); + + if (allStatus && allStatus !== status) { + return "unknown"; } else { allStatus = status; } } @@ -281,10 +333,10 @@ } } else if (this.isTypeCastExpression()) { return this.get("expression").resolve(dangerous, resolved); } else if (dangerous && this.isMemberExpression()) { const targetKey = this.toComputedKey(); - if (!t().isLiteral(targetKey)) return; + if (!t.isLiteral(targetKey)) return; const targetName = targetKey.value; const target = this.get("object").resolve(dangerous, resolved); if (target.isObjectExpression()) { const props = target.get("properties"); \ No newline at end of file