Skip to content

Call ToString for template literal placeholders #1838

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

Closed
wants to merge 1 commit into from

Conversation

arv
Copy link
Collaborator

@arv arv commented Mar 24, 2015

We were not calling ToString correctly for template literal
placeholder expressions. This lead to cases where we ended up with
calling valueOf instead as required by a + b.

Fixes #1836

We were not calling ToString correctly for template literal
placeholder expressions. This lead to cases where we ended up with
calling valueOf instead as required by a + b.

Fixes google#1836
@arv
Copy link
Collaborator Author

arv commented Mar 24, 2015

@caitp I got lazy and reused your tests.

@arv
Copy link
Collaborator Author

arv commented Mar 24, 2015

@johnjbarton PTAL

@@ -237,19 +233,7 @@ export class TemplateLiteralTransformer extends TempVarTransformer {
transformTemplateSubstitution(tree) {
let transformedTree = this.transformAny(tree.expression);
// Wrap in a paren expression if needed.
switch (transformedTree.type) {
case BINARY_EXPRESSION:
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not obvious from the description that this change is related.

@johnjbarton
Copy link
Contributor

LGTM

@@ -283,6 +267,9 @@ export class TemplateLiteralTransformer extends TempVarTransformer {
binaryExpression = binaryExpression.right;
}
let transformedTree = this.transformAny(tree.elements[i]);
if (element.type === TEMPLATE_SUBSTITUTION) {
transformedTree = parseExpression `String(${transformedTree})`;
Copy link
Contributor

Choose a reason for hiding this comment

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

String() is not really correct (won't throw on symbols) but I dunno if it matters much

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point.

Not sure what to do here. Doing a real ToString requires a typeof test. At this point I'm tempted to just not care about this and close this PR.

@pflannery
Copy link
Contributor

According to 5.1 spec regarding Addition Operator (+)

If Type(lprim) is String or Type(rprim) is String, then
Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim).

So ""+expr should be calling toString(). So I wonder if its a V8 issue?
So happens IE calls toString() when using ""+expr and it calls valueOf() when using expr+expr

thoughts?

@caitp
Copy link
Contributor

caitp commented Mar 25, 2015

thoughts?

Try "" + ({ toString: function() { return "A"; }, valueOf: function() { return "B"; } }) in any browser (and then try "" + String({ toString: function() { return "A"; }, valueOf: function() { return "B"; } }) for good measure)

@arv
Copy link
Collaborator Author

arv commented Mar 25, 2015

@pflannery "" + v first calls ToPrimitive(v) before calling ToString on the result of that.

@pflannery
Copy link
Contributor

Thanks. It looks like I got my Strings in a twist ;). I see now I misread the followed by ToString(**rprim**) part of the spec.

@arv
Copy link
Collaborator Author

arv commented Apr 3, 2015

Not going to do this one at the moment.

@arv arv closed this Apr 3, 2015
@arv arv removed the in progress label Apr 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Template literals are improperly being coerced
4 participants