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