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

ES2018 Feature: Async Iteration (for-await-of) #1990

Closed
5 tasks
JosephPecoraro opened this issue Aug 15, 2019 · 1 comment · Fixed by #2101
Closed
5 tasks

ES2018 Feature: Async Iteration (for-await-of) #1990

JosephPecoraro opened this issue Aug 15, 2019 · 1 comment · Fixed by #2101

Comments

@JosephPecoraro
Copy link
Contributor

JosephPecoraro commented Aug 15, 2019

Syntax:

A variation of the for-of iteration statement which iterates over async iterable objects (async generator). An example usage would be:

for await (const line of readLines(filePath)) {
  console.log(line);
}

async function* generator() { }

Async for-of statements are only allowed within async functions:

 IterationStatement[Yield, Await, Return]:
     ...
+    [+Await] for await ( [lookahead ≠ let] LeftHandSideExpression[?Yield, ?Await] of AssignmentExpression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return]
+    [+Await] for await ( var ForBinding[?Yield, ?Await] of AssignmentExpression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return]
+    [+Await] for await ( ForDeclaration[?Yield, ?Await] of AssignmentExpression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return]

And async generator functions / methods are introduced:

+ AsyncGeneratorMethod[Yield, Await]:
+     async *PropertyName[?Yield, ?Await] ( UniqueFormalParameters[+Yield, +Await] ) { AsyncGeneratorBody }
+ 
+ AsyncGeneratorDeclaration[Yield, Await, Default]:
+     async function *BindingIdentifier[?Yield, ?Await] ( FormalParameters[+Yield, +Await] ) { AsyncGeneratorBody }
+     async function * ( FormalParameters[+Yield, +Await] ) { AsyncGeneratorBody }
+ 
+ AsyncGeneratorExpression:
+     async function * BindingIdentifier[+Yield, +Await]? ( FormalParameters[+Yield, +Await] ) { AsyncGeneratorBody }
+ 
+ AsyncGeneratorBody:
+     FunctionBody[+Yield, +Await]

Spec:

TC39:
https://github.com/tc39/proposal-async-iteration
https://tc39.es/proposal-async-iteration/

ESTree:
https://github.com/estree/estree/blob/master/es2018.md


extend interface ForOfStatement {
  await: boolean;
}

Additional considerations

  • Supported in Chrome, Safari. (Firefox?)
  • Supported in Node, jsc

Remaining Tasks:

  • Update ForOfStatement node and docs (nodes.ts, docs/syntax-tree-format.md)
  • Update Parser for for-await-of (parser.ts in parseForStatement)
  • Update Parser for async generators (parser.ts allow for AsyncFunctionExpression, AsyncFunctionDeclaration which is a generator)
  • Provide Unit Tests and Coverage
  • Update test262 (~test262.git#2ee3864136) / test262-stream (~1.3.0)

Test Cases

Valid:

async function f() { for await (let x of []) {} }
async function *g() { for await (let x of []) {} }
o = { async method() { for await (let x of []) {} } }
o = { async *method() { for await (let x of []) {} } }

Invalid:

function f() { for await (let x of []) {} }
function *g() { for await (let x of []) {} }
o = { method() { for await (let x of []) {} } }
o = { *method() { for await (let x of []) {} } }
yury-s pushed a commit to yury-s/webkit-http that referenced this issue Aug 16, 2019
…eatures

https://bugs.webkit.org/show_bug.cgi?id=200796

Reviewed by Ross Kirsling.

Source/WebInspectorUI:

Use a fork of Esprima to support modern JavaScript language features
while going through the process to upstream support:

  ES2018 Feature: Async Iteration (for-await-of)
  jquery/esprima#1990

  ES2019 Feature: Numeric Separator
  jquery/esprima#1989

  ES2019 Feature: Optional catch binding
  jquery/esprima#1953

  ES2020 Feature: BigInt
  jquery/esprima#1988

ESTree compatible AST changes are summarized as:

  - CatchClause `param` property is now nullable
  - ForOfStatement now has a boolean `await` property
  - Literal can be a `"bigint"` type (works if the environment has BigInt or not)

The pretty printer only needed to have additional handling for `for-await-of`.

* UserInterface/External/Esprima/esprima.js:
New version. Typescript output expects a modern JavaScript environment
instead of just ES6.

* Tools/Formatting/index.html:
Update the formatting tool for easier use in case of errors.

* UserInterface/Models/ScriptSyntaxTree.js:
(WI.ScriptSyntaxTree.prototype._createInternalSyntaxTree):

* UserInterface/Test/TestHarness.js:
(TestHarness.prototype.passOrFail):
Convenience for pass/fail with the same message based on a condition.

* UserInterface/Workers/Formatter/EsprimaFormatter.js:
(EsprimaFormatter.prototype._handleTokenAtNode):
Ensure a space after `await` in `for await` syntax.

LayoutTests:

* inspector/formatting/resources/javascript-tests/classes-expected.js:
* inspector/formatting/resources/javascript-tests/classes.js:
* inspector/formatting/resources/javascript-tests/for-statements-expected.js:
* inspector/formatting/resources/javascript-tests/for-statements.js:
* inspector/formatting/resources/javascript-tests/generators-expected.js:
* inspector/formatting/resources/javascript-tests/generators.js:
* inspector/formatting/resources/javascript-tests/numbers-expected.js: Added.
* inspector/formatting/resources/javascript-tests/numbers.js: Added.
* inspector/formatting/resources/javascript-tests/try-catch-finally-statements-expected.js:
* inspector/formatting/resources/javascript-tests/try-catch-finally-statements.js:
* inspector/formatting/resources/javascript-tests/unary-binary-expressions-expected.js:
* inspector/formatting/resources/javascript-tests/unary-binary-expressions.js:
Test formatting of new JavaScript language features.

* inspector/formatting/formatting-css-expected.txt:
* inspector/formatting/formatting-javascript-expected.txt:
* inspector/formatting/formatting-javascript.html:
* inspector/formatting/resources/utilities.js:
(TestPage.registerInitializer.async.runFormattingTest):
(TestPage.registerInitializer.window.addFormattingTests):
(TestPage.registerInitializer):
Cleaner output and better handling for debugging failures.

* inspector/model/parse-script-syntax-tree.html:
Test new AST permutations.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@248760 268f45cc-cd09-0410-ab3c-d52691b4dbfc
@imsamurai
Copy link

any news?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants