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}"
232 (js2-deftest-parse function-with-destruct-after-default
233 "function foo(a = 1, {b, c}) {\n}"
236 (js2-deftest-parse function-with-rest-parameter
237 "function foo(a, b, ...rest) {\n}")
239 (js2-deftest-parse function-with-param-after-rest-parameter
240 "function foo(a, ...b, rest) {\n}"
241 :syntax-error "rest")
243 (js2-deftest-parse function-with-destruct-after-rest-parameter
244 "function foo(a, ...b, {}) {\n}"
247 (js2-deftest-parse function-with-rest-after-default-parameter
248 "function foo(a = 1, ...rest) {\n}")
250 ;;; Strict identifiers
252 (js2-deftest-parse function-bad-strict-parameters
253 "'use strict';\nfunction foo(eval, {arguments}, bar) {\n}"
254 :syntax-error "eval" :errors-count 2)
256 (js2-deftest-parse function-retroactive-bad-strict-parameters
257 "function foo(arguments) {'use strict';}"
258 :syntax-error "arguments" :errors-count 1)
260 (js2-deftest-parse function-duplicate-strict-parameters
261 "'use strict';\nfunction foo(a, a) {\n}"
262 :syntax-error "a" :errors-count 1)
264 (js2-deftest-parse function-bad-strict-function-name
265 "'use strict';\nfunction eval() {\n}"
266 :syntax-error "eval" :errors-count 1)
268 (js2-deftest-parse function-bad-retroactive-strict-function-name
269 "function arguments() {'use strict';}"
270 :syntax-error "arguments" :errors-count 1)
272 (js2-deftest-parse function-bad-strict-catch-name
273 "'use strict';\ntry {} catch (eval) {}"
274 :syntax-error "eval" :errors-count 1)
276 (js2-deftest-parse function-bad-strict-variable-name
277 "'use strict';\nvar eval = 'kekeke';"
278 :syntax-error "eval" :errors-count 1)
280 (js2-deftest-parse function-bad-strict-assignment
281 "'use strict';\narguments = 'fufufu';"
282 :syntax-error "arguments" :errors-count 1)
284 ;;; Strict syntax errors
286 (js2-deftest-parse function-strict-with
287 "'use strict';\nwith ({}) {}"
288 :syntax-error "with" :errors-count 1)
290 (js2-deftest-parse function-strict-octal
291 "'use strict';\nvar number = 0644;"
292 :syntax-error "0644" :errors-count 1)
296 (js2-deftest-parse spread-in-array-literal
297 "[1, ...[2, 3], 4, ...[5, 6]];")
299 (js2-deftest-parse spread-in-function-call
300 "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")
304 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
305 "() => false;" :reference "() => {false};")
307 (js2-deftest-parse arrow-function-with-args-and-curlies
308 "(a, b = 1, ...c) => { c;\n};")
310 (js2-deftest-parse arrow-function-with-destructuring
311 "([{a}, b]) => { a + b;\n};")
313 (js2-deftest-parse parenless-arrow-function-prohibits-rest
314 "...b => {b + 1;};" :syntax-error "=>")
316 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
317 "[a, b] => {a + b;};" :syntax-error "]" :errors-count 4)
319 (js2-deftest-parse arrow-function-recovers-from-error
320 "[(,foo) => 1];" :syntax-error "," :errors-count 6)
322 ;;; Automatic semicolon insertion
324 (js2-deftest-parse no-auto-semi-insertion-after-if
327 (js2-deftest-parse auto-semi-insertion-after-function
328 "a = function() {}" :reference "a = function() {};")
330 (js2-deftest-parse auto-semi-one-variable-per-line
331 "x\ny" :reference "x;\ny;")
335 (js2-deftest-parse labeled-stmt-node
336 "foo:\nbar:\nx = y + 1;")
338 (js2-deftest no-label-node-inside-expr "x = y:"
339 (let (js2-parse-interruptable-p)
341 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
342 (should (js2-name-node-p (js2-assign-node-right assignment)))))
344 (js2-deftest-parse label-and-loops "for (; ; ) {
353 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
355 (js2-deftest-parse legacy-generator-cannot-return
356 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
358 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
360 (js2-deftest-parse harmony-generator-yield-star "(function*(a) { yield* a;\n});")
364 (js2-deftest-parse parse-legacy-array-comp-loop-with-filter
365 "[a for (a in b) if (a == 2)];")
367 (js2-deftest-parse parse-array-comp-loop-with-filters
368 "[for (a in b) if (a == 2) if (b != 10) a];")
370 (js2-deftest-parse parse-generator-comp-loop-with-filters
371 "(for (x of y) if (x != 4) x);")
373 (js2-deftest-parse parse-array-comp-with-yield-is-ok
374 "(function() { return [for (x of []) yield x];\n});")
376 (js2-deftest-parse parse-generator-comp-with-yield-is-not-ok
377 "(function() { return (for (x of []) yield x);\n});"
378 :syntax-error "yield")
380 (js2-deftest-parse parse-generator-comp-with-yield-inside-function-is-ok
381 "(for (x of []) function*() { yield x;\n});")
385 (js2-deftest-parse decimal-starting-with-zero "081;" :reference "81;")
387 (js2-deftest-parse huge-hex "0x0123456789abcdefABCDEF;" :reference "-1;")
389 (js2-deftest-parse octal-without-o "071;" :reference "57;")
391 (js2-deftest-parse hex-number-okay "0x123;" :reference "291;")
393 (js2-deftest-parse hex-number-broken "0xz23;"
394 :syntax-error "0xz" :errors-count 2)
396 (js2-deftest-parse binary-number-okay "0b101;" :reference "5;")
398 (js2-deftest-parse binary-number-broken "0b210;"
399 :syntax-error "0b2" :errors-count 2)
401 (js2-deftest-parse octal-number-okay "0o765;" :reference "501;")
403 (js2-deftest-parse octal-number-broken "0o812;"
404 :syntax-error "0o8" :errors-count 2)
408 (js2-deftest parse-export-bindings "{one, two as dos}"
410 (should (js2-match-token js2-LC))
411 (let ((imports (js2-parse-export-bindings)))
412 (should (not (equal nil imports)))
413 (should (= 2 (length imports)))
414 (let ((first (nth 0 imports))
415 (second (nth 1 imports)))
416 (should (equal "one" (js2-name-node-name (js2-export-binding-node-extern-name first))))
417 (should (equal "two" (js2-name-node-name (js2-export-binding-node-extern-name second))))
418 (let ((first-name (js2-export-binding-node-local-name first))
419 (second-name (js2-export-binding-node-local-name second)))
420 (should (equal first (js2-node-parent first-name)))
421 (should (equal 3 (js2-node-len first-name)))
422 (should (equal "one" (js2-name-node-name first-name)))
423 (should (equal second (js2-node-parent second-name)))
424 (should (equal 3 (js2-node-len second-name)))
425 (should (equal "dos" (js2-name-node-name second-name)))))))
427 (js2-deftest parse-export-binding-as-default "one as default"
429 (let ((binding (js2-maybe-parse-export-binding)))
431 (should (js2-export-binding-node-p binding))
432 (let ((name (js2-export-binding-node-local-name binding)))
434 (should (equal "default" (js2-name-node-name name))))))
436 (js2-deftest parse-namepsace-import "* as lib;"
438 (should (js2-match-token js2-MUL))
439 (let ((namespace-import (js2-parse-namespace-import)))
440 (should (not (equal nil namespace-import)))
441 (should (js2-namespace-import-node-p namespace-import))
442 (should (= 1 (js2-node-pos namespace-import)))
443 (should (equal 8 (js2-node-len namespace-import)))
444 (let ((name-node (js2-namespace-import-node-name namespace-import)))
445 (should (equal "lib" (js2-name-node-name name-node)))
446 (should (= 5 (js2-node-pos name-node))))))
448 (js2-deftest parse-from-clause "from 'foo/bar';"
450 (let ((from (js2-parse-from-clause)))
451 (should (not (equal nil from)))
452 (should (= 1 (js2-node-pos from)))
453 (should (= 14 (js2-node-len from)))
454 (should (equal "foo/bar" (js2-from-clause-node-module-id from)))))
456 (js2-deftest parse-import-module-id-only "import 'src/lib'"
458 (should (js2-match-token js2-IMPORT))
459 (let ((import (js2-parse-import)))
460 (should (not (equal nil import)))
461 (should (= 1 (js2-node-pos import)))
462 (should (= 16 (js2-node-len import)))
463 (should (equal nil (js2-import-node-import import)))
464 (should (equal nil (js2-import-node-from import)))))
466 (js2-deftest parse-imported-default-binding "import theDefault from 'src/lib'"
467 (js2-push-scope (make-js2-scope :pos 0))
469 (should (js2-match-token js2-IMPORT))
470 (let ((import-node (js2-parse-import)))
471 (should (not (equal nil import-node)))
472 (should (equal "src/lib" (js2-import-node-module-id import-node)))
473 (let ((import (js2-import-node-import import-node)))
474 (should (not (equal nil import)))
475 (should (equal nil (js2-import-clause-node-namespace-import import)))
476 (should (equal nil (js2-import-clause-node-named-imports import)))
477 (let ((default (js2-import-clause-node-default-binding import)))
478 (should (not (equal nil default)))
479 (should (js2-export-binding-node-p default))
480 (should (equal "theDefault" (js2-name-node-name (js2-export-binding-node-extern-name default)))))))
481 (should (js2-scope-get-symbol js2-current-scope "theDefault")))
483 (js2-deftest parse-import-namespace-binding "import * as lib from 'src/lib'"
484 (js2-push-scope (make-js2-scope :pos 0))
486 (should (js2-match-token js2-IMPORT))
487 (let ((import-node (js2-parse-import)))
488 (should (not (equal nil import-node)))
489 (should (equal "src/lib" (js2-import-node-module-id import-node)))
490 (let ((import (js2-import-node-import import-node)))
491 (should (not (equal nil import)))
492 (should (equal nil (js2-import-clause-node-default-binding import)))
493 (should (equal nil (js2-import-clause-node-named-imports import)))
494 (let ((ns-import (js2-import-clause-node-namespace-import import)))
495 (should (not (equal nil ns-import)))
496 (should (js2-namespace-import-node-p ns-import))
497 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
498 (should (js2-scope-get-symbol js2-current-scope "lib")))
500 (js2-deftest parse-import-named-imports "import {foo as bar, baz} from 'src/lib'"
501 (js2-push-scope (make-js2-scope :pos 0))
503 (should (js2-match-token js2-IMPORT))
504 (let ((import-node (js2-parse-import)))
505 (should (not (equal nil import-node)))
506 (should (equal "src/lib" (js2-import-node-module-id import-node)))
507 (let ((import (js2-import-node-import import-node)))
508 (should (not (equal nil import)))
509 (should (equal nil (js2-import-clause-node-default-binding import)))
510 (should (equal nil (js2-import-clause-node-namespace-import import)))
511 (let ((named-imports (js2-import-clause-node-named-imports import)))
512 (should (not (equal nil named-imports)))
513 (should (listp named-imports))
514 (should (= 2 (length named-imports)))
515 (let ((first (nth 0 named-imports))
516 (second (nth 1 named-imports)))
517 (should (equal "bar" (js2-name-node-name (js2-export-binding-node-local-name first))))
518 (should (equal "baz" (js2-name-node-name (js2-export-binding-node-local-name second))))))))
519 (should (js2-scope-get-symbol js2-current-scope "bar"))
520 (should (js2-scope-get-symbol js2-current-scope "baz")))
522 (js2-deftest parse-import-default-and-namespace "import stuff, * as lib from 'src/lib'"
523 (js2-push-scope (make-js2-scope :pos 0))
525 (should (js2-match-token js2-IMPORT))
526 (let ((import-node (js2-parse-import)))
527 (should (not (equal nil import-node)))
528 (should (equal "src/lib" (js2-import-node-module-id import-node)))
529 (let ((import (js2-import-node-import import-node)))
530 (should (not (equal nil import)))
531 (should (equal nil (js2-import-clause-node-named-imports import)))
532 (let ((default (js2-import-clause-node-default-binding import))
533 (ns-import (js2-import-clause-node-namespace-import import)))
534 (should (not (equal nil default)))
535 (should (equal "stuff" (js2-name-node-name (js2-export-binding-node-local-name default))))
536 (should (not (equal nil ns-import)))
537 (should (js2-namespace-import-node-p ns-import))
538 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
539 (should (js2-scope-get-symbol js2-current-scope "stuff"))
540 (should (js2-scope-get-symbol js2-current-scope "lib")))
542 (js2-deftest parse-import-default-and-named-imports
543 "import robert as bob, {cookies, pi as PIE} from 'src/lib'"
544 (js2-push-scope (make-js2-scope :pos 0))
546 (should (js2-match-token js2-IMPORT))
547 (let ((import-node (js2-parse-import)))
548 (should (not (equal nil import-node)))
549 (should (equal "src/lib" (js2-import-node-module-id import-node)))
550 (let ((import (js2-import-node-import import-node)))
551 (should (not (equal nil import)))
552 (should (not (equal nil (js2-import-clause-node-named-imports import))))
553 (let ((default (js2-import-clause-node-default-binding import))
554 (named-imports (js2-import-clause-node-named-imports import)))
555 (should (not (equal nil default)))
556 (should (equal "bob" (js2-name-node-name (js2-export-binding-node-local-name default))))
557 (should (not (equal nil named-imports)))
558 (should (= 2 (length named-imports))))))
559 (should (js2-scope-get-symbol js2-current-scope "bob"))
560 (should (js2-scope-get-symbol js2-current-scope "cookies"))
561 (should (js2-scope-get-symbol js2-current-scope "PIE")))
563 (js2-deftest parse-this-module-in-from-clause "import {url} from this module;"
564 (js2-push-scope (make-js2-scope :pos 0))
566 (should (js2-match-token js2-IMPORT))
567 (let ((import-node (js2-parse-import)))
569 (let ((from-clause (js2-import-node-from import-node)))
571 (should (equal "this" (js2-from-clause-node-module-id from-clause)))
572 (should (js2-from-clause-node-metadata-p from-clause)))))
574 (js2-deftest-parse import-only-for-side-effects "import 'src/lib';")
575 (js2-deftest-parse import-default-only "import theDefault from 'src/lib';")
576 (js2-deftest-parse import-named-only "import {one, two} from 'src/lib';")
577 (js2-deftest-parse import-default-and-named "import theDefault, {one, two} from 'src/lib';")
578 (js2-deftest-parse import-renaming-default "import * as lib from 'src/mylib';")
579 (js2-deftest-parse import-renaming-named "import {one as uno, two as dos} from 'src/lib';")
580 (js2-deftest-parse import-default-and-namespace "import robert as bob, * as lib from 'src/lib';")
581 (js2-deftest-parse import-from-this-module "import {url} from this module;")
585 (js2-deftest export-rexport "export * from 'other/lib'"
587 (should (js2-match-token js2-EXPORT))
588 (let ((export-node (js2-parse-export)))
590 (should (js2-export-node-from-clause export-node))))
592 (js2-deftest export-export-named-list "export {foo, bar as bang};"
594 (should (js2-match-token js2-EXPORT))
595 (let ((export-node (js2-parse-export)))
597 (let ((exports (js2-export-node-exports-list export-node)))
599 (should (= 2 (length exports))))))
601 (js2-deftest re-export-named-list "export {foo, bar as bang} from 'other/lib'"
603 (should (js2-match-token js2-EXPORT))
604 (let ((export-node (js2-parse-export)))
606 (should (js2-export-node-from-clause export-node))
607 (let ((exports (js2-export-node-exports-list export-node)))
609 (should (= 2 (length exports))))))
611 (js2-deftest export-variable-statement "export var foo = 'bar', baz = 'bang';"
613 (js2-push-scope (make-js2-scope :pos 0))
614 (should (js2-match-token js2-EXPORT))
615 (let ((export-node (js2-parse-export)))
617 (should (js2-export-node-declaration export-node))))
619 (js2-deftest export-const-declaration "export const PI = Math.PI;"
621 (js2-push-scope (make-js2-scope :pos 0))
622 (should (js2-match-token js2-EXPORT))
623 (let ((export-node (js2-parse-export)))
625 (should (js2-export-node-declaration export-node))))
627 (js2-deftest export-let-declaration "export let foo = [1];"
629 (js2-push-scope (make-js2-scope :pos 0))
630 (should (js2-match-token js2-EXPORT))
631 (let ((export-node (js2-parse-export)))
633 (should (js2-var-decl-node-p (js2-export-node-declaration export-node)))))
635 (js2-deftest export-class-declaration "export class Foo {};"
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-class-node-p (js2-export-node-declaration export-node)))))
643 (js2-deftest export-function-declaration "export default function doStuff() {};"
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-default export-node))))
651 (js2-deftest export-generator-declaration "export default function* one() {};"
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-export-node-default export-node))))
659 (js2-deftest export-assignment-expression "export default a = b;"
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-export-node-default export-node))))
667 (js2-deftest export-function-no-semicolon "export default function foo() {}"
669 (should (equal nil js2-parsed-warnings)))
670 (js2-deftest export-default-function-no-semicolon "export function foo() {}"
672 (should (equal nil js2-parsed-warnings)))
673 (js2-deftest export-anything-else-does-require-a-semicolon "export var obj = {}"
675 (should (not (equal nil js2-parsed-warnings))))
677 (js2-deftest-parse parse-export-rexport "export * from 'other/lib';")
678 (js2-deftest-parse parse-export-export-named-list "export {foo, bar as bang};")
679 (js2-deftest-parse parse-re-export-named-list "export {foo, bar as bang} from 'other/lib';")
680 (js2-deftest-parse parse-export-const-declaration "export const PI = Math.PI;")
681 (js2-deftest-parse parse-export-let-declaration "export let foo = [1];")
682 (js2-deftest-parse parse-export-function-declaration "export default function doStuff() {};")
683 (js2-deftest-parse parse-export-generator-declaration "export default function* one() {};")
684 (js2-deftest-parse parse-export-assignment-expression "export default a = b;")
688 (js2-deftest-parse string-literal
691 (js2-deftest-parse object-get-string-literal
692 "var x = {y: 5};\nvar z = x[\"y\"];")
694 (js2-deftest-parse template-no-substritutions
696 def`, y = `\\u0000`;")
698 (js2-deftest-parse template-with-substitutions
699 "var y = `${a + b} ${d + e + f}`;")
701 (js2-deftest-parse tagged-template
702 "foo.args`${++x, \"o\"}k`;")
706 (js2-deftest-parse parse-harmony-class-statement
707 "class Foo {\n get bar() { return 42;\n}\n set bar(x) { y = x;\n}\n}")
709 (js2-deftest-parse parse-harmony-class-statement-without-name-is-not-ok
710 "class {\n get bar() { return 42;\n}\n}"
713 (js2-deftest-parse parse-harmony-class-expression
714 "var Foo1 = class Foo {\n bar() { return 42;\n}\n};")
716 (js2-deftest-parse parse-harmony-anonymous-class-expression
717 "var Foo = class {\n set bar(x) { bar = x;\n}\n};")
719 (js2-deftest-parse parse-harmony-class-with-extends
720 "class Foo extends Bar {\n}")
722 (js2-deftest-parse parse-harmony-anonymous-class-with-extends
723 "foo.Foo = class extends Bar {\n set bar(x) { bar = x;\n}\n};")
725 (js2-deftest-parse parse-harmony-class-with-complex-extends
726 "class Foo extends foo[BAR][2].Baz {\n}")
728 (js2-deftest-parse parse-harmony-class-missing-extended-class-is-not-ok
729 "class Foo extends {\n}"
730 :syntax-error "extends")
732 (js2-deftest-parse parse-harmony-class-static-method
733 "class Foo extends Bar {\n static bar() { return 42;\n}\n}")
735 (js2-deftest-parse parse-unterminated-class-is-not-okay
736 "class Foo {\n get bar() { return 42;\n}"
739 (js2-deftest-parse parse-super-keyword
740 "class Foo {\n constructor() { super(42);\n}\n foo() { super.foo();\n}\n}")
742 (js2-deftest-parse parse-class-keywordlike-method
743 "class C {\n delete() {}\n if() {}\n}")
747 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
749 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
750 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
751 (should (equal (js2-symbol-name entry) "foo"))
752 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
754 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
759 (let* ((scope (js2-node-at-point (point-min)))
760 (fn-entry (js2-scope-get-symbol scope 'bar))
761 (var-entry (js2-scope-get-symbol scope 'x)))
762 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
763 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
764 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
765 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
766 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
768 (defun js2-test-scope-of-nth-variable-satisifies-predicate (variable nth predicate)
769 (goto-char (point-min))
770 (dotimes (n (1+ nth)) (search-forward variable))
772 (let ((scope (js2-node-get-enclosing-scope (js2-node-at-point))))
773 (should (funcall predicate (js2-get-defining-scope scope variable)))))
775 (js2-deftest for-node-is-declaration-scope "for (let i = 0; i; ++i) {};"
777 (js2-test-scope-of-nth-variable-satisifies-predicate "i" 0 #'js2-for-node-p))
779 (js2-deftest const-scope-sloppy-script "{const a;} a;"
781 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-script-node-p)
782 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'js2-script-node-p))
784 (js2-deftest const-scope-strict-script "'use strict'; { const a; } a;"
786 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-block-node-p)
787 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'null))
789 (js2-deftest const-scope-sloppy-function "function f() { { const a; } a; }"
791 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-function-node-p)
792 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'js2-function-node-p))
794 (js2-deftest const-scope-strict-function "function f() { 'use strict'; { const a; } a; }"
796 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-block-node-p)
797 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'null))
799 (js2-deftest array-comp-is-result-scope "[x * 2 for (x in y)];"
801 (js2-test-scope-of-nth-variable-satisifies-predicate "x" 0 #'js2-comp-loop-node-p))
803 (js2-deftest array-comp-has-parent-scope
804 "var a,b=[for (i of [[1,2]]) for (j of i) j * a];"
806 (search-forward "for")
808 (let ((node (js2-node-at-point)))
809 (should (js2-scope-parent-scope node))
810 (should (js2-get-defining-scope node "j"))))
814 (js2-deftest get-token "(1+1)"
816 (should (eq js2-LP (js2-next-token)))
817 (should (eq js2-NUMBER (js2-next-token)))
818 (should (eq js2-ADD (js2-next-token)))
819 (should (eq js2-NUMBER (js2-next-token)))
820 (should (eq js2-RP (js2-next-token))))
822 (js2-deftest unget-token "()"
824 (should (eq js2-LP (js2-next-token)))
826 (should (eq js2-LP (js2-next-token)))
827 (should (eq js2-RP (js2-next-token))))
829 (js2-deftest get-token-or-eol "x\n++;"
831 (should (eq js2-NAME (js2-next-token)))
832 (should (eq js2-EOL (js2-peek-token-or-eol)))
833 (should (eq js2-INC (js2-next-token)))
834 (should (eq js2-SEMI (js2-peek-token-or-eol))))
836 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
838 (should (eq js2-NAME (js2-next-token)))
839 (should (eq js2-NAME (js2-next-token)))
840 (should (equal "y" (js2-current-token-string)))
842 (should (eq js2-NAME (js2-current-token-type)))
843 (should (equal "x" (js2-current-token-string))))
845 (js2-deftest ts-seek "(1+2)"
847 (should (eq js2-LP (js2-next-token)))
848 (should (eq js2-NUMBER (js2-next-token)))
850 (let ((state (make-js2-ts-state)))
851 (should (eq js2-NUMBER (js2-next-token)))
852 (should (eq js2-ADD (js2-next-token)))
854 (should (eq 1 js2-ti-lookahead))
855 (should (eq js2-NUMBER (js2-next-token)))
856 (should (eq 1 (js2-token-number
857 (js2-current-token))))))
859 (js2-deftest get-token-template-literal "`abc ${i} z ${j} def`"
861 (should (eq js2-TEMPLATE_HEAD (js2-next-token)))
862 (should (equal "abc " (js2-current-token-string)))
863 (should (eq js2-NAME (js2-next-token)))
864 (should (eq js2-RC (js2-next-token)))
865 (should (eq js2-TEMPLATE_HEAD (js2-next-token 'TEMPLATE_TAIL)))
866 (should (equal " z " (js2-current-token-string)))
867 (should (eq js2-NAME (js2-next-token)))
868 (should (eq js2-RC (js2-next-token)))
869 (should (eq js2-NO_SUBS_TEMPLATE (js2-next-token 'TEMPLATE_TAIL)))
870 (should (equal " def" (js2-current-token-string))))
874 (js2-deftest for-node-with-error-len "for "
876 (let ((node (js2-node-at-point (point-min))))
877 (should (= (js2-node-len (js2-node-parent node)) 4))))
879 (js2-deftest function-without-parens-error "function b {}"
880 ;; Should finish the parse.
885 (js2-deftest comment-node-length "//"
887 (let ((node (js2-node-at-point (point-min))))
888 (should (= (js2-node-len node) 2))))
890 (js2-deftest comment-node-length-newline "//\n"
892 (let ((node (js2-node-at-point (point-min))))
893 (should (= (js2-node-len node) 3))))
895 ;;; Variables classification
897 (defun js2--variables-summary (vars)
899 (setq vars (let (aslist)
900 (maphash (lambda (k v) (push (cons k v) aslist)) vars)
902 (dolist (v (sort vars (lambda (a b) (< (js2-node-abs-pos (js2-symbol-ast-node (car a)))
903 (js2-node-abs-pos (js2-symbol-ast-node (car b)))))))
904 (let* ((symbol (car v))
907 (symn (js2-symbol-ast-node symbol))
908 (namen (js2--get-name-node symn)))
909 (push (format "%s@%s:%s"
910 (js2-symbol-name symbol)
911 (js2-node-abs-pos namen)
917 (dolist (u (sort (cddr v) (lambda (a b) (< (js2-node-abs-pos a)
918 (js2-node-abs-pos b)))))
919 (push (js2-node-abs-pos u) r))))
922 (defmacro js2-deftest-classify-variables (name buffer-contents summary)
923 (declare (indent defun))
924 `(ert-deftest ,(intern (format "js2-classify-variables-%s" name)) ()
927 (insert ,buffer-contents))
931 (should (equal ,summary (js2--variables-summary
932 (js2--classify-variables)))))
933 (fundamental-mode)))))
935 (js2-deftest-classify-variables incomplete-var-statement
939 (js2-deftest-classify-variables unused-variable
940 "function foo () { var x; return 42; }"
941 '("foo@10:U" "x@23:U"))
943 (js2-deftest-classify-variables unused-variable-declared-twice
944 "function foo (a) { var x; function bar () { var x; x=42; }; return a;}"
945 '("foo@10:U" "a@15:P" 68 "x@24:U" "bar@36:U" "x@49:U"))
947 (js2-deftest-classify-variables assigned-variable
948 "function foo () { var x; x=42; return x; }"
949 '("foo@10:U" "x@23:I" 39))
951 (js2-deftest-classify-variables assignment-in-nested-function
952 "function foo () { var x; function bar () { x=42; }; }"
953 '("foo@10:U" "x@23:U" "bar@35:U"))
955 (js2-deftest-classify-variables unused-nested-function
956 "function foo() { var i, j=1; function bar() { var x, y=42, z=i; return y; } return i; }"
957 '("foo@10:U" "i@22:N" 62 84 "j@25:U" "bar@39:U" "x@51:U" "y@54:I" 72 "z@60:U"))
959 (js2-deftest-classify-variables prop-get-initialized
960 "function foo () { var x, y={}; y.a=x; }"
961 '("foo@10:U" "x@23:N" 36 "y@26:I" 32))
963 (js2-deftest-classify-variables prop-get-uninitialized
964 "function foo () { var x; if(x.foo) alert('boom'); }"
965 '("foo@10:U" "x@23:N" 29))
967 (js2-deftest-classify-variables prop-get-function-assignment
968 "(function(w) { w.f = function() { var a=42, m; return a; }; })(window);"
969 '("w@11:P" 11 16 "a@39:I" 55 "m@45:U"))
971 (js2-deftest-classify-variables let-declaration
972 "function foo () { let x,y=1; return x; }"
973 '("foo@10:U" "x@23:N" 37 "y@25:U"))
975 (js2-deftest-classify-variables external-function-call
976 "function foo (m) { console.log(m, arguments); }"
977 '("foo@10:U" "m@15:P" 32))
979 (js2-deftest-classify-variables global-function-call
980 "function bar () { return 42; } function foo (a) { return bar(); }"
981 '("bar@10:I" 58 "foo@41:U" "a@46:P"))
983 (js2-deftest-classify-variables let-declaration-for-scope
984 "function foo () { for(let x=1,y; x<y; y++) {} }"
985 '("foo@10:U" "x@27:I" 34 "y@31:N" 36 39))
987 (js2-deftest-classify-variables arguments-implicit-var
988 "function foo () { var p; for(p in arguments) { return p; } }"
989 '("foo@10:U" "p@23:I" 55))
991 (js2-deftest-classify-variables catch-error-variable
992 "function foo () { try { throw 'Foo'; } catch (e) { console.log(e); }"
993 '("foo@10:U" "e@47:I" 64))
995 (js2-deftest-classify-variables prop-get-assignment
996 "function foo () { var x={y:{z:{}}}; x.y.z=42; }"
997 '("foo@10:U" "x@23:I" 37))
999 (js2-deftest-classify-variables unused-function-argument
1000 "function foo (a) { return 42; }"
1001 '("foo@10:U" "a@15:P"))
1003 (js2-deftest-classify-variables used-function-argument
1004 "function foo (a) { a=42; return a; }"
1005 '("foo@10:U" "a@15:P" 33))
1007 (js2-deftest-classify-variables prop-get
1008 "function foo (a) { a=navigator.x||navigator.y; return a; }"
1009 '("foo@10:U" "a@15:P" 55))
1011 (js2-deftest-classify-variables for-in-loop
1012 "function foo () { var d={}; for(var k in d) {var v=d[k]; } }"
1013 '("foo@10:U" "d@23:I" 42 52 "k@37:I" 54 "v@50:U"))
1015 (js2-deftest-classify-variables array-comprehension-legacy
1016 "function foo() { var j,a=[for (i of [1,2,3]) i*j]; }"
1017 '("foo@10:U" "j@22:N" 48 "a@24:U" "i@32:I" 46))
1019 (js2-deftest-classify-variables array-comprehension
1020 "function foo() { var j,a=[[i,j] for (i of [1,2,3])]; }"
1021 '("foo@10:U" "j@22:N" 30 "a@24:U" "i@38:I" 28))
1023 (js2-deftest-classify-variables return-named-function
1024 "function foo() { var a=42; return function bar() { return a; } }"
1025 '("foo@10:U" "a@22:I" 59 "bar@44:I" 44))
1027 (js2-deftest-classify-variables named-wrapper-function
1028 "function foo() { var a; (function bar() { a=42; })(); return a; }"
1029 '("foo@10:U" "a@22:I" 62 "bar@35:I" 35))