Add allowYieldOutsideFunction to parser#17149
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/58824 |
| if (this.isContextual(tt._yield)) { | ||
| if (this.prodParam.hasYield) { | ||
| if ( | ||
| this.optionFlags & OptionFlags.AllowYieldOutsideFunction || |
There was a problem hiding this comment.
It should be chained with the !this.scope.inFunction check, after all we don't allow yield within a plain function.
There was a problem hiding this comment.
I just realize that the scope.inFunction check will omit the static block, which is essentially a plain function body and thus should be disallowed in my opinion. Since a var scope can only be one of Program, Function, Static Block and TSModule, I suggest we append the production parameter when parsing Program/TSModule according to AllowYieldOutsideFunction and AllowAwaitOutsideFunction, and then the check for every specific yield/await can be restored to only production parameters.
This approach should be potentially faster as a typical source has only one program node and a few TSModule blocks, but it may contain lots of yield/await expressions.
|
I'm a bit hesitant to add a |
|
Would you prefer to add |
|
I prefer the specific option flag. The Unlike // template option
require('babel-template')(`await [foo]`, { template: false })(foo: identifier("foo"))
// allowAwaitOutsideFunction option
require('babel-template')(`await [foo]`, { allowAwaitOutsideFunction: false })(foo: identifier("foo"))And because of that, |
This reverts commit 324e089.
nicolo-ribaudo
left a comment
There was a problem hiding this comment.
Note that await and yield have different parsing rules.
Can you test that we can properly parse this code without error?
async function fn() {
yield + 1; // this is a sum between "yield" and 1
await + 1; // this is awaiting "+1"
}| } | ||
|
|
||
| isAmbiguousAwait(): boolean { | ||
| isAmbiguousAst(): boolean { |
There was a problem hiding this comment.
Can we rename this to isAmbiguousPrefixOrIdentifier?
|
The Key observations:
I suggest considering context simulation through parser state flags rather than literal "allow outside function" semantics. Would appreciate other perspectives on this interpretation. |
|
@magic-akari |
|
@magic-akari I agree that the name is a bit unfortunate, ideally I would like some option like Note that in your example the |
nicolo-ribaudo
left a comment
There was a problem hiding this comment.
Good that this also improves error recovery.
|
@liuxingbaoyu Could you prepare the docs PR? |
* allowYieldOutsideFunction * dts * review * template * Revert "template" This reverts commit 324e089. * improve error * review
Uh oh!
There was an error while loading. Please reload this page.