Skip to content

Commit adbf23d

Browse files
test(js): reach the grammar's remaining branches from the parity corpus
Explicit resource management, pattern and export edge cases, template and number tokens, the start-location option and the semicolon and trailing comma callbacks are now held to acorn as well, which leaves grammar.js and parser.js at 99.5% line coverage from the unit suites alone.
1 parent f58b7fa commit adbf23d

1 file changed

Lines changed: 302 additions & 1 deletion

File tree

‎test/Parser.unittest.js‎

Lines changed: 302 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,14 +516,284 @@ const PROGRAMS = [
516516
"x = a\u212E;",
517517
"x = a\u2E2F;",
518518
"x = \u2E2F;",
519-
"x = \u2E2Fa;"
519+
"x = \u2E2Fa;",
520+
// explicit resource management
521+
"using x = a; await using y = b;",
522+
"function f() { using x = a; using y = b, z = c; }",
523+
"async function f() { await using x = a; }",
524+
"function f() { await using x = a; }",
525+
"function f() { using x; }",
526+
"function f() { using [x] = a; }",
527+
"if (a) using x = b;",
528+
"lbl: using x = y;",
529+
"switch (a) { case 1: using x = y; }",
530+
"switch (a) { case 1: { using x = y; } }",
531+
"for (using x of y) {}",
532+
"async function f() { for (await using x of y) {} }",
533+
"function f() { for (await using x of y) {} }",
534+
"async function f() { for await (using x of y) {} }",
535+
"async function f() { for await (await using x of y) {} }",
536+
"for (using x in y) {}",
537+
"for (using x = a;;) {}",
538+
"for (using x = a, y = b; x; x++) {}",
539+
"for (using of y) {}",
540+
"for (using of = 1;;) break;",
541+
"for (using of == 1;;) break;",
542+
"for (using of => 1;;) break;",
543+
"for (using\nof y) {}",
544+
"for (using in y) {}",
545+
"function f() { using\nx = 1; }",
546+
"using = 1; using(x); using.x; using[0]; using++; using instanceof x;",
547+
"function f() { using \\u0078 = 1; }",
548+
"function f() { using in = 1; }",
549+
"function f() { using await = 1; }",
550+
"async function f() { await using\nx = 1; }",
551+
"async function f() { await using = 1; }",
552+
// destructuring and assignment targets
553+
"({ a = 1 });",
554+
"x = { a = 1 };",
555+
"f({ a = 1 });",
556+
"let { a } = b; let \\u0061 = 1;",
557+
"({...{a}} = b);",
558+
"({ get a() {} } = b);",
559+
"[...a = 1] = b;",
560+
"[a += 1] = b;",
561+
"[(a = 1)] = b;",
562+
"({ a: (b = 1) } = c);",
563+
"(...a, b) => 1;",
564+
"(a, ...b, c) => 1;",
565+
"function f({ a: [b] }, [{ c }]) {}",
566+
"let let = 1;",
567+
"const let = 1;",
568+
"let [let] = a;",
569+
"[a?.b] = c;",
570+
"for (a?.b of c) {}",
571+
"({ a: b?.c } = d);",
572+
"let [a.b] = c;",
573+
"function f([a.b]) {}",
574+
"let [(a)] = b;",
575+
"[(a)] = b; [((a))] = b;",
576+
"x = ((a) = 1);",
577+
"(a = 1) ? b : c;",
578+
"[({ __proto__: 1, __proto__: 2 })] = x;",
579+
"switch ({ __proto__: 1, __proto__: 2 }) {}",
580+
"if ({ __proto__: 1, __proto__: 2 }) {}",
581+
"x = { ['__proto__']: 1, __proto__: 2 };",
582+
"x = { 1: 1, 1: 2, '1': 3 };",
583+
"(a, b,) => 1;",
584+
"[a, b,] = c;",
585+
"async function f() { (await) => 1 }",
586+
"async (await) => 1;",
587+
"async function f() { async (a = await 1) => a }",
588+
"async function f() { ({ await } = x); }",
589+
"async function f() { x = { await }; }",
590+
"async function f() { [await] = x; }",
591+
"x = (); x = (a,); x = (...a);",
592+
"x = { 1 };",
593+
"x = { 'a' };",
594+
"({ a() {} } = b);",
595+
"x = { *a };",
596+
"x = { async a };",
597+
"x = { async *a };",
598+
"x = { get a(b) {} };",
599+
"x = { set a() {} };",
600+
"x = { set a(...b) {} };",
601+
"function f(...a, b) {}",
602+
"let [...a, b] = c;",
603+
// statements
604+
"if (a) { import b from 'c'; }",
605+
"function f() { export const x = 1; }",
606+
"break 1;",
607+
"continue 1;",
608+
"a: { break a; }",
609+
"a: { continue a; }",
610+
"a: b: while (1) { break a; continue b; }",
611+
"a: while (1) { b: { break a; } }",
612+
"async function f() { for await (;;) {} }",
613+
"async function f() { for await (x in y) {} }",
614+
"async function f() { for await (var x = 1;;) {} }",
615+
"async function f() { for await (let x of y) {} }",
616+
"for (let a, b of c) {}",
617+
"for (var a, b in c) {}",
618+
"switch (a) { default: default: }",
619+
"switch (a) { x; }",
620+
"try {}",
621+
"let [a]; let { b };",
622+
"var 1 = 2;",
623+
"{ {} }",
624+
"x = {{}};",
625+
"{ { a } }",
626+
"x = async\n(a) => 1;",
627+
"x = async\nfunction () {};",
628+
"async\n(a);",
629+
// classes
630+
"class A { ; m() {} ; }",
631+
"class A { static 'prototype'() {} }",
632+
"class A { 'constructor' = 1 }",
633+
"class A { *constructor() {} }",
634+
"class A { async constructor() {} }",
635+
"class A { get a(b) {} }",
636+
"class A { set a() {} }",
637+
"class A { set a(...b) {} }",
638+
"class A { constructor = 1 }",
639+
"class A { static prototype = 1 }",
640+
"class A { static constructor = 1 }",
641+
"class {}",
642+
"class A extends B { m() { super; } }",
643+
"class A extends B { m() { super.#a } }",
644+
// modules
645+
"export * as 'string name' from 'x';",
646+
"export * as ns from 5;",
647+
"export * from 5;",
648+
"export { a } from 'b' with { type: 'json' };",
649+
"export { a } from 5;",
650+
"export { 'a' };",
651+
"export { 'a' as b };",
652+
"export default class A {}",
653+
"export default class A extends B {}",
654+
"export const [a, { b }, ...c] = d; export let { e = 1, ...f } = g;",
655+
"export var [a = 1, [b]] = c;",
656+
"export { a, }; var a;",
657+
"export { a as b, c as d, }; var a, c;",
658+
"import { a, } from 'b';",
659+
"import { a as b, } from 'b';",
660+
"import a from 'b' with { type: 'json', };",
661+
"import a from 5;",
662+
"import 5;",
663+
"export { a as '\\uD800' }; var a;",
664+
"export { a as '\\uD83D\\uDE00' }; var a;",
665+
"im\\u0070ort('a');",
666+
"import('x',);",
667+
"import('x', {},);",
668+
"import('x', {}, );",
669+
"import.m\\u0065ta;",
670+
"x = \\u006Eew A;",
671+
"function f() { new.tar\\u0067et }",
672+
"function f() { \\u006Eew.target }",
673+
// tokens
674+
"// a\r\nx = 1;",
675+
"/* a\r\nb */ x = 1;",
676+
"x = 1;\n--> comment\ny = 2;",
677+
"x = 1;\n --> comment",
678+
"/* a */ --> b\nx = 1;",
679+
"x = 1 --> 0;",
680+
"x = a\n--> b",
681+
"x = /a\nb/;",
682+
"x = /a/\\u0067;",
683+
"x = /a/g\\u0069;",
684+
"x = 0x1g;",
685+
"x = 0b12;",
686+
"x = 0o8;",
687+
"x = 0xg;",
688+
"x = 0x;",
689+
"x = 0b;",
690+
"x = 1a;",
691+
"x = 1_a;",
692+
"x = 1.a;",
693+
"x = 1e;",
694+
"x = 1n_;",
695+
"x = 08.5;",
696+
"x = 09n;",
697+
"x = `a\r\nb`;",
698+
"x = `a$b`; x = `$`; x = `a$`;",
699+
"x = tag`\\u{\r\n$x`;",
700+
"x = tag`\\1\n\r\n\r`;",
701+
"x = tag`\\1",
702+
"x = tag`\\1${a}\\2`;",
703+
"x = tag`\\1$`;",
704+
"x = tag`\\1${`;",
705+
"x = '\\r\\t\\b\\v\\f';",
706+
"x = a ? /b/ : /c/; x = [/a/, /=b/];",
707+
"x = a\n/b/\n/c/",
708+
"x = { a: /b/ };",
709+
"x = f(/a/, /b/);",
710+
"x = !/a/;",
711+
"x = typeof /a/;",
712+
"x = `${/a/}`;",
713+
"if (/a/) /b/;",
714+
"switch (/a/) { case /b/: }",
715+
"x = a++ / 2; x = a-- /2/ 1;",
716+
"x = a\n++/b/i.test(c)",
717+
// second pass over the grammar's remaining branches
718+
"let a\\u0062 = 1;",
719+
"function f() { using a\\u0062 = 1; }",
720+
"for (async of x) {}",
721+
"for ((async) of x) {}",
722+
"for (\\u0061sync of x) {}",
723+
"async function f() { for await (using x in y) {} }",
724+
"if (a) function* f() {}",
725+
"label: function* f() {}",
726+
"class A { get; set; }",
727+
"class A { get = 1; set() {} static get; }",
728+
"import a from 'b' with { type: json };",
729+
"async function f() { [\\u0061wait] = 1 }",
730+
"async function f() { ({ \\u0061wait } = 1) }",
731+
"async function f() { \\u0061wait = 1 }",
732+
"async function f() { (\\u0061wait) => 1 }",
733+
"(a.b) => 1;",
734+
"([a.b]) => 1;",
735+
"([(a)]) => 1;",
736+
"({ a: (b) }) => 1;",
737+
"++a?.b;",
738+
"a?.b++;",
739+
"for (a?.b in c) {}",
740+
"([...a.b]) => 1;",
741+
"([...[a]]) => 1;",
742+
"function f(...[a]) {}",
743+
"function f(...{ a }) {}",
744+
"function f(a, b,) {}",
745+
"function f() { return\n{ a } }",
746+
"function f() { return { a } }",
747+
"function* g() { yield\n{ a: 1 } }",
748+
"function* g() { yield { a: 1 } }",
749+
"for (x of\n{}) {}",
750+
"x = !{ __proto__: 1, __proto__: 2 };",
751+
"x = typeof { __proto__: 1, __proto__: 2 };",
752+
"x = a + { __proto__: 1, __proto__: 2 };",
753+
"x = { __proto__: 1, __proto__: 2, a: b = 1 };",
754+
"[(a), b] = c;",
755+
"x = [(a), b];",
756+
"x = #a in b;",
757+
"class A { #a; m() { for (#a in this;;) {} } }",
758+
"class A { #a; m() { x = (#a) in this } }",
759+
"x = [a,].b;",
760+
"x = [a,].b = c;",
761+
"x = (...a);",
762+
"x = (a, ...b);",
763+
"class A extends B { constructor() { new super; } }",
764+
"class A extends B { constructor() { new super(); } }",
765+
"let { ...a, b } = c;",
766+
"function f({ ...a, b }) {}",
767+
"x = { *a: 1 };",
768+
"x = { async a: 1 };",
769+
"let { a() {} } = b;",
770+
"x = { *get a() {} };",
771+
"x = { async get a() {} };",
772+
"x = { await };",
773+
"async ({ await }) => 1;",
774+
"function f() { x = { await }; async () => 1; }",
775+
"x = tag`\\1\\2`;",
776+
"x = tag`\\1\\``;",
777+
"x = `\\8`;",
778+
"x = `\\9`;",
779+
"x = '\\777'; x = '\\400'; x = '\\377';",
780+
"x = '\\7'; x = '\\77'; x = '\\08a';",
781+
"x = a\\b;",
782+
"x = \\x;",
783+
"x = a ? function () {} : b;",
784+
"x = a ? class {} : b;",
785+
"x = { a: function () {} };",
786+
"switch (a) { case function () {}: }",
787+
"x = a ? function : b;",
788+
"x = { a: function };"
520789
];
521790

522791
/** @type {[string, Record<string, unknown>][]} */
523792
const OPTION_SETS = [
524793
["script, latest", { ecmaVersion: "latest", sourceType: "script" }],
525794
["module, latest", { ecmaVersion: "latest", sourceType: "module" }],
526795
["commonjs, latest", { ecmaVersion: "latest", sourceType: "commonjs" }],
796+
["script, es3", { ecmaVersion: 3 }],
527797
["script, es5", { ecmaVersion: 5 }],
528798
["script, es2015", { ecmaVersion: 2015 }],
529799
["script, es2017", { ecmaVersion: 2017 }],
@@ -547,6 +817,14 @@ const OPTION_SETS = [
547817
"script with a source file",
548818
{ ecmaVersion: "latest", locations: true, sourceFile: "a.js" }
549819
],
820+
[
821+
"script starting at a given location",
822+
{
823+
ecmaVersion: "latest",
824+
locations: true,
825+
startLocation: { line: 5, column: 3 }
826+
}
827+
],
550828
[
551829
"script with a direct source file",
552830
{ ecmaVersion: "latest", directSourceFile: "a.js" }
@@ -743,6 +1021,29 @@ describe("Parser", () => {
7431021
);
7441022
});
7451023

1024+
it("reports inserted semicolons and trailing commas like acorn", () => {
1025+
const source = "a\nb\nf(c,)\nx = [d,]\nreturn";
1026+
/** @type {(parse: typeof Parser.parse) => unknown[][]} */
1027+
const eventsOf = (parse) => {
1028+
/** @type {unknown[][]} */
1029+
const events = [];
1030+
parse(source, {
1031+
ecmaVersion: "latest",
1032+
locations: true,
1033+
allowReturnOutsideFunction: true,
1034+
onInsertedSemicolon: (end, loc) =>
1035+
events.push(["semicolon", end, plain(loc)]),
1036+
onTrailingComma: (end, loc) => events.push(["comma", end, plain(loc)])
1037+
});
1038+
return events;
1039+
};
1040+
const ours = eventsOf((code, opts) => Parser.parse(code, opts));
1041+
expect(ours).not.toHaveLength(0);
1042+
expect(ours).toEqual(
1043+
eventsOf((code, opts) => acorn.Parser.parse(code, opts))
1044+
);
1045+
});
1046+
7461047
it("appends to a program node the caller continues", () => {
7471048
const program = acorn.Parser.parse("a;", { ecmaVersion: 2020 });
7481049
const continued = Parser.parse("b;", { ecmaVersion: 2020, program });

0 commit comments

Comments
 (0)