| 1 |
// |jit-test| --enable-import-attributes |
| 2 |
|
| 3 |
load(libdir + "match.js"); |
| 4 |
load(libdir + "asserts.js"); |
| 5 |
|
| 6 |
var { Pattern, MatchError } = Match; |
| 7 |
|
| 8 |
program = (elts) => Pattern({ |
| 9 |
type: "Program", |
| 10 |
body: elts |
| 11 |
}) |
| 12 |
exportDeclaration = (declaration, specifiers, moduleRequest, isDefault) => Pattern({ |
| 13 |
type: "ExportDeclaration", |
| 14 |
declaration: declaration, |
| 15 |
specifiers: specifiers, |
| 16 |
moduleRequest: moduleRequest, |
| 17 |
isDefault: isDefault |
| 18 |
}); |
| 19 |
moduleRequest = (specifier, attributes) => Pattern({ |
| 20 |
type: "ModuleRequest", |
| 21 |
source: specifier, |
| 22 |
attributes: attributes |
| 23 |
}); |
| 24 |
importAttribute = (key, value) => Pattern({ |
| 25 |
type: "ImportAttribute", |
| 26 |
key: key, |
| 27 |
value : value |
| 28 |
}); |
| 29 |
exportSpecifier = (id, name) => Pattern({ |
| 30 |
type: "ExportSpecifier", |
| 31 |
id: id, |
| 32 |
name: name |
| 33 |
}); |
| 34 |
exportBatchSpecifier = () => Pattern({ |
| 35 |
type: "ExportBatchSpecifier" |
| 36 |
}); |
| 37 |
blockStatement = (body) => Pattern({ |
| 38 |
type: "BlockStatement", |
| 39 |
body: body |
| 40 |
}); |
| 41 |
functionDeclaration = (id, params, body) => Pattern({ |
| 42 |
type: "FunctionDeclaration", |
| 43 |
id: id, |
| 44 |
params: params, |
| 45 |
defaults: [], |
| 46 |
body: body, |
| 47 |
rest: null, |
| 48 |
generator: false |
| 49 |
}); |
| 50 |
classDeclaration = (name) => Pattern({ |
| 51 |
type: "ClassStatement", |
| 52 |
id: name |
| 53 |
}); |
| 54 |
variableDeclaration = (decls) => Pattern({ |
| 55 |
type: "VariableDeclaration", |
| 56 |
kind: "var", |
| 57 |
declarations: decls |
| 58 |
}); |
| 59 |
constDeclaration = (decls) => Pattern({ |
| 60 |
type: "VariableDeclaration", |
| 61 |
kind: "const", |
| 62 |
declarations: decls |
| 63 |
}); |
| 64 |
letDeclaration = (decls) => Pattern({ |
| 65 |
type: "VariableDeclaration", |
| 66 |
kind: "let", |
| 67 |
declarations: decls |
| 68 |
}); |
| 69 |
ident = (name) => Pattern({ |
| 70 |
type: "Identifier", |
| 71 |
name: name |
| 72 |
}); |
| 73 |
lit = (val) => Pattern({ |
| 74 |
type: "Literal", |
| 75 |
value: val |
| 76 |
}); |
| 77 |
|
| 78 |
function parseAsModule(source) |
| 79 |
{ |
| 80 |
return Reflect.parse(source, {target: "module"}); |
| 81 |
} |
| 82 |
|
| 83 |
program([ |
| 84 |
exportDeclaration( |
| 85 |
null, |
| 86 |
[], |
| 87 |
null, |
| 88 |
false |
| 89 |
) |
| 90 |
]).assert(parseAsModule("export {}")); |
| 91 |
|
| 92 |
program([ |
| 93 |
letDeclaration([ |
| 94 |
{ |
| 95 |
id: ident("a"), |
| 96 |
init: lit(1) |
| 97 |
} |
| 98 |
]), |
| 99 |
exportDeclaration( |
| 100 |
null, |
| 101 |
[ |
| 102 |
exportSpecifier( |
| 103 |
ident("a"), |
| 104 |
ident("a") |
| 105 |
) |
| 106 |
], |
| 107 |
null, |
| 108 |
false |
| 109 |
) |
| 110 |
]).assert(parseAsModule("let a = 1; export { a }")); |
| 111 |
|
| 112 |
program([ |
| 113 |
letDeclaration([ |
| 114 |
{ |
| 115 |
id: ident("a"), |
| 116 |
init: lit(1) |
| 117 |
} |
| 118 |
]), |
| 119 |
exportDeclaration( |
| 120 |
null, |
| 121 |
[ |
| 122 |
exportSpecifier( |
| 123 |
ident("a"), |
| 124 |
ident("b") |
| 125 |
) |
| 126 |
], |
| 127 |
null, |
| 128 |
false |
| 129 |
) |
| 130 |
]).assert(parseAsModule("let a = 1; export { a as b }")); |
| 131 |
|
| 132 |
program([ |
| 133 |
letDeclaration([ |
| 134 |
{ |
| 135 |
id: ident("as"), |
| 136 |
init: lit(1) |
| 137 |
} |
| 138 |
]), |
| 139 |
exportDeclaration( |
| 140 |
null, |
| 141 |
[ |
| 142 |
exportSpecifier( |
| 143 |
ident("as"), |
| 144 |
ident("as") |
| 145 |
) |
| 146 |
], |
| 147 |
null, |
| 148 |
false |
| 149 |
) |
| 150 |
]).assert(parseAsModule("let as = 1; export { as as as }")); |
| 151 |
|
| 152 |
program([ |
| 153 |
letDeclaration([ |
| 154 |
{ |
| 155 |
id: ident("a"), |
| 156 |
init: lit(1) |
| 157 |
} |
| 158 |
]), |
| 159 |
exportDeclaration( |
| 160 |
null, |
| 161 |
[ |
| 162 |
exportSpecifier( |
| 163 |
ident("a"), |
| 164 |
ident("true") |
| 165 |
) |
| 166 |
], |
| 167 |
null, |
| 168 |
false |
| 169 |
) |
| 170 |
]).assert(parseAsModule("let a = 1; export { a as true }")); |
| 171 |
|
| 172 |
program([ |
| 173 |
letDeclaration([ |
| 174 |
{ |
| 175 |
id: ident("a"), |
| 176 |
init: lit(1) |
| 177 |
}, |
| 178 |
{ |
| 179 |
id: ident("b"), |
| 180 |
init: lit(2) |
| 181 |
} |
| 182 |
]), |
| 183 |
exportDeclaration( |
| 184 |
null, |
| 185 |
[ |
| 186 |
exportSpecifier( |
| 187 |
ident("a"), |
| 188 |
ident("a") |
| 189 |
), |
| 190 |
exportSpecifier( |
| 191 |
ident("b"), |
| 192 |
ident("b") |
| 193 |
), |
| 194 |
], |
| 195 |
null, |
| 196 |
false |
| 197 |
) |
| 198 |
]).assert(parseAsModule("let a = 1, b = 2; export { a, b }")); |
| 199 |
|
| 200 |
program([ |
| 201 |
letDeclaration([ |
| 202 |
{ |
| 203 |
id: ident("a"), |
| 204 |
init: lit(1) |
| 205 |
}, |
| 206 |
{ |
| 207 |
id: ident("c"), |
| 208 |
init: lit(2) |
| 209 |
} |
| 210 |
]), |
| 211 |
exportDeclaration( |
| 212 |
null, |
| 213 |
[ |
| 214 |
exportSpecifier( |
| 215 |
ident("a"), |
| 216 |
ident("b") |
| 217 |
), |
| 218 |
exportSpecifier( |
| 219 |
ident("c"), |
| 220 |
ident("d") |
| 221 |
), |
| 222 |
], |
| 223 |
null, |
| 224 |
false |
| 225 |
) |
| 226 |
]).assert(parseAsModule("let a = 1, c = 2; export { a as b, c as d }")); |
| 227 |
|
| 228 |
program([ |
| 229 |
exportDeclaration( |
| 230 |
null, |
| 231 |
[ |
| 232 |
exportSpecifier( |
| 233 |
ident("a"), |
| 234 |
ident("a") |
| 235 |
) |
| 236 |
], |
| 237 |
moduleRequest( |
| 238 |
lit("b"), |
| 239 |
[], |
| 240 |
), |
| 241 |
false |
| 242 |
) |
| 243 |
]).assert(parseAsModule("export { a } from 'b'")); |
| 244 |
|
| 245 |
program([ |
| 246 |
exportDeclaration( |
| 247 |
null, |
| 248 |
[ |
| 249 |
exportBatchSpecifier() |
| 250 |
], |
| 251 |
moduleRequest( |
| 252 |
lit("a"), |
| 253 |
[], |
| 254 |
), |
| 255 |
false |
| 256 |
) |
| 257 |
]).assert(parseAsModule("export * from 'a'")); |
| 258 |
|
| 259 |
program([ |
| 260 |
exportDeclaration( |
| 261 |
functionDeclaration( |
| 262 |
ident("f"), |
| 263 |
[], |
| 264 |
blockStatement([]) |
| 265 |
), |
| 266 |
null, |
| 267 |
null, |
| 268 |
false |
| 269 |
) |
| 270 |
]).assert(parseAsModule("export function f() {}")); |
| 271 |
|
| 272 |
program([ |
| 273 |
exportDeclaration( |
| 274 |
classDeclaration( |
| 275 |
ident("Foo") |
| 276 |
), |
| 277 |
null, |
| 278 |
null, |
| 279 |
false |
| 280 |
) |
| 281 |
]).assert(parseAsModule("export class Foo { constructor() {} }")); |
| 282 |
|
| 283 |
program([ |
| 284 |
exportDeclaration( |
| 285 |
variableDeclaration([ |
| 286 |
{ |
| 287 |
id: ident("a"), |
| 288 |
init: lit(1) |
| 289 |
}, { |
| 290 |
id: ident("b"), |
| 291 |
init: lit(2) |
| 292 |
} |
| 293 |
]), |
| 294 |
null, |
| 295 |
null, |
| 296 |
false |
| 297 |
) |
| 298 |
]).assert(parseAsModule("export var a = 1, b = 2;")); |
| 299 |
|
| 300 |
program([ |
| 301 |
exportDeclaration( |
| 302 |
constDeclaration([ |
| 303 |
{ |
| 304 |
id: ident("a"), |
| 305 |
init: lit(1) |
| 306 |
}, { |
| 307 |
id: ident("b"), |
| 308 |
init: lit(2) |
| 309 |
} |
| 310 |
]), |
| 311 |
null, |
| 312 |
null, |
| 313 |
false |
| 314 |
) |
| 315 |
]).assert(parseAsModule("export const a = 1, b = 2;")); |
| 316 |
|
| 317 |
program([ |
| 318 |
exportDeclaration( |
| 319 |
letDeclaration([ |
| 320 |
{ |
| 321 |
id: ident("a"), |
| 322 |
init: lit(1) |
| 323 |
}, { |
| 324 |
id: ident("b"), |
| 325 |
init: lit(2) |
| 326 |
} |
| 327 |
]), |
| 328 |
null, |
| 329 |
null, |
| 330 |
false |
| 331 |
) |
| 332 |
]).assert(parseAsModule("export let a = 1, b = 2;")); |
| 333 |
|
| 334 |
program([ |
| 335 |
exportDeclaration( |
| 336 |
functionDeclaration( |
| 337 |
ident("default"), |
| 338 |
[], |
| 339 |
blockStatement([]) |
| 340 |
), |
| 341 |
null, |
| 342 |
null, |
| 343 |
true |
| 344 |
) |
| 345 |
]).assert(parseAsModule("export default function() {}")); |
| 346 |
|
| 347 |
program([ |
| 348 |
exportDeclaration( |
| 349 |
functionDeclaration( |
| 350 |
ident("foo"), |
| 351 |
[], |
| 352 |
blockStatement([]) |
| 353 |
), |
| 354 |
null, |
| 355 |
null, |
| 356 |
true |
| 357 |
) |
| 358 |
]).assert(parseAsModule("export default function foo() {}")); |
| 359 |
|
| 360 |
program([ |
| 361 |
exportDeclaration( |
| 362 |
classDeclaration( |
| 363 |
ident("default") |
| 364 |
), |
| 365 |
null, |
| 366 |
null, |
| 367 |
true |
| 368 |
) |
| 369 |
]).assert(parseAsModule("export default class { constructor() {} }")); |
| 370 |
|
| 371 |
program([ |
| 372 |
exportDeclaration( |
| 373 |
classDeclaration( |
| 374 |
ident("Foo") |
| 375 |
), |
| 376 |
null, |
| 377 |
null, |
| 378 |
true |
| 379 |
) |
| 380 |
]).assert(parseAsModule("export default class Foo { constructor() {} }")); |
| 381 |
|
| 382 |
program([ |
| 383 |
exportDeclaration( |
| 384 |
lit(1234), |
| 385 |
null, |
| 386 |
null, |
| 387 |
true |
| 388 |
) |
| 389 |
]).assert(parseAsModule("export default 1234")); |
| 390 |
|
| 391 |
if (getRealmConfiguration("importAttributes")) { |
| 392 |
program([ |
| 393 |
exportDeclaration( |
| 394 |
null, |
| 395 |
[ |
| 396 |
exportSpecifier( |
| 397 |
ident("a"), |
| 398 |
ident("a") |
| 399 |
) |
| 400 |
], |
| 401 |
moduleRequest( |
| 402 |
lit("b"), |
| 403 |
[ |
| 404 |
importAttribute(ident('type'), lit('js')), |
| 405 |
] |
| 406 |
), |
| 407 |
false |
| 408 |
) |
| 409 |
]).assert(parseAsModule("export { a } from 'b' with { type: 'js' } ")); |
| 410 |
|
| 411 |
program([ |
| 412 |
exportDeclaration( |
| 413 |
null, |
| 414 |
[ |
| 415 |
exportSpecifier( |
| 416 |
ident("a"), |
| 417 |
ident("a") |
| 418 |
) |
| 419 |
], |
| 420 |
moduleRequest( |
| 421 |
lit("b"), |
| 422 |
[ |
| 423 |
importAttribute(ident('foo'), lit('bar')), |
| 424 |
], |
| 425 |
), |
| 426 |
false |
| 427 |
) |
| 428 |
]).assert(parseAsModule("export { a } from 'b' with { foo: 'bar', } ")); |
| 429 |
|
| 430 |
program([ |
| 431 |
exportDeclaration( |
| 432 |
null, |
| 433 |
[ |
| 434 |
exportSpecifier( |
| 435 |
ident("a"), |
| 436 |
ident("a") |
| 437 |
) |
| 438 |
], |
| 439 |
moduleRequest( |
| 440 |
lit("b"), |
| 441 |
[ |
| 442 |
importAttribute(ident('type'), lit('js')), |
| 443 |
importAttribute(ident('foo'), lit('bar')), |
| 444 |
importAttribute(ident('test'), lit('123')), |
| 445 |
] |
| 446 |
), |
| 447 |
false |
| 448 |
) |
| 449 |
]).assert(parseAsModule("export { a } from 'b' with { type: 'js', foo: 'bar', 'test': '123' } ")); |
| 450 |
|
| 451 |
assertThrowsInstanceOf(function () { |
| 452 |
parseAsModule("export { a } from 'b' with { type: type }"); |
| 453 |
}, SyntaxError); |
| 454 |
} |
| 455 |
|
| 456 |
assertThrowsInstanceOf(function () { |
| 457 |
parseAsModule("export default 1234 5678"); |
| 458 |
}, SyntaxError); |
| 459 |
|
| 460 |
var loc = parseAsModule("export { a as b } from 'c'", { |
| 461 |
loc: true |
| 462 |
}).body[0].loc; |
| 463 |
|
| 464 |
assertEq(loc.start.line, 1); |
| 465 |
assertEq(loc.start.column, 1); |
| 466 |
assertEq(loc.start.line, 1); |
| 467 |
assertEq(loc.end.column, 27); |
| 468 |
|
| 469 |
assertThrowsInstanceOf(function () { |
| 470 |
parseAsModule("function f() { export a }"); |
| 471 |
}, SyntaxError); |
| 472 |
|
| 473 |
assertThrowsInstanceOf(function () { |
| 474 |
parseAsModule("if (true) export a"); |
| 475 |
}, SyntaxError); |
| 476 |
|
| 477 |
assertThrowsInstanceOf(function() { |
| 478 |
parseAsModule("export {"); |
| 479 |
}, SyntaxError); |
| 480 |
|
| 481 |
assertThrowsInstanceOf(function() { |
| 482 |
parseAsModule("export {} from"); |
| 483 |
}, SyntaxError); |
| 484 |
|
| 485 |
assertThrowsInstanceOf(function() { |
| 486 |
parseAsModule("export {,} from 'a'"); |
| 487 |
}, SyntaxError); |
| 488 |
|
| 489 |
program([ |
| 490 |
exportDeclaration( |
| 491 |
null, |
| 492 |
[ |
| 493 |
exportSpecifier( |
| 494 |
ident("true"), |
| 495 |
ident("true") |
| 496 |
), |
| 497 |
], |
| 498 |
moduleRequest( |
| 499 |
lit("b"), |
| 500 |
[], |
| 501 |
), |
| 502 |
false |
| 503 |
) |
| 504 |
]).assert(parseAsModule("export { true } from 'b'")); |
| 505 |
|
| 506 |
program([ |
| 507 |
exportDeclaration( |
| 508 |
null, |
| 509 |
[ |
| 510 |
exportSpecifier( |
| 511 |
ident("true"), |
| 512 |
ident("name") |
| 513 |
), |
| 514 |
], |
| 515 |
moduleRequest( |
| 516 |
lit("b"), |
| 517 |
[], |
| 518 |
), |
| 519 |
false |
| 520 |
) |
| 521 |
]).assert(parseAsModule("export { true as name } from 'b'")); |
| 522 |
|
| 523 |
assertThrowsInstanceOf(function() { |
| 524 |
parseAsModule("export { true }"); |
| 525 |
}, SyntaxError); |
| 526 |
|
| 527 |
assertThrowsInstanceOf(function() { |
| 528 |
parseAsModule("export { true as name }"); |
| 529 |
}, SyntaxError); |
| 530 |
|
| 531 |
assertThrowsInstanceOf(function() { |
| 532 |
parseAsModule("export { static }"); |
| 533 |
}, SyntaxError); |
| 534 |
|
| 535 |
assertThrowsInstanceOf(function() { |
| 536 |
parseAsModule("export { static as name }"); |
| 537 |
}, SyntaxError); |
| 538 |
|
| 539 |
assertThrowsInstanceOf(function () { |
| 540 |
parseAsModule("export { name } from 'b' f();"); |
| 541 |
}, SyntaxError); |
| 542 |
|
| 543 |
assertThrowsInstanceOf(function () { |
| 544 |
parseAsModule("export *"); |
| 545 |
}, SyntaxError); |
| 546 |
|
| 547 |
assertThrowsInstanceOf(function () { |
| 548 |
parseAsModule("export * from 'b' f();"); |
| 549 |
}, SyntaxError); |
| 550 |
|
| 551 |
assertThrowsInstanceOf(function() { |
| 552 |
parseAsModule("export {}\nfrom ()"); |
| 553 |
}, SyntaxError); |
| 554 |
|
| 555 |
assertThrowsInstanceOf(function() { |
| 556 |
parseAsModule("function() {}"); |
| 557 |
}, SyntaxError); |
| 558 |
|
| 559 |
assertThrowsInstanceOf(function() { |
| 560 |
parseAsModule("class() { constructor() {} }"); |
| 561 |
}, SyntaxError); |
| 562 |
|
| 563 |
assertThrowsInstanceOf(function() { |
| 564 |
parseAsModule("export x"); |
| 565 |
}, SyntaxError); |
| 566 |
|
| 567 |
assertThrowsInstanceOf(function() { |
| 568 |
parseAsModule("export foo = 5"); |
| 569 |
}, SyntaxError); |