|
| 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', 'await', 'await 3']; |
| 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 | +cases = genTestCases(); |
| 67 | + |
| 68 | +for (const c of cases) { |
| 69 | + let script = genTestScript(c); |
| 70 | + let result = null; |
| 71 | + let error = null; |
| 72 | + try { |
| 73 | + result = eval(script); |
| 74 | + } catch (e) { |
| 75 | + error = e; |
| 76 | + } |
| 77 | + |
| 78 | + if (verbose || !expected(c, result, error)) { |
| 79 | + print(`Case: ${c.wrapper}:${c.fieldModifier}:${c.fieldInitExpr}`); |
| 80 | + print(`Script:\n${script}`); |
| 81 | + if (result != null) { |
| 82 | + print("Result: " + result); |
| 83 | + } else if (error != null) { |
| 84 | + print("Error: " + error); |
| 85 | + } else { |
| 86 | + print("Expecting either result or error!") |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments