]> code.delx.au - gnu-emacs-elpa/blob - tests/indent.el
Merge branch 'dgreensp-object-rest-spread'
[gnu-emacs-elpa] / tests / indent.el
1 ;;; tests/indent.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 'js2-mode)
24 (require 'cl-lib)
25 (require 'js2-old-indent)
26
27 (defun js2-test-indent (content keep-indent)
28 (let ((s (replace-regexp-in-string "^ *|" "" content)))
29 (with-temp-buffer
30 (insert
31 (if keep-indent
32 s
33 (replace-regexp-in-string "^ *" "" s)))
34 (js2-jsx-mode)
35 (js2-reparse) ; solely for js2-jsx-self-closing, for some reason
36 (indent-region (point-min) (point-max))
37 (should (string= s (buffer-substring-no-properties
38 (point-min) (point)))))))
39
40 (cl-defmacro js2-deftest-indent (name content &key bind keep-indent)
41 `(ert-deftest ,(intern (format "js2-%s" name)) ()
42 (let ,(append '(indent-tabs-mode
43 (js2-basic-offset 2)
44 (js2-pretty-multiline-declarations t)
45 (inhibit-point-motion-hooks t))
46 bind)
47 (js2-test-indent ,content ,keep-indent))))
48
49 (put 'js2-deftest-indent 'lisp-indent-function 'defun)
50
51 (js2-deftest-indent no-multiline-decl-indent-after-semicolon
52 "var foo = 1;
53 |bar = 2")
54
55 (js2-deftest-indent multiline-decl-indent-after-comma
56 "let foo = 1,
57 | bar = 2")
58
59 (js2-deftest-indent no-multiline-decl-when-disabled
60 "let foo = 1,
61 |bar = 2"
62 :bind ((js2-pretty-multiline-declarations nil)))
63
64 (js2-deftest-indent multiline-decl-with-continued-expr
65 "var foo = 100500
66 | + 1")
67
68 (js2-deftest-indent multiline-decl-with-continued-expr-same-line
69 "var foo = 100500 /
70 | 16;")
71
72 (js2-deftest-indent no-multiline-decl-with-operator-inside-string
73 "var foo = bar('/protocols/')
74 |baz()")
75
76 (js2-deftest-indent no-multiline-decl-implicit-semicolon
77 "var foo = 100500
78 |1")
79
80 (js2-deftest-indent multiline-decl-sees-keyword-width
81 "const foo = 1,
82 | bar = 2;")
83
84 (js2-deftest-indent multiline-decl-second-arg-value-parenthesised
85 "var foo = 1,
86 | bar = [
87 | 1, 2,
88 | 3, 4
89 | ],
90 | baz = 5;")
91
92 (js2-deftest-indent multiline-decl-first-arg-function-normal
93 "var foo = function() {
94 | return 7;
95 |},
96 | bar = 8;")
97
98 (js2-deftest-indent multiline-decl-first-arg-function-indent-all
99 "var foo = function() {
100 | return 7;
101 | },
102 | bar = 8;"
103 :bind ((js2-pretty-multiline-declarations 'all)))
104
105 (js2-deftest-indent default-keyword-as-property
106 "var foo = {
107 | case: 'zzzz',
108 | default: 'donkey',
109 | tee: 'ornery'
110 |};")
111
112 (js2-deftest-indent multiline-string-noop
113 "`multiline string
114 | contents
115 | are kept
116 | unchanged!`"
117 :keep-indent t)
118
119 (js2-deftest-indent no-multiline-decl-first-arg-function-dynamic
120 "var foo = function() {
121 | return 7;
122 |};"
123 :bind ((js2-pretty-multiline-declarations 'dynamic)))
124
125 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic
126 "var foo = function() {
127 | return 7;
128 | },
129 | bar = 8;"
130 :bind ((js2-pretty-multiline-declarations 'dynamic)))
131
132 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic-comment
133 "var foo = function() {
134 | return 7;
135 | }/* MUAHAHAHA, ah ha! */,
136 | bar = 8;"
137 :bind ((js2-pretty-multiline-declarations 'dynamic)))
138
139 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic-scan-error
140 "var foo = function() {
141 | return 7;
142 | ,
143 | bar = 8;"
144 :bind ((js2-pretty-multiline-declarations 'dynamic)))
145
146 (js2-deftest-indent indent-generator-method
147 "class A {
148 | * x() {
149 | return 1
150 | * a(2);
151 | }
152 |}")
153
154 (js2-deftest-indent indent-generator-computed-method
155 "class A {
156 | *[Symbol.iterator]() {
157 | yield 'Foo';
158 | yield 'Bar';
159 | }
160 |}")
161
162 (js2-deftest-indent case-inside-switch
163 "switch(true) {
164 |case 'true':
165 | return 1;
166 |}")
167
168 (js2-deftest-indent case-inside-switch-with-extra-indent
169 "switch(true) {
170 | case 'true':
171 | return 1;
172 |}"
173 :bind ((js2-indent-switch-body t)))
174
175 (js2-deftest-indent case-inside-switch-with-extra-indent-curly-after-newline
176 "switch(true)
177 |{
178 | case 'true':
179 | return 1;
180 |}"
181 :bind ((js2-indent-switch-body t)))
182
183 (js2-deftest-indent continued-expression-vs-unary-minus
184 "var arr = [
185 | -1, 2,
186 | -3, 4 +
187 | -5
188 |];")
189
190 (js2-deftest-indent jsx-one-line
191 "var foo = <div></div>;")
192
193 (js2-deftest-indent jsx-children-parentheses
194 "return (
195 | <div>
196 | </div>
197 | <div>
198 | <div></div>
199 | <div>
200 | <div></div>
201 | </div>
202 | </div>
203 |);")
204
205 (js2-deftest-indent jsx-children-unclosed
206 "return (
207 | <div>
208 | <div>")
209
210 (js2-deftest-indent jsx-argument
211 "React.render(
212 | <div>
213 | <div></div>
214 | </div>,
215 | {
216 | a: 1
217 | },
218 | <div>
219 | <div></div>
220 | </div>
221 |);")
222
223 (js2-deftest-indent jsx-leading-comment
224 "return (
225 | // Sneaky!
226 | <div></div>
227 |);")
228
229 (js2-deftest-indent jsx-trailing-comment
230 "return (
231 | <div></div>
232 | // Sneaky!
233 |);")
234
235 (js2-deftest-indent jsx-self-closing
236 ;; This ensures we know the bounds of a self-closing element
237 "React.render(
238 | <input
239 | />,
240 | {
241 | a: 1
242 | }
243 |);"
244 :bind ((sgml-attribute-offset 1))) ; Emacs 24.5 -> 25 compat
245
246 (js2-deftest-indent jsx-embedded-js-content
247 "return (
248 | <div>
249 | {array.map(function () {
250 | return {
251 | a: 1
252 | };
253 | })}
254 | </div>
255 |);")
256
257 (js2-deftest-indent jsx-embedded-js-unclosed
258 "return (
259 | <div>
260 | {array.map(function () {
261 | return {
262 | a: 1")
263
264 (js2-deftest-indent jsx-embedded-js-attribute
265 "return (
266 | <div attribute={array.map(function () {
267 | return {
268 | a: 1
269 | };
270 |
271 | return {
272 | a: 1
273 | };
274 |
275 | return {
276 | a: 1
277 | };
278 | })}>
279 | </div>
280 |);")