1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/@babel/plugin-transform-template-l.../lib/index.js

109 lines
3.7 KiB
JavaScript
Raw Normal View History

2023-12-18 13:12:25 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
2024-01-16 21:26:16 +08:00
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _core = require("@babel/core");
var _default = exports.default = (0, _helperPluginUtils.declare)((api, options) => {
var _api$assumption, _api$assumption2;
2023-12-18 13:12:25 +08:00
api.assertVersion(7);
2024-01-16 21:26:16 +08:00
const ignoreToPrimitiveHint = (_api$assumption = api.assumption("ignoreToPrimitiveHint")) != null ? _api$assumption : options.loose;
const mutableTemplateObject = (_api$assumption2 = api.assumption("mutableTemplateObject")) != null ? _api$assumption2 : options.loose;
2023-12-18 13:12:25 +08:00
let helperName = "taggedTemplateLiteral";
2024-01-16 21:26:16 +08:00
if (mutableTemplateObject) helperName += "Loose";
2023-12-18 13:12:25 +08:00
function buildConcatCallExpressions(items) {
let avail = true;
return items.reduce(function (left, right) {
2024-01-16 21:26:16 +08:00
let canBeInserted = _core.types.isLiteral(right);
2023-12-18 13:12:25 +08:00
if (!canBeInserted && avail) {
canBeInserted = true;
avail = false;
}
2024-01-16 21:26:16 +08:00
if (canBeInserted && _core.types.isCallExpression(left)) {
2023-12-18 13:12:25 +08:00
left.arguments.push(right);
return left;
}
2024-01-16 21:26:16 +08:00
return _core.types.callExpression(_core.types.memberExpression(left, _core.types.identifier("concat")), [right]);
2023-12-18 13:12:25 +08:00
});
}
return {
name: "transform-template-literals",
visitor: {
TaggedTemplateExpression(path) {
const {
node
} = path;
const {
quasi
} = node;
const strings = [];
const raws = [];
let isStringsRawEqual = true;
for (const elem of quasi.quasis) {
const {
raw,
cooked
} = elem.value;
2024-01-16 21:26:16 +08:00
const value = cooked == null ? path.scope.buildUndefinedNode() : _core.types.stringLiteral(cooked);
2023-12-18 13:12:25 +08:00
strings.push(value);
2024-01-16 21:26:16 +08:00
raws.push(_core.types.stringLiteral(raw));
2023-12-18 13:12:25 +08:00
if (raw !== cooked) {
isStringsRawEqual = false;
}
}
2024-01-16 21:26:16 +08:00
const helperArgs = [_core.types.arrayExpression(strings)];
2023-12-18 13:12:25 +08:00
if (!isStringsRawEqual) {
2024-01-16 21:26:16 +08:00
helperArgs.push(_core.types.arrayExpression(raws));
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
const tmp = path.scope.generateUidIdentifier("templateObject");
path.scope.getProgramParent().push({
id: _core.types.cloneNode(tmp)
});
path.replaceWith(_core.types.callExpression(node.tag, [_core.template.expression.ast`
${_core.types.cloneNode(tmp)} || (
${tmp} = ${this.addHelper(helperName)}(${helperArgs})
)
`, ...quasi.expressions]));
2023-12-18 13:12:25 +08:00
},
TemplateLiteral(path) {
2024-01-16 21:26:16 +08:00
if (path.parent.type === "TSLiteralType") {
return;
}
2023-12-18 13:12:25 +08:00
const nodes = [];
const expressions = path.get("expressions");
let index = 0;
for (const elem of path.node.quasis) {
if (elem.value.cooked) {
2024-01-16 21:26:16 +08:00
nodes.push(_core.types.stringLiteral(elem.value.cooked));
2023-12-18 13:12:25 +08:00
}
if (index < expressions.length) {
const expr = expressions[index++];
const node = expr.node;
2024-01-16 21:26:16 +08:00
if (!_core.types.isStringLiteral(node, {
2023-12-18 13:12:25 +08:00
value: ""
})) {
nodes.push(node);
}
}
}
2024-01-16 21:26:16 +08:00
if (!_core.types.isStringLiteral(nodes[0]) && !(ignoreToPrimitiveHint && _core.types.isStringLiteral(nodes[1]))) {
nodes.unshift(_core.types.stringLiteral(""));
2023-12-18 13:12:25 +08:00
}
let root = nodes[0];
2024-01-16 21:26:16 +08:00
if (ignoreToPrimitiveHint) {
2023-12-18 13:12:25 +08:00
for (let i = 1; i < nodes.length; i++) {
2024-01-16 21:26:16 +08:00
root = _core.types.binaryExpression("+", root, nodes[i]);
2023-12-18 13:12:25 +08:00
}
} else if (nodes.length > 1) {
root = buildConcatCallExpressions(nodes);
}
path.replaceWith(root);
}
}
};
});
2024-01-16 21:26:16 +08:00
//# sourceMappingURL=index.js.map