]> code.delx.au - gnu-emacs-elpa/blob - tests/parser.el
Merge pull request #211 from cowboyd/no-semicolon-warning-on-function-export
[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) (car (last 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 "=>")
259
260 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
261 "[a, b] => {a + b;};" :syntax-error "]" :errors-count 4)
262
263 (js2-deftest-parse arrow-function-recovers-from-error
264 "[(,foo) => 1];" :syntax-error "," :errors-count 6)
265
266 ;;; Automatic semicolon insertion
267
268 (js2-deftest-parse no-auto-semi-insertion-after-if
269 "if (true) {\n}")
270
271 (js2-deftest-parse auto-semi-insertion-after-function
272 "a = function() {}" :reference "a = function() {};")
273
274 (js2-deftest-parse auto-semi-one-variable-per-line
275 "x\ny" :reference "x;\ny;")
276
277 ;;; Labels
278
279 (js2-deftest-parse labeled-stmt-node
280 "foo:\nbar:\nx = y + 1;")
281
282 (js2-deftest no-label-node-inside-expr "x = y:"
283 (let (js2-parse-interruptable-p)
284 (js2-mode))
285 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
286 (should (js2-name-node-p (js2-assign-node-right assignment)))))
287
288 (js2-deftest-parse label-and-loops "for (; ; ) {
289 loop:
290 for (; ; ) {
291 continue loop;
292 }
293 }")
294
295 ;;; Generators
296
297 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
298
299 (js2-deftest-parse legacy-generator-cannot-return
300 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
301
302 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
303
304 (js2-deftest-parse harmony-generator-yield-star "(function*(a) { yield* a;\n});")
305
306 ;;; Comprehensions
307
308 (js2-deftest-parse parse-legacy-array-comp-loop-with-filter
309 "[a for (a in b) if (a == 2)];")
310
311 (js2-deftest-parse parse-array-comp-loop-with-filters
312 "[for (a in b) if (a == 2) if (b != 10) a];")
313
314 (js2-deftest-parse parse-generator-comp-loop-with-filters
315 "(for (x of y) if (x != 4) x);")
316
317 (js2-deftest-parse parse-array-comp-with-yield-is-ok
318 "(function() { return [for (x of []) yield x];\n});")
319
320 (js2-deftest-parse parse-generator-comp-with-yield-is-not-ok
321 "(function() { return (for (x of []) yield x);\n});"
322 :syntax-error "yield")
323
324 (js2-deftest-parse parse-generator-comp-with-yield-inside-function-is-ok
325 "(for (x of []) function*() { yield x;\n});")
326
327 ;;; Numbers
328
329 (js2-deftest-parse decimal-starting-with-zero "081;" :reference "81;")
330
331 (js2-deftest-parse huge-hex "0x0123456789abcdefABCDEF;" :reference "-1;")
332
333 (js2-deftest-parse octal-without-o "071;" :reference "57;")
334
335 (js2-deftest-parse hex-number-okay "0x123;" :reference "291;")
336
337 (js2-deftest-parse hex-number-broken "0xz23;"
338 :syntax-error "0xz" :errors-count 2)
339
340 (js2-deftest-parse binary-number-okay "0b101;" :reference "5;")
341
342 (js2-deftest-parse binary-number-broken "0b210;"
343 :syntax-error "0b2" :errors-count 2)
344
345 (js2-deftest-parse octal-number-okay "0o765;" :reference "501;")
346
347 (js2-deftest-parse octal-number-broken "0o812;"
348 :syntax-error "0o8" :errors-count 2)
349
350 ;;; Modules
351
352 (js2-deftest parse-export-bindings "{one, two as dos}"
353 (js2-init-scanner)
354 (should (js2-match-token js2-LC))
355 (let ((imports (js2-parse-export-bindings)))
356 (should (not (equal nil imports)))
357 (should (= 2 (length imports)))
358 (let ((first (nth 0 imports))
359 (second (nth 1 imports)))
360 (should (equal "one" (js2-name-node-name (js2-export-binding-node-extern-name first))))
361 (should (equal "two" (js2-name-node-name (js2-export-binding-node-extern-name second))))
362 (let ((first-name (js2-export-binding-node-local-name first))
363 (second-name (js2-export-binding-node-local-name second)))
364 (should (equal first (js2-node-parent first-name)))
365 (should (equal 3 (js2-node-len first-name)))
366 (should (equal "one" (js2-name-node-name first-name)))
367 (should (equal second (js2-node-parent second-name)))
368 (should (equal 3 (js2-node-len second-name)))
369 (should (equal "dos" (js2-name-node-name second-name)))))))
370
371 (js2-deftest parse-export-binding-as-default "one as default"
372 (js2-init-scanner)
373 (let ((binding (js2-maybe-parse-export-binding)))
374 (should binding)
375 (should (js2-export-binding-node-p binding))
376 (let ((name (js2-export-binding-node-local-name binding)))
377 (should name)
378 (should (equal "default" (js2-name-node-name name))))))
379
380 (js2-deftest parse-namepsace-import "* as lib;"
381 (js2-init-scanner)
382 (should (js2-match-token js2-MUL))
383 (let ((namespace-import (js2-parse-namespace-import)))
384 (should (not (equal nil namespace-import)))
385 (should (js2-namespace-import-node-p namespace-import))
386 (should (= 1 (js2-node-pos namespace-import)))
387 (should (equal 8 (js2-node-len namespace-import)))
388 (let ((name-node (js2-namespace-import-node-name namespace-import)))
389 (should (equal "lib" (js2-name-node-name name-node)))
390 (should (= 5 (js2-node-pos name-node))))))
391
392 (js2-deftest parse-from-clause "from 'foo/bar';"
393 (js2-init-scanner)
394 (let ((from (js2-parse-from-clause)))
395 (should (not (equal nil from)))
396 (should (= 1 (js2-node-pos from)))
397 (should (= 14 (js2-node-len from)))
398 (should (equal "foo/bar" (js2-from-clause-node-module-id from)))))
399
400 (js2-deftest parse-import-module-id-only "import 'src/lib'"
401 (js2-init-scanner)
402 (should (js2-match-token js2-IMPORT))
403 (let ((import (js2-parse-import)))
404 (should (not (equal nil import)))
405 (should (= 1 (js2-node-pos import)))
406 (should (= 16 (js2-node-len import)))
407 (should (equal nil (js2-import-node-import import)))
408 (should (equal nil (js2-import-node-from import)))))
409
410 (js2-deftest parse-imported-default-binding "import theDefault from 'src/lib'"
411 (js2-push-scope (make-js2-scope :pos 0))
412 (js2-init-scanner)
413 (should (js2-match-token js2-IMPORT))
414 (let ((import-node (js2-parse-import)))
415 (should (not (equal nil import-node)))
416 (should (equal "src/lib" (js2-import-node-module-id import-node)))
417 (let ((import (js2-import-node-import import-node)))
418 (should (not (equal nil import)))
419 (should (equal nil (js2-import-clause-node-namespace-import import)))
420 (should (equal nil (js2-import-clause-node-named-imports import)))
421 (let ((default (js2-import-clause-node-default-binding import)))
422 (should (not (equal nil default)))
423 (should (js2-export-binding-node-p default))
424 (should (equal "theDefault" (js2-name-node-name (js2-export-binding-node-extern-name default)))))))
425 (should (js2-scope-get-symbol js2-current-scope "theDefault")))
426
427 (js2-deftest parse-import-namespace-binding "import * as lib from 'src/lib'"
428 (js2-push-scope (make-js2-scope :pos 0))
429 (js2-init-scanner)
430 (should (js2-match-token js2-IMPORT))
431 (let ((import-node (js2-parse-import)))
432 (should (not (equal nil import-node)))
433 (should (equal "src/lib" (js2-import-node-module-id import-node)))
434 (let ((import (js2-import-node-import import-node)))
435 (should (not (equal nil import)))
436 (should (equal nil (js2-import-clause-node-default-binding import)))
437 (should (equal nil (js2-import-clause-node-named-imports import)))
438 (let ((ns-import (js2-import-clause-node-namespace-import import)))
439 (should (not (equal nil ns-import)))
440 (should (js2-namespace-import-node-p ns-import))
441 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
442 (should (js2-scope-get-symbol js2-current-scope "lib")))
443
444 (js2-deftest parse-import-named-imports "import {foo as bar, baz} from 'src/lib'"
445 (js2-push-scope (make-js2-scope :pos 0))
446 (js2-init-scanner)
447 (should (js2-match-token js2-IMPORT))
448 (let ((import-node (js2-parse-import)))
449 (should (not (equal nil import-node)))
450 (should (equal "src/lib" (js2-import-node-module-id import-node)))
451 (let ((import (js2-import-node-import import-node)))
452 (should (not (equal nil import)))
453 (should (equal nil (js2-import-clause-node-default-binding import)))
454 (should (equal nil (js2-import-clause-node-namespace-import import)))
455 (let ((named-imports (js2-import-clause-node-named-imports import)))
456 (should (not (equal nil named-imports)))
457 (should (listp named-imports))
458 (should (= 2 (length named-imports)))
459 (let ((first (nth 0 named-imports))
460 (second (nth 1 named-imports)))
461 (should (equal "bar" (js2-name-node-name (js2-export-binding-node-local-name first))))
462 (should (equal "baz" (js2-name-node-name (js2-export-binding-node-local-name second))))))))
463 (should (js2-scope-get-symbol js2-current-scope "bar"))
464 (should (js2-scope-get-symbol js2-current-scope "baz")))
465
466 (js2-deftest parse-import-default-and-namespace "import stuff, * as lib from 'src/lib'"
467 (js2-push-scope (make-js2-scope :pos 0))
468 (js2-init-scanner)
469 (should (js2-match-token js2-IMPORT))
470 (let ((import-node (js2-parse-import)))
471 (should (not (equal nil import-node)))
472 (should (equal "src/lib" (js2-import-node-module-id import-node)))
473 (let ((import (js2-import-node-import import-node)))
474 (should (not (equal nil import)))
475 (should (equal nil (js2-import-clause-node-named-imports import)))
476 (let ((default (js2-import-clause-node-default-binding import))
477 (ns-import (js2-import-clause-node-namespace-import import)))
478 (should (not (equal nil default)))
479 (should (equal "stuff" (js2-name-node-name (js2-export-binding-node-local-name default))))
480 (should (not (equal nil ns-import)))
481 (should (js2-namespace-import-node-p ns-import))
482 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
483 (should (js2-scope-get-symbol js2-current-scope "stuff"))
484 (should (js2-scope-get-symbol js2-current-scope "lib")))
485
486 (js2-deftest parse-import-default-and-named-imports
487 "import robert as bob, {cookies, pi as PIE} from 'src/lib'"
488 (js2-push-scope (make-js2-scope :pos 0))
489 (js2-init-scanner)
490 (should (js2-match-token js2-IMPORT))
491 (let ((import-node (js2-parse-import)))
492 (should (not (equal nil import-node)))
493 (should (equal "src/lib" (js2-import-node-module-id import-node)))
494 (let ((import (js2-import-node-import import-node)))
495 (should (not (equal nil import)))
496 (should (not (equal nil (js2-import-clause-node-named-imports import))))
497 (let ((default (js2-import-clause-node-default-binding import))
498 (named-imports (js2-import-clause-node-named-imports import)))
499 (should (not (equal nil default)))
500 (should (equal "bob" (js2-name-node-name (js2-export-binding-node-local-name default))))
501 (should (not (equal nil named-imports)))
502 (should (= 2 (length named-imports))))))
503 (should (js2-scope-get-symbol js2-current-scope "bob"))
504 (should (js2-scope-get-symbol js2-current-scope "cookies"))
505 (should (js2-scope-get-symbol js2-current-scope "PIE")))
506
507 (js2-deftest parse-this-module-in-from-clause "import {url} from this module;"
508 (js2-push-scope (make-js2-scope :pos 0))
509 (js2-init-scanner)
510 (should (js2-match-token js2-IMPORT))
511 (let ((import-node (js2-parse-import)))
512 (should import-node)
513 (let ((from-clause (js2-import-node-from import-node)))
514 (should from-clause)
515 (should (equal "this" (js2-from-clause-node-module-id from-clause)))
516 (should (js2-from-clause-node-metadata-p from-clause)))))
517
518 (js2-deftest-parse import-only-for-side-effects "import 'src/lib';")
519 (js2-deftest-parse import-default-only "import theDefault from 'src/lib';")
520 (js2-deftest-parse import-named-only "import {one, two} from 'src/lib';")
521 (js2-deftest-parse import-default-and-named "import theDefault, {one, two} from 'src/lib';")
522 (js2-deftest-parse import-renaming-default "import * as lib from 'src/mylib';")
523 (js2-deftest-parse import-renaming-named "import {one as uno, two as dos} from 'src/lib';")
524 (js2-deftest-parse import-default-and-namespace "import robert as bob, * as lib from 'src/lib';")
525 (js2-deftest-parse import-from-this-module "import {url} from this module;")
526
527 ;; Module Exports
528
529 (js2-deftest export-rexport "export * from 'other/lib'"
530 (js2-init-scanner)
531 (should (js2-match-token js2-EXPORT))
532 (let ((export-node (js2-parse-export)))
533 (should export-node)
534 (should (js2-export-node-from-clause export-node))))
535
536 (js2-deftest export-export-named-list "export {foo, bar as bang};"
537 (js2-init-scanner)
538 (should (js2-match-token js2-EXPORT))
539 (let ((export-node (js2-parse-export)))
540 (should export-node)
541 (let ((exports (js2-export-node-exports-list export-node)))
542 (should exports)
543 (should (= 2 (length exports))))))
544
545 (js2-deftest re-export-named-list "export {foo, bar as bang} from 'other/lib'"
546 (js2-init-scanner)
547 (should (js2-match-token js2-EXPORT))
548 (let ((export-node (js2-parse-export)))
549 (should export-node)
550 (should (js2-export-node-from-clause export-node))
551 (let ((exports (js2-export-node-exports-list export-node)))
552 (should exports)
553 (should (= 2 (length exports))))))
554
555 (js2-deftest export-variable-statement "export var foo = 'bar', baz = 'bang';"
556 (js2-init-scanner)
557 (js2-push-scope (make-js2-scope :pos 0))
558 (should (js2-match-token js2-EXPORT))
559 (let ((export-node (js2-parse-export)))
560 (should export-node)
561 (should (js2-export-node-declaration export-node))))
562
563 (js2-deftest export-const-declaration "export const PI = Math.PI;"
564 (js2-init-scanner)
565 (js2-push-scope (make-js2-scope :pos 0))
566 (should (js2-match-token js2-EXPORT))
567 (let ((export-node (js2-parse-export)))
568 (should export-node)
569 (should (js2-export-node-declaration export-node))))
570
571 (js2-deftest export-let-declaration "export let foo = [1];"
572 (js2-init-scanner)
573 (js2-push-scope (make-js2-scope :pos 0))
574 (should (js2-match-token js2-EXPORT))
575 (let ((export-node (js2-parse-export)))
576 (should export-node)
577 (should (js2-var-decl-node-p (js2-export-node-declaration export-node)))))
578
579 (js2-deftest export-class-declaration "export class Foo {};"
580 (js2-init-scanner)
581 (js2-push-scope (make-js2-scope :pos 0))
582 (should (js2-match-token js2-EXPORT))
583 (let ((export-node (js2-parse-export)))
584 (should export-node)
585 (should (js2-class-node-p (js2-export-node-declaration export-node)))))
586
587 (js2-deftest export-function-declaration "export default function doStuff() {};"
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-default export-node))))
594
595 (js2-deftest export-generator-declaration "export default function* one() {};"
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-default export-node))))
602
603 (js2-deftest export-assignment-expression "export default a = b;"
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-export-node-default export-node))))
610
611 (js2-deftest export-function-no-semicolon "export default function foo() {}"
612 (js2-mode)
613 (should (equal nil js2-parsed-warnings)))
614 (js2-deftest export-default-function-no-semicolon "export function foo() {}"
615 (js2-mode)
616 (should (equal nil js2-parsed-warnings)))
617 (js2-deftest export-anything-else-does-require-a-semicolon "export var obj = {}"
618 (js2-mode)
619 (should (not (equal nil js2-parsed-warnings))))
620
621 (js2-deftest-parse parse-export-rexport "export * from 'other/lib';")
622 (js2-deftest-parse parse-export-export-named-list "export {foo, bar as bang};")
623 (js2-deftest-parse parse-re-export-named-list "export {foo, bar as bang} from 'other/lib';")
624 (js2-deftest-parse parse-export-const-declaration "export const PI = Math.PI;")
625 (js2-deftest-parse parse-export-let-declaration "export let foo = [1];")
626 (js2-deftest-parse parse-export-function-declaration "export default function doStuff() {};")
627 (js2-deftest-parse parse-export-generator-declaration "export default function* one() {};")
628 (js2-deftest-parse parse-export-assignment-expression "export default a = b;")
629
630 ;;; Strings
631
632 (js2-deftest-parse string-literal
633 "var x = 'y';")
634
635 (js2-deftest-parse object-get-string-literal
636 "var x = {y: 5};\nvar z = x[\"y\"];")
637
638 (js2-deftest-parse template-no-substritutions
639 "var x = `abc
640 def`, y = `\\u0000`;")
641
642 (js2-deftest-parse template-with-substitutions
643 "var y = `${a + b} ${d + e + f}`;")
644
645 (js2-deftest-parse tagged-template
646 "foo.args`${++x, \"o\"}k`;")
647
648 ;;; Classes
649
650 (js2-deftest-parse parse-harmony-class-statement
651 "class Foo {\n get bar() { return 42;\n}\n set bar(x) { y = x;\n}\n}")
652
653 (js2-deftest-parse parse-harmony-class-statement-without-name-is-not-ok
654 "class {\n get bar() { return 42;\n}\n}"
655 :syntax-error "{")
656
657 (js2-deftest-parse parse-harmony-class-expression
658 "var Foo1 = class Foo {\n bar() { return 42;\n}\n};")
659
660 (js2-deftest-parse parse-harmony-anonymous-class-expression
661 "var Foo = class {\n set bar(x) { bar = x;\n}\n};")
662
663 (js2-deftest-parse parse-harmony-class-with-extends
664 "class Foo extends Bar {\n}")
665
666 (js2-deftest-parse parse-harmony-anonymous-class-with-extends
667 "foo.Foo = class extends Bar {\n set bar(x) { bar = x;\n}\n};")
668
669 (js2-deftest-parse parse-harmony-class-with-complex-extends
670 "class Foo extends foo[BAR][2].Baz {\n}")
671
672 (js2-deftest-parse parse-harmony-class-missing-extended-class-is-not-ok
673 "class Foo extends {\n}"
674 :syntax-error "extends")
675
676 (js2-deftest-parse parse-harmony-class-static-method
677 "class Foo extends Bar {\n static bar() { return 42;\n}\n}")
678
679 (js2-deftest-parse parse-unterminated-class-is-not-okay
680 "class Foo {\n get bar() { return 42;\n}"
681 :syntax-error "}")
682
683 (js2-deftest-parse parse-super-keyword
684 "class Foo {\n constructor() { super(42);\n}\n foo() { super.foo();\n}\n}")
685
686 ;;; Scopes
687
688 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
689 (js2-mode)
690 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
691 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
692 (should (equal (js2-symbol-name entry) "foo"))
693 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
694
695 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
696 function bar() {}
697 var x;
698 }"
699 (js2-mode)
700 (let* ((scope (js2-node-at-point (point-min)))
701 (fn-entry (js2-scope-get-symbol scope 'bar))
702 (var-entry (js2-scope-get-symbol scope 'x)))
703 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
704 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
705 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
706 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
707 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
708
709 ;;; Tokenizer
710
711 (js2-deftest get-token "(1+1)"
712 (js2-init-scanner)
713 (should (eq js2-LP (js2-next-token)))
714 (should (eq js2-NUMBER (js2-next-token)))
715 (should (eq js2-ADD (js2-next-token)))
716 (should (eq js2-NUMBER (js2-next-token)))
717 (should (eq js2-RP (js2-next-token))))
718
719 (js2-deftest unget-token "()"
720 (js2-init-scanner)
721 (should (eq js2-LP (js2-next-token)))
722 (js2-unget-token)
723 (should (eq js2-LP (js2-next-token)))
724 (should (eq js2-RP (js2-next-token))))
725
726 (js2-deftest get-token-or-eol "x\n++;"
727 (js2-init-scanner)
728 (should (eq js2-NAME (js2-next-token)))
729 (should (eq js2-EOL (js2-peek-token-or-eol)))
730 (should (eq js2-INC (js2-next-token)))
731 (should (eq js2-SEMI (js2-peek-token-or-eol))))
732
733 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
734 (js2-init-scanner)
735 (should (eq js2-NAME (js2-next-token)))
736 (should (eq js2-NAME (js2-next-token)))
737 (should (equal "y" (js2-current-token-string)))
738 (js2-unget-token)
739 (should (eq js2-NAME (js2-current-token-type)))
740 (should (equal "x" (js2-current-token-string))))
741
742 (js2-deftest ts-seek "(1+2)"
743 (js2-init-scanner)
744 (should (eq js2-LP (js2-next-token)))
745 (should (eq js2-NUMBER (js2-next-token)))
746 (js2-unget-token)
747 (let ((state (make-js2-ts-state)))
748 (should (eq js2-NUMBER (js2-next-token)))
749 (should (eq js2-ADD (js2-next-token)))
750 (js2-ts-seek state)
751 (should (eq 1 js2-ti-lookahead))
752 (should (eq js2-NUMBER (js2-next-token)))
753 (should (eq 1 (js2-token-number
754 (js2-current-token))))))
755
756 (js2-deftest get-token-template-literal "`abc ${i} z ${j} def`"
757 (js2-init-scanner)
758 (should (eq js2-TEMPLATE_HEAD (js2-next-token)))
759 (should (equal "abc " (js2-current-token-string)))
760 (should (eq js2-NAME (js2-next-token)))
761 (should (eq js2-RC (js2-next-token)))
762 (should (eq js2-TEMPLATE_HEAD (js2-next-token 'TEMPLATE_TAIL)))
763 (should (equal " z " (js2-current-token-string)))
764 (should (eq js2-NAME (js2-next-token)))
765 (should (eq js2-RC (js2-next-token)))
766 (should (eq js2-NO_SUBS_TEMPLATE (js2-next-token 'TEMPLATE_TAIL)))
767 (should (equal " def" (js2-current-token-string))))
768
769 ;;; Error handling
770
771 (js2-deftest for-node-with-error-len "for "
772 (js2-mode)
773 (let ((node (js2-node-at-point (point-min))))
774 (should (= (js2-node-len (js2-node-parent node)) 4))))
775
776 (js2-deftest function-without-parens-error "function b {}"
777 ;; Should finish the parse.
778 (js2-mode))