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