|
| 1 | +// Tests for 'yield' and 'await' inside class field initializers, where the class is declared inside |
| 2 | +// async and generator functions. |
| 3 | +const verbose = false; |
| 4 | + |
| 5 | +const wrappers = ['none', 'generator', 'async']; |
| 6 | +const fieldModifiers = ['', 'static']; |
| 7 | +const fieldInitExprs = ['yield', 'yield 42', 'function() { yield 21; }', 'await', 'await 3', '() => await 7', 'function() { await 9; }']; |
| 8 | + |
| 9 | +function genTestCases() { |
| 10 | + let cases = []; |
| 11 | + for (const wrapper of wrappers) { |
| 12 | + for (const fieldModifier of fieldModifiers) { |
| 13 | + for (const fieldInitExpr of fieldInitExprs) { |
| 14 | + cases.push({ wrapper, fieldModifier, fieldInitExpr }); |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + return cases; |
| 19 | +} |
| 20 | + |
| 21 | +function genTestScript(c) { |
| 22 | + let tabs = 0; |
| 23 | + let script = ""; |
| 24 | + |
| 25 | + function append(line) { |
| 26 | + for (t = 0; t < tabs; t++) |
| 27 | + script += ' '; |
| 28 | + script += line + '\n'; |
| 29 | + } |
| 30 | + |
| 31 | + switch (c.wrapper) { |
| 32 | + case 'generator': |
| 33 | + append('function * g() {'); |
| 34 | + break; |
| 35 | + case 'async': |
| 36 | + append('async function f() {'); |
| 37 | + break; |
| 38 | + case 'none': |
| 39 | + break; |
| 40 | + } |
| 41 | + tabs++; |
| 42 | + append('class C {'); |
| 43 | + tabs++; |
| 44 | + append(`${c.fieldModifier} f = ${c.fieldInitExpr};`); |
| 45 | + tabs--; |
| 46 | + append('}'); |
| 47 | + tabs--; |
| 48 | + if (c.wrapper !== 'none') append('}'); |
| 49 | + return script; |
| 50 | +} |
| 51 | + |
| 52 | +function expected(c, result, error) { |
| 53 | + if (c.fieldInitExpr === 'await') { |
| 54 | + // 'await' will parse as an identifier. |
| 55 | + if (c.wrapper === 'none' && c.fieldModifier === 'static') { |
| 56 | + // In this case, 'await' as identifier produces a ReferenceError. |
| 57 | + return result === null && error instanceof ReferenceError; |
| 58 | + } |
| 59 | + // In these cases, 'await' as identifier has value 'undefined'). |
| 60 | + return result === undefined && error === null; |
| 61 | + } |
| 62 | + // All other cases should result in a SyntaxError. |
| 63 | + return result === null && error instanceof SyntaxError; |
| 64 | +} |
| 65 | + |
| 66 | +// Verify that 'await' and 'yield' do not parse as keywords (directly) inside class field initializers. |
| 67 | +function testNegativeCases() { |
| 68 | + cases = genTestCases(); |
| 69 | + |
| 70 | + for (const c of cases) { |
| 71 | + let script = genTestScript(c); |
| 72 | + let result = null; |
| 73 | + let error = null; |
| 74 | + try { |
| 75 | + result = eval(script); |
| 76 | + } catch (e) { |
| 77 | + error = e; |
| 78 | + } |
| 79 | + |
| 80 | + if (verbose || !expected(c, result, error)) { |
| 81 | + print(`Case: ${c.wrapper}:${c.fieldModifier}:${c.fieldInitExpr}`); |
| 82 | + print(`Script:\n${script}`); |
| 83 | + print(`Result: ${result}`); |
| 84 | + print(`Error: ${error}\n`); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// Verify that 'await' and 'yield' work inside anonymous async / generator functions |
| 90 | +// used as class field initializers. |
| 91 | +function testPositiveCases() { |
| 92 | + function assertEq(got, expected) { |
| 93 | + if (got !== expected) { |
| 94 | + throw Error(`Got: ${got}, Expected: ${expected}`); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + class C { |
| 99 | + asyncFn0 = async y => await y; |
| 100 | + asyncFn1 = async () => { return await 5 }; |
| 101 | + asyncFn2 = async function(x) { return await x; }; |
| 102 | + |
| 103 | + yieldFn0 = function* () { yield 9; }; |
| 104 | + yieldFn1 = async function* () { yield await 11; }; |
| 105 | + }; |
| 106 | + let c = new C(); |
| 107 | + |
| 108 | + c.asyncFn0(3).then(x => assertEq(x, 3)); |
| 109 | + c.asyncFn1().then(x => assertEq(x, 5)); |
| 110 | + c.asyncFn2(7).then(x => assertEq(x, 7)); |
| 111 | + |
| 112 | + assertEq(c.yieldFn0().next().value, 9); |
| 113 | + c.yieldFn1().next().then(x => assertEq(x.value, 11)); |
| 114 | +} |
| 115 | + |
| 116 | +testNegativeCases(); |
| 117 | +testPositiveCases(); |
0 commit comments