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) (cl-first 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 ;;; Function definition
203 (js2-deftest function-redeclaring-var "var gen = 3; function gen() {};"
205 (should (= (length (js2-ast-root-warnings js2-mode-ast)) 1)))
207 (js2-deftest function-expression-var-same-name "var gen = function gen() {};"
209 (should (null (js2-ast-root-warnings js2-mode-ast))))
211 ;;; Function parameters
213 (js2-deftest-parse function-with-default-parameters
214 "function foo(a = 1, b = a + 1) {\n}")
216 (js2-deftest-parse function-with-no-default-after-default
217 "function foo(a = 1, b) {\n}"
220 (js2-deftest-parse function-with-destruct-after-default
221 "function foo(a = 1, {b, c}) {\n}"
224 (js2-deftest-parse function-with-rest-parameter
225 "function foo(a, b, ...rest) {\n}")
227 (js2-deftest-parse function-with-param-after-rest-parameter
228 "function foo(a, ...b, rest) {\n}"
229 :syntax-error "rest")
231 (js2-deftest-parse function-with-destruct-after-rest-parameter
232 "function foo(a, ...b, {}) {\n}"
235 (js2-deftest-parse function-with-rest-after-default-parameter
236 "function foo(a = 1, ...rest) {\n}")
240 (js2-deftest-parse spread-in-array-literal
241 "[1, ...[2, 3], 4, ...[5, 6]];")
243 (js2-deftest-parse spread-in-function-call
244 "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")
248 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
249 "() => false;" :reference "() => {false};")
251 (js2-deftest-parse arrow-function-with-args-and-curlies
252 "(a, b = 1, ...c) => { c;\n};")
254 (js2-deftest-parse arrow-function-with-destructuring
255 "([{a}, b]) => { a + b;\n};")
257 (js2-deftest-parse parenless-arrow-function-prohibits-rest
258 "...b => {b + 1;};" :syntax-error "=>" :errors-count 1)
260 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
261 "[a, b] => {a + b;};" :syntax-error "=>" :errors-count 4)
263 ;;; Automatic semicolon insertion
265 (js2-deftest-parse no-auto-semi-insertion-after-if
268 (js2-deftest-parse auto-semi-insertion-after-function
269 "a = function() {}" :reference "a = function() {};")
271 (js2-deftest-parse auto-semi-one-variable-per-line
272 "x\ny" :reference "x;\ny;")
276 (js2-deftest-parse labeled-stmt-node
277 "foo:\nbar:\nx = y + 1;")
279 (js2-deftest no-label-node-inside-expr "x = y:"
280 (let (js2-parse-interruptable-p)
282 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
283 (should (js2-name-node-p (js2-assign-node-right assignment)))))
285 (js2-deftest-parse label-and-loops "for (; ; ) {
294 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
296 (js2-deftest-parse legacy-generator-cannot-return
297 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
299 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
301 (js2-deftest-parse harmony-generator-yield-star "(function*(a) { yield* a;\n});")
305 (js2-deftest-parse parse-legacy-array-comp-loop-with-filter
306 "[a for (a in b) if (a == 2)];")
308 (js2-deftest-parse parse-array-comp-loop-with-filters
309 "[for (a in b) if (a == 2) if (b != 10) a];")
311 (js2-deftest-parse parse-generator-comp-loop-with-filters
312 "(for (x of y) if (x != 4) x);")
314 (js2-deftest-parse parse-array-comp-with-yield-is-ok
315 "(function() { return [for (x of []) yield x];\n});")
317 (js2-deftest-parse parse-generator-comp-with-yield-is-not-ok
318 "(function() { return (for (x of []) yield x);\n});"
319 :syntax-error "yield")
321 (js2-deftest-parse parse-generator-comp-with-yield-inside-function-is-ok
322 "(for (x of []) function*() { yield x;\n});")
326 (js2-deftest-parse decimal-starting-with-zero "081;" :reference "81;")
328 (js2-deftest-parse huge-hex "0x0123456789abcdefABCDEF;" :reference "-1;")
330 (js2-deftest-parse octal-without-o "071;" :reference "57;")
332 (js2-deftest-parse hex-number-okay "0x123;" :reference "291;")
334 (js2-deftest-parse hex-number-broken "0xz23;"
335 :syntax-error "0xz" :errors-count 2)
337 (js2-deftest-parse binary-number-okay "0b101;" :reference "5;")
339 (js2-deftest-parse binary-number-broken "0b210;"
340 :syntax-error "0b2" :errors-count 2)
342 (js2-deftest-parse octal-number-okay "0o765;" :reference "501;")
344 (js2-deftest-parse octal-number-broken "0o812;"
345 :syntax-error "0o8" :errors-count 2)
349 (js2-deftest parse-export-bindings "{one, two as dos}"
351 (should (js2-match-token js2-LC))
352 (let ((imports (js2-parse-export-bindings)))
353 (should (not (equal nil imports)))
354 (should (= 2 (length imports)))
355 (let ((first (nth 0 imports))
356 (second (nth 1 imports)))
357 (should (equal "one" (js2-name-node-name (js2-export-binding-node-extern-name first))))
358 (should (equal "two" (js2-name-node-name (js2-export-binding-node-extern-name second))))
359 (let ((first-name (js2-export-binding-node-local-name first))
360 (second-name (js2-export-binding-node-local-name second)))
361 (should (equal first (js2-node-parent first-name)))
362 (should (equal 3 (js2-node-len first-name)))
363 (should (equal "one" (js2-name-node-name first-name)))
364 (should (equal second (js2-node-parent second-name)))
365 (should (equal 3 (js2-node-len second-name)))
366 (should (equal "dos" (js2-name-node-name second-name)))))))
368 (js2-deftest parse-export-binding-as-default "one as default"
370 (let ((binding (js2-maybe-parse-export-binding)))
372 (should (js2-export-binding-node-p binding))
373 (let ((name (js2-export-binding-node-local-name binding)))
375 (should (equal "default" (js2-name-node-name name))))))
377 (js2-deftest parse-namepsace-import "* as lib;"
379 (should (js2-match-token js2-MUL))
380 (let ((namespace-import (js2-parse-namespace-import)))
381 (should (not (equal nil namespace-import)))
382 (should (js2-namespace-import-node-p namespace-import))
383 (should (= 1 (js2-node-pos namespace-import)))
384 (should (equal 8 (js2-node-len namespace-import)))
385 (let ((name-node (js2-namespace-import-node-name namespace-import)))
386 (should (equal "lib" (js2-name-node-name name-node)))
387 (should (= 5 (js2-node-pos name-node))))))
389 (js2-deftest parse-from-clause "from 'foo/bar';"
391 (let ((from (js2-parse-from-clause)))
392 (should (not (equal nil from)))
393 (should (= 1 (js2-node-pos from)))
394 (should (= 14 (js2-node-len from)))
395 (should (equal "foo/bar" (js2-from-clause-node-module-id from)))))
397 (js2-deftest parse-import-module-id-only "import 'src/lib'"
399 (should (js2-match-token js2-IMPORT))
400 (let ((import (js2-parse-import)))
401 (should (not (equal nil import)))
402 (should (= 1 (js2-node-pos import)))
403 (should (= 16 (js2-node-len import)))
404 (should (equal nil (js2-import-node-import import)))
405 (should (equal nil (js2-import-node-from import)))))
407 (js2-deftest parse-imported-default-binding "import theDefault from 'src/lib'"
408 (js2-push-scope (make-js2-scope :pos 0))
410 (should (js2-match-token js2-IMPORT))
411 (let ((import-node (js2-parse-import)))
412 (should (not (equal nil import-node)))
413 (should (equal "src/lib" (js2-import-node-module-id import-node)))
414 (let ((import (js2-import-node-import import-node)))
415 (should (not (equal nil import)))
416 (should (equal nil (js2-import-clause-node-namespace-import import)))
417 (should (equal nil (js2-import-clause-node-named-imports import)))
418 (let ((default (js2-import-clause-node-default-binding import)))
419 (should (not (equal nil default)))
420 (should (js2-export-binding-node-p default))
421 (should (equal "theDefault" (js2-name-node-name (js2-export-binding-node-extern-name default)))))))
422 (should (js2-scope-get-symbol js2-current-scope "theDefault")))
424 (js2-deftest parse-import-namespace-binding "import * as lib from 'src/lib'"
425 (js2-push-scope (make-js2-scope :pos 0))
427 (should (js2-match-token js2-IMPORT))
428 (let ((import-node (js2-parse-import)))
429 (should (not (equal nil import-node)))
430 (should (equal "src/lib" (js2-import-node-module-id import-node)))
431 (let ((import (js2-import-node-import import-node)))
432 (should (not (equal nil import)))
433 (should (equal nil (js2-import-clause-node-default-binding import)))
434 (should (equal nil (js2-import-clause-node-named-imports import)))
435 (let ((ns-import (js2-import-clause-node-namespace-import import)))
436 (should (not (equal nil ns-import)))
437 (should (js2-namespace-import-node-p ns-import))
438 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
439 (should (js2-scope-get-symbol js2-current-scope "lib")))
441 (js2-deftest parse-import-named-imports "import {foo as bar, baz} from 'src/lib'"
442 (js2-push-scope (make-js2-scope :pos 0))
444 (should (js2-match-token js2-IMPORT))
445 (let ((import-node (js2-parse-import)))
446 (should (not (equal nil import-node)))
447 (should (equal "src/lib" (js2-import-node-module-id import-node)))
448 (let ((import (js2-import-node-import import-node)))
449 (should (not (equal nil import)))
450 (should (equal nil (js2-import-clause-node-default-binding import)))
451 (should (equal nil (js2-import-clause-node-namespace-import import)))
452 (let ((named-imports (js2-import-clause-node-named-imports import)))
453 (should (not (equal nil named-imports)))
454 (should (listp named-imports))
455 (should (= 2 (length named-imports)))
456 (let ((first (nth 0 named-imports))
457 (second (nth 1 named-imports)))
458 (should (equal "bar" (js2-name-node-name (js2-export-binding-node-local-name first))))
459 (should (equal "baz" (js2-name-node-name (js2-export-binding-node-local-name second))))))))
460 (should (js2-scope-get-symbol js2-current-scope "bar"))
461 (should (js2-scope-get-symbol js2-current-scope "baz")))
463 (js2-deftest parse-import-default-and-namespace "import stuff, * as lib from 'src/lib'"
464 (js2-push-scope (make-js2-scope :pos 0))
466 (should (js2-match-token js2-IMPORT))
467 (let ((import-node (js2-parse-import)))
468 (should (not (equal nil import-node)))
469 (should (equal "src/lib" (js2-import-node-module-id import-node)))
470 (let ((import (js2-import-node-import import-node)))
471 (should (not (equal nil import)))
472 (should (equal nil (js2-import-clause-node-named-imports import)))
473 (let ((default (js2-import-clause-node-default-binding import))
474 (ns-import (js2-import-clause-node-namespace-import import)))
475 (should (not (equal nil default)))
476 (should (equal "stuff" (js2-name-node-name (js2-export-binding-node-local-name default))))
477 (should (not (equal nil ns-import)))
478 (should (js2-namespace-import-node-p ns-import))
479 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
480 (should (js2-scope-get-symbol js2-current-scope "stuff"))
481 (should (js2-scope-get-symbol js2-current-scope "lib")))
483 (js2-deftest parse-import-default-and-named-imports
484 "import robert as bob, {cookies, pi as PIE} from 'src/lib'"
485 (js2-push-scope (make-js2-scope :pos 0))
487 (should (js2-match-token js2-IMPORT))
488 (let ((import-node (js2-parse-import)))
489 (should (not (equal nil import-node)))
490 (should (equal "src/lib" (js2-import-node-module-id import-node)))
491 (let ((import (js2-import-node-import import-node)))
492 (should (not (equal nil import)))
493 (should (not (equal nil (js2-import-clause-node-named-imports import))))
494 (let ((default (js2-import-clause-node-default-binding import))
495 (named-imports (js2-import-clause-node-named-imports import)))
496 (should (not (equal nil default)))
497 (should (equal "bob" (js2-name-node-name (js2-export-binding-node-local-name default))))
498 (should (not (equal nil named-imports)))
499 (should (= 2 (length named-imports))))))
500 (should (js2-scope-get-symbol js2-current-scope "bob"))
501 (should (js2-scope-get-symbol js2-current-scope "cookies"))
502 (should (js2-scope-get-symbol js2-current-scope "PIE")))
504 (js2-deftest parse-this-module-in-from-clause "import {url} from this module;"
505 (js2-push-scope (make-js2-scope :pos 0))
507 (should (js2-match-token js2-IMPORT))
508 (let ((import-node (js2-parse-import)))
510 (let ((from-clause (js2-import-node-from import-node)))
512 (should (equal "this" (js2-from-clause-node-module-id from-clause)))
513 (should (js2-from-clause-node-metadata-p from-clause)))))
515 (js2-deftest-parse import-only-for-side-effects "import 'src/lib';")
516 (js2-deftest-parse import-default-only "import theDefault from 'src/lib';")
517 (js2-deftest-parse import-named-only "import {one, two} from 'src/lib';")
518 (js2-deftest-parse import-default-and-named "import theDefault, {one, two} from 'src/lib';")
519 (js2-deftest-parse import-renaming-default "import * as lib from 'src/mylib';")
520 (js2-deftest-parse import-renaming-named "import {one as uno, two as dos} from 'src/lib';")
521 (js2-deftest-parse import-default-and-namespace "import robert as bob, * as lib from 'src/lib';")
522 (js2-deftest-parse import-from-this-module "import {url} from this module;")
526 (js2-deftest export-rexport "export * from 'other/lib'"
528 (should (js2-match-token js2-EXPORT))
529 (let ((export-node (js2-parse-export)))
531 (should (js2-export-node-from-clause export-node))))
533 (js2-deftest export-export-named-list "export {foo, bar as bang};"
535 (should (js2-match-token js2-EXPORT))
536 (let ((export-node (js2-parse-export)))
538 (let ((exports (js2-export-node-exports-list export-node)))
540 (should (= 2 (length exports))))))
542 (js2-deftest re-export-named-list "export {foo, bar as bang} from 'other/lib'"
544 (should (js2-match-token js2-EXPORT))
545 (let ((export-node (js2-parse-export)))
547 (should (js2-export-node-from-clause export-node))
548 (let ((exports (js2-export-node-exports-list export-node)))
550 (should (= 2 (length exports))))))
552 (js2-deftest export-variable-statement "export var foo = 'bar', baz = 'bang';"
554 (js2-push-scope (make-js2-scope :pos 0))
555 (should (js2-match-token js2-EXPORT))
556 (let ((export-node (js2-parse-export)))
558 (should (js2-export-node-declaration export-node))))
560 (js2-deftest export-const-declaration "export const PI = Math.PI;"
562 (js2-push-scope (make-js2-scope :pos 0))
563 (should (js2-match-token js2-EXPORT))
564 (let ((export-node (js2-parse-export)))
566 (should (js2-export-node-declaration export-node))))
568 (js2-deftest export-let-declaration "export let foo = [1];"
570 (js2-push-scope (make-js2-scope :pos 0))
571 (should (js2-match-token js2-EXPORT))
572 (let ((export-node (js2-parse-export)))
574 (should (js2-var-decl-node-p (js2-export-node-declaration export-node)))))
576 (js2-deftest export-class-declaration "export class Foo {};"
578 (js2-push-scope (make-js2-scope :pos 0))
579 (should (js2-match-token js2-EXPORT))
580 (let ((export-node (js2-parse-export)))
582 (should (js2-class-node-p (js2-export-node-declaration export-node)))))
584 (js2-deftest export-function-declaration "export default function doStuff() {};"
586 (js2-push-scope (make-js2-scope :pos 0))
587 (should (js2-match-token js2-EXPORT))
588 (let ((export-node (js2-parse-export)))
590 (should (js2-export-node-default export-node))))
592 (js2-deftest export-generator-declaration "export default function* one() {};"
594 (js2-push-scope (make-js2-scope :pos 0))
595 (should (js2-match-token js2-EXPORT))
596 (let ((export-node (js2-parse-export)))
598 (should (js2-export-node-default export-node))))
600 (js2-deftest export-assignment-expression "export default a = b;"
602 (js2-push-scope (make-js2-scope :pos 0))
603 (should (js2-match-token js2-EXPORT))
604 (let ((export-node (js2-parse-export)))
606 (should (js2-export-node-default export-node))))
608 (js2-deftest-parse parse-export-rexport "export * from 'other/lib';")
609 (js2-deftest-parse parse-export-export-named-list "export {foo, bar as bang};")
610 (js2-deftest-parse parse-re-export-named-list "export {foo, bar as bang} from 'other/lib';")
611 (js2-deftest-parse parse-export-const-declaration "export const PI = Math.PI;")
612 (js2-deftest-parse parse-export-let-declaration "export let foo = [1];")
613 (js2-deftest-parse parse-export-function-declaration "export default function doStuff() {};")
614 (js2-deftest-parse parse-export-generator-declaration "export default function* one() {};")
615 (js2-deftest-parse parse-export-assignment-expression "export default a = b;")
619 (js2-deftest-parse string-literal
622 (js2-deftest-parse object-get-string-literal
623 "var x = {y: 5};\nvar z = x[\"y\"];")
625 (js2-deftest-parse template-no-substritutions
627 def`, y = `\\u0000`;")
629 (js2-deftest-parse template-with-substitutions
630 "var y = `${a + b} ${d + e + f}`;")
632 (js2-deftest-parse tagged-template
633 "foo.args`${++x, \"o\"}k`;")
637 (js2-deftest-parse parse-harmony-class-statement
638 "class Foo {\n get bar() { return 42;\n}\n set bar(x) { y = x;\n}\n}")
640 (js2-deftest-parse parse-harmony-class-statement-without-name-is-not-ok
641 "class {\n get bar() { return 42;\n}\n}"
644 (js2-deftest-parse parse-harmony-class-expression
645 "var Foo1 = class Foo {\n bar() { return 42;\n}\n};")
647 (js2-deftest-parse parse-harmony-anonymous-class-expression
648 "var Foo = class {\n set bar(x) { bar = x;\n}\n};")
650 (js2-deftest-parse parse-harmony-class-with-extends
651 "class Foo extends Bar {\n}")
653 (js2-deftest-parse parse-harmony-anonymous-class-with-extends
654 "foo.Foo = class extends Bar {\n set bar(x) { bar = x;\n}\n};")
656 (js2-deftest-parse parse-harmony-class-with-complex-extends
657 "class Foo extends foo[BAR][2].Baz {\n}")
659 (js2-deftest-parse parse-harmony-class-missing-extended-class-is-not-ok
660 "class Foo extends {\n}"
661 :syntax-error "extends")
663 (js2-deftest-parse parse-harmony-class-static-method
664 "class Foo extends Bar {\n static bar() { return 42;\n}\n}")
666 (js2-deftest-parse parse-unterminated-class-is-not-okay
667 "class Foo {\n get bar() { return 42;\n}"
670 (js2-deftest-parse parse-super-keyword
671 "class Foo {\n constructor() { super(42);\n}\n foo() { super.foo();\n}\n}")
675 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
677 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
678 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
679 (should (equal (js2-symbol-name entry) "foo"))
680 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
682 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
687 (let* ((scope (js2-node-at-point (point-min)))
688 (fn-entry (js2-scope-get-symbol scope 'bar))
689 (var-entry (js2-scope-get-symbol scope 'x)))
690 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
691 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
692 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
693 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
694 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
698 (js2-deftest get-token "(1+1)"
700 (should (eq js2-LP (js2-next-token)))
701 (should (eq js2-NUMBER (js2-next-token)))
702 (should (eq js2-ADD (js2-next-token)))
703 (should (eq js2-NUMBER (js2-next-token)))
704 (should (eq js2-RP (js2-next-token))))
706 (js2-deftest unget-token "()"
708 (should (eq js2-LP (js2-next-token)))
710 (should (eq js2-LP (js2-next-token)))
711 (should (eq js2-RP (js2-next-token))))
713 (js2-deftest get-token-or-eol "x\n++;"
715 (should (eq js2-NAME (js2-next-token)))
716 (should (eq js2-EOL (js2-peek-token-or-eol)))
717 (should (eq js2-INC (js2-next-token)))
718 (should (eq js2-SEMI (js2-peek-token-or-eol))))
720 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
722 (should (eq js2-NAME (js2-next-token)))
723 (should (eq js2-NAME (js2-next-token)))
724 (should (equal "y" (js2-current-token-string)))
726 (should (eq js2-NAME (js2-current-token-type)))
727 (should (equal "x" (js2-current-token-string))))
729 (js2-deftest ts-seek "(1+2)"
731 (should (eq js2-LP (js2-next-token)))
732 (should (eq js2-NUMBER (js2-next-token)))
734 (let ((state (make-js2-ts-state)))
735 (should (eq js2-NUMBER (js2-next-token)))
736 (should (eq js2-ADD (js2-next-token)))
738 (should (eq 1 js2-ti-lookahead))
739 (should (eq js2-NUMBER (js2-next-token)))
740 (should (eq 1 (js2-token-number
741 (js2-current-token))))))
743 (js2-deftest get-token-template-literal "`abc ${i} z ${j} def`"
745 (should (eq js2-TEMPLATE_HEAD (js2-next-token)))
746 (should (equal "abc " (js2-current-token-string)))
747 (should (eq js2-NAME (js2-next-token)))
748 (should (eq js2-RC (js2-next-token)))
749 (should (eq js2-TEMPLATE_HEAD (js2-next-token 'TEMPLATE_TAIL)))
750 (should (equal " z " (js2-current-token-string)))
751 (should (eq js2-NAME (js2-next-token)))
752 (should (eq js2-RC (js2-next-token)))
753 (should (eq js2-NO_SUBS_TEMPLATE (js2-next-token 'TEMPLATE_TAIL)))
754 (should (equal " def" (js2-current-token-string))))
758 (js2-deftest for-node-with-error-len "for "
760 (let ((node (js2-node-at-point (point-min))))
761 (should (= (js2-node-len (js2-node-parent node)) 4))))
763 (js2-deftest function-without-parens-error "function b {}"
764 ;; Should finish the parse.