diff --git a/test/paren-correctness.test.js b/test/paren-correctness.test.js deleted file mode 100644 index 907f3a7..0000000 --- a/test/paren-correctness.test.js +++ /dev/null @@ -1,50 +0,0 @@ -// @ts-check -import { expect, test } from 'vitest'; -import { acornParse } from './common.js'; -import { print } from '../src/index.js'; -import ts from '../src/languages/ts/index.js'; - -/** @param {string} input */ -function printed(input) { - const { ast } = acornParse(input); - return print(ast, ts()).code; -} - -// nested same-sign unary operators must not be glued into `--`/`++`, which would -// change the meaning (`-(-a)` -> `--a` is the decrement operator) or be invalid. -// these transform the input, so we also assert the output re-parses cleanly -test.each([ - ['-(-a);', '- -a;'], - ['+(+a);', '+ +a;'], - ['-(--a);', '- --a;'] -])('prints %j as %j without gluing operators', (input, expected) => { - const code = printed(input); - expect(code).toBe(expected); - expect(() => acornParse(code)).not.toThrow(); -}); - -// each of these must round-trip unchanged: dropping or adding parentheses here -// would change meaning or produce invalid code -test.each([ - // a parenthesized optional chain is a separate chain - '(a?.b)();', - '(a?.b).c;', - 'new (a?.b)();', - 'delete (a?.b).c;', - '(a?.b)`x`;', - '(a?.())();', - // `await` is not a valid left operand of `**` - 'async () => (await a) ** b;', - // `?:` is right-associative, so a conditional in the test position keeps its - // parens, else `(a ? b : c) ? d : e` re-associates to `a ? b : (c ? d : e)` - '(a ? b : c) ? d : e;', - '(a ? b : c) ? d : e ? f : g;', - // genuine optional chains must not gain parentheses - 'a?.b.c;', - 'a?.b().c;', - 'a?.b?.c;', - // a conditional in the alternate position needs no parentheses - 'a ? b : c ? d : e;' -])('%j round-trips with correct parentheses', (input) => { - expect(printed(input)).toBe(input); -}); diff --git a/test/samples/parenthesized-expression/expected.js b/test/samples/parenthesized-expression/expected.js index 7788656..dd0bb3d 100644 --- a/test/samples/parenthesized-expression/expected.js +++ b/test/samples/parenthesized-expression/expected.js @@ -8,4 +8,7 @@ a + b; ({}).x; (function () {})(); x = {}; -[a] = b; \ No newline at end of file +[a] = b; +(a ? b : c) ? d : e; +(a ? b : c) ? d : e ? f : g; +a ? b : c ? d : e; \ No newline at end of file diff --git a/test/samples/parenthesized-expression/expected.js.map b/test/samples/parenthesized-expression/expected.js.map index 9ce6c6d..deb5f7c 100644 --- a/test/samples/parenthesized-expression/expected.js.map +++ b/test/samples/parenthesized-expression/expected.js.map @@ -5,7 +5,7 @@ "input.js" ], "sourcesContent": [ - "a + b;\n({} + []);\n(class {});\n(class C {});\n({} ? a : b);\n({} && a);\n(function () {})`x`;\n({}).x;\n(function () {})();\nx = {};\n[a] = b;\n" + "a + b;\n({} + []);\n(class {});\n(class C {});\n({} ? a : b);\n({} && a);\n(function () {})`x`;\n({}).x;\n(function () {})();\nx = {};\n[a] = b;\n(a ? b : c) ? d : e;\n(a ? b : c) ? d : e ? f : g;\na ? b : c ? d : e;\n" ], - "mappings": "AAAA,CAAC,GAAG,CAAC;;CAEJ,MAAM,AAAA,CAAC,AAAA,CAAC;CACR,MAAM,AAAA,CAAC,CAAC,CAAC,AAAA,CAAC;MACL,CAAC,GAAG,CAAC;OACJ,CAAC;CACP,QAAQ,IAAI,CAAC,AAAA,CAAC;KACV,CAAC;CACL,QAAQ,IAAI,CAAC,AAAA,CAAC;AACf,CAAC;CACA,CAAC,IAAI,CAAC" + "mappings": "AAAA,CAAC,GAAG,CAAC;;CAEJ,MAAM,AAAA,CAAC,AAAA,CAAC;CACR,MAAM,AAAA,CAAC,CAAC,CAAC,AAAA,CAAC;MACL,CAAC,GAAG,CAAC;OACJ,CAAC;CACP,QAAQ,IAAI,CAAC,AAAA,CAAC;KACV,CAAC;CACL,QAAQ,IAAI,CAAC,AAAA,CAAC;AACf,CAAC;CACA,CAAC,IAAI,CAAC;CACN,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;CAClB,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC;AAC3B,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC" } \ No newline at end of file diff --git a/test/samples/parenthesized-expression/input.js b/test/samples/parenthesized-expression/input.js index 6cc8b99..8336a14 100644 --- a/test/samples/parenthesized-expression/input.js +++ b/test/samples/parenthesized-expression/input.js @@ -9,3 +9,6 @@ a + b; (function () {})(); x = {}; [a] = b; +(a ? b : c) ? d : e; +(a ? b : c) ? d : e ? f : g; +a ? b : c ? d : e;