]> code.delx.au - gnu-emacs-elpa/blob - packages/js2-mode/tests/indent.el
Make debbugs-newest-bugs more robust
[gnu-emacs-elpa] / packages / js2-mode / tests / indent.el
1 ;;; tests/indent.el --- Some tests for js2-mode.
2
3 ;; Copyright (C) 2009, 2011-2016 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 spread-inside-array
191 "var z = [
192 | ...iterableObj,
193 | 4,
194 | 5
195 |]")
196
197 (js2-deftest-indent jsx-one-line
198 "var foo = <div></div>;")
199
200 (js2-deftest-indent jsx-children-parentheses
201 "return (
202 | <div>
203 | </div>
204 | <div>
205 | <div></div>
206 | <div>
207 | <div></div>
208 | </div>
209 | </div>
210 |);")
211
212 (js2-deftest-indent jsx-children-unclosed
213 "return (
214 | <div>
215 | <div>")
216
217 (js2-deftest-indent jsx-argument
218 "React.render(
219 | <div>
220 | <div></div>
221 | </div>,
222 | {
223 | a: 1
224 | },
225 | <div>
226 | <div></div>
227 | </div>
228 |);")
229
230 (js2-deftest-indent jsx-leading-comment
231 "return (
232 | // Sneaky!
233 | <div></div>
234 |);")
235
236 (js2-deftest-indent jsx-trailing-comment
237 "return (
238 | <div></div>
239 | // Sneaky!
240 |);")
241
242 (js2-deftest-indent jsx-self-closing
243 ;; This ensures we know the bounds of a self-closing element
244 "React.render(
245 | <input
246 | />,
247 | {
248 | a: 1
249 | }
250 |);"
251 :bind ((sgml-attribute-offset 1))) ; Emacs 24.5 -> 25 compat
252
253 (js2-deftest-indent jsx-embedded-js-content
254 "return (
255 | <div>
256 | {array.map(function () {
257 | return {
258 | a: 1
259 | };
260 | })}
261 | </div>
262 |);")
263
264 (js2-deftest-indent jsx-embedded-js-unclosed
265 "return (
266 | <div>
267 | {array.map(function () {
268 | return {
269 | a: 1")
270
271 (js2-deftest-indent jsx-embedded-js-attribute
272 "return (
273 | <div attribute={array.map(function () {
274 | return {
275 | a: 1
276 | };
277 |
278 | return {
279 | a: 1
280 | };
281 |
282 | return {
283 | a: 1
284 | };
285 | })}>
286 | </div>
287 |);")