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

Account for template literals revision #5523

Merged
merged 4 commits into from Mar 22, 2017
Merged

Account for template literals revision #5523

merged 4 commits into from Mar 22, 2017

Conversation

@hzoo
Copy link
Member

@hzoo 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
Copy link

@codecov 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.

@Jessidhia

Jessidhia Mar 22, 2017
Member

I actually didn't know this required defineProperty :o

This comment has been minimized.

@hzoo

hzoo Mar 22, 2017
Author 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?

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?

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.

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?

This comment has been minimized.

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

This comment has been minimized.

@hzoo

hzoo Mar 22, 2017
Author 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
Author 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.

@@ -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()

This comment has been minimized.

@hzoo

hzoo Mar 22, 2017
Author Member

yep sorry forgot the name of the method

@hzoo hzoo force-pushed the template-literals-revision branch from eeb3e8f to b7aa909 Mar 22, 2017
@hzoo
Copy link
Member Author

@hzoo 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
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
Copy link
Member Author

@hzoo 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
Copy link
Member

@ljharb ljharb commented May 8, 2017

Any chance this could be backported to v6?

@xtuc
Copy link
Member

@xtuc 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
Copy link
Member

@ljharb ljharb commented May 9, 2017

Relying on a tagged template literal throwing?

@loganfsmyth
Copy link
Member

@loganfsmyth loganfsmyth commented May 9, 2017

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

@xtuc
Copy link
Member

@xtuc xtuc commented May 9, 2017

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

@lock lock bot added the outdated label Oct 6, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

7 participants
You can’t perform that action at this time.