-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly delegate .return() in async generator
- Loading branch information
1 parent
daf25af
commit 133a364
Showing
12 changed files
with
218 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...bel-plugin-proposal-async-generator-functions/test/fixtures/yield-star/issue-9905/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const log = []; | ||
|
||
async function* func1() { | ||
log.push(1); | ||
yield "a"; | ||
log.push(2); | ||
} | ||
|
||
async function* func2() { | ||
yield* func1(); | ||
log.push(3); | ||
} | ||
|
||
return (async () => { | ||
const iterator = func2(); | ||
await iterator.next(); | ||
await iterator.return(); | ||
|
||
expect(log).toEqual([1]); | ||
})(); |
10 changes: 10 additions & 0 deletions
10
...ges/babel-plugin-proposal-async-generator-functions/test/fixtures/yield-star/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"parserOpts": { | ||
"allowReturnOutsideFunction": true | ||
}, | ||
"plugins": [ | ||
"external-helpers", | ||
"transform-async-to-generator", | ||
"proposal-async-generator-functions" | ||
] | ||
} |
37 changes: 37 additions & 0 deletions
37
...l-async-generator-functions/test/fixtures/yield-star/return-method-with-finally-2/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const log = []; | ||
|
||
async function* inner() { | ||
try { | ||
log.push(1); | ||
yield "a"; | ||
log.push(2); | ||
yield "b"; | ||
log.push(3); | ||
} finally { | ||
log.push(4); | ||
yield "c"; | ||
log.push(5); | ||
} | ||
} | ||
|
||
async function* outer() { | ||
log.push(6); | ||
yield* inner(); | ||
log.push(7); | ||
} | ||
|
||
return (async () => { | ||
const iterator = outer(); | ||
|
||
let res = await iterator.next(); | ||
expect(res).toEqual({ value: "a", done: false }); | ||
expect(log).toEqual([6, 1]); | ||
|
||
res = await iterator.return(); | ||
expect(res).toEqual({ value: "c", done: false }); | ||
expect(log).toEqual([6, 1, 4]); | ||
|
||
res = await iterator.return(); | ||
expect(res).toEqual({ value: undefined, done: true }); | ||
expect(log).toEqual([6, 1, 4]); | ||
})(); |
37 changes: 37 additions & 0 deletions
37
...sal-async-generator-functions/test/fixtures/yield-star/return-method-with-finally/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const log = []; | ||
|
||
async function* inner() { | ||
try { | ||
log.push(1); | ||
yield "a"; | ||
log.push(2); | ||
yield "b"; | ||
log.push(3); | ||
} finally { | ||
log.push(4); | ||
yield "c"; | ||
log.push(5); | ||
} | ||
} | ||
|
||
async function* outer() { | ||
log.push(6); | ||
yield* inner(); | ||
log.push(7); | ||
} | ||
|
||
return (async () => { | ||
const iterator = outer(); | ||
|
||
let res = await iterator.next(); | ||
expect(res).toEqual({ value: "a", done: false }); | ||
expect(log).toEqual([6, 1]); | ||
|
||
res = await iterator.return(); | ||
expect(res).toEqual({ value: "c", done: false }); | ||
expect(log).toEqual([6, 1, 4]); | ||
|
||
res = await iterator.next(); | ||
expect(res).toEqual({ value: undefined, done: true }); | ||
expect(log).toEqual([6, 1, 4, 5, 7]); | ||
})(); |
31 changes: 31 additions & 0 deletions
31
...-plugin-proposal-async-generator-functions/test/fixtures/yield-star/return-method/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const log = []; | ||
|
||
async function* inner() { | ||
log.push(1); | ||
yield "a"; | ||
log.push(2); | ||
yield "b"; | ||
log.push(3); | ||
} | ||
|
||
async function* outer() { | ||
log.push(4); | ||
yield* inner(); | ||
log.push(5); | ||
} | ||
|
||
return (async () => { | ||
const iterator = outer(); | ||
|
||
let res = await iterator.next(); | ||
expect(res).toEqual({ value: "a", done: false }); | ||
expect(log).toEqual([4, 1]); | ||
|
||
res = await iterator.return(); | ||
expect(res).toEqual({ value: undefined, done: true }); | ||
expect(log).toEqual([4, 1]); | ||
|
||
res = await iterator.next(); | ||
expect(res).toEqual({ value: undefined, done: true }); | ||
expect(log).toEqual([4, 1]); | ||
})(); |
37 changes: 37 additions & 0 deletions
37
...oposal-async-generator-functions/test/fixtures/yield-star/throw-method-with-catch/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const log = []; | ||
|
||
async function* inner() { | ||
try { | ||
log.push(1); | ||
yield "a"; | ||
log.push(2); | ||
yield "b"; | ||
log.push(3); | ||
} catch { | ||
log.push(4); | ||
yield "c"; | ||
log.push(5); | ||
} | ||
} | ||
|
||
async function* outer() { | ||
log.push(6); | ||
yield* inner(); | ||
log.push(7); | ||
} | ||
|
||
return (async () => { | ||
const iterator = outer(); | ||
|
||
let res = await iterator.next(); | ||
expect(res).toEqual({ value: "a", done: false }); | ||
expect(log).toEqual([6, 1]); | ||
|
||
res = await iterator.throw(new Error("TEST")); | ||
expect(res).toEqual({ value: "c", done: false }); | ||
expect(log).toEqual([6, 1, 4]); | ||
|
||
res = await iterator.next(); | ||
expect(res).toEqual({ value: undefined, done: true }); | ||
expect(log).toEqual([6, 1, 4, 5, 7]); | ||
})(); |
37 changes: 37 additions & 0 deletions
37
...osal-async-generator-functions/test/fixtures/yield-star/throw-method-with-finally/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const log = []; | ||
|
||
async function* inner() { | ||
try { | ||
log.push(1); | ||
yield "a"; | ||
log.push(2); | ||
yield "b"; | ||
log.push(3); | ||
} finally { | ||
log.push(4); | ||
yield "c"; | ||
log.push(5); | ||
} | ||
} | ||
|
||
async function* outer() { | ||
log.push(6); | ||
yield* inner(); | ||
log.push(7); | ||
} | ||
|
||
return (async () => { | ||
const iterator = outer(); | ||
|
||
let res = await iterator.next(); | ||
expect(res).toEqual({ value: "a", done: false }); | ||
expect(log).toEqual([6, 1]); | ||
|
||
res = await iterator.throw(new Error("TEST")); | ||
expect(res).toEqual({ value: "c", done: false }); | ||
expect(log).toEqual([6, 1, 4]); | ||
|
||
// "yield" in finally suspended the exception for one turn | ||
await expect(iterator.next()).rejects.toThrow(/TEST/); | ||
expect(log).toEqual([6, 1, 4, 5]); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters