Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

πŸ› Confusing error for await-expression inside non-async context #2479

Closed
1 task done
Boshen opened this issue Apr 21, 2022 · 5 comments
Closed
1 task done

πŸ› Confusing error for await-expression inside non-async context #2479

Boshen opened this issue Apr 21, 2022 · 5 comments
Labels
A-Parser Area: parser L-JavaScript Langauge: JavaScript S-Bug: confirmed Status: report has been confirmed as a valid bug

Comments

@Boshen
Copy link
Contributor

Boshen commented Apr 21, 2022

What happened?

In non-async context (Module),

function foo() {
  await bar;
}

await expression shows

error[SyntaxError]: Illegal use of `await` as an identifier inside of a module
  β”Œβ”€ main.js:2:3
  β”‚
2 β”‚   await bar;
  β”‚   ^^^^^

error[SyntaxError]: Expected a semicolon or an implicit semicolon after a statement, but found none
  β”Œβ”€ main.js:2:9
  β”‚
2 β”‚   await bar;
  β”‚   ------^^^
  β”‚   β”‚     β”‚
  β”‚   β”‚     An explicit or implicit semicolon is expected here...
  β”‚   ...Which is required to end this statement

which is super confusing, and the formatter will break it into two lines.

Expected result

acorn: Cannot use keyword 'await' outside an async function
babel: Unexpected reserved word 'await'.
swc: await isn't allowed in non-async function
typescript: 'await' expressions are only allowed within async functions and at the top levels of modules.

Playground

https://play.rome.tools/?lineWidth=80&indentStyle=tab&quoteStyle=double&indentWidth=2&typescript=false&jsx=false&sourceType=module#ZgB1AG4AYwB0AGkAbwBuACAAZgBvAG8AKAApACAAewAKACAAIABhAHcAYQBpAHQAIABiAGEAcgA7AAoAfQA=

Code of Conduct

  • I agree to follow Rome's Code of Conduct
@Boshen Boshen added the S-To triage Status: user report of a possible bug that needs to be triaged label Apr 21, 2022
@ematipico
Copy link
Contributor

Hi @Boshen , could you please be more specific about what would you expect from our parser?

Even though our parser shows two errors, the first one seems to be cover the illegal use of await.

@Boshen
Copy link
Contributor Author

Boshen commented Apr 22, 2022

I'm expecting a single error, and any mention of async context.

The two error messages are understandable from the spec perspective, but from user perspective it's rather confusing.

Also, the formatter breaks it into two statements just because we forgot to write async is also annoying in this case.

@ematipico ematipico added S-Bug: confirmed Status: report has been confirmed as a valid bug L-JavaScript Langauge: JavaScript A-Parser Area: parser and removed S-To triage Status: user report of a possible bug that needs to be triaged labels Apr 22, 2022
@ematipico
Copy link
Contributor

ematipico commented Apr 22, 2022

Thank you! That makes sense now.

This should be an easy fix, I think: https://github.com/rome/tools/blob/main/crates/rome_js_parser/src/syntax/expr.rs#L1377-L1380

@leops
Copy link
Contributor

leops commented Apr 22, 2022

I think the root cause of the issue is that await expression parsing is completely disabled in non-async context (here https://github.com/rome/tools/blob/main/crates/rome_js_parser/src/syntax/expr.rs#L1730) so await gets misinterpreted as an identifier expression, instead we should probably parse those expressions anyway but rewrite them as unknown expressions and emit a syntax error with a better explanation for the issue if they successfully parse but are not in an async context

@MichaReiser
Copy link
Contributor

Fixed in #3573

@ematipico ematipico added this to the 11.0.0 milestone Nov 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Parser Area: parser L-JavaScript Langauge: JavaScript S-Bug: confirmed Status: report has been confirmed as a valid bug
Projects
Status: Done
Development

No branches or pull requests

4 participants