Skip to content

removeComments: false generates incorrect code in specific case #45336

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
SethDavenport opened this issue Aug 5, 2021 · 0 comments Β· Fixed by #46287
Closed

removeComments: false generates incorrect code in specific case #45336

SethDavenport opened this issue Aug 5, 2021 · 0 comments Β· Fixed by #46287
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@SethDavenport
Copy link

SethDavenport commented Aug 5, 2021

Bug Report

πŸ”Ž Search Terms

comments, removeComments, implicit semicolon

πŸ•— Version & Regression Information

I first noticed this in version 4.2.4. I have not tried earlier versions. It is still happening as of 4.3.5.

⏯ Playground Link

https://www.typescriptlang.org/play?target=1#code/FAYw9gdgzgLgBAMzGOBeOBGADFg3MUSWOAcwFMYAJMgSxIAt50AKASjQD5g44ADAEgDe3HnAD0YuAEE4AGzIQSMegE844ALYaFMETyRgRAXwAOAD164gA

πŸ’» Code

When removeComments: false and target: es5, the following code:

const foo = 100;

const getHeight = () =>
  `${
    // A lengthy comment
    foo
  }px`;

generates this output

"use strict";
var foo = 100;
var getHeight = function () {
    return 
    // A lengthy comment
    foo + "px";
};

πŸ™ Actual behavior

The outputted code is incorrect due to the 'implicit semicolon' immediately after the return. Instead of returning "100px" the generated getHeight function returns undefined and the foo + "px"; statement is unreachable code.

πŸ™‚ Expected behavior

I would instead expect the output to be more like this (note additional parentheses).

"use strict";
var foo = 100;
var getHeight = function () {
    return (
    // A lengthy comment
    foo + "px");
};
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants