|
1 load(libdir + "match.js"); |
|
2 load(libdir + "asserts.js"); |
|
3 |
|
4 var { Pattern, MatchError } = Match; |
|
5 |
|
6 program = (elts) => Pattern({ |
|
7 type: "Program", |
|
8 body: elts |
|
9 }) |
|
10 exportDeclaration = (declaration, specifiers, source) => Pattern({ |
|
11 type: "ExportDeclaration", |
|
12 declaration: declaration, |
|
13 specifiers: specifiers, |
|
14 source: source |
|
15 }); |
|
16 exportSpecifier = (id, name) => Pattern({ |
|
17 type: "ExportSpecifier", |
|
18 id: id, |
|
19 name: name |
|
20 }); |
|
21 exportBatchSpecifier = () => Pattern({ |
|
22 type: "ExportBatchSpecifier" |
|
23 }); |
|
24 blockStatement = (body) => Pattern({ |
|
25 type: "BlockStatement", |
|
26 body: body |
|
27 }); |
|
28 functionDeclaration = (id, params, body) => Pattern({ |
|
29 type: "FunctionDeclaration", |
|
30 id: id, |
|
31 params: params, |
|
32 defaults: [], |
|
33 body: body, |
|
34 rest: null, |
|
35 generator: false |
|
36 }); |
|
37 variableDeclaration = (decls) => Pattern({ |
|
38 type: "VariableDeclaration", |
|
39 kind: "var", |
|
40 declarations: decls |
|
41 }); |
|
42 constDeclaration = (decls) => Pattern({ |
|
43 type: "VariableDeclaration", |
|
44 kind: "const", |
|
45 declarations: decls |
|
46 }); |
|
47 letDeclaration = (decls) => Pattern({ |
|
48 type: "VariableDeclaration", |
|
49 kind: "let", |
|
50 declarations: decls |
|
51 }); |
|
52 ident = (name) => Pattern({ |
|
53 type: "Identifier", |
|
54 name: name |
|
55 }); |
|
56 lit = (val) => Pattern({ |
|
57 type: "Literal", |
|
58 value: val |
|
59 }); |
|
60 |
|
61 program([ |
|
62 exportDeclaration( |
|
63 null, |
|
64 [], |
|
65 null |
|
66 ) |
|
67 ]).assert(Reflect.parse("export {}")); |
|
68 |
|
69 program([ |
|
70 exportDeclaration( |
|
71 null, |
|
72 [ |
|
73 exportSpecifier( |
|
74 ident("a"), |
|
75 ident("a") |
|
76 ) |
|
77 ], |
|
78 null |
|
79 ) |
|
80 ]).assert(Reflect.parse("export { a }")); |
|
81 |
|
82 program([ |
|
83 exportDeclaration( |
|
84 null, |
|
85 [ |
|
86 exportSpecifier( |
|
87 ident("a"), |
|
88 ident("b") |
|
89 ) |
|
90 ], |
|
91 null |
|
92 ) |
|
93 ]).assert(Reflect.parse("export { a as b }")); |
|
94 |
|
95 program([ |
|
96 exportDeclaration( |
|
97 null, |
|
98 [ |
|
99 exportSpecifier( |
|
100 ident("as"), |
|
101 ident("as") |
|
102 ) |
|
103 ], |
|
104 null |
|
105 ) |
|
106 ]).assert(Reflect.parse("export { as as as }")); |
|
107 |
|
108 program([ |
|
109 exportDeclaration( |
|
110 null, |
|
111 [ |
|
112 exportSpecifier( |
|
113 ident("a"), |
|
114 ident("true") |
|
115 ) |
|
116 ], |
|
117 null |
|
118 ) |
|
119 ]).assert(Reflect.parse("export { a as true }")); |
|
120 |
|
121 program([ |
|
122 exportDeclaration( |
|
123 null, |
|
124 [ |
|
125 exportSpecifier( |
|
126 ident("a"), |
|
127 ident("a") |
|
128 ), |
|
129 exportSpecifier( |
|
130 ident("b"), |
|
131 ident("b") |
|
132 ), |
|
133 ], |
|
134 null |
|
135 ) |
|
136 ]).assert(Reflect.parse("export { a, b }")); |
|
137 |
|
138 program([ |
|
139 exportDeclaration( |
|
140 null, |
|
141 [ |
|
142 exportSpecifier( |
|
143 ident("a"), |
|
144 ident("b") |
|
145 ), |
|
146 exportSpecifier( |
|
147 ident("c"), |
|
148 ident("d") |
|
149 ), |
|
150 ], |
|
151 null |
|
152 ) |
|
153 ]).assert(Reflect.parse("export { a as b, c as d }")); |
|
154 |
|
155 program([ |
|
156 exportDeclaration( |
|
157 null, |
|
158 [ |
|
159 exportBatchSpecifier() |
|
160 ], |
|
161 null |
|
162 ) |
|
163 ]).assert(Reflect.parse("export *")); |
|
164 |
|
165 program([ |
|
166 exportDeclaration( |
|
167 null, |
|
168 [ |
|
169 exportBatchSpecifier() |
|
170 ], |
|
171 lit("a") |
|
172 ) |
|
173 ]).assert(Reflect.parse("export * from 'a'")); |
|
174 |
|
175 program([ |
|
176 exportDeclaration( |
|
177 functionDeclaration( |
|
178 ident("f"), |
|
179 [], |
|
180 blockStatement([]) |
|
181 ), |
|
182 null, |
|
183 null |
|
184 ) |
|
185 ]).assert(Reflect.parse("export function f() {}")); |
|
186 |
|
187 program([ |
|
188 exportDeclaration( |
|
189 variableDeclaration([ |
|
190 { |
|
191 id: ident("a"), |
|
192 init: lit(1) |
|
193 }, { |
|
194 id: ident("b"), |
|
195 init: lit(2) |
|
196 } |
|
197 ]), |
|
198 null, |
|
199 null |
|
200 ) |
|
201 ]).assert(Reflect.parse("export var a = 1, b = 2;")); |
|
202 |
|
203 program([ |
|
204 exportDeclaration( |
|
205 constDeclaration([ |
|
206 { |
|
207 id: ident("a"), |
|
208 init: lit(1) |
|
209 }, { |
|
210 id: ident("b"), |
|
211 init: lit(2) |
|
212 } |
|
213 ]), |
|
214 null, |
|
215 null |
|
216 ) |
|
217 ]).assert(Reflect.parse("export const a = 1, b = 2;")); |
|
218 |
|
219 // FIXME: In scripts, top level lets are converted back to vars. Fix this when |
|
220 // we implement compiling scripts as modules. |
|
221 program([ |
|
222 exportDeclaration( |
|
223 variableDeclaration([ |
|
224 { |
|
225 id: ident("a"), |
|
226 init: lit(1) |
|
227 }, { |
|
228 id: ident("b"), |
|
229 init: lit(2) |
|
230 } |
|
231 ]), |
|
232 null, |
|
233 null |
|
234 ) |
|
235 ]).assert(Reflect.parse("export let a = 1, b = 2;")); |
|
236 |
|
237 // NOTE: binding lists are treated as if they were let declarations by esprima, |
|
238 // so we follow that convention. |
|
239 program([ |
|
240 exportDeclaration( |
|
241 variableDeclaration([ |
|
242 { |
|
243 id: ident("a"), |
|
244 init: lit(1) |
|
245 }, { |
|
246 id: ident("b"), |
|
247 init: lit(2) |
|
248 } |
|
249 ]), |
|
250 null, |
|
251 null |
|
252 ) |
|
253 ]).assert(Reflect.parse("export a = 1, b = 2;")); |
|
254 |
|
255 var loc = Reflect.parse("export { a as b } from 'c'", { |
|
256 loc: true |
|
257 }).body[0].loc; |
|
258 |
|
259 assertEq(loc.start.line, 1); |
|
260 assertEq(loc.start.column, 0); |
|
261 assertEq(loc.start.line, 1); |
|
262 assertEq(loc.end.column, 26); |
|
263 |
|
264 assertThrowsInstanceOf(function () { |
|
265 Reflect.parse("function f() { export a }"); |
|
266 }, SyntaxError); |
|
267 |
|
268 assertThrowsInstanceOf(function () { |
|
269 Reflect.parse("if (true) export a"); |
|
270 }, SyntaxError); |
|
271 |
|
272 assertThrowsInstanceOf(function() { |
|
273 Reflect.parse("export {"); |
|
274 }, SyntaxError); |
|
275 |
|
276 assertThrowsInstanceOf(function() { |
|
277 Reflect.parse("export {} from"); |
|
278 }, SyntaxError); |
|
279 |
|
280 assertThrowsInstanceOf(function() { |
|
281 Reflect.parse("export {,} from 'a'"); |
|
282 }, SyntaxError); |
|
283 |
|
284 assertThrowsInstanceOf(function() { |
|
285 Reflect.parse("export { true as a } from 'b'"); |
|
286 }, SyntaxError); |
|
287 |
|
288 assertThrowsInstanceOf(function () { |
|
289 Reflect.parse("export { a } from 'b' f();"); |
|
290 }, SyntaxError); |
|
291 |
|
292 assertThrowsInstanceOf(function () { |
|
293 Reflect.parse("export * from 'b' f();"); |
|
294 }, SyntaxError); |
|
295 |
|
296 assertThrowsInstanceOf(function() { |
|
297 Reflect.parse("export {}\nfrom ()"); |
|
298 }, SyntaxError); |