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

Template string expression statement leading with object literal gives incorrect emit #4912

Closed
DanielRosenwasser opened this issue Sep 21, 2015 · 6 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this

Comments

@DanielRosenwasser
Copy link
Member

Something like

`${ { hello: 10; world: 20 } }HELLO`

will get emitted as

{ hello: 10; world: 20 } + "HELLO"

As an expresssion statement, the former evaluates to "[object Object]HELLO" while the latter evaluates to NaN.

This is because the former gets parsed as:

// block body
{
    hello: 10;
    world: 20;
};
"HELLO";
@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Sep 21, 2015
@DanielRosenwasser
Copy link
Member Author

Note this also applies to arrow functions, function expressions, and class expressions.

@mhegazy mhegazy added this to the Community milestone Sep 22, 2015
@mhegazy mhegazy added the Help Wanted You can do this label Sep 22, 2015
@weswigham
Copy link
Member

Interestingly, we also output extra parenthesis when doing a destructuring assignment within a template expression:

let x,y;
`${ {x,y} = {x: 2, y: 3} }HELLO`

emits as:

var x, y;
((_a = { x: 2, y: 3 }, x = _a.x, y = _a.y, _a)) + "HELLO";
var _a;

Additionally, the declaration of the temporary variable is placed after the expression - there's nothing expressly wrong with that, but it seems like bad form.

@DanielRosenwasser DanielRosenwasser removed the Help Wanted You can do this label Sep 22, 2015
@DanielRosenwasser
Copy link
Member Author

I might have a fix for this, so let's leave it assigned to me for now.

@DanielRosenwasser DanielRosenwasser added Good First Issue Well scoped, documented and has the green light Help Wanted You can do this labels Dec 28, 2015
@anirudhrb
Copy link

Is this still open? If yes, I would like to attempt a fix. :)

@kitsonk
Copy link
Contributor

kitsonk commented Sep 2, 2017

It doesn't appear to be.

`${{ hello: 10, world: 20 }}HELLO`;

...emits...

({ hello: 10, world: 20 } + "HELLO");

Both result in the same string of:

[object Object]HELLO

So ultimately this issue should be closed as the ES6+ behaviour matches the down-emit behaviour.

@j-oliveras
Copy link
Contributor

@DanielRosenwasser This can be closed, is solved since minimum September 2017.

`${{ hello: 10, world: 20 }}HELLO`;

`${() => 0}HELLO`;

`${function () { }}HELLO`;

`${class a { }}HELLO`;

is compiled to (comments are the result running it on firefox console):

({ hello: 10, world: 20 } + "HELLO"); // [object Object]HELLO

(function () { return 0; } + "HELLO"); // function () { return 0; }HELLO

(function () { } + "HELLO"); // function () { }HELLO

/** @class */ (function () {
    function a() {
    }
    return a;
}()) + "HELLO"; /* function a() {
    }HELLO */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

6 participants