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

Throw better error message when await is not used in async functions in errorRecovery mode #12436

Closed
1 task
fisker opened this issue Dec 2, 2020 · 3 comments · Fixed by #12520
Closed
1 task
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser

Comments

@fisker
Copy link
Contributor

fisker commented Dec 2, 2020

Bug Report

  • I would like to work on a fix!

Current behavior

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
}

Should throw an error with better message.

Input Code

function foo () {await bar();}

Expected behavior

Mabe like this, when errorRecovery: false

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
}

The error message is much better.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

Through API, without config

Environment

- Babel version(s): v7.12.7
- Node/npm version: Node 14/npm 6
- OS: Windows 10
- Monorepo: no
- How you are using Babel: api

Possible Solution

I have no idea

Additional context

Issue on Prettier prettier/prettier#9799 and prettier/prettier#9084

@babel-bot
Copy link
Collaborator

Hey @fisker! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Dec 2, 2020

When improving this error, we should be careful since

function foo () {
  await
  bar();
}

is valid code.

The reason for this "expected ;" is that await is a normal identifier: we finish parsing the expression statement (await) and then we expect either a newline or a semicolon before the following satement (bar();).

@sosukesuzuki
Copy link
Member

This happens for yield too.

require('@babel/parser').parse('function foo () { yield bar();}', {sourceType: 'module', errorRecovery: true })
SyntaxError: Unexpected token, expected ";" (1:24)

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Apr 25, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants