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/>.
26 (defmacro js2-deftest (name buffer-contents &rest body)
27 `(ert-deftest ,(intern (format "js2-%s" name)) ()
30 (insert ,buffer-contents))
34 (fundamental-mode)))))
36 (put 'js2-deftest 'lisp-indent-function 'defun)
38 (defun js2-test-string-to-ast (s)
41 (should (null js2-mode-buffer-dirty-p))
44 (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 (destructuring-bind (_ pos len) (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 (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 `(ert-deftest ,(intern (format "js2-%s" name)) ()
71 (let ,(append bind '((js2-basic-offset 2)))
72 (js2-test-parse-string ,code-string
73 :syntax-error ,syntax-error
74 :errors-count ,errors-count
75 :reference ,reference))))
77 (put 'js2-deftest-parse 'lisp-indent-function 'defun)
81 (js2-deftest-parse variable-assignment
84 (js2-deftest-parse empty-object-literal
87 (js2-deftest-parse empty-array-literal
90 (js2-deftest-parse array-with-missing-elements
93 (js2-deftest-parse comma-after-regexp
96 (js2-deftest-parse return-statement
97 "function foo() {\n return 2;\n}")
99 (js2-deftest-parse function-statement
100 "function foo() {\n}")
102 (js2-deftest-parse function-statement-inside-block
103 "if (true) {\n function foo() {\n }\n}")
105 (js2-deftest-parse function-expression-statements-are-verboten
106 "function() {}" :syntax-error "(")
108 (js2-deftest-parse member-expr-as-function-name
109 "function a.b.c[2](x, y) {\n}"
110 :bind ((js2-allow-member-expr-as-function-name t)))
112 (js2-deftest-parse named-function-expression
113 "a = function b() {};")
115 (js2-deftest-parse parenthesized-expression
118 (js2-deftest-parse for-with-in-operator-in-parens
119 "for (var y = (0 in []) in {}) {\n}")
121 (js2-deftest-parse for-with-in-operator-in-cond
122 "for (var y = 1 ? 0 in [] : false in {}) {\n}")
124 (js2-deftest-parse let-expression
127 (js2-deftest-parse let-expression-statement
130 ;;; Callers of `js2-valid-prop-name-token'
132 (js2-deftest-parse parse-property-access-when-not-keyword
135 (js2-deftest-parse parse-property-access-when-keyword
137 :bind ((js2-allow-keywords-as-property-names t)))
139 (js2-deftest-parse parse-property-access-when-keyword-no-xml
141 :bind ((js2-allow-keywords-as-property-names t)
142 (js2-compiler-xml-available nil)))
144 (js2-deftest-parse parse-object-literal-when-not-keyword
147 (js2-deftest-parse parse-object-literal-when-keyword
149 :bind ((js2-allow-keywords-as-property-names t)))
151 ;;; 'of' contextual keyword
153 (js2-deftest-parse parse-legacy-array-comp-loop-with-of
154 "[a for (a of [])];")
156 (js2-deftest-parse parse-array-comp-loop
157 "[for (a of []) a];")
159 (js2-deftest-parse parse-for-of
160 "for (var a of []) {\n}")
162 (js2-deftest-parse of-can-be-var-name
165 (js2-deftest-parse of-can-be-function-name
166 "function of() {\n}")
168 ;;; Destructuring binding
170 (js2-deftest-parse destruct-in-declaration
171 "var {a, b} = {a: 1, b: 2};")
173 (js2-deftest-parse destruct-in-arguments
174 "function f({a: aa, b: bb}) {\n}")
176 (js2-deftest-parse destruct-in-array-comp-loop
177 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
179 (js2-deftest-parse destruct-in-catch-clause
180 "try {\n} catch ({a, b}) {\n a + b;\n}")
184 (js2-deftest-parse object-literal-shorthand
185 "var x = {a: 1, b, c: 1, d};")
187 (js2-deftest-parse object-literal-shorthard-with-number
188 "var a = {1};" :syntax-error ";" :errors-count 2)
190 (js2-deftest-parse object-literal-method
191 "var x = {f(y) { return y;\n}};")
193 (js2-deftest-parse object-literal-getter-method
194 "var x = {get f() { return 42;\n}};")
196 (js2-deftest-parse object-literal-setter-method
197 "var x = {set f(y) { x = y;\n}};")
199 (js2-deftest-parse object-literal-computed-keys
200 "var x = {[Symbol.iterator]: function() {}};")
202 ;;; Function definition
204 (js2-deftest function-redeclaring-var "var gen = 3; function gen() {};"
206 (should (= (length (js2-ast-root-warnings js2-mode-ast)) 1)))
208 (js2-deftest function-expression-var-same-name "var gen = function gen() {};"
210 (should (null (js2-ast-root-warnings js2-mode-ast))))
212 ;;; Function parameters
214 (js2-deftest-parse function-with-default-parameters
215 "function foo(a = 1, b = a + 1) {\n}")
217 (js2-deftest-parse function-with-no-default-after-default
218 "function foo(a = 1, b) {\n}"
221 (js2-deftest-parse function-with-destruct-after-default
222 "function foo(a = 1, {b, c}) {\n}"
225 (js2-deftest-parse function-with-rest-parameter
226 "function foo(a, b, ...rest) {\n}")
228 (js2-deftest-parse function-with-param-after-rest-parameter
229 "function foo(a, ...b, rest) {\n}"
230 :syntax-error "rest")
232 (js2-deftest-parse function-with-destruct-after-rest-parameter
233 "function foo(a, ...b, {}) {\n}"
236 (js2-deftest-parse function-with-rest-after-default-parameter
237 "function foo(a = 1, ...rest) {\n}")
241 (js2-deftest-parse spread-in-array-literal
242 "[1, ...[2, 3], 4, ...[5, 6]];")
244 (js2-deftest-parse spread-in-function-call
245 "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")
249 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
250 "() => false;" :reference "() => {false};")
252 (js2-deftest-parse arrow-function-with-args-and-curlies
253 "(a, b = 1, ...c) => { c;\n};")
255 (js2-deftest-parse arrow-function-with-destructuring
256 "([{a}, b]) => { a + b;\n};")
258 (js2-deftest-parse parenless-arrow-function-prohibits-rest
259 "...b => {b + 1;};" :syntax-error "=>" :errors-count 1)
261 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
262 "[a, b] => {a + b;};" :syntax-error "=>" :errors-count 4)
264 ;;; Automatic semicolon insertion
266 (js2-deftest-parse no-auto-semi-insertion-after-if
269 (js2-deftest-parse auto-semi-insertion-after-function
270 "a = function() {}" :reference "a = function() {};")
272 (js2-deftest-parse auto-semi-one-variable-per-line
273 "x\ny" :reference "x;\ny;")
277 (js2-deftest-parse labeled-stmt-node
278 "foo:\nbar:\nx = y + 1;")
280 (js2-deftest no-label-node-inside-expr "x = y:"
281 (let (js2-parse-interruptable-p)
283 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
284 (should (js2-name-node-p (js2-assign-node-right assignment)))))
286 (js2-deftest-parse label-and-loops "for (; ; ) {
295 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
297 (js2-deftest-parse legacy-generator-cannot-return
298 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
300 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
302 (js2-deftest-parse harmony-generator-yield-star "(function*(a) { yield* a;\n});")
306 (js2-deftest-parse parse-legacy-array-comp-loop-with-filter
307 "[a for (a in b) if (a == 2)];")
309 (js2-deftest-parse parse-array-comp-loop-with-filters
310 "[for (a in b) if (a == 2) if (b != 10) a];")
312 (js2-deftest-parse parse-generator-comp-loop-with-filters
313 "(for (x of y) if (x != 4) x);")
315 (js2-deftest-parse parse-array-comp-with-yield-is-ok
316 "(function() { return [for (x of []) yield x];\n});")
318 (js2-deftest-parse parse-generator-comp-with-yield-is-not-ok
319 "(function() { return (for (x of []) yield x);\n});"
320 :syntax-error "yield")
322 (js2-deftest-parse parse-generator-comp-with-yield-inside-function-is-ok
323 "(for (x of []) function*() { yield x;\n});")
327 (js2-deftest-parse decimal-starting-with-zero "081;" :reference "81;")
329 (js2-deftest-parse huge-hex "0x0123456789abcdefABCDEF;" :reference "-1;")
331 (js2-deftest-parse octal-without-o "071;" :reference "57;")
333 (js2-deftest-parse hex-number-okay "0x123;" :reference "291;")
335 (js2-deftest-parse hex-number-broken "0xz23;"
336 :syntax-error "0xz" :errors-count 2)
338 (js2-deftest-parse binary-number-okay "0b101;" :reference "5;")
340 (js2-deftest-parse binary-number-broken "0b210;"
341 :syntax-error "0b2" :errors-count 2)
343 (js2-deftest-parse octal-number-okay "0o765;" :reference "501;")
345 (js2-deftest-parse octal-number-broken "0o812;"
346 :syntax-error "0o8" :errors-count 2)
350 (js2-deftest-parse string-literal
353 (js2-deftest-parse object-get-string-literal
354 "var x = {y: 5};\nvar z = x[\"y\"];")
356 (js2-deftest-parse template-no-substritutions
358 def`, y = `\\u0000`;")
360 (js2-deftest-parse template-with-substitutions
361 "var y = `${a + b} ${d + e + f}`;")
363 (js2-deftest-parse tagged-template
364 "foo.args`${++x, \"o\"}k`;")
368 (js2-deftest-parse parse-harmony-class-statement
369 "class Foo {\n get bar() { return 42;\n}\n set bar(x) { y = x;\n}\n}")
371 (js2-deftest-parse parse-harmony-class-statement-without-name-is-not-ok
372 "class {\n get bar() { return 42;\n}\n}"
375 (js2-deftest-parse parse-harmony-class-expression
376 "var Foo1 = class Foo {\n bar() { return 42;\n}\n};")
378 (js2-deftest-parse parse-harmony-anonymous-class-expression
379 "var Foo = class {\n set bar(x) { bar = x;\n}\n};")
381 (js2-deftest-parse parse-harmony-class-with-extends
382 "class Foo extends Bar {\n}")
384 (js2-deftest-parse parse-harmony-anonymous-class-with-extends
385 "foo.Foo = class extends Bar {\n set bar(x) { bar = x;\n}\n};")
387 (js2-deftest-parse parse-harmony-class-with-complex-extends
388 "class Foo extends foo[BAR][2].Baz {\n}")
390 (js2-deftest-parse parse-harmony-class-missing-extended-class-is-not-ok
391 "class Foo extends {\n}"
392 :syntax-error "extends")
394 (js2-deftest-parse parse-harmony-class-static-method
395 "class Foo extends Bar {\n static bar() { return 42;\n}\n}")
397 (js2-deftest-parse parse-unterminated-class-is-not-okay
398 "class Foo {\n get bar() { return 42;\n}"
401 (js2-deftest-parse parse-super-keyword
402 "class Foo {\n constructor() { super(42);\n}\n foo() { super.foo();\n}\n}")
406 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
408 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
409 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
410 (should (equal (js2-symbol-name entry) "foo"))
411 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
413 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
418 (let* ((scope (js2-node-at-point (point-min)))
419 (fn-entry (js2-scope-get-symbol scope 'bar))
420 (var-entry (js2-scope-get-symbol scope 'x)))
421 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
422 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
423 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
424 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
425 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
429 (js2-deftest get-token "(1+1)"
431 (should (eq js2-LP (js2-next-token)))
432 (should (eq js2-NUMBER (js2-next-token)))
433 (should (eq js2-ADD (js2-next-token)))
434 (should (eq js2-NUMBER (js2-next-token)))
435 (should (eq js2-RP (js2-next-token))))
437 (js2-deftest unget-token "()"
439 (should (eq js2-LP (js2-next-token)))
441 (should (eq js2-LP (js2-next-token)))
442 (should (eq js2-RP (js2-next-token))))
444 (js2-deftest get-token-or-eol "x\n++;"
446 (should (eq js2-NAME (js2-next-token)))
447 (should (eq js2-EOL (js2-peek-token-or-eol)))
448 (should (eq js2-INC (js2-next-token)))
449 (should (eq js2-SEMI (js2-peek-token-or-eol))))
451 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
453 (should (eq js2-NAME (js2-next-token)))
454 (should (eq js2-NAME (js2-next-token)))
455 (should (equal "y" (js2-current-token-string)))
457 (should (eq js2-NAME (js2-current-token-type)))
458 (should (equal "x" (js2-current-token-string))))
460 (js2-deftest ts-seek "(1+2)"
462 (should (eq js2-LP (js2-next-token)))
463 (should (eq js2-NUMBER (js2-next-token)))
465 (let ((state (make-js2-ts-state)))
466 (should (eq js2-NUMBER (js2-next-token)))
467 (should (eq js2-ADD (js2-next-token)))
469 (should (eq 1 js2-ti-lookahead))
470 (should (eq js2-NUMBER (js2-next-token)))
471 (should (eq 1 (js2-token-number
472 (js2-current-token))))))
474 (js2-deftest get-token-template-literal "`abc ${i} z ${j} def`"
476 (should (eq js2-TEMPLATE_HEAD (js2-next-token)))
477 (should (equal "abc " (js2-current-token-string)))
478 (should (eq js2-NAME (js2-next-token)))
479 (should (eq js2-RC (js2-next-token)))
480 (should (eq js2-TEMPLATE_HEAD (js2-next-token 'TEMPLATE_TAIL)))
481 (should (equal " z " (js2-current-token-string)))
482 (should (eq js2-NAME (js2-next-token)))
483 (should (eq js2-RC (js2-next-token)))
484 (should (eq js2-NO_SUBS_TEMPLATE (js2-next-token 'TEMPLATE_TAIL)))
485 (should (equal " def" (js2-current-token-string))))
489 (js2-deftest for-node-with-error-len "for "
491 (let ((node (js2-node-at-point (point-min))))
492 (should (= (js2-node-len (js2-node-parent node)) 4))))
494 (js2-deftest function-without-parens-error "function b {}"
495 ;; Should finish the parse.