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