]> code.delx.au - gnu-emacs-elpa/blob - tests/parser.el
Fix parsing of function statements in blocks
[gnu-emacs-elpa] / tests / parser.el
1 ;;; tests/parser.el --- Some tests for js2-mode.
2
3 ;; Copyright (C) 2009, 2011-2013 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
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.
11
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.
16
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/>.
19
20 ;;; Code:
21
22 (require 'ert)
23 (require 'ert-x)
24 (require 'js2-mode)
25
26 (defmacro js2-deftest (name buffer-contents &rest body)
27 `(ert-deftest ,(intern (format "js2-%s" name)) ()
28 (with-temp-buffer
29 (save-excursion
30 (insert ,buffer-contents))
31 (unwind-protect
32 (progn
33 ,@body)
34 (fundamental-mode)))))
35
36 (put 'js2-deftest 'lisp-indent-function 'defun)
37
38 (defun js2-test-string-to-ast (s)
39 (ert-with-test-buffer (:name 'origin)
40 (insert s)
41 (js2-mode)
42 (should (null js2-mode-buffer-dirty-p))
43 js2-mode-ast))
44
45 (defun* js2-test-parse-string (code-string &key syntax-error errors-count
46 reference)
47 (let ((ast (js2-test-string-to-ast code-string)))
48 (if syntax-error
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)
56 (js2-print-tree ast)
57 (skip-chars-backward " \t\n")
58 (should (string= (or reference code-string)
59 (buffer-substring-no-properties
60 (point-min) (point))))))))
61
62 (defmacro* js2-deftest-parse (name code-string &key bind syntax-error errors-count
63 reference)
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
69 the test."
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))))
76
77 (put 'js2-deftest-parse 'lisp-indent-function 'defun)
78
79 ;;; Basics
80
81 (js2-deftest-parse variable-assignment
82 "a = 1;")
83
84 (js2-deftest-parse empty-object-literal
85 "b = {};")
86
87 (js2-deftest-parse empty-array-literal
88 "c = [];")
89
90 (js2-deftest-parse comma-after-regexp
91 "d = /eee/, 42;")
92
93 (js2-deftest-parse return-statement
94 "function foo() {\n return 2;\n}")
95
96 (js2-deftest-parse function-statement
97 "function foo() {\n}")
98
99 (js2-deftest-parse function-statement-inside-block
100 "if (true) {\n function foo() {\n }\n}")
101
102 (js2-deftest-parse function-expression-statements-are-verboten
103 "function() {}" :syntax-error "(")
104
105 (js2-deftest-parse member-expr-as-function-name
106 "function a.b.c[2](x, y) {\n}"
107 :bind ((js2-allow-member-expr-as-function-name t)))
108
109 (js2-deftest-parse named-function-expression
110 "a = function b() {};")
111
112 ;;; Callers of `js2-valid-prop-name-token'
113
114 (js2-deftest-parse parse-property-access-when-not-keyword
115 "A.foo = 3;")
116
117 (js2-deftest-parse parse-property-access-when-keyword
118 "A.in = 3;"
119 :bind ((js2-allow-keywords-as-property-names t)))
120
121 (js2-deftest-parse parse-property-access-when-keyword-no-xml
122 "A.in = 3;"
123 :bind ((js2-allow-keywords-as-property-names t)
124 (js2-compiler-xml-available nil)))
125
126 (js2-deftest-parse parse-object-literal-when-not-keyword
127 "a = {b: 1};")
128
129 (js2-deftest-parse parse-object-literal-when-keyword
130 "a = {in: 1};"
131 :bind ((js2-allow-keywords-as-property-names t)))
132
133 ;;; 'of' contextual keyword
134
135 (js2-deftest-parse parse-array-comp-loop-with-of
136 "[a for (a of [])];")
137
138 (js2-deftest-parse parse-for-of
139 "for (var a of []) {\n}")
140
141 (js2-deftest-parse of-can-be-var-name
142 "var of = 3;")
143
144 (js2-deftest-parse of-can-be-function-name
145 "function of() {\n}")
146
147 ;;; Destructuring binding
148
149 (js2-deftest-parse destruct-in-declaration
150 "var {a, b} = {a: 1, b: 2};")
151
152 (js2-deftest-parse destruct-in-arguments
153 "function f({a: aa, b: bb}) {\n}")
154
155 (js2-deftest-parse destruct-in-array-comp-loop
156 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
157
158 (js2-deftest-parse destruct-in-catch-clause
159 "try {\n} catch ({a, b}) {\n a + b;\n}")
160
161 ;;; Function parameters
162
163 (js2-deftest-parse function-with-default-parameters
164 "function foo(a = 1, b = a + 1) {\n}")
165
166 (js2-deftest-parse function-with-no-default-after-default
167 "function foo(a = 1, b) {\n}"
168 :syntax-error "b")
169
170 (js2-deftest-parse function-with-destruct-after-default
171 "function foo(a = 1, {b, c}) {\n}"
172 :syntax-error "{")
173
174 (js2-deftest-parse function-with-rest-parameter
175 "function foo(a, b, ...rest) {\n}")
176
177 (js2-deftest-parse function-with-param-after-rest-parameter
178 "function foo(a, ...b, rest) {\n}"
179 :syntax-error "rest")
180
181 (js2-deftest-parse function-with-destruct-after-rest-parameter
182 "function foo(a, ...b, {}) {\n}"
183 :syntax-error "{}")
184
185 (js2-deftest-parse function-with-rest-after-default-parameter
186 "function foo(a = 1, ...rest) {\n}")
187
188 ;;; Spread operator
189
190 (js2-deftest-parse spread-in-array-literal
191 "[1, ...[2, 3], 4, ...[5, 6]];")
192
193 (js2-deftest-parse spread-in-function-call
194 "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")
195
196 ;;; Arrow functions
197
198 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
199 "() => false;" :reference "() => {false};")
200
201 (js2-deftest-parse arrow-function-with-args-and-curlies
202 "(a, b = 1, ...c) => { c;\n};")
203
204 (js2-deftest-parse parenless-arrow-function-prohibits-rest
205 "...b => {b + 1;};" :syntax-error "=>" :errors-count 2)
206
207 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
208 "[a, b] => {a + b;};" :syntax-error "=>" :errors-count 5)
209
210 ;;; Automatic semicolon insertion
211
212 (js2-deftest-parse no-auto-semi-insertion-after-if
213 "if (true) {\n}")
214
215 (js2-deftest-parse auto-semi-insertion-after-function
216 "a = function() {}" :reference "a = function() {};")
217
218 (js2-deftest-parse auto-semi-one-variable-per-line
219 "x\ny" :reference "x;\ny;")
220
221 ;;; Labels
222
223 (js2-deftest-parse labeled-stmt-node
224 "foo:\nbar:\nx = y + 1;")
225
226 (js2-deftest no-label-node-inside-expr "x = y:"
227 (let (js2-parse-interruptable-p)
228 (js2-mode))
229 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
230 (should (js2-name-node-p (js2-assign-node-right assignment)))))
231
232 (js2-deftest-parse label-and-loops "for (; ; ) {
233 loop:
234 for (; ; ) {
235 continue loop;
236 }
237 }")
238
239 ;;; Generators
240
241 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
242
243 (js2-deftest-parse legacy-generator-cannot-return
244 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
245
246 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
247
248 ;;; Scopes
249
250 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
251 (js2-mode)
252 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
253 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
254 (should (equal (js2-symbol-name entry) "foo"))
255 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
256
257 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
258 function bar() {}
259 var x;
260 }"
261 (js2-mode)
262 (let* ((scope (js2-node-at-point (point-min)))
263 (fn-entry (js2-scope-get-symbol scope 'bar))
264 (var-entry (js2-scope-get-symbol scope 'x)))
265 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
266 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
267 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
268 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
269 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
270
271 ;;; Tokenizer
272
273 (js2-deftest get-token "(1+1)"
274 (js2-init-scanner)
275 (should (eq js2-LP (js2-next-token)))
276 (should (eq js2-NUMBER (js2-next-token)))
277 (should (eq js2-ADD (js2-next-token)))
278 (should (eq js2-NUMBER (js2-next-token)))
279 (should (eq js2-RP (js2-next-token))))
280
281 (js2-deftest unget-token "()"
282 (js2-init-scanner)
283 (should (eq js2-LP (js2-next-token)))
284 (js2-unget-token)
285 (should (eq js2-LP (js2-next-token)))
286 (should (eq js2-RP (js2-next-token))))
287
288 (js2-deftest get-token-or-eol "x\n++;"
289 (js2-init-scanner)
290 (should (eq js2-NAME (js2-next-token)))
291 (should (eq js2-EOL (js2-peek-token-or-eol)))
292 (should (eq js2-INC (js2-next-token)))
293 (should (eq js2-SEMI (js2-peek-token-or-eol))))
294
295 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
296 (js2-init-scanner)
297 (should (eq js2-NAME (js2-next-token)))
298 (should (eq js2-NAME (js2-next-token)))
299 (should (equal "y" (js2-current-token-string)))
300 (js2-unget-token)
301 (should (eq js2-NAME (js2-current-token-type)))
302 (should (equal "x" (js2-current-token-string))))
303
304 (js2-deftest ts-seek "(1+2)"
305 (js2-init-scanner)
306 (should (eq js2-LP (js2-next-token)))
307 (should (eq js2-NUMBER (js2-next-token)))
308 (js2-unget-token)
309 (let ((state (make-js2-ts-state)))
310 (should (eq js2-NUMBER (js2-next-token)))
311 (should (eq js2-ADD (js2-next-token)))
312 (js2-ts-seek state)
313 (should (eq 1 js2-ti-lookahead))
314 (should (eq js2-NUMBER (js2-next-token)))
315 (should (eq 1 (js2-token-number
316 (js2-current-token))))))
317
318 ;;; Error handling
319
320 (js2-deftest for-node-with-error-len "for "
321 (js2-mode)
322 (let ((node (js2-node-at-point (point-min))))
323 (should (= (js2-node-len (js2-node-parent node)) 4))))
324
325 (js2-deftest function-without-parens-error "function b {}"
326 ;; Should finish the parse.
327 (js2-mode))