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