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