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