]> code.delx.au - gnu-emacs-elpa/blob - tests/parser.el
Support ES6 module syntax
[gnu-emacs-elpa] / tests / parser.el
1 ;;; tests/parser.el --- Some tests for js2-mode.
2
3 ;; Copyright (C) 2009, 2011-2013 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
19
20 ;;; Code:
21
22 (require 'ert)
23 (require 'ert-x)
24 (require 'js2-mode)
25
26 (defmacro js2-deftest (name buffer-contents &rest body)
27 `(ert-deftest ,(intern (format "js2-%s" name)) ()
28 (with-temp-buffer
29 (save-excursion
30 (insert ,buffer-contents))
31 (unwind-protect
32 (progn
33 ,@body)
34 (fundamental-mode)))))
35
36 (put 'js2-deftest 'lisp-indent-function 'defun)
37
38 (defun js2-test-string-to-ast (s)
39 (insert s)
40 (js2-mode)
41 (should (null js2-mode-buffer-dirty-p))
42 js2-mode-ast)
43
44 (defun* js2-test-parse-string (code-string &key syntax-error errors-count
45 reference)
46 (ert-with-test-buffer (:name 'origin)
47 (let ((ast (js2-test-string-to-ast code-string)))
48 (if syntax-error
49 (let ((errors (js2-ast-root-errors ast)))
50 (should (= (or errors-count 1) (length errors)))
51 (destructuring-bind (_ pos len) (first errors)
52 (should (string= syntax-error (substring code-string
53 (1- pos) (+ pos len -1))))))
54 (should (= 0 (length (js2-ast-root-errors ast))))
55 (ert-with-test-buffer (:name 'copy)
56 (js2-print-tree ast)
57 (skip-chars-backward " \t\n")
58 (should (string= (or reference code-string)
59 (buffer-substring-no-properties
60 (point-min) (point)))))))))
61
62 (defmacro* js2-deftest-parse (name code-string &key bind syntax-error errors-count
63 reference)
64 "Parse CODE-STRING. If SYNTAX-ERROR is nil, print syntax tree
65 with `js2-print-tree' and assert the result to be equal to
66 REFERENCE, if present, or the original string. If SYNTAX-ERROR
67 is passed, expect syntax error highlighting substring equal to
68 SYNTAX-ERROR value. BIND defines bindings to apply them around
69 the test."
70 `(ert-deftest ,(intern (format "js2-%s" name)) ()
71 (let ,(append bind '((js2-basic-offset 2)))
72 (js2-test-parse-string ,code-string
73 :syntax-error ,syntax-error
74 :errors-count ,errors-count
75 :reference ,reference))))
76
77 (put 'js2-deftest-parse 'lisp-indent-function 'defun)
78
79 ;;; Basics
80
81 (js2-deftest-parse variable-assignment
82 "a = 1;")
83
84 (js2-deftest-parse empty-object-literal
85 "b = {};")
86
87 (js2-deftest-parse empty-array-literal
88 "c = [];")
89
90 (js2-deftest-parse array-with-missing-elements
91 "var a = [1, 2, ,];")
92
93 (js2-deftest-parse comma-after-regexp
94 "d = /eee/, 42;")
95
96 (js2-deftest-parse return-statement
97 "function foo() {\n return 2;\n}")
98
99 (js2-deftest-parse function-statement
100 "function foo() {\n}")
101
102 (js2-deftest-parse function-statement-inside-block
103 "if (true) {\n function foo() {\n }\n}")
104
105 (js2-deftest-parse function-expression-statements-are-verboten
106 "function() {}" :syntax-error "(")
107
108 (js2-deftest-parse member-expr-as-function-name
109 "function a.b.c[2](x, y) {\n}"
110 :bind ((js2-allow-member-expr-as-function-name t)))
111
112 (js2-deftest-parse named-function-expression
113 "a = function b() {};")
114
115 (js2-deftest-parse parenthesized-expression
116 "(1 + 2);")
117
118 (js2-deftest-parse for-with-in-operator-in-parens
119 "for (var y = (0 in []) in {}) {\n}")
120
121 (js2-deftest-parse for-with-in-operator-in-cond
122 "for (var y = 1 ? 0 in [] : false in {}) {\n}")
123
124 (js2-deftest-parse let-expression
125 "(let (x = 42) x);")
126
127 (js2-deftest-parse let-expression-statement
128 "let (x = 42) x;")
129
130 ;;; Callers of `js2-valid-prop-name-token'
131
132 (js2-deftest-parse parse-property-access-when-not-keyword
133 "A.foo = 3;")
134
135 (js2-deftest-parse parse-property-access-when-keyword
136 "A.in = 3;"
137 :bind ((js2-allow-keywords-as-property-names t)))
138
139 (js2-deftest-parse parse-property-access-when-keyword-no-xml
140 "A.in = 3;"
141 :bind ((js2-allow-keywords-as-property-names t)
142 (js2-compiler-xml-available nil)))
143
144 (js2-deftest-parse parse-object-literal-when-not-keyword
145 "a = {b: 1};")
146
147 (js2-deftest-parse parse-object-literal-when-keyword
148 "a = {in: 1};"
149 :bind ((js2-allow-keywords-as-property-names t)))
150
151 ;;; 'of' contextual keyword
152
153 (js2-deftest-parse parse-legacy-array-comp-loop-with-of
154 "[a for (a of [])];")
155
156 (js2-deftest-parse parse-array-comp-loop
157 "[for (a of []) a];")
158
159 (js2-deftest-parse parse-for-of
160 "for (var a of []) {\n}")
161
162 (js2-deftest-parse of-can-be-var-name
163 "var of = 3;")
164
165 (js2-deftest-parse of-can-be-function-name
166 "function of() {\n}")
167
168 ;;; Destructuring binding
169
170 (js2-deftest-parse destruct-in-declaration
171 "var {a, b} = {a: 1, b: 2};")
172
173 (js2-deftest-parse destruct-in-arguments
174 "function f({a: aa, b: bb}) {\n}")
175
176 (js2-deftest-parse destruct-in-array-comp-loop
177 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
178
179 (js2-deftest-parse destruct-in-catch-clause
180 "try {\n} catch ({a, b}) {\n a + b;\n}")
181
182 ;;; Object literals
183
184 (js2-deftest-parse object-literal-shorthand
185 "var x = {a: 1, b, c: 1, d};")
186
187 (js2-deftest-parse object-literal-shorthard-with-number
188 "var a = {1};" :syntax-error ";" :errors-count 2)
189
190 (js2-deftest-parse object-literal-method
191 "var x = {f(y) { return y;\n}};")
192
193 (js2-deftest-parse object-literal-getter-method
194 "var x = {get f() { return 42;\n}};")
195
196 (js2-deftest-parse object-literal-setter-method
197 "var x = {set f(y) { x = y;\n}};")
198
199 (js2-deftest-parse object-literal-computed-keys
200 "var x = {[Symbol.iterator]: function() {}};")
201
202 ;;; Function definition
203
204 (js2-deftest function-redeclaring-var "var gen = 3; function gen() {};"
205 (js2-mode)
206 (should (= (length (js2-ast-root-warnings js2-mode-ast)) 1)))
207
208 (js2-deftest function-expression-var-same-name "var gen = function gen() {};"
209 (js2-mode)
210 (should (null (js2-ast-root-warnings js2-mode-ast))))
211
212 ;;; Function parameters
213
214 (js2-deftest-parse function-with-default-parameters
215 "function foo(a = 1, b = a + 1) {\n}")
216
217 (js2-deftest-parse function-with-no-default-after-default
218 "function foo(a = 1, b) {\n}"
219 :syntax-error "b")
220
221 (js2-deftest-parse function-with-destruct-after-default
222 "function foo(a = 1, {b, c}) {\n}"
223 :syntax-error "{")
224
225 (js2-deftest-parse function-with-rest-parameter
226 "function foo(a, b, ...rest) {\n}")
227
228 (js2-deftest-parse function-with-param-after-rest-parameter
229 "function foo(a, ...b, rest) {\n}"
230 :syntax-error "rest")
231
232 (js2-deftest-parse function-with-destruct-after-rest-parameter
233 "function foo(a, ...b, {}) {\n}"
234 :syntax-error "{}")
235
236 (js2-deftest-parse function-with-rest-after-default-parameter
237 "function foo(a = 1, ...rest) {\n}")
238
239 ;;; Spread operator
240
241 (js2-deftest-parse spread-in-array-literal
242 "[1, ...[2, 3], 4, ...[5, 6]];")
243
244 (js2-deftest-parse spread-in-function-call
245 "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")
246
247 ;;; Arrow functions
248
249 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
250 "() => false;" :reference "() => {false};")
251
252 (js2-deftest-parse arrow-function-with-args-and-curlies
253 "(a, b = 1, ...c) => { c;\n};")
254
255 (js2-deftest-parse arrow-function-with-destructuring
256 "([{a}, b]) => { a + b;\n};")
257
258 (js2-deftest-parse parenless-arrow-function-prohibits-rest
259 "...b => {b + 1;};" :syntax-error "=>" :errors-count 1)
260
261 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
262 "[a, b] => {a + b;};" :syntax-error "=>" :errors-count 4)
263
264 ;;; Automatic semicolon insertion
265
266 (js2-deftest-parse no-auto-semi-insertion-after-if
267 "if (true) {\n}")
268
269 (js2-deftest-parse auto-semi-insertion-after-function
270 "a = function() {}" :reference "a = function() {};")
271
272 (js2-deftest-parse auto-semi-one-variable-per-line
273 "x\ny" :reference "x;\ny;")
274
275 ;;; Labels
276
277 (js2-deftest-parse labeled-stmt-node
278 "foo:\nbar:\nx = y + 1;")
279
280 (js2-deftest no-label-node-inside-expr "x = y:"
281 (let (js2-parse-interruptable-p)
282 (js2-mode))
283 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
284 (should (js2-name-node-p (js2-assign-node-right assignment)))))
285
286 (js2-deftest-parse label-and-loops "for (; ; ) {
287 loop:
288 for (; ; ) {
289 continue loop;
290 }
291 }")
292
293 ;;; Generators
294
295 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
296
297 (js2-deftest-parse legacy-generator-cannot-return
298 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
299
300 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
301
302 (js2-deftest-parse harmony-generator-yield-star "(function*(a) { yield* a;\n});")
303
304 ;;; Comprehensions
305
306 (js2-deftest-parse parse-legacy-array-comp-loop-with-filter
307 "[a for (a in b) if (a == 2)];")
308
309 (js2-deftest-parse parse-array-comp-loop-with-filters
310 "[for (a in b) if (a == 2) if (b != 10) a];")
311
312 (js2-deftest-parse parse-generator-comp-loop-with-filters
313 "(for (x of y) if (x != 4) x);")
314
315 (js2-deftest-parse parse-array-comp-with-yield-is-ok
316 "(function() { return [for (x of []) yield x];\n});")
317
318 (js2-deftest-parse parse-generator-comp-with-yield-is-not-ok
319 "(function() { return (for (x of []) yield x);\n});"
320 :syntax-error "yield")
321
322 (js2-deftest-parse parse-generator-comp-with-yield-inside-function-is-ok
323 "(for (x of []) function*() { yield x;\n});")
324
325 ;;; Numbers
326
327 (js2-deftest-parse decimal-starting-with-zero "081;" :reference "81;")
328
329 (js2-deftest-parse huge-hex "0x0123456789abcdefABCDEF;" :reference "-1;")
330
331 (js2-deftest-parse octal-without-o "071;" :reference "57;")
332
333 (js2-deftest-parse hex-number-okay "0x123;" :reference "291;")
334
335 (js2-deftest-parse hex-number-broken "0xz23;"
336 :syntax-error "0xz" :errors-count 2)
337
338 (js2-deftest-parse binary-number-okay "0b101;" :reference "5;")
339
340 (js2-deftest-parse binary-number-broken "0b210;"
341 :syntax-error "0b2" :errors-count 2)
342
343 (js2-deftest-parse octal-number-okay "0o765;" :reference "501;")
344
345 (js2-deftest-parse octal-number-broken "0o812;"
346 :syntax-error "0o8" :errors-count 2)
347
348 ;;; Modules
349
350 (js2-deftest parse-export-bindings "{one, two as dos}"
351 (js2-init-scanner)
352 (should (js2-match-token js2-LC))
353 (let ((imports (js2-parse-export-bindings)))
354 (should (not (equal nil imports)))
355 (should (= 2 (length imports)))
356 (let ((first (nth 0 imports))
357 (second (nth 1 imports)))
358 (should (equal "one" (js2-name-node-name (js2-export-binding-node-extern-name first))))
359 (should (equal "two" (js2-name-node-name (js2-export-binding-node-extern-name second))))
360 (let ((first-name (js2-export-binding-node-local-name first))
361 (second-name (js2-export-binding-node-local-name second)))
362 (should (equal first (js2-node-parent first-name)))
363 (should (equal 3 (js2-node-len first-name)))
364 (should (equal "one" (js2-name-node-name first-name)))
365 (should (equal second (js2-node-parent second-name)))
366 (should (equal 3 (js2-node-len second-name)))
367 (should (equal "dos" (js2-name-node-name second-name)))))))
368
369 (js2-deftest parse-export-binding-as-default "one as default"
370 (js2-init-scanner)
371 (let ((binding (js2-maybe-parse-export-binding)))
372 (should binding)
373 (should (js2-export-binding-node-p binding))
374 (let ((name (js2-export-binding-node-local-name binding)))
375 (should name)
376 (should (equal "default" (js2-name-node-name name))))))
377
378 (js2-deftest parse-namepsace-import "* as lib;"
379 (js2-init-scanner)
380 (should (js2-match-token js2-MUL))
381 (let ((namespace-import (js2-parse-namespace-import)))
382 (should (not (equal nil namespace-import)))
383 (should (js2-namespace-import-node-p namespace-import))
384 (should (= 1 (js2-node-pos namespace-import)))
385 (should (equal 8 (js2-node-len namespace-import)))
386 (let ((name-node (js2-namespace-import-node-name namespace-import)))
387 (should (equal "lib" (js2-name-node-name name-node)))
388 (should (= 5 (js2-node-pos name-node))))))
389
390 (js2-deftest parse-from-clause "from 'foo/bar';"
391 (js2-init-scanner)
392 (let ((from (js2-parse-from-clause)))
393 (should (not (equal nil from)))
394 (should (= 1 (js2-node-pos from)))
395 (should (= 14 (js2-node-len from)))
396 (should (equal "foo/bar" (js2-from-clause-node-module-id from)))))
397
398 (js2-deftest parse-import-module-id-only "import 'src/lib'"
399 (js2-init-scanner)
400 (should (js2-match-token js2-IMPORT))
401 (let ((import (js2-parse-import)))
402 (should (not (equal nil import)))
403 (should (= 1 (js2-node-pos import)))
404 (should (= 16 (js2-node-len import)))
405 (should (equal nil (js2-import-node-import import)))
406 (should (equal nil (js2-import-node-from import)))))
407
408 (js2-deftest parse-imported-default-binding "import theDefault from 'src/lib'"
409 (js2-push-scope (make-js2-scope :pos 0))
410 (js2-init-scanner)
411 (should (js2-match-token js2-IMPORT))
412 (let ((import-node (js2-parse-import)))
413 (should (not (equal nil import-node)))
414 (should (equal "src/lib" (js2-import-node-module-id import-node)))
415 (let ((import (js2-import-node-import import-node)))
416 (should (not (equal nil import)))
417 (should (equal nil (js2-import-clause-node-namespace-import import)))
418 (should (equal nil (js2-import-clause-node-named-imports import)))
419 (let ((default (js2-import-clause-node-default-binding import)))
420 (should (not (equal nil default)))
421 (should (js2-export-binding-node-p default))
422 (should (equal "theDefault" (js2-name-node-name (js2-export-binding-node-extern-name default)))))))
423 (should (js2-scope-get-symbol js2-current-scope "theDefault")))
424
425 (js2-deftest parse-import-namespace-binding "import * as lib from 'src/lib'"
426 (js2-push-scope (make-js2-scope :pos 0))
427 (js2-init-scanner)
428 (should (js2-match-token js2-IMPORT))
429 (let ((import-node (js2-parse-import)))
430 (should (not (equal nil import-node)))
431 (should (equal "src/lib" (js2-import-node-module-id import-node)))
432 (let ((import (js2-import-node-import import-node)))
433 (should (not (equal nil import)))
434 (should (equal nil (js2-import-clause-node-default-binding import)))
435 (should (equal nil (js2-import-clause-node-named-imports import)))
436 (let ((ns-import (js2-import-clause-node-namespace-import import)))
437 (should (not (equal nil ns-import)))
438 (should (js2-namespace-import-node-p ns-import))
439 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
440 (should (js2-scope-get-symbol js2-current-scope "lib")))
441
442 (js2-deftest parse-import-named-imports "import {foo as bar, baz} from 'src/lib'"
443 (js2-push-scope (make-js2-scope :pos 0))
444 (js2-init-scanner)
445 (should (js2-match-token js2-IMPORT))
446 (let ((import-node (js2-parse-import)))
447 (should (not (equal nil import-node)))
448 (should (equal "src/lib" (js2-import-node-module-id import-node)))
449 (let ((import (js2-import-node-import import-node)))
450 (should (not (equal nil import)))
451 (should (equal nil (js2-import-clause-node-default-binding import)))
452 (should (equal nil (js2-import-clause-node-namespace-import import)))
453 (let ((named-imports (js2-import-clause-node-named-imports import)))
454 (should (not (equal nil named-imports)))
455 (should (listp named-imports))
456 (should (= 2 (length named-imports)))
457 (let ((first (nth 0 named-imports))
458 (second (nth 1 named-imports)))
459 (should (equal "bar" (js2-name-node-name (js2-export-binding-node-local-name first))))
460 (should (equal "baz" (js2-name-node-name (js2-export-binding-node-local-name second))))))))
461 (should (js2-scope-get-symbol js2-current-scope "bar"))
462 (should (js2-scope-get-symbol js2-current-scope "baz")))
463
464 (js2-deftest parse-import-default-and-namespace "import stuff, * as lib from 'src/lib'"
465 (js2-push-scope (make-js2-scope :pos 0))
466 (js2-init-scanner)
467 (should (js2-match-token js2-IMPORT))
468 (let ((import-node (js2-parse-import)))
469 (should (not (equal nil import-node)))
470 (should (equal "src/lib" (js2-import-node-module-id import-node)))
471 (let ((import (js2-import-node-import import-node)))
472 (should (not (equal nil import)))
473 (should (equal nil (js2-import-clause-node-named-imports import)))
474 (let ((default (js2-import-clause-node-default-binding import))
475 (ns-import (js2-import-clause-node-namespace-import import)))
476 (should (not (equal nil default)))
477 (should (equal "stuff" (js2-name-node-name (js2-export-binding-node-local-name default))))
478 (should (not (equal nil ns-import)))
479 (should (js2-namespace-import-node-p ns-import))
480 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
481 (should (js2-scope-get-symbol js2-current-scope "stuff"))
482 (should (js2-scope-get-symbol js2-current-scope "lib")))
483
484 (js2-deftest parse-import-default-and-named-imports
485 "import robert as bob, {cookies, pi as PIE} from 'src/lib'"
486 (js2-push-scope (make-js2-scope :pos 0))
487 (js2-init-scanner)
488 (should (js2-match-token js2-IMPORT))
489 (let ((import-node (js2-parse-import)))
490 (should (not (equal nil import-node)))
491 (should (equal "src/lib" (js2-import-node-module-id import-node)))
492 (let ((import (js2-import-node-import import-node)))
493 (should (not (equal nil import)))
494 (should (not (equal nil (js2-import-clause-node-named-imports import))))
495 (let ((default (js2-import-clause-node-default-binding import))
496 (named-imports (js2-import-clause-node-named-imports import)))
497 (should (not (equal nil default)))
498 (should (equal "bob" (js2-name-node-name (js2-export-binding-node-local-name default))))
499 (should (not (equal nil named-imports)))
500 (should (= 2 (length named-imports))))))
501 (should (js2-scope-get-symbol js2-current-scope "bob"))
502 (should (js2-scope-get-symbol js2-current-scope "cookies"))
503 (should (js2-scope-get-symbol js2-current-scope "PIE")))
504
505 (js2-deftest parse-this-module-in-from-clause "import {url} from this module;"
506 (js2-push-scope (make-js2-scope :pos 0))
507 (js2-init-scanner)
508 (should (js2-match-token js2-IMPORT))
509 (let ((import-node (js2-parse-import)))
510 (should import-node)
511 (let ((from-clause (js2-import-node-from import-node)))
512 (should from-clause)
513 (should (equal "this" (js2-from-clause-node-module-id from-clause)))
514 (should (js2-from-clause-node-metadata-p from-clause)))))
515
516 (js2-deftest-parse import-only-for-side-effects "import 'src/lib';")
517 (js2-deftest-parse import-default-only "import theDefault from 'src/lib';")
518 (js2-deftest-parse import-named-only "import {one, two} from 'src/lib';")
519 (js2-deftest-parse import-default-and-named "import theDefault, {one, two} from 'src/lib';")
520 (js2-deftest-parse import-renaming-default "import * as lib from 'src/mylib';")
521 (js2-deftest-parse import-renaming-named "import {one as uno, two as dos} from 'src/lib';")
522 (js2-deftest-parse import-default-and-namespace "import robert as bob, * as lib from 'src/lib';")
523 (js2-deftest-parse import-from-this-module "import {url} from this module;")
524
525 ;; Module Exports
526
527 (js2-deftest export-rexport "export * from 'other/lib'"
528 (js2-init-scanner)
529 (should (js2-match-token js2-EXPORT))
530 (let ((export-node (js2-parse-export)))
531 (should export-node)
532 (should (js2-export-node-from-clause export-node))))
533
534 (js2-deftest export-export-named-list "export {foo, bar as bang};"
535 (js2-init-scanner)
536 (should (js2-match-token js2-EXPORT))
537 (let ((export-node (js2-parse-export)))
538 (should export-node)
539 (let ((exports (js2-export-node-exports-list export-node)))
540 (should exports)
541 (should (= 2 (length exports))))))
542
543 (js2-deftest re-export-named-list "export {foo, bar as bang} from 'other/lib'"
544 (js2-init-scanner)
545 (should (js2-match-token js2-EXPORT))
546 (let ((export-node (js2-parse-export)))
547 (should export-node)
548 (should (js2-export-node-from-clause export-node))
549 (let ((exports (js2-export-node-exports-list export-node)))
550 (should exports)
551 (should (= 2 (length exports))))))
552
553 (js2-deftest export-variable-statement "export var foo = 'bar', baz = 'bang';"
554 (js2-init-scanner)
555 (js2-push-scope (make-js2-scope :pos 0))
556 (should (js2-match-token js2-EXPORT))
557 (let ((export-node (js2-parse-export)))
558 (should export-node)
559 (should (js2-export-node-declaration export-node))))
560
561 (js2-deftest export-const-declaration "export const PI = Math.PI;"
562 (js2-init-scanner)
563 (js2-push-scope (make-js2-scope :pos 0))
564 (should (js2-match-token js2-EXPORT))
565 (let ((export-node (js2-parse-export)))
566 (should export-node)
567 (should (js2-export-node-declaration export-node))))
568
569 (js2-deftest export-let-declaration "export let foo = [1];"
570 (js2-init-scanner)
571 (js2-push-scope (make-js2-scope :pos 0))
572 (should (js2-match-token js2-EXPORT))
573 (let ((export-node (js2-parse-export)))
574 (should export-node)
575 (should (js2-var-decl-node-p (js2-export-node-declaration export-node)))))
576
577 (js2-deftest export-class-declaration "export class Foo {};"
578 (js2-init-scanner)
579 (js2-push-scope (make-js2-scope :pos 0))
580 (should (js2-match-token js2-EXPORT))
581 (let ((export-node (js2-parse-export)))
582 (should export-node)
583 (should (js2-class-node-p (js2-export-node-declaration export-node)))))
584
585 (js2-deftest export-function-declaration "export default function doStuff() {};"
586 (js2-init-scanner)
587 (js2-push-scope (make-js2-scope :pos 0))
588 (should (js2-match-token js2-EXPORT))
589 (let ((export-node (js2-parse-export)))
590 (should export-node)
591 (should (js2-export-node-default export-node))))
592
593 (js2-deftest export-generator-declaration "export default function* one() {};"
594 (js2-init-scanner)
595 (js2-push-scope (make-js2-scope :pos 0))
596 (should (js2-match-token js2-EXPORT))
597 (let ((export-node (js2-parse-export)))
598 (should export-node)
599 (should (js2-export-node-default export-node))))
600
601 (js2-deftest export-assignment-expression "export default a = b;"
602 (js2-init-scanner)
603 (js2-push-scope (make-js2-scope :pos 0))
604 (should (js2-match-token js2-EXPORT))
605 (let ((export-node (js2-parse-export)))
606 (should export-node)
607 (should (js2-export-node-default export-node))))
608
609 (js2-deftest-parse parse-export-rexport "export * from 'other/lib';")
610 (js2-deftest-parse parse-export-export-named-list "export {foo, bar as bang};")
611 (js2-deftest-parse parse-re-export-named-list "export {foo, bar as bang} from 'other/lib';")
612 (js2-deftest-parse parse-export-const-declaration "export const PI = Math.PI;")
613 (js2-deftest-parse parse-export-let-declaration "export let foo = [1];")
614 (js2-deftest-parse parse-export-function-declaration "export default function doStuff() {};")
615 (js2-deftest-parse parse-export-generator-declaration "export default function* one() {};")
616 (js2-deftest-parse parse-export-assignment-expression "export default a = b;")
617
618 ;;; Strings
619
620 (js2-deftest-parse string-literal
621 "var x = 'y';")
622
623 (js2-deftest-parse object-get-string-literal
624 "var x = {y: 5};\nvar z = x[\"y\"];")
625
626 (js2-deftest-parse template-no-substritutions
627 "var x = `abc
628 def`, y = `\\u0000`;")
629
630 (js2-deftest-parse template-with-substitutions
631 "var y = `${a + b} ${d + e + f}`;")
632
633 (js2-deftest-parse tagged-template
634 "foo.args`${++x, \"o\"}k`;")
635
636 ;;; Classes
637
638 (js2-deftest-parse parse-harmony-class-statement
639 "class Foo {\n get bar() { return 42;\n}\n set bar(x) { y = x;\n}\n}")
640
641 (js2-deftest-parse parse-harmony-class-statement-without-name-is-not-ok
642 "class {\n get bar() { return 42;\n}\n}"
643 :syntax-error "{")
644
645 (js2-deftest-parse parse-harmony-class-expression
646 "var Foo1 = class Foo {\n bar() { return 42;\n}\n};")
647
648 (js2-deftest-parse parse-harmony-anonymous-class-expression
649 "var Foo = class {\n set bar(x) { bar = x;\n}\n};")
650
651 (js2-deftest-parse parse-harmony-class-with-extends
652 "class Foo extends Bar {\n}")
653
654 (js2-deftest-parse parse-harmony-anonymous-class-with-extends
655 "foo.Foo = class extends Bar {\n set bar(x) { bar = x;\n}\n};")
656
657 (js2-deftest-parse parse-harmony-class-with-complex-extends
658 "class Foo extends foo[BAR][2].Baz {\n}")
659
660 (js2-deftest-parse parse-harmony-class-missing-extended-class-is-not-ok
661 "class Foo extends {\n}"
662 :syntax-error "extends")
663
664 (js2-deftest-parse parse-harmony-class-static-method
665 "class Foo extends Bar {\n static bar() { return 42;\n}\n}")
666
667 (js2-deftest-parse parse-unterminated-class-is-not-okay
668 "class Foo {\n get bar() { return 42;\n}"
669 :syntax-error "}")
670
671 (js2-deftest-parse parse-super-keyword
672 "class Foo {\n constructor() { super(42);\n}\n foo() { super.foo();\n}\n}")
673
674 ;;; Scopes
675
676 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
677 (js2-mode)
678 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
679 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
680 (should (equal (js2-symbol-name entry) "foo"))
681 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
682
683 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
684 function bar() {}
685 var x;
686 }"
687 (js2-mode)
688 (let* ((scope (js2-node-at-point (point-min)))
689 (fn-entry (js2-scope-get-symbol scope 'bar))
690 (var-entry (js2-scope-get-symbol scope 'x)))
691 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
692 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
693 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
694 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
695 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
696
697 ;;; Tokenizer
698
699 (js2-deftest get-token "(1+1)"
700 (js2-init-scanner)
701 (should (eq js2-LP (js2-next-token)))
702 (should (eq js2-NUMBER (js2-next-token)))
703 (should (eq js2-ADD (js2-next-token)))
704 (should (eq js2-NUMBER (js2-next-token)))
705 (should (eq js2-RP (js2-next-token))))
706
707 (js2-deftest unget-token "()"
708 (js2-init-scanner)
709 (should (eq js2-LP (js2-next-token)))
710 (js2-unget-token)
711 (should (eq js2-LP (js2-next-token)))
712 (should (eq js2-RP (js2-next-token))))
713
714 (js2-deftest get-token-or-eol "x\n++;"
715 (js2-init-scanner)
716 (should (eq js2-NAME (js2-next-token)))
717 (should (eq js2-EOL (js2-peek-token-or-eol)))
718 (should (eq js2-INC (js2-next-token)))
719 (should (eq js2-SEMI (js2-peek-token-or-eol))))
720
721 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
722 (js2-init-scanner)
723 (should (eq js2-NAME (js2-next-token)))
724 (should (eq js2-NAME (js2-next-token)))
725 (should (equal "y" (js2-current-token-string)))
726 (js2-unget-token)
727 (should (eq js2-NAME (js2-current-token-type)))
728 (should (equal "x" (js2-current-token-string))))
729
730 (js2-deftest ts-seek "(1+2)"
731 (js2-init-scanner)
732 (should (eq js2-LP (js2-next-token)))
733 (should (eq js2-NUMBER (js2-next-token)))
734 (js2-unget-token)
735 (let ((state (make-js2-ts-state)))
736 (should (eq js2-NUMBER (js2-next-token)))
737 (should (eq js2-ADD (js2-next-token)))
738 (js2-ts-seek state)
739 (should (eq 1 js2-ti-lookahead))
740 (should (eq js2-NUMBER (js2-next-token)))
741 (should (eq 1 (js2-token-number
742 (js2-current-token))))))
743
744 (js2-deftest get-token-template-literal "`abc ${i} z ${j} def`"
745 (js2-init-scanner)
746 (should (eq js2-TEMPLATE_HEAD (js2-next-token)))
747 (should (equal "abc " (js2-current-token-string)))
748 (should (eq js2-NAME (js2-next-token)))
749 (should (eq js2-RC (js2-next-token)))
750 (should (eq js2-TEMPLATE_HEAD (js2-next-token 'TEMPLATE_TAIL)))
751 (should (equal " z " (js2-current-token-string)))
752 (should (eq js2-NAME (js2-next-token)))
753 (should (eq js2-RC (js2-next-token)))
754 (should (eq js2-NO_SUBS_TEMPLATE (js2-next-token 'TEMPLATE_TAIL)))
755 (should (equal " def" (js2-current-token-string))))
756
757 ;;; Error handling
758
759 (js2-deftest for-node-with-error-len "for "
760 (js2-mode)
761 (let ((node (js2-node-at-point (point-min))))
762 (should (= (js2-node-len (js2-node-parent node)) 4))))
763
764 (js2-deftest function-without-parens-error "function b {}"
765 ;; Should finish the parse.
766 (js2-mode))