|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- "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 = require("@babel/types");
-
- var _helperFunctionName = require("@babel/helper-function-name");
-
- function toComputedKey() {
- let key;
-
- if (this.isMemberExpression()) {
- key = this.node.property;
- } else if (this.isProperty() || this.isMethod()) {
- key = this.node.key;
- } else {
- throw new ReferenceError("todo");
- }
-
- if (!this.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,
- noNewArrows = !specCompliant
- } = {}) {
- if (!this.isArrowFunctionExpression()) {
- throw this.buildCodeFrameError("Cannot convert non-arrow function to a function expression.");
- }
-
- const thisBinding = hoistFunctionEnvironment(this, noNewArrows, allowInsertArrow);
- this.ensureBlock();
- this.node.type = "FunctionExpression";
-
- if (!noNewArrows) {
- 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, noNewArrows = true, allowInsertArrow = true) {
- const thisEnvFn = fnPath.findParent(p => {
- return p.isFunction() && !p.isArrowFunctionExpression() || p.isProgram() || p.isClassProperty({
- static: false
- });
- });
- const inConstructor = (thisEnvFn == null ? void 0 : 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 || !noNewArrows) {
- thisBinding = getThisBinding(thisEnvFn, inConstructor);
-
- if (noNewArrows || 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 (!noNewArrows) 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
- };
- }
|