123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- 'use strict';
-
- Object.defineProperty(exports, '__esModule', { value: true });
-
- var t = require('@babel/types');
-
- function _interopNamespace(e) {
- if (e && e.__esModule) return e;
- var n = Object.create(null);
- if (e) {
- Object.keys(e).forEach(function (k) {
- if (k !== 'default') {
- var d = Object.getOwnPropertyDescriptor(e, k);
- Object.defineProperty(n, k, d.get ? d : {
- enumerable: true,
- get: function () {
- return e[k];
- }
- });
- }
- });
- }
- n['default'] = e;
- return Object.freeze(n);
- }
-
- var t__namespace = /*#__PURE__*/_interopNamespace(t);
-
- function willPathCastToBoolean(path) {
- const maybeWrapped = path;
- const {
- node,
- parentPath
- } = maybeWrapped;
-
- if (parentPath.isLogicalExpression()) {
- const {
- operator,
- right
- } = parentPath.node;
-
- if (operator === "&&" || operator === "||" || operator === "??" && node === right) {
- return willPathCastToBoolean(parentPath);
- }
- }
-
- if (parentPath.isSequenceExpression()) {
- const {
- expressions
- } = parentPath.node;
-
- if (expressions[expressions.length - 1] === node) {
- return willPathCastToBoolean(parentPath);
- } else {
- return true;
- }
- }
-
- return parentPath.isConditional({
- test: node
- }) || parentPath.isUnaryExpression({
- operator: "!"
- }) || parentPath.isLoop({
- test: node
- });
- }
-
- class AssignmentMemoiser {
- constructor() {
- this._map = void 0;
- this._map = new WeakMap();
- }
-
- has(key) {
- return this._map.has(key);
- }
-
- get(key) {
- if (!this.has(key)) return;
-
- const record = this._map.get(key);
-
- const {
- value
- } = record;
- record.count--;
-
- if (record.count === 0) {
- return t__namespace.assignmentExpression("=", value, key);
- }
-
- return value;
- }
-
- set(key, value, count) {
- return this._map.set(key, {
- count,
- value
- });
- }
-
- }
-
- function toNonOptional(path, base) {
- const {
- node
- } = path;
-
- if (path.isOptionalMemberExpression()) {
- return t__namespace.memberExpression(base, node.property, node.computed);
- }
-
- if (path.isOptionalCallExpression()) {
- const callee = path.get("callee");
-
- if (path.node.optional && callee.isOptionalMemberExpression()) {
- const {
- object
- } = callee.node;
- const context = path.scope.maybeGenerateMemoised(object) || object;
- callee.get("object").replaceWith(t__namespace.assignmentExpression("=", context, object));
- return t__namespace.callExpression(t__namespace.memberExpression(base, t__namespace.identifier("call")), [context, ...node.arguments]);
- }
-
- return t__namespace.callExpression(base, node.arguments);
- }
-
- return path.node;
- }
-
- function isInDetachedTree(path) {
- while (path) {
- if (path.isProgram()) break;
- const {
- parentPath,
- container,
- listKey
- } = path;
- const parentNode = parentPath.node;
-
- if (listKey) {
- if (container !== parentNode[listKey]) return true;
- } else {
- if (container !== parentNode) return true;
- }
-
- path = parentPath;
- }
-
- return false;
- }
-
- const handle = {
- memoise() {},
-
- handle(member, noDocumentAll) {
- const {
- node,
- parent,
- parentPath,
- scope
- } = member;
-
- if (member.isOptionalMemberExpression()) {
- if (isInDetachedTree(member)) return;
- const endPath = member.find(({
- node,
- parent,
- parentPath
- }) => {
- if (parentPath.isOptionalMemberExpression()) {
- return parent.optional || parent.object !== node;
- }
-
- if (parentPath.isOptionalCallExpression()) {
- return node !== member.node && parent.optional || parent.callee !== node;
- }
-
- return true;
- });
-
- if (scope.path.isPattern()) {
- endPath.replaceWith(t__namespace.callExpression(t__namespace.arrowFunctionExpression([], endPath.node), []));
- return;
- }
-
- const willEndPathCastToBoolean = willPathCastToBoolean(endPath);
- const rootParentPath = endPath.parentPath;
-
- if (rootParentPath.isUpdateExpression({
- argument: node
- }) || rootParentPath.isAssignmentExpression({
- left: node
- })) {
- throw member.buildCodeFrameError(`can't handle assignment`);
- }
-
- const isDeleteOperation = rootParentPath.isUnaryExpression({
- operator: "delete"
- });
-
- if (isDeleteOperation && endPath.isOptionalMemberExpression() && endPath.get("property").isPrivateName()) {
- throw member.buildCodeFrameError(`can't delete a private class element`);
- }
-
- let startingOptional = member;
-
- for (;;) {
- if (startingOptional.isOptionalMemberExpression()) {
- if (startingOptional.node.optional) break;
- startingOptional = startingOptional.get("object");
- continue;
- } else if (startingOptional.isOptionalCallExpression()) {
- if (startingOptional.node.optional) break;
- startingOptional = startingOptional.get("callee");
- continue;
- }
-
- throw new Error(`Internal error: unexpected ${startingOptional.node.type}`);
- }
-
- const startingProp = startingOptional.isOptionalMemberExpression() ? "object" : "callee";
- const startingNode = startingOptional.node[startingProp];
- const baseNeedsMemoised = scope.maybeGenerateMemoised(startingNode);
- const baseRef = baseNeedsMemoised != null ? baseNeedsMemoised : startingNode;
- const parentIsOptionalCall = parentPath.isOptionalCallExpression({
- callee: node
- });
- const parentIsCall = parentPath.isCallExpression({
- callee: node
- });
- startingOptional.replaceWith(toNonOptional(startingOptional, baseRef));
-
- if (parentIsOptionalCall) {
- if (parent.optional) {
- parentPath.replaceWith(this.optionalCall(member, parent.arguments));
- } else {
- parentPath.replaceWith(this.call(member, parent.arguments));
- }
- } else if (parentIsCall) {
- member.replaceWith(this.boundGet(member));
- } else {
- member.replaceWith(this.get(member));
- }
-
- let regular = member.node;
-
- for (let current = member; current !== endPath;) {
- const {
- parentPath
- } = current;
-
- if (parentPath === endPath && parentIsOptionalCall && parent.optional) {
- regular = parentPath.node;
- break;
- }
-
- regular = toNonOptional(parentPath, regular);
- current = parentPath;
- }
-
- let context;
- const endParentPath = endPath.parentPath;
-
- if (t__namespace.isMemberExpression(regular) && endParentPath.isOptionalCallExpression({
- callee: endPath.node,
- optional: true
- })) {
- const {
- object
- } = regular;
- context = member.scope.maybeGenerateMemoised(object);
-
- if (context) {
- regular.object = t__namespace.assignmentExpression("=", context, object);
- }
- }
-
- let replacementPath = endPath;
-
- if (isDeleteOperation) {
- replacementPath = endParentPath;
- regular = endParentPath.node;
- }
-
- const baseMemoised = baseNeedsMemoised ? t__namespace.assignmentExpression("=", t__namespace.cloneNode(baseRef), t__namespace.cloneNode(startingNode)) : t__namespace.cloneNode(baseRef);
-
- if (willEndPathCastToBoolean) {
- let nonNullishCheck;
-
- if (noDocumentAll) {
- nonNullishCheck = t__namespace.binaryExpression("!=", baseMemoised, t__namespace.nullLiteral());
- } else {
- nonNullishCheck = t__namespace.logicalExpression("&&", t__namespace.binaryExpression("!==", baseMemoised, t__namespace.nullLiteral()), t__namespace.binaryExpression("!==", t__namespace.cloneNode(baseRef), scope.buildUndefinedNode()));
- }
-
- replacementPath.replaceWith(t__namespace.logicalExpression("&&", nonNullishCheck, regular));
- } else {
- let nullishCheck;
-
- if (noDocumentAll) {
- nullishCheck = t__namespace.binaryExpression("==", baseMemoised, t__namespace.nullLiteral());
- } else {
- nullishCheck = t__namespace.logicalExpression("||", t__namespace.binaryExpression("===", baseMemoised, t__namespace.nullLiteral()), t__namespace.binaryExpression("===", t__namespace.cloneNode(baseRef), scope.buildUndefinedNode()));
- }
-
- replacementPath.replaceWith(t__namespace.conditionalExpression(nullishCheck, isDeleteOperation ? t__namespace.booleanLiteral(true) : scope.buildUndefinedNode(), regular));
- }
-
- if (context) {
- const endParent = endParentPath.node;
- endParentPath.replaceWith(t__namespace.optionalCallExpression(t__namespace.optionalMemberExpression(endParent.callee, t__namespace.identifier("call"), false, true), [t__namespace.cloneNode(context), ...endParent.arguments], false));
- }
-
- return;
- }
-
- if (parentPath.isUpdateExpression({
- argument: node
- })) {
- if (this.simpleSet) {
- member.replaceWith(this.simpleSet(member));
- return;
- }
-
- const {
- operator,
- prefix
- } = parent;
- this.memoise(member, 2);
- const value = t__namespace.binaryExpression(operator[0], t__namespace.unaryExpression("+", this.get(member)), t__namespace.numericLiteral(1));
-
- if (prefix) {
- parentPath.replaceWith(this.set(member, value));
- } else {
- const {
- scope
- } = member;
- const ref = scope.generateUidIdentifierBasedOnNode(node);
- scope.push({
- id: ref
- });
- value.left = t__namespace.assignmentExpression("=", t__namespace.cloneNode(ref), value.left);
- parentPath.replaceWith(t__namespace.sequenceExpression([this.set(member, value), t__namespace.cloneNode(ref)]));
- }
-
- return;
- }
-
- if (parentPath.isAssignmentExpression({
- left: node
- })) {
- if (this.simpleSet) {
- member.replaceWith(this.simpleSet(member));
- return;
- }
-
- const {
- operator,
- right: value
- } = parent;
-
- if (operator === "=") {
- parentPath.replaceWith(this.set(member, value));
- } else {
- const operatorTrunc = operator.slice(0, -1);
-
- if (t__namespace.LOGICAL_OPERATORS.includes(operatorTrunc)) {
- this.memoise(member, 1);
- parentPath.replaceWith(t__namespace.logicalExpression(operatorTrunc, this.get(member), this.set(member, value)));
- } else {
- this.memoise(member, 2);
- parentPath.replaceWith(this.set(member, t__namespace.binaryExpression(operatorTrunc, this.get(member), value)));
- }
- }
-
- return;
- }
-
- if (parentPath.isCallExpression({
- callee: node
- })) {
- parentPath.replaceWith(this.call(member, parent.arguments));
- return;
- }
-
- if (parentPath.isOptionalCallExpression({
- callee: node
- })) {
- if (scope.path.isPattern()) {
- parentPath.replaceWith(t__namespace.callExpression(t__namespace.arrowFunctionExpression([], parentPath.node), []));
- return;
- }
-
- parentPath.replaceWith(this.optionalCall(member, parent.arguments));
- return;
- }
-
- if (parentPath.isForXStatement({
- left: node
- }) || parentPath.isObjectProperty({
- value: node
- }) && parentPath.parentPath.isObjectPattern() || parentPath.isAssignmentPattern({
- left: node
- }) && parentPath.parentPath.isObjectProperty({
- value: parent
- }) && parentPath.parentPath.parentPath.isObjectPattern() || parentPath.isArrayPattern() || parentPath.isAssignmentPattern({
- left: node
- }) && parentPath.parentPath.isArrayPattern() || parentPath.isRestElement()) {
- member.replaceWith(this.destructureSet(member));
- return;
- }
-
- if (parentPath.isTaggedTemplateExpression()) {
- member.replaceWith(this.boundGet(member));
- } else {
- member.replaceWith(this.get(member));
- }
- }
-
- };
- function memberExpressionToFunctions(path, visitor, state) {
- path.traverse(visitor, Object.assign({}, handle, state, {
- memoiser: new AssignmentMemoiser()
- }));
- }
-
- exports.default = memberExpressionToFunctions;
- //# sourceMappingURL=index.js.map
|