Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing error when using await in non-async function #9799

Closed
anishkny opened this issue Nov 30, 2020 · 7 comments
Closed

Confusing error when using await in non-async function #9799

anishkny opened this issue Nov 30, 2020 · 7 comments
Labels
lang:javascript Issues affecting JS locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. scope:dependency Issues that cannot be solved inside Prettier itself, and must be fixed in a dependency type:enhancement A potential new feature to be added, or an improvement to how we print something

Comments

@anishkny
Copy link

Prettier 2.2.1
Playground link

--parser babel

Input:

function foo () {
  await bar();
}

Output:

SyntaxError: Unexpected token, expected ";" (2:9)
  1 | function foo () {
> 2 |   await bar();
    |         ^
  3 | }

Expected behavior:
Honestly, this error message makes no sense.

Perhaps a more relevant message would be:

SyntaxError: await is only valid in async function
@thorn0 thorn0 added lang:javascript Issues affecting JS type:enhancement A potential new feature to be added, or an improvement to how we print something labels Nov 30, 2020
@thorn0
Copy link
Member

thorn0 commented Nov 30, 2020

This comment might help with improving this: #9084 (comment)

@fisker
Copy link
Sponsor Member

fisker commented Dec 2, 2020

All parsers throw similar errors

espree SyntaxError: Unexpected token bar
meriyah [1:23]: Unexpected token: 'identifier'
flow Unexpected identifier, expected the end of an expression statement (`;`)

I think there is nothing we can do, unless parsers improve it.

@thorn0
Copy link
Member

thorn0 commented Dec 2, 2020

Specifically Babel can throw "'await' is only allowed within async functions", but it somehow trickily depends on its configuration.

@fisker
Copy link
Sponsor Member

fisker commented Dec 2, 2020

You are right

require('@babel/parser').parse('function foo () {await bar();}', {sourceType: 'module'})
Uncaught [SyntaxError: 'await' is only allowed within async functions (1:17)
] {
  loc: Position { line: 1, column: 17 },
  pos: 17
}

@fisker
Copy link
Sponsor Member

fisker commented Dec 2, 2020

require('@babel/parser').parse('function foo () {await bar();}', {sourceType: 'module', errorRecovery: true})
Uncaught [SyntaxError: Unexpected token, expected ";" (1:23)
] {
  loc: Position { line: 1, column: 23 },
  pos: 23
}

@thorn0 thorn0 added the scope:dependency Issues that cannot be solved inside Prettier itself, and must be fixed in a dependency label Dec 2, 2020
@fisker
Copy link
Sponsor Member

fisker commented Dec 2, 2020

Filed an issue on babel babel/babel#12436

@sosukesuzuki
Copy link
Member

Fixed by #10345

@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label May 27, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lang:javascript Issues affecting JS locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. scope:dependency Issues that cannot be solved inside Prettier itself, and must be fixed in a dependency type:enhancement A potential new feature to be added, or an improvement to how we print something
Projects
None yet
Development

No branches or pull requests

4 participants