Skip to content

Commit

Permalink
Implement mutableTemplateObject and ignoreToPrimitiveHint assumpt…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
nicolo-ribaudo committed Nov 27, 2020
1 parent 9b673e3 commit 0ec7b4b
Show file tree
Hide file tree
Showing 41 changed files with 56 additions and 5 deletions.
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",
"newableArrowFunctions",
"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 =
options.loose || api.assumption("ignoreToPrimitiveHint");
const mutableTemplateObject =
options.loose || api.assumption("mutableTemplateObject");

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);

0 comments on commit 0ec7b4b

Please sign in to comment.