Skip to content

Commit 3f9e433

Browse files
authored
fix(async-rewriter2): ensure sort without compare fn works MONGOSH-676 (#773)
1 parent c4f8e91 commit 3f9e433

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,14 @@ describe('AsyncWriter', () => {
674674
`);
675675
expect(arr).to.deep.equal(new Uint8Array([1, 4, 9, 16]));
676676
});
677+
678+
it('supports Array.prototype.sort without callback', () => {
679+
const arr = runTranspiledCode(`
680+
const arr = [ 1, 9, 4, 16 ];
681+
arr.sort()
682+
`);
683+
expect(arr).to.deep.equal([ 1, 16, 4, 9 ]);
684+
});
677685
});
678686

679687
context('Function.prototype.toString', () => {

packages/async-rewriter2/src/runtime-support.nocov.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,23 +453,23 @@ module.exports = '(' + function() {
453453

454454
const origArraySort = Array.prototype.sort;
455455
Array.prototype.sort = function(compareFn) {
456-
return origArraySort.call(this, function(...args) {
456+
return origArraySort.call(this, compareFn ? function(...args) {
457457
// (Ab-)use a generator function as one of the places where using
458458
// implicit async expression results in an error.
459459
return [...(function*() {
460460
yield compareFn(...args);
461461
})()][0];
462-
});
462+
} : undefined);
463463
};
464464
const origTypedArraySort = TypedArray.prototype.sort;
465465
TypedArray.prototype.sort = function(compareFn) {
466-
return origTypedArraySort.call(this, function(...args) {
466+
return origTypedArraySort.call(this, compareFn ? function(...args) {
467467
// (Ab-)use a generator function as one of the places where using
468468
// implicit async expression results in an error.
469469
return [...(function*() {
470470
yield compareFn(...args);
471471
})()][0];
472-
});
472+
} : undefined);
473473
};
474474

475475
Array.prototype.flatMap = function(...args) {

0 commit comments

Comments
 (0)