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)
39 (ert-with-test-buffer (:name 'origin)
42 (should (null js2-mode-buffer-dirty-p))
45 (defun* js2-test-parse-string (code-string &key syntax-error errors-count
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 comma-after-regexp
93 (js2-deftest-parse return-statement
94 "function foo() {\n return 2;\n}")
96 (js2-deftest-parse function-statement
97 "function foo() {\n}")
99 (js2-deftest-parse function-expression-statements-are-verboten
100 "function() {}" :syntax-error "(")
102 (js2-deftest-parse member-expr-as-function-name
103 "function a.b.c[2](x, y) {\n}"
104 :bind ((js2-allow-member-expr-as-function-name t)))
106 (js2-deftest-parse named-function-expression
107 "a = function b() {};")
109 ;;; Callers of `js2-valid-prop-name-token'
111 (js2-deftest-parse parse-property-access-when-not-keyword
114 (js2-deftest-parse parse-property-access-when-keyword
116 :bind ((js2-allow-keywords-as-property-names t)))
118 (js2-deftest-parse parse-property-access-when-keyword-no-xml
120 :bind ((js2-allow-keywords-as-property-names t)
121 (js2-compiler-xml-available nil)))
123 (js2-deftest-parse parse-object-literal-when-not-keyword
126 (js2-deftest-parse parse-object-literal-when-keyword
128 :bind ((js2-allow-keywords-as-property-names t)))
130 ;;; 'of' contextual keyword
132 (js2-deftest-parse parse-array-comp-loop-with-of
133 "[a for (a of [])];")
135 (js2-deftest-parse parse-for-of
136 "for (var a of []) {\n}")
138 (js2-deftest-parse of-can-be-var-name
141 (js2-deftest-parse of-can-be-function-name
142 "function of() {\n}")
144 ;;; Destructuring binding
146 (js2-deftest-parse destruct-in-declaration
147 "var {a, b} = {a: 1, b: 2};")
149 (js2-deftest-parse destruct-in-arguments
150 "function f({a: aa, b: bb}) {\n}")
152 (js2-deftest-parse destruct-in-array-comp-loop
153 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
155 (js2-deftest-parse destruct-in-catch-clause
156 "try {\n} catch ({a, b}) {\n a + b;\n}")
158 ;;; Function parameters
160 (js2-deftest-parse function-with-default-parameters
161 "function foo(a = 1, b = a + 1) {\n}")
163 (js2-deftest-parse function-with-no-default-after-default
164 "function foo(a = 1, b) {\n}"
167 (js2-deftest-parse function-with-destruct-after-default
168 "function foo(a = 1, {b, c}) {\n}"
171 (js2-deftest-parse function-with-rest-parameter
172 "function foo(a, b, ...rest) {\n}")
174 (js2-deftest-parse function-with-param-after-rest-parameter
175 "function foo(a, ...b, rest) {\n}"
176 :syntax-error "rest")
178 (js2-deftest-parse function-with-destruct-after-rest-parameter
179 "function foo(a, ...b, {}) {\n}"
182 (js2-deftest-parse function-with-rest-after-default-parameter
183 "function foo(a = 1, ...rest) {\n}")
187 (js2-deftest-parse spread-in-array-literal
188 "[1, ...[2, 3], 4, ...[5, 6]];")
190 (js2-deftest-parse spread-in-function-call
191 "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")
195 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
196 "() => false;" :reference "() => {false};")
198 (js2-deftest-parse arrow-function-with-args-and-curlies
199 "(a, b = 1, ...c) => { c;\n};")
201 (js2-deftest-parse parenless-arrow-function-prohibits-rest
202 "...b => {b + 1;};" :syntax-error "=>" :errors-count 2)
204 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
205 "[a, b] => {a + b;};" :syntax-error "=>" :errors-count 5)
207 ;;; Automatic semicolon insertion
209 (js2-deftest-parse no-auto-semi-insertion-after-if
212 (js2-deftest-parse auto-semi-insertion-after-function
213 "a = function() {}" :reference "a = function() {};")
215 (js2-deftest-parse auto-semi-one-variable-per-line
216 "x\ny" :reference "x;\ny;")
220 (js2-deftest-parse labeled-stmt-node
221 "foo:\nbar:\nx = y + 1;")
223 (js2-deftest no-label-node-inside-expr "x = y:"
224 (let (js2-parse-interruptable-p)
226 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
227 (should (js2-name-node-p (js2-assign-node-right assignment)))))
229 (js2-deftest-parse label-and-loops "for (; ; ) {
238 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
240 (js2-deftest-parse legacy-generator-cannot-return
241 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
243 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
247 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
249 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
250 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
251 (should (equal (js2-symbol-name entry) "foo"))
252 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
254 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
259 (let* ((scope (js2-node-at-point (point-min)))
260 (fn-entry (js2-scope-get-symbol scope 'bar))
261 (var-entry (js2-scope-get-symbol scope 'x)))
262 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
263 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
264 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
265 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
266 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
270 (js2-deftest get-token "(1+1)"
272 (should (eq js2-LP (js2-next-token)))
273 (should (eq js2-NUMBER (js2-next-token)))
274 (should (eq js2-ADD (js2-next-token)))
275 (should (eq js2-NUMBER (js2-next-token)))
276 (should (eq js2-RP (js2-next-token))))
278 (js2-deftest unget-token "()"
280 (should (eq js2-LP (js2-next-token)))
282 (should (eq js2-LP (js2-next-token)))
283 (should (eq js2-RP (js2-next-token))))
285 (js2-deftest get-token-or-eol "x\n++;"
287 (should (eq js2-NAME (js2-next-token)))
288 (should (eq js2-EOL (js2-peek-token-or-eol)))
289 (should (eq js2-INC (js2-next-token)))
290 (should (eq js2-SEMI (js2-peek-token-or-eol))))
292 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
294 (should (eq js2-NAME (js2-next-token)))
295 (should (eq js2-NAME (js2-next-token)))
296 (should (equal "y" (js2-current-token-string)))
298 (should (eq js2-NAME (js2-current-token-type)))
299 (should (equal "x" (js2-current-token-string))))
301 (js2-deftest ts-seek "(1+2)"
303 (should (eq js2-LP (js2-next-token)))
304 (should (eq js2-NUMBER (js2-next-token)))
306 (let ((state (make-js2-ts-state)))
307 (should (eq js2-NUMBER (js2-next-token)))
308 (should (eq js2-ADD (js2-next-token)))
310 (should (eq 1 js2-ti-lookahead))
311 (should (eq js2-NUMBER (js2-next-token)))
312 (should (eq 1 (js2-token-number
313 (js2-current-token))))))
317 (js2-deftest for-node-with-error-len "for "
319 (let ((node (js2-node-at-point (point-min))))
320 (should (= (js2-node-len (js2-node-parent node)) 4))))
322 (js2-deftest function-without-parens-error "function b {}"
323 ;; Should finish the parse.