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

Fix await binding error within static block #13088

Merged
merged 4 commits into from Jul 16, 2021

Conversation

@JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Apr 1, 2021

Q                       A
Fixed Issues? Implements tc39/proposal-class-static-block#46
Tests Added + Pass? Yes
License MIT

This PR aligns Babel behaviour to current V8 implementation, which follows Contains Static Semantics. However since this behaviour is not specified in that way, we have ongoing discussions in tc39/proposal-class-static-block#43

I will mark this PR as ready when consensus is reached.

This PR implements tc39/proposal-class-static-block#46. The spec now adds [Await] production parameter to static block and throw if it contains (specified in SDO containsAwait) AwaitExpression and for await. However implementing the SDO containsAwait requires scan of whole AST tree, which is not performant. Babel parses the StaticBlock in [~Await] and uses parser scopes to throw if an await identifier is within the static block.

See scope#inStaticBlock for current implementation detail.

@babel-bot
Copy link
Collaborator

@babel-bot babel-bot commented Apr 1, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/47370/

@codesandbox
Copy link

@codesandbox codesandbox bot commented Apr 1, 2021

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 0ce8c72:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@JLHwung JLHwung force-pushed the fix-static-block-await branch from ce0c08f to ebfc550 Apr 2, 2021
@JLHwung JLHwung force-pushed the fix-static-block-await branch from ebfc550 to 3886970 May 14, 2021
@JLHwung JLHwung force-pushed the fix-static-block-await branch from 3886970 to c9155bd Jun 9, 2021
@JLHwung JLHwung marked this pull request as ready for review Jun 9, 2021
@JLHwung JLHwung force-pushed the fix-static-block-await branch 3 times, most recently from 0aeebe9 to c116d2f Jun 9, 2021
@JLHwung JLHwung requested a review from nicolo-ribaudo Jun 9, 2021
@KFlash
Copy link

@KFlash KFlash commented Jun 13, 2021

@JLHwung Why not set 'this.scope.inStaticBlock' when you enter a static block, and unset this again every time you enter function body, arrow or block statement, and then for await check if this is set and if so, throw.

@JLHwung
Copy link
Contributor Author

@JLHwung JLHwung commented Jun 14, 2021

@KFlash Besides function body, arrow, we should also take care of the class field initializers. A plain block statement { expr } inherit the await production, so we should not reset for plain block. Essentially it should be maintained in whatever production that does not inherit [Await], which is easily overlooked when new language feature is introduced. A boolean state has fastest access time, however such state inStaticBlock will only be accessed when we see a binding identifier named await, which is rare.

I think the current approach is easier to maintain because we already have to push a lexical scope for static block, so an extra mark on the scope, compacted to bit arrays, does not introduce extra recording tasks. The worst scenario of current approach is an await nested in N block statements within a static block, while the boolean approach certainly performs better in this scenario.

@KFlash
Copy link

@KFlash KFlash commented Jun 14, 2021

@JLHwung An easy solution for this is to "copy" what the author of this proposal did. Look at the open PR for implementation of static block in the Typescript repo.

@JLHwung JLHwung added this to To review in Nicolò's ideal PR review order list via automation Jun 24, 2021
@JLHwung JLHwung force-pushed the fix-static-block-await branch from d5b9109 to 682682c Jul 16, 2021
@JLHwung JLHwung force-pushed the fix-static-block-await branch from 7122dfd to 0ce8c72 Jul 16, 2021
@JLHwung JLHwung merged commit 6e57617 into babel:main Jul 16, 2021
24 of 27 checks passed
Nicolò's ideal PR review order list automation moved this from To review to Done Jul 16, 2021
@JLHwung JLHwung deleted the fix-static-block-await branch Jul 16, 2021
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this issue Jul 30, 2021
* fix: allow await within SCOPE_FUNCTION under static block

* perf: scan scopeStack for once

* add new test case

* chore: update allowlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants