| [9af201e] | 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = void 0;
|
|---|
| 7 | var _core = require("@babel/core");
|
|---|
| 8 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
|---|
| 9 | var _template = require("@babel/template");
|
|---|
| 10 | var DefineAccessorHelper = _template.default.expression.ast`
|
|---|
| 11 | function (type, obj, key, fn) {
|
|---|
| 12 | var desc = { configurable: true, enumerable: true };
|
|---|
| 13 | desc[type] = fn;
|
|---|
| 14 | return Object.defineProperty(obj, key, desc);
|
|---|
| 15 | }
|
|---|
| 16 | `;
|
|---|
| 17 | DefineAccessorHelper._compact = true;
|
|---|
| 18 | var _default = exports.default = (0, _helperPluginUtils.declare)((api, options) => {
|
|---|
| 19 | var _api$assumption;
|
|---|
| 20 | api.assertVersion(7);
|
|---|
| 21 | const setComputedProperties = (_api$assumption = api.assumption("setComputedProperties")) != null ? _api$assumption : options.loose;
|
|---|
| 22 | const pushComputedProps = setComputedProperties ? pushComputedPropsLoose : pushComputedPropsSpec;
|
|---|
| 23 | function buildDefineAccessor(state, obj, prop) {
|
|---|
| 24 | const type = prop.kind;
|
|---|
| 25 | const key = !prop.computed && _core.types.isIdentifier(prop.key) ? _core.types.stringLiteral(prop.key.name) : prop.key;
|
|---|
| 26 | const fn = getValue(prop);
|
|---|
| 27 | let helper;
|
|---|
| 28 | if (state.availableHelper("defineAccessor")) {
|
|---|
| 29 | helper = state.addHelper("defineAccessor");
|
|---|
| 30 | } else {
|
|---|
| 31 | const file = state.file;
|
|---|
| 32 | helper = file.get("fallbackDefineAccessorHelper");
|
|---|
| 33 | if (!helper) {
|
|---|
| 34 | const id = file.scope.generateUidIdentifier("defineAccessor");
|
|---|
| 35 | file.scope.push({
|
|---|
| 36 | id,
|
|---|
| 37 | init: DefineAccessorHelper
|
|---|
| 38 | });
|
|---|
| 39 | file.set("fallbackDefineAccessorHelper", helper = id);
|
|---|
| 40 | }
|
|---|
| 41 | helper = _core.types.cloneNode(helper);
|
|---|
| 42 | }
|
|---|
| 43 | return _core.types.callExpression(helper, [_core.types.stringLiteral(type), obj, key, fn]);
|
|---|
| 44 | }
|
|---|
| 45 | function getValue(prop) {
|
|---|
| 46 | if (_core.types.isObjectProperty(prop)) {
|
|---|
| 47 | return prop.value;
|
|---|
| 48 | } else if (_core.types.isObjectMethod(prop)) {
|
|---|
| 49 | return _core.types.functionExpression(null, prop.params, prop.body, prop.generator, prop.async);
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 | function pushAssign(objId, prop, body) {
|
|---|
| 53 | body.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.cloneNode(objId), prop.key, prop.computed || _core.types.isLiteral(prop.key)), getValue(prop))));
|
|---|
| 54 | }
|
|---|
| 55 | function pushComputedPropsLoose(info) {
|
|---|
| 56 | const {
|
|---|
| 57 | computedProps,
|
|---|
| 58 | state,
|
|---|
| 59 | initPropExpression,
|
|---|
| 60 | objId,
|
|---|
| 61 | body
|
|---|
| 62 | } = info;
|
|---|
| 63 | for (const prop of computedProps) {
|
|---|
| 64 | if (_core.types.isObjectMethod(prop) && (prop.kind === "get" || prop.kind === "set")) {
|
|---|
| 65 | if (computedProps.length === 1) {
|
|---|
| 66 | return buildDefineAccessor(state, initPropExpression, prop);
|
|---|
| 67 | } else {
|
|---|
| 68 | body.push(_core.types.expressionStatement(buildDefineAccessor(state, _core.types.cloneNode(objId), prop)));
|
|---|
| 69 | }
|
|---|
| 70 | } else {
|
|---|
| 71 | pushAssign(_core.types.cloneNode(objId), prop, body);
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 | function pushComputedPropsSpec(info) {
|
|---|
| 76 | const {
|
|---|
| 77 | objId,
|
|---|
| 78 | body,
|
|---|
| 79 | computedProps,
|
|---|
| 80 | state
|
|---|
| 81 | } = info;
|
|---|
| 82 | const CHUNK_LENGTH_CAP = 10;
|
|---|
| 83 | let currentChunk = null;
|
|---|
| 84 | const computedPropsChunks = [];
|
|---|
| 85 | for (const prop of computedProps) {
|
|---|
| 86 | if (!currentChunk || currentChunk.length === CHUNK_LENGTH_CAP) {
|
|---|
| 87 | currentChunk = [];
|
|---|
| 88 | computedPropsChunks.push(currentChunk);
|
|---|
| 89 | }
|
|---|
| 90 | currentChunk.push(prop);
|
|---|
| 91 | }
|
|---|
| 92 | for (const chunk of computedPropsChunks) {
|
|---|
| 93 | const single = computedPropsChunks.length === 1;
|
|---|
| 94 | let node = single ? info.initPropExpression : _core.types.cloneNode(objId);
|
|---|
| 95 | for (const prop of chunk) {
|
|---|
| 96 | if (_core.types.isObjectMethod(prop) && (prop.kind === "get" || prop.kind === "set")) {
|
|---|
| 97 | node = buildDefineAccessor(info.state, node, prop);
|
|---|
| 98 | } else {
|
|---|
| 99 | node = _core.types.callExpression(state.addHelper("defineProperty"), [node, _core.types.toComputedKey(prop), getValue(prop)]);
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | if (single) return node;
|
|---|
| 103 | body.push(_core.types.expressionStatement(node));
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | return {
|
|---|
| 107 | name: "transform-computed-properties",
|
|---|
| 108 | visitor: {
|
|---|
| 109 | ObjectExpression: {
|
|---|
| 110 | exit(path, state) {
|
|---|
| 111 | const {
|
|---|
| 112 | node,
|
|---|
| 113 | parent,
|
|---|
| 114 | scope
|
|---|
| 115 | } = path;
|
|---|
| 116 | let hasComputed = false;
|
|---|
| 117 | for (const prop of node.properties) {
|
|---|
| 118 | hasComputed = prop.computed === true;
|
|---|
| 119 | if (hasComputed) break;
|
|---|
| 120 | }
|
|---|
| 121 | if (!hasComputed) return;
|
|---|
| 122 | const initProps = [];
|
|---|
| 123 | const computedProps = [];
|
|---|
| 124 | let foundComputed = false;
|
|---|
| 125 | for (const prop of node.properties) {
|
|---|
| 126 | if (_core.types.isSpreadElement(prop)) {
|
|---|
| 127 | continue;
|
|---|
| 128 | }
|
|---|
| 129 | if (prop.computed) {
|
|---|
| 130 | foundComputed = true;
|
|---|
| 131 | }
|
|---|
| 132 | if (foundComputed) {
|
|---|
| 133 | computedProps.push(prop);
|
|---|
| 134 | } else {
|
|---|
| 135 | initProps.push(prop);
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | const objId = scope.generateUidIdentifierBasedOnNode(parent);
|
|---|
| 139 | const initPropExpression = _core.types.objectExpression(initProps);
|
|---|
| 140 | const body = [];
|
|---|
| 141 | body.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(objId, initPropExpression)]));
|
|---|
| 142 | const single = pushComputedProps({
|
|---|
| 143 | scope,
|
|---|
| 144 | objId,
|
|---|
| 145 | body,
|
|---|
| 146 | computedProps,
|
|---|
| 147 | initPropExpression,
|
|---|
| 148 | state
|
|---|
| 149 | });
|
|---|
| 150 | if (single) {
|
|---|
| 151 | path.replaceWith(single);
|
|---|
| 152 | } else {
|
|---|
| 153 | if (setComputedProperties) {
|
|---|
| 154 | body.push(_core.types.expressionStatement(_core.types.cloneNode(objId)));
|
|---|
| 155 | }
|
|---|
| 156 | path.replaceWithMultiple(body);
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 | };
|
|---|
| 162 | });
|
|---|
| 163 |
|
|---|
| 164 | //# sourceMappingURL=index.js.map
|
|---|