Skip to content

Commit 5cccf81

Browse files
authored
fix(async-rewriter2): ensure eval works MONGOSH-665 (#752)
1 parent 4bfe315 commit 5cccf81

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

packages/async-rewriter2/src/async-writer-babel.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,16 @@ describe('AsyncWriter', () => {
324324
expect(ret[Symbol.for('@@mongosh.syntheticPromise')]).to.equal(true);
325325
expect(await ret).to.equal('bar');
326326
});
327+
328+
it('works with eval', async() => {
329+
implicitlyAsyncFn.resolves('yes');
330+
expect(runTranspiledCode('eval("42")')).to.equal(42);
331+
expect(runTranspiledCode('let a = 43; eval("a");')).to.equal(43);
332+
expect(runTranspiledCode('(() => { let b = 44; return eval("b"); })()')).to.equal(44);
333+
expect(await runTranspiledCode(`(() => {
334+
globalThis.eval = implicitlyAsyncFn; return eval("b");
335+
})()`)).to.equal('yes');
336+
});
327337
});
328338

329339
context('error handling', () => {

packages/async-rewriter2/src/async-writer-babel.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,12 @@ export const makeMaybeAsyncFunctionPlugin = ({ types: t }: { types: typeof Babel
536536
// If we ever do, replacing all function calls with
537537
// Function.prototype.call.call(fn, target, ...originalArgs) might be
538538
// a feasible solution.
539+
// Additionally, skip calls to 'eval', since literal calls to it
540+
// have semantics that are different from calls to an expressio that
541+
// evaluates to 'eval'.
539542
if (path.parentPath.isCallExpression() &&
540543
path.key === 'callee' &&
541-
path.isMemberExpression()) {
544+
(path.isMemberExpression() || (path.isIdentifier() && path.node.name === 'eval'))) {
542545
return;
543546
}
544547

0 commit comments

Comments
 (0)