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