Skip to content

[Do not merge] Prototype: Do not use babel exit() hooks in async rewriter #1947

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/async-rewriter2/src/async-writer-babel.spec.ts
Original file line number Diff line number Diff line change
@@ -78,6 +78,7 @@ describe('AsyncWriter', function () {
});
runTranspiledCode = (code: string, context?: any) => {
const transpiled = asyncWriter.process(code);
//console.log({transpiled})
return runUntranspiledCode(transpiled, context);
};
runUntranspiledCode = (code: string, context?: any) => {
40 changes: 28 additions & 12 deletions packages/async-rewriter2/src/stages/transform-maybe-await.ts
Original file line number Diff line number Diff line change
@@ -46,6 +46,11 @@ const isGeneratedHelper = asNodeKey(Symbol('isGeneratedHelper'));
const isOriginalBody = asNodeKey(Symbol('isOriginalBody'));
const isAlwaysSyncFunction = asNodeKey(Symbol('isAlwaysSyncFunction'));
const isExpandedTypeof = asNodeKey(Symbol('isExpandedTypeof'));
const isExpandedReturnExpression = asNodeKey(
Symbol('isExpandedReturnExpression')
);
const isExpandedExpression = asNodeKey(Symbol('isExpandedExpression'));
const isExpandedCatchClause = asNodeKey(Symbol('isExpandedCatchClause'));
// Using this key, we store data on Function nodes that contains the identifiers
// of helpers which are available inside the function.
const identifierGroupKey = '@@mongosh.identifierGroup';
@@ -509,6 +514,8 @@ export default ({
path.replaceWith(t.blockStatement([t.returnStatement(path.node)]));
}

if (path.node[isExpandedExpression]) return;

// If there is a [isGeneratedHelper] between the function we're in
// and this node, that means we've already handled this node.
if (
@@ -518,8 +525,7 @@ export default ({
) {
return path.skip();
}
},
exit(path) {

// We have seen an expression. If we're not inside an async function,
// or a function that we explicitly marked as needing always-synchronous
// treatment, we don't care.
@@ -558,7 +564,7 @@ export default ({
}
if (
path.parentPath.isReturnStatement() &&
!path.node[isGeneratedHelper]
!path.node[isExpandedReturnExpression]
) {
// If this is inside a return statement that we have not already handled,
// we replace the `return ...` with
@@ -571,7 +577,7 @@ export default ({
FUNCTION_STATE_IDENTIFIER: identifierGroup.functionState,
NODE: path.node,
}),
{ [isGeneratedHelper]: true }
{ [isExpandedReturnExpression]: true }
)
);
return;
@@ -679,25 +685,35 @@ export default ({
return;
}

if (
path.node.type === 'CallExpression' &&
path.node.callee.type === 'Identifier' &&
path.node.callee.name === isSyntheticPromise.name
)
return;

path.replaceWith(
Object.assign(
awaitSyntheticPromiseTemplate({
ORIGINAL_SOURCE: getOriginalSourceString(this, path.node),
ORIGINAL_SOURCE: Object.assign(
getOriginalSourceString(this, path.node),
{ [isGeneratedHelper]: true }
),
EXPRESSION_HOLDER: expressionHolder,
ISP_IDENTIFIER: isSyntheticPromise,
NODE: path.node,
NODE: Object.assign(path.node, {
[isExpandedExpression]: true,
}),
}),
{ [isGeneratedHelper]: true }
{ [isExpandedExpression]: true }
)
);
// We are exiting from the current path and don't need to visit it again.
path.skip();
},
},
CatchClause: {
exit(path) {
enter(path) {
if (
path.node[isGeneratedHelper] ||
path.node[isExpandedCatchClause] ||
!path.node.param ||
path.node.param.type !== 'Identifier'
)
@@ -724,7 +740,7 @@ export default ({
path.node.body,
])
),
{ [isGeneratedHelper]: true }
{ [isExpandedCatchClause]: true }
)
);
},