Skip to content

[Bug] Emitter prints TemplateExpression very wrong! #14948

@grandsong

Description

@grandsong

I found this bug when I parse some typescript code into AST (by sf = ts.createSourceFile ) and then ts.createPrinter().printFile(sf) back to typescript (not js) code again.

What's the Problem

You can reproduce this bug online with npm runkit.

Chrome is recommended.

Paste these code and click "Run".

var ts =  require("typescript")
var s = "let greeting = `Hi ${name}, how are you?`";
var sf = ts.createSourceFile('', s, ts.ScriptTarget.ES2017, true);
console.info(ts.createPrinter().printFile(sf));

The output is supposed to be the same as s. However, you will see:

"let greeting = \"Hi \"name\", how are you?\";\n"

The printed ts code for the TemplateExpression AST node is completed messed up .

Probably Helpful Info

I did my little investigation in typescript.js.

Finally, I reached the function emitLiteral where a TemplateHead node (such as that of "Hi ") is converted into a string text = getLiteralTextOfNode(node) with double quotes.

I am not sure what to edit to fix this bug. So I stopped and post this issue.

Hope someone kindly fix it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: APIRelates to the public API for TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions