Skip to content

Downlevel emit for template strings should use 'void 0' not 'undefined' #38430

Closed
@rbuckton

Description

@rbuckton

TypeScript Version: 3.9.x, 4.0.x-dev

Search Terms: template string emit

Code

From ~/tests/cases/conformance/es2018/invalidTaggedTemplateEscapeSequences.ts:

const x = tag`\u{hello} ${ 100 } \xtraordinary ${ 200 } wonderful ${ 300 } \uworld`;

Expected behavior:

ES5/ES3 emit:

const x = tag(__makeTemplateObject([void 0, void 0, " wonderful ", void 0], ["\\u{hello} ", " \\xtraordinary ", " wonderful ", " \\uworld"]), 100, 200, 300);

Actual behavior:

ES5/ES3 emit:

const x = tag(__makeTemplateObject([undefined, undefined, " wonderful ", undefined], ["\\u{hello} ", " \\xtraordinary ", " wonderful ", " \\uworld"]), 100, 200, 300);

In ES5, undefined is an identifier and not a keyword and can be redeclared. In ES3, undefined isn't declared. In general we emit void 0 in these cases.

This can be easily addressed by changing createTemplateCooked in ~/src/compiler/transformers/taggedTemplate.ts to return createVoidZero() instead of createIdentifier("undefined") when a template contains an invalid escape sequence..

Playground Link: link

Related Issues: #23801, #12700

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions