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