Debian Codegraph @

js/parse_test.go

raw
1 package js
2
3 import (
4 "fmt"
5 "io"
6 "sort"
7 "strings"
8 "testing"
9
10 "github.com/tdewolff/parse/v2"
11 "github.com/tdewolff/test"
12 )
13
14 ////////////////////////////////////////////////////////////////
15
16 func TestParse(t *testing.T) {
17 var tests = []struct {
18 js string
19 expected string
20 }{
21 // grammar
22 {"", ""},
23 {"\n", ""},
24 {"#!/usr/bin/env node", "Stmt(#!/usr/bin/env node)"},
25 {"/* comment */", ""},
26 {"/*! comment */", "Stmt(/*! comment */)"},
27 {"5 + /*! comment */ 6", "Stmt(/*! comment */) Stmt(5+6)"},
28 {"var a = /*! comment */ 6", "Stmt(/*! comment */) Decl(var Binding(a = 6))"},
29 {"{}", "Stmt({ })"},
30 {`"use strict"`, `Stmt("use strict")`},
31 {"var a = b;", "Decl(var Binding(a = b))"},
32 {"const a = b;", "Decl(const Binding(a = b))"},
33 {"let a = b;", "Decl(let Binding(a = b))"},
34 {"let [a,b] = [1, 2];", "Decl(let Binding([ Binding(a), Binding(b) ] = [1, 2]))"},
35 {"let [a,[b,c]] = [1, [2, 3]];", "Decl(let Binding([ Binding(a), Binding([ Binding(b), Binding(c) ]) ] = [1, [2, 3]]))"},
36 {"let [,,c] = [1, 2, 3];", "Decl(let Binding([ Binding(), Binding(), Binding(c) ] = [1, 2, 3]))"},
37 {"let [a, ...b] = [1, 2, 3];", "Decl(let Binding([ Binding(a), ...Binding(b) ] = [1, 2, 3]))"},
38 {"let {a, b} = {a: 3, b: 4};", "Decl(let Binding({ Binding(a), Binding(b) } = {a: 3, b: 4}))"},
39 {"let {a: [b, {c}]} = {a: [5, {c: 3}]};", "Decl(let Binding({ a: Binding([ Binding(b), Binding({ Binding(c) }) ]) } = {a: [5, {c: 3}]}))"},
40 {"let [a = 2] = [];", "Decl(let Binding([ Binding(a = 2) ] = []))"},
41 {"let {a: b = 2} = {};", "Decl(let Binding({ a: Binding(b = 2) } = {}))"},
42 {"var a = 5 * 4 / 3 ** 2 + ( 5 - 3 );", "Decl(var Binding(a = (((5*4)/(3**2))+((5-3)))))"},
43 {"var a, b = c;", "Decl(var Binding(a) Binding(b = c))"},
44 {"var a,\nb = c;", "Decl(var Binding(a) Binding(b = c))"},
45 {";", "Stmt()"},
46 {"{; var a = 3;}", "Stmt({ Stmt() Decl(var Binding(a = 3)) })"},
47 {"{a=5}", "Stmt({ Stmt(a=5) })"},
48 {"break", "Stmt(break)"},
49 {"break LABEL", "Stmt(break LABEL)"},
50 {"continue", "Stmt(continue)"},
51 {"continue LABEL", "Stmt(continue LABEL)"},
52 {"if (a == 5) continue LABEL", "Stmt(if (a==5) Stmt(continue LABEL))"},
53 {"if (a == 5) continue LABEL else continue LABEL", "Stmt(if (a==5) Stmt(continue LABEL) else Stmt(continue LABEL))"},
54 {"if (a) b; else if (c) d;", "Stmt(if a Stmt(b) else Stmt(if c Stmt(d)))"},
55 {"if (a) 1; else if (b) 2; else 3", "Stmt(if a Stmt(1) else Stmt(if b Stmt(2) else Stmt(3)))"},
56 {"with (a = 5) continue LABEL", "Stmt(with (a=5) Stmt(continue LABEL))"},
57 {"do a++; while (a < 4)", "Stmt(do Stmt(a++) while (a<4))"},
58 {"do {a++} while (a < 4)", "Stmt(do Stmt({ Stmt(a++) }) while (a<4))"},
59 {"while (a < 4) a++", "Stmt(while (a<4) Stmt(a++))"},
60 {"for (var a = 0; a < 4; a++) b = a", "Stmt(for Decl(var Binding(a = 0)) ; (a<4) ; (a++) Stmt({ Stmt(b=a) }))"},
61 {"for (5; a < 4; a++) {}", "Stmt(for 5 ; (a<4) ; (a++) Stmt({ }))"},
62 {"for (;;) {}", "Stmt(for ; ; Stmt({ }))"},
63 {"for (a,b=5;;) {}", "Stmt(for (a,(b=5)) ; ; Stmt({ }))"},
64 {"for (let a;;) {}", "Stmt(for Decl(let Binding(a)) ; ; Stmt({ }))"},
65 {"for (var a in b) {}", "Stmt(for Decl(var Binding(a)) in b Stmt({ }))"},
66 {"for (var a in b) c", "Stmt(for Decl(var Binding(a)) in b Stmt({ Stmt(c) }))"},
67 {"for (var a of b) {}", "Stmt(for Decl(var Binding(a)) of b Stmt({ }))"},
68 {"for (var a of b) c", "Stmt(for Decl(var Binding(a)) of b Stmt({ Stmt(c) }))"},
69 {"for ((b in c) in d) {}", "Stmt(for ((b in c)) in d Stmt({ }))"},
70 {"for (c && (a in b);;) {}", "Stmt(for (c&&((a in b))) ; ; Stmt({ }))"},
71 {"for (a in b) {}", "Stmt(for a in b Stmt({ }))"},
72 {"for (a = b;;) {}", "Stmt(for (a=b) ; ; Stmt({ }))"},
73 {"for (!a;;) {}", "Stmt(for (!a) ; ; Stmt({ }))"},
74 {"for (var [a] in b) {}", "Stmt(for Decl(var Binding([ Binding(a) ])) in b Stmt({ }))"},
75 {"for await (async of b);", "Stmt(for await async of b Stmt({ }))"},
76 {"for (async of => 5;;);", "Stmt(for (async Params(Binding(of)) => Stmt({ Stmt(return 5) })) ; ; Stmt({ }))"},
77 {"throw 5", "Stmt(throw 5)"},
78 {"try {} catch {b}", "Stmt(try Stmt({ }) catch Stmt({ Stmt(b) }))"},
79 {"try {} finally {c}", "Stmt(try Stmt({ }) finally Stmt({ Stmt(c) }))"},
80 {"try {} catch {b} finally {c}", "Stmt(try Stmt({ }) catch Stmt({ Stmt(b) }) finally Stmt({ Stmt(c) }))"},
81 {"try {} catch (e) {b}", "Stmt(try Stmt({ }) catch Binding(e) Stmt({ Stmt(b) }))"},
82 {"debugger", "Stmt(debugger)"},
83 {"label: var a", "Stmt(label : Decl(var Binding(a)))"},
84 {"yield: var a", "Stmt(yield : Decl(var Binding(a)))"},
85 {"function a(){ await: var a }", "Decl(function a Params() Stmt({ Stmt(await : Decl(var Binding(a))) }))"},
86 {"switch (5) {}", "Stmt(switch 5)"},
87 {"switch (5) { case 3: {} default: {}}", "Stmt(switch 5 Clause(case 3 Stmt({ })) Clause(default Stmt({ })))"},
88 {"function a(b) {}", "Decl(function a Params(Binding(b)) Stmt({ }))"},
89 {"async function a(b) {}", "Decl(async function a Params(Binding(b)) Stmt({ }))"},
90 {"function* a(b) {}", "Decl(function* a Params(Binding(b)) Stmt({ }))"},
91 {"function a(b,) {}", "Decl(function a Params(Binding(b)) Stmt({ }))"},
92 {"function a(b, c) {}", "Decl(function a Params(Binding(b), Binding(c)) Stmt({ }))"},
93 {"function a(...b) {}", "Decl(function a Params(...Binding(b)) Stmt({ }))"},
94 {"function a(b, ...c) {}", "Decl(function a Params(Binding(b), ...Binding(c)) Stmt({ }))"},
95 {"function a(b) {return}", "Decl(function a Params(Binding(b)) Stmt({ Stmt(return) }))"},
96 {"function a(){return 5*3}", "Decl(function a Params() Stmt({ Stmt(return (5*3)) }))"},
97 {"class A {}", "Decl(class A)"},
98 {"class A {;}", "Decl(class A)"},
99 {"!class{}", "Stmt(!Decl(class))"},
100 {"class A extends B { }", "Decl(class A extends B)"},
101 {"class A { a(b) {} }", "Decl(class A Method(a Params(Binding(b)) Stmt({ })))"},
102 {"class A { 5(b) {} }", "Decl(class A Method(5 Params(Binding(b)) Stmt({ })))"},
103 {"class A { 'a'(b) {} }", "Decl(class A Method(a Params(Binding(b)) Stmt({ })))"},
104 {"class A { '5'(b) {} }", "Decl(class A Method(5 Params(Binding(b)) Stmt({ })))"},
105 {"class A { '%'(b) {} }", "Decl(class A Method('%' Params(Binding(b)) Stmt({ })))"},
106 {"class A { get() {} }", "Decl(class A Method(get Params() Stmt({ })))"},
107 {"class A { get a() {} }", "Decl(class A Method(get a Params() Stmt({ })))"},
108 {"class A { set a(b) {} }", "Decl(class A Method(set a Params(Binding(b)) Stmt({ })))"},
109 {"class A { * a(b) {} }", "Decl(class A Method(* a Params(Binding(b)) Stmt({ })))"},
110 {"class A { async a(b) {} }", "Decl(class A Method(async a Params(Binding(b)) Stmt({ })))"},
111 {"class A { async * a(b) {} }", "Decl(class A Method(async * a Params(Binding(b)) Stmt({ })))"},
112 {"class A { static() {} }", "Decl(class A Method(static Params() Stmt({ })))"},
113 {"class A { static a(b) {} }", "Decl(class A Method(static a Params(Binding(b)) Stmt({ })))"},
114 {"class A { [5](b) {} }", "Decl(class A Method([5] Params(Binding(b)) Stmt({ })))"},
115 {"class A { field }", "Decl(class A Field(field))"},
116 {"class A { #field }", "Decl(class A Field(#field))"},
117 {"class A { static }", "Decl(class A Field(static))"},
118 {"class A { static field }", "Decl(class A Field(static field))"},
119 {"class A { field=5 }", "Decl(class A Field(field = 5))"},
120 {"class A { #field=5 }", "Decl(class A Field(#field = 5))"},
121 {"class A { static #field=5 }", "Decl(class A Field(static #field = 5))"},
122 {"class A { #method(){} }", "Decl(class A Method(#method Params() Stmt({ })))"},
123 {"class A { get }", "Decl(class A Field(get))"},
124 {"class A { field static get method(){ return 5 } }", "Decl(class A Field(field) Method(static get method Params() Stmt({ Stmt(return 5) })))"},
125 {"class A { static { this.field = 5 } }", "Decl(class A Static(Stmt({ Stmt((this.field)=5) })))"},
126 {"class A { get #a(){} set #a(x){} }", "Decl(class A Method(get #a Params() Stmt({ })) Method(set #a Params(Binding(x)) Stmt({ })))"},
127 //{"class A { get get get(){} }", "Decl(class A Definition(get) Method(get get Params() Stmt({ })))"}, // doesn't look like this should be supported
128 {"`tmpl`", "Stmt(`tmpl`)"},
129 {"`tmpl${x}`", "Stmt(`tmpl${x}`)"},
130 {"`tmpl${x}tmpl${x}`", "Stmt(`tmpl${x}tmpl${x}`)"},
131 {`import "pkg";`, `Stmt(import "pkg")`},
132 {`import yield from "pkg"`, `Stmt(import yield from "pkg")`},
133 {`import * as yield from "pkg"`, `Stmt(import * as yield from "pkg")`},
134 {`import {yield, for as yield,} from "pkg"`, `Stmt(import { yield , for as yield , } from "pkg")`},
135 {`import yield, * as yield from "pkg"`, `Stmt(import yield , * as yield from "pkg")`},
136 {`import yield, {yield} from "pkg"`, `Stmt(import yield , { yield } from "pkg")`},
137 {`import {yield,} from "pkg"`, `Stmt(import { yield , } from "pkg")`},
138 {`import {"abc'def" as a} from "pkg"`, `Stmt(import { "abc'def" as a } from "pkg")`},
139 {`export * from "pkg";`, `Stmt(export * from "pkg")`},
140 {`export * as for from "pkg"`, `Stmt(export * as for from "pkg")`},
141 {`export * as "abc'def" from "pkg"`, `Stmt(export * as "abc'def" from "pkg")`},
142 {`export {if, for as switch} from "pkg"`, `Stmt(export { if , for as switch } from "pkg")`},
143 {"export {if, for as switch,}", "Stmt(export { if , for as switch , })"},
144 {`export {"abc'def", "a" as 'b'}`, `Stmt(export { "abc'def" , "a" as 'b' })`},
145 {"export var a", "Stmt(export Decl(var Binding(a)))"},
146 {"export function a(b){}", "Stmt(export Decl(function a Params(Binding(b)) Stmt({ })))"},
147 {"export async function a(b){}", "Stmt(export Decl(async function a Params(Binding(b)) Stmt({ })))"},
148 {"export class A{}", "Stmt(export Decl(class A))"},
149 {"export default function(b){}", "Stmt(export default Decl(function Params(Binding(b)) Stmt({ })))"},
150 {"export default async function(b){}", "Stmt(export default Decl(async function Params(Binding(b)) Stmt({ })))"},
151 {"export default class{}", "Stmt(export default Decl(class))"},
152 {"export default a", "Stmt(export default a)"},
153 {"export default async", "Stmt(export default async)"},
154
155 // yield, await, async
156 {"yield\na = 5", "Stmt(yield) Stmt(a=5)"},
157 {"yield * yield * a", "Stmt((yield*yield)*a)"},
158 {"(yield) => 5", "Stmt(Params(Binding(yield)) => Stmt({ Stmt(return 5) }))"},
159 {"function a(){ (await) => 5 }", "Decl(function a Params() Stmt({ Stmt(Params(Binding(await)) => Stmt({ Stmt(return 5) })) }))"},
160 {"async", "Stmt(async)"},
161 {"async = a", "Stmt(async=a)"},
162 {"async\n= a", "Stmt(async=a)"},
163 {"async a => b", "Stmt(async Params(Binding(a)) => Stmt({ Stmt(return b) }))"},
164 {"async (a) => b", "Stmt(async Params(Binding(a)) => Stmt({ Stmt(return b) }))"},
165 {"async(a)", "Stmt(async(a))"},
166 {"async();", "Stmt(async())"},
167 {"async(a=6, ...b)", "Stmt(async((a=6), ...b))"},
168 {"async(function(){})", "Stmt(async(Decl(function Params() Stmt({ }))))"},
169 {"async.value", "Stmt(async.value)"},
170 {"if(a)async.value", "Stmt(if a Stmt(async.value))"},
171 {"function a(){ async\nawait => b }", "Decl(function a Params() Stmt({ Stmt(async) Stmt(Params(Binding(await)) => Stmt({ Stmt(return b) })) }))"},
172 {"a + async\nb", "Stmt(a+async) Stmt(b)"},
173 {"a + async\nfunction f(){}", "Stmt(a+async) Decl(function f Params() Stmt({ }))"},
174 {"class a extends async {}", "Decl(class a extends async)"},
175 {"function*a(){ yield a = 5 }", "Decl(function* a Params() Stmt({ Stmt(yield (a=5)) }))"},
176 {"function*a(){ yield * a = 5 }", "Decl(function* a Params() Stmt({ Stmt(yield* (a=5)) }))"},
177 {"function*a(){ yield\na = 5 }", "Decl(function* a Params() Stmt({ Stmt(yield) Stmt(a=5) }))"},
178 {"function*a(){ yield yield a }", "Decl(function* a Params() Stmt({ Stmt(yield (yield a)) }))"},
179 {"function*a(){ yield * yield * a }", "Decl(function* a Params() Stmt({ Stmt(yield* (yield* a)) }))"},
180 {"function*a(b = yield c){}", "Decl(function* a Params(Binding(b = (yield c))) Stmt({ }))"},
181 {"function*a(){ x = function yield(){} }", "Decl(function* a Params() Stmt({ Stmt(x=Decl(function yield Params() Stmt({ }))) }))"},
182 {"function*a(){ x = function b(){ x = yield } }", "Decl(function* a Params() Stmt({ Stmt(x=Decl(function b Params() Stmt({ Stmt(x=yield) }))) }))"},
183 {"function*a(){ (yield) }", "Decl(function* a Params() Stmt({ Stmt((yield)) }))"},
184 {"function*a(){ (yield a) }", "Decl(function* a Params() Stmt({ Stmt((yield a)) }))"},
185 {"function a(){ let\nawait }", "Decl(function a Params() Stmt({ Decl(let Binding(await)) }))"},
186 {"function*a(){ b => yield%5 }", "Decl(function* a Params() Stmt({ Stmt(Params(Binding(b)) => Stmt({ Stmt(return (yield%5)) })) }))"},
187 {"x = {await}", "Stmt(x={await})"},
188 {"async function a(){ x = {await: 5} }", "Decl(async function a Params() Stmt({ Stmt(x={await: 5}) }))"},
189 {"async function a(){ x = await a }", "Decl(async function a Params() Stmt({ Stmt(x=(await a)) }))"},
190 {"async function a(){ x = await a+y }", "Decl(async function a Params() Stmt({ Stmt(x=((await a)+y)) }))"},
191 {"async function a(b = await c){}", "Decl(async function a Params(Binding(b = (await c))) Stmt({ }))"},
192 {"async function a(){ x = function await(){} }", "Decl(async function a Params() Stmt({ Stmt(x=Decl(function await Params() Stmt({ }))) }))"},
193 {"async function a(){ x = function b(){ x = await } }", "Decl(async function a Params() Stmt({ Stmt(x=Decl(function b Params() Stmt({ Stmt(x=await) }))) }))"},
194 {"async function a(){ for await (var a of b) {} }", "Decl(async function a Params() Stmt({ Stmt(for await Decl(var Binding(a)) of b Stmt({ })) }))"},
195 {"async function a(){ (await a) }", "Decl(async function a Params() Stmt({ Stmt((await a)) }))"},
196 {"x = {async a(b){}}", "Stmt(x={Method(async a Params(Binding(b)) Stmt({ }))})"},
197 {"async(...a)", "Stmt(async(...a))"},
198 {"async(...a,...b)", "Stmt(async(...a, ...b))"},
199
200 // bindings
201 {"let [] = z", "Decl(let Binding([ ] = z))"},
202 {"let [,] = z", "Decl(let Binding([ ] = z))"},
203 {"let [,a] = z", "Decl(let Binding([ Binding(), Binding(a) ] = z))"},
204 {"let [name = 5] = z", "Decl(let Binding([ Binding(name = 5) ] = z))"},
205 {"let [name = 5,] = z", "Decl(let Binding([ Binding(name = 5) ] = z))"},
206 {"let [name = 5,,] = z", "Decl(let Binding([ Binding(name = 5) ] = z))"},
207 {"let [name = 5,, ...yield] = z", "Decl(let Binding([ Binding(name = 5), Binding(), ...Binding(yield) ] = z))"},
208 {"let [...yield] = z", "Decl(let Binding([ ...Binding(yield) ] = z))"},
209 {"let [,,...yield] = z", "Decl(let Binding([ Binding(), Binding(), ...Binding(yield) ] = z))"},
210 {"let [name = 5,, ...[yield]] = z", "Decl(let Binding([ Binding(name = 5), Binding(), ...Binding([ Binding(yield) ]) ] = z))"},
211 {"let [name = 5,, ...{yield}] = z", "Decl(let Binding([ Binding(name = 5), Binding(), ...Binding({ Binding(yield) }) ] = z))"},
212 {"let {} = z", "Decl(let Binding({ } = z))"},
213 {"let {name = 5} = z", "Decl(let Binding({ Binding(name = 5) } = z))"},
214 {"function a(){ let {await = 5} = z }", "Decl(function a Params() Stmt({ Decl(let Binding({ Binding(await = 5) } = z)) }))"},
215 {"let {await: a} = z", "Decl(let Binding({ await: Binding(a) } = z))"},
216 {"let {if: name} = z", "Decl(let Binding({ if: Binding(name) } = z))"},
217 {"let {\"string\": name} = z", "Decl(let Binding({ string: Binding(name) } = z))"},
218 {"let {[a = 5]: name} = z", "Decl(let Binding({ [a=5]: Binding(name) } = z))"},
219 {"let {if: name = 5} = z", "Decl(let Binding({ if: Binding(name = 5) } = z))"},
220 {"let {if: yield = 5} = z", "Decl(let Binding({ if: Binding(yield = 5) } = z))"},
221 {"let {if: [name] = 5} = z", "Decl(let Binding({ if: Binding([ Binding(name) ] = 5) } = z))"},
222 {"let {if: {name} = 5} = z", "Decl(let Binding({ if: Binding({ Binding(name) } = 5) } = z))"},
223 {"let {...yield} = z", "Decl(let Binding({ ...Binding(yield) } = z))"},
224 {"let {if: name, ...yield} = z", "Decl(let Binding({ if: Binding(name), ...Binding(yield) } = z))"},
225 {"let i;for(let i;;);", "Decl(let Binding(i)) Stmt(for Decl(let Binding(i)) ; ; Stmt({ }))"},
226 {"let i;for(let i in x);", "Decl(let Binding(i)) Stmt(for Decl(let Binding(i)) in x Stmt({ }))"},
227 {"let i;for(let i of x);", "Decl(let Binding(i)) Stmt(for Decl(let Binding(i)) of x Stmt({ }))"},
228 {"for(let a in [0,1,2]){let a=5}", "Stmt(for Decl(let Binding(a)) in [0, 1, 2] Stmt({ Decl(let Binding(a = 5)) }))"},
229 {"for(var a in [0,1,2]){let a=5}", "Stmt(for Decl(var Binding(a)) in [0, 1, 2] Stmt({ Decl(let Binding(a = 5)) }))"},
230 {"for(var a in [0,1,2]){var a=5}", "Stmt(for Decl(var Binding(a)) in [0, 1, 2] Stmt({ Decl(var Binding(a = 5)) }))"},
231 {"for(let a=0; a<10; a++){let a=5}", "Stmt(for Decl(let Binding(a = 0)) ; (a<10) ; (a++) Stmt({ Decl(let Binding(a = 5)) }))"},
232 {"for(var a=0; a<10; a++){let a=5}", "Stmt(for Decl(var Binding(a = 0)) ; (a<10) ; (a++) Stmt({ Decl(let Binding(a = 5)) }))"},
233 {"for(var a=0; a<10; a++){var a=5}", "Stmt(for Decl(var Binding(a = 0)) ; (a<10) ; (a++) Stmt({ Decl(var Binding(a = 5)) }))"},
234
235 // expressions
236 {"x = [a, ...b]", "Stmt(x=[a, ...b])"},
237 {"x = [...b]", "Stmt(x=[...b])"},
238 {"x = [...a, ...b]", "Stmt(x=[...a, ...b])"},
239 {"x = [,]", "Stmt(x=[,])"},
240 {"x = [,,]", "Stmt(x=[, ,])"},
241 {"x = [a,]", "Stmt(x=[a])"},
242 {"x = [a,,]", "Stmt(x=[a, ,])"},
243 {"x = [,a]", "Stmt(x=[, a])"},
244 {"x = {a}", "Stmt(x={a})"},
245 {"x = {...a}", "Stmt(x={...a})"},
246 {"x = {a, ...b}", "Stmt(x={a, ...b})"},
247 {"x = {...a, ...b}", "Stmt(x={...a, ...b})"},
248 {"x = {a=5}", "Stmt(x={a = 5})"},
249 {"x = {yield=5}", "Stmt(x={yield = 5})"},
250 {"x = {a:5}", "Stmt(x={a: 5})"},
251 {"x = {yield:5}", "Stmt(x={yield: 5})"},
252 {"x = {async:5}", "Stmt(x={async: 5})"},
253 {"x = {if:5}", "Stmt(x={if: 5})"},
254 {"x = {\"string\":5}", "Stmt(x={string: 5})"},
255 {"x = {3:5}", "Stmt(x={3: 5})"},
256 {"x = {[3]:5}", "Stmt(x={[3]: 5})"},
257 {"x = {a, if: b, do(){}, ...d}", "Stmt(x={a, if: b, Method(do Params() Stmt({ })), ...d})"},
258 {"x = {*a(){}}", "Stmt(x={Method(* a Params() Stmt({ }))})"},
259 {"x = {async*a(){}}", "Stmt(x={Method(async * a Params() Stmt({ }))})"},
260 {"x = {get a(){}}", "Stmt(x={Method(get a Params() Stmt({ }))})"},
261 {"x = {set a(){}}", "Stmt(x={Method(set a Params() Stmt({ }))})"},
262 {"x = {get(){ return 5 }}", "Stmt(x={Method(get Params() Stmt({ Stmt(return 5) }))})"},
263 {"x = {set(){}}", "Stmt(x={Method(set Params() Stmt({ }))})"},
264 {"x = (a, b)", "Stmt(x=((a,b)))"},
265 {"x = function() {}", "Stmt(x=Decl(function Params() Stmt({ })))"},
266 {"x = async function() {}", "Stmt(x=Decl(async function Params() Stmt({ })))"},
267 {"x = class {}", "Stmt(x=Decl(class))"},
268 {"x = class {a(){}}", "Stmt(x=Decl(class Method(a Params() Stmt({ }))))"},
269 {"x = class A { [(0())]; };", "Stmt(x=Decl(class A Field([(0())])))"},
270 {"x = a => a++", "Stmt(x=(Params(Binding(a)) => Stmt({ Stmt(return (a++)) })))"},
271 {"x = a => {a++}", "Stmt(x=(Params(Binding(a)) => Stmt({ Stmt(a++) })))"},
272 {"x = a => {return}", "Stmt(x=(Params(Binding(a)) => Stmt({ Stmt(return) })))"},
273 {"x = a => {return a}", "Stmt(x=(Params(Binding(a)) => Stmt({ Stmt(return a) })))"},
274 {"x = yield => a++", "Stmt(x=(Params(Binding(yield)) => Stmt({ Stmt(return (a++)) })))"},
275 {"x = yield => {a++}", "Stmt(x=(Params(Binding(yield)) => Stmt({ Stmt(a++) })))"},
276 {"x = async a => a++", "Stmt(x=(async Params(Binding(a)) => Stmt({ Stmt(return (a++)) })))"},
277 {"x = async a => {a++}", "Stmt(x=(async Params(Binding(a)) => Stmt({ Stmt(a++) })))"},
278 {"x = async a => await b", "Stmt(x=(async Params(Binding(a)) => Stmt({ Stmt(return (await b)) })))"},
279 {"function a(){ x = await => a++ }", "Decl(function a Params() Stmt({ Stmt(x=(Params(Binding(await)) => Stmt({ Stmt(return (a++)) }))) }))"},
280 {"x = a??b", "Stmt(x=(a??b))"},
281 {"x = a[b]", "Stmt(x=(a[b]))"},
282 {"x = a`tpl`", "Stmt(x=a`tpl`)"},
283 {"x = a.#b", "Stmt(x=(a.#b))"},
284 {"x = a?.b?.c.d", "Stmt(x=(((a?.b)?.c).d))"},
285 {"x = a?.[b]?.`tpl`", "Stmt(x=(a?.[b])?.`tpl`)"},
286 {"x = a?.(b)", "Stmt(x=(a?.(b)))"},
287 {"x = a?.#b", "Stmt(x=(a?.#b))"},
288 {"x = super(a)", "Stmt(x=(super(a)))"},
289 {"x = a(a,b,...c,)", "Stmt(x=(a(a, b, ...c)))"},
290 {"x = a(...a,...b)", "Stmt(x=(a(...a, ...b)))"},
291 {"x = new a", "Stmt(x=(new a))"},
292 {"x = new a()", "Stmt(x=(new a))"},
293 {"x = new a(b)", "Stmt(x=(new a(b)))"},
294 {"x = new a().b(c)", "Stmt(x=(((new a).b)(c)))"},
295 {"x = new a().#b", "Stmt(x=((new a).#b))"},
296 {"x = new new.target", "Stmt(x=(new (new.target)))"},
297 {"x = new import.meta", "Stmt(x=(new (import.meta)))"},
298 {"x = import(a)", "Stmt(x=(import(a)))"},
299 {"import.meta.url", "Stmt((import.meta).url)"},
300 {"import('module')", "Stmt(import('module'))"},
301 {"x = +a", "Stmt(x=(+a))"},
302 {"x = ++a", "Stmt(x=(++a))"},
303 {"x = -a", "Stmt(x=(-a))"},
304 {"x = --a", "Stmt(x=(--a))"},
305 {"x = a--", "Stmt(x=(a--))"},
306 {"x = a<<b", "Stmt(x=(a<<b))"},
307 {"x = a|b", "Stmt(x=(a|b))"},
308 {"x = a&b", "Stmt(x=(a&b))"},
309 {"x = a^b", "Stmt(x=(a^b))"},
310 {"x = a||b", "Stmt(x=(a||b))"},
311 {"x = a&&b", "Stmt(x=(a&&b))"},
312 {"x = !a", "Stmt(x=(!a))"},
313 {"x = delete a", "Stmt(x=(delete a))"},
314 {"x = a in b", "Stmt(x=(a in b))"},
315 {"x = a.replace(b, c)", "Stmt(x=((a.replace)(b, c)))"},
316 {"x &&= a", "Stmt(x&&=a)"},
317 {"x ||= a", "Stmt(x||=a)"},
318 {"x ??= a", "Stmt(x??=a)"},
319 {"class a extends async function(){}{}", "Decl(class a extends Decl(async function Params() Stmt({ })))"},
320 {"x = a?b:c=d", "Stmt(x=(a ? b : (c=d)))"},
321 {"implements = 0", "Stmt(implements=0)"},
322 {"interface = 0", "Stmt(interface=0)"},
323 {"let = 0", "Stmt(let=0)"},
324 {"(let [a] = 0)", "Stmt(((let[a])=0))"},
325 {"package = 0", "Stmt(package=0)"},
326 {"private = 0", "Stmt(private=0)"},
327 {"protected = 0", "Stmt(protected=0)"},
328 {"public = 0", "Stmt(public=0)"},
329 {"static = 0", "Stmt(static=0)"},
330 {"#x in a", "Stmt(#x in a)"},
331
332 // expression to arrow function parameters
333 {"x = (a,b,c) => {a++}", "Stmt(x=(Params(Binding(a), Binding(b), Binding(c)) => Stmt({ Stmt(a++) })))"},
334 {"x = (a,b,...c) => {a++}", "Stmt(x=(Params(Binding(a), Binding(b), ...Binding(c)) => Stmt({ Stmt(a++) })))"},
335 {"x = ([a, ...b]) => {a++}", "Stmt(x=(Params(Binding([ Binding(a), ...Binding(b) ])) => Stmt({ Stmt(a++) })))"},
336 {"x = ([,a,]) => {a++}", "Stmt(x=(Params(Binding([ Binding(), Binding(a) ])) => Stmt({ Stmt(a++) })))"},
337 {"x = ({a}) => {a++}", "Stmt(x=(Params(Binding({ Binding(a) })) => Stmt({ Stmt(a++) })))"},
338 {"x = ({a:b, c:d}) => {a++}", "Stmt(x=(Params(Binding({ a: Binding(b), c: Binding(d) })) => Stmt({ Stmt(a++) })))"},
339 {"x = ({a:[b]}) => {a++}", "Stmt(x=(Params(Binding({ a: Binding([ Binding(b) ]) })) => Stmt({ Stmt(a++) })))"},
340 {"x = ({a=5}) => {a++}", "Stmt(x=(Params(Binding({ Binding(a = 5) })) => Stmt({ Stmt(a++) })))"},
341 {"x = ({...a}) => {a++}", "Stmt(x=(Params(Binding({ ...Binding(a) })) => Stmt({ Stmt(a++) })))"},
342 {"x = ([{...a}]) => {a++}", "Stmt(x=(Params(Binding([ Binding({ ...Binding(a) }) ])) => Stmt({ Stmt(a++) })))"},
343 {"x = ([{a: b}]) => {a++}", "Stmt(x=(Params(Binding([ Binding({ a: Binding(b) }) ])) => Stmt({ Stmt(a++) })))"},
344 {"x = (a = 5) => {a++}", "Stmt(x=(Params(Binding(a = 5)) => Stmt({ Stmt(a++) })))"},
345 {"x = ({a = 5}) => {a++}", "Stmt(x=(Params(Binding({ Binding(a = 5) })) => Stmt({ Stmt(a++) })))"},
346 {`({a:b=1}={})=>b`, `Stmt(Params(Binding({ a: Binding(b = 1) } = {})) => Stmt({ Stmt(return b) }))`},
347
348 // expression precedence
349 {"!!a", "Stmt(!(!a))"},
350 {"x = a.b.c", "Stmt(x=((a.b).c))"},
351 {"x = a+b+c", "Stmt(x=((a+b)+c))"},
352 {"x = a**b**c", "Stmt(x=(a**(b**c)))"},
353 {"a++ < b", "Stmt((a++)<b)"},
354 {"a&&b&&c", "Stmt((a&&b)&&c)"},
355 {"a||b||c", "Stmt((a||b)||c)"},
356 {"new new a(b)", "Stmt(new (new a(b)))"},
357 {"new super.a(b)", "Stmt(new (super.a)(b))"},
358 {"new new.target(a)", "Stmt(new (new.target)(a))"},
359 {"new import.meta(a)", "Stmt(new (import.meta)(a))"},
360 {"a(b)[c]", "Stmt((a(b))[c])"},
361 {"a[b]`tmpl`", "Stmt((a[b])`tmpl`)"},
362 {"a||b?c:d", "Stmt((a||b) ? c : d)"},
363 {"a??b?c:d", "Stmt((a??b) ? c : d)"},
364 {"a==b==c", "Stmt((a==b)==c)"},
365 {"new a?.b", "Stmt((new a)?.b)"},
366 {"new a++", "Stmt((new a)++)"},
367 {"new a--", "Stmt((new a)--)"},
368 {"a<<b<<c", "Stmt((a<<b)<<c)"},
369 {"a&b&c", "Stmt((a&b)&c)"},
370 {"a|b|c", "Stmt((a|b)|c)"},
371 {"a^b^c", "Stmt((a^b)^c)"},
372 {"a,b,c", "Stmt(a,b,c)"},
373
374 // regular expressions
375 {"/abc/", "Stmt(/abc/)"},
376 {"function f(){return /abc/;}", "Decl(function f Params() Stmt({ Stmt(return /abc/) }))"},
377 {"a/b/g", "Stmt((a/b)/g)"},
378 {"{}/1/g", "Stmt({ }) Stmt(/1/g)"},
379 {"i(0)/1/g", "Stmt(((i(0))/1)/g)"},
380 {"if(0)/1/g", "Stmt(if 0 Stmt(/1/g))"},
381 {"a.if(0)/1/g", "Stmt((((a.if)(0))/1)/g)"},
382 {"this/1/g", "Stmt((this/1)/g)"},
383 {"switch(a){case /1/g:}", "Stmt(switch a Clause(case /1/g))"},
384 {"(a+b)/1/g", "Stmt((((a+b))/1)/g)"},
385 {"f(); function foo() {} /42/i", "Stmt(f()) Decl(function foo Params() Stmt({ })) Stmt(/42/i)"},
386 {"x = function() {} /42/i", "Stmt(x=((Decl(function Params() Stmt({ }))/42)/i))"},
387 {"x = function foo() {} /42/i", "Stmt(x=((Decl(function foo Params() Stmt({ }))/42)/i))"},
388 {"x = /foo/", "Stmt(x=/foo/)"},
389 {"x = (/foo/)", "Stmt(x=(/foo/))"},
390 {"x = {a: /foo/}", "Stmt(x={a: /foo/})"},
391 {"x = (a) / foo", "Stmt(x=((a)/foo))"},
392 {"do { /foo/ } while (a)", "Stmt(do Stmt({ Stmt(/foo/) }) while a)"},
393 {"if (true) /foo/", "Stmt(if true Stmt(/foo/))"},
394 {"/abc/ ? /def/ : /geh/", "Stmt(/abc/ ? /def/ : /geh/)"},
395 {"yield * /abc/", "Stmt(yield*/abc/)"},
396
397 // variable reuse
398 {"var a; var a", "Decl(var Binding(a)) Decl(var Binding(a))"},
399 {"var a; {let a}", "Decl(var Binding(a)) Stmt({ Decl(let Binding(a)) })"},
400 {"{let a} var a", "Stmt({ Decl(let Binding(a)) }) Decl(var Binding(a))"},
401 {"function a(b){var b}", "Decl(function a Params(Binding(b)) Stmt({ Decl(var Binding(b)) }))"},
402 {"a=function(b){var b}", "Stmt(a=Decl(function Params(Binding(b)) Stmt({ Decl(var Binding(b)) })))"},
403 {"a=function b(){var b}", "Stmt(a=Decl(function b Params() Stmt({ Decl(var Binding(b)) })))"},
404 {"a=function b(){let b}", "Stmt(a=Decl(function b Params() Stmt({ Decl(let Binding(b)) })))"},
405 {"a=>{var a}", "Stmt(Params(Binding(a)) => Stmt({ Decl(var Binding(a)) }))"},
406 {"try{}catch(a){var a}", "Stmt(try Stmt({ }) catch Binding(a) Stmt({ Decl(var Binding(a)) }))"},
407
408 // ASI
409 {"function f(){return a}", "Decl(function f Params() Stmt({ Stmt(return a) }))"},
410 {"function f(){return; a}", "Decl(function f Params() Stmt({ Stmt(return) Stmt(a) }))"},
411 {"function f(){return\na}", "Decl(function f Params() Stmt({ Stmt(return) Stmt(a) }))"},
412 {"function f(){return /*comment*/ a}", "Decl(function f Params() Stmt({ Stmt(return a) }))"},
413 {"function f(){return /*com\nment*/ a}", "Decl(function f Params() Stmt({ Stmt(return) Stmt(a) }))"},
414 {"function f(){return //comment\n a}", "Decl(function f Params() Stmt({ Stmt(return) Stmt(a) }))"},
415 {"if(a)while(true)\n;else;", "Stmt(if a Stmt(while true Stmt()) else Stmt())"},
416 {"if(a)for(;;)\n;else;", "Stmt(if a Stmt(for ; ; Stmt({ })) else Stmt())"},
417
418 {"a?.b\n`c`", "Stmt((a?.b)`c`)"},
419 {"() => { const v=6; x={v} }", "Stmt(Params() => Stmt({ Decl(const Binding(v = 6)) Stmt(x={v}) }))"},
420 {`([]=l=>{let{e}={e}})`, `Stmt(([]=(Params(Binding(l)) => Stmt({ Decl(let Binding({ Binding(e) } = {e})) }))))`}, // go-fuzz
421 }
422 for _, tt := range tests {
423 t.Run(tt.js, func(t *testing.T) {
424 ast, err := Parse(parse.NewInputString(tt.js), Options{})
425 if err != io.EOF {
426 test.Error(t, err)
427 }
428 test.String(t, ast.String(), tt.expected)
429 })
430 }
431
432 // coverage
433 for i := 0; ; i++ {
434 if OpPrec(i).String() == fmt.Sprintf("Invalid(%d)", i) {
435 break
436 }
437 }
438 for i := 0; ; i++ {
439 if DeclType(i).String() == fmt.Sprintf("Invalid(%d)", i) {
440 break
441 }
442 }
443 }
444
445 func TestParseError(t *testing.T) {
446 var tests = []struct {
447 js string
448 err string
449 }{
450 {"5a", "unexpected identifier after number"},
451 {"{a", "unexpected EOF"},
452 {"if", "expected ( instead of EOF in if statement"},
453 {"if(a", "expected ) instead of EOF in if statement"},
454 {"if(a)let b", "unexpected b in expression"},
455 {"if(a)const b", "unexpected const in statement"},
456 {"with", "expected ( instead of EOF in with statement"},
457 {"with(a", "expected ) instead of EOF in with statement"},
458 {"do a++", "expected while instead of EOF in do-while statement"},
459 {"do a++ while", "unexpected while in expression"},
460 {"do a++; while", "expected ( instead of EOF in do-while statement"},
461 {"do a++; while(a", "expected ) instead of EOF in do-while statement"},
462 {"while", "expected ( instead of EOF in while statement"},
463 {"while(a", "expected ) instead of EOF in while statement"},
464 {"for", "expected ( instead of EOF in for statement"},
465 {"for(a", "expected in, of, or ; instead of EOF in for statement"},
466 {"for(a;a", "expected ; instead of EOF in for statement"},
467 {"for(a;a;a", "expected ) instead of EOF in for statement"},
468 {"for(var [a],b;", "unexpected ; in for statement"},
469 {"for(var [a]=5,{b};", "expected = instead of ; in var statement"},
470 {`for(let{[(`, `unexpected EOF in expression`},
471 {"for(async of b);", "expected => instead of b in arrow function"},
472 {"for await", "expected ( instead of EOF in for statement"},
473 {"function a(){for await", "expected ( instead of await in for statement"},
474 {"async function a(){ for await(a;", "expected of instead of ; in for statement"},
475 {"async function a(){ for await(a in", "expected of instead of in in for statement"},
476 {"for(var a of b", "expected ) instead of EOF in for statement"},
477 {"switch", "expected ( instead of EOF in switch statement"},
478 {"switch(a", "expected ) instead of EOF in switch statement"},
479 {"switch(a)", "expected { instead of EOF in switch statement"},
480 {"switch(a){bad:5}", "expected case or default instead of bad in switch statement"},
481 {"switch(a){case", "unexpected EOF in expression"},
482 {"switch(a){case a", "expected : instead of EOF in switch statement"},
483 {"switch(a){case a:", "unexpected EOF in switch statement"},
484 {"try", "expected { instead of EOF in try statement"},
485 {"try{", "unexpected EOF"},
486 {"try{}", "expected catch or finally instead of EOF in try statement"},
487 {"try{}catch(a", "expected ) instead of EOF in try-catch statement"},
488 {"try{}catch(a,", "expected ) instead of , in try-catch statement"},
489 {"try{}catch", "expected { instead of EOF in try-catch statement"},
490 {"try{}finally", "expected { instead of EOF in try-finally statement"},
491 {"function", "expected Identifier instead of EOF in function declaration"},
492 {"function(", "expected Identifier instead of ( in function declaration"},
493 {"!function", "expected Identifier or ( instead of EOF in function declaration"},
494 {"async function", "expected Identifier instead of EOF in function declaration"},
495 {"function a", "expected ( instead of EOF in function declaration"},
496 {"function a(b", "unexpected EOF in function declaration"},
497 {"function a(b,", "unexpected EOF in function declaration"},
498 {"function a(...b", "expected ) instead of EOF in function declaration"},
499 {"function a()", "expected { instead of EOF in function declaration"},
500 {"class", "expected Identifier instead of EOF in class declaration"},
501 {"class{", "expected Identifier instead of { in class declaration"},
502 {"!class", "expected { instead of EOF in class declaration"},
503 {"class A", "expected { instead of EOF in class declaration"},
504 {"class A{", "unexpected EOF in class declaration"},
505 {"class A extends a b {}", "expected { instead of b in class declaration"},
506 {"class A{+", "expected Identifier, String, Numeric, or [ instead of + in method or field definition"},
507 {"class A{[a", "expected ] instead of EOF in method or field definition"},
508 {"var [...a", "expected ] instead of EOF in array binding pattern"},
509 {"var [a", "expected , or ] instead of EOF in array binding pattern"},
510 {"var [a]", "expected = instead of EOF in var statement"},
511 {"var {[a", "expected ] instead of EOF in object binding pattern"},
512 {"var {+", "expected Identifier, String, Numeric, or [ instead of + in object binding pattern"},
513 {"var {a", "expected , or } instead of EOF in object binding pattern"},
514 {"var {...a", "expected } instead of EOF in object binding pattern"},
515 {"var {a}", "expected = instead of EOF in var statement"},
516 {"var 0", "unexpected 0 in binding"},
517 {"const a", "expected = instead of EOF in const statement"},
518 {"x={", "expected } instead of EOF in object literal"},
519 {"x={[a", "expected ] instead of EOF in object literal"},
520 {"x={[a]", "expected : or ( instead of EOF in object literal"},
521 {"x={+", "expected Identifier, String, Numeric, or [ instead of + in object literal"},
522 {"x={async\na", "unexpected a in object literal"},
523 {"class a extends ||", "unexpected || in expression"},
524 {"class a extends =", "unexpected = in expression"},
525 {"class a extends ?", "unexpected ? in expression"},
526 {"class a extends =>", "unexpected => in expression"},
527 {"x=a?b", "expected : instead of EOF in conditional expression"},
528 {"x=(a", "unexpected EOF in expression"},
529 {"x+(a", "expected ) instead of EOF in expression"},
530 {"x={a", "unexpected EOF in object literal"},
531 {"x=a[b", "expected ] instead of EOF in index expression"},
532 {"x=async a", "expected => instead of EOF in arrow function"},
533 {"x=async (a", "unexpected EOF in expression"},
534 {"x=async (a,", "unexpected EOF in expression"},
535 {"x=async function", "expected Identifier or ( instead of EOF in function declaration"},
536 {"x=async function *", "expected Identifier or ( instead of EOF in function declaration"},
537 {"x=async function a", "expected ( instead of EOF in function declaration"},
538 {"x=?.?.b", "unexpected ?. in expression"},
539 {"x=a?.?.b", "expected Identifier, (, [, or Template instead of ?. in optional chaining expression"},
540 {"x=a?..b", "expected Identifier, (, [, or Template instead of . in optional chaining expression"},
541 {"x=a?.[b", "expected ] instead of EOF in optional chaining expression"},
542 {"`tmp${", "unexpected EOF in expression"},
543 {"`tmp${x", "expected Template instead of EOF in template literal"},
544 {"`tmpl` x `tmpl`", "unexpected x in expression"},
545 {"x=5=>", "unexpected => in expression"},
546 {"x=new.bad", "expected target instead of bad in new.target expression"},
547 {"x=import.bad", "expected meta instead of bad in import.meta expression"},
548 {"x=super", "expected [, (, or . instead of EOF in super expression"},
549 {"x=super(a", "expected , or ) instead of EOF in arguments"},
550 {"x=super[a", "expected ] instead of EOF in index expression"},
551 {"x=super.", "expected Identifier instead of EOF in dot expression"},
552 {"x=new super(b)", "expected [ or . instead of ( in super expression"},
553 {"x=import", "expected ( instead of EOF in import expression"},
554 {"x=import(5", "expected , or ) instead of EOF in arguments"},
555 {"x=new import(b)", "unexpected ( in expression"},
556 {"import", "expected String, Identifier, *, or { instead of EOF in import statement"},
557 {"import *", "expected as instead of EOF in import statement"},
558 {"import * as", "expected Identifier instead of EOF in import statement"},
559 {"import {", "expected } instead of EOF in import statement"},
560 {"import {yield", "expected } instead of EOF in import statement"},
561 {"import {yield as", "expected Identifier instead of EOF in import statement"},
562 {"import {yield,", "expected } instead of EOF in import statement"},
563 {"import yield", "expected from instead of EOF in import statement"},
564 {"import yield,", "expected * or { instead of EOF in import statement"},
565 {"import yield from", "expected String instead of EOF in import statement"},
566 {"export", "expected *, {, var, let, const, function, async, class, or default instead of EOF in export statement"},
567 {"export *", "expected from instead of EOF in export statement"},
568 {"export * as", "expected Identifier or String instead of EOF in export statement"},
569 {"export * as if", "expected from instead of EOF in export statement"},
570 {"export {", "expected } instead of EOF in export statement"},
571 {"export {yield", "expected } instead of EOF in export statement"},
572 {"export {yield,", "expected } instead of EOF in export statement"},
573 {"export {yield as", "expected Identifier or String instead of EOF in export statement"},
574 {"export {} from", "expected String instead of EOF in export statement"},
575 {"export {} from", "expected String instead of EOF in export statement"},
576 {"export async", "expected function instead of EOF in export statement"},
577 {"export default async=>a,b", "unexpected , in expression"},
578 {"throw", "unexpected EOF in expression"},
579 {"throw\n", "unexpected newline in throw statement"},
580 {"#private", "expected in instead of EOF in relational expression"},
581 {"new #private in z", "unexpected #private in expression"},
582 {"new\n#private in z", "unexpected #private in expression"},
583
584 // no declarations
585 {"if(a) function f(){}", "unexpected function in statement"},
586 {"if(a) async function f(){}", "unexpected function in statement"},
587 {"if(a) class c{}", "unexpected class in statement"},
588
589 // yield, async, await
590 {"yield a = 5", "unexpected a in expression"},
591 {"function*a() { yield: var a", "unexpected : in expression"},
592 {"function*a() { x = b + yield c", "unexpected yield in expression"},
593 {"function a(b = yield c){}", "unexpected c in function declaration"},
594 {"function*a(){ (yield) => yield }", "unexpected => in expression"},
595 {"function*a(){ (yield=5) => yield }", "unexpected = in expression"},
596 {"function*a(){ (...yield) => yield }", "unexpected => in expression"},
597 {"function*a(){ yield yield\n?.() }", "unexpected ?. in expression"},
598 {"x = await\n=> a++", "unexpected => in expression"},
599 {"x=async (await,", "unexpected , in expression"},
600 {"async function a() { class a extends await", "unexpected await in expression"},
601 {"async function a() { await: var a", "unexpected : in expression"},
602 {"async function a() { let await", "unexpected await in binding"},
603 {"async function a() { let\nawait", "unexpected await in binding"},
604 {"async function a() { x = new await c", "unexpected await in expression"},
605 {"async function a() { x = await =>", "unexpected => in expression"},
606 {"async function a(){ (await) => await }", "unexpected ) in expression"},
607 {"async function a(){ (await=5) => await }", "unexpected = in expression"},
608 {"async function a(){ (...await) => await }", "unexpected ) in expression"},
609 {"async+a b", "unexpected b in expression"},
610 {"(async\nfunction(){})", "unexpected function in expression"},
611 {"a + async b", "unexpected b in expression"},
612 {"async await => 5", "unexpected await in arrow function"},
613 {"await: var a", "unexpected : in expression"},
614 {"(await) => 5", "unexpected ) in expression"},
615 {"async\nawait => b", "unexpected => in expression"},
616 {"let\nawait", "unexpected await in binding"},
617 {"let {await = 5} = z", "expected : instead of = in object binding"},
618 {"x = await => a++", "unexpected => in expression"},
619 {"function*a(){ b => yield 0 }", "unexpected 0 in expression"},
620
621 // specific cases
622 {"{a, if: b, do(){}, ...d}", "unexpected if in expression"}, // block stmt
623 {"let {if = 5}", "expected : instead of = in object binding pattern"},
624 {"let {...}", "expected Identifier instead of } in object binding pattern"},
625 {"let {...[]}", "expected Identifier instead of [ in object binding pattern"},
626 {"let {...{}}", "expected Identifier instead of { in object binding pattern"},
627 {"for", "expected ( instead of EOF in for statement"},
628 {"for b", "expected ( instead of b in for statement"},
629 {"for (a b)", "expected in, of, or ; instead of b in for statement"},
630 {"for (var a in b;) {}", "expected ) instead of ; in for statement"},
631 {"for (var a,b in c) {}", "unexpected in in for statement"},
632 {"for (var a,b of c) {}", "unexpected of in for statement"},
633 {"if (a) 1 else 3", "unexpected else in expression"},
634 {"x = [...]", "unexpected ] in expression"},
635 {"x = {...}", "unexpected } in expression"},
636 {"let\nawait 0", "unexpected await in binding"},
637 {"const\nawait 0", "unexpected await in binding"},
638 {"var\nawait 0", "unexpected await in binding"},
639 {"function a(){let\nawait 0", "unexpected 0 in let declaration"},
640
641 // expression to arrow function parameters
642 {"x = ()", "expected => instead of EOF in arrow function"},
643 {"x = [x] => a", "unexpected => in expression"},
644 {"x = ((x)) => a", "unexpected => in expression"},
645 {"x = ([...x, y]) => a", "unexpected => in expression"},
646 {"x = ({...x, y}) => a", "unexpected => in expression"},
647 {"x = ({b(){}}) => a", "unexpected => in expression"},
648 {"x = (a, b, ...c)", "expected => instead of EOF in arrow function"},
649 {"x = (a+b) =>", "unexpected => in expression"},
650 {"x = ([...a, b]) =>", "unexpected => in expression"},
651 {"x = ([...5]) =>", "unexpected => in expression"},
652 {"x = ([5]) =>", "unexpected => in expression"},
653 {"x = ({...a, b}) =>", "unexpected => in expression"},
654 {"x = ({...5}) =>", "unexpected => in expression"},
655 {"x = ({5: 5}) =>", "unexpected => in expression"},
656 {"x = ({[4+5]: 5}) =>", "unexpected => in expression"},
657 {"x = (a, a) =>", "unexpected => in expression"},
658 {"x = (a, a, ...a) =>", "unexpected => in expression"},
659 {"x = (a, ...a) =>", "unexpected => in expression"},
660 {"(A,{}%0%{})=>0", "invalid parameters in arrow function"},
661 {"({}``=1)=>0", "invalid parameters in arrow function"},
662
663 // expression precedence
664 {"x = a + yield b", "unexpected b in expression"},
665 {"a??b||c", "unexpected || in expression"},
666 {"a??b&&c", "unexpected && in expression"},
667 {"a||b??c", "unexpected ?? in expression"},
668 {"a&&b??c", "unexpected ?? in expression"},
669 {"x = a++--", "unexpected -- in expression"},
670 {"x = a\n++", "unexpected EOF in expression"},
671 {"x = a++?", "unexpected EOF in expression"},
672 {"a+b =", "unexpected = in expression"},
673 {"!a**b", "unexpected ** in expression"},
674 {"new !a", "unexpected ! in expression"},
675 {"new +a", "unexpected + in expression"},
676 {"new -a", "unexpected - in expression"},
677 {"new ++a", "unexpected ++ in expression"},
678 {"new --a", "unexpected -- in expression"},
679 {"a=>{return a} < b", "unexpected < in expression"},
680 {"a=>{return a} == b", "unexpected == in expression"},
681 {"a=>{return a} . b", "unexpected . in expression"},
682 {"a=>{return a} (", "unexpected ( in expression"},
683 {"a=>{return a} [", "unexpected [ in expression"},
684 {"a=>{return a} '", "unterminated string literal"},
685 {"a=>{return a} `", "unterminated template literal"},
686 {"a=>{return a} ++", "unexpected ++ in expression"},
687 {"a=>{return a} --", "unexpected -- in expression"},
688 {"a=>{return a} * b", "unexpected * in expression"},
689 {"a=>{return a} + b", "unexpected + in expression"},
690 {"a=>{return a} << b", "unexpected << in expression"},
691 {"a=>{return a} & b", "unexpected & in expression"},
692 {"a=>{return a} | b", "unexpected | in expression"},
693 {"a=>{return a} ^ b", "unexpected ^ in expression"},
694 {"a=>{return a} ? b", "unexpected ? in expression"},
695 {"a=>{return a} => b=>b", "unexpected => in expression"},
696 {"class a extends b=>b", "expected { instead of => in class declaration"},
697
698 // regular expressions
699 {"x = x / foo /", "unexpected EOF in expression"},
700 {"bar (true) /foo/", "unexpected EOF in expression"},
701 {"yield /abc/", "unexpected EOF in expression"},
702
703 // variable reuse
704 {"let a; var a", "identifier a has already been declared"},
705 {"let a; {var a}", "identifier a has already been declared"},
706 {"{let a; {var a}}", "identifier a has already been declared"},
707 {"var a; let a", "identifier a has already been declared"},
708 {"{var a} let a", "identifier a has already been declared"},
709 {"var a; const a", "identifier a has already been declared"},
710 {"var a; class a{}", "identifier a has already been declared"},
711 {"function a(b){let b}", "identifier b has already been declared"},
712 {"a=function(b){let b}", "identifier b has already been declared"},
713 {"a=>{let a}", "identifier a has already been declared"},
714 {"let a;function a(){}", "identifier a has already been declared"},
715 {"try{}catch(a){let a}", "identifier a has already been declared"},
716 {"let {a, a}", "identifier a has already been declared"},
717 {"let {a, ...a}", "identifier a has already been declared"},
718 {"for(let a in [0,1,2]){var a = 5}", "identifier a has already been declared"},
719 {"for(let a=0; a<10; a++){var a = 5}", "identifier a has already been declared"},
720 //{"function a(){}; var a", "identifier a has already been declared"},
721 //{"export function a(){}; var a", "identifier a has already been declared"},
722 //{"export default function a(){}; var a", "identifier a has already been declared"},
723
724 // other
725 {"\x00", "unexpected 0x00"},
726 {"@", "unexpected @"},
727 {"\u200F", "unexpected U+200F"},
728 {"\u2010", "unexpected \u2010"},
729 {"a=\u2010", "unexpected \u2010 in expression"},
730 {"/", "unexpected EOF or newline in regular expression"},
731 {"({...[]})=>a", "unexpected => in expression"}, // go-fuzz
732 {`for(;;)let[a]`, `unexpected let [ in single-statement context`}, // go-fuzz
733 }
734 for _, tt := range tests {
735 t.Run(tt.js, func(t *testing.T) {
736 _, err := Parse(parse.NewInputString(tt.js), Options{})
737 test.That(t, err != io.EOF && err != nil)
738
739 e := err.Error()
740 if len(tt.err) < len(err.Error()) {
741 e = e[:len(tt.err)]
742 }
743 test.String(t, e, tt.err)
744 })
745 }
746 }
747
748 type ScopeVars struct {
749 bound, uses string
750 scopes int
751 refs map[*Var]int
752 }
753
754 func NewScopeVars() *ScopeVars {
755 return &ScopeVars{
756 refs: map[*Var]int{},
757 }
758 }
759
760 func (sv *ScopeVars) String() string {
761 return "bound:" + sv.bound + " uses:" + sv.uses
762 }
763
764 func (sv *ScopeVars) Ref(v *Var) int {
765 if ref, ok := sv.refs[v]; ok {
766 return ref
767 }
768 sv.refs[v] = len(sv.refs) + 1
769 return len(sv.refs)
770 }
771
772 func (sv *ScopeVars) AddScope(scope Scope) {
773 if sv.scopes != 0 {
774 sv.bound += "/"
775 sv.uses += "/"
776 }
777 sv.scopes++
778
779 bounds := []string{}
780 for _, v := range scope.Declared {
781 bounds = append(bounds, fmt.Sprintf("%s=%d", string(v.Data), sv.Ref(v)))
782 }
783 sv.bound += strings.Join(bounds, ",")
784
785 uses := []string{}
786 for _, v := range scope.Undeclared {
787 var links strings.Builder
788 for v.Link != nil {
789 v = v.Link
790 links.WriteString("*")
791 }
792 uses = append(uses, fmt.Sprintf("%s=%d%s", string(v.Data), sv.Ref(v), links.String()))
793 }
794 sv.uses += strings.Join(uses, ",")
795 }
796
797 func (sv *ScopeVars) AddExpr(iexpr IExpr) {
798 switch expr := iexpr.(type) {
799 case *FuncDecl:
800 sv.AddScope(expr.Body.Scope)
801 for _, item := range expr.Params.List {
802 if item.Binding != nil {
803 sv.AddBinding(item.Binding)
804 }
805 if item.Default != nil {
806 sv.AddExpr(item.Default)
807 }
808 }
809 if expr.Params.Rest != nil {
810 sv.AddBinding(expr.Params.Rest)
811 }
812 for _, item := range expr.Body.List {
813 sv.AddStmt(item)
814 }
815 case *ClassDecl:
816 sv.AddScope(expr.Scope)
817 for _, item := range expr.List {
818 if item.Method != nil {
819 sv.AddScope(item.Method.Body.Scope)
820 }
821 }
822 case *ArrowFunc:
823 sv.AddScope(expr.Body.Scope)
824 for _, item := range expr.Params.List {
825 if item.Binding != nil {
826 sv.AddBinding(item.Binding)
827 }
828 if item.Default != nil {
829 sv.AddExpr(item.Default)
830 }
831 }
832 if expr.Params.Rest != nil {
833 sv.AddBinding(expr.Params.Rest)
834 }
835 for _, item := range expr.Body.List {
836 sv.AddStmt(item)
837 }
838 case *CondExpr:
839 sv.AddExpr(expr.Cond)
840 sv.AddExpr(expr.X)
841 sv.AddExpr(expr.Y)
842 case *UnaryExpr:
843 sv.AddExpr(expr.X)
844 case *BinaryExpr:
845 sv.AddExpr(expr.X)
846 sv.AddExpr(expr.Y)
847 case *GroupExpr:
848 sv.AddExpr(expr.X)
849 case *CommaExpr:
850 for _, item := range expr.List {
851 sv.AddExpr(item)
852 }
853 case *ObjectExpr:
854 for _, item := range expr.List {
855 if method, ok := item.Value.(*MethodDecl); ok {
856 sv.AddScope(method.Body.Scope)
857 }
858 }
859 }
860 }
861
862 func (sv *ScopeVars) AddBinding(ibinding IBinding) {
863 switch binding := ibinding.(type) {
864 case *BindingArray:
865 for _, item := range binding.List {
866 if item.Binding != nil {
867 sv.AddBinding(item.Binding)
868 }
869 if item.Default != nil {
870 sv.AddExpr(item.Default)
871 }
872 }
873 if binding.Rest != nil {
874 sv.AddBinding(binding.Rest)
875 }
876 case *BindingObject:
877 for _, item := range binding.List {
878 if item.Key.IsComputed() {
879 sv.AddExpr(item.Key.Computed)
880 }
881 if item.Value.Binding != nil {
882 sv.AddBinding(item.Value.Binding)
883 }
884 if item.Value.Default != nil {
885 sv.AddExpr(item.Value.Default)
886 }
887 }
888 }
889 }
890
891 func (sv *ScopeVars) AddStmt(istmt IStmt) {
892 switch stmt := istmt.(type) {
893 case *BlockStmt:
894 sv.AddScope(stmt.Scope)
895 for _, item := range stmt.List {
896 sv.AddStmt(item)
897 }
898 case *FuncDecl:
899 sv.AddScope(stmt.Body.Scope)
900 for _, item := range stmt.Params.List {
901 if item.Binding != nil {
902 sv.AddBinding(item.Binding)
903 }
904 if item.Default != nil {
905 sv.AddExpr(item.Default)
906 }
907 }
908 if stmt.Params.Rest != nil {
909 sv.AddBinding(stmt.Params.Rest)
910 }
911 for _, item := range stmt.Body.List {
912 sv.AddStmt(item)
913 }
914 case *ClassDecl:
915 sv.AddScope(stmt.Scope)
916 for _, item := range stmt.List {
917 if item.Method != nil {
918 sv.AddScope(item.Method.Body.Scope)
919 }
920 }
921 case *ReturnStmt:
922 sv.AddExpr(stmt.Value)
923 case *ThrowStmt:
924 sv.AddExpr(stmt.Value)
925 case *ForStmt:
926 sv.AddStmt(stmt.Body)
927 case *ForInStmt:
928 sv.AddStmt(stmt.Body)
929 case *ForOfStmt:
930 sv.AddStmt(stmt.Body)
931 case *IfStmt:
932 sv.AddStmt(stmt.Body)
933 if stmt.Else != nil {
934 sv.AddStmt(stmt.Else)
935 }
936 case *TryStmt:
937 if 0 < len(stmt.Body.List) {
938 sv.AddStmt(stmt.Body)
939 }
940 if stmt.Catch != nil {
941 sv.AddStmt(stmt.Catch)
942 }
943 if stmt.Finally != nil {
944 sv.AddStmt(stmt.Finally)
945 }
946 case *VarDecl:
947 for _, item := range stmt.List {
948 if item.Default != nil {
949 sv.AddExpr(item.Default)
950 }
951 }
952 case *ExprStmt:
953 sv.AddExpr(stmt.Value)
954 }
955 }
956
957 func TestParseScope(t *testing.T) {
958 // vars registers all bound and unbound variables per scope. Unbound variables are not defined in that particular scope and are defined in another scope (parent, global, child of a parent, ...). Bound variables are variables that are defined in this scope. Each scope is separated by /, and the variables are separated by commas. Each variable is assigned a unique ID (sort by first bounded than unbounded per scope) in order to make sure which identifiers refer to the same variable.
959 // var and function declarations are function-scoped
960 // const, let, and class declarations are block-scoped
961 var tests = []struct {
962 js string
963 bound, uses string
964 }{
965 {"a; a", "", "a=1"},
966 {"a;{a;{a}}", "//", "a=1/a=1/a=1*"},
967 {"var a; b", "a=1", "b=2"},
968 {"var {a:b, c=d, ...e} = z;", "b=1,c=2,e=3", "d=4,z=5"},
969 {"var [a, b=c, ...d] = z;", "a=1,b=2,d=3", "c=4,z=5"},
970 {"x={a:b, c=d, ...e};", "", "x=1,b=2,c=3,d=4,e=5"},
971 {"x=[a, b=c, ...d];", "", "x=1,a=2,b=3,c=4,d=5"},
972 {"yield = 5", "", "yield=1"},
973 {"function a(){ await = 5 }", "a=1/", "await=2/await=2"},
974 {"function a(b,c){var d; e = 5; a}", "a=1/b=3,c=4,d=5", "e=2/e=2,a=1"},
975 {"function a(b,c=b){}", "a=1/b=2,c=3", "/"},
976 {"function a(b=c,c){}", "a=1/b=3,c=4", "c=2/c=2"},
977 {"function a(b=c){var c}", "a=1/b=3,c=4", "c=2/c=2"},
978 {"function a(b){var b}", "a=1/b=2", "/"},
979 {"!function a(b,c){var d; e = 5; a}", "/a=2,b=3,c=4,d=5", "e=1/e=1"},
980 {"a=function(b,c=b){}", "/b=2,c=3", "a=1/"},
981 {"a=function(b=c,c){}", "/b=3,c=4", "a=1,c=2/c=2"},
982 {"a=function(b=c){var c}", "/b=3,c=4", "a=1,c=2/c=2"},
983 {"a=function(b){var b}", "/b=2", "a=1/"},
984 {"export function a(){}", "a=1", ""},
985 {"export default function a(){}", "a=1", ""},
986 {"class a{b(){}}", "a=1//", "//"},
987 {"!class a{b(){}}", "//", "//"},
988 {"class a{#b; b(){ this.#b }}", "a=1/#b=2/", "//#b=2"},
989 {"class a{#b; b(){ this?.#b }}", "a=1/#b=2/", "//#b=2"},
990 {"!class a{#b; b(){ x().#b }}", "/#b=2/", "x=1/x=1/x=1,#b=2"},
991 {"a => a%5", "/a=1", "/"},
992 {"a => a%b", "/a=2", "b=1/b=1"},
993 {"var a;a => a%5", "a=1/a=2", "/"},
994 {"(a) + (a)", "", "a=1"},
995 {"(a,b)", "", "a=1,b=2"},
996 {"(a,b) + (a,b)", "", "a=1,b=2"},
997 {"(a) + (a => a%5)", "/a=2", "a=1/"},
998 {"(a=b) => {var c; d = 5}", "/a=3,c=4", "b=1,d=2/b=1,d=2"},
999 {"(a,b=a) => {}", "/a=1,b=2", "/"},
1000 {"(a=b,b)=>{}", "/a=2,b=3", "b=1/b=1"},
1001 {"a=>{var a}", "/a=1", "/"},
1002 {"(a=b) => {var b}", "/a=2,b=3", "b=1/b=1"},
1003 {"({[a+b]:c}) => {}", "/c=3", "a=1,b=2/a=1,b=2"},
1004 {"({a:b, c=d, ...e}=f) => 5", "/b=3,c=4,e=5", "d=1,f=2/d=1,f=2"},
1005 {"([a, b=c, ...d]=e) => 5", "/a=3,b=4,d=5", "c=1,e=2/c=1,e=2"},
1006 {"(a) + ((b,c) => {var d; e = 5; return e})", "/b=3,c=4,d=5", "a=1,e=2/e=2"},
1007 {"(a) + ((a,b) => {var c; d = 5; return d})", "/a=3,b=4,c=5", "a=1,d=2/d=2"},
1008 {"{(a) + ((a,b) => {var c; d = 5; return d})}", "//a=3,b=4,c=5", "a=1,d=2/a=1,d=2/d=2"},
1009 {"(a=(b=>b/a)) => a", "/a=1/b=2", "//a=1*"},
1010 {"(a=(b=>b/c)) => a", "/a=2/b=3", "c=1/c=1/c=1"},
1011 {"(a=(function b(){})) => a", "/a=1/b=2", "//"},
1012 {"label: a", "", "a=1"},
1013 {"yield => yield%5", "/yield=1", "/"},
1014 {"function a(){ await => await%5 }", "a=1//await=2", "//"},
1015 {"function*a(){ b => { yield%5 } }", "a=1//b=3", "yield=2/yield=2/yield=2"},
1016 {"async function a(){ b => await%5 }", "a=1//b=3", "await=2/await=2/await=2"},
1017 {"let a; {let b = a}", "a=1/b=2", "/a=1"},
1018 {"let a; {var b}", "a=1,b=2/", "/b=2"}, // may remove b from uses
1019 {"let a; {var b = a}", "a=1,b=2/", "/b=2,a=1"},
1020 {"let a; {class b{}}", "a=1/b=2/", "//"},
1021 {"a = 5; var a;", "a=1", ""},
1022 {"a = 5; let a;", "a=1", ""},
1023 {"a = 5; {var a}", "a=1/", "/a=1"},
1024 {"a = 5; {let a}", "/a=2", "a=1/"},
1025 {"{a = 5} var a", "a=1/", "/a=1"},
1026 {"{a = 5} let a", "a=1/", "/a=1"},
1027 {"var a; {a = 5}", "a=1/", "/a=1"},
1028 {"var a; {var a}", "a=1/", "/a=1"},
1029 {"var a; {let a}", "a=1/a=2", "/"},
1030 {"let a; {a = 5}", "a=1/", "/a=1"},
1031 {"{var a} a = 5", "a=1/", "/a=1"},
1032 {"{let a} a = 5", "/a=2", "a=1/"},
1033 {"!function(){throw new Error()}", "/", "Error=1/Error=1"},
1034 {"!function(){return a}", "/", "a=1/a=1"},
1035 {"!function(){return a};var a;", "a=1/", "/a=1"},
1036 {"!function(){return a};if(5){var a}", "a=1//", "/a=1/a=1"},
1037 {"try{}catch(a){var a}", "a=1/a=2", "/a=1"},
1038 {"try{}catch(a){let b; c}", "/a=2,b=3", "c=1/c=1"},
1039 {"try{}catch(a){var b; c}", "b=1/a=3", "c=2/b=1,c=2"},
1040 {"var a;try{}catch(a){a}", "a=1/a=2", "/"},
1041 {"var a;try{}catch(a){var a}", "a=1/a=2", "/a=1"},
1042 {"var a;try{}catch(b){var a}", "a=1/b=2", "/a=1"},
1043 {"function r(o){function l(t){if(!z[t]){if(!o[t]);}}}", "r=1/o=3,l=4/t=5/", "z=2/z=2/z=2,o=3/o=3*,t=5"},
1044 {"function a(){var name;{var name}}", "a=1/name=2/", "//name=2"}, // may remove name from uses
1045 {"function a(){var name;{var name=7}}", "a=1/name=2/", "//name=2"},
1046 {"!function(){a};!function(){a};var a", "a=1//", "/a=1/a=1"},
1047 {"!function(){var a;!function(){a;var a}}", "/a=1/a=2", "//"},
1048 {"!function(){var a;!function(){!function(){a}}}", "/a=1//", "//a=1/a=1*"},
1049 {"!function(){var a;!function(){a;!function(){a}}}", "/a=1//", "//a=1/a=1*"},
1050 {"!function(){var b;{(T=x),T}{var T}}", "/b=2,T=3//", "x=1/x=1/x=1,T=3/T=3"},
1051 {"var T;!function(){var b;{(T=x),T}{var T}}", "T=1/b=3,T=4//", "x=2/x=2/x=2,T=4/T=4"},
1052 {"!function(){let a=b,b=c,c=d,d=e,e=f,f=g,g=h,h=a,j;for(let i=0;;)j=4;}", "/a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,j=9/i=10", "//j=9"},
1053 {"{a} {a} var a", "a=1//", "/a=1/a=1"}, // second block must add a new var in case the block contains a var decl
1054 {"(a),(a)", "", "a=1"}, // second parens could have been arrow function, so must have added new var
1055 {"var a,b,c;(a = b[c])", "a=1,b=2,c=3", ""}, // parens could have been arrow function, so must have added new var
1056 {"!function(a){var b,c;return b?(c=function(){return[a];a.dispatch()},c):t}", "/a=2,b=3,c=4/", "t=1/t=1/a=2*"},
1057 {"(...{a=function(){return [b]}}) => 5", "/a=2/", "b=1/b=1/b=1"},
1058 {"(...[a=function(){return [b]}]) => 5", "/a=2/", "b=1/b=1/b=1"},
1059 {`a=>{for(let b of c){b,a;{var d}}}`, "/a=2,d=3/b=4/", "c=1/c=1/c=1,a=2,d=3/d=3"},
1060 {`var a;{let b;{var a}}`, "a=1/b=2/", "/a=1/a=1"},
1061 {`for(let b of c){let b;{b}}`, "/b=2,b=3/", "c=1/c=1/b=3"},
1062 {`for(var b of c){let b;{b}}`, "b=1/b=3/", "c=2/b=1,c=2/b=3"},
1063 {`for(var b of c){var b;{b}}`, "b=1//", "c=2/b=1,c=2/b=1"},
1064 {`function a(b){for(let c of b){let b;}}`, "a=1/b=2/c=3,b=4", "//b=2"},
1065 {`({a(key){if(x){b.push({key})}}})`, "/key=3", "x=1,b=2/x=1,b=2"},
1066 {`({a(key){if(x){b.push({key:key})}}})`, "/key=3", "x=1,b=2/x=1,b=2"},
1067 }
1068 for _, tt := range tests {
1069 t.Run(tt.js, func(t *testing.T) {
1070 ast, err := Parse(parse.NewInputString(tt.js), Options{})
1071 if err != io.EOF {
1072 test.Error(t, err)
1073 }
1074
1075 vars := NewScopeVars()
1076 vars.AddScope(ast.Scope)
1077 for _, istmt := range ast.List {
1078 vars.AddStmt(istmt)
1079 }
1080 test.String(t, vars.String(), "bound:"+tt.bound+" uses:"+tt.uses)
1081 })
1082 }
1083 }
1084
1085 func TestScope(t *testing.T) {
1086 js := "let a,b; b = 5; var c; {d}{{d}}"
1087 ast, err := Parse(parse.NewInputString(js), Options{})
1088 if err != io.EOF {
1089 test.Error(t, err)
1090 }
1091 scope := ast.Scope
1092
1093 // test output
1094 test.T(t, scope.String(), "Scope{Declared: [Var{LexicalDecl a 0 1}, Var{LexicalDecl b 0 2}, Var{VariableDecl c 0 1}], Undeclared: [Var{NoDecl d 0 2}]}")
1095
1096 // test sort
1097 sort.Sort(VarsByUses(scope.Declared))
1098 test.T(t, scope.String(), "Scope{Declared: [Var{LexicalDecl b 0 2}, Var{LexicalDecl a 0 1}, Var{VariableDecl c 0 1}], Undeclared: [Var{NoDecl d 0 2}]}")
1099
1100 // test variable link
1101 test.T(t, ast.List[3].(*BlockStmt).Scope.String(), "Scope{Declared: [], Undeclared: [Var{NoDecl d 0 2}]}")
1102 test.T(t, ast.List[4].(*BlockStmt).Scope.String(), "Scope{Declared: [], Undeclared: [Var{NoDecl d 0 2}]}")
1103 test.T(t, ast.List[4].(*BlockStmt).List[0].(*BlockStmt).Scope.String(), "Scope{Declared: [], Undeclared: [Var{NoDecl d 1 2}]}")
1104 }
1105
1106 func TestParseInputError(t *testing.T) {
1107 _, err := Parse(parse.NewInput(test.NewErrorReader(0)), Options{})
1108 test.T(t, err, test.ErrPlain)
1109
1110 _, err = Parse(parse.NewInput(test.NewErrorReader(1)), Options{})
1111 test.T(t, err, test.ErrPlain)
1112 }