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