Debian Codegraph @

js/src/jit-test/tests/modules/import-declaration.js

raw
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 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 if (getRealmConfiguration("importAttributes")) {
330 program([
331 importDeclaration(
332 [
333 importSpecifier(
334 ident("default"),
335 ident("a")
336 )
337 ],
338 moduleRequest(
339 lit("b"),
340 []
341 )
342 )
343 ]).assert(parseAsModule("import a from 'b' with {}"));
344
345 program([
346 importDeclaration(
347 [
348 importSpecifier(
349 ident("default"),
350 ident("a")
351 )
352 ],
353 moduleRequest(
354 lit("b"),
355 [
356 importAttribute(ident('type'), lit('js')),
357 ]
358 )
359 )
360 ]).assert(parseAsModule("import a from 'b' with { type: 'js' }"));
361
362 program([
363 importDeclaration(
364 [
365 importSpecifier(
366 ident("default"),
367 ident("a")
368 )
369 ],
370 moduleRequest(
371 lit("b"),
372 [
373 importAttribute(ident('foo'), lit('bar')),
374 ]
375 )
376 )
377 ]).assert(parseAsModule("import a from 'b' with { foo: 'bar' }"));
378
379 program([
380 importDeclaration(
381 [
382 importSpecifier(
383 ident("default"),
384 ident("a")
385 )
386 ],
387 moduleRequest(
388 lit("b"),
389 [
390 importAttribute(ident('foo'), lit('bar')),
391 ]
392 )
393 )
394 ]).assert(parseAsModule(`import a from 'b' with { foo: 'bar' }`));
395
396 // `assert` has NLTH but `with` doesn't
397 program([
398 importDeclaration(
399 [
400 importSpecifier(
401 ident("default"),
402 ident("a")
403 )
404 ],
405 moduleRequest(
406 lit("b"),
407 [
408 importAttribute(ident('foo'), lit('bar')),
409 ]
410 )
411 )
412 ]).assert(parseAsModule(`import a from 'b'
413 with { foo: 'bar' }`));
414
415 program([
416 importDeclaration(
417 [
418 importSpecifier(
419 ident("default"),
420 ident("a")
421 )
422 ],
423 moduleRequest(
424 lit("b"),
425 [
426 importAttribute(ident('type'), lit('js')),
427 importAttribute(ident('foo'), lit('bar')),
428 ]
429 )
430 )
431 ]).assert(parseAsModule("import a from 'b' with { type: 'js', foo: 'bar' }"));
432
433 assertThrowsInstanceOf(function () {
434 parseAsModule("import a from 'b' with { type: type }");
435 }, SyntaxError);
436 }
437
438 var loc = parseAsModule("import { a as b } from 'c'", {
439 loc: true
440 }).body[0].loc;
441
442 assertEq(loc.start.line, 1);
443 assertEq(loc.start.column, 1);
444 assertEq(loc.start.line, 1);
445 assertEq(loc.end.column, 27);
446
447 assertThrowsInstanceOf(function () {
448 parseAsModule("function f() { import a from 'b' }");
449 }, SyntaxError);
450
451 assertThrowsInstanceOf(function () {
452 parseAsModule("if (true) import a from 'b'");
453 }, SyntaxError);
454
455 assertThrowsInstanceOf(function() {
456 parseAsModule("import {");
457 }, SyntaxError);
458
459 assertThrowsInstanceOf(function() {
460 parseAsModule("import {}");
461 }, SyntaxError);
462
463 assertThrowsInstanceOf(function() {
464 parseAsModule("import {} from");
465 }, SyntaxError);
466
467 assertThrowsInstanceOf(function() {
468 parseAsModule("import {,} from 'a'");
469 }, SyntaxError);
470
471 assertThrowsInstanceOf(function() {
472 parseAsModule("import { a as true } from 'b'");
473 }, SyntaxError);
474
475 assertThrowsInstanceOf(function() {
476 parseAsModule("import { true } from 'a'");
477 }, SyntaxError);
478
479 assertThrowsInstanceOf(function() {
480 parseAsModule("import a,");
481 }, SyntaxError);
482
483 assertThrowsInstanceOf(function() {
484 parseAsModule("import a, b from 'a'");
485 }, SyntaxError);
486
487 assertThrowsInstanceOf(function() {
488 parseAsModule("import * as a,");
489 }, SyntaxError);
490
491 assertThrowsInstanceOf(function() {
492 parseAsModule("import * as a, {} from 'a'");
493 }, SyntaxError);
494
495 assertThrowsInstanceOf(function() {
496 parseAsModule("import as a from 'a'");
497 }, SyntaxError);
498
499 assertThrowsInstanceOf(function() {
500 parseAsModule("import * a from 'a'");
501 }, SyntaxError);
502
503 assertThrowsInstanceOf(function() {
504 parseAsModule("import * as from 'a'");
505 }, SyntaxError);
506
507 assertThrowsInstanceOf(function() {
508 parseAsModule("import , {} from 'a'");
509 }, SyntaxError);
510
511 assertThrowsInstanceOf(function() {
512 parseAsModule("import d, from 'a'");
513 }, SyntaxError);
514
515 assertThrowsInstanceOf(function() {
516 parseAsModule("import * as true from 'b'");
517 }, SyntaxError);