1 ;;; tests/parser.el --- Some tests for js2-mode.
3 ;; Copyright (C) 2009, 2011-2013 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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.
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.
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/>.
27 (defmacro js2-deftest (name buffer-contents &rest body)
28 (declare (indent defun))
29 `(ert-deftest ,(intern (format "js2-%s" name)) ()
32 (insert ,buffer-contents))
36 (fundamental-mode)))))
38 (defun js2-test-string-to-ast (s)
41 (should (null js2-mode-buffer-dirty-p))
44 (cl-defun js2-test-parse-string (code-string &key syntax-error errors-count
46 (ert-with-test-buffer (:name 'origin)
47 (let ((ast (js2-test-string-to-ast code-string)))
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)
57 (skip-chars-backward " \t\n")
58 (should (string= (or reference code-string)
59 (buffer-substring-no-properties
60 (point-min) (point)))))))))
62 (cl-defmacro js2-deftest-parse (name code-string &key bind syntax-error errors-count
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
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))))
80 (js2-deftest-parse variable-assignment
83 (js2-deftest-parse empty-object-literal
86 (js2-deftest-parse empty-array-literal
89 (js2-deftest-parse array-with-missing-elements
92 (js2-deftest-parse comma-after-regexp
95 (js2-deftest-parse return-statement
96 "function foo() {\n return 2;\n}")
98 (js2-deftest-parse function-statement
99 "function foo() {\n}")
101 (js2-deftest-parse function-statement-inside-block
102 "if (true) {\n function foo() {\n }\n}")
104 (js2-deftest-parse function-expression-statements-are-verboten
105 "function() {}" :syntax-error "(")
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)))
111 (js2-deftest-parse named-function-expression
112 "a = function b() {};")
114 (js2-deftest-parse parenthesized-expression
117 (js2-deftest-parse for-with-in-operator-in-parens
118 "for (var y = (0 in []) in {}) {\n}")
120 (js2-deftest-parse for-with-in-operator-in-cond
121 "for (var y = 1 ? 0 in [] : false in {}) {\n}")
123 (js2-deftest-parse let-expression
126 (js2-deftest-parse let-expression-statement
129 ;;; Callers of `js2-valid-prop-name-token'
131 (js2-deftest-parse parse-property-access-when-not-keyword
134 (js2-deftest-parse parse-property-access-when-keyword
136 :bind ((js2-allow-keywords-as-property-names t)))
138 (js2-deftest-parse parse-property-access-when-keyword-no-xml
140 :bind ((js2-allow-keywords-as-property-names t)
141 (js2-compiler-xml-available nil)))
143 (js2-deftest-parse parse-object-literal-when-not-keyword
146 (js2-deftest-parse parse-object-literal-when-keyword
148 :bind ((js2-allow-keywords-as-property-names t)))
150 ;;; 'of' contextual keyword
152 (js2-deftest-parse parse-legacy-array-comp-loop-with-of
153 "[a for (a of [])];")
155 (js2-deftest-parse parse-array-comp-loop
156 "[for (a of []) a];")
158 (js2-deftest-parse parse-for-of
159 "for (var a of []) {\n}")
161 (js2-deftest-parse of-can-be-var-name
164 (js2-deftest-parse of-can-be-function-name
165 "function of() {\n}")
167 ;;; Destructuring binding
169 (js2-deftest-parse destruct-in-declaration
170 "var {a, b} = {a: 1, b: 2};")
172 (js2-deftest-parse destruct-in-arguments
173 "function f({a: aa, b: bb}) {\n}")
175 (js2-deftest-parse destruct-in-array-comp-loop
176 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
178 (js2-deftest-parse destruct-in-catch-clause
179 "try {\n} catch ({a, b}) {\n a + b;\n}")
183 (js2-deftest-parse object-literal-shorthand
184 "var x = {a: 1, b, c: 1, d};")
186 (js2-deftest-parse object-literal-shorthard-with-number
187 "var a = {1};" :syntax-error "}" :errors-count 2)
189 (js2-deftest-parse object-literal-method
190 "var x = {f(y) { return y;\n}};")
192 (js2-deftest object-literal-method-own-name-in-scope "({f(){f();}});"
194 (should (equal '("msg.undeclared.variable" "f")
195 (caar js2-parsed-warnings))))
197 (js2-deftest-parse object-literal-getter-method
198 "var x = {get f() { return 42;\n}};")
200 (js2-deftest-parse object-literal-setter-method
201 "var x = {set f(y) { x = y;\n}};")
203 (js2-deftest-parse object-literal-computed-keys
204 "var x = {[Symbol.iterator]: function() {}};")
206 (js2-deftest-parse object-literal-computed-function-keys
207 "var x = {[foo + bar](y) { return y;\n}};")
209 (js2-deftest-parse object-literal-computed-getter-key
210 "var x = {get [foo + bar]() { return 42;\n}};")
212 (js2-deftest-parse object-literal-generator
213 "var x = {*foo() { yield 42;\n}};")
215 (js2-deftest-parse object-literal-computed-generator-key
216 "var x = {*[foo + bar]() { yield 42;\n}};")
218 ;;; Function definition
220 (js2-deftest function-redeclaring-var "var gen = 3; function gen() {};"
222 (should (= (length (js2-ast-root-warnings js2-mode-ast)) 1)))
224 (js2-deftest function-expression-var-same-name "var gen = function gen() {};"
226 (should (null (js2-ast-root-warnings js2-mode-ast))))
228 ;;; Function parameters
230 (js2-deftest-parse function-with-default-parameters
231 "function foo(a = 1, b = a + 1) {\n}")
233 (js2-deftest-parse function-with-no-default-after-default
234 "function foo(a = 1, b) {\n}")
236 (js2-deftest-parse function-with-destruct-after-default
237 "function foo(a = 1, {b, c}) {\n}")
239 (js2-deftest-parse function-with-rest-parameter
240 "function foo(a, b, ...rest) {\n}")
242 (js2-deftest-parse function-with-param-after-rest-parameter
243 "function foo(a, ...b, rest) {\n}"
244 :syntax-error "rest")
246 (js2-deftest-parse function-with-destruct-after-rest-parameter
247 "function foo(a, ...b, {}) {\n}"
250 (js2-deftest-parse function-with-rest-after-default-parameter
251 "function foo(a = 1, ...rest) {\n}")
253 ;;; Strict mode errors
255 (js2-deftest-parse function-bad-strict-parameters
256 "'use strict';\nfunction foo(eval, {arguments}, bar) {\n}"
257 :syntax-error "eval" :errors-count 2)
259 (js2-deftest-parse function-retroactive-bad-strict-parameters
260 "function foo(arguments) {'use strict';}"
261 :syntax-error "arguments" :errors-count 1)
263 (js2-deftest-parse function-duplicate-strict-parameters
264 "'use strict';\nfunction foo(a, a) {\n}"
265 :syntax-error "a" :errors-count 1)
267 (js2-deftest-parse function-bad-strict-function-name
268 "'use strict';\nfunction eval() {\n}"
269 :syntax-error "eval" :errors-count 1)
271 (js2-deftest-parse function-bad-retroactive-strict-function-name
272 "function arguments() {'use strict';}"
273 :syntax-error "arguments" :errors-count 1)
275 (js2-deftest-parse function-bad-strict-catch-name
276 "'use strict';\ntry {} catch (eval) {}"
277 :syntax-error "eval" :errors-count 1)
279 (js2-deftest-parse function-bad-strict-variable-name
280 "'use strict';\nvar eval = 'kekeke';"
281 :syntax-error "eval" :errors-count 1)
283 (js2-deftest-parse function-bad-strict-assignment
284 "'use strict';\narguments = 'fufufu';"
285 :syntax-error "arguments" :errors-count 1)
287 (js2-deftest-parse function-property-strict-assignment
288 "'use strict';\narguments.okay = 'alright';")
290 (js2-deftest-parse function-strict-with
291 "'use strict';\nwith ({}) {}"
292 :syntax-error "with" :errors-count 1)
294 (js2-deftest-parse function-strict-octal
295 "'use strict';\nvar number = 0644;"
296 :syntax-error "0644" :errors-count 1)
298 (js2-deftest-parse function-strict-duplicate-keys
299 "'use strict';\nvar object = {a: 1, a: 2, 'a': 3, ['a']: 4, 1: 5, '1': 6, [1 + 1]: 7};"
300 :syntax-error "a" :errors-count 4) ; "a" has 3 dupes, "1" has 1 dupe.
302 (js2-deftest-parse function-strict-duplicate-getter
303 "'use strict';\nvar a = {get x() {}, get x() {}};"
304 :syntax-error "x" :errors-count 1)
306 (js2-deftest-parse function-strict-duplicate-setter
307 "'use strict';\nvar a = {set x() {}, set x() {}};"
308 :syntax-error "x" :errors-count 1)
310 ;;; Lack of errors in strict mode
312 (js2-deftest-parse function-strict-const-scope
313 "'use strict';\nconst a;\nif (1) {\n const a;\n}")
315 (js2-deftest-parse function-strict-no-getter-setter-duplicate
316 "'use strict';\nvar a = {get x() {}, set x() {}};")
320 (js2-deftest-parse spread-in-array-literal
321 "[1, ...[2, 3], 4, ...[5, 6]];")
323 (js2-deftest-parse spread-in-function-call
324 "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")
328 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
329 "() => false;" :reference "() => {false};")
331 (js2-deftest-parse arrow-function-with-args-and-curlies
332 "(a, b = 1, ...c) => { c;\n};")
334 (js2-deftest-parse arrow-function-with-destructuring
335 "([{a}, b]) => { a + b;\n};")
337 (js2-deftest-parse parenless-arrow-function-prohibits-rest
338 "...b => {b + 1;};" :syntax-error "=>")
340 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
341 "[a, b] => {a + b;};" :syntax-error "]" :errors-count 4)
343 (js2-deftest-parse arrow-function-recovers-from-error
344 "[(,foo) => 1];" :syntax-error "," :errors-count 6)
346 ;;; Automatic semicolon insertion
348 (js2-deftest-parse no-auto-semi-insertion-after-if
351 (js2-deftest-parse auto-semi-insertion-after-function
352 "a = function() {}" :reference "a = function() {};")
354 (js2-deftest-parse auto-semi-one-variable-per-line
355 "x\ny" :reference "x;\ny;")
359 (js2-deftest-parse labeled-stmt-node
360 "foo:\nbar:\nx = y + 1;")
362 (js2-deftest no-label-node-inside-expr "x = y:"
363 (let (js2-parse-interruptable-p)
365 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
366 (should (js2-name-node-p (js2-assign-node-right assignment)))))
368 (js2-deftest-parse label-and-loops "for (; ; ) {
377 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
379 (js2-deftest-parse legacy-generator-cannot-return
380 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
382 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
384 (js2-deftest-parse harmony-generator-yield-star "(function*(a) { yield* a;\n});")
388 (js2-deftest-parse parse-legacy-array-comp-loop-with-filter
389 "[a for (a in b) if (a == 2)];")
391 (js2-deftest-parse parse-array-comp-loop-with-filters
392 "[for (a in b) if (a == 2) if (b != 10) a];")
394 (js2-deftest-parse parse-generator-comp-loop-with-filters
395 "(for (x of y) if (x != 4) x);")
397 (js2-deftest-parse parse-array-comp-with-yield-is-ok
398 "(function() { return [for (x of []) yield x];\n});")
400 (js2-deftest-parse parse-generator-comp-with-yield-is-not-ok
401 "(function() { return (for (x of []) yield x);\n});"
402 :syntax-error "yield")
404 (js2-deftest-parse parse-generator-comp-with-yield-inside-function-is-ok
405 "(for (x of []) function*() { yield x;\n});")
409 (js2-deftest-parse decimal-starting-with-zero "081;" :reference "81;")
411 (js2-deftest-parse huge-hex "0x0123456789abcdefABCDEF;" :reference "-1;")
413 (js2-deftest-parse octal-without-o "071;" :reference "57;")
415 (js2-deftest-parse hex-number-okay "0x123;" :reference "291;")
417 (js2-deftest-parse hex-number-broken "0xz23;"
418 :syntax-error "0xz" :errors-count 2)
420 (js2-deftest-parse binary-number-okay "0b101;" :reference "5;")
422 (js2-deftest-parse binary-number-broken "0b210;"
423 :syntax-error "0b2" :errors-count 2)
425 (js2-deftest-parse octal-number-okay "0o765;" :reference "501;")
427 (js2-deftest-parse octal-number-broken "0o812;"
428 :syntax-error "0o8" :errors-count 2)
432 (js2-deftest parse-export-bindings "{one, two as dos}"
434 (should (js2-match-token js2-LC))
435 (let ((imports (js2-parse-export-bindings)))
436 (should (not (equal nil imports)))
437 (should (= 2 (length imports)))
438 (let ((first (nth 0 imports))
439 (second (nth 1 imports)))
440 (should (equal "one" (js2-name-node-name (js2-export-binding-node-extern-name first))))
441 (should (equal "two" (js2-name-node-name (js2-export-binding-node-extern-name second))))
442 (let ((first-name (js2-export-binding-node-local-name first))
443 (second-name (js2-export-binding-node-local-name second)))
444 (should (equal first (js2-node-parent first-name)))
445 (should (equal 3 (js2-node-len first-name)))
446 (should (equal "one" (js2-name-node-name first-name)))
447 (should (equal second (js2-node-parent second-name)))
448 (should (equal 3 (js2-node-len second-name)))
449 (should (equal "dos" (js2-name-node-name second-name)))))))
451 (js2-deftest parse-export-binding-as-default "one as default"
453 (let ((binding (js2-maybe-parse-export-binding)))
455 (should (js2-export-binding-node-p binding))
456 (let ((name (js2-export-binding-node-local-name binding)))
458 (should (equal "default" (js2-name-node-name name))))))
460 (js2-deftest parse-namepsace-import "* as lib;"
462 (should (js2-match-token js2-MUL))
463 (let ((namespace-import (js2-parse-namespace-import)))
464 (should (not (equal nil namespace-import)))
465 (should (js2-namespace-import-node-p namespace-import))
466 (should (= 1 (js2-node-pos namespace-import)))
467 (should (equal 8 (js2-node-len namespace-import)))
468 (let ((name-node (js2-namespace-import-node-name namespace-import)))
469 (should (equal "lib" (js2-name-node-name name-node)))
470 (should (= 5 (js2-node-pos name-node))))))
472 (js2-deftest parse-from-clause "from 'foo/bar';"
474 (let ((from (js2-parse-from-clause)))
475 (should (not (equal nil from)))
476 (should (= 1 (js2-node-pos from)))
477 (should (= 14 (js2-node-len from)))
478 (should (equal "foo/bar" (js2-from-clause-node-module-id from)))))
480 (js2-deftest parse-import-module-id-only "import 'src/lib'"
482 (should (js2-match-token js2-IMPORT))
483 (let ((import (js2-parse-import)))
484 (should (not (equal nil import)))
485 (should (= 1 (js2-node-pos import)))
486 (should (= 16 (js2-node-len import)))
487 (should (equal nil (js2-import-node-import import)))
488 (should (equal nil (js2-import-node-from import)))))
490 (js2-deftest parse-imported-default-binding "import theDefault from 'src/lib'"
491 (js2-push-scope (make-js2-scope :pos 0))
493 (should (js2-match-token js2-IMPORT))
494 (let ((import-node (js2-parse-import)))
495 (should (not (equal nil import-node)))
496 (should (equal "src/lib" (js2-import-node-module-id import-node)))
497 (let ((import (js2-import-node-import import-node)))
498 (should (not (equal nil import)))
499 (should (equal nil (js2-import-clause-node-namespace-import import)))
500 (should (equal nil (js2-import-clause-node-named-imports import)))
501 (let ((default (js2-import-clause-node-default-binding import)))
502 (should (not (equal nil default)))
503 (should (js2-export-binding-node-p default))
504 (should (equal "theDefault" (js2-name-node-name (js2-export-binding-node-extern-name default)))))))
505 (should (js2-scope-get-symbol js2-current-scope "theDefault")))
507 (js2-deftest parse-import-namespace-binding "import * as lib from 'src/lib'"
508 (js2-push-scope (make-js2-scope :pos 0))
510 (should (js2-match-token js2-IMPORT))
511 (let ((import-node (js2-parse-import)))
512 (should (not (equal nil import-node)))
513 (should (equal "src/lib" (js2-import-node-module-id import-node)))
514 (let ((import (js2-import-node-import import-node)))
515 (should (not (equal nil import)))
516 (should (equal nil (js2-import-clause-node-default-binding import)))
517 (should (equal nil (js2-import-clause-node-named-imports import)))
518 (let ((ns-import (js2-import-clause-node-namespace-import import)))
519 (should (not (equal nil ns-import)))
520 (should (js2-namespace-import-node-p ns-import))
521 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
522 (should (js2-scope-get-symbol js2-current-scope "lib")))
524 (js2-deftest parse-import-named-imports "import {foo as bar, baz} from 'src/lib'"
525 (js2-push-scope (make-js2-scope :pos 0))
527 (should (js2-match-token js2-IMPORT))
528 (let ((import-node (js2-parse-import)))
529 (should (not (equal nil import-node)))
530 (should (equal "src/lib" (js2-import-node-module-id import-node)))
531 (let ((import (js2-import-node-import import-node)))
532 (should (not (equal nil import)))
533 (should (equal nil (js2-import-clause-node-default-binding import)))
534 (should (equal nil (js2-import-clause-node-namespace-import import)))
535 (let ((named-imports (js2-import-clause-node-named-imports import)))
536 (should (not (equal nil named-imports)))
537 (should (listp named-imports))
538 (should (= 2 (length named-imports)))
539 (let ((first (nth 0 named-imports))
540 (second (nth 1 named-imports)))
541 (should (equal "bar" (js2-name-node-name (js2-export-binding-node-local-name first))))
542 (should (equal "baz" (js2-name-node-name (js2-export-binding-node-local-name second))))))))
543 (should (js2-scope-get-symbol js2-current-scope "bar"))
544 (should (js2-scope-get-symbol js2-current-scope "baz")))
546 (js2-deftest parse-import-default-and-namespace "import stuff, * as lib from 'src/lib'"
547 (js2-push-scope (make-js2-scope :pos 0))
549 (should (js2-match-token js2-IMPORT))
550 (let ((import-node (js2-parse-import)))
551 (should (not (equal nil import-node)))
552 (should (equal "src/lib" (js2-import-node-module-id import-node)))
553 (let ((import (js2-import-node-import import-node)))
554 (should (not (equal nil import)))
555 (should (equal nil (js2-import-clause-node-named-imports import)))
556 (let ((default (js2-import-clause-node-default-binding import))
557 (ns-import (js2-import-clause-node-namespace-import import)))
558 (should (not (equal nil default)))
559 (should (equal "stuff" (js2-name-node-name (js2-export-binding-node-local-name default))))
560 (should (not (equal nil ns-import)))
561 (should (js2-namespace-import-node-p ns-import))
562 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
563 (should (js2-scope-get-symbol js2-current-scope "stuff"))
564 (should (js2-scope-get-symbol js2-current-scope "lib")))
566 (js2-deftest parse-import-default-and-named-imports
567 "import robert as bob, {cookies, pi as PIE} from 'src/lib'"
568 (js2-push-scope (make-js2-scope :pos 0))
570 (should (js2-match-token js2-IMPORT))
571 (let ((import-node (js2-parse-import)))
572 (should (not (equal nil import-node)))
573 (should (equal "src/lib" (js2-import-node-module-id import-node)))
574 (let ((import (js2-import-node-import import-node)))
575 (should (not (equal nil import)))
576 (should (not (equal nil (js2-import-clause-node-named-imports import))))
577 (let ((default (js2-import-clause-node-default-binding import))
578 (named-imports (js2-import-clause-node-named-imports import)))
579 (should (not (equal nil default)))
580 (should (equal "bob" (js2-name-node-name (js2-export-binding-node-local-name default))))
581 (should (not (equal nil named-imports)))
582 (should (= 2 (length named-imports))))))
583 (should (js2-scope-get-symbol js2-current-scope "bob"))
584 (should (js2-scope-get-symbol js2-current-scope "cookies"))
585 (should (js2-scope-get-symbol js2-current-scope "PIE")))
587 (js2-deftest parse-this-module-in-from-clause "import {url} from this module;"
588 (js2-push-scope (make-js2-scope :pos 0))
590 (should (js2-match-token js2-IMPORT))
591 (let ((import-node (js2-parse-import)))
593 (let ((from-clause (js2-import-node-from import-node)))
595 (should (equal "this" (js2-from-clause-node-module-id from-clause)))
596 (should (js2-from-clause-node-metadata-p from-clause)))))
598 (js2-deftest-parse import-only-for-side-effects "import 'src/lib';")
599 (js2-deftest-parse import-default-only "import theDefault from 'src/lib';")
600 (js2-deftest-parse import-named-only "import {one, two} from 'src/lib';")
601 (js2-deftest-parse import-default-and-named "import theDefault, {one, two} from 'src/lib';")
602 (js2-deftest-parse import-renaming-default "import * as lib from 'src/mylib';")
603 (js2-deftest-parse import-renaming-named "import {one as uno, two as dos} from 'src/lib';")
604 (js2-deftest-parse import-default-and-namespace "import robert as bob, * as lib from 'src/lib';")
605 (js2-deftest-parse import-from-this-module "import {url} from this module;")
609 (js2-deftest export-rexport "export * from 'other/lib'"
611 (should (js2-match-token js2-EXPORT))
612 (let ((export-node (js2-parse-export)))
614 (should (js2-export-node-from-clause export-node))))
616 (js2-deftest export-export-named-list "export {foo, bar as bang};"
618 (should (js2-match-token js2-EXPORT))
619 (let ((export-node (js2-parse-export)))
621 (let ((exports (js2-export-node-exports-list export-node)))
623 (should (= 2 (length exports))))))
625 (js2-deftest re-export-named-list "export {foo, bar as bang} from 'other/lib'"
627 (should (js2-match-token js2-EXPORT))
628 (let ((export-node (js2-parse-export)))
630 (should (js2-export-node-from-clause export-node))
631 (let ((exports (js2-export-node-exports-list export-node)))
633 (should (= 2 (length exports))))))
635 (js2-deftest export-variable-statement "export var foo = 'bar', baz = 'bang';"
637 (js2-push-scope (make-js2-scope :pos 0))
638 (should (js2-match-token js2-EXPORT))
639 (let ((export-node (js2-parse-export)))
641 (should (js2-export-node-declaration export-node))))
643 (js2-deftest export-const-declaration "export const PI = Math.PI;"
645 (js2-push-scope (make-js2-scope :pos 0))
646 (should (js2-match-token js2-EXPORT))
647 (let ((export-node (js2-parse-export)))
649 (should (js2-export-node-declaration export-node))))
651 (js2-deftest export-let-declaration "export let foo = [1];"
653 (js2-push-scope (make-js2-scope :pos 0))
654 (should (js2-match-token js2-EXPORT))
655 (let ((export-node (js2-parse-export)))
657 (should (js2-var-decl-node-p (js2-export-node-declaration export-node)))))
659 (js2-deftest export-class-declaration "export class Foo {};"
661 (js2-push-scope (make-js2-scope :pos 0))
662 (should (js2-match-token js2-EXPORT))
663 (let ((export-node (js2-parse-export)))
665 (should (js2-class-node-p (js2-export-node-declaration export-node)))))
667 (js2-deftest export-function-declaration "export default function doStuff() {};"
669 (js2-push-scope (make-js2-scope :pos 0))
670 (should (js2-match-token js2-EXPORT))
671 (let ((export-node (js2-parse-export)))
673 (should (js2-export-node-default export-node))))
675 (js2-deftest export-generator-declaration "export default function* one() {};"
677 (js2-push-scope (make-js2-scope :pos 0))
678 (should (js2-match-token js2-EXPORT))
679 (let ((export-node (js2-parse-export)))
681 (should (js2-export-node-default export-node))))
683 (js2-deftest export-assignment-expression "export default a = b;"
685 (js2-push-scope (make-js2-scope :pos 0))
686 (should (js2-match-token js2-EXPORT))
687 (let ((export-node (js2-parse-export)))
689 (should (js2-export-node-default export-node))))
691 (js2-deftest export-function-no-semicolon "export default function foo() {}"
693 (should (equal nil js2-parsed-warnings)))
694 (js2-deftest export-default-function-no-semicolon "export function foo() {}"
696 (should (equal nil js2-parsed-warnings)))
697 (js2-deftest export-anything-else-does-require-a-semicolon "export var obj = {}"
699 (should (not (equal nil js2-parsed-warnings))))
701 (js2-deftest-parse parse-export-rexport "export * from 'other/lib';")
702 (js2-deftest-parse parse-export-export-named-list "export {foo, bar as bang};")
703 (js2-deftest-parse parse-re-export-named-list "export {foo, bar as bang} from 'other/lib';")
704 (js2-deftest-parse parse-export-const-declaration "export const PI = Math.PI;")
705 (js2-deftest-parse parse-export-let-declaration "export let foo = [1];")
706 (js2-deftest-parse parse-export-function-declaration "export default function doStuff() {};")
707 (js2-deftest-parse parse-export-generator-declaration "export default function* one() {};")
708 (js2-deftest-parse parse-export-assignment-expression "export default a = b;")
712 (js2-deftest-parse string-literal
715 (js2-deftest-parse object-get-string-literal
716 "var x = {y: 5};\nvar z = x[\"y\"];")
718 (js2-deftest-parse template-no-substritutions
720 def`, y = `\\u0000`;")
722 (js2-deftest-parse template-with-substitutions
723 "var y = `${a + b} ${d + e + f}`;")
725 (js2-deftest-parse tagged-template
726 "foo.args`${++x, \"o\"}k`;")
730 (js2-deftest-parse parse-harmony-class-statement
731 "class Foo {\n get bar() { return 42;\n}\n set bar(x) { y = x;\n}\n}")
733 (js2-deftest-parse parse-harmony-class-statement-without-name-is-not-ok
734 "class {\n get bar() { return 42;\n}\n}"
737 (js2-deftest-parse parse-harmony-class-expression
738 "var Foo1 = class Foo {\n bar() { return 42;\n}\n};")
740 (js2-deftest-parse parse-harmony-anonymous-class-expression
741 "var Foo = class {\n set bar(x) { bar = x;\n}\n};")
743 (js2-deftest-parse parse-harmony-class-with-extends
744 "class Foo extends Bar {\n}")
746 (js2-deftest-parse parse-harmony-anonymous-class-with-extends
747 "foo.Foo = class extends Bar {\n set bar(x) { bar = x;\n}\n};")
749 (js2-deftest-parse parse-harmony-class-with-complex-extends
750 "class Foo extends foo[BAR][2].Baz {\n}")
752 (js2-deftest-parse parse-harmony-class-missing-extended-class-is-not-ok
753 "class Foo extends {\n}"
754 :syntax-error "extends")
756 (js2-deftest-parse parse-harmony-class-static-method
757 "class Foo extends Bar {\n static bar() { return 42;\n}\n}")
759 (js2-deftest-parse parse-unterminated-class-is-not-okay
760 "class Foo {\n get bar() { return 42;\n}"
763 (js2-deftest-parse parse-super-keyword
764 "class Foo {\n constructor() { super(42);\n}\n foo() { super.foo();\n}\n}")
766 (js2-deftest-parse parse-class-keywordlike-method
767 "class C {\n delete() {}\n if() {}\n}")
769 (js2-deftest-parse parse-harmony-class-allow-semicolon-element
770 "class Foo {;}" :reference "class Foo {\n}")
774 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
776 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
777 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
778 (should (equal (js2-symbol-name entry) "foo"))
779 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
781 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
786 (let* ((scope (js2-node-at-point (point-min)))
787 (fn-entry (js2-scope-get-symbol scope 'bar))
788 (var-entry (js2-scope-get-symbol scope 'x)))
789 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
790 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
791 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
792 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
793 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
795 (defun js2-test-scope-of-nth-variable-satisifies-predicate (variable nth predicate)
796 (goto-char (point-min))
797 (dotimes (n (1+ nth)) (search-forward variable))
799 (let ((scope (js2-node-get-enclosing-scope (js2-node-at-point))))
800 (should (funcall predicate (js2-get-defining-scope scope variable)))))
802 (js2-deftest for-node-is-declaration-scope "for (let i = 0; i; ++i) {};"
804 (js2-test-scope-of-nth-variable-satisifies-predicate "i" 0 #'js2-for-node-p))
806 (js2-deftest const-scope-sloppy-script "{const a;} a;"
808 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-script-node-p)
809 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'js2-script-node-p))
811 (js2-deftest const-scope-strict-script "'use strict'; { const a; } a;"
813 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-block-node-p)
814 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'null))
816 (js2-deftest const-scope-sloppy-function "function f() { { const a; } a; }"
818 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-function-node-p)
819 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'js2-function-node-p))
821 (js2-deftest const-scope-strict-function "function f() { 'use strict'; { const a; } a; }"
823 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-block-node-p)
824 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'null))
826 (js2-deftest array-comp-is-result-scope "[x * 2 for (x in y)];"
828 (js2-test-scope-of-nth-variable-satisifies-predicate "x" 0 #'js2-comp-loop-node-p))
830 (js2-deftest array-comp-has-parent-scope
831 "var a,b=[for (i of [[1,2]]) for (j of i) j * a];"
833 (search-forward "for")
835 (let ((node (js2-node-at-point)))
836 (should (js2-scope-parent-scope node))
837 (should (js2-get-defining-scope node "j"))))
841 (js2-deftest get-token "(1+1)"
843 (should (eq js2-LP (js2-next-token)))
844 (should (eq js2-NUMBER (js2-next-token)))
845 (should (eq js2-ADD (js2-next-token)))
846 (should (eq js2-NUMBER (js2-next-token)))
847 (should (eq js2-RP (js2-next-token))))
849 (js2-deftest unget-token "()"
851 (should (eq js2-LP (js2-next-token)))
853 (should (eq js2-LP (js2-next-token)))
854 (should (eq js2-RP (js2-next-token))))
856 (js2-deftest get-token-or-eol "x\n++;"
858 (should (eq js2-NAME (js2-next-token)))
859 (should (eq js2-EOL (js2-peek-token-or-eol)))
860 (should (eq js2-INC (js2-next-token)))
861 (should (eq js2-SEMI (js2-peek-token-or-eol))))
863 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
865 (should (eq js2-NAME (js2-next-token)))
866 (should (eq js2-NAME (js2-next-token)))
867 (should (equal "y" (js2-current-token-string)))
869 (should (eq js2-NAME (js2-current-token-type)))
870 (should (equal "x" (js2-current-token-string))))
872 (js2-deftest ts-seek "(1+2)"
874 (should (eq js2-LP (js2-next-token)))
875 (should (eq js2-NUMBER (js2-next-token)))
877 (let ((state (make-js2-ts-state)))
878 (should (eq js2-NUMBER (js2-next-token)))
879 (should (eq js2-ADD (js2-next-token)))
881 (should (eq 1 js2-ti-lookahead))
882 (should (eq js2-NUMBER (js2-next-token)))
883 (should (eq 1 (js2-token-number
884 (js2-current-token))))))
886 (js2-deftest get-token-template-literal "`abc ${i} z ${j} def`"
888 (should (eq js2-TEMPLATE_HEAD (js2-next-token)))
889 (should (equal "abc " (js2-current-token-string)))
890 (should (eq js2-NAME (js2-next-token)))
891 (should (eq js2-RC (js2-next-token)))
892 (should (eq js2-TEMPLATE_HEAD (js2-next-token 'TEMPLATE_TAIL)))
893 (should (equal " z " (js2-current-token-string)))
894 (should (eq js2-NAME (js2-next-token)))
895 (should (eq js2-RC (js2-next-token)))
896 (should (eq js2-NO_SUBS_TEMPLATE (js2-next-token 'TEMPLATE_TAIL)))
897 (should (equal " def" (js2-current-token-string))))
901 (js2-deftest for-node-with-error-len "for "
903 (let ((node (js2-node-at-point (point-min))))
904 (should (= (js2-node-len (js2-node-parent node)) 4))))
906 (js2-deftest function-without-parens-error "function b {}"
907 ;; Should finish the parse.
912 (js2-deftest comment-node-length "//"
914 (let ((node (js2-node-at-point (point-min))))
915 (should (= (js2-node-len node) 2))))
917 (js2-deftest comment-node-length-newline "//\n"
919 (let ((node (js2-node-at-point (point-min))))
920 (should (= (js2-node-len node) 3))))
922 ;;; Variables classification
924 (defun js2--variables-summary (vars)
926 (setq vars (let (aslist)
927 (maphash (lambda (k v) (push (cons k v) aslist)) vars)
929 (dolist (v (sort vars (lambda (a b) (< (js2-node-abs-pos (js2-symbol-ast-node (car a)))
930 (js2-node-abs-pos (js2-symbol-ast-node (car b)))))))
931 (let* ((symbol (car v))
934 (symn (js2-symbol-ast-node symbol))
935 (namen (js2--get-name-node symn)))
936 (push (format "%s@%s:%s"
937 (js2-symbol-name symbol)
938 (js2-node-abs-pos namen)
944 (dolist (u (sort (cddr v) (lambda (a b) (< (js2-node-abs-pos a)
945 (js2-node-abs-pos b)))))
946 (push (js2-node-abs-pos u) r))))
949 (defmacro js2-deftest-classify-variables (name buffer-contents summary)
950 (declare (indent defun))
951 `(ert-deftest ,(intern (format "js2-classify-variables-%s" name)) ()
954 (insert ,buffer-contents))
958 (should (equal ,summary (js2--variables-summary
959 (js2--classify-variables)))))
960 (fundamental-mode)))))
962 (js2-deftest-classify-variables incomplete-var-statement
966 (js2-deftest-classify-variables unused-variable
967 "function foo () { var x; return 42; }"
968 '("foo@10:U" "x@23:U"))
970 (js2-deftest-classify-variables unused-variable-declared-twice
971 "function foo (a) { var x; function bar () { var x; x=42; }; return a;}"
972 '("foo@10:U" "a@15:P" 68 "x@24:U" "bar@36:U" "x@49:U"))
974 (js2-deftest-classify-variables assigned-variable
975 "function foo () { var x; x=42; return x; }"
976 '("foo@10:U" "x@23:I" 39))
978 (js2-deftest-classify-variables assignment-in-nested-function
979 "function foo () { var x; function bar () { x=42; }; }"
980 '("foo@10:U" "x@23:U" "bar@35:U"))
982 (js2-deftest-classify-variables unused-nested-function
983 "function foo() { var i, j=1; function bar() { var x, y=42, z=i; return y; } return i; }"
984 '("foo@10:U" "i@22:N" 62 84 "j@25:U" "bar@39:U" "x@51:U" "y@54:I" 72 "z@60:U"))
986 (js2-deftest-classify-variables prop-get-initialized
987 "function foo () { var x, y={}; y.a=x; }"
988 '("foo@10:U" "x@23:N" 36 "y@26:I" 32))
990 (js2-deftest-classify-variables prop-get-uninitialized
991 "function foo () { var x; if(x.foo) alert('boom'); }"
992 '("foo@10:U" "x@23:N" 29))
994 (js2-deftest-classify-variables prop-get-function-assignment
995 "(function(w) { w.f = function() { var a=42, m; return a; }; })(window);"
996 '("w@11:P" 11 16 "a@39:I" 55 "m@45:U"))
998 (js2-deftest-classify-variables let-declaration
999 "function foo () { let x,y=1; return x; }"
1000 '("foo@10:U" "x@23:N" 37 "y@25:U"))
1002 (js2-deftest-classify-variables external-function-call
1003 "function foo (m) { console.log(m, arguments); }"
1004 '("foo@10:U" "m@15:P" 32))
1006 (js2-deftest-classify-variables global-function-call
1007 "function bar () { return 42; } function foo (a) { return bar(); }"
1008 '("bar@10:I" 58 "foo@41:U" "a@46:P"))
1010 (js2-deftest-classify-variables let-declaration-for-scope
1011 "function foo () { for(let x=1,y; x<y; y++) {} }"
1012 '("foo@10:U" "x@27:I" 34 "y@31:N" 36 39))
1014 (js2-deftest-classify-variables arguments-implicit-var
1015 "function foo () { var p; for(p in arguments) { return p; } }"
1016 '("foo@10:U" "p@23:I" 55))
1018 (js2-deftest-classify-variables catch-error-variable
1019 "function foo () { try { throw 'Foo'; } catch (e) { console.log(e); }"
1020 '("foo@10:U" "e@47:I" 64))
1022 (js2-deftest-classify-variables prop-get-assignment
1023 "function foo () { var x={y:{z:{}}}; x.y.z=42; }"
1024 '("foo@10:U" "x@23:I" 37))
1026 (js2-deftest-classify-variables unused-function-argument
1027 "function foo (a) { return 42; }"
1028 '("foo@10:U" "a@15:P"))
1030 (js2-deftest-classify-variables used-function-argument
1031 "function foo (a) { a=42; return a; }"
1032 '("foo@10:U" "a@15:P" 33))
1034 (js2-deftest-classify-variables prop-get
1035 "function foo (a) { a=navigator.x||navigator.y; return a; }"
1036 '("foo@10:U" "a@15:P" 55))
1038 (js2-deftest-classify-variables for-in-loop
1039 "function foo () { var d={}; for(var k in d) {var v=d[k]; } }"
1040 '("foo@10:U" "d@23:I" 42 52 "k@37:I" 54 "v@50:U"))
1042 (js2-deftest-classify-variables array-comprehension-legacy
1043 "function foo() { var j,a=[for (i of [1,2,3]) i*j]; }"
1044 '("foo@10:U" "j@22:N" 48 "a@24:U" "i@32:I" 46))
1046 (js2-deftest-classify-variables array-comprehension
1047 "function foo() { var j,a=[[i,j] for (i of [1,2,3])]; }"
1048 '("foo@10:U" "j@22:N" 30 "a@24:U" "i@38:I" 28))
1050 (js2-deftest-classify-variables return-named-function
1051 "function foo() { var a=42; return function bar() { return a; } }"
1052 '("foo@10:U" "a@22:I" 59 "bar@44:I" 44))
1054 (js2-deftest-classify-variables named-wrapper-function
1055 "function foo() { var a; (function bar() { a=42; })(); return a; }"
1056 '("foo@10:U" "a@22:I" 62 "bar@35:I" 35))