]> code.delx.au - gnu-emacs-elpa/blob - tests/parser.el
Parse async methods
[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 (js2-deftest-parse destruct-with-initializer-in-object
182 "var {a, b = 2, c} = {};")
183
184 (js2-deftest-parse destruct-with-initializer-in-array
185 "var [a, b = 2, c] = [];")
186
187 (js2-deftest-parse destruct-non-name-target-is-error
188 "var {1=1} = {};" :syntax-error "1" :errors-count 1)
189
190 (js2-deftest-parse destruct-with-initializer-in-function-arguments
191 "function f({a, b = 1, c}, [d, e = 1, f]) {\n}")
192
193 (js2-deftest-parse destruct-name-conflict-is-error-in-object
194 "\"use strict\";\nvar {a=1,a=2} = {};" :syntax-error "a" :errors-count 1)
195
196 (js2-deftest destruct-name-conflict-is-warning-in-array "\"use strict\";\nvar [a=1,a=2] = [];"
197 (js2-mode)
198 (should (equal '("msg.var.redecl" "a")
199 (caar js2-parsed-warnings))))
200
201 (js2-deftest initializer-outside-destruct-is-error "({a=1});"
202 (js2-mode)
203 (should (equal "msg.init.no.destruct"
204 (car (caar js2-parsed-errors)))))
205
206 ;;; Object literals
207
208 (js2-deftest-parse object-literal-shorthand
209 "var x = {a: 1, b, c: 1, d};")
210
211 (js2-deftest-parse object-literal-shorthard-with-number
212 "var a = {1};" :syntax-error "}" :errors-count 2)
213
214 (js2-deftest-parse object-literal-method
215 "var x = {f(y) { return y;\n}};")
216
217 (js2-deftest object-literal-method-own-name-in-scope "({f(){f();}});"
218 (js2-mode)
219 (should (equal '("msg.undeclared.variable" "f")
220 (caar js2-parsed-warnings))))
221
222 (js2-deftest-parse object-literal-getter-method
223 "var x = {get f() { return 42;\n}};")
224
225 (js2-deftest-parse object-literal-setter-method
226 "var x = {set f(y) { x = y;\n}};")
227
228 (js2-deftest-parse object-literal-computed-keys
229 "var x = {[Symbol.iterator]: function() {}};")
230
231 (js2-deftest-parse object-literal-computed-function-keys
232 "var x = {[foo + bar](y) { return y;\n}};")
233
234 (js2-deftest-parse object-literal-computed-getter-key
235 "var x = {get [foo + bar]() { return 42;\n}};")
236
237 (js2-deftest-parse object-literal-generator
238 "var x = {*foo() { yield 42;\n}};")
239
240 (js2-deftest-parse object-literal-computed-generator-key
241 "var x = {*[foo + bar]() { yield 42;\n}};")
242
243 ;;; Function definition
244
245 (js2-deftest function-redeclaring-var "var gen = 3; function gen() {};"
246 (js2-mode)
247 (should (= (length (js2-ast-root-warnings js2-mode-ast)) 1)))
248
249 (js2-deftest function-expression-var-same-name "var gen = function gen() {};"
250 (js2-mode)
251 (should (null (js2-ast-root-warnings js2-mode-ast))))
252
253 ;;; Function parameters
254
255 (js2-deftest-parse function-with-default-parameters
256 "function foo(a = 1, b = a + 1) {\n}")
257
258 (js2-deftest-parse function-with-no-default-after-default
259 "function foo(a = 1, b) {\n}")
260
261 (js2-deftest-parse function-with-destruct-after-default
262 "function foo(a = 1, {b, c}) {\n}")
263
264 (js2-deftest-parse function-with-rest-parameter
265 "function foo(a, b, ...rest) {\n}")
266
267 (js2-deftest-parse function-with-param-after-rest-parameter
268 "function foo(a, ...b, rest) {\n}"
269 :syntax-error "rest")
270
271 (js2-deftest-parse function-with-destruct-after-rest-parameter
272 "function foo(a, ...b, {}) {\n}"
273 :syntax-error "{}")
274
275 (js2-deftest-parse function-with-rest-after-default-parameter
276 "function foo(a = 1, ...rest) {\n}")
277
278 ;;; Strict mode errors
279
280 (js2-deftest-parse function-bad-strict-parameters
281 "'use strict';\nfunction foo(eval, {arguments}, bar) {\n}"
282 :syntax-error "eval" :errors-count 2)
283
284 (js2-deftest-parse function-retroactive-bad-strict-parameters
285 "function foo(arguments) {'use strict';}"
286 :syntax-error "arguments" :errors-count 1)
287
288 (js2-deftest-parse function-duplicate-strict-parameters
289 "'use strict';\nfunction foo(a, a) {\n}"
290 :syntax-error "a" :errors-count 1)
291
292 (js2-deftest-parse function-bad-strict-function-name
293 "'use strict';\nfunction eval() {\n}"
294 :syntax-error "eval" :errors-count 1)
295
296 (js2-deftest-parse function-bad-retroactive-strict-function-name
297 "function arguments() {'use strict';}"
298 :syntax-error "arguments" :errors-count 1)
299
300 (js2-deftest-parse function-bad-strict-catch-name
301 "'use strict';\ntry {} catch (eval) {}"
302 :syntax-error "eval" :errors-count 1)
303
304 (js2-deftest-parse function-bad-strict-variable-name
305 "'use strict';\nvar eval = 'kekeke';"
306 :syntax-error "eval" :errors-count 1)
307
308 (js2-deftest-parse function-bad-strict-assignment
309 "'use strict';\narguments = 'fufufu';"
310 :syntax-error "arguments" :errors-count 1)
311
312 (js2-deftest-parse function-property-strict-assignment
313 "'use strict';\narguments.okay = 'alright';")
314
315 (js2-deftest-parse function-strict-with
316 "'use strict';\nwith ({}) {}"
317 :syntax-error "with" :errors-count 1)
318
319 (js2-deftest-parse function-strict-octal
320 "'use strict';\nvar number = 0644;"
321 :syntax-error "0644" :errors-count 1)
322
323 (js2-deftest-parse function-strict-duplicate-keys
324 "'use strict';\nvar object = {a: 1, a: 2, 'a': 3, ['a']: 4, 1: 5, '1': 6, [1 + 1]: 7};"
325 :syntax-error "a" :errors-count 4) ; "a" has 3 dupes, "1" has 1 dupe.
326
327 (js2-deftest-parse function-strict-duplicate-getter
328 "'use strict';\nvar a = {get x() {}, get x() {}};"
329 :syntax-error "x" :errors-count 1)
330
331 (js2-deftest-parse function-strict-duplicate-setter
332 "'use strict';\nvar a = {set x() {}, set x() {}};"
333 :syntax-error "x" :errors-count 1)
334
335 ;;; Lack of errors in strict mode
336
337 (js2-deftest-parse function-strict-const-scope
338 "'use strict';\nconst a;\nif (1) {\n const a;\n}")
339
340 (js2-deftest-parse function-strict-no-getter-setter-duplicate
341 "'use strict';\nvar a = {get x() {}, set x() {}};")
342
343 ;;; Spread operator
344
345 (js2-deftest-parse spread-in-array-literal
346 "[1, ...[2, 3], 4, ...[5, 6]];")
347
348 (js2-deftest-parse spread-in-function-call
349 "f(3, ...[t(2), t(3)], 42, ...[t(4)]);")
350
351 ;;; Arrow functions
352
353 (js2-deftest-parse arrow-function-with-empty-args-and-no-curlies
354 "() => false;" :reference "() => {false};")
355
356 (js2-deftest-parse arrow-function-with-args-and-curlies
357 "(a, b = 1, ...c) => { c;\n};")
358
359 (js2-deftest-parse arrow-function-with-destructuring
360 "([{a}, b]) => { a + b;\n};")
361
362 (js2-deftest-parse parenless-arrow-function-prohibits-rest
363 "...b => {b + 1;};" :syntax-error "=>")
364
365 (js2-deftest-parse parenless-arrow-function-prohibits-destructuring
366 "[a, b] => {a + b;};" :syntax-error "]" :errors-count 4)
367
368 (js2-deftest-parse arrow-function-recovers-from-error
369 "[(,foo) => 1];" :syntax-error "," :errors-count 6)
370
371 ;;; Automatic semicolon insertion
372
373 (js2-deftest-parse no-auto-semi-insertion-after-if
374 "if (true) {\n}")
375
376 (js2-deftest-parse auto-semi-insertion-after-function
377 "a = function() {}" :reference "a = function() {};")
378
379 (js2-deftest-parse auto-semi-one-variable-per-line
380 "x\ny" :reference "x;\ny;")
381
382 ;;; Labels
383
384 (js2-deftest-parse labeled-stmt-node
385 "foo:\nbar:\nx = y + 1;")
386
387 (js2-deftest no-label-node-inside-expr "x = y:"
388 (let (js2-parse-interruptable-p)
389 (js2-mode))
390 (let ((assignment (js2-expr-stmt-node-expr (car (js2-scope-kids js2-mode-ast)))))
391 (should (js2-name-node-p (js2-assign-node-right assignment)))))
392
393 (js2-deftest-parse label-and-loops "for (; ; ) {
394 loop:
395 for (; ; ) {
396 continue loop;
397 }
398 }")
399
400 ;;; Generators
401
402 (js2-deftest-parse legacy-generator "function foo() {\n yield 1;\n}")
403
404 (js2-deftest-parse legacy-generator-cannot-return
405 "function foo() {\n yield 1;\n return 2;\n}" :syntax-error "return 2")
406
407 (js2-deftest-parse harmony-generator "function* bar() {\n yield 2;\n return 3;\n}")
408
409 (js2-deftest-parse harmony-generator-yield-star "(function*(a) { yield* a;\n});")
410
411 ;;; Comprehensions
412
413 (js2-deftest-parse parse-legacy-array-comp-loop-with-filter
414 "[a for (a in b) if (a == 2)];")
415
416 (js2-deftest-parse parse-array-comp-loop-with-filters
417 "[for (a in b) if (a == 2) if (b != 10) a];")
418
419 (js2-deftest-parse parse-generator-comp-loop-with-filters
420 "(for (x of y) if (x != 4) x);")
421
422 (js2-deftest-parse parse-array-comp-with-yield-is-ok
423 "(function() { return [for (x of []) yield x];\n});")
424
425 (js2-deftest-parse parse-generator-comp-with-yield-is-not-ok
426 "(function() { return (for (x of []) yield x);\n});"
427 :syntax-error "yield")
428
429 (js2-deftest-parse parse-generator-comp-with-yield-inside-function-is-ok
430 "(for (x of []) function*() { yield x;\n});")
431
432 ;;; Async
433
434 (js2-deftest-parse async-function-statement
435 "async function foo() {\n}")
436
437 (js2-deftest-parse async-function-statement-inside-block
438 "if (true) {\n async function foo() {\n }\n}")
439
440 (js2-deftest-parse async-function-expression-statements-are-verboten
441 "async function() {}" :syntax-error "(")
442
443 (js2-deftest-parse async-named-function-expression
444 "a = async function b() {};")
445
446 (js2-deftest-parse async-arrow-function-expression
447 "a = async (b) => { b;\n};")
448
449 (js2-deftest-parse async-method-in-object-literal
450 "({async f() {}});")
451
452 (js2-deftest-parse async-method-in-class-body
453 "class C {\n async foo() {}\n}")
454
455 (js2-deftest-parse static-async-method-in-class-body
456 "class C {\n static async foo() {}\n}")
457
458 (js2-deftest-parse async-method-allow-await
459 "({async f() { await x;\n}});")
460
461 ;;; Await
462
463 (js2-deftest-parse await-is-ok "async function foo() {\n await bar();\n}")
464
465 (js2-deftest-parse await-inside-assignment-is-ok
466 "async function foo() {\n var result = await bar();\n}")
467
468 (js2-deftest-parse await-inside-array-is-ok
469 "async function foo() {\n var results = [await bar(), await baz()];\n}")
470
471 (js2-deftest-parse await-inside-non-async-function-is-not-ok
472 "function foo() {\n await bar();\n}"
473 :syntax-error "await")
474
475 (js2-deftest-parse await-inside-non-async-arrow-function-is-not-ok
476 "a = () => { await bar();\n}"
477 :syntax-error "await")
478
479 ;;; 'async' and 'await' are contextual keywords
480
481 (js2-deftest-parse async-can-be-var-name
482 "var async = 3;")
483
484 (js2-deftest-parse async-can-be-function-name
485 "function async() {\n}")
486
487 (js2-deftest-parse await-can-be-var-name
488 "var await = 3;")
489
490 (js2-deftest-parse await-can-be-function-name
491 "function await() {\n}")
492
493 ;;; Numbers
494
495 (js2-deftest-parse decimal-starting-with-zero "081;" :reference "81;")
496
497 (js2-deftest-parse huge-hex "0x0123456789abcdefABCDEF;" :reference "-1;")
498
499 (js2-deftest-parse octal-without-o "071;" :reference "57;")
500
501 (js2-deftest-parse hex-number-okay "0x123;" :reference "291;")
502
503 (js2-deftest-parse hex-number-broken "0xz23;"
504 :syntax-error "0xz" :errors-count 2)
505
506 (js2-deftest-parse binary-number-okay "0b101;" :reference "5;")
507
508 (js2-deftest-parse binary-number-broken "0b210;"
509 :syntax-error "0b2" :errors-count 2)
510
511 (js2-deftest-parse octal-number-okay "0o765;" :reference "501;")
512
513 (js2-deftest-parse octal-number-broken "0o812;"
514 :syntax-error "0o8" :errors-count 2)
515
516 ;;; Modules
517
518 (js2-deftest parse-export-bindings "{one, two as dos}"
519 (js2-init-scanner)
520 (should (js2-match-token js2-LC))
521 (let ((imports (js2-parse-export-bindings)))
522 (should (not (equal nil imports)))
523 (should (= 2 (length imports)))
524 (let ((first (nth 0 imports))
525 (second (nth 1 imports)))
526 (should (equal "one" (js2-name-node-name (js2-export-binding-node-extern-name first))))
527 (should (equal "two" (js2-name-node-name (js2-export-binding-node-extern-name second))))
528 (let ((first-name (js2-export-binding-node-local-name first))
529 (second-name (js2-export-binding-node-local-name second)))
530 (should (equal first (js2-node-parent first-name)))
531 (should (equal 3 (js2-node-len first-name)))
532 (should (equal "one" (js2-name-node-name first-name)))
533 (should (equal second (js2-node-parent second-name)))
534 (should (equal 3 (js2-node-len second-name)))
535 (should (equal "dos" (js2-name-node-name second-name)))))))
536
537 (js2-deftest parse-export-binding-as-default "one as default"
538 (js2-init-scanner)
539 (let ((binding (js2-maybe-parse-export-binding)))
540 (should binding)
541 (should (js2-export-binding-node-p binding))
542 (let ((name (js2-export-binding-node-local-name binding)))
543 (should name)
544 (should (equal "default" (js2-name-node-name name))))))
545
546 (js2-deftest parse-namepsace-import "* as lib;"
547 (js2-init-scanner)
548 (should (js2-match-token js2-MUL))
549 (let ((namespace-import (js2-parse-namespace-import)))
550 (should (not (equal nil namespace-import)))
551 (should (js2-namespace-import-node-p namespace-import))
552 (should (= 1 (js2-node-pos namespace-import)))
553 (should (equal 8 (js2-node-len namespace-import)))
554 (let ((name-node (js2-namespace-import-node-name namespace-import)))
555 (should (equal "lib" (js2-name-node-name name-node)))
556 (should (= 5 (js2-node-pos name-node))))))
557
558 (js2-deftest parse-from-clause "from 'foo/bar';"
559 (js2-init-scanner)
560 (let ((from (js2-parse-from-clause)))
561 (should (not (equal nil from)))
562 (should (= 1 (js2-node-pos from)))
563 (should (= 14 (js2-node-len from)))
564 (should (equal "foo/bar" (js2-from-clause-node-module-id from)))))
565
566 (js2-deftest parse-import-module-id-only "import 'src/lib'"
567 (js2-init-scanner)
568 (should (js2-match-token js2-IMPORT))
569 (let ((import (js2-parse-import)))
570 (should (not (equal nil import)))
571 (should (= 1 (js2-node-pos import)))
572 (should (= 16 (js2-node-len import)))
573 (should (equal nil (js2-import-node-import import)))
574 (should (equal nil (js2-import-node-from import)))))
575
576 (js2-deftest parse-imported-default-binding "import theDefault from 'src/lib'"
577 (js2-push-scope (make-js2-scope :pos 0))
578 (js2-init-scanner)
579 (should (js2-match-token js2-IMPORT))
580 (let ((import-node (js2-parse-import)))
581 (should (not (equal nil import-node)))
582 (should (equal "src/lib" (js2-import-node-module-id import-node)))
583 (let ((import (js2-import-node-import import-node)))
584 (should (not (equal nil import)))
585 (should (equal nil (js2-import-clause-node-namespace-import import)))
586 (should (equal nil (js2-import-clause-node-named-imports import)))
587 (let ((default (js2-import-clause-node-default-binding import)))
588 (should (not (equal nil default)))
589 (should (js2-export-binding-node-p default))
590 (should (equal "theDefault" (js2-name-node-name (js2-export-binding-node-extern-name default)))))))
591 (should (js2-scope-get-symbol js2-current-scope "theDefault")))
592
593 (js2-deftest parse-import-namespace-binding "import * as lib from 'src/lib'"
594 (js2-push-scope (make-js2-scope :pos 0))
595 (js2-init-scanner)
596 (should (js2-match-token js2-IMPORT))
597 (let ((import-node (js2-parse-import)))
598 (should (not (equal nil import-node)))
599 (should (equal "src/lib" (js2-import-node-module-id import-node)))
600 (let ((import (js2-import-node-import import-node)))
601 (should (not (equal nil import)))
602 (should (equal nil (js2-import-clause-node-default-binding import)))
603 (should (equal nil (js2-import-clause-node-named-imports import)))
604 (let ((ns-import (js2-import-clause-node-namespace-import import)))
605 (should (not (equal nil ns-import)))
606 (should (js2-namespace-import-node-p ns-import))
607 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
608 (should (js2-scope-get-symbol js2-current-scope "lib")))
609
610 (js2-deftest parse-import-named-imports "import {foo as bar, baz} from 'src/lib'"
611 (js2-push-scope (make-js2-scope :pos 0))
612 (js2-init-scanner)
613 (should (js2-match-token js2-IMPORT))
614 (let ((import-node (js2-parse-import)))
615 (should (not (equal nil import-node)))
616 (should (equal "src/lib" (js2-import-node-module-id import-node)))
617 (let ((import (js2-import-node-import import-node)))
618 (should (not (equal nil import)))
619 (should (equal nil (js2-import-clause-node-default-binding import)))
620 (should (equal nil (js2-import-clause-node-namespace-import import)))
621 (let ((named-imports (js2-import-clause-node-named-imports import)))
622 (should (not (equal nil named-imports)))
623 (should (listp named-imports))
624 (should (= 2 (length named-imports)))
625 (let ((first (nth 0 named-imports))
626 (second (nth 1 named-imports)))
627 (should (equal "bar" (js2-name-node-name (js2-export-binding-node-local-name first))))
628 (should (equal "baz" (js2-name-node-name (js2-export-binding-node-local-name second))))))))
629 (should (js2-scope-get-symbol js2-current-scope "bar"))
630 (should (js2-scope-get-symbol js2-current-scope "baz")))
631
632 (js2-deftest parse-import-default-and-namespace "import stuff, * as lib from 'src/lib'"
633 (js2-push-scope (make-js2-scope :pos 0))
634 (js2-init-scanner)
635 (should (js2-match-token js2-IMPORT))
636 (let ((import-node (js2-parse-import)))
637 (should (not (equal nil import-node)))
638 (should (equal "src/lib" (js2-import-node-module-id import-node)))
639 (let ((import (js2-import-node-import import-node)))
640 (should (not (equal nil import)))
641 (should (equal nil (js2-import-clause-node-named-imports import)))
642 (let ((default (js2-import-clause-node-default-binding import))
643 (ns-import (js2-import-clause-node-namespace-import import)))
644 (should (not (equal nil default)))
645 (should (equal "stuff" (js2-name-node-name (js2-export-binding-node-local-name default))))
646 (should (not (equal nil ns-import)))
647 (should (js2-namespace-import-node-p ns-import))
648 (should (equal "lib" (js2-name-node-name (js2-namespace-import-node-name ns-import)))))))
649 (should (js2-scope-get-symbol js2-current-scope "stuff"))
650 (should (js2-scope-get-symbol js2-current-scope "lib")))
651
652 (js2-deftest parse-import-default-and-named-imports
653 "import robert as bob, {cookies, pi as PIE} from 'src/lib'"
654 (js2-push-scope (make-js2-scope :pos 0))
655 (js2-init-scanner)
656 (should (js2-match-token js2-IMPORT))
657 (let ((import-node (js2-parse-import)))
658 (should (not (equal nil import-node)))
659 (should (equal "src/lib" (js2-import-node-module-id import-node)))
660 (let ((import (js2-import-node-import import-node)))
661 (should (not (equal nil import)))
662 (should (not (equal nil (js2-import-clause-node-named-imports import))))
663 (let ((default (js2-import-clause-node-default-binding import))
664 (named-imports (js2-import-clause-node-named-imports import)))
665 (should (not (equal nil default)))
666 (should (equal "bob" (js2-name-node-name (js2-export-binding-node-local-name default))))
667 (should (not (equal nil named-imports)))
668 (should (= 2 (length named-imports))))))
669 (should (js2-scope-get-symbol js2-current-scope "bob"))
670 (should (js2-scope-get-symbol js2-current-scope "cookies"))
671 (should (js2-scope-get-symbol js2-current-scope "PIE")))
672
673 (js2-deftest parse-this-module-in-from-clause "import {url} from this module;"
674 (js2-push-scope (make-js2-scope :pos 0))
675 (js2-init-scanner)
676 (should (js2-match-token js2-IMPORT))
677 (let ((import-node (js2-parse-import)))
678 (should import-node)
679 (let ((from-clause (js2-import-node-from import-node)))
680 (should from-clause)
681 (should (equal "this" (js2-from-clause-node-module-id from-clause)))
682 (should (js2-from-clause-node-metadata-p from-clause)))))
683
684 (js2-deftest-parse import-only-for-side-effects "import 'src/lib';")
685 (js2-deftest-parse import-default-only "import theDefault from 'src/lib';")
686 (js2-deftest-parse import-named-only "import {one, two} from 'src/lib';")
687 (js2-deftest-parse import-default-and-named "import theDefault, {one, two} from 'src/lib';")
688 (js2-deftest-parse import-renaming-default "import * as lib from 'src/mylib';")
689 (js2-deftest-parse import-renaming-named "import {one as uno, two as dos} from 'src/lib';")
690 (js2-deftest-parse import-default-and-namespace "import robert as bob, * as lib from 'src/lib';")
691 (js2-deftest-parse import-from-this-module "import {url} from this module;")
692
693 ;; Module Exports
694
695 (js2-deftest export-rexport "export * from 'other/lib'"
696 (js2-init-scanner)
697 (should (js2-match-token js2-EXPORT))
698 (let ((export-node (js2-parse-export)))
699 (should export-node)
700 (should (js2-export-node-from-clause export-node))))
701
702 (js2-deftest export-export-named-list "export {foo, bar as bang};"
703 (js2-init-scanner)
704 (should (js2-match-token js2-EXPORT))
705 (let ((export-node (js2-parse-export)))
706 (should export-node)
707 (let ((exports (js2-export-node-exports-list export-node)))
708 (should exports)
709 (should (= 2 (length exports))))))
710
711 (js2-deftest re-export-named-list "export {foo, bar as bang} from 'other/lib'"
712 (js2-init-scanner)
713 (should (js2-match-token js2-EXPORT))
714 (let ((export-node (js2-parse-export)))
715 (should export-node)
716 (should (js2-export-node-from-clause export-node))
717 (let ((exports (js2-export-node-exports-list export-node)))
718 (should exports)
719 (should (= 2 (length exports))))))
720
721 (js2-deftest export-variable-statement "export var foo = 'bar', baz = 'bang';"
722 (js2-init-scanner)
723 (js2-push-scope (make-js2-scope :pos 0))
724 (should (js2-match-token js2-EXPORT))
725 (let ((export-node (js2-parse-export)))
726 (should export-node)
727 (should (js2-export-node-declaration export-node))))
728
729 (js2-deftest export-const-declaration "export const PI = Math.PI;"
730 (js2-init-scanner)
731 (js2-push-scope (make-js2-scope :pos 0))
732 (should (js2-match-token js2-EXPORT))
733 (let ((export-node (js2-parse-export)))
734 (should export-node)
735 (should (js2-export-node-declaration export-node))))
736
737 (js2-deftest export-let-declaration "export let foo = [1];"
738 (js2-init-scanner)
739 (js2-push-scope (make-js2-scope :pos 0))
740 (should (js2-match-token js2-EXPORT))
741 (let ((export-node (js2-parse-export)))
742 (should export-node)
743 (should (js2-var-decl-node-p (js2-export-node-declaration export-node)))))
744
745 (js2-deftest export-class-declaration "export class Foo {};"
746 (js2-init-scanner)
747 (js2-push-scope (make-js2-scope :pos 0))
748 (should (js2-match-token js2-EXPORT))
749 (let ((export-node (js2-parse-export)))
750 (should export-node)
751 (should (js2-class-node-p (js2-export-node-declaration export-node)))))
752
753 (js2-deftest export-function-declaration "export default function doStuff() {};"
754 (js2-init-scanner)
755 (js2-push-scope (make-js2-scope :pos 0))
756 (should (js2-match-token js2-EXPORT))
757 (let ((export-node (js2-parse-export)))
758 (should export-node)
759 (should (js2-export-node-default export-node))))
760
761 (js2-deftest export-generator-declaration "export default function* one() {};"
762 (js2-init-scanner)
763 (js2-push-scope (make-js2-scope :pos 0))
764 (should (js2-match-token js2-EXPORT))
765 (let ((export-node (js2-parse-export)))
766 (should export-node)
767 (should (js2-export-node-default export-node))))
768
769 (js2-deftest export-assignment-expression "export default a = b;"
770 (js2-init-scanner)
771 (js2-push-scope (make-js2-scope :pos 0))
772 (should (js2-match-token js2-EXPORT))
773 (let ((export-node (js2-parse-export)))
774 (should export-node)
775 (should (js2-export-node-default export-node))))
776
777 (js2-deftest export-function-no-semicolon "export default function foo() {}"
778 (js2-mode)
779 (should (equal nil js2-parsed-warnings)))
780 (js2-deftest export-default-function-no-semicolon "export function foo() {}"
781 (js2-mode)
782 (should (equal nil js2-parsed-warnings)))
783 (js2-deftest export-anything-else-does-require-a-semicolon "export var obj = {}"
784 (js2-mode)
785 (should (not (equal nil js2-parsed-warnings))))
786
787 (js2-deftest-parse parse-export-rexport "export * from 'other/lib';")
788 (js2-deftest-parse parse-export-export-named-list "export {foo, bar as bang};")
789 (js2-deftest-parse parse-re-export-named-list "export {foo, bar as bang} from 'other/lib';")
790 (js2-deftest-parse parse-export-const-declaration "export const PI = Math.PI;")
791 (js2-deftest-parse parse-export-let-declaration "export let foo = [1];")
792 (js2-deftest-parse parse-export-function-declaration "export default function doStuff() {};")
793 (js2-deftest-parse parse-export-generator-declaration "export default function* one() {};")
794 (js2-deftest-parse parse-export-assignment-expression "export default a = b;")
795
796 ;;; Strings
797
798 (js2-deftest-parse string-literal
799 "var x = 'y';")
800
801 (js2-deftest-parse object-get-string-literal
802 "var x = {y: 5};\nvar z = x[\"y\"];")
803
804 (js2-deftest-parse template-no-substritutions
805 "var x = `abc
806 def`, y = `\\u0000`;")
807
808 (js2-deftest-parse template-with-substitutions
809 "var y = `${a + b} ${d + e + f}`;")
810
811 (js2-deftest-parse tagged-template
812 "foo.args`${++x, \"o\"}k`;")
813
814 ;;; Classes
815
816 (js2-deftest-parse parse-harmony-class-statement
817 "class Foo {\n get bar() { return 42;\n}\n set bar(x) { y = x;\n}\n}")
818
819 (js2-deftest-parse parse-harmony-class-statement-without-name-is-not-ok
820 "class {\n get bar() { return 42;\n}\n}"
821 :syntax-error "{")
822
823 (js2-deftest-parse parse-harmony-class-expression
824 "var Foo1 = class Foo {\n bar() { return 42;\n}\n};")
825
826 (js2-deftest-parse parse-harmony-anonymous-class-expression
827 "var Foo = class {\n set bar(x) { bar = x;\n}\n};")
828
829 (js2-deftest-parse parse-harmony-class-with-extends
830 "class Foo extends Bar {\n}")
831
832 (js2-deftest-parse parse-harmony-anonymous-class-with-extends
833 "foo.Foo = class extends Bar {\n set bar(x) { bar = x;\n}\n};")
834
835 (js2-deftest-parse parse-harmony-class-with-complex-extends
836 "class Foo extends foo[BAR][2].Baz {\n}")
837
838 (js2-deftest-parse parse-harmony-class-missing-extended-class-is-not-ok
839 "class Foo extends {\n}"
840 :syntax-error "extends")
841
842 (js2-deftest-parse parse-harmony-class-static-method
843 "class Foo extends Bar {\n static bar() { return 42;\n}\n}")
844
845 (js2-deftest-parse parse-unterminated-class-is-not-okay
846 "class Foo {\n get bar() { return 42;\n}"
847 :syntax-error "}")
848
849 (js2-deftest-parse parse-super-keyword
850 "class Foo {\n constructor() { super(42);\n}\n foo() { super.foo();\n}\n}")
851
852 (js2-deftest-parse parse-class-keywordlike-method
853 "class C {\n delete() {}\n if() {}\n}")
854
855 (js2-deftest-parse parse-harmony-class-allow-semicolon-element
856 "class Foo {;}" :reference "class Foo {\n}")
857
858 ;;; Scopes
859
860 (js2-deftest ast-symbol-table-includes-fn-node "function foo() {}"
861 (js2-mode)
862 (let ((entry (js2-scope-get-symbol js2-mode-ast 'foo)))
863 (should (= (js2-symbol-decl-type entry) js2-FUNCTION))
864 (should (equal (js2-symbol-name entry) "foo"))
865 (should (js2-function-node-p (js2-symbol-ast-node entry)))))
866
867 (js2-deftest fn-symbol-table-includes-nested-fn "function foo() {
868 function bar() {}
869 var x;
870 }"
871 (js2-mode)
872 (let* ((scope (js2-node-at-point (point-min)))
873 (fn-entry (js2-scope-get-symbol scope 'bar))
874 (var-entry (js2-scope-get-symbol scope 'x)))
875 (should (string= (js2-name-node-name (js2-function-node-name scope)) "foo"))
876 (should (= (js2-symbol-decl-type fn-entry) js2-FUNCTION))
877 (should (js2-function-node-p (js2-symbol-ast-node fn-entry)))
878 (should (= (js2-symbol-decl-type var-entry) js2-VAR))
879 (should (js2-name-node-p (js2-symbol-ast-node var-entry)))))
880
881 (defun js2-test-scope-of-nth-variable-satisifies-predicate (variable nth predicate)
882 (goto-char (point-min))
883 (dotimes (n (1+ nth)) (search-forward variable))
884 (forward-char -1)
885 (let ((scope (js2-node-get-enclosing-scope (js2-node-at-point))))
886 (should (funcall predicate (js2-get-defining-scope scope variable)))))
887
888 (js2-deftest for-node-is-declaration-scope "for (let i = 0; i; ++i) {};"
889 (js2-mode)
890 (js2-test-scope-of-nth-variable-satisifies-predicate "i" 0 #'js2-for-node-p))
891
892 (js2-deftest const-scope-sloppy-script "{const a;} a;"
893 (js2-mode)
894 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-script-node-p)
895 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'js2-script-node-p))
896
897 (js2-deftest const-scope-strict-script "'use strict'; { const a; } a;"
898 (js2-mode)
899 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-block-node-p)
900 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'null))
901
902 (js2-deftest const-scope-sloppy-function "function f() { { const a; } a; }"
903 (js2-mode)
904 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-function-node-p)
905 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'js2-function-node-p))
906
907 (js2-deftest const-scope-strict-function "function f() { 'use strict'; { const a; } a; }"
908 (js2-mode)
909 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 0 #'js2-block-node-p)
910 (js2-test-scope-of-nth-variable-satisifies-predicate "a" 1 #'null))
911
912 (js2-deftest array-comp-is-result-scope "[x * 2 for (x in y)];"
913 (js2-mode)
914 (js2-test-scope-of-nth-variable-satisifies-predicate "x" 0 #'js2-comp-loop-node-p))
915
916 (js2-deftest array-comp-has-parent-scope
917 "var a,b=[for (i of [[1,2]]) for (j of i) j * a];"
918 (js2-mode)
919 (search-forward "for")
920 (forward-char -3)
921 (let ((node (js2-node-at-point)))
922 (should (js2-scope-parent-scope node))
923 (should (js2-get-defining-scope node "j"))))
924
925 ;;; Tokenizer
926
927 (js2-deftest get-token "(1+1)"
928 (js2-init-scanner)
929 (should (eq js2-LP (js2-next-token)))
930 (should (eq js2-NUMBER (js2-next-token)))
931 (should (eq js2-ADD (js2-next-token)))
932 (should (eq js2-NUMBER (js2-next-token)))
933 (should (eq js2-RP (js2-next-token))))
934
935 (js2-deftest unget-token "()"
936 (js2-init-scanner)
937 (should (eq js2-LP (js2-next-token)))
938 (js2-unget-token)
939 (should (eq js2-LP (js2-next-token)))
940 (should (eq js2-RP (js2-next-token))))
941
942 (js2-deftest get-token-or-eol "x\n++;"
943 (js2-init-scanner)
944 (should (eq js2-NAME (js2-next-token)))
945 (should (eq js2-EOL (js2-peek-token-or-eol)))
946 (should (eq js2-INC (js2-next-token)))
947 (should (eq js2-SEMI (js2-peek-token-or-eol))))
948
949 (js2-deftest unget-token-over-eol-and-comment "x\n//abc\ny"
950 (js2-init-scanner)
951 (should (eq js2-NAME (js2-next-token)))
952 (should (eq js2-NAME (js2-next-token)))
953 (should (equal "y" (js2-current-token-string)))
954 (js2-unget-token)
955 (should (eq js2-NAME (js2-current-token-type)))
956 (should (equal "x" (js2-current-token-string))))
957
958 (js2-deftest ts-seek "(1+2)"
959 (js2-init-scanner)
960 (should (eq js2-LP (js2-next-token)))
961 (should (eq js2-NUMBER (js2-next-token)))
962 (js2-unget-token)
963 (let ((state (make-js2-ts-state)))
964 (should (eq js2-NUMBER (js2-next-token)))
965 (should (eq js2-ADD (js2-next-token)))
966 (js2-ts-seek state)
967 (should (eq 1 js2-ti-lookahead))
968 (should (eq js2-NUMBER (js2-next-token)))
969 (should (eq 1 (js2-token-number
970 (js2-current-token))))))
971
972 (js2-deftest get-token-template-literal "`abc ${i} z ${j} def`"
973 (js2-init-scanner)
974 (should (eq js2-TEMPLATE_HEAD (js2-next-token)))
975 (should (equal "abc " (js2-current-token-string)))
976 (should (eq js2-NAME (js2-next-token)))
977 (should (eq js2-RC (js2-next-token)))
978 (should (eq js2-TEMPLATE_HEAD (js2-next-token 'TEMPLATE_TAIL)))
979 (should (equal " z " (js2-current-token-string)))
980 (should (eq js2-NAME (js2-next-token)))
981 (should (eq js2-RC (js2-next-token)))
982 (should (eq js2-NO_SUBS_TEMPLATE (js2-next-token 'TEMPLATE_TAIL)))
983 (should (equal " def" (js2-current-token-string))))
984
985 ;;; Error handling
986
987 (js2-deftest for-node-with-error-len "for "
988 (js2-mode)
989 (let ((node (js2-node-at-point (point-min))))
990 (should (= (js2-node-len (js2-node-parent node)) 4))))
991
992 (js2-deftest function-without-parens-error "function b {}"
993 ;; Should finish the parse.
994 (js2-mode))
995
996 ;;; Comments
997
998 (js2-deftest comment-node-length "//"
999 (js2-mode)
1000 (let ((node (js2-node-at-point (point-min))))
1001 (should (= (js2-node-len node) 2))))
1002
1003 (js2-deftest comment-node-length-newline "//\n"
1004 (js2-mode)
1005 (let ((node (js2-node-at-point (point-min))))
1006 (should (= (js2-node-len node) 3))))
1007
1008 ;;; Variables classification
1009
1010 (defun js2--variables-summary (vars)
1011 (let (r)
1012 (setq vars (let (aslist)
1013 (maphash (lambda (k v) (push (cons k v) aslist)) vars)
1014 aslist))
1015 (dolist (v (sort vars (lambda (a b) (< (js2-node-abs-pos (js2-symbol-ast-node (car a)))
1016 (js2-node-abs-pos (js2-symbol-ast-node (car b)))))))
1017 (let* ((symbol (car v))
1018 (inition (cadr v))
1019 (uses (cddr v))
1020 (symn (js2-symbol-ast-node symbol))
1021 (namen (js2--get-name-node symn)))
1022 (push (format "%s@%s:%s"
1023 (js2-symbol-name symbol)
1024 (js2-node-abs-pos namen)
1025 (if (eq inition ?P)
1026 "P"
1027 (if uses
1028 (if inition "I" "N")
1029 "U"))) r)
1030 (dolist (u (sort (cddr v) (lambda (a b) (< (js2-node-abs-pos a)
1031 (js2-node-abs-pos b)))))
1032 (push (js2-node-abs-pos u) r))))
1033 (reverse r)))
1034
1035 (defmacro js2-deftest-classify-variables (name buffer-contents summary)
1036 (declare (indent defun))
1037 `(ert-deftest ,(intern (format "js2-classify-variables-%s" name)) ()
1038 (with-temp-buffer
1039 (save-excursion
1040 (insert ,buffer-contents))
1041 (unwind-protect
1042 (progn
1043 (js2-mode)
1044 (should (equal ,summary (js2--variables-summary
1045 (js2--classify-variables)))))
1046 (fundamental-mode)))))
1047
1048 (js2-deftest-classify-variables incomplete-var-statement
1049 "var"
1050 '())
1051
1052 (js2-deftest-classify-variables unused-variable
1053 "function foo () { var x; return 42; }"
1054 '("foo@10:U" "x@23:U"))
1055
1056 (js2-deftest-classify-variables unused-variable-declared-twice
1057 "function foo (a) { var x; function bar () { var x; x=42; }; return a;}"
1058 '("foo@10:U" "a@15:P" 68 "x@24:U" "bar@36:U" "x@49:U"))
1059
1060 (js2-deftest-classify-variables assigned-variable
1061 "function foo () { var x; x=42; return x; }"
1062 '("foo@10:U" "x@23:I" 39))
1063
1064 (js2-deftest-classify-variables assignment-in-nested-function
1065 "function foo () { var x; function bar () { x=42; }; }"
1066 '("foo@10:U" "x@23:U" "bar@35:U"))
1067
1068 (js2-deftest-classify-variables unused-nested-function
1069 "function foo() { var i, j=1; function bar() { var x, y=42, z=i; return y; } return i; }"
1070 '("foo@10:U" "i@22:N" 62 84 "j@25:U" "bar@39:U" "x@51:U" "y@54:I" 72 "z@60:U"))
1071
1072 (js2-deftest-classify-variables prop-get-initialized
1073 "function foo () { var x, y={}; y.a=x; }"
1074 '("foo@10:U" "x@23:N" 36 "y@26:I" 32))
1075
1076 (js2-deftest-classify-variables prop-get-uninitialized
1077 "function foo () { var x; if(x.foo) alert('boom'); }"
1078 '("foo@10:U" "x@23:N" 29))
1079
1080 (js2-deftest-classify-variables prop-get-function-assignment
1081 "(function(w) { w.f = function() { var a=42, m; return a; }; })(window);"
1082 '("w@11:P" 11 16 "a@39:I" 55 "m@45:U"))
1083
1084 (js2-deftest-classify-variables let-declaration
1085 "function foo () { let x,y=1; return x; }"
1086 '("foo@10:U" "x@23:N" 37 "y@25:U"))
1087
1088 (js2-deftest-classify-variables external-function-call
1089 "function foo (m) { console.log(m, arguments); }"
1090 '("foo@10:U" "m@15:P" 32))
1091
1092 (js2-deftest-classify-variables global-function-call
1093 "function bar () { return 42; } function foo (a) { return bar(); }"
1094 '("bar@10:I" 58 "foo@41:U" "a@46:P"))
1095
1096 (js2-deftest-classify-variables let-declaration-for-scope
1097 "function foo () { for(let x=1,y; x<y; y++) {} }"
1098 '("foo@10:U" "x@27:I" 34 "y@31:N" 36 39))
1099
1100 (js2-deftest-classify-variables arguments-implicit-var
1101 "function foo () { var p; for(p in arguments) { return p; } }"
1102 '("foo@10:U" "p@23:I" 55))
1103
1104 (js2-deftest-classify-variables catch-error-variable
1105 "function foo () { try { throw 'Foo'; } catch (e) { console.log(e); }"
1106 '("foo@10:U" "e@47:I" 64))
1107
1108 (js2-deftest-classify-variables prop-get-assignment
1109 "function foo () { var x={y:{z:{}}}; x.y.z=42; }"
1110 '("foo@10:U" "x@23:I" 37))
1111
1112 (js2-deftest-classify-variables unused-function-argument
1113 "function foo (a) { return 42; }"
1114 '("foo@10:U" "a@15:P"))
1115
1116 (js2-deftest-classify-variables used-function-argument
1117 "function foo (a) { a=42; return a; }"
1118 '("foo@10:U" "a@15:P" 33))
1119
1120 (js2-deftest-classify-variables prop-get
1121 "function foo (a) { a=navigator.x||navigator.y; return a; }"
1122 '("foo@10:U" "a@15:P" 55))
1123
1124 (js2-deftest-classify-variables for-in-loop
1125 "function foo () { var d={}; for(var k in d) {var v=d[k]; } }"
1126 '("foo@10:U" "d@23:I" 42 52 "k@37:I" 54 "v@50:U"))
1127
1128 (js2-deftest-classify-variables array-comprehension-legacy
1129 "function foo() { var j,a=[for (i of [1,2,3]) i*j]; }"
1130 '("foo@10:U" "j@22:N" 48 "a@24:U" "i@32:I" 46))
1131
1132 (js2-deftest-classify-variables array-comprehension
1133 "function foo() { var j,a=[[i,j] for (i of [1,2,3])]; }"
1134 '("foo@10:U" "j@22:N" 30 "a@24:U" "i@38:I" 28))
1135
1136 (js2-deftest-classify-variables return-named-function
1137 "function foo() { var a=42; return function bar() { return a; } }"
1138 '("foo@10:U" "a@22:I" 59 "bar@44:I" 44))
1139
1140 (js2-deftest-classify-variables named-wrapper-function
1141 "function foo() { var a; (function bar() { a=42; })(); return a; }"
1142 '("foo@10:U" "a@22:I" 62 "bar@35:I" 35))