]> code.delx.au - gnu-emacs-elpa/blob - tests/indent.el
Fix computed property generator methods indentation
[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 jsx-one-line
183 "var foo = <div></div>;")
184
185 (js2-deftest-indent jsx-children-parentheses
186 "return (
187 | <div>
188 | </div>
189 | <div>
190 | <div></div>
191 | <div>
192 | <div></div>
193 | </div>
194 | </div>
195 |);")
196
197 (js2-deftest-indent jsx-children-unclosed
198 "return (
199 | <div>
200 | <div>")
201
202 (js2-deftest-indent jsx-argument
203 "React.render(
204 | <div>
205 | <div></div>
206 | </div>,
207 | {
208 | a: 1
209 | },
210 | <div>
211 | <div></div>
212 | </div>
213 |);")
214
215 (js2-deftest-indent jsx-leading-comment
216 "return (
217 | // Sneaky!
218 | <div></div>
219 |);")
220
221 (js2-deftest-indent jsx-trailing-comment
222 "return (
223 | <div></div>
224 | // Sneaky!
225 |);")
226
227 (js2-deftest-indent jsx-self-closing
228 ;; This ensures we know the bounds of a self-closing element
229 "React.render(
230 | <input
231 | />,
232 | {
233 | a: 1
234 | }
235 |);"
236 :bind ((sgml-attribute-offset 1))) ; Emacs 24.5 -> 25 compat
237
238 (js2-deftest-indent jsx-embedded-js-content
239 "return (
240 | <div>
241 | {array.map(function () {
242 | return {
243 | a: 1
244 | };
245 | })}
246 | </div>
247 |);")
248
249 (js2-deftest-indent jsx-embedded-js-unclosed
250 "return (
251 | <div>
252 | {array.map(function () {
253 | return {
254 | a: 1")
255
256 (js2-deftest-indent jsx-embedded-js-attribute
257 "return (
258 | <div attribute={array.map(function () {
259 | return {
260 | a: 1
261 | };
262 |
263 | return {
264 | a: 1
265 | };
266 |
267 | return {
268 | a: 1
269 | };
270 | })}>
271 | </div>
272 |);")