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-parse object-literal-getter-method
193 "var x = {get f() { return 42;\n}};")
195 (js2-deftest-parse object-literal-setter-method
196 "var x = {set f(y) { x = y;\n}};")
198 (js2-deftest-parse object-literal-computed-keys
199 "var x = {[Symbol.iterator]: function() {}};")
201 (js2-deftest-parse object-literal-computed-function-keys
202 "var x = {[foo + bar](y) { return y;\n}};")
204 (js2-deftest-parse object-literal-computed-getter-key
205 "var x = {get [foo + bar]() { return 42;\n}};")
207 (js2-deftest-parse object-literal-generator
208 "var x = {*foo() { yield 42;\n}};")
210 (js2-deftest-parse object-literal-computed-generator-key
211 "var x = {*[foo + bar]() { yield 42;\n}};")
213 ;;; Function definition
215 (js2-deftest function-redeclaring-var "var gen = 3; function gen() {};"
217 (should (= (length (js2-ast-root-warnings js2-mode-ast)) 1)))
219 (js2-deftest function-expression-var-same-name "var gen = function gen() {};"
221 (should (null (js2-ast-root-warnings js2-mode-ast))))
223 ;;; Function parameters
225 (js2-deftest-parse function-with-default-parameters
226 "function foo(a = 1, b = a + 1) {\n}")
228 (js2-deftest-parse function-with-no-default-after-default
229 "function foo(a = 1, b) {\n}")
231 (js2-deftest-parse function-with-destruct-after-default
232 "function foo(a = 1, {b, c}) {\n}")
234 (js2-deftest-parse function-with-rest-parameter
235 "function foo(a, b, ...rest) {\n}")
237 (js2-deftest-parse function-with-param-after-rest-parameter
238 "function foo(a, ...b, rest) {\n}"
239 :syntax-error "rest")
241 (js2-deftest-parse function-with-destruct-after-rest-parameter
242 "function foo(a, ...b, {}) {\n}"
245 (js2-deftest-parse function-with-rest-after-default-parameter
246 "function foo(a = 1, ...rest) {\n}")
248 ;;; Strict mode errors
250 (js2-deftest-parse function-bad-strict-parameters
251 "'use strict';\nfunction foo(eval, {arguments}, bar) {\n}"
252 :syntax-error "eval" :errors-count 2)
254 (js2-deftest-parse function-retroactive-bad-strict-parameters
255 "function foo(arguments) {'use strict';}"
256 :syntax-error "arguments" :errors-count 1)
258 (js2-deftest-parse function-duplicate-strict-parameters
259 "'use strict';\nfunction foo(a, a) {\n}"
260 :syntax-error "a" :errors-count 1)
262 (js2-deftest-parse function-bad-strict-function-name
263 "'use strict';\nfunction eval() {\n}"
264 :syntax-error "eval" :errors-count 1)
266 (js2-deftest-parse function-bad-retroactive-strict-function-name
267 "function arguments() {'use strict';}"
268 :syntax-error "arguments" :errors-count 1)
270 (js2-deftest-parse function-bad-strict-catch-name
271 "'use strict';\ntry {} catch (eval) {}"
272 :syntax-error "eval" :errors-count 1)
274 (js2-deftest-parse function-bad-strict-variable-name
275 "'use strict';\nvar eval = 'kekeke';"
276 :syntax-error "eval" :errors-count 1)
278 (js2-deftest-parse function-bad-strict-assignment
279 "'use strict';\narguments = 'fufufu';"
280 :syntax-error "arguments" :errors-count 1)
282 (js2-deftest-parse function-property-strict-assignment
283 "'use strict';\narguments.okay = 'alright';")
285 (js2-deftest-parse function-strict-with
286 "'use strict';\nwith ({}) {}"
287 :syntax-error "with" :errors-count 1)
289 (js2-deftest-parse function-strict-octal
290 "'use strict';\nvar number = 0644;"
291 :syntax-error "0644" :errors-count 1)
293 (js2-deftest-parse function-strict-duplicate-keys
294 "'use strict';\nvar object = {a: 1, a: 2, 'a': 3, ['a']: 4, 1: 5, '1': 6, [1 + 1]: 7};"
295 :syntax-error "a" :errors-count 4) ; "a" has 3 dupes, "1" has 1 dupe.
297 (js2-deftest-parse function-strict-duplicate-getter
298 "'use strict';\nvar a = {get x() {}, get x() {}};"
299 :syntax-error "x" :errors-count 1)
301 (js2-deftest-parse function-strict-duplicate-setter
302 "'use strict';\nvar a = {set x() {}, set x() {}};"
303 :syntax-error "x" :errors-count 1)
305 ;;; Lack of errors in strict mode
307 (js2-deftest-parse function-strict-const-scope
308 "'use strict';\nconst a;\nif (1) {\n const a;\n}")
310 (js2-deftest-parse function-strict-no-getter-setter-duplicate
311 "'use strict';\nvar a = {get x() {}, set x() {}};")
315 (js2-deftest-parse spread-in-array-literal
316 "[1, ...[2, 3], 4, ...[5, 6]];")
318 (js2-deftest-parse spread-in-function-call
319 "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")
323 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
324 "() => false;" :reference "() => {false};")
326 (js2-deftest-parse arrow-function-with-args-and-curlies
327 "(a, b = 1, ...c) => { c;\n};")
329 (js2-deftest-parse arrow-function-with-destructuring
330 "([{a}, b]) => { a + b;\n};")
332 (js2-deftest-parse parenless-arrow-function-prohibits-rest
333 "...b => {b + 1;};" :syntax-error "=>")
335 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
336 "[a, b] => {a + b;};" :syntax-error "]" :errors-count 4)
338 (js2-deftest-parse arrow-function-recovers-from-error
339 "[(,foo) => 1];" :syntax-error "," :errors-count 6)
341 ;;; Automatic semicolon insertion
343 (js2-deftest-parse no-auto-semi-insertion-after-if
346 (js2-deftest-parse auto-semi-insertion-after-function
347 "a = function() {}" :reference "a = function() {};")
349 (js2-deftest-parse auto-semi-one-variable-per-line
350 "x\ny" :reference "x;\ny;")
354 (js2-deftest-parse labeled-stmt-node
355 "foo:\nbar:\nx = y + 1;")
357 (js2-deftest no-label-node-inside-expr "x = y:"
358 (let (js2-parse-interruptable-p)
360 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
361 (should (js2-name-node-p (js2-assign-node-right assignment)))))
363 (js2-deftest-parse label-and-loops "for (; ; ) {
372 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
374 (js2-deftest-parse legacy-generator-cannot-return
375 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
377 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
379 (js2-deftest-parse harmony-generator-yield-star "(function*(a) { yield* a;\n});")
383 (js2-deftest-parse parse-legacy-array-comp-loop-with-filter
384 "[a for (a in b) if (a == 2)];")
386 (js2-deftest-parse parse-array-comp-loop-with-filters
387 "[for (a in b) if (a == 2) if (b != 10) a];")
389 (js2-deftest-parse parse-generator-comp-loop-with-filters
390 "(for (x of y) if (x != 4) x);")
392 (js2-deftest-parse parse-array-comp-with-yield-is-ok
393 "(function() { return [for (x of []) yield x];\n});")
395 (js2-deftest-parse parse-generator-comp-with-yield-is-not-ok
396 "(function() { return (for (x of []) yield x);\n});"
397 :syntax-error "yield")
399 (js2-deftest-parse parse-generator-comp-with-yield-inside-function-is-ok
400 "(for (x of []) function*() { yield x;\n});")
404 (js2-deftest-parse decimal-starting-with-zero "081;" :reference "81;")
406 (js2-deftest-parse huge-hex "0x0123456789abcdefABCDEF;" :reference "-1;")
408 (js2-deftest-parse octal-without-o "071;" :reference "57;")
410 (js2-deftest-parse hex-number-okay "0x123;" :reference "291;")
412 (js2-deftest-parse hex-number-broken "0xz23;"
413 :syntax-error "0xz" :errors-count 2)
415 (js2-deftest-parse binary-number-okay "0b101;" :reference "5;")
417 (js2-deftest-parse binary-number-broken "0b210;"
418 :syntax-error "0b2" :errors-count 2)
420 (js2-deftest-parse octal-number-okay "0o765;" :reference "501;")
422 (js2-deftest-parse octal-number-broken "0o812;"
423 :syntax-error "0o8" :errors-count 2)
427 (js2-deftest parse-export-bindings "{one, two as dos}"
429 (should (js2-match-token js2-LC))
430 (let ((imports (js2-parse-export-bindings)))
431 (should (not (equal nil imports)))
432 (should (= 2 (length imports)))
433 (let ((first (nth 0 imports))
434 (second (nth 1 imports)))
435 (should (equal "one" (js2-name-node-name (js2-export-binding-node-extern-name first))))
436 (should (equal "two" (js2-name-node-name (js2-export-binding-node-extern-name second))))
437 (let ((first-name (js2-export-binding-node-local-name first))
438 (second-name (js2-export-binding-node-local-name second)))
439 (should (equal first (js2-node-parent first-name)))
440 (should (equal 3 (js2-node-len first-name)))
441 (should (equal "one" (js2-name-node-name first-name)))
442 (should (equal second (js2-node-parent second-name)))
443 (should (equal 3 (js2-node-len second-name)))
444 (should (equal "dos" (js2-name-node-name second-name)))))))
446 (js2-deftest parse-export-binding-as-default "one as default"
448 (let ((binding (js2-maybe-parse-export-binding)))
450 (should (js2-export-binding-node-p binding))
451 (let ((name (js2-export-binding-node-local-name binding)))
453 (should (equal "default" (js2-name-node-name name))))))
455 (js2-deftest parse-namepsace-import "* as lib;"
457 (should (js2-match-token js2-MUL))
458 (let ((namespace-import (js2-parse-namespace-import)))
459 (should (not (equal nil namespace-import)))
460 (should (js2-namespace-import-node-p namespace-import))
461 (should (= 1 (js2-node-pos namespace-import)))
462 (should (equal 8 (js2-node-len namespace-import)))
463 (let ((name-node (js2-namespace-import-node-name namespace-import)))
464 (should (equal "lib" (js2-name-node-name name-node)))
465 (should (= 5 (js2-node-pos name-node))))))
467 (js2-deftest parse-from-clause "from 'foo/bar';"
469 (let ((from (js2-parse-from-clause)))
470 (should (not (equal nil from)))
471 (should (= 1 (js2-node-pos from)))
472 (should (= 14 (js2-node-len from)))
473 (should (equal "foo/bar" (js2-from-clause-node-module-id from)))))
475 (js2-deftest parse-import-module-id-only "import 'src/lib'"
477 (should (js2-match-token js2-IMPORT))
478 (let ((import (js2-parse-import)))
479 (should (not (equal nil import)))
480 (should (= 1 (js2-node-pos import)))
481 (should (= 16 (js2-node-len import)))
482 (should (equal nil (js2-import-node-import import)))
483 (should (equal nil (js2-import-node-from import)))))
485 (js2-deftest parse-imported-default-binding "import theDefault from 'src/lib'"
486 (js2-push-scope (make-js2-scope :pos 0))
488 (should (js2-match-token js2-IMPORT))
489 (let ((import-node (js2-parse-import)))
490 (should (not (equal nil import-node)))
491 (should (equal "src/lib" (js2-import-node-module-id import-node)))
492 (let ((import (js2-import-node-import import-node)))
493 (should (not (equal nil import)))
494 (should (equal nil (js2-import-clause-node-namespace-import import)))
495 (should (equal nil (js2-import-clause-node-named-imports import)))
496 (let ((default (js2-import-clause-node-default-binding import)))
497 (should (not (equal nil default)))
498 (should (js2-export-binding-node-p default))
499 (should (equal "theDefault" (js2-name-node-name (js2-export-binding-node-extern-name default)))))))
500 (should (js2-scope-get-symbol js2-current-scope "theDefault")))
502 (js2-deftest parse-import-namespace-binding "import * as lib from 'src/lib'"
503 (js2-push-scope (make-js2-scope :pos 0))
505 (should (js2-match-token js2-IMPORT))
506 (let ((import-node (js2-parse-import)))
507 (should (not (equal nil import-node)))
508 (should (equal "src/lib" (js2-import-node-module-id import-node)))
509 (let ((import (js2-import-node-import import-node)))
510 (should (not (equal nil import)))
511 (should (equal nil (js2-import-clause-node-default-binding import)))
512 (should (equal nil (js2-import-clause-node-named-imports import)))
513 (let ((ns-import (js2-import-clause-node-namespace-import import)))
514 (should (not (equal nil ns-import)))
515 (should (js2-namespace-import-node-p ns-import))
516 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
517 (should (js2-scope-get-symbol js2-current-scope "lib")))
519 (js2-deftest parse-import-named-imports "import {foo as bar, baz} from 'src/lib'"
520 (js2-push-scope (make-js2-scope :pos 0))
522 (should (js2-match-token js2-IMPORT))
523 (let ((import-node (js2-parse-import)))
524 (should (not (equal nil import-node)))
525 (should (equal "src/lib" (js2-import-node-module-id import-node)))
526 (let ((import (js2-import-node-import import-node)))
527 (should (not (equal nil import)))
528 (should (equal nil (js2-import-clause-node-default-binding import)))
529 (should (equal nil (js2-import-clause-node-namespace-import import)))
530 (let ((named-imports (js2-import-clause-node-named-imports import)))
531 (should (not (equal nil named-imports)))
532 (should (listp named-imports))
533 (should (= 2 (length named-imports)))
534 (let ((first (nth 0 named-imports))
535 (second (nth 1 named-imports)))
536 (should (equal "bar" (js2-name-node-name (js2-export-binding-node-local-name first))))
537 (should (equal "baz" (js2-name-node-name (js2-export-binding-node-local-name second))))))))
538 (should (js2-scope-get-symbol js2-current-scope "bar"))
539 (should (js2-scope-get-symbol js2-current-scope "baz")))
541 (js2-deftest parse-import-default-and-namespace "import stuff, * as lib from 'src/lib'"
542 (js2-push-scope (make-js2-scope :pos 0))
544 (should (js2-match-token js2-IMPORT))
545 (let ((import-node (js2-parse-import)))
546 (should (not (equal nil import-node)))
547 (should (equal "src/lib" (js2-import-node-module-id import-node)))
548 (let ((import (js2-import-node-import import-node)))
549 (should (not (equal nil import)))
550 (should (equal nil (js2-import-clause-node-named-imports import)))
551 (let ((default (js2-import-clause-node-default-binding import))
552 (ns-import (js2-import-clause-node-namespace-import import)))
553 (should (not (equal nil default)))
554 (should (equal "stuff" (js2-name-node-name (js2-export-binding-node-local-name default))))
555 (should (not (equal nil ns-import)))
556 (should (js2-namespace-import-node-p ns-import))
557 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
558 (should (js2-scope-get-symbol js2-current-scope "stuff"))
559 (should (js2-scope-get-symbol js2-current-scope "lib")))
561 (js2-deftest parse-import-default-and-named-imports
562 "import robert as bob, {cookies, pi as PIE} from 'src/lib'"
563 (js2-push-scope (make-js2-scope :pos 0))
565 (should (js2-match-token js2-IMPORT))
566 (let ((import-node (js2-parse-import)))
567 (should (not (equal nil import-node)))
568 (should (equal "src/lib" (js2-import-node-module-id import-node)))
569 (let ((import (js2-import-node-import import-node)))
570 (should (not (equal nil import)))
571 (should (not (equal nil (js2-import-clause-node-named-imports import))))
572 (let ((default (js2-import-clause-node-default-binding import))
573 (named-imports (js2-import-clause-node-named-imports import)))
574 (should (not (equal nil default)))
575 (should (equal "bob" (js2-name-node-name (js2-export-binding-node-local-name default))))
576 (should (not (equal nil named-imports)))
577 (should (= 2 (length named-imports))))))
578 (should (js2-scope-get-symbol js2-current-scope "bob"))
579 (should (js2-scope-get-symbol js2-current-scope "cookies"))
580 (should (js2-scope-get-symbol js2-current-scope "PIE")))
582 (js2-deftest parse-this-module-in-from-clause "import {url} from this module;"
583 (js2-push-scope (make-js2-scope :pos 0))
585 (should (js2-match-token js2-IMPORT))
586 (let ((import-node (js2-parse-import)))
588 (let ((from-clause (js2-import-node-from import-node)))
590 (should (equal "this" (js2-from-clause-node-module-id from-clause)))
591 (should (js2-from-clause-node-metadata-p from-clause)))))
593 (js2-deftest-parse import-only-for-side-effects "import 'src/lib';")
594 (js2-deftest-parse import-default-only "import theDefault from 'src/lib';")
595 (js2-deftest-parse import-named-only "import {one, two} from 'src/lib';")
596 (js2-deftest-parse import-default-and-named "import theDefault, {one, two} from 'src/lib';")
597 (js2-deftest-parse import-renaming-default "import * as lib from 'src/mylib';")
598 (js2-deftest-parse import-renaming-named "import {one as uno, two as dos} from 'src/lib';")
599 (js2-deftest-parse import-default-and-namespace "import robert as bob, * as lib from 'src/lib';")
600 (js2-deftest-parse import-from-this-module "import {url} from this module;")
604 (js2-deftest export-rexport "export * from 'other/lib'"
606 (should (js2-match-token js2-EXPORT))
607 (let ((export-node (js2-parse-export)))
609 (should (js2-export-node-from-clause export-node))))
611 (js2-deftest export-export-named-list "export {foo, bar as bang};"
613 (should (js2-match-token js2-EXPORT))
614 (let ((export-node (js2-parse-export)))
616 (let ((exports (js2-export-node-exports-list export-node)))
618 (should (= 2 (length exports))))))
620 (js2-deftest re-export-named-list "export {foo, bar as bang} from 'other/lib'"
622 (should (js2-match-token js2-EXPORT))
623 (let ((export-node (js2-parse-export)))
625 (should (js2-export-node-from-clause export-node))
626 (let ((exports (js2-export-node-exports-list export-node)))
628 (should (= 2 (length exports))))))
630 (js2-deftest export-variable-statement "export var foo = 'bar', baz = 'bang';"
632 (js2-push-scope (make-js2-scope :pos 0))
633 (should (js2-match-token js2-EXPORT))
634 (let ((export-node (js2-parse-export)))
636 (should (js2-export-node-declaration export-node))))
638 (js2-deftest export-const-declaration "export const PI = Math.PI;"
640 (js2-push-scope (make-js2-scope :pos 0))
641 (should (js2-match-token js2-EXPORT))
642 (let ((export-node (js2-parse-export)))
644 (should (js2-export-node-declaration export-node))))
646 (js2-deftest export-let-declaration "export let foo = [1];"
648 (js2-push-scope (make-js2-scope :pos 0))
649 (should (js2-match-token js2-EXPORT))
650 (let ((export-node (js2-parse-export)))
652 (should (js2-var-decl-node-p (js2-export-node-declaration export-node)))))
654 (js2-deftest export-class-declaration "export class Foo {};"
656 (js2-push-scope (make-js2-scope :pos 0))
657 (should (js2-match-token js2-EXPORT))
658 (let ((export-node (js2-parse-export)))
660 (should (js2-class-node-p (js2-export-node-declaration export-node)))))
662 (js2-deftest export-function-declaration "export default function doStuff() {};"
664 (js2-push-scope (make-js2-scope :pos 0))
665 (should (js2-match-token js2-EXPORT))
666 (let ((export-node (js2-parse-export)))
668 (should (js2-export-node-default export-node))))
670 (js2-deftest export-generator-declaration "export default function* one() {};"
672 (js2-push-scope (make-js2-scope :pos 0))
673 (should (js2-match-token js2-EXPORT))
674 (let ((export-node (js2-parse-export)))
676 (should (js2-export-node-default export-node))))
678 (js2-deftest export-assignment-expression "export default a = b;"
680 (js2-push-scope (make-js2-scope :pos 0))
681 (should (js2-match-token js2-EXPORT))
682 (let ((export-node (js2-parse-export)))
684 (should (js2-export-node-default export-node))))
686 (js2-deftest export-function-no-semicolon "export default function foo() {}"
688 (should (equal nil js2-parsed-warnings)))
689 (js2-deftest export-default-function-no-semicolon "export function foo() {}"
691 (should (equal nil js2-parsed-warnings)))
692 (js2-deftest export-anything-else-does-require-a-semicolon "export var obj = {}"
694 (should (not (equal nil js2-parsed-warnings))))
696 (js2-deftest-parse parse-export-rexport "export * from 'other/lib';")
697 (js2-deftest-parse parse-export-export-named-list "export {foo, bar as bang};")
698 (js2-deftest-parse parse-re-export-named-list "export {foo, bar as bang} from 'other/lib';")
699 (js2-deftest-parse parse-export-const-declaration "export const PI = Math.PI;")
700 (js2-deftest-parse parse-export-let-declaration "export let foo = [1];")
701 (js2-deftest-parse parse-export-function-declaration "export default function doStuff() {};")
702 (js2-deftest-parse parse-export-generator-declaration "export default function* one() {};")
703 (js2-deftest-parse parse-export-assignment-expression "export default a = b;")
707 (js2-deftest-parse string-literal
710 (js2-deftest-parse object-get-string-literal
711 "var x = {y: 5};\nvar z = x[\"y\"];")
713 (js2-deftest-parse template-no-substritutions
715 def`, y = `\\u0000`;")
717 (js2-deftest-parse template-with-substitutions
718 "var y = `${a + b} ${d + e + f}`;")
720 (js2-deftest-parse tagged-template
721 "foo.args`${++x, \"o\"}k`;")
725 (js2-deftest-parse parse-harmony-class-statement
726 "class Foo {\n get bar() { return 42;\n}\n set bar(x) { y = x;\n}\n}")
728 (js2-deftest-parse parse-harmony-class-statement-without-name-is-not-ok
729 "class {\n get bar() { return 42;\n}\n}"
732 (js2-deftest-parse parse-harmony-class-expression
733 "var Foo1 = class Foo {\n bar() { return 42;\n}\n};")
735 (js2-deftest-parse parse-harmony-anonymous-class-expression
736 "var Foo = class {\n set bar(x) { bar = x;\n}\n};")
738 (js2-deftest-parse parse-harmony-class-with-extends
739 "class Foo extends Bar {\n}")
741 (js2-deftest-parse parse-harmony-anonymous-class-with-extends
742 "foo.Foo = class extends Bar {\n set bar(x) { bar = x;\n}\n};")
744 (js2-deftest-parse parse-harmony-class-with-complex-extends
745 "class Foo extends foo[BAR][2].Baz {\n}")
747 (js2-deftest-parse parse-harmony-class-missing-extended-class-is-not-ok
748 "class Foo extends {\n}"
749 :syntax-error "extends")
751 (js2-deftest-parse parse-harmony-class-static-method
752 "class Foo extends Bar {\n static bar() { return 42;\n}\n}")
754 (js2-deftest-parse parse-unterminated-class-is-not-okay
755 "class Foo {\n get bar() { return 42;\n}"
758 (js2-deftest-parse parse-super-keyword
759 "class Foo {\n constructor() { super(42);\n}\n foo() { super.foo();\n}\n}")
761 (js2-deftest-parse parse-class-keywordlike-method
762 "class C {\n delete() {}\n if() {}\n}")
766 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
768 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
769 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
770 (should (equal (js2-symbol-name entry) "foo"))
771 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
773 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
778 (let* ((scope (js2-node-at-point (point-min)))
779 (fn-entry (js2-scope-get-symbol scope 'bar))
780 (var-entry (js2-scope-get-symbol scope 'x)))
781 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
782 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
783 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
784 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
785 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
787 (defun js2-test-scope-of-nth-variable-satisifies-predicate (variable nth predicate)
788 (goto-char (point-min))
789 (dotimes (n (1+ nth)) (search-forward variable))
791 (let ((scope (js2-node-get-enclosing-scope (js2-node-at-point))))
792 (should (funcall predicate (js2-get-defining-scope scope variable)))))
794 (js2-deftest for-node-is-declaration-scope "for (let i = 0; i; ++i) {};"
796 (js2-test-scope-of-nth-variable-satisifies-predicate "i" 0 #'js2-for-node-p))
798 (js2-deftest const-scope-sloppy-script "{const a;} a;"
800 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-script-node-p)
801 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'js2-script-node-p))
803 (js2-deftest const-scope-strict-script "'use strict'; { const a; } a;"
805 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-block-node-p)
806 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'null))
808 (js2-deftest const-scope-sloppy-function "function f() { { const a; } a; }"
810 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-function-node-p)
811 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'js2-function-node-p))
813 (js2-deftest const-scope-strict-function "function f() { 'use strict'; { const a; } a; }"
815 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-block-node-p)
816 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'null))
818 (js2-deftest array-comp-is-result-scope "[x * 2 for (x in y)];"
820 (js2-test-scope-of-nth-variable-satisifies-predicate "x" 0 #'js2-comp-loop-node-p))
822 (js2-deftest array-comp-has-parent-scope
823 "var a,b=[for (i of [[1,2]]) for (j of i) j * a];"
825 (search-forward "for")
827 (let ((node (js2-node-at-point)))
828 (should (js2-scope-parent-scope node))
829 (should (js2-get-defining-scope node "j"))))
833 (js2-deftest get-token "(1+1)"
835 (should (eq js2-LP (js2-next-token)))
836 (should (eq js2-NUMBER (js2-next-token)))
837 (should (eq js2-ADD (js2-next-token)))
838 (should (eq js2-NUMBER (js2-next-token)))
839 (should (eq js2-RP (js2-next-token))))
841 (js2-deftest unget-token "()"
843 (should (eq js2-LP (js2-next-token)))
845 (should (eq js2-LP (js2-next-token)))
846 (should (eq js2-RP (js2-next-token))))
848 (js2-deftest get-token-or-eol "x\n++;"
850 (should (eq js2-NAME (js2-next-token)))
851 (should (eq js2-EOL (js2-peek-token-or-eol)))
852 (should (eq js2-INC (js2-next-token)))
853 (should (eq js2-SEMI (js2-peek-token-or-eol))))
855 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
857 (should (eq js2-NAME (js2-next-token)))
858 (should (eq js2-NAME (js2-next-token)))
859 (should (equal "y" (js2-current-token-string)))
861 (should (eq js2-NAME (js2-current-token-type)))
862 (should (equal "x" (js2-current-token-string))))
864 (js2-deftest ts-seek "(1+2)"
866 (should (eq js2-LP (js2-next-token)))
867 (should (eq js2-NUMBER (js2-next-token)))
869 (let ((state (make-js2-ts-state)))
870 (should (eq js2-NUMBER (js2-next-token)))
871 (should (eq js2-ADD (js2-next-token)))
873 (should (eq 1 js2-ti-lookahead))
874 (should (eq js2-NUMBER (js2-next-token)))
875 (should (eq 1 (js2-token-number
876 (js2-current-token))))))
878 (js2-deftest get-token-template-literal "`abc ${i} z ${j} def`"
880 (should (eq js2-TEMPLATE_HEAD (js2-next-token)))
881 (should (equal "abc " (js2-current-token-string)))
882 (should (eq js2-NAME (js2-next-token)))
883 (should (eq js2-RC (js2-next-token)))
884 (should (eq js2-TEMPLATE_HEAD (js2-next-token 'TEMPLATE_TAIL)))
885 (should (equal " z " (js2-current-token-string)))
886 (should (eq js2-NAME (js2-next-token)))
887 (should (eq js2-RC (js2-next-token)))
888 (should (eq js2-NO_SUBS_TEMPLATE (js2-next-token 'TEMPLATE_TAIL)))
889 (should (equal " def" (js2-current-token-string))))
893 (js2-deftest for-node-with-error-len "for "
895 (let ((node (js2-node-at-point (point-min))))
896 (should (= (js2-node-len (js2-node-parent node)) 4))))
898 (js2-deftest function-without-parens-error "function b {}"
899 ;; Should finish the parse.
904 (js2-deftest comment-node-length "//"
906 (let ((node (js2-node-at-point (point-min))))
907 (should (= (js2-node-len node) 2))))
909 (js2-deftest comment-node-length-newline "//\n"
911 (let ((node (js2-node-at-point (point-min))))
912 (should (= (js2-node-len node) 3))))
914 ;;; Variables classification
916 (defun js2--variables-summary (vars)
918 (setq vars (let (aslist)
919 (maphash (lambda (k v) (push (cons k v) aslist)) vars)
921 (dolist (v (sort vars (lambda (a b) (< (js2-node-abs-pos (js2-symbol-ast-node (car a)))
922 (js2-node-abs-pos (js2-symbol-ast-node (car b)))))))
923 (let* ((symbol (car v))
926 (symn (js2-symbol-ast-node symbol))
927 (namen (js2--get-name-node symn)))
928 (push (format "%s@%s:%s"
929 (js2-symbol-name symbol)
930 (js2-node-abs-pos namen)
936 (dolist (u (sort (cddr v) (lambda (a b) (< (js2-node-abs-pos a)
937 (js2-node-abs-pos b)))))
938 (push (js2-node-abs-pos u) r))))
941 (defmacro js2-deftest-classify-variables (name buffer-contents summary)
942 (declare (indent defun))
943 `(ert-deftest ,(intern (format "js2-classify-variables-%s" name)) ()
946 (insert ,buffer-contents))
950 (should (equal ,summary (js2--variables-summary
951 (js2--classify-variables)))))
952 (fundamental-mode)))))
954 (js2-deftest-classify-variables incomplete-var-statement
958 (js2-deftest-classify-variables unused-variable
959 "function foo () { var x; return 42; }"
960 '("foo@10:U" "x@23:U"))
962 (js2-deftest-classify-variables unused-variable-declared-twice
963 "function foo (a) { var x; function bar () { var x; x=42; }; return a;}"
964 '("foo@10:U" "a@15:P" 68 "x@24:U" "bar@36:U" "x@49:U"))
966 (js2-deftest-classify-variables assigned-variable
967 "function foo () { var x; x=42; return x; }"
968 '("foo@10:U" "x@23:I" 39))
970 (js2-deftest-classify-variables assignment-in-nested-function
971 "function foo () { var x; function bar () { x=42; }; }"
972 '("foo@10:U" "x@23:U" "bar@35:U"))
974 (js2-deftest-classify-variables unused-nested-function
975 "function foo() { var i, j=1; function bar() { var x, y=42, z=i; return y; } return i; }"
976 '("foo@10:U" "i@22:N" 62 84 "j@25:U" "bar@39:U" "x@51:U" "y@54:I" 72 "z@60:U"))
978 (js2-deftest-classify-variables prop-get-initialized
979 "function foo () { var x, y={}; y.a=x; }"
980 '("foo@10:U" "x@23:N" 36 "y@26:I" 32))
982 (js2-deftest-classify-variables prop-get-uninitialized
983 "function foo () { var x; if(x.foo) alert('boom'); }"
984 '("foo@10:U" "x@23:N" 29))
986 (js2-deftest-classify-variables prop-get-function-assignment
987 "(function(w) { w.f = function() { var a=42, m; return a; }; })(window);"
988 '("w@11:P" 11 16 "a@39:I" 55 "m@45:U"))
990 (js2-deftest-classify-variables let-declaration
991 "function foo () { let x,y=1; return x; }"
992 '("foo@10:U" "x@23:N" 37 "y@25:U"))
994 (js2-deftest-classify-variables external-function-call
995 "function foo (m) { console.log(m, arguments); }"
996 '("foo@10:U" "m@15:P" 32))
998 (js2-deftest-classify-variables global-function-call
999 "function bar () { return 42; } function foo (a) { return bar(); }"
1000 '("bar@10:I" 58 "foo@41:U" "a@46:P"))
1002 (js2-deftest-classify-variables let-declaration-for-scope
1003 "function foo () { for(let x=1,y; x<y; y++) {} }"
1004 '("foo@10:U" "x@27:I" 34 "y@31:N" 36 39))
1006 (js2-deftest-classify-variables arguments-implicit-var
1007 "function foo () { var p; for(p in arguments) { return p; } }"
1008 '("foo@10:U" "p@23:I" 55))
1010 (js2-deftest-classify-variables catch-error-variable
1011 "function foo () { try { throw 'Foo'; } catch (e) { console.log(e); }"
1012 '("foo@10:U" "e@47:I" 64))
1014 (js2-deftest-classify-variables prop-get-assignment
1015 "function foo () { var x={y:{z:{}}}; x.y.z=42; }"
1016 '("foo@10:U" "x@23:I" 37))
1018 (js2-deftest-classify-variables unused-function-argument
1019 "function foo (a) { return 42; }"
1020 '("foo@10:U" "a@15:P"))
1022 (js2-deftest-classify-variables used-function-argument
1023 "function foo (a) { a=42; return a; }"
1024 '("foo@10:U" "a@15:P" 33))
1026 (js2-deftest-classify-variables prop-get
1027 "function foo (a) { a=navigator.x||navigator.y; return a; }"
1028 '("foo@10:U" "a@15:P" 55))
1030 (js2-deftest-classify-variables for-in-loop
1031 "function foo () { var d={}; for(var k in d) {var v=d[k]; } }"
1032 '("foo@10:U" "d@23:I" 42 52 "k@37:I" 54 "v@50:U"))
1034 (js2-deftest-classify-variables array-comprehension-legacy
1035 "function foo() { var j,a=[for (i of [1,2,3]) i*j]; }"
1036 '("foo@10:U" "j@22:N" 48 "a@24:U" "i@32:I" 46))
1038 (js2-deftest-classify-variables array-comprehension
1039 "function foo() { var j,a=[[i,j] for (i of [1,2,3])]; }"
1040 '("foo@10:U" "j@22:N" 30 "a@24:U" "i@38:I" 28))
1042 (js2-deftest-classify-variables return-named-function
1043 "function foo() { var a=42; return function bar() { return a; } }"
1044 '("foo@10:U" "a@22:I" 59 "bar@44:I" 44))
1046 (js2-deftest-classify-variables named-wrapper-function
1047 "function foo() { var a; (function bar() { a=42; })(); return a; }"
1048 '("foo@10:U" "a@22:I" 62 "bar@35:I" 35))