Skip to content

removeComments: false generates incorrect code in specific caseΒ #45336

Closed
@SethDavenport

Description

@SethDavenport

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

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions