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
44 changes: 44 additions & 0 deletions packages/async-rewriter2/src/async-writer-babel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ describe('AsyncWriter', () => {
for (const value of gen) return value;
})()`)).to.throw('[ASYNC-10012] Result of expression "implicitlyAsyncFn()" cannot be used in this context');
});

it('cannot implicitly await inside of array.sort() callback', () => {
implicitlyAsyncFn.callsFake((x, y) => x.a - y.a);
expect(() => runTranspiledCode(`
const arr = [{ a: 2 }, { a : 1 }];
arr.sort((x, y) => implicitlyAsyncFn(x, y));
`)).to.throw('[ASYNC-10012] Result of expression "compareFn(...args)" cannot be used in this context');
});
});
});

Expand Down Expand Up @@ -523,6 +531,15 @@ describe('AsyncWriter', () => {
expect(implicitlyAsyncFn).to.have.been.calledWith(4, 4, set);
expect(implicitlyAsyncFn).to.have.been.calledWith(6, 6, set);
});

it('supports Array.prototype.flatMap', async() => {
implicitlyAsyncFn.callsFake(x => [ x - 1, x ]);
const arr = await runTranspiledCode(`
const arr = [ 2, 4, 6, 8 ];
arr.flatMap(implicitlyAsyncFn)
`);
expect(arr).to.deep.equal([1, 2, 3, 4, 5, 6, 7, 8]);
});
});

context('synchronous', () => {
Expand Down Expand Up @@ -630,6 +647,33 @@ describe('AsyncWriter', () => {
expect(plainFn).to.have.been.calledWith(4, 4, set);
expect(plainFn).to.have.been.calledWith(6, 6, set);
});

it('supports Array.prototype.flatMap', () => {
plainFn.callsFake(x => [ x - 1, x ]);
const arr = runTranspiledCode(`
const arr = [ 2, 4, 6, 8 ];
arr.flatMap(plainFn)
`);
expect(arr).to.deep.equal([1, 2, 3, 4, 5, 6, 7, 8]);
});

it('supports Array.prototype.sort', () => {
plainFn.callsFake((x, y) => x.a - y.a);
const arr = runTranspiledCode(`
const arr = [ { a: 1 }, { a: 9 }, { a: 4 }, { a: 16 } ];
arr.sort(plainFn)
`);
expect(arr).to.deep.equal([ { a: 1 }, { a: 4 }, { a: 9 }, { a: 16 } ]);
});

it('supports TypedArray.prototype.sort', () => {
plainFn.callsFake((x, y) => x - y);
const arr = runTranspiledCode(`
const arr = new Uint8Array([1, 9, 4, 16]);
arr.sort(plainFn)
`);
expect(arr).to.deep.equal(new Uint8Array([1, 4, 9, 16]));
});
});

context('Function.prototype.toString', () => {
Expand Down
26 changes: 24 additions & 2 deletions packages/async-rewriter2/src/runtime-support.nocov.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,30 @@ module.exports = '(' + function() {
});
};

// Currently Missing: (Typed)Array.prototype.sort
// Currently Missing: Array.prototype.flatMap
const origArraySort = Array.prototype.sort;
Array.prototype.sort = function(compareFn) {
return origArraySort.call(this, 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];
});
};
const origTypedArraySort = TypedArray.prototype.sort;
TypedArray.prototype.sort = function(compareFn) {
return origTypedArraySort.call(this, 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];
});
};

Array.prototype.flatMap = function(...args) {
return Array.prototype.map.call(this, ...args).flat();
};

TypedArray.prototype.reduce = Array.prototype.reduce;
TypedArray.prototype.reduceRight = Array.prototype.reduceRight;
Expand Down