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}")
181 (js2-deftest-parse destruct-with-initializer-in-object
182 "var {a, b = 2, c} = {};")
184 (js2-deftest-parse destruct-with-initializer-in-array
185 "var [a, b = 2, c] = [];")
187 (js2-deftest-parse destruct-non-name-target-is-error
188 "var {1=1} = {};" :syntax-error "1" :errors-count 1)
190 (js2-deftest-parse destruct-with-initializer-in-function-arguments
191 "function f({a, b = 1, c}, [d, e = 1, f]) {\n}")
193 (js2-deftest-parse destruct-name-conflict-is-error-in-object
194 "\"use strict\";\nvar {a=1,a=2} = {};" :syntax-error "a" :errors-count 1)
196 (js2-deftest destruct-name-conflict-is-warning-in-array "\"use strict\";\nvar [a=1,a=2] = [];"
198 (should (equal '("msg.var.redecl" "a")
199 (caar js2-parsed-warnings))))
201 (js2-deftest initializer-outside-destruct-is-error "({a=1});"
203 (should (equal "msg.init.no.destruct"
204 (car (caar js2-parsed-errors)))))
208 (js2-deftest-parse object-literal-shorthand
209 "var x = {a: 1, b, c: 1, d};")
211 (js2-deftest-parse object-literal-shorthard-with-number
212 "var a = {1};" :syntax-error "}" :errors-count 2)
214 (js2-deftest-parse object-literal-method
215 "var x = {f(y) { return y;\n}};")
217 (js2-deftest object-literal-method-own-name-in-scope "({f(){f();}});"
219 (should (equal '("msg.undeclared.variable" "f")
220 (caar js2-parsed-warnings))))
222 (js2-deftest-parse object-literal-getter-method
223 "var x = {get f() { return 42;\n}};")
225 (js2-deftest-parse object-literal-setter-method
226 "var x = {set f(y) { x = y;\n}};")
228 (js2-deftest-parse object-literal-computed-keys
229 "var x = {[Symbol.iterator]: function() {}};")
231 (js2-deftest-parse object-literal-computed-function-keys
232 "var x = {[foo + bar](y) { return y;\n}};")
234 (js2-deftest-parse object-literal-computed-getter-key
235 "var x = {get [foo + bar]() { return 42;\n}};")
237 (js2-deftest-parse object-literal-generator
238 "var x = {*foo() { yield 42;\n}};")
240 (js2-deftest-parse object-literal-computed-generator-key
241 "var x = {*[foo + bar]() { yield 42;\n}};")
243 ;;; Function definition
245 (js2-deftest function-redeclaring-var "var gen = 3; function gen() {};"
247 (should (= (length (js2-ast-root-warnings js2-mode-ast)) 1)))
249 (js2-deftest function-expression-var-same-name "var gen = function gen() {};"
251 (should (null (js2-ast-root-warnings js2-mode-ast))))
253 ;;; Function parameters
255 (js2-deftest-parse function-with-default-parameters
256 "function foo(a = 1, b = a + 1) {\n}")
258 (js2-deftest-parse function-with-no-default-after-default
259 "function foo(a = 1, b) {\n}")
261 (js2-deftest-parse function-with-destruct-after-default
262 "function foo(a = 1, {b, c}) {\n}")
264 (js2-deftest-parse function-with-rest-parameter
265 "function foo(a, b, ...rest) {\n}")
267 (js2-deftest-parse function-with-param-after-rest-parameter
268 "function foo(a, ...b, rest) {\n}"
269 :syntax-error "rest")
271 (js2-deftest-parse function-with-destruct-after-rest-parameter
272 "function foo(a, ...b, {}) {\n}"
275 (js2-deftest-parse function-with-rest-after-default-parameter
276 "function foo(a = 1, ...rest) {\n}")
278 ;;; Strict mode errors
280 (js2-deftest-parse function-bad-strict-parameters
281 "'use strict';\nfunction foo(eval, {arguments}, bar) {\n}"
282 :syntax-error "eval" :errors-count 2)
284 (js2-deftest-parse function-retroactive-bad-strict-parameters
285 "function foo(arguments) {'use strict';}"
286 :syntax-error "arguments" :errors-count 1)
288 (js2-deftest-parse function-duplicate-strict-parameters
289 "'use strict';\nfunction foo(a, a) {\n}"
290 :syntax-error "a" :errors-count 1)
292 (js2-deftest-parse function-bad-strict-function-name
293 "'use strict';\nfunction eval() {\n}"
294 :syntax-error "eval" :errors-count 1)
296 (js2-deftest-parse function-bad-retroactive-strict-function-name
297 "function arguments() {'use strict';}"
298 :syntax-error "arguments" :errors-count 1)
300 (js2-deftest-parse function-bad-strict-catch-name
301 "'use strict';\ntry {} catch (eval) {}"
302 :syntax-error "eval" :errors-count 1)
304 (js2-deftest-parse function-bad-strict-variable-name
305 "'use strict';\nvar eval = 'kekeke';"
306 :syntax-error "eval" :errors-count 1)
308 (js2-deftest-parse function-bad-strict-assignment
309 "'use strict';\narguments = 'fufufu';"
310 :syntax-error "arguments" :errors-count 1)
312 (js2-deftest-parse function-property-strict-assignment
313 "'use strict';\narguments.okay = 'alright';")
315 (js2-deftest-parse function-strict-with
316 "'use strict';\nwith ({}) {}"
317 :syntax-error "with" :errors-count 1)
319 (js2-deftest-parse function-strict-octal
320 "'use strict';\nvar number = 0644;"
321 :syntax-error "0644" :errors-count 1)
323 (js2-deftest-parse function-strict-duplicate-keys
324 "'use strict';\nvar object = {a: 1, a: 2, 'a': 3, ['a']: 4, 1: 5, '1': 6, [1 + 1]: 7};"
325 :syntax-error "a" :errors-count 4) ; "a" has 3 dupes, "1" has 1 dupe.
327 (js2-deftest-parse function-strict-duplicate-getter
328 "'use strict';\nvar a = {get x() {}, get x() {}};"
329 :syntax-error "x" :errors-count 1)
331 (js2-deftest-parse function-strict-duplicate-setter
332 "'use strict';\nvar a = {set x() {}, set x() {}};"
333 :syntax-error "x" :errors-count 1)
335 ;;; Lack of errors in strict mode
337 (js2-deftest-parse function-strict-const-scope
338 "'use strict';\nconst a;\nif (1) {\n const a;\n}")
340 (js2-deftest-parse function-strict-no-getter-setter-duplicate
341 "'use strict';\nvar a = {get x() {}, set x() {}};")
345 (js2-deftest-parse spread-in-array-literal
346 "[1, ...[2, 3], 4, ...[5, 6]];")
348 (js2-deftest-parse spread-in-function-call
349 "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")
353 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
354 "() => false;" :reference "() => {false};")
356 (js2-deftest-parse arrow-function-with-args-and-curlies
357 "(a, b = 1, ...c) => { c;\n};")
359 (js2-deftest-parse arrow-function-with-destructuring
360 "([{a}, b]) => { a + b;\n};")
362 (js2-deftest-parse parenless-arrow-function-prohibits-rest
363 "...b => {b + 1;};" :syntax-error "=>")
365 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
366 "[a, b] => {a + b;};" :syntax-error "]" :errors-count 4)
368 (js2-deftest-parse arrow-function-recovers-from-error
369 "[(,foo) => 1];" :syntax-error "," :errors-count 6)
371 ;;; Automatic semicolon insertion
373 (js2-deftest-parse no-auto-semi-insertion-after-if
376 (js2-deftest-parse auto-semi-insertion-after-function
377 "a = function() {}" :reference "a = function() {};")
379 (js2-deftest-parse auto-semi-one-variable-per-line
380 "x\ny" :reference "x;\ny;")
384 (js2-deftest-parse labeled-stmt-node
385 "foo:\nbar:\nx = y + 1;")
387 (js2-deftest no-label-node-inside-expr "x = y:"
388 (let (js2-parse-interruptable-p)
390 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
391 (should (js2-name-node-p (js2-assign-node-right assignment)))))
393 (js2-deftest-parse label-and-loops "for (; ; ) {
402 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
404 (js2-deftest-parse legacy-generator-cannot-return
405 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
407 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
409 (js2-deftest-parse harmony-generator-yield-star "(function*(a) { yield* a;\n});")
413 (js2-deftest-parse parse-legacy-array-comp-loop-with-filter
414 "[a for (a in b) if (a == 2)];")
416 (js2-deftest-parse parse-array-comp-loop-with-filters
417 "[for (a in b) if (a == 2) if (b != 10) a];")
419 (js2-deftest-parse parse-generator-comp-loop-with-filters
420 "(for (x of y) if (x != 4) x);")
422 (js2-deftest-parse parse-array-comp-with-yield-is-ok
423 "(function() { return [for (x of []) yield x];\n});")
425 (js2-deftest-parse parse-generator-comp-with-yield-is-not-ok
426 "(function() { return (for (x of []) yield x);\n});"
427 :syntax-error "yield")
429 (js2-deftest-parse parse-generator-comp-with-yield-inside-function-is-ok
430 "(for (x of []) function*() { yield x;\n});")
434 (js2-deftest-parse async-function-statement
435 "async function foo() {\n}")
437 (js2-deftest-parse async-function-statement-inside-block
438 "if (true) {\n async function foo() {\n }\n}")
440 (js2-deftest-parse async-function-expression-statements-are-verboten
441 "async function() {}" :syntax-error "(")
443 (js2-deftest-parse async-named-function-expression
444 "a = async function b() {};")
446 (js2-deftest-parse async-arrow-function-expression
447 "a = async (b) => { b;\n};")
451 (js2-deftest-parse await-is-ok "async function foo() {\n await bar();\n}")
453 (js2-deftest-parse await-inside-assignment-is-ok
454 "async function foo() {\n var result = await bar();\n}")
456 (js2-deftest-parse await-inside-array-is-ok
457 "async function foo() {\n var results = [await bar(), await baz()];\n}")
459 (js2-deftest-parse await-inside-non-async-function-is-not-ok
460 "function foo() {\n await bar();\n}"
461 :syntax-error "await")
463 (js2-deftest-parse await-inside-non-async-arrow-function-is-not-ok
464 "a = () => { await bar();\n}"
465 :syntax-error "await")
467 ;;; 'async' and 'await' are contextual keywords
469 (js2-deftest-parse async-can-be-var-name
472 (js2-deftest-parse async-can-be-function-name
473 "function async() {\n}")
475 (js2-deftest-parse await-can-be-var-name
478 (js2-deftest-parse await-can-be-function-name
479 "function await() {\n}")
483 (js2-deftest-parse decimal-starting-with-zero "081;" :reference "81;")
485 (js2-deftest-parse huge-hex "0x0123456789abcdefABCDEF;" :reference "-1;")
487 (js2-deftest-parse octal-without-o "071;" :reference "57;")
489 (js2-deftest-parse hex-number-okay "0x123;" :reference "291;")
491 (js2-deftest-parse hex-number-broken "0xz23;"
492 :syntax-error "0xz" :errors-count 2)
494 (js2-deftest-parse binary-number-okay "0b101;" :reference "5;")
496 (js2-deftest-parse binary-number-broken "0b210;"
497 :syntax-error "0b2" :errors-count 2)
499 (js2-deftest-parse octal-number-okay "0o765;" :reference "501;")
501 (js2-deftest-parse octal-number-broken "0o812;"
502 :syntax-error "0o8" :errors-count 2)
506 (js2-deftest parse-export-bindings "{one, two as dos}"
508 (should (js2-match-token js2-LC))
509 (let ((imports (js2-parse-export-bindings)))
510 (should (not (equal nil imports)))
511 (should (= 2 (length imports)))
512 (let ((first (nth 0 imports))
513 (second (nth 1 imports)))
514 (should (equal "one" (js2-name-node-name (js2-export-binding-node-extern-name first))))
515 (should (equal "two" (js2-name-node-name (js2-export-binding-node-extern-name second))))
516 (let ((first-name (js2-export-binding-node-local-name first))
517 (second-name (js2-export-binding-node-local-name second)))
518 (should (equal first (js2-node-parent first-name)))
519 (should (equal 3 (js2-node-len first-name)))
520 (should (equal "one" (js2-name-node-name first-name)))
521 (should (equal second (js2-node-parent second-name)))
522 (should (equal 3 (js2-node-len second-name)))
523 (should (equal "dos" (js2-name-node-name second-name)))))))
525 (js2-deftest parse-export-binding-as-default "one as default"
527 (let ((binding (js2-maybe-parse-export-binding)))
529 (should (js2-export-binding-node-p binding))
530 (let ((name (js2-export-binding-node-local-name binding)))
532 (should (equal "default" (js2-name-node-name name))))))
534 (js2-deftest parse-namepsace-import "* as lib;"
536 (should (js2-match-token js2-MUL))
537 (let ((namespace-import (js2-parse-namespace-import)))
538 (should (not (equal nil namespace-import)))
539 (should (js2-namespace-import-node-p namespace-import))
540 (should (= 1 (js2-node-pos namespace-import)))
541 (should (equal 8 (js2-node-len namespace-import)))
542 (let ((name-node (js2-namespace-import-node-name namespace-import)))
543 (should (equal "lib" (js2-name-node-name name-node)))
544 (should (= 5 (js2-node-pos name-node))))))
546 (js2-deftest parse-from-clause "from 'foo/bar';"
548 (let ((from (js2-parse-from-clause)))
549 (should (not (equal nil from)))
550 (should (= 1 (js2-node-pos from)))
551 (should (= 14 (js2-node-len from)))
552 (should (equal "foo/bar" (js2-from-clause-node-module-id from)))))
554 (js2-deftest parse-import-module-id-only "import 'src/lib'"
556 (should (js2-match-token js2-IMPORT))
557 (let ((import (js2-parse-import)))
558 (should (not (equal nil import)))
559 (should (= 1 (js2-node-pos import)))
560 (should (= 16 (js2-node-len import)))
561 (should (equal nil (js2-import-node-import import)))
562 (should (equal nil (js2-import-node-from import)))))
564 (js2-deftest parse-imported-default-binding "import theDefault from 'src/lib'"
565 (js2-push-scope (make-js2-scope :pos 0))
567 (should (js2-match-token js2-IMPORT))
568 (let ((import-node (js2-parse-import)))
569 (should (not (equal nil import-node)))
570 (should (equal "src/lib" (js2-import-node-module-id import-node)))
571 (let ((import (js2-import-node-import import-node)))
572 (should (not (equal nil import)))
573 (should (equal nil (js2-import-clause-node-namespace-import import)))
574 (should (equal nil (js2-import-clause-node-named-imports import)))
575 (let ((default (js2-import-clause-node-default-binding import)))
576 (should (not (equal nil default)))
577 (should (js2-export-binding-node-p default))
578 (should (equal "theDefault" (js2-name-node-name (js2-export-binding-node-extern-name default)))))))
579 (should (js2-scope-get-symbol js2-current-scope "theDefault")))
581 (js2-deftest parse-import-namespace-binding "import * as lib from 'src/lib'"
582 (js2-push-scope (make-js2-scope :pos 0))
584 (should (js2-match-token js2-IMPORT))
585 (let ((import-node (js2-parse-import)))
586 (should (not (equal nil import-node)))
587 (should (equal "src/lib" (js2-import-node-module-id import-node)))
588 (let ((import (js2-import-node-import import-node)))
589 (should (not (equal nil import)))
590 (should (equal nil (js2-import-clause-node-default-binding import)))
591 (should (equal nil (js2-import-clause-node-named-imports import)))
592 (let ((ns-import (js2-import-clause-node-namespace-import import)))
593 (should (not (equal nil ns-import)))
594 (should (js2-namespace-import-node-p ns-import))
595 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
596 (should (js2-scope-get-symbol js2-current-scope "lib")))
598 (js2-deftest parse-import-named-imports "import {foo as bar, baz} from 'src/lib'"
599 (js2-push-scope (make-js2-scope :pos 0))
601 (should (js2-match-token js2-IMPORT))
602 (let ((import-node (js2-parse-import)))
603 (should (not (equal nil import-node)))
604 (should (equal "src/lib" (js2-import-node-module-id import-node)))
605 (let ((import (js2-import-node-import import-node)))
606 (should (not (equal nil import)))
607 (should (equal nil (js2-import-clause-node-default-binding import)))
608 (should (equal nil (js2-import-clause-node-namespace-import import)))
609 (let ((named-imports (js2-import-clause-node-named-imports import)))
610 (should (not (equal nil named-imports)))
611 (should (listp named-imports))
612 (should (= 2 (length named-imports)))
613 (let ((first (nth 0 named-imports))
614 (second (nth 1 named-imports)))
615 (should (equal "bar" (js2-name-node-name (js2-export-binding-node-local-name first))))
616 (should (equal "baz" (js2-name-node-name (js2-export-binding-node-local-name second))))))))
617 (should (js2-scope-get-symbol js2-current-scope "bar"))
618 (should (js2-scope-get-symbol js2-current-scope "baz")))
620 (js2-deftest parse-import-default-and-namespace "import stuff, * as lib from 'src/lib'"
621 (js2-push-scope (make-js2-scope :pos 0))
623 (should (js2-match-token js2-IMPORT))
624 (let ((import-node (js2-parse-import)))
625 (should (not (equal nil import-node)))
626 (should (equal "src/lib" (js2-import-node-module-id import-node)))
627 (let ((import (js2-import-node-import import-node)))
628 (should (not (equal nil import)))
629 (should (equal nil (js2-import-clause-node-named-imports import)))
630 (let ((default (js2-import-clause-node-default-binding import))
631 (ns-import (js2-import-clause-node-namespace-import import)))
632 (should (not (equal nil default)))
633 (should (equal "stuff" (js2-name-node-name (js2-export-binding-node-local-name default))))
634 (should (not (equal nil ns-import)))
635 (should (js2-namespace-import-node-p ns-import))
636 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
637 (should (js2-scope-get-symbol js2-current-scope "stuff"))
638 (should (js2-scope-get-symbol js2-current-scope "lib")))
640 (js2-deftest parse-import-default-and-named-imports
641 "import robert as bob, {cookies, pi as PIE} from 'src/lib'"
642 (js2-push-scope (make-js2-scope :pos 0))
644 (should (js2-match-token js2-IMPORT))
645 (let ((import-node (js2-parse-import)))
646 (should (not (equal nil import-node)))
647 (should (equal "src/lib" (js2-import-node-module-id import-node)))
648 (let ((import (js2-import-node-import import-node)))
649 (should (not (equal nil import)))
650 (should (not (equal nil (js2-import-clause-node-named-imports import))))
651 (let ((default (js2-import-clause-node-default-binding import))
652 (named-imports (js2-import-clause-node-named-imports import)))
653 (should (not (equal nil default)))
654 (should (equal "bob" (js2-name-node-name (js2-export-binding-node-local-name default))))
655 (should (not (equal nil named-imports)))
656 (should (= 2 (length named-imports))))))
657 (should (js2-scope-get-symbol js2-current-scope "bob"))
658 (should (js2-scope-get-symbol js2-current-scope "cookies"))
659 (should (js2-scope-get-symbol js2-current-scope "PIE")))
661 (js2-deftest parse-this-module-in-from-clause "import {url} from this module;"
662 (js2-push-scope (make-js2-scope :pos 0))
664 (should (js2-match-token js2-IMPORT))
665 (let ((import-node (js2-parse-import)))
667 (let ((from-clause (js2-import-node-from import-node)))
669 (should (equal "this" (js2-from-clause-node-module-id from-clause)))
670 (should (js2-from-clause-node-metadata-p from-clause)))))
672 (js2-deftest-parse import-only-for-side-effects "import 'src/lib';")
673 (js2-deftest-parse import-default-only "import theDefault from 'src/lib';")
674 (js2-deftest-parse import-named-only "import {one, two} from 'src/lib';")
675 (js2-deftest-parse import-default-and-named "import theDefault, {one, two} from 'src/lib';")
676 (js2-deftest-parse import-renaming-default "import * as lib from 'src/mylib';")
677 (js2-deftest-parse import-renaming-named "import {one as uno, two as dos} from 'src/lib';")
678 (js2-deftest-parse import-default-and-namespace "import robert as bob, * as lib from 'src/lib';")
679 (js2-deftest-parse import-from-this-module "import {url} from this module;")
683 (js2-deftest export-rexport "export * from 'other/lib'"
685 (should (js2-match-token js2-EXPORT))
686 (let ((export-node (js2-parse-export)))
688 (should (js2-export-node-from-clause export-node))))
690 (js2-deftest export-export-named-list "export {foo, bar as bang};"
692 (should (js2-match-token js2-EXPORT))
693 (let ((export-node (js2-parse-export)))
695 (let ((exports (js2-export-node-exports-list export-node)))
697 (should (= 2 (length exports))))))
699 (js2-deftest re-export-named-list "export {foo, bar as bang} from 'other/lib'"
701 (should (js2-match-token js2-EXPORT))
702 (let ((export-node (js2-parse-export)))
704 (should (js2-export-node-from-clause export-node))
705 (let ((exports (js2-export-node-exports-list export-node)))
707 (should (= 2 (length exports))))))
709 (js2-deftest export-variable-statement "export var foo = 'bar', baz = 'bang';"
711 (js2-push-scope (make-js2-scope :pos 0))
712 (should (js2-match-token js2-EXPORT))
713 (let ((export-node (js2-parse-export)))
715 (should (js2-export-node-declaration export-node))))
717 (js2-deftest export-const-declaration "export const PI = Math.PI;"
719 (js2-push-scope (make-js2-scope :pos 0))
720 (should (js2-match-token js2-EXPORT))
721 (let ((export-node (js2-parse-export)))
723 (should (js2-export-node-declaration export-node))))
725 (js2-deftest export-let-declaration "export let foo = [1];"
727 (js2-push-scope (make-js2-scope :pos 0))
728 (should (js2-match-token js2-EXPORT))
729 (let ((export-node (js2-parse-export)))
731 (should (js2-var-decl-node-p (js2-export-node-declaration export-node)))))
733 (js2-deftest export-class-declaration "export class Foo {};"
735 (js2-push-scope (make-js2-scope :pos 0))
736 (should (js2-match-token js2-EXPORT))
737 (let ((export-node (js2-parse-export)))
739 (should (js2-class-node-p (js2-export-node-declaration export-node)))))
741 (js2-deftest export-function-declaration "export default function doStuff() {};"
743 (js2-push-scope (make-js2-scope :pos 0))
744 (should (js2-match-token js2-EXPORT))
745 (let ((export-node (js2-parse-export)))
747 (should (js2-export-node-default export-node))))
749 (js2-deftest export-generator-declaration "export default function* one() {};"
751 (js2-push-scope (make-js2-scope :pos 0))
752 (should (js2-match-token js2-EXPORT))
753 (let ((export-node (js2-parse-export)))
755 (should (js2-export-node-default export-node))))
757 (js2-deftest export-assignment-expression "export default a = b;"
759 (js2-push-scope (make-js2-scope :pos 0))
760 (should (js2-match-token js2-EXPORT))
761 (let ((export-node (js2-parse-export)))
763 (should (js2-export-node-default export-node))))
765 (js2-deftest export-function-no-semicolon "export default function foo() {}"
767 (should (equal nil js2-parsed-warnings)))
768 (js2-deftest export-default-function-no-semicolon "export function foo() {}"
770 (should (equal nil js2-parsed-warnings)))
771 (js2-deftest export-anything-else-does-require-a-semicolon "export var obj = {}"
773 (should (not (equal nil js2-parsed-warnings))))
775 (js2-deftest-parse parse-export-rexport "export * from 'other/lib';")
776 (js2-deftest-parse parse-export-export-named-list "export {foo, bar as bang};")
777 (js2-deftest-parse parse-re-export-named-list "export {foo, bar as bang} from 'other/lib';")
778 (js2-deftest-parse parse-export-const-declaration "export const PI = Math.PI;")
779 (js2-deftest-parse parse-export-let-declaration "export let foo = [1];")
780 (js2-deftest-parse parse-export-function-declaration "export default function doStuff() {};")
781 (js2-deftest-parse parse-export-generator-declaration "export default function* one() {};")
782 (js2-deftest-parse parse-export-assignment-expression "export default a = b;")
786 (js2-deftest-parse string-literal
789 (js2-deftest-parse object-get-string-literal
790 "var x = {y: 5};\nvar z = x[\"y\"];")
792 (js2-deftest-parse template-no-substritutions
794 def`, y = `\\u0000`;")
796 (js2-deftest-parse template-with-substitutions
797 "var y = `${a + b} ${d + e + f}`;")
799 (js2-deftest-parse tagged-template
800 "foo.args`${++x, \"o\"}k`;")
804 (js2-deftest-parse parse-harmony-class-statement
805 "class Foo {\n get bar() { return 42;\n}\n set bar(x) { y = x;\n}\n}")
807 (js2-deftest-parse parse-harmony-class-statement-without-name-is-not-ok
808 "class {\n get bar() { return 42;\n}\n}"
811 (js2-deftest-parse parse-harmony-class-expression
812 "var Foo1 = class Foo {\n bar() { return 42;\n}\n};")
814 (js2-deftest-parse parse-harmony-anonymous-class-expression
815 "var Foo = class {\n set bar(x) { bar = x;\n}\n};")
817 (js2-deftest-parse parse-harmony-class-with-extends
818 "class Foo extends Bar {\n}")
820 (js2-deftest-parse parse-harmony-anonymous-class-with-extends
821 "foo.Foo = class extends Bar {\n set bar(x) { bar = x;\n}\n};")
823 (js2-deftest-parse parse-harmony-class-with-complex-extends
824 "class Foo extends foo[BAR][2].Baz {\n}")
826 (js2-deftest-parse parse-harmony-class-missing-extended-class-is-not-ok
827 "class Foo extends {\n}"
828 :syntax-error "extends")
830 (js2-deftest-parse parse-harmony-class-static-method
831 "class Foo extends Bar {\n static bar() { return 42;\n}\n}")
833 (js2-deftest-parse parse-unterminated-class-is-not-okay
834 "class Foo {\n get bar() { return 42;\n}"
837 (js2-deftest-parse parse-super-keyword
838 "class Foo {\n constructor() { super(42);\n}\n foo() { super.foo();\n}\n}")
840 (js2-deftest-parse parse-class-keywordlike-method
841 "class C {\n delete() {}\n if() {}\n}")
843 (js2-deftest-parse parse-harmony-class-allow-semicolon-element
844 "class Foo {;}" :reference "class Foo {\n}")
848 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
850 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
851 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
852 (should (equal (js2-symbol-name entry) "foo"))
853 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
855 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
860 (let* ((scope (js2-node-at-point (point-min)))
861 (fn-entry (js2-scope-get-symbol scope 'bar))
862 (var-entry (js2-scope-get-symbol scope 'x)))
863 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
864 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
865 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
866 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
867 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
869 (defun js2-test-scope-of-nth-variable-satisifies-predicate (variable nth predicate)
870 (goto-char (point-min))
871 (dotimes (n (1+ nth)) (search-forward variable))
873 (let ((scope (js2-node-get-enclosing-scope (js2-node-at-point))))
874 (should (funcall predicate (js2-get-defining-scope scope variable)))))
876 (js2-deftest for-node-is-declaration-scope "for (let i = 0; i; ++i) {};"
878 (js2-test-scope-of-nth-variable-satisifies-predicate "i" 0 #'js2-for-node-p))
880 (js2-deftest const-scope-sloppy-script "{const a;} a;"
882 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-script-node-p)
883 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'js2-script-node-p))
885 (js2-deftest const-scope-strict-script "'use strict'; { const a; } a;"
887 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-block-node-p)
888 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'null))
890 (js2-deftest const-scope-sloppy-function "function f() { { const a; } a; }"
892 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-function-node-p)
893 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'js2-function-node-p))
895 (js2-deftest const-scope-strict-function "function f() { 'use strict'; { const a; } a; }"
897 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-block-node-p)
898 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'null))
900 (js2-deftest array-comp-is-result-scope "[x * 2 for (x in y)];"
902 (js2-test-scope-of-nth-variable-satisifies-predicate "x" 0 #'js2-comp-loop-node-p))
904 (js2-deftest array-comp-has-parent-scope
905 "var a,b=[for (i of [[1,2]]) for (j of i) j * a];"
907 (search-forward "for")
909 (let ((node (js2-node-at-point)))
910 (should (js2-scope-parent-scope node))
911 (should (js2-get-defining-scope node "j"))))
915 (js2-deftest get-token "(1+1)"
917 (should (eq js2-LP (js2-next-token)))
918 (should (eq js2-NUMBER (js2-next-token)))
919 (should (eq js2-ADD (js2-next-token)))
920 (should (eq js2-NUMBER (js2-next-token)))
921 (should (eq js2-RP (js2-next-token))))
923 (js2-deftest unget-token "()"
925 (should (eq js2-LP (js2-next-token)))
927 (should (eq js2-LP (js2-next-token)))
928 (should (eq js2-RP (js2-next-token))))
930 (js2-deftest get-token-or-eol "x\n++;"
932 (should (eq js2-NAME (js2-next-token)))
933 (should (eq js2-EOL (js2-peek-token-or-eol)))
934 (should (eq js2-INC (js2-next-token)))
935 (should (eq js2-SEMI (js2-peek-token-or-eol))))
937 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
939 (should (eq js2-NAME (js2-next-token)))
940 (should (eq js2-NAME (js2-next-token)))
941 (should (equal "y" (js2-current-token-string)))
943 (should (eq js2-NAME (js2-current-token-type)))
944 (should (equal "x" (js2-current-token-string))))
946 (js2-deftest ts-seek "(1+2)"
948 (should (eq js2-LP (js2-next-token)))
949 (should (eq js2-NUMBER (js2-next-token)))
951 (let ((state (make-js2-ts-state)))
952 (should (eq js2-NUMBER (js2-next-token)))
953 (should (eq js2-ADD (js2-next-token)))
955 (should (eq 1 js2-ti-lookahead))
956 (should (eq js2-NUMBER (js2-next-token)))
957 (should (eq 1 (js2-token-number
958 (js2-current-token))))))
960 (js2-deftest get-token-template-literal "`abc ${i} z ${j} def`"
962 (should (eq js2-TEMPLATE_HEAD (js2-next-token)))
963 (should (equal "abc " (js2-current-token-string)))
964 (should (eq js2-NAME (js2-next-token)))
965 (should (eq js2-RC (js2-next-token)))
966 (should (eq js2-TEMPLATE_HEAD (js2-next-token 'TEMPLATE_TAIL)))
967 (should (equal " z " (js2-current-token-string)))
968 (should (eq js2-NAME (js2-next-token)))
969 (should (eq js2-RC (js2-next-token)))
970 (should (eq js2-NO_SUBS_TEMPLATE (js2-next-token 'TEMPLATE_TAIL)))
971 (should (equal " def" (js2-current-token-string))))
975 (js2-deftest for-node-with-error-len "for "
977 (let ((node (js2-node-at-point (point-min))))
978 (should (= (js2-node-len (js2-node-parent node)) 4))))
980 (js2-deftest function-without-parens-error "function b {}"
981 ;; Should finish the parse.
986 (js2-deftest comment-node-length "//"
988 (let ((node (js2-node-at-point (point-min))))
989 (should (= (js2-node-len node) 2))))
991 (js2-deftest comment-node-length-newline "//\n"
993 (let ((node (js2-node-at-point (point-min))))
994 (should (= (js2-node-len node) 3))))
996 ;;; Variables classification
998 (defun js2--variables-summary (vars)
1000 (setq vars (let (aslist)
1001 (maphash (lambda (k v) (push (cons k v) aslist)) vars)
1003 (dolist (v (sort vars (lambda (a b) (< (js2-node-abs-pos (js2-symbol-ast-node (car a)))
1004 (js2-node-abs-pos (js2-symbol-ast-node (car b)))))))
1005 (let* ((symbol (car v))
1008 (symn (js2-symbol-ast-node symbol))
1009 (namen (js2--get-name-node symn)))
1010 (push (format "%s@%s:%s"
1011 (js2-symbol-name symbol)
1012 (js2-node-abs-pos namen)
1016 (if inition "I" "N")
1018 (dolist (u (sort (cddr v) (lambda (a b) (< (js2-node-abs-pos a)
1019 (js2-node-abs-pos b)))))
1020 (push (js2-node-abs-pos u) r))))
1023 (defmacro js2-deftest-classify-variables (name buffer-contents summary)
1024 (declare (indent defun))
1025 `(ert-deftest ,(intern (format "js2-classify-variables-%s" name)) ()
1028 (insert ,buffer-contents))
1032 (should (equal ,summary (js2--variables-summary
1033 (js2--classify-variables)))))
1034 (fundamental-mode)))))
1036 (js2-deftest-classify-variables incomplete-var-statement
1040 (js2-deftest-classify-variables unused-variable
1041 "function foo () { var x; return 42; }"
1042 '("foo@10:U" "x@23:U"))
1044 (js2-deftest-classify-variables unused-variable-declared-twice
1045 "function foo (a) { var x; function bar () { var x; x=42; }; return a;}"
1046 '("foo@10:U" "a@15:P" 68 "x@24:U" "bar@36:U" "x@49:U"))
1048 (js2-deftest-classify-variables assigned-variable
1049 "function foo () { var x; x=42; return x; }"
1050 '("foo@10:U" "x@23:I" 39))
1052 (js2-deftest-classify-variables assignment-in-nested-function
1053 "function foo () { var x; function bar () { x=42; }; }"
1054 '("foo@10:U" "x@23:U" "bar@35:U"))
1056 (js2-deftest-classify-variables unused-nested-function
1057 "function foo() { var i, j=1; function bar() { var x, y=42, z=i; return y; } return i; }"
1058 '("foo@10:U" "i@22:N" 62 84 "j@25:U" "bar@39:U" "x@51:U" "y@54:I" 72 "z@60:U"))
1060 (js2-deftest-classify-variables prop-get-initialized
1061 "function foo () { var x, y={}; y.a=x; }"
1062 '("foo@10:U" "x@23:N" 36 "y@26:I" 32))
1064 (js2-deftest-classify-variables prop-get-uninitialized
1065 "function foo () { var x; if(x.foo) alert('boom'); }"
1066 '("foo@10:U" "x@23:N" 29))
1068 (js2-deftest-classify-variables prop-get-function-assignment
1069 "(function(w) { w.f = function() { var a=42, m; return a; }; })(window);"
1070 '("w@11:P" 11 16 "a@39:I" 55 "m@45:U"))
1072 (js2-deftest-classify-variables let-declaration
1073 "function foo () { let x,y=1; return x; }"
1074 '("foo@10:U" "x@23:N" 37 "y@25:U"))
1076 (js2-deftest-classify-variables external-function-call
1077 "function foo (m) { console.log(m, arguments); }"
1078 '("foo@10:U" "m@15:P" 32))
1080 (js2-deftest-classify-variables global-function-call
1081 "function bar () { return 42; } function foo (a) { return bar(); }"
1082 '("bar@10:I" 58 "foo@41:U" "a@46:P"))
1084 (js2-deftest-classify-variables let-declaration-for-scope
1085 "function foo () { for(let x=1,y; x<y; y++) {} }"
1086 '("foo@10:U" "x@27:I" 34 "y@31:N" 36 39))
1088 (js2-deftest-classify-variables arguments-implicit-var
1089 "function foo () { var p; for(p in arguments) { return p; } }"
1090 '("foo@10:U" "p@23:I" 55))
1092 (js2-deftest-classify-variables catch-error-variable
1093 "function foo () { try { throw 'Foo'; } catch (e) { console.log(e); }"
1094 '("foo@10:U" "e@47:I" 64))
1096 (js2-deftest-classify-variables prop-get-assignment
1097 "function foo () { var x={y:{z:{}}}; x.y.z=42; }"
1098 '("foo@10:U" "x@23:I" 37))
1100 (js2-deftest-classify-variables unused-function-argument
1101 "function foo (a) { return 42; }"
1102 '("foo@10:U" "a@15:P"))
1104 (js2-deftest-classify-variables used-function-argument
1105 "function foo (a) { a=42; return a; }"
1106 '("foo@10:U" "a@15:P" 33))
1108 (js2-deftest-classify-variables prop-get
1109 "function foo (a) { a=navigator.x||navigator.y; return a; }"
1110 '("foo@10:U" "a@15:P" 55))
1112 (js2-deftest-classify-variables for-in-loop
1113 "function foo () { var d={}; for(var k in d) {var v=d[k]; } }"
1114 '("foo@10:U" "d@23:I" 42 52 "k@37:I" 54 "v@50:U"))
1116 (js2-deftest-classify-variables array-comprehension-legacy
1117 "function foo() { var j,a=[for (i of [1,2,3]) i*j]; }"
1118 '("foo@10:U" "j@22:N" 48 "a@24:U" "i@32:I" 46))
1120 (js2-deftest-classify-variables array-comprehension
1121 "function foo() { var j,a=[[i,j] for (i of [1,2,3])]; }"
1122 '("foo@10:U" "j@22:N" 30 "a@24:U" "i@38:I" 28))
1124 (js2-deftest-classify-variables return-named-function
1125 "function foo() { var a=42; return function bar() { return a; } }"
1126 '("foo@10:U" "a@22:I" 59 "bar@44:I" 44))
1128 (js2-deftest-classify-variables named-wrapper-function
1129 "function foo() { var a; (function bar() { a=42; })(); return a; }"
1130 '("foo@10:U" "a@22:I" 62 "bar@35:I" 35))