From 9fb8a80f6051a0724557eab2107a43717b3ddca3 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 15 Nov 2014 03:00:53 +1100 Subject: [PATCH] support `raw` property on tagged template literals - closes #164 --- lib/6to5/file.js | 3 ++- lib/6to5/templates/tagged-template-literal.js | 4 ++++ .../transformers/template-literals.js | 21 +++++++++++++------ .../template-literals/tag/actual.js | 2 +- .../template-literals/tag/expected.js | 10 ++++++++- 5 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 lib/6to5/templates/tagged-template-literal.js diff --git a/lib/6to5/file.js b/lib/6to5/file.js index a2d0806595..46d42045bf 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -18,7 +18,8 @@ function File(opts) { this.ast = {}; } -File.declarations = ["extends", "class-props", "slice", "apply-constructor"]; +File.declarations = ["extends", "class-props", "slice", "apply-constructor", + "tagged-template-literal"]; File.normaliseOptions = function (opts) { opts = _.cloneDeep(opts || {}); diff --git a/lib/6to5/templates/tagged-template-literal.js b/lib/6to5/templates/tagged-template-literal.js new file mode 100644 index 0000000000..9d3b26a3b7 --- /dev/null +++ b/lib/6to5/templates/tagged-template-literal.js @@ -0,0 +1,4 @@ +(function (strings, raw) { + return Object.defineProperties(strings, { raw: { value: raw } }); +}); + diff --git a/lib/6to5/transformation/transformers/template-literals.js b/lib/6to5/transformation/transformers/template-literals.js index e881434176..3e9654af11 100644 --- a/lib/6to5/transformation/transformers/template-literals.js +++ b/lib/6to5/transformation/transformers/template-literals.js @@ -1,18 +1,27 @@ -var t = require("../../types"); -var _ = require("lodash"); +var util = require("../../util"); +var t = require("../../types"); +var _ = require("lodash"); var buildBinaryExpression = function (left, right) { return t.binaryExpression("+", left, right); }; -exports.TaggedTemplateExpression = function (node) { +exports.TaggedTemplateExpression = function (node, parent, file) { var args = []; var quasi = node.quasi; - var strings = quasi.quasis.map(function (elem) { - return t.literal(elem.value.raw); + var strings = []; + var raw = []; + + _.each(quasi.quasis, function (elem) { + strings.push(t.literal(elem.value.cooked)); + raw.push(t.literal(elem.value.raw)); }); - args.push(t.arrayExpression(strings)); + + args.push(t.callExpression(file.addDeclaration("tagged-template-literal"), [ + t.arrayExpression(strings), + t.arrayExpression(raw) + ])); _.each(quasi.expressions, function (expr) { args.push(expr); diff --git a/test/fixtures/transformation/template-literals/tag/actual.js b/test/fixtures/transformation/template-literals/tag/actual.js index 34a227f571..7fe1864736 100644 --- a/test/fixtures/transformation/template-literals/tag/actual.js +++ b/test/fixtures/transformation/template-literals/tag/actual.js @@ -1 +1 @@ -var foo = bar`a${ 42 }b ${_.foobar()}`; +var foo = bar`wow\na${ 42 }b ${_.foobar()}`; diff --git a/test/fixtures/transformation/template-literals/tag/expected.js b/test/fixtures/transformation/template-literals/tag/expected.js index cff3164497..9992ad8f15 100644 --- a/test/fixtures/transformation/template-literals/tag/expected.js +++ b/test/fixtures/transformation/template-literals/tag/expected.js @@ -1,3 +1,11 @@ "use strict"; -var foo = bar(["a", "b ", ""], 42, _.foobar()); \ No newline at end of file +var _taggedTemplateLiteral = function (strings, raw) { + return Object.defineProperties(strings, { + raw: { + value: raw + } + }); +}; + +var foo = bar(_taggedTemplateLiteral(["wow\na", "b ", ""], ["wow\\na", "b ", ""]), 42, _.foobar());