Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement mutableTemplateObject and ignoreToPrimitiveHint #12408

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-core/src/config/validation/options.js
Expand Up @@ -332,6 +332,7 @@ type EnvPath = $ReadOnly<{
export type NestingPath = RootPath | OverridesPath | EnvPath;

export const assumptionsNames = new Set<string>([
"ignoreToPrimitiveHint",
"mutableTemplateObject",
"setPublicClassFields",
]);
Expand Down
16 changes: 11 additions & 5 deletions packages/babel-plugin-transform-template-literals/src/index.js
Expand Up @@ -3,10 +3,14 @@ import { template, types as t } from "@babel/core";

export default declare((api, options) => {
api.assertVersion(7);
const { loose } = options;

const ignoreToPrimitiveHint =
api.assumption("ignoreToPrimitiveHint") ?? options.loose;
const mutableTemplateObject =
api.assumption("mutableTemplateObject") ?? options.loose;

let helperName = "taggedTemplateLiteral";
if (loose) helperName += "Loose";
if (mutableTemplateObject) helperName += "Loose";

/**
* This function groups the objects into multiple calls to `.concat()` in
Expand Down Expand Up @@ -122,13 +126,15 @@ export default declare((api, options) => {

// since `+` is left-to-right associative
// ensure the first node is a string if first/second isn't
const considerSecondNode = !loose || !t.isStringLiteral(nodes[1]);
if (!t.isStringLiteral(nodes[0]) && considerSecondNode) {
if (
!t.isStringLiteral(nodes[0]) &&
!(ignoreToPrimitiveHint && t.isStringLiteral(nodes[1]))
) {
nodes.unshift(t.stringLiteral(""));
}
let root = nodes[0];

if (loose) {
if (ignoreToPrimitiveHint) {
for (let i = 1; i < nodes.length; i++) {
root = t.binaryExpression("+", root, nodes[i]);
}
Expand Down
@@ -0,0 +1,6 @@
{
"plugins": ["transform-template-literals"],
"assumptions": {
"ignoreToPrimitiveHint": true
}
}
@@ -0,0 +1 @@
tag`foo ${bar} baz`;
@@ -0,0 +1,13 @@
function _templateObject() {
const data = _taggedTemplateLiteral(["foo ", " baz"]);

_templateObject = function () {
return data;
};

return data;
}

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

tag(_templateObject(), bar);
@@ -0,0 +1 @@
`foo ${bar} baz`;
@@ -0,0 +1 @@
"foo ".concat(bar, " baz");
@@ -0,0 +1,6 @@
{
"plugins": ["transform-template-literals"],
"assumptions": {
"mutableTemplateObject": true
}
}
@@ -0,0 +1 @@
var o = `foo ${bar} baz`;
@@ -0,0 +1 @@
var o = "foo " + bar + " baz";
@@ -0,0 +1 @@
var o = tag`foo ${bar} baz`;
@@ -0,0 +1,13 @@
function _templateObject() {
const data = _taggedTemplateLiteralLoose(["foo ", " baz"]);

_templateObject = function () {
return data;
};

return data;
}

function _taggedTemplateLiteralLoose(strings, raw) { if (!raw) { raw = strings.slice(0); } strings.raw = raw; return strings; }

var o = tag(_templateObject(), bar);