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

Reuse same object for tagged template quasis. #67

Merged
merged 2 commits into from
Dec 6, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/program/Program.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export default function Program(source, ast, transforms, options) {
wrap((this.body = ast), this);
this.body.__proto__ = BlockStatement.prototype;

this.templateLiteralQuasis = Object.create(null);

this.indentExclusionElements = [];
this.body.initialise(transforms);

Expand Down
16 changes: 14 additions & 2 deletions src/program/types/TaggedTemplateExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,26 @@ export default class TaggedTemplateExpression extends Node {
.concat(this.quasi.quasis)
.sort((a, b) => a.start - b.start);

const program = this.program;
const rootScope = program.body.scope;

// insert strings at start
const templateStrings = this.quasi.quasis.map(quasi =>
JSON.stringify(quasi.value.cooked)
);
).join(', ');

let templateObject = this.program.templateLiteralQuasis[templateStrings];
if (!templateObject) {
templateObject = rootScope.createIdentifier('templateObject');
code.prependRight(0, `var ${templateObject} = Object.freeze([${templateStrings}]);\n`);

this.program.templateLiteralQuasis[templateStrings] = templateObject;
}

code.overwrite(
this.tag.end,
ordered[0].start,
`([${templateStrings.join(', ')}]`
`(${templateObject}`
);

let lastIndex = ordered[0].start;
Expand Down
11 changes: 9 additions & 2 deletions test/samples/template-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,22 @@ module.exports = [
'transpiles tagged template literals with `transforms.dangerousTaggedTemplateString = true`',
options: { transforms: { dangerousTaggedTemplateString: true } },
input: 'var str = x`y${(() => 42)()}`;',
output: `var str = x(["y", ""], (function () { return 42; })());`
output: `var templateObject = Object.freeze(["y", ""]);\nvar str = x(templateObject, (function () { return 42; })());`
},

{
description:
'transpiles tagged template literals with `transforms.dangerousTaggedTemplateString = true`',
options: { transforms: { dangerousTaggedTemplateString: true } },
input: 'var str = x`${(() => 42)()}y`;',
output: `var str = x(["", "y"], (function () { return 42; })());`
output: `var templateObject = Object.freeze(["", "y"]);\nvar str = x(templateObject, (function () { return 42; })());`
},

{
description: 'reuses quasi array for identical tagged template strings',
options: { transforms: { dangerousTaggedTemplateString: true } },
input: 'x`a${a}b`, x`a${b}b`, x`b${c}a`',
output: `var templateObject$1 = Object.freeze(["b", "a"]);\nvar templateObject = Object.freeze(["a", "b"]);\nx(templateObject, a), x(templateObject, b), x(templateObject$1, c)`
},

{
Expand Down