Enable top-level await parsing by default#13387
Conversation
| { | ||
| "sourceType": "module" | ||
| "sourceType": "module", | ||
| "throws": "Unexpected token (1:6)" |
There was a problem hiding this comment.
The input code is await = foo();. We can potentially recover here I think, parsing await as an identifier.
| "start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}}, | ||
| "errors": [ | ||
| "SyntaxError: Unexpected reserved word 'await'. (1:6)" | ||
| "SyntaxError: Can not use 'await' as identifier inside an async function. (1:6)" |
There was a problem hiding this comment.
The input code is const await = foo();. As reported on twitter by @KFlash, this error message is confusing (https://twitter.com/buzyescayadev/status/1398240006670454784).
I will open a follow-up PR to fix it, to keep this one as only switching the plugin on by default.
There was a problem hiding this comment.
We could just change the error message to "... async function or top level module".
There was a problem hiding this comment.
@JLHwung then what will be the error message when you are parsing in an non-async func body or arrow? Same error?
None of this is consistent with the error message thrown when e.g. parsing out a formal param list with await.
await = x is an unexpected token? Where is that token? Between 1 - 6 there are 3x token and 2x whitespace
function x () { await } trigger an different error message again.
There was a problem hiding this comment.
The token is the one at line 1, column 6: any editor, or the Babel cli, or Babel integrations will point to the =.
There was a problem hiding this comment.
Maybe we can always throw Unexpected reserved word 'await'. in modules (since the reason we throw is that it's reserved), and keep throwing the current error in async functions inside scripts (await is not reserved there, but it's special-cased to be illegal).
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/47735/ |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 62473e4:
|
330acd3 to
8dbbbae
Compare
| exportStarFromEmptyModule.ts | ||
| exportStarNotElided.ts | ||
| expressionsForbiddenInParameterInitializers.ts | ||
| extendedUnicodePlaneIdentifiers.ts |
| decorators/migrated_0003.js | ||
| export_import_reserved_words/migrated_0003.js | ||
| export_statements/export_trailing_comma.js | ||
| for_await_loops/migrated_0000.js |
There was a problem hiding this comment.
For reference, this file now parses successfully due to top level await,
for await (let x of e) {}and we assume all the Flow parser tests are running with module source type.
| JSX_invalid/migrated_0000.js | ||
| arrow_function/object_return_type.js | ||
| arrow_function_invalid/migrated_0002.js | ||
| async_arrow_functions/with_type_parameters_types_disabled.js |
There was a problem hiding this comment.
This test is added because Flow has a new test options: types: boolean that can control whether parameter types should be parsed:
We don't have such an option.
There was a problem hiding this comment.
From a parsing perspective, is it the same as all: true?
| comment_interning/class_method.js | ||
| comment_interning/class_property.js | ||
| comment_interning/declare_export_declaration.js | ||
| comment_interning/declare_interface.js | ||
| comment_interning/declare_type_alias.js | ||
| comment_interning/decorator.js | ||
| comment_interning/function_declaration.js | ||
| comment_interning/import.js | ||
| comment_interning/interface.js | ||
| comment_interning/object_type.js | ||
| comment_interning/remove_type_trailing_comments.js | ||
| comment_interning/super.js | ||
| comment_interning/type_alias.js |
There was a problem hiding this comment.
Babel is throwing on these cases because of duplicate class declaration class C. The Flow parser is tolerant on duplicate binding, which are throw later by the type checker.
a4df67e to
28cd2c8
Compare
28cd2c8 to
d92d4cd
Compare
| startPos, | ||
| this.hasPlugin("topLevelAwait") | ||
| ? Errors.AwaitNotInAsyncContext | ||
| : Errors.AwaitNotInAsyncFunction, |
There was a problem hiding this comment.
We can remove unused Errors.AwaitNotInAsyncFunction.
d92d4cd to
cfe17ab
Compare
cfe17ab to
62473e4
Compare
|
does this mean i no longer need |
|
Correct! But you need to make sure to use |
|
i'm only using here is the related part of my config: parser: "@babel/eslint-parser",
parserOptions: {
requireConfigFile: false,
sourceType: "module",
babelOptions: {
plugins: [
"@babel/plugin-syntax-top-level-await"
],
},
}can I just remove |
|
Yes, it should work. |
Uh oh!
There was an error while loading. Please reload this page.