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