| 1 |
// |jit-test| skip-if: !getBuildConfiguration("explicit-resource-management"); --disable-explicit-resource-management |
| 2 |
|
| 3 |
load(libdir + "asserts.js"); |
| 4 |
|
| 5 |
function assertThrowsSyntaxError(str, reflectParseOptions = undefined) { |
| 6 |
assertThrowsInstanceOf(() => Reflect.parse(str, reflectParseOptions), SyntaxError); |
| 7 |
} |
| 8 |
|
| 9 |
// Valid `using` syntaxes that should not work. |
| 10 |
assertThrowsSyntaxError("{ using x = {} }"); |
| 11 |
assertThrowsSyntaxError("using x = {}", { target: "module" }); |
| 12 |
assertThrowsSyntaxError("{ using x = fn() }"); |
| 13 |
assertThrowsSyntaxError("{ using x = fn(); }"); |
| 14 |
assertThrowsSyntaxError("function f() { using x = fn(); }"); |
| 15 |
assertThrowsSyntaxError("switch (x) { case 1: using x = fn(); }"); |
| 16 |
assertThrowsSyntaxError("if (x == 1) { using x = fn(); }"); |
| 17 |
assertThrowsSyntaxError("for (let i = 0; i < 10; i++) { using x = fn(); }"); |
| 18 |
assertThrowsSyntaxError("for (const i of [1, 2, 3]) { using x = fn(); }"); |
| 19 |
assertThrowsSyntaxError("for (const i in {a: 1, b: 2}) { using x = fn(); }"); |
| 20 |
assertThrowsSyntaxError("function* gen() { using x = fn(); }"); |
| 21 |
assertThrowsSyntaxError("async function* fn() { using x = fn(); }"); |
| 22 |
assertThrowsSyntaxError("async function fn() { using x = fn(); }"); |
| 23 |
assertThrowsSyntaxError("class xyz { static { using x = fn(); } }"); |
| 24 |
assertThrowsSyntaxError("{ using a = fn1(), b = fn2(); }"); |
| 25 |
assertThrowsSyntaxError("{ using x = null }"); |
| 26 |
assertThrowsSyntaxError("{ using x = undefined }"); |
| 27 |
assertThrowsSyntaxError("{ for (using x of y) {} }"); |
| 28 |
assertThrowsSyntaxError("for (using x of y) {}"); |
| 29 |
assertThrowsSyntaxError("for await (using x of y) {}", { target: "module" }); |
| 30 |
assertThrowsSyntaxError("async function fn() { for await (using x of y) {} }"); |
| 31 |
assertThrowsSyntaxError("{ using await = {}; }"); |
| 32 |
assertThrowsSyntaxError("{ using yield = {}; }"); |
| 33 |
assertThrowsSyntaxError("{ using public = {}; }"); |
| 34 |
assertThrowsSyntaxError("{ using private = {}; }"); |
| 35 |
assertThrowsSyntaxError("{ using protected = {}; }"); |
| 36 |
assertThrowsSyntaxError("{ using static = {}; }"); |
| 37 |
assertThrowsSyntaxError("{ using as = {}; }"); |
| 38 |
assertThrowsSyntaxError("{ using async = {}; }"); |
| 39 |
assertThrowsSyntaxError("{ using implements = {}; }"); |
| 40 |
assertThrowsSyntaxError("{ using interface = {}; }"); |
| 41 |
assertThrowsSyntaxError("{ using package = {}; }"); |
| 42 |
assertThrowsSyntaxError("'use strict'; { using await = {}; }"); |
| 43 |
assertThrowsSyntaxError("for (using await of y) {}"); |
| 44 |
assertThrowsSyntaxError("for (using yield of y) {}"); |
| 45 |
assertThrowsSyntaxError("using async = {};", { target: "module" }); |
| 46 |
assertThrowsSyntaxError("await using async = {};", { target: "module" }); |
| 47 |
assertThrowsSyntaxError("async function fn() { using async = {}; }"); |
| 48 |
|
| 49 |
// Valid `await using` syntaxes that should not work. |
| 50 |
assertThrowsSyntaxError("await using x = {}", { target: "module" }); |
| 51 |
assertThrowsSyntaxError("if (x == 1) { await using x = fn(); }", { target: "module" }); |
| 52 |
assertThrowsSyntaxError("switch (x) { case 1: await using x = fn(); }", { target: "module" }); |
| 53 |
assertThrowsSyntaxError("async function fn() { await using x = {}; }"); |
| 54 |
assertThrowsSyntaxError("async function* gen() { await using x = {}; }"); |
| 55 |
assertThrowsSyntaxError("for (let i = 0; i < 10; i++) { await using x = fn(); }", { target: "module" }); |
| 56 |
assertThrowsSyntaxError("for (const i of [1, 2, 3]) { await using x = fn(); }", { target: "module" }); |
| 57 |
assertThrowsSyntaxError("for (const i in {a: 1, b: 2}) { await using x = fn(); }", { target: "module" }); |
| 58 |
assertThrowsSyntaxError("for (await using x of y) {}", { target: "module" }); |
| 59 |
assertThrowsSyntaxError("for await (await using x of y) {}", { target: "module" }); |
| 60 |
assertThrowsSyntaxError("async function fn() { for (await using x of y) {} }"); |
| 61 |
assertThrowsSyntaxError("async function fn() { for await (await using x of y) {} }"); |
| 62 |
assertThrowsSyntaxError("async function fn() { await using yield = {} }"); |
| 63 |
assertThrowsSyntaxError("async function fn() { await using public = {} }"); |
| 64 |
assertThrowsSyntaxError("async function fn() { await using private = {} }"); |
| 65 |
assertThrowsSyntaxError("async function fn() { await using protected = {} }"); |
| 66 |
assertThrowsSyntaxError("async function fn() { await using static = {} }"); |
| 67 |
assertThrowsSyntaxError("async function fn() { await using as = {} }"); |
| 68 |
assertThrowsSyntaxError("async function fn() { await using async = {} }"); |
| 69 |
assertThrowsSyntaxError("async function fn() { await using implements = {} }"); |
| 70 |
assertThrowsSyntaxError("async function fn() { await using package = {}; }"); |
| 71 |
assertThrowsSyntaxError("await using as = {}", { target: "module" }); |
| 72 |
assertThrowsSyntaxError("await using async = {};", { target: "module" }); |
| 73 |
assertThrowsSyntaxError("for (await using of of y) {}", { target: "module" }); |
| 74 |
|
| 75 |
// Valid syntaxes close to `using` but not `using` declarations. |
| 76 |
Reflect.parse("for (using of y) {}"); |
| 77 |
Reflect.parse("for (using of of) {}"); |
| 78 |
Reflect.parse("for (using\nof y) {}"); |
| 79 |
Reflect.parse("{ const using = 10; }"); |
| 80 |
Reflect.parse("{ let using = 10 }"); |
| 81 |
Reflect.parse("{ var using = 10 }"); |
| 82 |
Reflect.parse("{ using = 10 }"); |
| 83 |
Reflect.parse("{ using + 1 }"); |
| 84 |
Reflect.parse("{ using++ }"); |
| 85 |
Reflect.parse("{ using\nx = 10 }"); |
| 86 |
Reflect.parse("{ using = {x: 10} }"); |
| 87 |
Reflect.parse("{ x = { using: 10 } }"); |
| 88 |
Reflect.parse("{ x.using = 10 }"); |
| 89 |
Reflect.parse("{ x\n.using = 10 }"); |
| 90 |
Reflect.parse("{ using.x = 10 }"); |
| 91 |
Reflect.parse("{ using\n.x = 10 }"); |
| 92 |
Reflect.parse("for (using[1] of {}) {}"); |
| 93 |
Reflect.parse("for (using\n[1] of {}) {}") |
| 94 |
Reflect.parse("for (using.x of {}) {}"); |
| 95 |
Reflect.parse("for (using\n.x of {}) {}"); |
| 96 |
Reflect.parse("for (x.using in {}) {}"); |
| 97 |
Reflect.parse("for (x\n.using in {}) {}") |
| 98 |
Reflect.parse("{ using: x = 10; }"); |
| 99 |
Reflect.parse("{ using\n: x = 10; }"); |
| 100 |
Reflect.parse("{ using /a/g; }"); |
| 101 |
Reflect.parse("{ /using/g }"); |
| 102 |
Reflect.parse("{ using\nlet = {} }"); |
| 103 |
Reflect.parse("export const using = 10", { target: "module" }); |
| 104 |
Reflect.parse("import using from 'xyz'", { target: "module" }); |
| 105 |
|
| 106 |
const ast4 = Reflect.parse("{ using = 10 }"); |
| 107 |
assertEq(ast4.body[0].body[0].type, "ExpressionStatement"); |
| 108 |
assertEq(ast4.body[0].body[0].expression.type, "AssignmentExpression"); |
| 109 |
assertEq(ast4.body[0].body[0].expression.left.type, "Identifier"); |
| 110 |
assertEq(ast4.body[0].body[0].expression.left.name, "using"); |
| 111 |
|
| 112 |
const ast5 = Reflect.parse("for (using of y) {}"); |
| 113 |
assertEq(ast5.body[0].type, "ForOfStatement"); |
| 114 |
assertEq(ast5.body[0].left.type, "Identifier"); |
| 115 |
assertEq(ast5.body[0].left.name, "using"); |
| 116 |
|
| 117 |
const ast6 = Reflect.parse("{ using + 1 }"); |
| 118 |
assertEq(ast6.body[0].body[0].type, "ExpressionStatement"); |
| 119 |
assertEq(ast6.body[0].body[0].expression.type, "BinaryExpression"); |
| 120 |
assertEq(ast6.body[0].body[0].expression.left.type, "Identifier"); |
| 121 |
assertEq(ast6.body[0].body[0].expression.left.name, "using"); |
| 122 |
|
| 123 |
const ast7 = Reflect.parse("for (using of of) {}"); |
| 124 |
assertEq(ast7.body[0].type, "ForOfStatement"); |
| 125 |
assertEq(ast7.body[0].left.type, "Identifier"); |
| 126 |
assertEq(ast7.body[0].left.name, "using"); |
| 127 |
assertEq(ast7.body[0].right.type, "Identifier"); |
| 128 |
assertEq(ast7.body[0].right.name, "of"); |
| 129 |
|
| 130 |
const ast8 = Reflect.parse("for (using\nof y) {}"); |
| 131 |
assertEq(ast8.body[0].type, "ForOfStatement"); |
| 132 |
assertEq(ast8.body[0].left.type, "Identifier"); |
| 133 |
assertEq(ast8.body[0].left.name, "using"); |
| 134 |
assertEq(ast8.body[0].right.type, "Identifier"); |
| 135 |
assertEq(ast8.body[0].right.name, "y"); |
| 136 |
|
| 137 |
const ast9 = Reflect.parse("for (using[1] of {}) {}"); |
| 138 |
assertEq(ast9.body[0].type, "ForOfStatement"); |
| 139 |
assertEq(ast9.body[0].left.type, "MemberExpression"); |
| 140 |
assertEq(ast9.body[0].left.object.type, "Identifier"); |
| 141 |
assertEq(ast9.body[0].left.object.name, "using"); |
| 142 |
assertEq(ast9.body[0].left.property.type, "Literal"); |
| 143 |
assertEq(ast9.body[0].left.property.value, 1); |
| 144 |
|
| 145 |
const ast10 = Reflect.parse("for (using\n[1] of {}) {}"); |
| 146 |
assertEq(ast10.body[0].type, "ForOfStatement"); |
| 147 |
assertEq(ast10.body[0].left.type, "MemberExpression"); |
| 148 |
assertEq(ast10.body[0].left.object.type, "Identifier"); |
| 149 |
assertEq(ast10.body[0].left.object.name, "using"); |
| 150 |
assertEq(ast10.body[0].left.property.type, "Literal"); |
| 151 |
assertEq(ast10.body[0].left.property.value, 1); |
| 152 |
|
| 153 |
const ast11 = Reflect.parse("{ /using/g }"); |
| 154 |
assertEq(ast11.body[0].body[0].type, "ExpressionStatement"); |
| 155 |
assertEq(ast11.body[0].body[0].expression.type, "Literal"); |
| 156 |
assertEq(ast11.body[0].body[0].expression.value.source, "using"); |
| 157 |
assertEq(ast11.body[0].body[0].expression.value.flags, "g"); |
| 158 |
|
| 159 |
const ast12 = Reflect.parse("{ using: x = 10; }"); |
| 160 |
assertEq(ast12.body[0].body[0].type, "LabeledStatement"); |
| 161 |
assertEq(ast12.body[0].body[0].label.type, "Identifier"); |
| 162 |
assertEq(ast12.body[0].body[0].label.name, "using"); |
| 163 |
|
| 164 |
const ast13 = Reflect.parse("{ using\n: x = 10; }"); |
| 165 |
assertEq(ast13.body[0].body[0].type, "LabeledStatement"); |
| 166 |
assertEq(ast13.body[0].body[0].label.type, "Identifier"); |
| 167 |
assertEq(ast13.body[0].body[0].label.name, "using"); |
| 168 |
|
| 169 |
const ast14 = Reflect.parse("{ using /a/g; }"); |
| 170 |
// should be parsed as division, not regex |
| 171 |
assertEq(ast14.body[0].body[0].type, "ExpressionStatement"); |
| 172 |
assertEq(ast14.body[0].body[0].expression.type, "BinaryExpression"); |
| 173 |
assertEq(ast14.body[0].body[0].expression.operator, "/"); |
| 174 |
assertEq(ast14.body[0].body[0].expression.left.type, "BinaryExpression"); |
| 175 |
assertEq(ast14.body[0].body[0].expression.left.operator, "/"); |
| 176 |
|
| 177 |
const ast15 = Reflect.parse("import using from 'xyz'", { target: "module" }); |
| 178 |
assertEq(ast15.body[0].type, "ImportDeclaration"); |
| 179 |
assertEq(ast15.body[0].specifiers[0].type, "ImportSpecifier"); |
| 180 |
assertEq(ast15.body[0].specifiers[0].name.name, "using"); |
| 181 |
|
| 182 |
// Valid syntaxes close to `await using` but not `await using` declarations. |
| 183 |
Reflect.parse("await /xyz/g"); |
| 184 |
Reflect.parse("{ await /using/g }"); |
| 185 |
Reflect.parse("async function fn() { await using; }"); |
| 186 |
Reflect.parse("async function fn() { await\nusing; }") |
| 187 |
Reflect.parse("async function fn() { await /using/g }"); |
| 188 |
Reflect.parse("async function fn() { await using/g }"); |
| 189 |
Reflect.parse("async function fn() { await using/\ng }"); |
| 190 |
Reflect.parse("async function fn() { for(await using;;) {} }"); |
| 191 |
Reflect.parse("await using;", { target: "module" }); |
| 192 |
Reflect.parse("await\nusing;", { target: "module" }); |
| 193 |
Reflect.parse("await /using/g", { target: "module" }); |
| 194 |
Reflect.parse("await using/g", { target: "module" }); |
| 195 |
Reflect.parse("await using/\ng", { target: "module" }); |
| 196 |
Reflect.parse("for(await using;;) {}", { target: "module" }); |
| 197 |
Reflect.parse("await using\nx;", { target: "module" }); |
| 198 |
|
| 199 |
const ast19 = Reflect.parse("await using", { target: "module" }); |
| 200 |
assertEq(ast19.body[0].type, "ExpressionStatement"); |
| 201 |
assertEq(ast19.body[0].expression.type, "UnaryExpression"); |
| 202 |
assertEq(ast19.body[0].expression.operator, "await"); |
| 203 |
assertEq(ast19.body[0].expression.argument.type, "Identifier"); |
| 204 |
assertEq(ast19.body[0].expression.argument.name, "using"); |
| 205 |
|
| 206 |
const ast20 = Reflect.parse("await /using/g", { target: "module" }); |
| 207 |
assertEq(ast20.body[0].type, "ExpressionStatement"); |
| 208 |
assertEq(ast20.body[0].expression.type, "UnaryExpression"); |
| 209 |
assertEq(ast20.body[0].expression.operator, "await"); |
| 210 |
assertEq(ast20.body[0].expression.argument.type, "Literal"); |
| 211 |
assertEq(ast20.body[0].expression.argument.value.source, "using"); |
| 212 |
assertEq(ast20.body[0].expression.argument.value.flags, "g"); |
| 213 |
|
| 214 |
const ast21 = Reflect.parse("await using/g", { target: "module" }); |
| 215 |
assertEq(ast21.body[0].type, "ExpressionStatement"); |
| 216 |
assertEq(ast21.body[0].expression.type, "BinaryExpression"); |
| 217 |
assertEq(ast21.body[0].expression.operator, "/"); |
| 218 |
assertEq(ast21.body[0].expression.left.type, "UnaryExpression"); |
| 219 |
assertEq(ast21.body[0].expression.left.operator, "await"); |
| 220 |
assertEq(ast21.body[0].expression.left.argument.type, "Identifier"); |
| 221 |
assertEq(ast21.body[0].expression.left.argument.name, "using"); |
| 222 |
|
| 223 |
const ast22 = Reflect.parse("for(await using;;) {}", { target: "module" }); |
| 224 |
assertEq(ast22.body[0].type, "ForStatement"); |
| 225 |
assertEq(ast22.body[0].init.type, "UnaryExpression"); |
| 226 |
assertEq(ast22.body[0].init.operator, "await"); |
| 227 |
assertEq(ast22.body[0].init.argument.type, "Identifier"); |
| 228 |
assertEq(ast22.body[0].init.argument.name, "using"); |