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
Improve output of for await
#15943
Open
liuxingbaoyu
wants to merge
3
commits into
babel:main
Choose a base branch
from
liuxingbaoyu:improve-for-await
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Improve output of for await
#15943
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 4 additions & 13 deletions
17
...-plugin-transform-async-generator-functions/test/fixtures/for-await/async-arrow/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,17 @@ | ||
/*#__PURE__*/babelHelpers.asyncToGenerator(function* () { | ||
var _iteratorAbruptCompletion = false; | ||
var _didIteratorError = false; | ||
var _iteratorError; | ||
var _step = {}; | ||
try { | ||
for (var _iterator = babelHelpers.asyncIterator(y), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false) { | ||
for (var _iterator = babelHelpers.asyncIterator(y); !(_step = yield _iterator.next()).done;) { | ||
let x = _step.value; | ||
{ | ||
f(x); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (_iteratorAbruptCompletion && _iterator.return != null) { | ||
if (!_step.done && _iterator.return != null) { | ||
yield _iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} catch (e) {} | ||
} | ||
}); |
17 changes: 4 additions & 13 deletions
17
...m-async-generator-functions/test/fixtures/for-await/async-function-no-transform/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,14 @@ | ||
async function foo() { | ||
var _iteratorAbruptCompletion = false; | ||
var _didIteratorError = false; | ||
var _iteratorError; | ||
var _step = {}; | ||
try { | ||
for (var _iterator = babelHelpers.asyncIterator(y), _step; _iteratorAbruptCompletion = !(_step = await _iterator.next()).done; _iteratorAbruptCompletion = false) { | ||
for (var _iterator = babelHelpers.asyncIterator(y); !(_step = await _iterator.next()).done;) { | ||
const x = _step.value; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (_iteratorAbruptCompletion && _iterator.return != null) { | ||
if (!_step.done && _iterator.return != null) { | ||
await _iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} catch (e) {} | ||
} | ||
} |
17 changes: 4 additions & 13 deletions
17
...ugin-transform-async-generator-functions/test/fixtures/for-await/async-function/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,20 @@ | ||
var _f; | ||
function f() { | ||
return (_f = _f || babelHelpers.asyncToGenerator(function* () { | ||
var _iteratorAbruptCompletion = false; | ||
var _didIteratorError = false; | ||
var _iteratorError; | ||
var _step = {}; | ||
try { | ||
for (var _iterator = babelHelpers.asyncIterator(y), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false) { | ||
for (var _iterator = babelHelpers.asyncIterator(y); !(_step = yield _iterator.next()).done;) { | ||
let x = _step.value; | ||
{ | ||
g(x); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (_iteratorAbruptCompletion && _iterator.return != null) { | ||
if (!_step.done && _iterator.return != null) { | ||
yield _iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} catch (e) {} | ||
} | ||
})).apply(this, arguments); | ||
} |
17 changes: 4 additions & 13 deletions
17
...gin-transform-async-generator-functions/test/fixtures/for-await/async-generator/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,20 @@ | ||
var _g; | ||
function g() { | ||
return (_g = _g || babelHelpers.wrapAsyncGenerator(function* () { | ||
var _iteratorAbruptCompletion = false; | ||
var _didIteratorError = false; | ||
var _iteratorError; | ||
var _step = {}; | ||
try { | ||
for (var _iterator = babelHelpers.asyncIterator(y), _step; _iteratorAbruptCompletion = !(_step = yield babelHelpers.awaitAsyncGenerator(_iterator.next())).done; _iteratorAbruptCompletion = false) { | ||
for (var _iterator = babelHelpers.asyncIterator(y); !(_step = yield babelHelpers.awaitAsyncGenerator(_iterator.next())).done;) { | ||
let x = _step.value; | ||
{ | ||
f(x); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (_iteratorAbruptCompletion && _iterator.return != null) { | ||
if (!_step.done && _iterator.return != null) { | ||
yield babelHelpers.awaitAsyncGenerator(_iterator.return()); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} catch (e) {} | ||
} | ||
})).apply(this, arguments); | ||
} |
17 changes: 4 additions & 13 deletions
17
...ync-generator-functions/test/fixtures/for-await/create-async-from-sync-iterator/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,22 @@ | ||
var _fn; | ||
function fn() { | ||
return (_fn = _fn || babelHelpers.wrapAsyncGenerator(function* () { | ||
var _iteratorAbruptCompletion = false; | ||
var _didIteratorError = false; | ||
var _iteratorError; | ||
var _step = {}; | ||
try { | ||
for (var _iterator = babelHelpers.asyncIterator([Promise.resolve("ok")]), _step; _iteratorAbruptCompletion = !(_step = yield babelHelpers.awaitAsyncGenerator(_iterator.next())).done; _iteratorAbruptCompletion = false) { | ||
for (var _iterator = babelHelpers.asyncIterator([Promise.resolve("ok")]); !(_step = yield babelHelpers.awaitAsyncGenerator(_iterator.next())).done;) { | ||
const result = _step.value; | ||
{ | ||
return { | ||
result | ||
}; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (_iteratorAbruptCompletion && _iterator.return != null) { | ||
if (!_step.done && _iterator.return != null) { | ||
yield babelHelpers.awaitAsyncGenerator(_iterator.return()); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} catch (e) {} | ||
} | ||
})).apply(this, arguments); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 4 additions & 13 deletions
17
...ansform-async-generator-functions/test/fixtures/for-await/lhs-member-expression/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,14 @@ | ||
babelHelpers.asyncToGenerator(function* () { | ||
var _iteratorAbruptCompletion = false; | ||
var _didIteratorError = false; | ||
var _iteratorError; | ||
var _step = {}; | ||
try { | ||
for (var _iterator = babelHelpers.asyncIterator(y), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false) { | ||
for (var _iterator = babelHelpers.asyncIterator(y); !(_step = yield _iterator.next()).done;) { | ||
obj.x = _step.value; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (_iteratorAbruptCompletion && _iterator.return != null) { | ||
if (!_step.done && _iterator.return != null) { | ||
yield _iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} catch (e) {} | ||
} | ||
})(); |
17 changes: 4 additions & 13 deletions
17
...m-async-generator-functions/test/fixtures/for-await/re-declare-var-in-init-body/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,17 @@ | ||
/*#__PURE__*/babelHelpers.asyncToGenerator(function* () { | ||
var _iteratorAbruptCompletion = false; | ||
var _didIteratorError = false; | ||
var _iteratorError; | ||
var _step = {}; | ||
try { | ||
for (var _iterator = babelHelpers.asyncIterator([]), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false) { | ||
for (var _iterator = babelHelpers.asyncIterator([]); !(_step = yield _iterator.next()).done;) { | ||
let x = _step.value; | ||
{ | ||
let x; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (_iteratorAbruptCompletion && _iterator.return != null) { | ||
if (!_step.done && _iterator.return != null) { | ||
yield _iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} catch (e) {} | ||
} | ||
}); |
25 changes: 25 additions & 0 deletions
25
...es/babel-plugin-transform-async-generator-functions/test/fixtures/for-await/throw/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
let returnCalled = false; | ||
|
||
let iterable = { | ||
[Symbol.asyncIterator || "@@asyncIterator"]() { | ||
return { | ||
next: () => ({ done: false, value: 1 }), | ||
return: () => { | ||
returnCalled = true; | ||
throw "return" | ||
} | ||
}; | ||
}, | ||
}; | ||
|
||
return async function () { | ||
let value; | ||
try { | ||
for await (value of iterable) { | ||
throw "error" | ||
} | ||
} catch (err) { | ||
expect(err).toBe("error"); | ||
} | ||
expect(returnCalled).toBe(true); | ||
}(); |
25 changes: 25 additions & 0 deletions
25
...s/babel-plugin-transform-async-generator-functions/test/fixtures/for-await/throw/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
let returnCalled = false; | ||
|
||
let iterable = { | ||
[Symbol.asyncIterator || "@@asyncIterator"]() { | ||
return { | ||
next: () => ({ done: false, value: 1 }), | ||
return: () => { | ||
returnCalled = true; | ||
throw "return" | ||
} | ||
}; | ||
}, | ||
}; | ||
|
||
return async function () { | ||
let value; | ||
try { | ||
for await (value of iterable) { | ||
throw "error" | ||
} | ||
} catch (err) { | ||
expect(err).toBe("error"); | ||
} | ||
expect(returnCalled).toBe(true); | ||
}(); |
5 changes: 5 additions & 0 deletions
5
...bel-plugin-transform-async-generator-functions/test/fixtures/for-await/throw/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"parserOpts": { | ||
"allowReturnOutsideFunction": true | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This accesses
.done
on the object twice, which is observable if it is a getter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do something like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to set
NOT_DONE_KEY
to false after the loop ends to avoid executingreturn()
when it throws innext()
.