Commit 6b91c4a7 authored by Rosanny Sihombing's avatar Rosanny Sihombing
Browse files

delete and ignore node_modules

parent 22482a26
Showing with 0 additions and 3143 deletions
+0 -3143
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = traverse;
Object.defineProperty(exports, "NodePath", {
enumerable: true,
get: function () {
return _path.default;
}
});
Object.defineProperty(exports, "Scope", {
enumerable: true,
get: function () {
return _scope.default;
}
});
Object.defineProperty(exports, "Hub", {
enumerable: true,
get: function () {
return _hub.default;
}
});
exports.visitors = void 0;
var _context = _interopRequireDefault(require("./context"));
var visitors = _interopRequireWildcard(require("./visitors"));
exports.visitors = visitors;
var _includes = _interopRequireDefault(require("lodash/includes"));
var t = _interopRequireWildcard(require("@babel/types"));
var cache = _interopRequireWildcard(require("./cache"));
var _path = _interopRequireDefault(require("./path"));
var _scope = _interopRequireDefault(require("./scope"));
var _hub = _interopRequireDefault(require("./hub"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
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; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function traverse(parent, opts, scope, state, parentPath) {
if (!parent) return;
if (!opts) opts = {};
if (!opts.noScope && !scope) {
if (parent.type !== "Program" && parent.type !== "File") {
throw new Error("You must pass a scope and parentPath unless traversing a Program/File. " + `Instead of that you tried to traverse a ${parent.type} node without ` + "passing scope and parentPath.");
}
}
if (!t.VISITOR_KEYS[parent.type]) {
return;
}
visitors.explode(opts);
traverse.node(parent, opts, scope, state, parentPath);
}
traverse.visitors = visitors;
traverse.verify = visitors.verify;
traverse.explode = visitors.explode;
traverse.cheap = function (node, enter) {
return t.traverseFast(node, enter);
};
traverse.node = function (node, opts, scope, state, parentPath, skipKeys) {
const keys = t.VISITOR_KEYS[node.type];
if (!keys) return;
const context = new _context.default(scope, opts, state, parentPath);
for (const key of keys) {
if (skipKeys && skipKeys[key]) continue;
if (context.visit(node, key)) return;
}
};
traverse.clearNode = function (node, opts) {
t.removeProperties(node, opts);
cache.path.delete(node);
};
traverse.removeProperties = function (tree, opts) {
t.traverseFast(tree, traverse.clearNode, opts);
return tree;
};
function hasBlacklistedType(path, state) {
if (path.node.type === state.type) {
state.has = true;
path.stop();
}
}
traverse.hasType = function (tree, type, blacklistTypes) {
if ((0, _includes.default)(blacklistTypes, tree.type)) return false;
if (tree.type === type) return true;
const state = {
has: false,
type: type
};
traverse(tree, {
noScope: true,
blacklist: blacklistTypes,
enter: hasBlacklistedType
}, null, state);
return state.has;
};
traverse.cache = cache;
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.findParent = findParent;
exports.find = find;
exports.getFunctionParent = getFunctionParent;
exports.getStatementParent = getStatementParent;
exports.getEarliestCommonAncestorFrom = getEarliestCommonAncestorFrom;
exports.getDeepestCommonAncestorFrom = getDeepestCommonAncestorFrom;
exports.getAncestry = getAncestry;
exports.isAncestor = isAncestor;
exports.isDescendant = isDescendant;
exports.inType = inType;
var t = _interopRequireWildcard(require("@babel/types"));
var _index = _interopRequireDefault(require("./index"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
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; }
function findParent(callback) {
let path = this;
while (path = path.parentPath) {
if (callback(path)) return path;
}
return null;
}
function find(callback) {
let path = this;
do {
if (callback(path)) return path;
} while (path = path.parentPath);
return null;
}
function getFunctionParent() {
return this.findParent(p => p.isFunction());
}
function getStatementParent() {
let path = this;
do {
if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
break;
} else {
path = path.parentPath;
}
} while (path);
if (path && (path.isProgram() || path.isFile())) {
throw new Error("File/Program node, we can't possibly find a statement parent to this");
}
return path;
}
function getEarliestCommonAncestorFrom(paths) {
return this.getDeepestCommonAncestorFrom(paths, function (deepest, i, ancestries) {
let earliest;
const keys = t.VISITOR_KEYS[deepest.type];
for (const ancestry of ancestries) {
const path = ancestry[i + 1];
if (!earliest) {
earliest = path;
continue;
}
if (path.listKey && earliest.listKey === path.listKey) {
if (path.key < earliest.key) {
earliest = path;
continue;
}
}
const earliestKeyIndex = keys.indexOf(earliest.parentKey);
const currentKeyIndex = keys.indexOf(path.parentKey);
if (earliestKeyIndex > currentKeyIndex) {
earliest = path;
}
}
return earliest;
});
}
function getDeepestCommonAncestorFrom(paths, filter) {
if (!paths.length) {
return this;
}
if (paths.length === 1) {
return paths[0];
}
let minDepth = Infinity;
let lastCommonIndex, lastCommon;
const ancestries = paths.map(path => {
const ancestry = [];
do {
ancestry.unshift(path);
} while ((path = path.parentPath) && path !== this);
if (ancestry.length < minDepth) {
minDepth = ancestry.length;
}
return ancestry;
});
const first = ancestries[0];
depthLoop: for (let i = 0; i < minDepth; i++) {
const shouldMatch = first[i];
for (const ancestry of ancestries) {
if (ancestry[i] !== shouldMatch) {
break depthLoop;
}
}
lastCommonIndex = i;
lastCommon = shouldMatch;
}
if (lastCommon) {
if (filter) {
return filter(lastCommon, lastCommonIndex, ancestries);
} else {
return lastCommon;
}
} else {
throw new Error("Couldn't find intersection");
}
}
function getAncestry() {
let path = this;
const paths = [];
do {
paths.push(path);
} while (path = path.parentPath);
return paths;
}
function isAncestor(maybeDescendant) {
return maybeDescendant.isDescendant(this);
}
function isDescendant(maybeAncestor) {
return !!this.findParent(parent => parent === maybeAncestor);
}
function inType() {
let path = this;
while (path) {
for (const type of arguments) {
if (path.node.type === type) return true;
}
path = path.parentPath;
}
return false;
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.shareCommentsWithSiblings = shareCommentsWithSiblings;
exports.addComment = addComment;
exports.addComments = addComments;
var t = _interopRequireWildcard(require("@babel/types"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
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; }
function shareCommentsWithSiblings() {
if (typeof this.key === "string") return;
const node = this.node;
if (!node) return;
const trailing = node.trailingComments;
const leading = node.leadingComments;
if (!trailing && !leading) return;
const prev = this.getSibling(this.key - 1);
const next = this.getSibling(this.key + 1);
const hasPrev = Boolean(prev.node);
const hasNext = Boolean(next.node);
if (hasPrev && hasNext) {} else if (hasPrev) {
prev.addComments("trailing", trailing);
} else if (hasNext) {
next.addComments("leading", leading);
}
}
function addComment(type, content, line) {
t.addComment(this.node, type, content, line);
}
function addComments(type, comments) {
t.addComments(this.node, type, comments);
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.call = call;
exports._call = _call;
exports.isBlacklisted = isBlacklisted;
exports.visit = visit;
exports.skip = skip;
exports.skipKey = skipKey;
exports.stop = stop;
exports.setScope = setScope;
exports.setContext = setContext;
exports.resync = resync;
exports._resyncParent = _resyncParent;
exports._resyncKey = _resyncKey;
exports._resyncList = _resyncList;
exports._resyncRemoved = _resyncRemoved;
exports.popContext = popContext;
exports.pushContext = pushContext;
exports.setup = setup;
exports.setKey = setKey;
exports.requeue = requeue;
exports._getQueueContexts = _getQueueContexts;
var _index = _interopRequireDefault(require("../index"));
var _index2 = require("./index");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function call(key) {
const opts = this.opts;
this.debug(key);
if (this.node) {
if (this._call(opts[key])) return true;
}
if (this.node) {
return this._call(opts[this.node.type] && opts[this.node.type][key]);
}
return false;
}
function _call(fns) {
if (!fns) return false;
for (const fn of fns) {
if (!fn) continue;
const node = this.node;
if (!node) return true;
const ret = fn.call(this.state, this, this.state);
if (ret && typeof ret === "object" && typeof ret.then === "function") {
throw new Error(`You appear to be using a plugin with an async traversal visitor, ` + `which your current version of Babel does not support. ` + `If you're using a published plugin, you may need to upgrade ` + `your @babel/core version.`);
}
if (ret) {
throw new Error(`Unexpected return value from visitor method ${fn}`);
}
if (this.node !== node) return true;
if (this._traverseFlags > 0) return true;
}
return false;
}
function isBlacklisted() {
const blacklist = this.opts.blacklist;
return blacklist && blacklist.indexOf(this.node.type) > -1;
}
function visit() {
if (!this.node) {
return false;
}
if (this.isBlacklisted()) {
return false;
}
if (this.opts.shouldSkip && this.opts.shouldSkip(this)) {
return false;
}
if (this.shouldSkip || this.call("enter") || this.shouldSkip) {
this.debug("Skip...");
return this.shouldStop;
}
this.debug("Recursing into...");
_index.default.node(this.node, this.opts, this.scope, this.state, this, this.skipKeys);
this.call("exit");
return this.shouldStop;
}
function skip() {
this.shouldSkip = true;
}
function skipKey(key) {
if (this.skipKeys == null) {
this.skipKeys = {};
}
this.skipKeys[key] = true;
}
function stop() {
this._traverseFlags |= _index2.SHOULD_SKIP | _index2.SHOULD_STOP;
}
function setScope() {
if (this.opts && this.opts.noScope) return;
let path = this.parentPath;
let target;
while (path && !target) {
if (path.opts && path.opts.noScope) return;
target = path.scope;
path = path.parentPath;
}
this.scope = this.getScope(target);
if (this.scope) this.scope.init();
}
function setContext(context) {
if (this.skipKeys != null) {
this.skipKeys = {};
}
this._traverseFlags = 0;
if (context) {
this.context = context;
this.state = context.state;
this.opts = context.opts;
}
this.setScope();
return this;
}
function resync() {
if (this.removed) return;
this._resyncParent();
this._resyncList();
this._resyncKey();
}
function _resyncParent() {
if (this.parentPath) {
this.parent = this.parentPath.node;
}
}
function _resyncKey() {
if (!this.container) return;
if (this.node === this.container[this.key]) return;
if (Array.isArray(this.container)) {
for (let i = 0; i < this.container.length; i++) {
if (this.container[i] === this.node) {
return this.setKey(i);
}
}
} else {
for (const key of Object.keys(this.container)) {
if (this.container[key] === this.node) {
return this.setKey(key);
}
}
}
this.key = null;
}
function _resyncList() {
if (!this.parent || !this.inList) return;
const newContainer = this.parent[this.listKey];
if (this.container === newContainer) return;
this.container = newContainer || null;
}
function _resyncRemoved() {
if (this.key == null || !this.container || this.container[this.key] !== this.node) {
this._markRemoved();
}
}
function popContext() {
this.contexts.pop();
if (this.contexts.length > 0) {
this.setContext(this.contexts[this.contexts.length - 1]);
} else {
this.setContext(undefined);
}
}
function pushContext(context) {
this.contexts.push(context);
this.setContext(context);
}
function setup(parentPath, container, listKey, key) {
this.listKey = listKey;
this.container = container;
this.parentPath = parentPath || this.parentPath;
this.setKey(key);
}
function setKey(key) {
this.key = key;
this.node = this.container[this.key];
this.type = this.node && this.node.type;
}
function requeue(pathToQueue = this) {
if (pathToQueue.removed) return;
const contexts = this.contexts;
for (const context of contexts) {
context.maybeQueue(pathToQueue);
}
}
function _getQueueContexts() {
let path = this;
let contexts = this.contexts;
while (!contexts.length) {
path = path.parentPath;
if (!path) break;
contexts = path.contexts;
}
return contexts;
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toComputedKey = toComputedKey;
exports.ensureBlock = ensureBlock;
exports.arrowFunctionToShadowed = arrowFunctionToShadowed;
exports.unwrapFunctionEnvironment = unwrapFunctionEnvironment;
exports.arrowFunctionToExpression = arrowFunctionToExpression;
var t = _interopRequireWildcard(require("@babel/types"));
var _helperFunctionName = _interopRequireDefault(require("@babel/helper-function-name"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
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; }
function toComputedKey() {
const node = this.node;
let key;
if (this.isMemberExpression()) {
key = node.property;
} else if (this.isProperty() || this.isMethod()) {
key = node.key;
} else {
throw new ReferenceError("todo");
}
if (!node.computed) {
if (t.isIdentifier(key)) key = t.stringLiteral(key.name);
}
return key;
}
function ensureBlock() {
const body = this.get("body");
const bodyNode = body.node;
if (Array.isArray(body)) {
throw new Error("Can't convert array path to a block statement");
}
if (!bodyNode) {
throw new Error("Can't convert node without a body");
}
if (body.isBlockStatement()) {
return bodyNode;
}
const statements = [];
let stringPath = "body";
let key;
let listKey;
if (body.isStatement()) {
listKey = "body";
key = 0;
statements.push(body.node);
} else {
stringPath += ".body.0";
if (this.isFunction()) {
key = "argument";
statements.push(t.returnStatement(body.node));
} else {
key = "expression";
statements.push(t.expressionStatement(body.node));
}
}
this.node.body = t.blockStatement(statements);
const parentPath = this.get(stringPath);
body.setup(parentPath, listKey ? parentPath.node[listKey] : parentPath.node, listKey, key);
return this.node;
}
function arrowFunctionToShadowed() {
if (!this.isArrowFunctionExpression()) return;
this.arrowFunctionToExpression();
}
function unwrapFunctionEnvironment() {
if (!this.isArrowFunctionExpression() && !this.isFunctionExpression() && !this.isFunctionDeclaration()) {
throw this.buildCodeFrameError("Can only unwrap the environment of a function.");
}
hoistFunctionEnvironment(this);
}
function arrowFunctionToExpression({
allowInsertArrow = true,
specCompliant = false
} = {}) {
if (!this.isArrowFunctionExpression()) {
throw this.buildCodeFrameError("Cannot convert non-arrow function to a function expression.");
}
const thisBinding = hoistFunctionEnvironment(this, specCompliant, allowInsertArrow);
this.ensureBlock();
this.node.type = "FunctionExpression";
if (specCompliant) {
const checkBinding = thisBinding ? null : this.parentPath.scope.generateUidIdentifier("arrowCheckId");
if (checkBinding) {
this.parentPath.scope.push({
id: checkBinding,
init: t.objectExpression([])
});
}
this.get("body").unshiftContainer("body", t.expressionStatement(t.callExpression(this.hub.addHelper("newArrowCheck"), [t.thisExpression(), checkBinding ? t.identifier(checkBinding.name) : t.identifier(thisBinding)])));
this.replaceWith(t.callExpression(t.memberExpression((0, _helperFunctionName.default)(this, true) || this.node, t.identifier("bind")), [checkBinding ? t.identifier(checkBinding.name) : t.thisExpression()]));
}
}
function hoistFunctionEnvironment(fnPath, specCompliant = false, allowInsertArrow = true) {
const thisEnvFn = fnPath.findParent(p => {
return p.isFunction() && !p.isArrowFunctionExpression() || p.isProgram() || p.isClassProperty({
static: false
});
});
const inConstructor = thisEnvFn && thisEnvFn.node.kind === "constructor";
if (thisEnvFn.isClassProperty()) {
throw fnPath.buildCodeFrameError("Unable to transform arrow inside class property");
}
const {
thisPaths,
argumentsPaths,
newTargetPaths,
superProps,
superCalls
} = getScopeInformation(fnPath);
if (inConstructor && superCalls.length > 0) {
if (!allowInsertArrow) {
throw superCalls[0].buildCodeFrameError("Unable to handle nested super() usage in arrow");
}
const allSuperCalls = [];
thisEnvFn.traverse({
Function(child) {
if (child.isArrowFunctionExpression()) return;
child.skip();
},
ClassProperty(child) {
child.skip();
},
CallExpression(child) {
if (!child.get("callee").isSuper()) return;
allSuperCalls.push(child);
}
});
const superBinding = getSuperBinding(thisEnvFn);
allSuperCalls.forEach(superCall => {
const callee = t.identifier(superBinding);
callee.loc = superCall.node.callee.loc;
superCall.get("callee").replaceWith(callee);
});
}
if (argumentsPaths.length > 0) {
const argumentsBinding = getBinding(thisEnvFn, "arguments", () => t.identifier("arguments"));
argumentsPaths.forEach(argumentsChild => {
const argsRef = t.identifier(argumentsBinding);
argsRef.loc = argumentsChild.node.loc;
argumentsChild.replaceWith(argsRef);
});
}
if (newTargetPaths.length > 0) {
const newTargetBinding = getBinding(thisEnvFn, "newtarget", () => t.metaProperty(t.identifier("new"), t.identifier("target")));
newTargetPaths.forEach(targetChild => {
const targetRef = t.identifier(newTargetBinding);
targetRef.loc = targetChild.node.loc;
targetChild.replaceWith(targetRef);
});
}
if (superProps.length > 0) {
if (!allowInsertArrow) {
throw superProps[0].buildCodeFrameError("Unable to handle nested super.prop usage");
}
const flatSuperProps = superProps.reduce((acc, superProp) => acc.concat(standardizeSuperProperty(superProp)), []);
flatSuperProps.forEach(superProp => {
const key = superProp.node.computed ? "" : superProp.get("property").node.name;
const isAssignment = superProp.parentPath.isAssignmentExpression({
left: superProp.node
});
const isCall = superProp.parentPath.isCallExpression({
callee: superProp.node
});
const superBinding = getSuperPropBinding(thisEnvFn, isAssignment, key);
const args = [];
if (superProp.node.computed) {
args.push(superProp.get("property").node);
}
if (isAssignment) {
const value = superProp.parentPath.node.right;
args.push(value);
}
const call = t.callExpression(t.identifier(superBinding), args);
if (isCall) {
superProp.parentPath.unshiftContainer("arguments", t.thisExpression());
superProp.replaceWith(t.memberExpression(call, t.identifier("call")));
thisPaths.push(superProp.parentPath.get("arguments.0"));
} else if (isAssignment) {
superProp.parentPath.replaceWith(call);
} else {
superProp.replaceWith(call);
}
});
}
let thisBinding;
if (thisPaths.length > 0 || specCompliant) {
thisBinding = getThisBinding(thisEnvFn, inConstructor);
if (!specCompliant || inConstructor && hasSuperClass(thisEnvFn)) {
thisPaths.forEach(thisChild => {
const thisRef = thisChild.isJSX() ? t.jsxIdentifier(thisBinding) : t.identifier(thisBinding);
thisRef.loc = thisChild.node.loc;
thisChild.replaceWith(thisRef);
});
if (specCompliant) thisBinding = null;
}
}
return thisBinding;
}
function standardizeSuperProperty(superProp) {
if (superProp.parentPath.isAssignmentExpression() && superProp.parentPath.node.operator !== "=") {
const assignmentPath = superProp.parentPath;
const op = assignmentPath.node.operator.slice(0, -1);
const value = assignmentPath.node.right;
assignmentPath.node.operator = "=";
if (superProp.node.computed) {
const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
assignmentPath.get("left").replaceWith(t.memberExpression(superProp.node.object, t.assignmentExpression("=", tmp, superProp.node.property), true));
assignmentPath.get("right").replaceWith(t.binaryExpression(op, t.memberExpression(superProp.node.object, t.identifier(tmp.name), true), value));
} else {
assignmentPath.get("left").replaceWith(t.memberExpression(superProp.node.object, superProp.node.property));
assignmentPath.get("right").replaceWith(t.binaryExpression(op, t.memberExpression(superProp.node.object, t.identifier(superProp.node.property.name)), value));
}
return [assignmentPath.get("left"), assignmentPath.get("right").get("left")];
} else if (superProp.parentPath.isUpdateExpression()) {
const updateExpr = superProp.parentPath;
const tmp = superProp.scope.generateDeclaredUidIdentifier("tmp");
const computedKey = superProp.node.computed ? superProp.scope.generateDeclaredUidIdentifier("prop") : null;
const parts = [t.assignmentExpression("=", tmp, t.memberExpression(superProp.node.object, computedKey ? t.assignmentExpression("=", computedKey, superProp.node.property) : superProp.node.property, superProp.node.computed)), t.assignmentExpression("=", t.memberExpression(superProp.node.object, computedKey ? t.identifier(computedKey.name) : superProp.node.property, superProp.node.computed), t.binaryExpression("+", t.identifier(tmp.name), t.numericLiteral(1)))];
if (!superProp.parentPath.node.prefix) {
parts.push(t.identifier(tmp.name));
}
updateExpr.replaceWith(t.sequenceExpression(parts));
const left = updateExpr.get("expressions.0.right");
const right = updateExpr.get("expressions.1.left");
return [left, right];
}
return [superProp];
}
function hasSuperClass(thisEnvFn) {
return thisEnvFn.isClassMethod() && !!thisEnvFn.parentPath.parentPath.node.superClass;
}
function getThisBinding(thisEnvFn, inConstructor) {
return getBinding(thisEnvFn, "this", thisBinding => {
if (!inConstructor || !hasSuperClass(thisEnvFn)) return t.thisExpression();
const supers = new WeakSet();
thisEnvFn.traverse({
Function(child) {
if (child.isArrowFunctionExpression()) return;
child.skip();
},
ClassProperty(child) {
child.skip();
},
CallExpression(child) {
if (!child.get("callee").isSuper()) return;
if (supers.has(child.node)) return;
supers.add(child.node);
child.replaceWithMultiple([child.node, t.assignmentExpression("=", t.identifier(thisBinding), t.identifier("this"))]);
}
});
});
}
function getSuperBinding(thisEnvFn) {
return getBinding(thisEnvFn, "supercall", () => {
const argsBinding = thisEnvFn.scope.generateUidIdentifier("args");
return t.arrowFunctionExpression([t.restElement(argsBinding)], t.callExpression(t.super(), [t.spreadElement(t.identifier(argsBinding.name))]));
});
}
function getSuperPropBinding(thisEnvFn, isAssignment, propName) {
const op = isAssignment ? "set" : "get";
return getBinding(thisEnvFn, `superprop_${op}:${propName || ""}`, () => {
const argsList = [];
let fnBody;
if (propName) {
fnBody = t.memberExpression(t.super(), t.identifier(propName));
} else {
const method = thisEnvFn.scope.generateUidIdentifier("prop");
argsList.unshift(method);
fnBody = t.memberExpression(t.super(), t.identifier(method.name), true);
}
if (isAssignment) {
const valueIdent = thisEnvFn.scope.generateUidIdentifier("value");
argsList.push(valueIdent);
fnBody = t.assignmentExpression("=", fnBody, t.identifier(valueIdent.name));
}
return t.arrowFunctionExpression(argsList, fnBody);
});
}
function getBinding(thisEnvFn, key, init) {
const cacheKey = "binding:" + key;
let data = thisEnvFn.getData(cacheKey);
if (!data) {
const id = thisEnvFn.scope.generateUidIdentifier(key);
data = id.name;
thisEnvFn.setData(cacheKey, data);
thisEnvFn.scope.push({
id: id,
init: init(data)
});
}
return data;
}
function getScopeInformation(fnPath) {
const thisPaths = [];
const argumentsPaths = [];
const newTargetPaths = [];
const superProps = [];
const superCalls = [];
fnPath.traverse({
ClassProperty(child) {
child.skip();
},
Function(child) {
if (child.isArrowFunctionExpression()) return;
child.skip();
},
ThisExpression(child) {
thisPaths.push(child);
},
JSXIdentifier(child) {
if (child.node.name !== "this") return;
if (!child.parentPath.isJSXMemberExpression({
object: child.node
}) && !child.parentPath.isJSXOpeningElement({
name: child.node
})) {
return;
}
thisPaths.push(child);
},
CallExpression(child) {
if (child.get("callee").isSuper()) superCalls.push(child);
},
MemberExpression(child) {
if (child.get("object").isSuper()) superProps.push(child);
},
ReferencedIdentifier(child) {
if (child.node.name !== "arguments") return;
argumentsPaths.push(child);
},
MetaProperty(child) {
if (!child.get("meta").isIdentifier({
name: "new"
})) return;
if (!child.get("property").isIdentifier({
name: "target"
})) return;
newTargetPaths.push(child);
}
});
return {
thisPaths,
argumentsPaths,
newTargetPaths,
superProps,
superCalls
};
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.evaluateTruthy = evaluateTruthy;
exports.evaluate = evaluate;
const VALID_CALLEES = ["String", "Number", "Math"];
const INVALID_METHODS = ["random"];
function evaluateTruthy() {
const res = this.evaluate();
if (res.confident) return !!res.value;
}
function deopt(path, state) {
if (!state.confident) return;
state.deoptPath = path;
state.confident = false;
}
function evaluateCached(path, state) {
const {
node
} = path;
const {
seen
} = state;
if (seen.has(node)) {
const existing = seen.get(node);
if (existing.resolved) {
return existing.value;
} else {
deopt(path, state);
return;
}
} else {
const item = {
resolved: false
};
seen.set(node, item);
const val = _evaluate(path, state);
if (state.confident) {
item.resolved = true;
item.value = val;
}
return val;
}
}
function _evaluate(path, state) {
if (!state.confident) return;
const {
node
} = path;
if (path.isSequenceExpression()) {
const exprs = path.get("expressions");
return evaluateCached(exprs[exprs.length - 1], state);
}
if (path.isStringLiteral() || path.isNumericLiteral() || path.isBooleanLiteral()) {
return node.value;
}
if (path.isNullLiteral()) {
return null;
}
if (path.isTemplateLiteral()) {
return evaluateQuasis(path, node.quasis, state);
}
if (path.isTaggedTemplateExpression() && path.get("tag").isMemberExpression()) {
const object = path.get("tag.object");
const {
node: {
name
}
} = object;
const property = path.get("tag.property");
if (object.isIdentifier() && name === "String" && !path.scope.getBinding(name, true) && property.isIdentifier && property.node.name === "raw") {
return evaluateQuasis(path, node.quasi.quasis, state, true);
}
}
if (path.isConditionalExpression()) {
const testResult = evaluateCached(path.get("test"), state);
if (!state.confident) return;
if (testResult) {
return evaluateCached(path.get("consequent"), state);
} else {
return evaluateCached(path.get("alternate"), state);
}
}
if (path.isExpressionWrapper()) {
return evaluateCached(path.get("expression"), state);
}
if (path.isMemberExpression() && !path.parentPath.isCallExpression({
callee: node
})) {
const property = path.get("property");
const object = path.get("object");
if (object.isLiteral() && property.isIdentifier()) {
const value = object.node.value;
const type = typeof value;
if (type === "number" || type === "string") {
return value[property.node.name];
}
}
}
if (path.isReferencedIdentifier()) {
const binding = path.scope.getBinding(node.name);
if (binding && binding.constantViolations.length > 0) {
return deopt(binding.path, state);
}
if (binding && path.node.start < binding.path.node.end) {
return deopt(binding.path, state);
}
if (binding && binding.hasValue) {
return binding.value;
} else {
if (node.name === "undefined") {
return binding ? deopt(binding.path, state) : undefined;
} else if (node.name === "Infinity") {
return binding ? deopt(binding.path, state) : Infinity;
} else if (node.name === "NaN") {
return binding ? deopt(binding.path, state) : NaN;
}
const resolved = path.resolve();
if (resolved === path) {
return deopt(path, state);
} else {
return evaluateCached(resolved, state);
}
}
}
if (path.isUnaryExpression({
prefix: true
})) {
if (node.operator === "void") {
return undefined;
}
const argument = path.get("argument");
if (node.operator === "typeof" && (argument.isFunction() || argument.isClass())) {
return "function";
}
const arg = evaluateCached(argument, state);
if (!state.confident) return;
switch (node.operator) {
case "!":
return !arg;
case "+":
return +arg;
case "-":
return -arg;
case "~":
return ~arg;
case "typeof":
return typeof arg;
}
}
if (path.isArrayExpression()) {
const arr = [];
const elems = path.get("elements");
for (const elem of elems) {
const elemValue = elem.evaluate();
if (elemValue.confident) {
arr.push(elemValue.value);
} else {
return deopt(elem, state);
}
}
return arr;
}
if (path.isObjectExpression()) {
const obj = {};
const props = path.get("properties");
for (const prop of props) {
if (prop.isObjectMethod() || prop.isSpreadElement()) {
return deopt(prop, state);
}
const keyPath = prop.get("key");
let key = keyPath;
if (prop.node.computed) {
key = key.evaluate();
if (!key.confident) {
return deopt(keyPath, state);
}
key = key.value;
} else if (key.isIdentifier()) {
key = key.node.name;
} else {
key = key.node.value;
}
const valuePath = prop.get("value");
let value = valuePath.evaluate();
if (!value.confident) {
return deopt(valuePath, state);
}
value = value.value;
obj[key] = value;
}
return obj;
}
if (path.isLogicalExpression()) {
const wasConfident = state.confident;
const left = evaluateCached(path.get("left"), state);
const leftConfident = state.confident;
state.confident = wasConfident;
const right = evaluateCached(path.get("right"), state);
const rightConfident = state.confident;
switch (node.operator) {
case "||":
state.confident = leftConfident && (!!left || rightConfident);
if (!state.confident) return;
return left || right;
case "&&":
state.confident = leftConfident && (!left || rightConfident);
if (!state.confident) return;
return left && right;
}
}
if (path.isBinaryExpression()) {
const left = evaluateCached(path.get("left"), state);
if (!state.confident) return;
const right = evaluateCached(path.get("right"), state);
if (!state.confident) return;
switch (node.operator) {
case "-":
return left - right;
case "+":
return left + right;
case "/":
return left / right;
case "*":
return left * right;
case "%":
return left % right;
case "**":
return Math.pow(left, right);
case "<":
return left < right;
case ">":
return left > right;
case "<=":
return left <= right;
case ">=":
return left >= right;
case "==":
return left == right;
case "!=":
return left != right;
case "===":
return left === right;
case "!==":
return left !== right;
case "|":
return left | right;
case "&":
return left & right;
case "^":
return left ^ right;
case "<<":
return left << right;
case ">>":
return left >> right;
case ">>>":
return left >>> right;
}
}
if (path.isCallExpression()) {
const callee = path.get("callee");
let context;
let func;
if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name, true) && VALID_CALLEES.indexOf(callee.node.name) >= 0) {
func = global[node.callee.name];
}
if (callee.isMemberExpression()) {
const object = callee.get("object");
const property = callee.get("property");
if (object.isIdentifier() && property.isIdentifier() && VALID_CALLEES.indexOf(object.node.name) >= 0 && INVALID_METHODS.indexOf(property.node.name) < 0) {
context = global[object.node.name];
func = context[property.node.name];
}
if (object.isLiteral() && property.isIdentifier()) {
const type = typeof object.node.value;
if (type === "string" || type === "number") {
context = object.node.value;
func = context[property.node.name];
}
}
}
if (func) {
const args = path.get("arguments").map(arg => evaluateCached(arg, state));
if (!state.confident) return;
return func.apply(context, args);
}
}
deopt(path, state);
}
function evaluateQuasis(path, quasis, state, raw = false) {
let str = "";
let i = 0;
const exprs = path.get("expressions");
for (const elem of quasis) {
if (!state.confident) break;
str += raw ? elem.value.raw : elem.value.cooked;
const expr = exprs[i++];
if (expr) str += String(evaluateCached(expr, state));
}
if (!state.confident) return;
return str;
}
function evaluate() {
const state = {
confident: true,
deoptPath: null,
seen: new Map()
};
let value = evaluateCached(this, state);
if (!state.confident) value = undefined;
return {
confident: state.confident,
deopt: state.deoptPath,
value: value
};
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getOpposite = getOpposite;
exports.getCompletionRecords = getCompletionRecords;
exports.getSibling = getSibling;
exports.getPrevSibling = getPrevSibling;
exports.getNextSibling = getNextSibling;
exports.getAllNextSiblings = getAllNextSiblings;
exports.getAllPrevSiblings = getAllPrevSiblings;
exports.get = get;
exports._getKey = _getKey;
exports._getPattern = _getPattern;
exports.getBindingIdentifiers = getBindingIdentifiers;
exports.getOuterBindingIdentifiers = getOuterBindingIdentifiers;
exports.getBindingIdentifierPaths = getBindingIdentifierPaths;
exports.getOuterBindingIdentifierPaths = getOuterBindingIdentifierPaths;
var _index = _interopRequireDefault(require("./index"));
var t = _interopRequireWildcard(require("@babel/types"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
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; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function getOpposite() {
if (this.key === "left") {
return this.getSibling("right");
} else if (this.key === "right") {
return this.getSibling("left");
}
}
function addCompletionRecords(path, paths) {
if (path) return paths.concat(path.getCompletionRecords());
return paths;
}
function completionRecordForSwitch(cases, paths) {
let isLastCaseWithConsequent = true;
for (let i = cases.length - 1; i >= 0; i--) {
const switchCase = cases[i];
const consequent = switchCase.get("consequent");
let breakStatement;
findBreak: for (const statement of consequent) {
if (statement.isBlockStatement()) {
for (const statementInBlock of statement.get("body")) {
if (statementInBlock.isBreakStatement()) {
breakStatement = statementInBlock;
break findBreak;
}
}
} else if (statement.isBreakStatement()) {
breakStatement = statement;
break;
}
}
if (breakStatement) {
while (breakStatement.key === 0 && breakStatement.parentPath.isBlockStatement()) {
breakStatement = breakStatement.parentPath;
}
const prevSibling = breakStatement.getPrevSibling();
if (breakStatement.key > 0 && (prevSibling.isExpressionStatement() || prevSibling.isBlockStatement())) {
paths = addCompletionRecords(prevSibling, paths);
breakStatement.remove();
} else {
breakStatement.replaceWith(breakStatement.scope.buildUndefinedNode());
paths = addCompletionRecords(breakStatement, paths);
}
} else if (isLastCaseWithConsequent) {
const statementFinder = statement => !statement.isBlockStatement() || statement.get("body").some(statementFinder);
const hasConsequent = consequent.some(statementFinder);
if (hasConsequent) {
paths = addCompletionRecords(consequent[consequent.length - 1], paths);
isLastCaseWithConsequent = false;
}
}
}
return paths;
}
function getCompletionRecords() {
let paths = [];
if (this.isIfStatement()) {
paths = addCompletionRecords(this.get("consequent"), paths);
paths = addCompletionRecords(this.get("alternate"), paths);
} else if (this.isDoExpression() || this.isFor() || this.isWhile()) {
paths = addCompletionRecords(this.get("body"), paths);
} else if (this.isProgram() || this.isBlockStatement()) {
paths = addCompletionRecords(this.get("body").pop(), paths);
} else if (this.isFunction()) {
return this.get("body").getCompletionRecords();
} else if (this.isTryStatement()) {
paths = addCompletionRecords(this.get("block"), paths);
paths = addCompletionRecords(this.get("handler"), paths);
} else if (this.isCatchClause()) {
paths = addCompletionRecords(this.get("body"), paths);
} else if (this.isSwitchStatement()) {
paths = completionRecordForSwitch(this.get("cases"), paths);
} else {
paths.push(this);
}
return paths;
}
function getSibling(key) {
return _index.default.get({
parentPath: this.parentPath,
parent: this.parent,
container: this.container,
listKey: this.listKey,
key: key
});
}
function getPrevSibling() {
return this.getSibling(this.key - 1);
}
function getNextSibling() {
return this.getSibling(this.key + 1);
}
function getAllNextSiblings() {
let _key = this.key;
let sibling = this.getSibling(++_key);
const siblings = [];
while (sibling.node) {
siblings.push(sibling);
sibling = this.getSibling(++_key);
}
return siblings;
}
function getAllPrevSiblings() {
let _key = this.key;
let sibling = this.getSibling(--_key);
const siblings = [];
while (sibling.node) {
siblings.push(sibling);
sibling = this.getSibling(--_key);
}
return siblings;
}
function get(key, context) {
if (context === true) context = this.context;
const parts = key.split(".");
if (parts.length === 1) {
return this._getKey(key, context);
} else {
return this._getPattern(parts, context);
}
}
function _getKey(key, context) {
const node = this.node;
const container = node[key];
if (Array.isArray(container)) {
return container.map((_, i) => {
return _index.default.get({
listKey: key,
parentPath: this,
parent: node,
container: container,
key: i
}).setContext(context);
});
} else {
return _index.default.get({
parentPath: this,
parent: node,
container: node,
key: key
}).setContext(context);
}
}
function _getPattern(parts, context) {
let path = this;
for (const part of parts) {
if (part === ".") {
path = path.parentPath;
} else {
if (Array.isArray(path)) {
path = path[part];
} else {
path = path.get(part, context);
}
}
}
return path;
}
function getBindingIdentifiers(duplicates) {
return t.getBindingIdentifiers(this.node, duplicates);
}
function getOuterBindingIdentifiers(duplicates) {
return t.getOuterBindingIdentifiers(this.node, duplicates);
}
function getBindingIdentifierPaths(duplicates = false, outerOnly = false) {
const path = this;
let search = [].concat(path);
const ids = Object.create(null);
while (search.length) {
const id = search.shift();
if (!id) continue;
if (!id.node) continue;
const keys = t.getBindingIdentifiers.keys[id.node.type];
if (id.isIdentifier()) {
if (duplicates) {
const _ids = ids[id.node.name] = ids[id.node.name] || [];
_ids.push(id);
} else {
ids[id.node.name] = id;
}
continue;
}
if (id.isExportDeclaration()) {
const declaration = id.get("declaration");
if (declaration.isDeclaration()) {
search.push(declaration);
}
continue;
}
if (outerOnly) {
if (id.isFunctionDeclaration()) {
search.push(id.get("id"));
continue;
}
if (id.isFunctionExpression()) {
continue;
}
}
if (keys) {
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const child = id.get(key);
if (Array.isArray(child) || child.node) {
search = search.concat(child);
}
}
}
}
return ids;
}
function getOuterBindingIdentifierPaths(duplicates) {
return this.getBindingIdentifierPaths(duplicates, true);
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.SHOULD_SKIP = exports.SHOULD_STOP = exports.REMOVED = void 0;
var virtualTypes = _interopRequireWildcard(require("./lib/virtual-types"));
var _debug = _interopRequireDefault(require("debug"));
var _index = _interopRequireDefault(require("../index"));
var _scope = _interopRequireDefault(require("../scope"));
var t = _interopRequireWildcard(require("@babel/types"));
var _cache = require("../cache");
var _generator = _interopRequireDefault(require("@babel/generator"));
var NodePath_ancestry = _interopRequireWildcard(require("./ancestry"));
var NodePath_inference = _interopRequireWildcard(require("./inference"));
var NodePath_replacement = _interopRequireWildcard(require("./replacement"));
var NodePath_evaluation = _interopRequireWildcard(require("./evaluation"));
var NodePath_conversion = _interopRequireWildcard(require("./conversion"));
var NodePath_introspection = _interopRequireWildcard(require("./introspection"));
var NodePath_context = _interopRequireWildcard(require("./context"));
var NodePath_removal = _interopRequireWildcard(require("./removal"));
var NodePath_modification = _interopRequireWildcard(require("./modification"));
var NodePath_family = _interopRequireWildcard(require("./family"));
var NodePath_comments = _interopRequireWildcard(require("./comments"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
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; }
const debug = (0, _debug.default)("babel");
const REMOVED = 1 << 0;
exports.REMOVED = REMOVED;
const SHOULD_STOP = 1 << 1;
exports.SHOULD_STOP = SHOULD_STOP;
const SHOULD_SKIP = 1 << 2;
exports.SHOULD_SKIP = SHOULD_SKIP;
class NodePath {
constructor(hub, parent) {
this.parent = parent;
this.hub = hub;
this.contexts = [];
this.data = null;
this._traverseFlags = 0;
this.state = null;
this.opts = null;
this.skipKeys = null;
this.parentPath = null;
this.context = null;
this.container = null;
this.listKey = null;
this.key = null;
this.node = null;
this.scope = null;
this.type = null;
}
static get({
hub,
parentPath,
parent,
container,
listKey,
key
}) {
if (!hub && parentPath) {
hub = parentPath.hub;
}
if (!parent) {
throw new Error("To get a node path the parent needs to exist");
}
const targetNode = container[key];
const paths = _cache.path.get(parent) || [];
if (!_cache.path.has(parent)) {
_cache.path.set(parent, paths);
}
let path;
for (let i = 0; i < paths.length; i++) {
const pathCheck = paths[i];
if (pathCheck.node === targetNode) {
path = pathCheck;
break;
}
}
if (!path) {
path = new NodePath(hub, parent);
paths.push(path);
}
path.setup(parentPath, container, listKey, key);
return path;
}
getScope(scope) {
return this.isScope() ? new _scope.default(this) : scope;
}
setData(key, val) {
if (this.data == null) {
this.data = Object.create(null);
}
return this.data[key] = val;
}
getData(key, def) {
if (this.data == null) {
this.data = Object.create(null);
}
let val = this.data[key];
if (val === undefined && def !== undefined) val = this.data[key] = def;
return val;
}
buildCodeFrameError(msg, Error = SyntaxError) {
return this.hub.buildError(this.node, msg, Error);
}
traverse(visitor, state) {
(0, _index.default)(this.node, visitor, this.scope, state, this);
}
set(key, node) {
t.validate(this.node, key, node);
this.node[key] = node;
}
getPathLocation() {
const parts = [];
let path = this;
do {
let key = path.key;
if (path.inList) key = `${path.listKey}[${key}]`;
parts.unshift(key);
} while (path = path.parentPath);
return parts.join(".");
}
debug(message) {
if (!debug.enabled) return;
debug(`${this.getPathLocation()} ${this.type}: ${message}`);
}
toString() {
return (0, _generator.default)(this.node).code;
}
get inList() {
return !!this.listKey;
}
set inList(inList) {
if (!inList) {
this.listKey = null;
}
}
get parentKey() {
return this.listKey || this.key;
}
get shouldSkip() {
return !!(this._traverseFlags & SHOULD_SKIP);
}
set shouldSkip(v) {
if (v) {
this._traverseFlags |= SHOULD_SKIP;
} else {
this._traverseFlags &= ~SHOULD_SKIP;
}
}
get shouldStop() {
return !!(this._traverseFlags & SHOULD_STOP);
}
set shouldStop(v) {
if (v) {
this._traverseFlags |= SHOULD_STOP;
} else {
this._traverseFlags &= ~SHOULD_STOP;
}
}
get removed() {
return !!(this._traverseFlags & REMOVED);
}
set removed(v) {
if (v) {
this._traverseFlags |= REMOVED;
} else {
this._traverseFlags &= ~REMOVED;
}
}
}
exports.default = NodePath;
Object.assign(NodePath.prototype, NodePath_ancestry, NodePath_inference, NodePath_replacement, NodePath_evaluation, NodePath_conversion, NodePath_introspection, NodePath_context, NodePath_removal, NodePath_modification, NodePath_family, NodePath_comments);
for (const type of t.TYPES) {
const typeKey = `is${type}`;
const fn = t[typeKey];
NodePath.prototype[typeKey] = function (opts) {
return fn(this.node, opts);
};
NodePath.prototype[`assert${type}`] = function (opts) {
if (!fn(this.node, opts)) {
throw new TypeError(`Expected node path of type ${type}`);
}
};
}
for (const type of Object.keys(virtualTypes)) {
if (type[0] === "_") continue;
if (t.TYPES.indexOf(type) < 0) t.TYPES.push(type);
const virtualType = virtualTypes[type];
NodePath.prototype[`is${type}`] = function (opts) {
return virtualType.checkPath(this, opts);
};
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getTypeAnnotation = getTypeAnnotation;
exports._getTypeAnnotation = _getTypeAnnotation;
exports.isBaseType = isBaseType;
exports.couldBeBaseType = couldBeBaseType;
exports.baseTypeStrictlyMatches = baseTypeStrictlyMatches;
exports.isGenericType = isGenericType;
var inferers = _interopRequireWildcard(require("./inferers"));
var t = _interopRequireWildcard(require("@babel/types"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
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; }
function getTypeAnnotation() {
if (this.typeAnnotation) return this.typeAnnotation;
let type = this._getTypeAnnotation() || t.anyTypeAnnotation();
if (t.isTypeAnnotation(type)) type = type.typeAnnotation;
return this.typeAnnotation = type;
}
function _getTypeAnnotation() {
const node = this.node;
if (!node) {
if (this.key === "init" && this.parentPath.isVariableDeclarator()) {
const declar = this.parentPath.parentPath;
const declarParent = declar.parentPath;
if (declar.key === "left" && declarParent.isForInStatement()) {
return t.stringTypeAnnotation();
}
if (declar.key === "left" && declarParent.isForOfStatement()) {
return t.anyTypeAnnotation();
}
return t.voidTypeAnnotation();
} else {
return;
}
}
if (node.typeAnnotation) {
return node.typeAnnotation;
}
let inferer = inferers[node.type];
if (inferer) {
return inferer.call(this, node);
}
inferer = inferers[this.parentPath.type];
if (inferer && inferer.validParent) {
return this.parentPath.getTypeAnnotation();
}
}
function isBaseType(baseName, soft) {
return _isBaseType(baseName, this.getTypeAnnotation(), soft);
}
function _isBaseType(baseName, type, soft) {
if (baseName === "string") {
return t.isStringTypeAnnotation(type);
} else if (baseName === "number") {
return t.isNumberTypeAnnotation(type);
} else if (baseName === "boolean") {
return t.isBooleanTypeAnnotation(type);
} else if (baseName === "any") {
return t.isAnyTypeAnnotation(type);
} else if (baseName === "mixed") {
return t.isMixedTypeAnnotation(type);
} else if (baseName === "empty") {
return t.isEmptyTypeAnnotation(type);
} else if (baseName === "void") {
return t.isVoidTypeAnnotation(type);
} else {
if (soft) {
return false;
} else {
throw new Error(`Unknown base type ${baseName}`);
}
}
}
function couldBeBaseType(name) {
const type = this.getTypeAnnotation();
if (t.isAnyTypeAnnotation(type)) return true;
if (t.isUnionTypeAnnotation(type)) {
for (const type2 of type.types) {
if (t.isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {
return true;
}
}
return false;
} else {
return _isBaseType(name, type, true);
}
}
function baseTypeStrictlyMatches(right) {
const left = this.getTypeAnnotation();
right = right.getTypeAnnotation();
if (!t.isAnyTypeAnnotation(left) && t.isFlowBaseAnnotation(left)) {
return right.type === left.type;
}
}
function isGenericType(genericName) {
const type = this.getTypeAnnotation();
return t.isGenericTypeAnnotation(type) && t.isIdentifier(type.id, {
name: genericName
});
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var t = _interopRequireWildcard(require("@babel/types"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
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; }
function _default(node) {
if (!this.isReferenced()) return;
const binding = this.scope.getBinding(node.name);
if (binding) {
if (binding.identifier.typeAnnotation) {
return binding.identifier.typeAnnotation;
} else {
return getTypeAnnotationBindingConstantViolations(binding, this, node.name);
}
}
if (node.name === "undefined") {
return t.voidTypeAnnotation();
} else if (node.name === "NaN" || node.name === "Infinity") {
return t.numberTypeAnnotation();
} else if (node.name === "arguments") {}
}
function getTypeAnnotationBindingConstantViolations(binding, path, name) {
const types = [];
const functionConstantViolations = [];
let constantViolations = getConstantViolationsBefore(binding, path, functionConstantViolations);
const testType = getConditionalAnnotation(binding, path, name);
if (testType) {
const testConstantViolations = getConstantViolationsBefore(binding, testType.ifStatement);
constantViolations = constantViolations.filter(path => testConstantViolations.indexOf(path) < 0);
types.push(testType.typeAnnotation);
}
if (constantViolations.length) {
constantViolations = constantViolations.concat(functionConstantViolations);
for (const violation of constantViolations) {
types.push(violation.getTypeAnnotation());
}
}
if (types.length) {
return t.createUnionTypeAnnotation(types);
}
}
function getConstantViolationsBefore(binding, path, functions) {
const violations = binding.constantViolations.slice();
violations.unshift(binding.path);
return violations.filter(violation => {
violation = violation.resolve();
const status = violation._guessExecutionStatusRelativeTo(path);
if (functions && status === "unknown") functions.push(violation);
return status === "before";
});
}
function inferAnnotationFromBinaryExpression(name, path) {
const operator = path.node.operator;
const right = path.get("right").resolve();
const left = path.get("left").resolve();
let target;
if (left.isIdentifier({
name
})) {
target = right;
} else if (right.isIdentifier({
name
})) {
target = left;
}
if (target) {
if (operator === "===") {
return target.getTypeAnnotation();
}
if (t.BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
return t.numberTypeAnnotation();
}
return;
}
if (operator !== "===" && operator !== "==") return;
let typeofPath;
let typePath;
if (left.isUnaryExpression({
operator: "typeof"
})) {
typeofPath = left;
typePath = right;
} else if (right.isUnaryExpression({
operator: "typeof"
})) {
typeofPath = right;
typePath = left;
}
if (!typeofPath) return;
if (!typeofPath.get("argument").isIdentifier({
name
})) return;
typePath = typePath.resolve();
if (!typePath.isLiteral()) return;
const typeValue = typePath.node.value;
if (typeof typeValue !== "string") return;
return t.createTypeAnnotationBasedOnTypeof(typeValue);
}
function getParentConditionalPath(binding, path, name) {
let parentPath;
while (parentPath = path.parentPath) {
if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {
if (path.key === "test") {
return;
}
return parentPath;
}
if (parentPath.isFunction()) {
if (parentPath.parentPath.scope.getBinding(name) !== binding) return;
}
path = parentPath;
}
}
function getConditionalAnnotation(binding, path, name) {
const ifStatement = getParentConditionalPath(binding, path, name);
if (!ifStatement) return;
const test = ifStatement.get("test");
const paths = [test];
const types = [];
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
if (path.isLogicalExpression()) {
if (path.node.operator === "&&") {
paths.push(path.get("left"));
paths.push(path.get("right"));
}
} else if (path.isBinaryExpression()) {
const type = inferAnnotationFromBinaryExpression(name, path);
if (type) types.push(type);
}
}
if (types.length) {
return {
typeAnnotation: t.createUnionTypeAnnotation(types),
ifStatement
};
}
return getConditionalAnnotation(ifStatement, name);
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.VariableDeclarator = VariableDeclarator;
exports.TypeCastExpression = TypeCastExpression;
exports.NewExpression = NewExpression;
exports.TemplateLiteral = TemplateLiteral;
exports.UnaryExpression = UnaryExpression;
exports.BinaryExpression = BinaryExpression;
exports.LogicalExpression = LogicalExpression;
exports.ConditionalExpression = ConditionalExpression;
exports.SequenceExpression = SequenceExpression;
exports.ParenthesizedExpression = ParenthesizedExpression;
exports.AssignmentExpression = AssignmentExpression;
exports.UpdateExpression = UpdateExpression;
exports.StringLiteral = StringLiteral;
exports.NumericLiteral = NumericLiteral;
exports.BooleanLiteral = BooleanLiteral;
exports.NullLiteral = NullLiteral;
exports.RegExpLiteral = RegExpLiteral;
exports.ObjectExpression = ObjectExpression;
exports.ArrayExpression = ArrayExpression;
exports.RestElement = RestElement;
exports.ClassDeclaration = exports.ClassExpression = exports.FunctionDeclaration = exports.ArrowFunctionExpression = exports.FunctionExpression = Func;
exports.CallExpression = CallExpression;
exports.TaggedTemplateExpression = TaggedTemplateExpression;
Object.defineProperty(exports, "Identifier", {
enumerable: true,
get: function () {
return _infererReference.default;
}
});
var t = _interopRequireWildcard(require("@babel/types"));
var _infererReference = _interopRequireDefault(require("./inferer-reference"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
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; }
function VariableDeclarator() {
const id = this.get("id");
if (!id.isIdentifier()) return;
const init = this.get("init");
let type = init.getTypeAnnotation();
if (type && type.type === "AnyTypeAnnotation") {
if (init.isCallExpression() && init.get("callee").isIdentifier({
name: "Array"
}) && !init.scope.hasBinding("Array", true)) {
type = ArrayExpression();
}
}
return type;
}
function TypeCastExpression(node) {
return node.typeAnnotation;
}
TypeCastExpression.validParent = true;
function NewExpression(node) {
if (this.get("callee").isIdentifier()) {
return t.genericTypeAnnotation(node.callee);
}
}
function TemplateLiteral() {
return t.stringTypeAnnotation();
}
function UnaryExpression(node) {
const operator = node.operator;
if (operator === "void") {
return t.voidTypeAnnotation();
} else if (t.NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
return t.numberTypeAnnotation();
} else if (t.STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {
return t.stringTypeAnnotation();
} else if (t.BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {
return t.booleanTypeAnnotation();
}
}
function BinaryExpression(node) {
const operator = node.operator;
if (t.NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
return t.numberTypeAnnotation();
} else if (t.BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
return t.booleanTypeAnnotation();
} else if (operator === "+") {
const right = this.get("right");
const left = this.get("left");
if (left.isBaseType("number") && right.isBaseType("number")) {
return t.numberTypeAnnotation();
} else if (left.isBaseType("string") || right.isBaseType("string")) {
return t.stringTypeAnnotation();
}
return t.unionTypeAnnotation([t.stringTypeAnnotation(), t.numberTypeAnnotation()]);
}
}
function LogicalExpression() {
return t.createUnionTypeAnnotation([this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()]);
}
function ConditionalExpression() {
return t.createUnionTypeAnnotation([this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()]);
}
function SequenceExpression() {
return this.get("expressions").pop().getTypeAnnotation();
}
function ParenthesizedExpression() {
return this.get("expression").getTypeAnnotation();
}
function AssignmentExpression() {
return this.get("right").getTypeAnnotation();
}
function UpdateExpression(node) {
const operator = node.operator;
if (operator === "++" || operator === "--") {
return t.numberTypeAnnotation();
}
}
function StringLiteral() {
return t.stringTypeAnnotation();
}
function NumericLiteral() {
return t.numberTypeAnnotation();
}
function BooleanLiteral() {
return t.booleanTypeAnnotation();
}
function NullLiteral() {
return t.nullLiteralTypeAnnotation();
}
function RegExpLiteral() {
return t.genericTypeAnnotation(t.identifier("RegExp"));
}
function ObjectExpression() {
return t.genericTypeAnnotation(t.identifier("Object"));
}
function ArrayExpression() {
return t.genericTypeAnnotation(t.identifier("Array"));
}
function RestElement() {
return ArrayExpression();
}
RestElement.validParent = true;
function Func() {
return t.genericTypeAnnotation(t.identifier("Function"));
}
const isArrayFrom = t.buildMatchMemberExpression("Array.from");
const isObjectKeys = t.buildMatchMemberExpression("Object.keys");
const isObjectValues = t.buildMatchMemberExpression("Object.values");
const isObjectEntries = t.buildMatchMemberExpression("Object.entries");
function CallExpression() {
const {
callee
} = this.node;
if (isObjectKeys(callee)) {
return t.arrayTypeAnnotation(t.stringTypeAnnotation());
} else if (isArrayFrom(callee) || isObjectValues(callee)) {
return t.arrayTypeAnnotation(t.anyTypeAnnotation());
} else if (isObjectEntries(callee)) {
return t.arrayTypeAnnotation(t.tupleTypeAnnotation([t.stringTypeAnnotation(), t.anyTypeAnnotation()]));
}
return resolveCall(this.get("callee"));
}
function TaggedTemplateExpression() {
return resolveCall(this.get("tag"));
}
function resolveCall(callee) {
callee = callee.resolve();
if (callee.isFunction()) {
if (callee.is("async")) {
if (callee.is("generator")) {
return t.genericTypeAnnotation(t.identifier("AsyncIterator"));
} else {
return t.genericTypeAnnotation(t.identifier("Promise"));
}
} else {
if (callee.node.returnType) {
return callee.node.returnType;
} else {}
}
}
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
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;
exports.isConstantExpression = isConstantExpression;
exports.isInStrictMode = isInStrictMode;
exports.is = void 0;
var _includes = _interopRequireDefault(require("lodash/includes"));
var t = _interopRequireWildcard(require("@babel/types"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
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; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function matchesPattern(pattern, allowPartial) {
return t.matchesPattern(this.node, pattern, allowPartial);
}
function has(key) {
const 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);
}
const is = has;
exports.is = is;
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) {
let path = this;
let first = true;
do {
const 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, _includes.default)(t.STATEMENT_OR_BLOCK_KEYS, this.key);
}
}
function referencesImport(moduleSource, importName) {
if (!this.isReferencedIdentifier()) return false;
const binding = this.scope.getBinding(this.node.name);
if (!binding || binding.kind !== "module") return false;
const path = binding.path;
const 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() {
const node = this.node;
if (node.end) {
const code = this.hub.getCode();
if (code) return code.slice(node.start, node.end);
}
return "";
}
function willIMaybeExecuteBefore(target) {
return this._guessExecutionStatusRelativeTo(target) !== "after";
}
function getOuterFunction(path) {
return (path.scope.getFunctionParent() || path.scope.getProgramParent()).path;
}
function isExecutionUncertain(type, key) {
switch (type) {
case "LogicalExpression":
return key === "right";
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;
}
}
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;
const commonIndex = {
target: 0,
this: 0
};
while (!commonPath && commonIndex.this < paths.this.length) {
const path = paths.this[commonIndex.this];
commonIndex.target = paths.target.indexOf(path);
if (commonIndex.target >= 0) {
commonPath = path;
} else {
commonIndex.this++;
}
}
if (!commonPath) {
throw new Error("Internal Babel error - The two compared nodes" + " don't appear to belong to the same program.");
}
if (isExecutionUncertainInList(paths.this, commonIndex.this - 1) || isExecutionUncertainInList(paths.target, commonIndex.target - 1)) {
return "unknown";
}
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 keyPosition = {
this: keys.indexOf(divergence.this.parentKey),
target: keys.indexOf(divergence.target.parentKey)
};
return keyPosition.target > keyPosition.this ? "before" : "after";
}
const executionOrderCheckedNodes = new WeakSet();
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 === 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);
executionOrderCheckedNodes.delete(path.node);
if (allStatus && allStatus !== status) {
return "unknown";
} 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()) {
const binding = this.scope.getBinding(this.node.name);
if (!binding) return;
if (!binding.constant) return;
if (binding.kind === "module") return;
if (binding.path !== this) {
const ret = binding.path.resolve(dangerous, resolved);
if (this.find(parent => parent.node === ret.node)) return;
return ret;
}
} 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;
const targetName = targetKey.value;
const target = this.get("object").resolve(dangerous, resolved);
if (target.isObjectExpression()) {
const props = target.get("properties");
for (const prop of props) {
if (!prop.isProperty()) continue;
const key = prop.get("key");
let 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)) {
const elems = target.get("elements");
const elem = elems[targetName];
if (elem) return elem.resolve(dangerous, resolved);
}
}
}
function isConstantExpression() {
if (this.isIdentifier()) {
const binding = this.scope.getBinding(this.node.name);
if (!binding) return false;
return binding.constant;
}
if (this.isLiteral()) {
if (this.isRegExpLiteral()) {
return false;
}
if (this.isTemplateLiteral()) {
return this.get("expressions").every(expression => expression.isConstantExpression());
}
return true;
}
if (this.isUnaryExpression()) {
if (this.get("operator").node !== "void") {
return false;
}
return this.get("argument").isConstantExpression();
}
if (this.isBinaryExpression()) {
return this.get("left").isConstantExpression() && this.get("right").isConstantExpression();
}
return false;
}
function isInStrictMode() {
const start = this.isProgram() ? this : this.parentPath;
const strictParent = start.find(path => {
if (path.isProgram({
sourceType: "module"
})) return true;
if (path.isClass()) return true;
if (!path.isProgram() && !path.isFunction()) return false;
if (path.isArrowFunctionExpression() && !path.get("body").isBlockStatement()) {
return false;
}
let {
node
} = path;
if (path.isFunction()) node = node.body;
for (const directive of node.directives) {
if (directive.value.value === "use strict") {
return true;
}
}
});
return !!strictParent;
}
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var t = _interopRequireWildcard(require("@babel/types"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
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; }
const referenceVisitor = {
ReferencedIdentifier(path, state) {
if (path.isJSXIdentifier() && t.react.isCompatTag(path.node.name) && !path.parentPath.isJSXMemberExpression()) {
return;
}
if (path.node.name === "this") {
let scope = path.scope;
do {
if (scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
break;
}
} while (scope = scope.parent);
if (scope) state.breakOnScopePaths.push(scope.path);
}
const binding = path.scope.getBinding(path.node.name);
if (!binding) return;
for (const violation of binding.constantViolations) {
if (violation.scope !== binding.path.scope) {
state.mutableBinding = true;
path.stop();
return;
}
}
if (binding !== state.scope.getBinding(path.node.name)) return;
state.bindings[path.node.name] = binding;
}
};
class PathHoister {
constructor(path, scope) {
this.breakOnScopePaths = [];
this.bindings = {};
this.mutableBinding = false;
this.scopes = [];
this.scope = scope;
this.path = path;
this.attachAfter = false;
}
isCompatibleScope(scope) {
for (const key of Object.keys(this.bindings)) {
const binding = this.bindings[key];
if (!scope.bindingIdentifierEquals(key, binding.identifier)) {
return false;
}
}
return true;
}
getCompatibleScopes() {
let scope = this.path.scope;
do {
if (this.isCompatibleScope(scope)) {
this.scopes.push(scope);
} else {
break;
}
if (this.breakOnScopePaths.indexOf(scope.path) >= 0) {
break;
}
} while (scope = scope.parent);
}
getAttachmentPath() {
let path = this._getAttachmentPath();
if (!path) return;
let targetScope = path.scope;
if (targetScope.path === path) {
targetScope = path.scope.parent;
}
if (targetScope.path.isProgram() || targetScope.path.isFunction()) {
for (const name of Object.keys(this.bindings)) {
if (!targetScope.hasOwnBinding(name)) continue;
const binding = this.bindings[name];
if (binding.kind === "param" || binding.path.parentKey === "params") {
continue;
}
const bindingParentPath = this.getAttachmentParentForPath(binding.path);
if (bindingParentPath.key >= path.key) {
this.attachAfter = true;
path = binding.path;
for (const violationPath of binding.constantViolations) {
if (this.getAttachmentParentForPath(violationPath).key > path.key) {
path = violationPath;
}
}
}
}
}
return path;
}
_getAttachmentPath() {
const scopes = this.scopes;
const scope = scopes.pop();
if (!scope) return;
if (scope.path.isFunction()) {
if (this.hasOwnParamBindings(scope)) {
if (this.scope === scope) return;
const bodies = scope.path.get("body").get("body");
for (let i = 0; i < bodies.length; i++) {
if (bodies[i].node._blockHoist) continue;
return bodies[i];
}
} else {
return this.getNextScopeAttachmentParent();
}
} else if (scope.path.isProgram()) {
return this.getNextScopeAttachmentParent();
}
}
getNextScopeAttachmentParent() {
const scope = this.scopes.pop();
if (scope) return this.getAttachmentParentForPath(scope.path);
}
getAttachmentParentForPath(path) {
do {
if (!path.parentPath || Array.isArray(path.container) && path.isStatement()) {
return path;
}
} while (path = path.parentPath);
}
hasOwnParamBindings(scope) {
for (const name of Object.keys(this.bindings)) {
if (!scope.hasOwnBinding(name)) continue;
const binding = this.bindings[name];
if (binding.kind === "param" && binding.constant) return true;
}
return false;
}
run() {
this.path.traverse(referenceVisitor, this);
if (this.mutableBinding) return;
this.getCompatibleScopes();
const attachTo = this.getAttachmentPath();
if (!attachTo) return;
if (attachTo.getFunctionParent() === this.path.getFunctionParent()) return;
let uid = attachTo.scope.generateUidIdentifier("ref");
const declarator = t.variableDeclarator(uid, this.path.node);
const insertFn = this.attachAfter ? "insertAfter" : "insertBefore";
const [attached] = attachTo[insertFn]([attachTo.isVariableDeclarator() ? declarator : t.variableDeclaration("var", [declarator])]);
const parent = this.path.parentPath;
if (parent.isJSXElement() && this.path.container === parent.node.children) {
uid = t.JSXExpressionContainer(uid);
}
this.path.replaceWith(t.cloneNode(uid));
return attachTo.isVariableDeclarator() ? attached.get("init") : attached.get("declarations.0.init");
}
}
exports.default = PathHoister;
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hooks = void 0;
const hooks = [function (self, parent) {
const removeParent = self.key === "test" && (parent.isWhile() || parent.isSwitchCase()) || self.key === "declaration" && parent.isExportDeclaration() || self.key === "body" && parent.isLabeledStatement() || self.listKey === "declarations" && parent.isVariableDeclaration() && parent.node.declarations.length === 1 || self.key === "expression" && parent.isExpressionStatement();
if (removeParent) {
parent.remove();
return true;
}
}, function (self, parent) {
if (parent.isSequenceExpression() && parent.node.expressions.length === 1) {
parent.replaceWith(parent.node.expressions[0]);
return true;
}
}, function (self, parent) {
if (parent.isBinary()) {
if (self.key === "left") {
parent.replaceWith(parent.node.right);
} else {
parent.replaceWith(parent.node.left);
}
return true;
}
}, function (self, parent) {
if (parent.isIfStatement() && (self.key === "consequent" || self.key === "alternate") || self.key === "body" && (parent.isLoop() || parent.isArrowFunctionExpression())) {
self.replaceWith({
type: "BlockStatement",
body: []
});
return true;
}
}];
exports.hooks = hooks;
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment