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