From 1b42e5ebefeab51d6049a6291d823b66846e4734 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 8 Apr 2021 11:53:57 +0200 Subject: [PATCH] fix(async-rewriter2): ensure sort without compare fn works MONGOSH-676 --- packages/async-rewriter2/src/async-writer-babel.spec.ts | 8 ++++++++ packages/async-rewriter2/src/runtime-support.nocov.js | 8 ++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/async-rewriter2/src/async-writer-babel.spec.ts b/packages/async-rewriter2/src/async-writer-babel.spec.ts index 47ac0728c2..38d0491029 100644 --- a/packages/async-rewriter2/src/async-writer-babel.spec.ts +++ b/packages/async-rewriter2/src/async-writer-babel.spec.ts @@ -674,6 +674,14 @@ describe('AsyncWriter', () => { `); expect(arr).to.deep.equal(new Uint8Array([1, 4, 9, 16])); }); + + it('supports Array.prototype.sort without callback', () => { + const arr = runTranspiledCode(` + const arr = [ 1, 9, 4, 16 ]; + arr.sort() + `); + expect(arr).to.deep.equal([ 1, 16, 4, 9 ]); + }); }); context('Function.prototype.toString', () => { diff --git a/packages/async-rewriter2/src/runtime-support.nocov.js b/packages/async-rewriter2/src/runtime-support.nocov.js index 9fb33c60f6..728033a54f 100644 --- a/packages/async-rewriter2/src/runtime-support.nocov.js +++ b/packages/async-rewriter2/src/runtime-support.nocov.js @@ -453,23 +453,23 @@ module.exports = '(' + function() { const origArraySort = Array.prototype.sort; Array.prototype.sort = function(compareFn) { - return origArraySort.call(this, function(...args) { + return origArraySort.call(this, compareFn ? function(...args) { // (Ab-)use a generator function as one of the places where using // implicit async expression results in an error. return [...(function*() { yield compareFn(...args); })()][0]; - }); + } : undefined); }; const origTypedArraySort = TypedArray.prototype.sort; TypedArray.prototype.sort = function(compareFn) { - return origTypedArraySort.call(this, function(...args) { + return origTypedArraySort.call(this, compareFn ? function(...args) { // (Ab-)use a generator function as one of the places where using // implicit async expression results in an error. return [...(function*() { yield compareFn(...args); })()][0]; - }); + } : undefined); }; Array.prototype.flatMap = function(...args) {