-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
base: main
Are you sure you want to change the base?
Improve output of for await
#15943
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,31 +1,21 @@ | ||||||
import { types as t, template } from "@babel/core"; | ||||||
import type { NodePath } from "@babel/traverse"; | ||||||
|
||||||
const buildForAwait = template(` | ||||||
async function wrapper() { | ||||||
var ITERATOR_ABRUPT_COMPLETION = false; | ||||||
var ITERATOR_HAD_ERROR_KEY = false; | ||||||
var ITERATOR_ERROR_KEY; | ||||||
const buildForAwait = template.statements(` | ||||||
var ITERATOR_KEY = GET_ITERATOR(OBJECT), STEP_KEY = {}, NOT_DONE_KEY; | ||||||
try { | ||||||
for (;NOT_DONE_KEY = !(STEP_KEY = await ITERATOR_KEY.next()).done;NOT_DONE_KEY = false) { | ||||||
} | ||||||
} catch(e) { | ||||||
STEP_KEY = null; | ||||||
throw e; | ||||||
} finally { | ||||||
try { | ||||||
for ( | ||||||
var ITERATOR_KEY = GET_ITERATOR(OBJECT), STEP_KEY; | ||||||
ITERATOR_ABRUPT_COMPLETION = !(STEP_KEY = await ITERATOR_KEY.next()).done; | ||||||
ITERATOR_ABRUPT_COMPLETION = false | ||||||
) { | ||||||
} | ||||||
} catch (err) { | ||||||
ITERATOR_HAD_ERROR_KEY = true; | ||||||
ITERATOR_ERROR_KEY = err; | ||||||
} finally { | ||||||
try { | ||||||
if (ITERATOR_ABRUPT_COMPLETION && ITERATOR_KEY.return != null) { | ||||||
await ITERATOR_KEY.return(); | ||||||
} | ||||||
} finally { | ||||||
if (ITERATOR_HAD_ERROR_KEY) { | ||||||
throw ITERATOR_ERROR_KEY; | ||||||
} | ||||||
if (NOT_DONE_KEY && ITERATOR_KEY.return) { | ||||||
await ITERATOR_KEY.return(); | ||||||
} | ||||||
} catch (e) { | ||||||
if (STEP_KEY) throw e; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Could you also rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it's worth doing, it's really weird usage. :)
Since I completely changed the position of variable declarations and the logic of throwing exceptions, the changes may not be less, |
||||||
} | ||||||
} | ||||||
`); | ||||||
|
@@ -52,24 +42,16 @@ export default function ( | |||||
t.variableDeclarator(left.declarations[0].id, stepValue), | ||||||
]); | ||||||
} | ||||||
let template = buildForAwait({ | ||||||
ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"), | ||||||
ITERATOR_ABRUPT_COMPLETION: scope.generateUidIdentifier( | ||||||
"iteratorAbruptCompletion", | ||||||
), | ||||||
ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"), | ||||||
const template = buildForAwait({ | ||||||
ITERATOR_KEY: scope.generateUidIdentifier("iterator"), | ||||||
NOT_DONE_KEY: scope.generateUidIdentifier("notDone"), | ||||||
GET_ITERATOR: getAsyncIterator, | ||||||
OBJECT: node.right, | ||||||
STEP_KEY: t.cloneNode(stepKey), | ||||||
}); | ||||||
|
||||||
// remove async function wrapper | ||||||
// @ts-expect-error todo(flow->ts) improve type annotation for buildForAwait | ||||||
template = template.body.body as t.Statement[]; | ||||||
|
||||||
const isLabeledParent = t.isLabeledStatement(parent); | ||||||
const tryBody = (template[3] as t.TryStatement).block.body; | ||||||
const tryBody = (template[1] as t.TryStatement).block.body; | ||||||
const loop = tryBody[0] as t.ForStatement; | ||||||
|
||||||
if (isLabeledParent) { | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
/*#__PURE__*/babelHelpers.asyncToGenerator(function* () { | ||
var _iteratorAbruptCompletion = false; | ||
var _didIteratorError = false; | ||
var _iteratorError; | ||
var _iterator = babelHelpers.asyncIterator(y), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If calling |
||
_step = {}, | ||
_notDone; | ||
try { | ||
for (var _iterator = babelHelpers.asyncIterator(y), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false) { | ||
for (; _notDone = !(_step = yield _iterator.next()).done; _notDone = false) { | ||
let x = _step.value; | ||
{ | ||
f(x); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} catch (e) { | ||
_step = null; | ||
throw e; | ||
} finally { | ||
try { | ||
if (_iteratorAbruptCompletion && _iterator.return != null) { | ||
if (_notDone && _iterator.return) { | ||
yield _iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} catch (e) { | ||
if (_step) throw e; | ||
} | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
async function foo() { | ||
var _iteratorAbruptCompletion = false; | ||
var _didIteratorError = false; | ||
var _iteratorError; | ||
var _iterator = babelHelpers.asyncIterator(y), | ||
_step = {}, | ||
_notDone; | ||
try { | ||
for (var _iterator = babelHelpers.asyncIterator(y), _step; _iteratorAbruptCompletion = !(_step = await _iterator.next()).done; _iteratorAbruptCompletion = false) { | ||
for (; _notDone = !(_step = await _iterator.next()).done; _notDone = false) { | ||
const x = _step.value; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} catch (e) { | ||
_step = null; | ||
throw e; | ||
} finally { | ||
try { | ||
if (_iteratorAbruptCompletion && _iterator.return != null) { | ||
if (_notDone && _iterator.return) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only undefined should skip the call. |
||
await _iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} catch (e) { | ||
if (_step) throw e; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
babelHelpers.asyncToGenerator(function* () { | ||
var _iteratorAbruptCompletion = false; | ||
var _didIteratorError = false; | ||
var _iteratorError; | ||
var _iterator = babelHelpers.asyncIterator(y), | ||
_step = {}, | ||
_notDone; | ||
try { | ||
for (var _iterator = babelHelpers.asyncIterator(y), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false) { | ||
for (; _notDone = !(_step = yield _iterator.next()).done; _notDone = false) { | ||
obj.x = _step.value; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} catch (e) { | ||
_step = null; | ||
throw e; | ||
} finally { | ||
try { | ||
if (_iteratorAbruptCompletion && _iterator.return != null) { | ||
if (_notDone && _iterator.return) { | ||
yield _iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} catch (e) { | ||
if (_step) throw e; | ||
} | ||
} | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
/*#__PURE__*/babelHelpers.asyncToGenerator(function* () { | ||
var _iteratorAbruptCompletion = false; | ||
var _didIteratorError = false; | ||
var _iteratorError; | ||
var _iterator = babelHelpers.asyncIterator([]), | ||
_step = {}, | ||
_notDone; | ||
try { | ||
for (var _iterator = babelHelpers.asyncIterator([]), _step; _iteratorAbruptCompletion = !(_step = yield _iterator.next()).done; _iteratorAbruptCompletion = false) { | ||
for (; _notDone = !(_step = yield _iterator.next()).done; _notDone = false) { | ||
let x = _step.value; | ||
{ | ||
let x; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} catch (e) { | ||
_step = null; | ||
throw e; | ||
} finally { | ||
try { | ||
if (_iteratorAbruptCompletion && _iterator.return != null) { | ||
if (_notDone && _iterator.return) { | ||
yield _iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} catch (e) { | ||
if (_step) throw e; | ||
} | ||
} | ||
}); |
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.
Is there an upside to initializing STEP_KEY here? It'll be overwritten before use.
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.
Now it's no longer needed, thank you!