Skip to content

Invalid code generation for top-level await with newline (expression duplicated) #39186

Closed
@evanw

Description

@evanw

TypeScript Version: Nightly

Search Terms: top-level await newline duplicate duplicated

Code

export let foo = () => {
  console.log('called')
  return Promise.resolve(123)
}
export let bar = await
  foo()

Expected behavior:

The generated code should only call foo() once.

Actual behavior:

The generated code calls foo() twice:

export let foo = () => {
    console.log('called');
    return Promise.resolve(123);
};
export let bar = await foo();
foo();

This may lead to subtle correctness bugs. This only happens when there's a newline between the await keyword and the awaited expression. This also only happens for top-level await. Enclosing the await in an async function makes the bug disappear.

Playground Link: link

Related Issues:

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions