forked from daren.hsu/line_push
update
This commit is contained in:
+22
-22
@@ -10,12 +10,13 @@ var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
||||
var _core = require("@babel/core");
|
||||
|
||||
var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
||||
var _api$assumption, _api$assumption2;
|
||||
|
||||
api.assertVersion(7);
|
||||
const {
|
||||
loose
|
||||
} = options;
|
||||
const ignoreToPrimitiveHint = (_api$assumption = api.assumption("ignoreToPrimitiveHint")) != null ? _api$assumption : options.loose;
|
||||
const mutableTemplateObject = (_api$assumption2 = api.assumption("mutableTemplateObject")) != null ? _api$assumption2 : options.loose;
|
||||
let helperName = "taggedTemplateLiteral";
|
||||
if (loose) helperName += "Loose";
|
||||
if (mutableTemplateObject) helperName += "Loose";
|
||||
|
||||
function buildConcatCallExpressions(items) {
|
||||
let avail = true;
|
||||
@@ -64,27 +65,28 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
||||
}
|
||||
}
|
||||
|
||||
const scope = path.scope.getProgramParent();
|
||||
const templateObject = scope.generateUidIdentifier("templateObject");
|
||||
const helperId = this.addHelper(helperName);
|
||||
const callExpressionInput = [_core.types.arrayExpression(strings)];
|
||||
const helperArgs = [_core.types.arrayExpression(strings)];
|
||||
|
||||
if (!isStringsRawEqual) {
|
||||
callExpressionInput.push(_core.types.arrayExpression(raws));
|
||||
helperArgs.push(_core.types.arrayExpression(raws));
|
||||
}
|
||||
|
||||
const lazyLoad = _core.template.ast`
|
||||
function ${templateObject}() {
|
||||
const data = ${_core.types.callExpression(helperId, callExpressionInput)};
|
||||
${templateObject} = function() { return data };
|
||||
return data;
|
||||
}
|
||||
`;
|
||||
scope.path.unshiftContainer("body", lazyLoad);
|
||||
path.replaceWith(_core.types.callExpression(node.tag, [_core.types.callExpression(_core.types.cloneNode(templateObject), []), ...quasi.expressions]));
|
||||
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]));
|
||||
},
|
||||
|
||||
TemplateLiteral(path) {
|
||||
if (path.parent.type === "TSLiteralType") {
|
||||
return;
|
||||
}
|
||||
|
||||
const nodes = [];
|
||||
const expressions = path.get("expressions");
|
||||
let index = 0;
|
||||
@@ -106,15 +108,13 @@ var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
||||
}
|
||||
}
|
||||
|
||||
const considerSecondNode = !loose || !_core.types.isStringLiteral(nodes[1]);
|
||||
|
||||
if (!_core.types.isStringLiteral(nodes[0]) && considerSecondNode) {
|
||||
if (!_core.types.isStringLiteral(nodes[0]) && !(ignoreToPrimitiveHint && _core.types.isStringLiteral(nodes[1]))) {
|
||||
nodes.unshift(_core.types.stringLiteral(""));
|
||||
}
|
||||
|
||||
let root = nodes[0];
|
||||
|
||||
if (loose) {
|
||||
if (ignoreToPrimitiveHint) {
|
||||
for (let i = 1; i < nodes.length; i++) {
|
||||
root = _core.types.binaryExpression("+", root, nodes[i]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user