]> code.delx.au - gnu-emacs-elpa/blob - tests/parser.el
Support method definitions in object literals.
[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 array-with-missing-elements
91 "var a = [1, 2, ,];")
92
93 (js2-deftest-parse comma-after-regexp
94 "d = /eee/, 42;")
95
96 (js2-deftest-parse return-statement
97 "function foo() {\n return 2;\n}")
98
99 (js2-deftest-parse function-statement
100 "function foo() {\n}")
101
102 (js2-deftest-parse function-statement-inside-block
103 "if (true) {\n function foo() {\n }\n}")
104
105 (js2-deftest-parse function-expression-statements-are-verboten
106 "function() {}" :syntax-error "(")
107
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)))
111
112 (js2-deftest-parse named-function-expression
113 "a = function b() {};")
114
115 (js2-deftest-parse parenthesized-expression
116 "(1 + 2);")
117
118 (js2-deftest-parse for-with-in-operator-in-parens
119 "for (var y = (0 in []) in {}) {\n}")
120
121 (js2-deftest-parse for-with-in-operator-in-cond
122 "for (var y = 1 ? 0 in [] : false in {}) {\n}")
123
124 (js2-deftest-parse let-expression
125 "(let (x = 42) x);")
126
127 (js2-deftest-parse let-expression-statement
128 "let (x = 42) x;")
129
130 ;;; Callers of `js2-valid-prop-name-token'
131
132 (js2-deftest-parse parse-property-access-when-not-keyword
133 "A.foo = 3;")
134
135 (js2-deftest-parse parse-property-access-when-keyword
136 "A.in = 3;"
137 :bind ((js2-allow-keywords-as-property-names t)))
138
139 (js2-deftest-parse parse-property-access-when-keyword-no-xml
140 "A.in = 3;"
141 :bind ((js2-allow-keywords-as-property-names t)
142 (js2-compiler-xml-available nil)))
143
144 (js2-deftest-parse parse-object-literal-when-not-keyword
145 "a = {b: 1};")
146
147 (js2-deftest-parse parse-object-literal-when-keyword
148 "a = {in: 1};"
149 :bind ((js2-allow-keywords-as-property-names t)))
150
151 ;;; 'of' contextual keyword
152
153 (js2-deftest-parse parse-legacy-array-comp-loop-with-of
154 "[a for (a of [])];")
155
156 (js2-deftest-parse parse-array-comp-loop
157 "[for (a of []) a];")
158
159 (js2-deftest-parse parse-for-of
160 "for (var a of []) {\n}")
161
162 (js2-deftest-parse of-can-be-var-name
163 "var of = 3;")
164
165 (js2-deftest-parse of-can-be-function-name
166 "function of() {\n}")
167
168 ;;; Destructuring binding
169
170 (js2-deftest-parse destruct-in-declaration
171 "var {a, b} = {a: 1, b: 2};")
172
173 (js2-deftest-parse destruct-in-arguments
174 "function f({a: aa, b: bb}) {\n}")
175
176 (js2-deftest-parse destruct-in-array-comp-loop
177 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
178
179 (js2-deftest-parse destruct-in-catch-clause
180 "try {\n} catch ({a, b}) {\n a + b;\n}")
181
182 ;;; Object literals
183
184 (js2-deftest-parse abbreviated-object
185 "var x = {a: 1, b, c: 1, d};")
186
187 (js2-deftest-parse object-literal-method
188 "var x = {f(y) { return y;\n}};")
189
190 (js2-deftest-parse object-literal-getter-method
191 "var x = {get f() { return 42;\n}};")
192
193 (js2-deftest-parse object-literal-setter-method
194 "var x = {set f(y) { x = y;\n}};")
195
196 ;;; Function parameters
197
198 (js2-deftest-parse function-with-default-parameters
199 "function foo(a = 1, b = a + 1) {\n}")
200
201 (js2-deftest-parse function-with-no-default-after-default
202 "function foo(a = 1, b) {\n}"
203 :syntax-error "b")
204
205 (js2-deftest-parse function-with-destruct-after-default
206 "function foo(a = 1, {b, c}) {\n}"
207 :syntax-error "{")
208
209 (js2-deftest-parse function-with-rest-parameter
210 "function foo(a, b, ...rest) {\n}")
211
212 (js2-deftest-parse function-with-param-after-rest-parameter
213 "function foo(a, ...b, rest) {\n}"
214 :syntax-error "rest")
215
216 (js2-deftest-parse function-with-destruct-after-rest-parameter
217 "function foo(a, ...b, {}) {\n}"
218 :syntax-error "{}")
219
220 (js2-deftest-parse function-with-rest-after-default-parameter
221 "function foo(a = 1, ...rest) {\n}")
222
223 ;;; Spread operator
224
225 (js2-deftest-parse spread-in-array-literal
226 "[1, ...[2, 3], 4, ...[5, 6]];")
227
228 (js2-deftest-parse spread-in-function-call
229 "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")
230
231 ;;; Arrow functions
232
233 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
234 "() => false;" :reference "() => {false};")
235
236 (js2-deftest-parse arrow-function-with-args-and-curlies
237 "(a, b = 1, ...c) => { c;\n};")
238
239 (js2-deftest-parse arrow-function-with-destructuring
240 "([{a}, b]) => { a + b;\n};")
241
242 (js2-deftest-parse parenless-arrow-function-prohibits-rest
243 "...b => {b + 1;};" :syntax-error "=>" :errors-count 1)
244
245 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
246 "[a, b] => {a + b;};" :syntax-error "=>" :errors-count 4)
247
248 ;;; Automatic semicolon insertion
249
250 (js2-deftest-parse no-auto-semi-insertion-after-if
251 "if (true) {\n}")
252
253 (js2-deftest-parse auto-semi-insertion-after-function
254 "a = function() {}" :reference "a = function() {};")
255
256 (js2-deftest-parse auto-semi-one-variable-per-line
257 "x\ny" :reference "x;\ny;")
258
259 ;;; Labels
260
261 (js2-deftest-parse labeled-stmt-node
262 "foo:\nbar:\nx = y + 1;")
263
264 (js2-deftest no-label-node-inside-expr "x = y:"
265 (let (js2-parse-interruptable-p)
266 (js2-mode))
267 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
268 (should (js2-name-node-p (js2-assign-node-right assignment)))))
269
270 (js2-deftest-parse label-and-loops "for (; ; ) {
271 loop:
272 for (; ; ) {
273 continue loop;
274 }
275 }")
276
277 ;;; Generators
278
279 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
280
281 (js2-deftest-parse legacy-generator-cannot-return
282 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
283
284 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
285
286 (js2-deftest-parse harmony-generator-yield-star "(function*(a) { yield* a;\n});")
287
288 ;;; Comprehensions
289
290 (js2-deftest-parse parse-legacy-array-comp-loop-with-filter
291 "[a for (a in b) if (a == 2)];")
292
293 (js2-deftest-parse parse-array-comp-loop-with-filters
294 "[for (a in b) if (a == 2) if (b != 10) a];")
295
296 (js2-deftest-parse parse-generator-comp-loop-with-filters
297 "(for (x of y) if (x != 4) x);")
298
299 (js2-deftest-parse parse-array-comp-with-yield-is-ok
300 "(function() { return [for (x of []) yield x];\n});")
301
302 (js2-deftest-parse parse-generator-comp-with-yield-is-not-ok
303 "(function() { return (for (x of []) yield x);\n});"
304 :syntax-error "yield")
305
306 (js2-deftest-parse parse-generator-comp-with-yield-inside-function-is-ok
307 "(for (x of []) function*() { yield x;\n});")
308
309 ;;; Numbers
310
311 (js2-deftest-parse decimal-starting-with-zero "081;" :reference "81;")
312
313 (js2-deftest-parse huge-hex "0x0123456789abcdefABCDEF;" :reference "-1;")
314
315 (js2-deftest-parse octal-without-o "071;" :reference "57;")
316
317 (js2-deftest-parse hex-number-okay "0x123;" :reference "291;")
318
319 (js2-deftest-parse hex-number-broken "0xz23;"
320 :syntax-error "0xz" :errors-count 2)
321
322 (js2-deftest-parse binary-number-okay "0b101;" :reference "5;")
323
324 (js2-deftest-parse binary-number-broken "0b210;"
325 :syntax-error "0b2" :errors-count 2)
326
327 (js2-deftest-parse octal-number-okay "0o765;" :reference "501;")
328
329 (js2-deftest-parse octal-number-broken "0o812;"
330 :syntax-error "0o8" :errors-count 2)
331
332 ;;; Scopes
333
334 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
335 (js2-mode)
336 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
337 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
338 (should (equal (js2-symbol-name entry) "foo"))
339 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
340
341 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
342 function bar() {}
343 var x;
344 }"
345 (js2-mode)
346 (let* ((scope (js2-node-at-point (point-min)))
347 (fn-entry (js2-scope-get-symbol scope 'bar))
348 (var-entry (js2-scope-get-symbol scope 'x)))
349 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
350 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
351 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
352 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
353 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
354
355 ;;; Tokenizer
356
357 (js2-deftest get-token "(1+1)"
358 (js2-init-scanner)
359 (should (eq js2-LP (js2-next-token)))
360 (should (eq js2-NUMBER (js2-next-token)))
361 (should (eq js2-ADD (js2-next-token)))
362 (should (eq js2-NUMBER (js2-next-token)))
363 (should (eq js2-RP (js2-next-token))))
364
365 (js2-deftest unget-token "()"
366 (js2-init-scanner)
367 (should (eq js2-LP (js2-next-token)))
368 (js2-unget-token)
369 (should (eq js2-LP (js2-next-token)))
370 (should (eq js2-RP (js2-next-token))))
371
372 (js2-deftest get-token-or-eol "x\n++;"
373 (js2-init-scanner)
374 (should (eq js2-NAME (js2-next-token)))
375 (should (eq js2-EOL (js2-peek-token-or-eol)))
376 (should (eq js2-INC (js2-next-token)))
377 (should (eq js2-SEMI (js2-peek-token-or-eol))))
378
379 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
380 (js2-init-scanner)
381 (should (eq js2-NAME (js2-next-token)))
382 (should (eq js2-NAME (js2-next-token)))
383 (should (equal "y" (js2-current-token-string)))
384 (js2-unget-token)
385 (should (eq js2-NAME (js2-current-token-type)))
386 (should (equal "x" (js2-current-token-string))))
387
388 (js2-deftest ts-seek "(1+2)"
389 (js2-init-scanner)
390 (should (eq js2-LP (js2-next-token)))
391 (should (eq js2-NUMBER (js2-next-token)))
392 (js2-unget-token)
393 (let ((state (make-js2-ts-state)))
394 (should (eq js2-NUMBER (js2-next-token)))
395 (should (eq js2-ADD (js2-next-token)))
396 (js2-ts-seek state)
397 (should (eq 1 js2-ti-lookahead))
398 (should (eq js2-NUMBER (js2-next-token)))
399 (should (eq 1 (js2-token-number
400 (js2-current-token))))))
401
402 ;;; Error handling
403
404 (js2-deftest for-node-with-error-len "for "
405 (js2-mode)
406 (let ((node (js2-node-at-point (point-min))))
407 (should (= (js2-node-len (js2-node-parent node)) 4))))
408
409 (js2-deftest function-without-parens-error "function b {}"
410 ;; Should finish the parse.
411 (js2-mode))