Account for template literals revision #5523

Merged
merged 4 commits into from Mar 22, 2017

Conversation

Projects
None yet
7 participants
@hzoo
Member

hzoo commented Mar 22, 2017

Input

tag`\unicode and \u{55}`;

Output

function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
var _templateObject = _taggedTemplateLiteral([], ["\\unicode and \\u{55}"]);
tag(_templateObject);

Fixes #4798

  • need to add more tests
  • also release a version of babylon that removes the need for the plugin since it's stage 4

cc @bakkot @TimothyGu

@hzoo hzoo added this to the Babel 7 milestone Mar 22, 2017

@codecov

This comment has been minimized.

Show comment
Hide comment
@codecov

codecov bot Mar 22, 2017

Codecov Report

Merging #5523 into 7.0 will increase coverage by <.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##              7.0    #5523      +/-   ##
==========================================
+ Coverage   85.38%   85.39%   +<.01%     
==========================================
  Files         200      200              
  Lines        9492     9495       +3     
  Branches     2696     2698       +2     
==========================================
+ Hits         8105     8108       +3     
  Misses        889      889              
  Partials      498      498
Impacted Files Coverage Δ
...in-transform-es2015-template-literals/src/index.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 492ee95...b7aa909. Read the comment docs.

codecov bot commented Mar 22, 2017

Codecov Report

Merging #5523 into 7.0 will increase coverage by <.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##              7.0    #5523      +/-   ##
==========================================
+ Coverage   85.38%   85.39%   +<.01%     
==========================================
  Files         200      200              
  Lines        9492     9495       +3     
  Branches     2696     2698       +2     
==========================================
+ Hits         8105     8108       +3     
  Misses        889      889              
  Partials      498      498
Impacted Files Coverage Δ
...in-transform-es2015-template-literals/src/index.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 492ee95...b7aa909. Read the comment docs.

+ _templateObject6 = _taggedTemplateLiteral(["left", "right"], ["left", "\\u000g", "right"]),
+ _templateObject7 = _taggedTemplateLiteral(["left", "right"], ["left", "\\u{-0}", "right"]);
+
+function _taggedTemplateLiteral(strings, raw) { return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

This comment has been minimized.

@Kovensky

Kovensky Mar 22, 2017

Member

I actually didn't know this required defineProperty :o

@Kovensky

Kovensky Mar 22, 2017

Member

I actually didn't know this required defineProperty :o

This comment has been minimized.

@hzoo

hzoo Mar 22, 2017

Member

i didn't either - maybe this..
right this is the non-loose mode one.

function _taggedTemplateLiteralLoose(strings, raw) { strings.raw = raw; return strings; }

@hzoo

hzoo Mar 22, 2017

Member

i didn't either - maybe this..
right this is the non-loose mode one.

function _taggedTemplateLiteralLoose(strings, raw) { strings.raw = raw; return strings; }

@@ -18,7 +18,9 @@ export default function ({ types: t }) {
let raw = [];
for (const elem of (quasi.quasis: Array)) {
- strings.push(t.stringLiteral(elem.value.cooked));
+ if (elem.value.cooked !== null) {

This comment has been minimized.

@loganfsmyth

loganfsmyth Mar 22, 2017

Member

Should this be left out, or explicitly be undefined/void 0 in the array?

@loganfsmyth

loganfsmyth Mar 22, 2017

Member

Should this be left out, or explicitly be undefined/void 0 in the array?

This comment has been minimized.

@loganfsmyth

loganfsmyth Mar 22, 2017

Member

Not sure what's supposed to happen for .length of strs or if there is danger that one of several pieces might be missing cooked, like what is the output for \unicode${45}\u0065, are both pieces missing the .cooked?

@loganfsmyth

loganfsmyth Mar 22, 2017

Member

Not sure what's supposed to happen for .length of strs or if there is danger that one of several pieces might be missing cooked, like what is the output for \unicode${45}\u0065, are both pieces missing the .cooked?

This comment has been minimized.

@bakkot

bakkot Mar 22, 2017

Contributor

It should be present with value undefined. Length is still the same as that of .raw.

@bakkot

bakkot Mar 22, 2017

Contributor

It should be present with value undefined. Length is still the same as that of .raw.

This comment has been minimized.

@loganfsmyth

loganfsmyth Mar 22, 2017

Member

Cool. Is every cooked value excluded if one is, or is it each individual value either may or may not use invalid escapes independent of the others?

@loganfsmyth

loganfsmyth Mar 22, 2017

Member

Cool. Is every cooked value excluded if one is, or is it each individual value either may or may not use invalid escapes independent of the others?

This comment has been minimized.

@bakkot

bakkot Mar 22, 2017

Contributor

They're independent. (That's how Babylon constructs the AST, too.)

@bakkot

bakkot Mar 22, 2017

Contributor

They're independent. (That's how Babylon constructs the AST, too.)

This comment has been minimized.

@loganfsmyth

loganfsmyth Mar 22, 2017

Member

Cool, wanted to verify my understanding then. So @hzoo this should do

const value = elem.value.cooked == null
  ? t.unaryExpression("void", t.numericLiteral(0))
  : t.stringLiteral(elem.value.cooked);
strings.push(value);
@loganfsmyth

loganfsmyth Mar 22, 2017

Member

Cool, wanted to verify my understanding then. So @hzoo this should do

const value = elem.value.cooked == null
  ? t.unaryExpression("void", t.numericLiteral(0))
  : t.stringLiteral(elem.value.cooked);
strings.push(value);

This comment has been minimized.

@hzoo

hzoo Mar 22, 2017

Member

what about just t.identifier("undefined")? turns into void 0 already I thought

-var _templateObject = _taggedTemplateLiteral([undefined], ["\\unicode and \\u{55}"]),
-    _templateObject2 = _taggedTemplateLiteral([undefined], ["\\01"]),
-    _templateObject3 = _taggedTemplateLiteral([undefined, "right"], ["\\xg", "right"]),
-    _templateObject4 = _taggedTemplateLiteral(["left", undefined], ["left", "\\xg"]),
-    _templateObject5 = _taggedTemplateLiteral(["left", undefined, "right"], ["left", "\\xg", "right"]),
-    _templateObject6 = _taggedTemplateLiteral(["left", undefined, "right"], ["left", "\\u000g", "right"]),
-    _templateObject7 = _taggedTemplateLiteral(["left", undefined, "right"], ["left", "\\u{-0}", "right"]);
+var _templateObject = _taggedTemplateLiteral([], ["\\unicode and \\u{55}"]),
+    _templateObject2 = _taggedTemplateLiteral([], ["\\01"]),
+    _templateObject3 = _taggedTemplateLiteral(["right"], ["\\xg", "right"]),
+    _templateObject4 = _taggedTemplateLiteral(["left"], ["left", "\\xg"]),
+    _templateObject5 = _taggedTemplateLiteral(["left", "right"], ["left", "\\xg", "right"]),
+    _templateObject6 = _taggedTemplateLiteral(["left", "right"], ["left", "\\u000g", "right"]),
+    _templateObject7 = _taggedTemplateLiteral(["left", "right"], ["left", "\\u{-0}", "right"]);
@hzoo

hzoo Mar 22, 2017

Member

what about just t.identifier("undefined")? turns into void 0 already I thought

-var _templateObject = _taggedTemplateLiteral([undefined], ["\\unicode and \\u{55}"]),
-    _templateObject2 = _taggedTemplateLiteral([undefined], ["\\01"]),
-    _templateObject3 = _taggedTemplateLiteral([undefined, "right"], ["\\xg", "right"]),
-    _templateObject4 = _taggedTemplateLiteral(["left", undefined], ["left", "\\xg"]),
-    _templateObject5 = _taggedTemplateLiteral(["left", undefined, "right"], ["left", "\\xg", "right"]),
-    _templateObject6 = _taggedTemplateLiteral(["left", undefined, "right"], ["left", "\\u000g", "right"]),
-    _templateObject7 = _taggedTemplateLiteral(["left", undefined, "right"], ["left", "\\u{-0}", "right"]);
+var _templateObject = _taggedTemplateLiteral([], ["\\unicode and \\u{55}"]),
+    _templateObject2 = _taggedTemplateLiteral([], ["\\01"]),
+    _templateObject3 = _taggedTemplateLiteral(["right"], ["\\xg", "right"]),
+    _templateObject4 = _taggedTemplateLiteral(["left"], ["left", "\\xg"]),
+    _templateObject5 = _taggedTemplateLiteral(["left", "right"], ["left", "\\xg", "right"]),
+    _templateObject6 = _taggedTemplateLiteral(["left", "right"], ["left", "\\u000g", "right"]),
+    _templateObject7 = _taggedTemplateLiteral(["left", "right"], ["left", "\\u{-0}", "right"]);

This comment has been minimized.

@hzoo

hzoo Mar 22, 2017

Member

k plz approve or thumb if ready 🙌

@hzoo

hzoo Mar 22, 2017

Member

k plz approve or thumb if ready 🙌

This comment has been minimized.

@bakkot

bakkot Mar 22, 2017

Contributor

@hzoo var undefined = 0 works at least in sloppy mode, so you do need `void 0 or equivalent. Otherwise lgtm.

@bakkot

bakkot Mar 22, 2017

Contributor

@hzoo var undefined = 0 works at least in sloppy mode, so you do need `void 0 or equivalent. Otherwise lgtm.

@@ -18,7 +18,10 @@ export default function ({ types: t }) {
let raw = [];
for (const elem of (quasi.quasis: Array)) {
- strings.push(t.stringLiteral(elem.value.cooked));
+ const value = elem.value.cooked == null
+ ? t.identifier("undefined")

This comment has been minimized.

@loganfsmyth

loganfsmyth Mar 22, 2017

Member

There could be a var undefined = 4 in the top scope of the module so it would break. You could also use path.scope.buildUndefinedNode()

@loganfsmyth

loganfsmyth Mar 22, 2017

Member

There could be a var undefined = 4 in the top scope of the module so it would break. You could also use path.scope.buildUndefinedNode()

This comment has been minimized.

@hzoo

hzoo Mar 22, 2017

Member

yep sorry forgot the name of the method

@hzoo

hzoo Mar 22, 2017

Member

yep sorry forgot the name of the method

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Mar 22, 2017

Member

changed to use path.scope.buildUndefinedNode() (although hasBinding has issues)

also added tests for spec/loose mode (no differences really)

Member

hzoo commented Mar 22, 2017

changed to use path.scope.buildUndefinedNode() (although hasBinding has issues)

also added tests for spec/loose mode (no differences really)

@hzoo hzoo merged commit b638c8b into 7.0 Mar 22, 2017

2 of 3 checks passed

continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
ci/circleci Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@hzoo hzoo referenced this pull request in babel/website Mar 22, 2017

Closed

Add to Upgrade Guide for Babel 7 #1209

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Mar 22, 2017

Member

btw @bakkot do you want to be a collaborator? (messaged you on slack)

Member

hzoo commented Mar 22, 2017

btw @bakkot do you want to be a collaborator? (messaged you on slack)

@hzoo hzoo deleted the template-literals-revision branch Mar 22, 2017

@ljharb

This comment has been minimized.

Show comment
Hide comment
@ljharb

ljharb May 8, 2017

Any chance this could be backported to v6?

ljharb commented May 8, 2017

Any chance this could be backported to v6?

@xtuc

This comment has been minimized.

Show comment
Hide comment
@xtuc

xtuc May 9, 2017

Member

Yes this is a bug fix but also a breaking changes for people relying on this (don't want to know why 😄), it also require Babylon to backport it.

I don't think we want to land this on 6.

Member

xtuc commented May 9, 2017

Yes this is a bug fix but also a breaking changes for people relying on this (don't want to know why 😄), it also require Babylon to backport it.

I don't think we want to land this on 6.

@ljharb

This comment has been minimized.

Show comment
Hide comment
@ljharb

ljharb May 9, 2017

Relying on a tagged template literal throwing?

ljharb commented May 9, 2017

Relying on a tagged template literal throwing?

@loganfsmyth

This comment has been minimized.

Show comment
Hide comment
@loganfsmyth

loganfsmyth May 9, 2017

Member

I think this+babylon seems backportable. Probably a good issue to file as a help-wanted?

Member

loganfsmyth commented May 9, 2017

I think this+babylon seems backportable. Probably a good issue to file as a help-wanted?

@xtuc

This comment has been minimized.

Show comment
Hide comment
@xtuc

xtuc May 9, 2017

Member

As discussed on Slack, i'm fine landing this in 6.

Member

xtuc commented May 9, 2017

As discussed on Slack, i'm fine landing this in 6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment