5 (defun js2-test-string-to-ast (s)
6 (ert-with-test-buffer (:name 'origin)
9 (should (null js2-mode-buffer-dirty-p))
12 (defun js2-test-parse-string (code-string &key syntax-error)
13 (let ((ast (js2-test-string-to-ast code-string)))
15 (let ((errors (js2-ast-root-errors ast)))
16 (should (= 1 (length errors)))
17 (destructuring-bind (_ pos len) (first errors)
18 (should (string= syntax-error (substring code-string
19 (1- pos) (+ pos len -1))))))
20 (should (= 0 (length (js2-ast-root-errors ast))))
21 (ert-with-test-buffer (:name 'copy)
23 (skip-chars-backward " \t\n")
24 (should (string= code-string (buffer-substring-no-properties
25 (point-min) (point))))))))
27 (defmacro* js2-deftest-parse (name code-string &key bind syntax-error)
28 "Parse CODE-STRING. If SYNTAX-ERROR is nil, print syntax tree
29 with `js2-print-tree' and assert the result to be equal to the
30 original string. If SYNTAX-ERROR is passed, expect syntax error
31 highlighting substring equal to SYNTAX-ERROR value.
32 BIND defines bindings to apply them around the test."
33 `(ert-deftest ,(intern (format "js2-%s" name)) ()
34 (let ,(append bind '((js2-basic-offset 2)))
35 (js2-test-parse-string ,code-string :syntax-error ,syntax-error))))
37 (put 'js2-deftest-parse 'lisp-indent-function 'defun)
39 ;;; Callers of `js2-valid-prop-name-token'.
41 (js2-deftest-parse parse-property-access-when-not-keyword
44 (js2-deftest-parse parse-property-access-when-keyword
46 :bind ((js2-allow-keywords-as-property-names t)))
48 (js2-deftest-parse parse-property-access-when-keyword-no-xml
50 :bind ((js2-allow-keywords-as-property-names t)
51 (js2-compiler-xml-available nil)))
53 (js2-deftest-parse parse-array-literal-when-not-keyword
56 (js2-deftest-parse parse-array-literal-when-keyword
58 :bind ((js2-allow-keywords-as-property-names t)))
60 ;;; 'of' contextual keyword.
62 (js2-deftest-parse parse-array-comp-loop-with-of
65 (js2-deftest-parse parse-for-of
66 "for (var a of []) {\n}")
68 (js2-deftest-parse of-can-be-var-name
71 (js2-deftest-parse of-can-be-function-name
74 ;;; Destructuring binding.
76 (js2-deftest-parse destruct-in-declaration
77 "var {a, b} = {a: 1, b: 2};")
79 (js2-deftest-parse destruct-in-arguments
80 "function f({a: aa, b: bb}) {\n}")
82 (js2-deftest-parse destruct-in-array-comp-loop
83 "[a + b for ([a, b] in [[0, 1], [1, 2]])];")
85 (js2-deftest-parse destruct-in-catch-clause
86 "try {\n} catch ({a, b}) {\n a + b;\n}")
88 ;;; Function parameters.
90 (js2-deftest-parse function-with-default-parameters
91 "function foo(a = 1, b = a + 1) {\n}")
93 (js2-deftest-parse function-with-no-default-after-default
94 "function foo(a = 1, b) {\n}"
97 (js2-deftest-parse function-with-destruct-after-default
98 "function foo(a = 1, {b, c}) {\n}"
101 (js2-deftest-parse function-with-rest-parameter
102 "function foo(a, b, ...rest) {\n}")
104 (js2-deftest-parse function-with-param-after-rest-parameter
105 "function foo(a, ...b, rest) {\n}"
106 :syntax-error "rest")
108 (js2-deftest-parse function-with-destruct-after-rest-parameter
109 "function foo(a, ...b, {}) {\n}"
112 (js2-deftest-parse function-with-rest-after-default-parameter
113 "function foo(a = 1, ...rest) {\n}")