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 the PR: Breaking Change 💥 A type of pull request used for our changelog categories for next major release label Mar 22, 2017
@hzoo hzoo added this to the Babel 7 milestone Mar 22, 2017
@codecov
Copy link

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) } })); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually didn't know this required defineProperty :o

Copy link
Member Author

@hzoo hzoo Mar 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

@loganfsmyth loganfsmyth Mar 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

@hzoo hzoo Mar 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k plz approve or thumb if ready 🙌

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep sorry forgot the name of the method

@hzoo hzoo force-pushed the template-literals-revision branch from eeb3e8f to b7aa909 Compare March 22, 2017 19:56
@hzoo
Copy link
Member Author

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

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 March 22, 2017 23:54
@ljharb
Copy link
Member

ljharb commented May 8, 2017

Any chance this could be backported to v6?

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

ljharb commented May 9, 2017

Relying on a tagged template literal throwing?

@loganfsmyth
Copy link
Member

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

@xtuc
Copy link
Member

xtuc commented May 9, 2017

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

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue 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.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Breaking Change 💥 A type of pull request used for our changelog categories for next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants