Skip to content

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

Description

@fisker

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: parser

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions