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