Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions packages/async-rewriter2/src/async-writer-babel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ describe('AsyncWriter', () => {
expect(ret[Symbol.for('@@mongosh.syntheticPromise')]).to.equal(true);
expect(await ret).to.equal('bar');
});

it('works with eval', async() => {
implicitlyAsyncFn.resolves('yes');
expect(runTranspiledCode('eval("42")')).to.equal(42);
expect(runTranspiledCode('let a = 43; eval("a");')).to.equal(43);
expect(runTranspiledCode('(() => { let b = 44; return eval("b"); })()')).to.equal(44);
expect(await runTranspiledCode(`(() => {
globalThis.eval = implicitlyAsyncFn; return eval("b");
})()`)).to.equal('yes');
});
});

context('error handling', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/async-rewriter2/src/async-writer-babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,12 @@ export const makeMaybeAsyncFunctionPlugin = ({ types: t }: { types: typeof Babel
// If we ever do, replacing all function calls with
// Function.prototype.call.call(fn, target, ...originalArgs) might be
// a feasible solution.
// Additionally, skip calls to 'eval', since literal calls to it
// have semantics that are different from calls to an expressio that
// evaluates to 'eval'.
if (path.parentPath.isCallExpression() &&
path.key === 'callee' &&
path.isMemberExpression()) {
(path.isMemberExpression() || (path.isIdentifier() && path.node.name === 'eval'))) {
return;
}

Expand Down