Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Commit

Permalink
toString() contains the compiled code for "generator" mode
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Feb 5, 2020
1 parent d56395c commit d5bea60
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ namespace degenerator {
if (isAsyncFunction(fn)) {
return fn;
} else {
return (generatorToPromise(fn) as unknown) as T;
const rtn = (generatorToPromise(fn) as unknown) as T;
Object.defineProperty(rtn, 'toString', {
value: fn.toString.bind(fn),
enumerable: false
})
return rtn;
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ describe('degenerator()', () => {
assert.equal(val, 'cb');
});
});
it('should contain the compiled code in `toString()` output', () => {
const a = () => 'a';
const b = () => 'b';
function aPlusB(): string {
return a() + b();
}
const fn = compile<() => Promise<string>>(
'' + aPlusB,
'aPlusB',
['b'],
{
sandbox: { a, b }
}
);
if (supportsAsync) {
assert(/async b\(\)/.test(fn+''));
} else {
assert(/yield b\(\)/.test(fn+''));
}
});
it('should be able to await non-promises', () => {
const a = () => 'a';
const b = () => 'b';
Expand Down

0 comments on commit d5bea60

Please sign in to comment.