export-declaration.js (11640B)
1 // |jit-test| 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 program([ 392 exportDeclaration( 393 null, 394 [ 395 exportSpecifier( 396 ident("a"), 397 ident("a") 398 ) 399 ], 400 moduleRequest( 401 lit("b"), 402 [ 403 importAttribute(ident('type'), lit('js')), 404 ] 405 ), 406 false 407 ) 408 ]).assert(parseAsModule("export { a } from 'b' with { type: 'js' } ")); 409 410 program([ 411 exportDeclaration( 412 null, 413 [ 414 exportSpecifier( 415 ident("a"), 416 ident("a") 417 ) 418 ], 419 moduleRequest( 420 lit("b"), 421 [ 422 importAttribute(ident('foo'), lit('bar')), 423 ], 424 ), 425 false 426 ) 427 ]).assert(parseAsModule("export { a } from 'b' with { foo: 'bar', } ")); 428 429 program([ 430 exportDeclaration( 431 null, 432 [ 433 exportSpecifier( 434 ident("a"), 435 ident("a") 436 ) 437 ], 438 moduleRequest( 439 lit("b"), 440 [ 441 importAttribute(ident('type'), lit('js')), 442 importAttribute(ident('foo'), lit('bar')), 443 importAttribute(ident('test'), lit('123')), 444 ] 445 ), 446 false 447 ) 448 ]).assert(parseAsModule("export { a } from 'b' with { type: 'js', foo: 'bar', 'test': '123' } ")); 449 450 assertThrowsInstanceOf(function () { 451 parseAsModule("export { a } from 'b' with { type: type }"); 452 }, SyntaxError); 453 454 assertThrowsInstanceOf(function () { 455 parseAsModule("export default 1234 5678"); 456 }, SyntaxError); 457 458 var loc = parseAsModule("export { a as b } from 'c'", { 459 loc: true 460 }).body[0].loc; 461 462 assertEq(loc.start.line, 1); 463 assertEq(loc.start.column, 1); 464 assertEq(loc.start.line, 1); 465 assertEq(loc.end.column, 27); 466 467 assertThrowsInstanceOf(function () { 468 parseAsModule("function f() { export a }"); 469 }, SyntaxError); 470 471 assertThrowsInstanceOf(function () { 472 parseAsModule("if (true) export a"); 473 }, SyntaxError); 474 475 assertThrowsInstanceOf(function() { 476 parseAsModule("export {"); 477 }, SyntaxError); 478 479 assertThrowsInstanceOf(function() { 480 parseAsModule("export {} from"); 481 }, SyntaxError); 482 483 assertThrowsInstanceOf(function() { 484 parseAsModule("export {,} from 'a'"); 485 }, SyntaxError); 486 487 program([ 488 exportDeclaration( 489 null, 490 [ 491 exportSpecifier( 492 ident("true"), 493 ident("true") 494 ), 495 ], 496 moduleRequest( 497 lit("b"), 498 [], 499 ), 500 false 501 ) 502 ]).assert(parseAsModule("export { true } from 'b'")); 503 504 program([ 505 exportDeclaration( 506 null, 507 [ 508 exportSpecifier( 509 ident("true"), 510 ident("name") 511 ), 512 ], 513 moduleRequest( 514 lit("b"), 515 [], 516 ), 517 false 518 ) 519 ]).assert(parseAsModule("export { true as name } from 'b'")); 520 521 assertThrowsInstanceOf(function() { 522 parseAsModule("export { true }"); 523 }, SyntaxError); 524 525 assertThrowsInstanceOf(function() { 526 parseAsModule("export { true as name }"); 527 }, SyntaxError); 528 529 assertThrowsInstanceOf(function() { 530 parseAsModule("export { static }"); 531 }, SyntaxError); 532 533 assertThrowsInstanceOf(function() { 534 parseAsModule("export { static as name }"); 535 }, SyntaxError); 536 537 assertThrowsInstanceOf(function () { 538 parseAsModule("export { name } from 'b' f();"); 539 }, SyntaxError); 540 541 assertThrowsInstanceOf(function () { 542 parseAsModule("export *"); 543 }, SyntaxError); 544 545 assertThrowsInstanceOf(function () { 546 parseAsModule("export * from 'b' f();"); 547 }, SyntaxError); 548 549 assertThrowsInstanceOf(function() { 550 parseAsModule("export {}\nfrom ()"); 551 }, SyntaxError); 552 553 assertThrowsInstanceOf(function() { 554 parseAsModule("function() {}"); 555 }, SyntaxError); 556 557 assertThrowsInstanceOf(function() { 558 parseAsModule("class() { constructor() {} }"); 559 }, SyntaxError); 560 561 assertThrowsInstanceOf(function() { 562 parseAsModule("export x"); 563 }, SyntaxError); 564 565 assertThrowsInstanceOf(function() { 566 parseAsModule("export foo = 5"); 567 }, SyntaxError);