]> code.delx.au - gnu-emacs-elpa/blob - tests/parser.el
Highlight unused and/or uninitialized variables
[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 (require 'cl-lib)
26
27 (defmacro js2-deftest (name buffer-contents &rest body)
28 (declare (indent defun))
29 `(ert-deftest ,(intern (format "js2-%s" name)) ()
30 (with-temp-buffer
31 (save-excursion
32 (insert ,buffer-contents))
33 (unwind-protect
34 (progn
35 ,@body)
36 (fundamental-mode)))))
37
38 (defun js2-test-string-to-ast (s)
39 (insert s)
40 (js2-mode)
41 (should (null js2-mode-buffer-dirty-p))
42 js2-mode-ast)
43
44 (cl-defun js2-test-parse-string (code-string &key syntax-error errors-count
45 reference)
46 (ert-with-test-buffer (:name 'origin)
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 (cl-destructuring-bind (_ pos len) (car (last 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 (cl-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 (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))))
77
78 ;;; Basics
79
80 (js2-deftest-parse variable-assignment
81 "a = 1;")
82
83 (js2-deftest-parse empty-object-literal
84 "b = {};")
85
86 (js2-deftest-parse empty-array-literal
87 "c = [];")
88
89 (js2-deftest-parse array-with-missing-elements
90 "var a = [1, 2, ,];")
91
92 (js2-deftest-parse comma-after-regexp
93 "d = /eee/, 42;")
94
95 (js2-deftest-parse return-statement
96 "function foo() {\n return 2;\n}")
97
98 (js2-deftest-parse function-statement
99 "function foo() {\n}")
100
101 (js2-deftest-parse function-statement-inside-block
102 "if (true) {\n function foo() {\n }\n}")
103
104 (js2-deftest-parse function-expression-statements-are-verboten
105 "function() {}" :syntax-error "(")
106
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)))
110
111 (js2-deftest-parse named-function-expression
112 "a = function b() {};")
113
114 (js2-deftest-parse parenthesized-expression
115 "(1 + 2);")
116
117 (js2-deftest-parse for-with-in-operator-in-parens
118 "for (var y = (0 in []) in {}) {\n}")
119
120 (js2-deftest-parse for-with-in-operator-in-cond
121 "for (var y = 1 ? 0 in [] : false in {}) {\n}")
122
123 (js2-deftest-parse let-expression
124 "(let (x = 42) x);")
125
126 (js2-deftest-parse let-expression-statement
127 "let (x = 42) x;")
128
129 ;;; Callers of `js2-valid-prop-name-token'
130
131 (js2-deftest-parse parse-property-access-when-not-keyword
132 "A.foo = 3;")
133
134 (js2-deftest-parse parse-property-access-when-keyword
135 "A.in = 3;"
136 :bind ((js2-allow-keywords-as-property-names t)))
137
138 (js2-deftest-parse parse-property-access-when-keyword-no-xml
139 "A.in = 3;"
140 :bind ((js2-allow-keywords-as-property-names t)
141 (js2-compiler-xml-available nil)))
142
143 (js2-deftest-parse parse-object-literal-when-not-keyword
144 "a = {b: 1};")
145
146 (js2-deftest-parse parse-object-literal-when-keyword
147 "a = {in: 1};"
148 :bind ((js2-allow-keywords-as-property-names t)))
149
150 ;;; 'of' contextual keyword
151
152 (js2-deftest-parse parse-legacy-array-comp-loop-with-of
153 "[a for (a of [])];")
154
155 (js2-deftest-parse parse-array-comp-loop
156 "[for (a of []) a];")
157
158 (js2-deftest-parse parse-for-of
159 "for (var a of []) {\n}")
160
161 (js2-deftest-parse of-can-be-var-name
162 "var of = 3;")
163
164 (js2-deftest-parse of-can-be-function-name
165 "function of() {\n}")
166
167 ;;; Destructuring binding
168
169 (js2-deftest-parse destruct-in-declaration
170 "var {a, b} = {a: 1, b: 2};")
171
172 (js2-deftest-parse destruct-in-arguments
173 "function f({a: aa, b: bb}) {\n}")
174
175 (js2-deftest-parse destruct-in-array-comp-loop
176 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
177
178 (js2-deftest-parse destruct-in-catch-clause
179 "try {\n} catch ({a, b}) {\n a + b;\n}")
180
181 ;;; Object literals
182
183 (js2-deftest-parse object-literal-shorthand
184 "var x = {a: 1, b, c: 1, d};")
185
186 (js2-deftest-parse object-literal-shorthard-with-number
187 "var a = {1};" :syntax-error "}" :errors-count 2)
188
189 (js2-deftest-parse object-literal-method
190 "var x = {f(y) { return y;\n}};")
191
192 (js2-deftest-parse object-literal-getter-method
193 "var x = {get f() { return 42;\n}};")
194
195 (js2-deftest-parse object-literal-setter-method
196 "var x = {set f(y) { x = y;\n}};")
197
198 (js2-deftest-parse object-literal-computed-keys
199 "var x = {[Symbol.iterator]: function() {}};")
200
201 (js2-deftest-parse object-literal-computed-function-keys
202 "var x = {[foo + bar](y) { return y;\n}};")
203
204 (js2-deftest-parse object-literal-computed-getter-key
205 "var x = {get [foo + bar]() { return 42;\n}};")
206
207 (js2-deftest-parse object-literal-generator
208 "var x = {*foo() { yield 42;\n}};")
209
210 (js2-deftest-parse object-literal-computed-generator-key
211 "var x = {*[foo + bar]() { yield 42;\n}};")
212
213 ;;; Function definition
214
215 (js2-deftest function-redeclaring-var "var gen = 3; function gen() {};"
216 (js2-mode)
217 (should (= (length (js2-ast-root-warnings js2-mode-ast)) 1)))
218
219 (js2-deftest function-expression-var-same-name "var gen = function gen() {};"
220 (js2-mode)
221 (should (null (js2-ast-root-warnings js2-mode-ast))))
222
223 ;;; Function parameters
224
225 (js2-deftest-parse function-with-default-parameters
226 "function foo(a = 1, b = a + 1) {\n}")
227
228 (js2-deftest-parse function-with-no-default-after-default
229 "function foo(a = 1, b) {\n}"
230 :syntax-error "b")
231
232 (js2-deftest-parse function-with-destruct-after-default
233 "function foo(a = 1, {b, c}) {\n}"
234 :syntax-error "{")
235
236 (js2-deftest-parse function-with-rest-parameter
237 "function foo(a, b, ...rest) {\n}")
238
239 (js2-deftest-parse function-with-param-after-rest-parameter
240 "function foo(a, ...b, rest) {\n}"
241 :syntax-error "rest")
242
243 (js2-deftest-parse function-with-destruct-after-rest-parameter
244 "function foo(a, ...b, {}) {\n}"
245 :syntax-error "{}")
246
247 (js2-deftest-parse function-with-rest-after-default-parameter
248 "function foo(a = 1, ...rest) {\n}")
249
250 ;;; Spread operator
251
252 (js2-deftest-parse spread-in-array-literal
253 "[1, ...[2, 3], 4, ...[5, 6]];")
254
255 (js2-deftest-parse spread-in-function-call
256 "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")
257
258 ;;; Arrow functions
259
260 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
261 "() => false;" :reference "() => {false};")
262
263 (js2-deftest-parse arrow-function-with-args-and-curlies
264 "(a, b = 1, ...c) => { c;\n};")
265
266 (js2-deftest-parse arrow-function-with-destructuring
267 "([{a}, b]) => { a + b;\n};")
268
269 (js2-deftest-parse parenless-arrow-function-prohibits-rest
270 "...b => {b + 1;};" :syntax-error "=>")
271
272 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
273 "[a, b] => {a + b;};" :syntax-error "]" :errors-count 4)
274
275 (js2-deftest-parse arrow-function-recovers-from-error
276 "[(,foo) => 1];" :syntax-error "," :errors-count 6)
277
278 ;;; Automatic semicolon insertion
279
280 (js2-deftest-parse no-auto-semi-insertion-after-if
281 "if (true) {\n}")
282
283 (js2-deftest-parse auto-semi-insertion-after-function
284 "a = function() {}" :reference "a = function() {};")
285
286 (js2-deftest-parse auto-semi-one-variable-per-line
287 "x\ny" :reference "x;\ny;")
288
289 ;;; Labels
290
291 (js2-deftest-parse labeled-stmt-node
292 "foo:\nbar:\nx = y + 1;")
293
294 (js2-deftest no-label-node-inside-expr "x = y:"
295 (let (js2-parse-interruptable-p)
296 (js2-mode))
297 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
298 (should (js2-name-node-p (js2-assign-node-right assignment)))))
299
300 (js2-deftest-parse label-and-loops "for (; ; ) {
301 loop:
302 for (; ; ) {
303 continue loop;
304 }
305 }")
306
307 ;;; Generators
308
309 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
310
311 (js2-deftest-parse legacy-generator-cannot-return
312 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
313
314 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
315
316 (js2-deftest-parse harmony-generator-yield-star "(function*(a) { yield* a;\n});")
317
318 ;;; Comprehensions
319
320 (js2-deftest-parse parse-legacy-array-comp-loop-with-filter
321 "[a for (a in b) if (a == 2)];")
322
323 (js2-deftest-parse parse-array-comp-loop-with-filters
324 "[for (a in b) if (a == 2) if (b != 10) a];")
325
326 (js2-deftest-parse parse-generator-comp-loop-with-filters
327 "(for (x of y) if (x != 4) x);")
328
329 (js2-deftest-parse parse-array-comp-with-yield-is-ok
330 "(function() { return [for (x of []) yield x];\n});")
331
332 (js2-deftest-parse parse-generator-comp-with-yield-is-not-ok
333 "(function() { return (for (x of []) yield x);\n});"
334 :syntax-error "yield")
335
336 (js2-deftest-parse parse-generator-comp-with-yield-inside-function-is-ok
337 "(for (x of []) function*() { yield x;\n});")
338
339 ;;; Numbers
340
341 (js2-deftest-parse decimal-starting-with-zero "081;" :reference "81;")
342
343 (js2-deftest-parse huge-hex "0x0123456789abcdefABCDEF;" :reference "-1;")
344
345 (js2-deftest-parse octal-without-o "071;" :reference "57;")
346
347 (js2-deftest-parse hex-number-okay "0x123;" :reference "291;")
348
349 (js2-deftest-parse hex-number-broken "0xz23;"
350 :syntax-error "0xz" :errors-count 2)
351
352 (js2-deftest-parse binary-number-okay "0b101;" :reference "5;")
353
354 (js2-deftest-parse binary-number-broken "0b210;"
355 :syntax-error "0b2" :errors-count 2)
356
357 (js2-deftest-parse octal-number-okay "0o765;" :reference "501;")
358
359 (js2-deftest-parse octal-number-broken "0o812;"
360 :syntax-error "0o8" :errors-count 2)
361
362 ;;; Modules
363
364 (js2-deftest parse-export-bindings "{one, two as dos}"
365 (js2-init-scanner)
366 (should (js2-match-token js2-LC))
367 (let ((imports (js2-parse-export-bindings)))
368 (should (not (equal nil imports)))
369 (should (= 2 (length imports)))
370 (let ((first (nth 0 imports))
371 (second (nth 1 imports)))
372 (should (equal "one" (js2-name-node-name (js2-export-binding-node-extern-name first))))
373 (should (equal "two" (js2-name-node-name (js2-export-binding-node-extern-name second))))
374 (let ((first-name (js2-export-binding-node-local-name first))
375 (second-name (js2-export-binding-node-local-name second)))
376 (should (equal first (js2-node-parent first-name)))
377 (should (equal 3 (js2-node-len first-name)))
378 (should (equal "one" (js2-name-node-name first-name)))
379 (should (equal second (js2-node-parent second-name)))
380 (should (equal 3 (js2-node-len second-name)))
381 (should (equal "dos" (js2-name-node-name second-name)))))))
382
383 (js2-deftest parse-export-binding-as-default "one as default"
384 (js2-init-scanner)
385 (let ((binding (js2-maybe-parse-export-binding)))
386 (should binding)
387 (should (js2-export-binding-node-p binding))
388 (let ((name (js2-export-binding-node-local-name binding)))
389 (should name)
390 (should (equal "default" (js2-name-node-name name))))))
391
392 (js2-deftest parse-namepsace-import "* as lib;"
393 (js2-init-scanner)
394 (should (js2-match-token js2-MUL))
395 (let ((namespace-import (js2-parse-namespace-import)))
396 (should (not (equal nil namespace-import)))
397 (should (js2-namespace-import-node-p namespace-import))
398 (should (= 1 (js2-node-pos namespace-import)))
399 (should (equal 8 (js2-node-len namespace-import)))
400 (let ((name-node (js2-namespace-import-node-name namespace-import)))
401 (should (equal "lib" (js2-name-node-name name-node)))
402 (should (= 5 (js2-node-pos name-node))))))
403
404 (js2-deftest parse-from-clause "from 'foo/bar';"
405 (js2-init-scanner)
406 (let ((from (js2-parse-from-clause)))
407 (should (not (equal nil from)))
408 (should (= 1 (js2-node-pos from)))
409 (should (= 14 (js2-node-len from)))
410 (should (equal "foo/bar" (js2-from-clause-node-module-id from)))))
411
412 (js2-deftest parse-import-module-id-only "import 'src/lib'"
413 (js2-init-scanner)
414 (should (js2-match-token js2-IMPORT))
415 (let ((import (js2-parse-import)))
416 (should (not (equal nil import)))
417 (should (= 1 (js2-node-pos import)))
418 (should (= 16 (js2-node-len import)))
419 (should (equal nil (js2-import-node-import import)))
420 (should (equal nil (js2-import-node-from import)))))
421
422 (js2-deftest parse-imported-default-binding "import theDefault from 'src/lib'"
423 (js2-push-scope (make-js2-scope :pos 0))
424 (js2-init-scanner)
425 (should (js2-match-token js2-IMPORT))
426 (let ((import-node (js2-parse-import)))
427 (should (not (equal nil import-node)))
428 (should (equal "src/lib" (js2-import-node-module-id import-node)))
429 (let ((import (js2-import-node-import import-node)))
430 (should (not (equal nil import)))
431 (should (equal nil (js2-import-clause-node-namespace-import import)))
432 (should (equal nil (js2-import-clause-node-named-imports import)))
433 (let ((default (js2-import-clause-node-default-binding import)))
434 (should (not (equal nil default)))
435 (should (js2-export-binding-node-p default))
436 (should (equal "theDefault" (js2-name-node-name (js2-export-binding-node-extern-name default)))))))
437 (should (js2-scope-get-symbol js2-current-scope "theDefault")))
438
439 (js2-deftest parse-import-namespace-binding "import * as lib from 'src/lib'"
440 (js2-push-scope (make-js2-scope :pos 0))
441 (js2-init-scanner)
442 (should (js2-match-token js2-IMPORT))
443 (let ((import-node (js2-parse-import)))
444 (should (not (equal nil import-node)))
445 (should (equal "src/lib" (js2-import-node-module-id import-node)))
446 (let ((import (js2-import-node-import import-node)))
447 (should (not (equal nil import)))
448 (should (equal nil (js2-import-clause-node-default-binding import)))
449 (should (equal nil (js2-import-clause-node-named-imports import)))
450 (let ((ns-import (js2-import-clause-node-namespace-import import)))
451 (should (not (equal nil ns-import)))
452 (should (js2-namespace-import-node-p ns-import))
453 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
454 (should (js2-scope-get-symbol js2-current-scope "lib")))
455
456 (js2-deftest parse-import-named-imports "import {foo as bar, baz} from 'src/lib'"
457 (js2-push-scope (make-js2-scope :pos 0))
458 (js2-init-scanner)
459 (should (js2-match-token js2-IMPORT))
460 (let ((import-node (js2-parse-import)))
461 (should (not (equal nil import-node)))
462 (should (equal "src/lib" (js2-import-node-module-id import-node)))
463 (let ((import (js2-import-node-import import-node)))
464 (should (not (equal nil import)))
465 (should (equal nil (js2-import-clause-node-default-binding import)))
466 (should (equal nil (js2-import-clause-node-namespace-import import)))
467 (let ((named-imports (js2-import-clause-node-named-imports import)))
468 (should (not (equal nil named-imports)))
469 (should (listp named-imports))
470 (should (= 2 (length named-imports)))
471 (let ((first (nth 0 named-imports))
472 (second (nth 1 named-imports)))
473 (should (equal "bar" (js2-name-node-name (js2-export-binding-node-local-name first))))
474 (should (equal "baz" (js2-name-node-name (js2-export-binding-node-local-name second))))))))
475 (should (js2-scope-get-symbol js2-current-scope "bar"))
476 (should (js2-scope-get-symbol js2-current-scope "baz")))
477
478 (js2-deftest parse-import-default-and-namespace "import stuff, * as lib from 'src/lib'"
479 (js2-push-scope (make-js2-scope :pos 0))
480 (js2-init-scanner)
481 (should (js2-match-token js2-IMPORT))
482 (let ((import-node (js2-parse-import)))
483 (should (not (equal nil import-node)))
484 (should (equal "src/lib" (js2-import-node-module-id import-node)))
485 (let ((import (js2-import-node-import import-node)))
486 (should (not (equal nil import)))
487 (should (equal nil (js2-import-clause-node-named-imports import)))
488 (let ((default (js2-import-clause-node-default-binding import))
489 (ns-import (js2-import-clause-node-namespace-import import)))
490 (should (not (equal nil default)))
491 (should (equal "stuff" (js2-name-node-name (js2-export-binding-node-local-name default))))
492 (should (not (equal nil ns-import)))
493 (should (js2-namespace-import-node-p ns-import))
494 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
495 (should (js2-scope-get-symbol js2-current-scope "stuff"))
496 (should (js2-scope-get-symbol js2-current-scope "lib")))
497
498 (js2-deftest parse-import-default-and-named-imports
499 "import robert as bob, {cookies, pi as PIE} from 'src/lib'"
500 (js2-push-scope (make-js2-scope :pos 0))
501 (js2-init-scanner)
502 (should (js2-match-token js2-IMPORT))
503 (let ((import-node (js2-parse-import)))
504 (should (not (equal nil import-node)))
505 (should (equal "src/lib" (js2-import-node-module-id import-node)))
506 (let ((import (js2-import-node-import import-node)))
507 (should (not (equal nil import)))
508 (should (not (equal nil (js2-import-clause-node-named-imports import))))
509 (let ((default (js2-import-clause-node-default-binding import))
510 (named-imports (js2-import-clause-node-named-imports import)))
511 (should (not (equal nil default)))
512 (should (equal "bob" (js2-name-node-name (js2-export-binding-node-local-name default))))
513 (should (not (equal nil named-imports)))
514 (should (= 2 (length named-imports))))))
515 (should (js2-scope-get-symbol js2-current-scope "bob"))
516 (should (js2-scope-get-symbol js2-current-scope "cookies"))
517 (should (js2-scope-get-symbol js2-current-scope "PIE")))
518
519 (js2-deftest parse-this-module-in-from-clause "import {url} from this module;"
520 (js2-push-scope (make-js2-scope :pos 0))
521 (js2-init-scanner)
522 (should (js2-match-token js2-IMPORT))
523 (let ((import-node (js2-parse-import)))
524 (should import-node)
525 (let ((from-clause (js2-import-node-from import-node)))
526 (should from-clause)
527 (should (equal "this" (js2-from-clause-node-module-id from-clause)))
528 (should (js2-from-clause-node-metadata-p from-clause)))))
529
530 (js2-deftest-parse import-only-for-side-effects "import 'src/lib';")
531 (js2-deftest-parse import-default-only "import theDefault from 'src/lib';")
532 (js2-deftest-parse import-named-only "import {one, two} from 'src/lib';")
533 (js2-deftest-parse import-default-and-named "import theDefault, {one, two} from 'src/lib';")
534 (js2-deftest-parse import-renaming-default "import * as lib from 'src/mylib';")
535 (js2-deftest-parse import-renaming-named "import {one as uno, two as dos} from 'src/lib';")
536 (js2-deftest-parse import-default-and-namespace "import robert as bob, * as lib from 'src/lib';")
537 (js2-deftest-parse import-from-this-module "import {url} from this module;")
538
539 ;; Module Exports
540
541 (js2-deftest export-rexport "export * from 'other/lib'"
542 (js2-init-scanner)
543 (should (js2-match-token js2-EXPORT))
544 (let ((export-node (js2-parse-export)))
545 (should export-node)
546 (should (js2-export-node-from-clause export-node))))
547
548 (js2-deftest export-export-named-list "export {foo, bar as bang};"
549 (js2-init-scanner)
550 (should (js2-match-token js2-EXPORT))
551 (let ((export-node (js2-parse-export)))
552 (should export-node)
553 (let ((exports (js2-export-node-exports-list export-node)))
554 (should exports)
555 (should (= 2 (length exports))))))
556
557 (js2-deftest re-export-named-list "export {foo, bar as bang} from 'other/lib'"
558 (js2-init-scanner)
559 (should (js2-match-token js2-EXPORT))
560 (let ((export-node (js2-parse-export)))
561 (should export-node)
562 (should (js2-export-node-from-clause export-node))
563 (let ((exports (js2-export-node-exports-list export-node)))
564 (should exports)
565 (should (= 2 (length exports))))))
566
567 (js2-deftest export-variable-statement "export var foo = 'bar', baz = 'bang';"
568 (js2-init-scanner)
569 (js2-push-scope (make-js2-scope :pos 0))
570 (should (js2-match-token js2-EXPORT))
571 (let ((export-node (js2-parse-export)))
572 (should export-node)
573 (should (js2-export-node-declaration export-node))))
574
575 (js2-deftest export-const-declaration "export const PI = Math.PI;"
576 (js2-init-scanner)
577 (js2-push-scope (make-js2-scope :pos 0))
578 (should (js2-match-token js2-EXPORT))
579 (let ((export-node (js2-parse-export)))
580 (should export-node)
581 (should (js2-export-node-declaration export-node))))
582
583 (js2-deftest export-let-declaration "export let foo = [1];"
584 (js2-init-scanner)
585 (js2-push-scope (make-js2-scope :pos 0))
586 (should (js2-match-token js2-EXPORT))
587 (let ((export-node (js2-parse-export)))
588 (should export-node)
589 (should (js2-var-decl-node-p (js2-export-node-declaration export-node)))))
590
591 (js2-deftest export-class-declaration "export class Foo {};"
592 (js2-init-scanner)
593 (js2-push-scope (make-js2-scope :pos 0))
594 (should (js2-match-token js2-EXPORT))
595 (let ((export-node (js2-parse-export)))
596 (should export-node)
597 (should (js2-class-node-p (js2-export-node-declaration export-node)))))
598
599 (js2-deftest export-function-declaration "export default function doStuff() {};"
600 (js2-init-scanner)
601 (js2-push-scope (make-js2-scope :pos 0))
602 (should (js2-match-token js2-EXPORT))
603 (let ((export-node (js2-parse-export)))
604 (should export-node)
605 (should (js2-export-node-default export-node))))
606
607 (js2-deftest export-generator-declaration "export default function* one() {};"
608 (js2-init-scanner)
609 (js2-push-scope (make-js2-scope :pos 0))
610 (should (js2-match-token js2-EXPORT))
611 (let ((export-node (js2-parse-export)))
612 (should export-node)
613 (should (js2-export-node-default export-node))))
614
615 (js2-deftest export-assignment-expression "export default a = b;"
616 (js2-init-scanner)
617 (js2-push-scope (make-js2-scope :pos 0))
618 (should (js2-match-token js2-EXPORT))
619 (let ((export-node (js2-parse-export)))
620 (should export-node)
621 (should (js2-export-node-default export-node))))
622
623 (js2-deftest export-function-no-semicolon "export default function foo() {}"
624 (js2-mode)
625 (should (equal nil js2-parsed-warnings)))
626 (js2-deftest export-default-function-no-semicolon "export function foo() {}"
627 (js2-mode)
628 (should (equal nil js2-parsed-warnings)))
629 (js2-deftest export-anything-else-does-require-a-semicolon "export var obj = {}"
630 (js2-mode)
631 (should (not (equal nil js2-parsed-warnings))))
632
633 (js2-deftest-parse parse-export-rexport "export * from 'other/lib';")
634 (js2-deftest-parse parse-export-export-named-list "export {foo, bar as bang};")
635 (js2-deftest-parse parse-re-export-named-list "export {foo, bar as bang} from 'other/lib';")
636 (js2-deftest-parse parse-export-const-declaration "export const PI = Math.PI;")
637 (js2-deftest-parse parse-export-let-declaration "export let foo = [1];")
638 (js2-deftest-parse parse-export-function-declaration "export default function doStuff() {};")
639 (js2-deftest-parse parse-export-generator-declaration "export default function* one() {};")
640 (js2-deftest-parse parse-export-assignment-expression "export default a = b;")
641
642 ;;; Strings
643
644 (js2-deftest-parse string-literal
645 "var x = 'y';")
646
647 (js2-deftest-parse object-get-string-literal
648 "var x = {y: 5};\nvar z = x[\"y\"];")
649
650 (js2-deftest-parse template-no-substritutions
651 "var x = `abc
652 def`, y = `\\u0000`;")
653
654 (js2-deftest-parse template-with-substitutions
655 "var y = `${a + b} ${d + e + f}`;")
656
657 (js2-deftest-parse tagged-template
658 "foo.args`${++x, \"o\"}k`;")
659
660 ;;; Classes
661
662 (js2-deftest-parse parse-harmony-class-statement
663 "class Foo {\n get bar() { return 42;\n}\n set bar(x) { y = x;\n}\n}")
664
665 (js2-deftest-parse parse-harmony-class-statement-without-name-is-not-ok
666 "class {\n get bar() { return 42;\n}\n}"
667 :syntax-error "{")
668
669 (js2-deftest-parse parse-harmony-class-expression
670 "var Foo1 = class Foo {\n bar() { return 42;\n}\n};")
671
672 (js2-deftest-parse parse-harmony-anonymous-class-expression
673 "var Foo = class {\n set bar(x) { bar = x;\n}\n};")
674
675 (js2-deftest-parse parse-harmony-class-with-extends
676 "class Foo extends Bar {\n}")
677
678 (js2-deftest-parse parse-harmony-anonymous-class-with-extends
679 "foo.Foo = class extends Bar {\n set bar(x) { bar = x;\n}\n};")
680
681 (js2-deftest-parse parse-harmony-class-with-complex-extends
682 "class Foo extends foo[BAR][2].Baz {\n}")
683
684 (js2-deftest-parse parse-harmony-class-missing-extended-class-is-not-ok
685 "class Foo extends {\n}"
686 :syntax-error "extends")
687
688 (js2-deftest-parse parse-harmony-class-static-method
689 "class Foo extends Bar {\n static bar() { return 42;\n}\n}")
690
691 (js2-deftest-parse parse-unterminated-class-is-not-okay
692 "class Foo {\n get bar() { return 42;\n}"
693 :syntax-error "}")
694
695 (js2-deftest-parse parse-super-keyword
696 "class Foo {\n constructor() { super(42);\n}\n foo() { super.foo();\n}\n}")
697
698 (js2-deftest-parse parse-class-keywordlike-method
699 "class C {\n delete() {}\n if() {}\n}")
700
701 ;;; Scopes
702
703 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
704 (js2-mode)
705 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
706 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
707 (should (equal (js2-symbol-name entry) "foo"))
708 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
709
710 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
711 function bar() {}
712 var x;
713 }"
714 (js2-mode)
715 (let* ((scope (js2-node-at-point (point-min)))
716 (fn-entry (js2-scope-get-symbol scope 'bar))
717 (var-entry (js2-scope-get-symbol scope 'x)))
718 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
719 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
720 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
721 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
722 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
723
724 (js2-deftest for-node-is-declaration-scope "for (let i = 0; i; ++i) {};"
725 (js2-mode)
726 (search-forward "i")
727 (forward-char -1)
728 (let ((scope (js2-node-get-enclosing-scope (js2-node-at-point))))
729 (should (js2-for-node-p (js2-get-defining-scope scope "i")))))
730
731 (js2-deftest array-comp-is-result-scope "[x * 2 for (x in y)];"
732 (js2-mode)
733 (search-forward "x")
734 (forward-char -1)
735 (let ((scope (js2-node-get-enclosing-scope (js2-node-at-point))))
736 (should (js2-comp-loop-node-p (js2-get-defining-scope scope "x")))))
737
738 (js2-deftest array-comp-has-parent-scope
739 "var a,b=[for (i of [[1,2]]) for (j of i) j * a];"
740 (js2-mode)
741 (search-forward "for")
742 (forward-char -3)
743 (let ((node (js2-node-at-point)))
744 (should (js2-scope-parent-scope node))
745 (should (js2-get-defining-scope node "j"))))
746
747 ;;; Tokenizer
748
749 (js2-deftest get-token "(1+1)"
750 (js2-init-scanner)
751 (should (eq js2-LP (js2-next-token)))
752 (should (eq js2-NUMBER (js2-next-token)))
753 (should (eq js2-ADD (js2-next-token)))
754 (should (eq js2-NUMBER (js2-next-token)))
755 (should (eq js2-RP (js2-next-token))))
756
757 (js2-deftest unget-token "()"
758 (js2-init-scanner)
759 (should (eq js2-LP (js2-next-token)))
760 (js2-unget-token)
761 (should (eq js2-LP (js2-next-token)))
762 (should (eq js2-RP (js2-next-token))))
763
764 (js2-deftest get-token-or-eol "x\n++;"
765 (js2-init-scanner)
766 (should (eq js2-NAME (js2-next-token)))
767 (should (eq js2-EOL (js2-peek-token-or-eol)))
768 (should (eq js2-INC (js2-next-token)))
769 (should (eq js2-SEMI (js2-peek-token-or-eol))))
770
771 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
772 (js2-init-scanner)
773 (should (eq js2-NAME (js2-next-token)))
774 (should (eq js2-NAME (js2-next-token)))
775 (should (equal "y" (js2-current-token-string)))
776 (js2-unget-token)
777 (should (eq js2-NAME (js2-current-token-type)))
778 (should (equal "x" (js2-current-token-string))))
779
780 (js2-deftest ts-seek "(1+2)"
781 (js2-init-scanner)
782 (should (eq js2-LP (js2-next-token)))
783 (should (eq js2-NUMBER (js2-next-token)))
784 (js2-unget-token)
785 (let ((state (make-js2-ts-state)))
786 (should (eq js2-NUMBER (js2-next-token)))
787 (should (eq js2-ADD (js2-next-token)))
788 (js2-ts-seek state)
789 (should (eq 1 js2-ti-lookahead))
790 (should (eq js2-NUMBER (js2-next-token)))
791 (should (eq 1 (js2-token-number
792 (js2-current-token))))))
793
794 (js2-deftest get-token-template-literal "`abc ${i} z ${j} def`"
795 (js2-init-scanner)
796 (should (eq js2-TEMPLATE_HEAD (js2-next-token)))
797 (should (equal "abc " (js2-current-token-string)))
798 (should (eq js2-NAME (js2-next-token)))
799 (should (eq js2-RC (js2-next-token)))
800 (should (eq js2-TEMPLATE_HEAD (js2-next-token 'TEMPLATE_TAIL)))
801 (should (equal " z " (js2-current-token-string)))
802 (should (eq js2-NAME (js2-next-token)))
803 (should (eq js2-RC (js2-next-token)))
804 (should (eq js2-NO_SUBS_TEMPLATE (js2-next-token 'TEMPLATE_TAIL)))
805 (should (equal " def" (js2-current-token-string))))
806
807 ;;; Error handling
808
809 (js2-deftest for-node-with-error-len "for "
810 (js2-mode)
811 (let ((node (js2-node-at-point (point-min))))
812 (should (= (js2-node-len (js2-node-parent node)) 4))))
813
814 (js2-deftest function-without-parens-error "function b {}"
815 ;; Should finish the parse.
816 (js2-mode))
817
818 ;;; Comments
819
820 (js2-deftest comment-node-length "//"
821 (js2-mode)
822 (let ((node (js2-node-at-point (point-min))))
823 (should (= (js2-node-len node) 2))))
824
825 (js2-deftest comment-node-length-newline "//\n"
826 (js2-mode)
827 (let ((node (js2-node-at-point (point-min))))
828 (should (= (js2-node-len node) 3))))
829
830 ;;; Variables classification
831
832 (defun js2--variables-summary (vars)
833 (let (r)
834 (setq vars (let (aslist)
835 (maphash (lambda (k v) (push (cons k v) aslist)) vars)
836 aslist))
837 (dolist (v (sort vars (lambda (a b) (< (js2-node-abs-pos (js2-symbol-ast-node (car a)))
838 (js2-node-abs-pos (js2-symbol-ast-node (car b)))))))
839 (let* ((symbol (car v))
840 (inition (cadr v))
841 (uses (cddr v))
842 (symn (js2-symbol-ast-node symbol))
843 (namen (js2--get-name-node symn)))
844 (push (format "%s@%s:%s"
845 (js2-symbol-name symbol)
846 (js2-node-abs-pos namen)
847 (if (eq inition ?P)
848 "P"
849 (if uses
850 (if inition "I" "N")
851 "U"))) r)
852 (dolist (u (sort (cddr v) (lambda (a b) (< (js2-node-abs-pos a)
853 (js2-node-abs-pos b)))))
854 (push (js2-node-abs-pos u) r))))
855 (reverse r)))
856
857 (defmacro js2-deftest-classify-variables (name buffer-contents summary)
858 (declare (indent defun))
859 `(ert-deftest ,(intern (format "js2-classify-variables-%s" name)) ()
860 (with-temp-buffer
861 (save-excursion
862 (insert ,buffer-contents))
863 (unwind-protect
864 (progn
865 (js2-mode)
866 (should (equal ,summary (js2--variables-summary
867 (js2--classify-variables)))))
868 (fundamental-mode)))))
869
870 (js2-deftest-classify-variables incomplete-var-statement
871 "var"
872 '())
873
874 (js2-deftest-classify-variables unused-variable
875 "function foo () { var x; return 42; }"
876 '("foo@10:U" "x@23:U"))
877
878 (js2-deftest-classify-variables unused-variable-declared-twice
879 "function foo (a) { var x; function bar () { var x; x=42; }; return a;}"
880 '("foo@10:U" "a@15:P" 68 "x@24:U" "bar@36:U" "x@49:U"))
881
882 (js2-deftest-classify-variables assigned-variable
883 "function foo () { var x; x=42; return x; }"
884 '("foo@10:U" "x@23:I" 39))
885
886 (js2-deftest-classify-variables assignment-in-nested-function
887 "function foo () { var x; function bar () { x=42; }; }"
888 '("foo@10:U" "x@23:U" "bar@35:U"))
889
890 (js2-deftest-classify-variables unused-nested-function
891 "function foo() { var i, j=1; function bar() { var x, y=42, z=i; return y; } return i; }"
892 '("foo@10:U" "i@22:N" 62 84 "j@25:U" "bar@39:U" "x@51:U" "y@54:I" 72 "z@60:U"))
893
894 (js2-deftest-classify-variables prop-get-initialized
895 "function foo () { var x, y={}; y.a=x; }"
896 '("foo@10:U" "x@23:N" 36 "y@26:I" 32))
897
898 (js2-deftest-classify-variables prop-get-uninitialized
899 "function foo () { var x; if(x.foo) alert('boom'); }"
900 '("foo@10:U" "x@23:N" 29))
901
902 (js2-deftest-classify-variables prop-get-function-assignment
903 "(function(w) { w.f = function() { var a=42, m; return a; }; })(window);"
904 '("w@11:P" 11 16 "a@39:I" 55 "m@45:U"))
905
906 (js2-deftest-classify-variables let-declaration
907 "function foo () { let x,y=1; return x; }"
908 '("foo@10:U" "x@23:N" 37 "y@25:U"))
909
910 (js2-deftest-classify-variables external-function-call
911 "function foo (m) { console.log(m, arguments); }"
912 '("foo@10:U" "m@15:P" 32))
913
914 (js2-deftest-classify-variables global-function-call
915 "function bar () { return 42; } function foo (a) { return bar(); }"
916 '("bar@10:I" 58 "foo@41:U" "a@46:P"))
917
918 (js2-deftest-classify-variables let-declaration-for-scope
919 "function foo () { for(let x=1,y; x<y; y++) {} }"
920 '("foo@10:U" "x@27:I" 34 "y@31:N" 36 39))
921
922 (js2-deftest-classify-variables arguments-implicit-var
923 "function foo () { var p; for(p in arguments) { return p; } }"
924 '("foo@10:U" "p@23:I" 55))
925
926 (js2-deftest-classify-variables catch-error-variable
927 "function foo () { try { throw 'Foo'; } catch (e) { console.log(e); }"
928 '("foo@10:U" "e@47:I" 64))
929
930 (js2-deftest-classify-variables prop-get-assignment
931 "function foo () { var x={y:{z:{}}}; x.y.z=42; }"
932 '("foo@10:U" "x@23:I" 37))
933
934 (js2-deftest-classify-variables unused-function-argument
935 "function foo (a) { return 42; }"
936 '("foo@10:U" "a@15:P"))
937
938 (js2-deftest-classify-variables used-function-argument
939 "function foo (a) { a=42; return a; }"
940 '("foo@10:U" "a@15:P" 33))
941
942 (js2-deftest-classify-variables prop-get
943 "function foo (a) { a=navigator.x||navigator.y; return a; }"
944 '("foo@10:U" "a@15:P" 55))
945
946 (js2-deftest-classify-variables for-in-loop
947 "function foo () { var d={}; for(var k in d) {var v=d[k]; } }"
948 '("foo@10:U" "d@23:I" 42 52 "k@37:I" 54 "v@50:U"))
949
950 (js2-deftest-classify-variables array-comprehension-legacy
951 "function foo() { var j,a=[for (i of [1,2,3]) i*j]; }"
952 '("foo@10:U" "j@22:N" 48 "a@24:U" "i@32:I" 46))
953
954 (js2-deftest-classify-variables array-comprehension
955 "function foo() { var j,a=[[i,j] for (i of [1,2,3])]; }"
956 '("foo@10:U" "j@22:N" 30 "a@24:U" "i@38:I" 28))
957
958 (js2-deftest-classify-variables return-named-function
959 "function foo() { var a=42; return function bar() { return a; } }"
960 '("foo@10:U" "a@22:I" 59 "bar@44:I" 44))
961
962 (js2-deftest-classify-variables named-wrapper-function
963 "function foo() { var a; (function bar() { a=42; })(); return a; }"
964 '("foo@10:U" "a@22:I" 62 "bar@35:I" 35))