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