Debian Codegraph @

js/ast_test.go

raw
1 package js
2
3 import (
4 "io"
5 "regexp"
6 "testing"
7
8 "github.com/tdewolff/parse/v2"
9 "github.com/tdewolff/test"
10 )
11
12 func TestJS(t *testing.T) {
13 var tests = []struct {
14 js string
15 expected string
16 }{
17 {"if (true) { x(1, 2, 3); }", "if (true) { x(1, 2, 3); }"},
18 {"if (true) { true; }", "if (true) { true; }"},
19 {"if (true) { true; } else { false; }", "if (true) { true; } else { false; }"},
20 {"if (true) { true; } else { if(true) { true; } else { false; } }", "if (true) { true; } else { if (true) { true; } else { false; } }"},
21 {"do { continue; } while (true);", "do { continue; } while (true);"},
22 {"do { x = 1; } while (true);", "do { x = 1; } while (true);"},
23 {"while (true) { true; }", "while (true) { true; }"},
24 {"while (true) { x = 1; }", "while (true) { x = 1; }"},
25 {"for ( ; ; ) { true; }", "for ( ; ; ) { true; }"},
26 {"for (x = 1; ; ) { true; }", "for (x = 1; ; ) { true; }"},
27 {"for (x = 1; x < 2; ) { true; }", "for (x = 1; x < 2; ) { true; }"},
28 {"for (x = 1; x < 2; x++) { true; }", "for (x = 1; x < 2; x++) { true; }"},
29 {"for (x = 1; x < 2; x++) { x = 1; }", "for (x = 1; x < 2; x++) { x = 1; }"},
30 {"for (var x in [1, 2]) { true; }", "for (var x in [1, 2]) { true; }"},
31 {"for (var x in [1, 2]) { x = 1; }", "for (var x in [1, 2]) { x = 1; }"},
32 {"for (const element of [1, 2]) { true; }", "for (const element of [1, 2]) { true; }"},
33 {"for (const element of [1, 2]) { x = 1; }", "for (const element of [1, 2]) { x = 1; }"},
34 {"switch (true) { case true: break; case false: false; }", "switch (true) { case true: break; case false: false; }"},
35 {"switch (true) { case true: x(); break; case false: x(); false; }", "switch (true) { case true: x(); break; case false: x(); false; }"},
36 {"switch (true) { default: false; }", "switch (true) { default: false; }"},
37 {"for (i = 0; i < 3; i++) { continue; }", "for (i = 0; i < 3; i++) { continue; }"},
38 {"for (i = 0; i < 3; i++) { x = 1; }", "for (i = 0; i < 3; i++) { x = 1; }"},
39 {"function f(){return;}", "function f() { return; }"},
40 {"function f(){return 1;}", "function f() { return 1; }"},
41 {"with (true) { true; }", "with (true) { true; }"},
42 {"with (true) { x = 1; }", "with (true) { x = 1; }"},
43 {"loop: for (x = 0; x < 1; x++) { true; }", "loop: for (x = 0; x < 1; x++) { true; }"},
44 {"throw x;", "throw x;"},
45 {"try { true; } catch(e) { }", "try { true; } catch(e) {}"},
46 {"try { true; } catch(e) { true; }", "try { true; } catch(e) { true; }"},
47 {"try { true; } catch(e) { x = 1; }", "try { true; } catch(e) { x = 1; }"},
48 {"debugger;", "debugger;"},
49 {"import * as name from 'module-name';", "import * as name from 'module-name';"},
50 {"import defaultExport from 'module-name';", "import defaultExport from 'module-name';"},
51 {"import * as name from 'module-name';", "import * as name from 'module-name';"},
52 {"import { export1 } from 'module-name';", "import { export1 } from 'module-name';"},
53 {"import { export1 as alias1 } from 'module-name';", "import { export1 as alias1 } from 'module-name';"},
54 {"import { export1 , export2 } from 'module-name';", "import { export1, export2 } from 'module-name';"},
55 {"import { foo , bar } from 'module-name/path/to/specific/un-exported/file';", "import { foo, bar } from 'module-name/path/to/specific/un-exported/file';"},
56 {"import defaultExport, * as name from 'module-name';", "import defaultExport, * as name from 'module-name';"},
57 {"import 'module-name';", "import 'module-name';"},
58 {"var promise = import('module-name');", "var promise = import('module-name');"},
59 {"export { myFunction as default }", "export { myFunction as default };"},
60 {"export default k = 12;", "export default k = 12;"},
61 {"'use strict';", "'use strict';"},
62 {"let [name1, name2 = 6] = z;", "let [name1, name2 = 6] = z;"},
63 {"let {name1, key2: name2} = z;", "let {name1, key2: name2} = z;"},
64 {"let [{name: key, ...rest}, ...[c,d=9]] = z;", "let [{name: key, ...rest}, ...[c, d = 9]] = z;"},
65 {"var x;", "var x;"},
66 {"var x = 1;", "var x = 1;"},
67 {"var x, y = [];", "var x, y = [];"},
68 {"let x;", "let x;"},
69 {"let x = 1;", "let x = 1;"},
70 {"const x = 1;", "const x = 1;"},
71 {"function xyz (a, b) { }", "function xyz(a, b) {}"},
72 {"function xyz (a, b, ...c) { }", "function xyz(a, b, ...c) {}"},
73 {"function xyz (a, b) { }", "function xyz(a, b) {}"},
74 {"class A { field; static get method () { } }", "class A { field; static get method () {} }"},
75 {"class A { field; }", "class A { field; }"},
76 {"class A { field = 5; }", "class A { field = 5; }"},
77 {"class A { field; static get method () { } }", "class A { field; static get method () {} }"},
78 {"class B extends A { field; static get method () { } }", "class B extends A { field; static get method () {} }"},
79
80 {"x = 1;", "x = 1;"},
81 {"'test';", "'test';"},
82 {"[1, 2, 3];", "[1, 2, 3];"},
83 {`x = {x: "value"};`, `x = {x: "value"};`},
84 {`x = {"x": "value"};`, `x = {x: "value"};`},
85 {`x = {"1a": 2};`, `x = {"1a": 2};`},
86 {`x = {x: "value", y: "value"};`, `x = {x: "value", y: "value"};`},
87 {"x = `value`;", "x = `value`;"},
88 {"x = `value${'hi'}`;", "x = `value${'hi'}`;"},
89 {"x = (1 + 1) / 1;", "x = (1 + 1) / 1;"},
90 {"x = y[1];", "x = y[1];"},
91 {"x = y.z;", "x = y.z;"},
92 {"x = new.target;", "x = new.target;"},
93 {"x = import.meta;", "x = import.meta;"},
94 {"x(1, 2);", "x(1, 2);"},
95 {"new x;", "new x();"},
96 {"new x(1);", "new x(1);"},
97 {"new Date().getTime();", "new Date().getTime();"},
98 {"x();", "x();"},
99 {"x = y?.z;", "x = y?.z;"},
100 {"x = -a;", "x = -a;"},
101 {"x = - --a;", "x = - --a;"},
102 {"a << b;", "a << b;"},
103 {"a && b;", "a && b;"},
104 {"a || b;", "a || b;"},
105 {"x = function* foo (x) { while (x < 2) { yield x; x++; } };", "x = function* foo(x) { while (x < 2) { yield x; x++; } };"},
106 {"(x) => { y(); };", "(x) => { y(); };"},
107 {"(x, y) => { z(); };", "(x, y) => { z(); };"},
108 {"async (x, y) => { z(); };", "async (x, y) => { z(); };"},
109 {"await x;", "await x;"},
110 {"export default await x;", "export default await x;"},
111 {"export let a = await x;", "export let a = await x;"},
112 {"if(k00)while((0))", "if (k00) while ((0));"},
113 {"export{};from", "export {}; from;"},
114 {"import{} from 'a'", "import {} from 'a';"},
115 {"import o,{} from''", "import o, {} from '';"},
116 {"if(0)var s;else", "if (0) var s; else;"},
117 {"async\n()", "async();"},
118 {"{};;", "{} ;"},
119 {"{}\n;", "{} ;"},
120 {"- - --3", "- - --3;"},
121 {"([,,])=>P", "([,,]) => { return P; };"},
122 {"(t)=>{//!\n}", "(t) => { //! };"}, // space after //! is newline
123 {"import();", "import();"},
124 {"0\n.k", "(0).k;"},
125 {"do//!\n; while(1)", "//! do; while (1);"}, // space after //! is newline
126 {"//!\nn=>{ return n }", "//! (n) => { return n; };"}, // space after //! is newline
127 {"//!\n{//!\n}", "//! { //! }"}, // space after //! is newline
128 {`for(;;)let = 5`, `for ( ; ; ) { (let = 5); }`},
129 {"{`\n`}", "{ ` `; }"}, // space in template literal is newline
130 {"import.meta;", "import.meta;"},
131 }
132
133 re := regexp.MustCompile("\n *")
134 for _, tt := range tests {
135 t.Run(tt.js, func(t *testing.T) {
136 ast, err := Parse(parse.NewInputString(tt.js), Options{})
137 if err != io.EOF {
138 test.Error(t, err)
139 }
140 src := ast.JSString()
141 src = re.ReplaceAllString(src, " ")
142 test.String(t, src, tt.expected)
143 })
144 }
145 }
146
147 func TestJSON(t *testing.T) {
148 input := `[{"key": [2.5, '\r'], '"': -2E+9}, null, false, true, 5.0e-6, "string", 'stri"ng']`
149 ast, err := Parse(parse.NewInputString(input), Options{})
150 if err != nil {
151 t.Fatal(err)
152 }
153 json, err := ast.JSONString()
154 if err != nil {
155 t.Fatal(err)
156 }
157 test.String(t, json, `[{"key": [2.5, "\r"], "\"": -2E+9}, null, false, true, 5.0e-6, "string", "stri\"ng"]`)
158 }