blob: 52f45c7ddd1e07ef36caa2791796fa9a8fcaeed9 [file] [log] [blame]
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +00001//===- unittest/Format/FormatTestJS.cpp - Formatting unit tests for JS ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000010#include "FormatTestUtils.h"
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000011#include "clang/Format/Format.h"
12#include "llvm/Support/Debug.h"
13#include "gtest/gtest.h"
14
Chandler Carruth10346662014-04-22 03:17:02 +000015#define DEBUG_TYPE "format-test"
16
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000017namespace clang {
18namespace format {
19
20class FormatTestJS : public ::testing::Test {
21protected:
22 static std::string format(llvm::StringRef Code, unsigned Offset,
23 unsigned Length, const FormatStyle &Style) {
24 DEBUG(llvm::errs() << "---\n");
25 DEBUG(llvm::errs() << Code << "\n\n");
26 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27 tooling::Replacements Replaces = reformat(Style, Code, Ranges);
28 std::string Result = applyAllReplacements(Code, Replaces);
29 EXPECT_NE("", Result);
30 DEBUG(llvm::errs() << "\n" << Result << "\n\n");
31 return Result;
32 }
33
Daniel Jasper069e5f42014-05-20 11:14:57 +000034 static std::string format(
35 llvm::StringRef Code,
36 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000037 return format(Code, 0, Code.size(), Style);
38 }
39
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000040 static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
Nico Weber514ecc82014-02-02 20:50:45 +000041 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000042 Style.ColumnLimit = ColumnLimit;
43 return Style;
44 }
45
Nico Weber514ecc82014-02-02 20:50:45 +000046 static void verifyFormat(
47 llvm::StringRef Code,
48 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000049 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
50 }
51};
52
53TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
54 verifyFormat("a == = b;");
55 verifyFormat("a != = b;");
56
57 verifyFormat("a === b;");
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000058 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000059 verifyFormat("a !== b;");
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000060 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000061 verifyFormat("if (a + b + c +\n"
62 " d !==\n"
63 " e + f + g)\n"
64 " q();",
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000065 getGoogleJSStyleWithColumns(20));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000066
67 verifyFormat("a >> >= b;");
68
69 verifyFormat("a >>> b;");
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000070 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000071 verifyFormat("a >>>= b;");
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000072 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000073 verifyFormat("if (a + b + c +\n"
74 " d >>>\n"
75 " e + f + g)\n"
76 " q();",
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000077 getGoogleJSStyleWithColumns(20));
78 verifyFormat("var x = aaaaaaaaaa ?\n"
79 " bbbbbb :\n"
80 " ccc;",
81 getGoogleJSStyleWithColumns(20));
Daniel Jasper78214392014-05-19 07:27:02 +000082
83 verifyFormat("var b = a.map((x) => x + 1);");
Daniel Jasper3549ea12014-09-19 10:48:15 +000084 verifyFormat("return ('aaa') in bbbb;");
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000085}
86
Daniel Jasper3a038de2014-09-05 08:53:45 +000087TEST_F(FormatTestJS, UnderstandsAmpAmp) {
88 verifyFormat("e && e.SomeFunction();");
89}
90
Daniel Jasper4db69bd2014-09-04 18:23:42 +000091TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
92 verifyFormat("not.and.or.not_eq = 1;");
93}
94
Daniel Jasper0dd52912014-05-19 07:37:07 +000095TEST_F(FormatTestJS, ES6DestructuringAssignment) {
96 verifyFormat("var [a, b, c] = [1, 2, 3];");
97 verifyFormat("var {a, b} = {a: 1, b: 2};");
98}
99
Daniel Jasper17062ff2014-06-10 14:44:02 +0000100TEST_F(FormatTestJS, ContainerLiterals) {
101 verifyFormat("return {\n"
102 " link: function() {\n"
103 " f(); //\n"
104 " }\n"
105 "};");
106 verifyFormat("return {\n"
107 " a: a,\n"
108 " link: function() {\n"
109 " f(); //\n"
110 " }\n"
111 "};");
112 verifyFormat("return {\n"
113 " a: a,\n"
Daniel Jasper90ebc982014-09-05 09:27:38 +0000114 " link: function() {\n"
115 " f(); //\n"
116 " },\n"
117 " link: function() {\n"
118 " f(); //\n"
119 " }\n"
Daniel Jasper17062ff2014-06-10 14:44:02 +0000120 "};");
Daniel Jasper94e11d02014-09-04 14:58:30 +0000121 verifyFormat("var stuff = {\n"
122 " // comment for update\n"
123 " update: false,\n"
124 " // comment for modules\n"
125 " modules: false,\n"
126 " // comment for tasks\n"
127 " tasks: false\n"
128 "};");
Daniel Jasper97bfb7b2014-09-05 08:29:31 +0000129 verifyFormat("return {\n"
130 " 'finish':\n"
131 " //\n"
132 " a\n"
133 "};");
Daniel Jasper17062ff2014-06-10 14:44:02 +0000134}
135
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000136TEST_F(FormatTestJS, SpacesInContainerLiterals) {
137 verifyFormat("var arr = [1, 2, 3];");
138 verifyFormat("var obj = {a: 1, b: 2, c: 3};");
Nico Weber514ecc82014-02-02 20:50:45 +0000139
Daniel Jasper2a958322014-05-21 13:26:58 +0000140 verifyFormat("var object_literal_with_long_name = {\n"
141 " a: 'aaaaaaaaaaaaaaaaaa',\n"
142 " b: 'bbbbbbbbbbbbbbbbbb'\n"
143 "};");
144
Nico Weber514ecc82014-02-02 20:50:45 +0000145 verifyFormat("var obj = {a: 1, b: 2, c: 3};",
146 getChromiumStyle(FormatStyle::LK_JavaScript));
Daniel Jasper89519082014-05-09 10:26:08 +0000147 verifyFormat("someVariable = {'a': [{}]};");
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000148}
149
Daniel Jasper86fee2f2014-01-31 12:49:42 +0000150TEST_F(FormatTestJS, SingleQuoteStrings) {
151 verifyFormat("this.function('', true);");
152}
153
Daniel Jasper4a39c842014-05-06 13:54:10 +0000154TEST_F(FormatTestJS, GoogScopes) {
155 verifyFormat("goog.scope(function() {\n"
156 "var x = a.b;\n"
157 "var y = c.d;\n"
158 "}); // goog.scope");
159}
160
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000161TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
162 verifyFormat("function outer1(a, b) {\n"
163 " function inner1(a, b) { return a; }\n"
164 " inner1(a, b);\n"
165 "}\n"
166 "function outer2(a, b) {\n"
167 " function inner2(a, b) { return a; }\n"
168 " inner2(a, b);\n"
169 "}");
170}
171
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000172TEST_F(FormatTestJS, FunctionLiterals) {
Daniel Jasper3f69ba12014-09-05 08:42:27 +0000173 verifyFormat("doFoo(function() {});");
Daniel Jasper79dffb42014-05-07 09:48:30 +0000174 verifyFormat("doFoo(function() { return 1; });");
175 verifyFormat("var func = function() { return 1; };");
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000176 verifyFormat("return {\n"
177 " body: {\n"
178 " setAttribute: function(key, val) { this[key] = val; },\n"
179 " getAttribute: function(key) { return this[key]; },\n"
180 " style: {direction: ''}\n"
181 " }\n"
182 "};");
Daniel Jasper069e5f42014-05-20 11:14:57 +0000183 EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };",
184 format("abc=xyz?function(){return 1;}:function(){return -1;};"));
Daniel Jasperb16b9692014-05-21 12:51:23 +0000185
186 verifyFormat("var closure = goog.bind(\n"
187 " function() { // comment\n"
188 " foo();\n"
189 " bar();\n"
190 " },\n"
191 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
192 " arg3IsReallyLongAndNeeedsLineBreaks);");
193 verifyFormat("var closure = goog.bind(function() { // comment\n"
194 " foo();\n"
195 " bar();\n"
196 "}, this);");
Daniel Jasper58cb2ed2014-06-06 13:49:04 +0000197 verifyFormat("return {\n"
198 " a: 'E',\n"
199 " b: function() {\n"
200 " return function() {\n"
201 " f(); //\n"
202 " };\n"
203 " }\n"
204 "};");
Daniel Jasper5f3ea472014-05-22 08:36:53 +0000205
206 verifyFormat("var x = {a: function() { return 1; }};",
207 getGoogleJSStyleWithColumns(38));
208 verifyFormat("var x = {\n"
209 " a: function() { return 1; }\n"
210 "};",
211 getGoogleJSStyleWithColumns(37));
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000212
213 verifyFormat("return {\n"
214 " a: function SomeFunction() {\n"
215 " // ...\n"
216 " return 1;\n"
217 " }\n"
218 "};");
Daniel Jasper79dffb42014-05-07 09:48:30 +0000219}
220
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000221TEST_F(FormatTestJS, MultipleFunctionLiterals) {
222 verifyFormat("promise.then(\n"
223 " function success() {\n"
224 " doFoo();\n"
225 " doBar();\n"
226 " },\n"
227 " function error() {\n"
228 " doFoo();\n"
229 " doBaz();\n"
230 " },\n"
231 " []);\n");
232 verifyFormat("promise.then(\n"
233 " function success() {\n"
234 " doFoo();\n"
235 " doBar();\n"
236 " },\n"
237 " [],\n"
238 " function error() {\n"
239 " doFoo();\n"
240 " doBaz();\n"
241 " });\n");
242 // FIXME: Here, we should probably break right after the "(" for consistency.
243 verifyFormat("promise.then([],\n"
244 " function success() {\n"
245 " doFoo();\n"
246 " doBar();\n"
247 " },\n"
248 " function error() {\n"
249 " doFoo();\n"
250 " doBaz();\n"
251 " });\n");
252}
253
Daniel Jasper166c19b2014-05-06 14:12:21 +0000254TEST_F(FormatTestJS, ReturnStatements) {
255 verifyFormat("function() { return [hello, world]; }");
256}
257
Daniel Jasper484033b2014-05-06 14:41:29 +0000258TEST_F(FormatTestJS, ClosureStyleComments) {
259 verifyFormat("var x = /** @type {foo} */ (bar);");
260}
261
Daniel Jasper04a71a42014-05-08 11:58:24 +0000262TEST_F(FormatTestJS, TryCatch) {
263 verifyFormat("try {\n"
264 " f();\n"
265 "} catch (e) {\n"
266 " g();\n"
267 "} finally {\n"
268 " h();\n"
269 "}");
Daniel Jasper8f2e94c2014-09-04 15:03:34 +0000270
271 // But, of course, "catch" is a perfectly fine function name in JavaScript.
272 verifyFormat("someObject.catch();");
Daniel Jasper04a71a42014-05-08 11:58:24 +0000273}
274
Daniel Jasper49802ef2014-05-22 09:10:04 +0000275TEST_F(FormatTestJS, StringLiteralConcatenation) {
276 verifyFormat("var literal = 'hello ' +\n"
277 " 'world';");
278}
279
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000280TEST_F(FormatTestJS, RegexLiteralClassification) {
281 // Regex literals.
282 verifyFormat("var regex = /abc/;");
283 verifyFormat("f(/abc/);");
284 verifyFormat("f(abc, /abc/);");
285 verifyFormat("some_map[/abc/];");
286 verifyFormat("var x = a ? /abc/ : /abc/;");
287 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
288 verifyFormat("var x = !/abc/.test(y);");
289 verifyFormat("var x = a && /abc/.test(y);");
290 verifyFormat("var x = a || /abc/.test(y);");
291 verifyFormat("var x = a + /abc/.search(y);");
Daniel Jasperf7405c12014-05-08 07:45:18 +0000292 verifyFormat("var regexs = {/abc/, /abc/};");
293 verifyFormat("return /abc/;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000294
295 // Not regex literals.
296 verifyFormat("var a = a / 2 + b / 3;");
297}
298
299TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
300 verifyFormat("var regex = /a*/;");
301 verifyFormat("var regex = /a+/;");
302 verifyFormat("var regex = /a?/;");
303 verifyFormat("var regex = /.a./;");
304 verifyFormat("var regex = /a\\*/;");
305 verifyFormat("var regex = /^a$/;");
306 verifyFormat("var regex = /\\/a/;");
307 verifyFormat("var regex = /(?:x)/;");
308 verifyFormat("var regex = /x(?=y)/;");
309 verifyFormat("var regex = /x(?!y)/;");
310 verifyFormat("var regex = /x|y/;");
311 verifyFormat("var regex = /a{2}/;");
312 verifyFormat("var regex = /a{1,3}/;");
313 verifyFormat("var regex = /[abc]/;");
314 verifyFormat("var regex = /[^abc]/;");
315 verifyFormat("var regex = /[\\b]/;");
316 verifyFormat("var regex = /\\b/;");
317 verifyFormat("var regex = /\\B/;");
318 verifyFormat("var regex = /\\d/;");
319 verifyFormat("var regex = /\\D/;");
320 verifyFormat("var regex = /\\f/;");
321 verifyFormat("var regex = /\\n/;");
322 verifyFormat("var regex = /\\r/;");
323 verifyFormat("var regex = /\\s/;");
324 verifyFormat("var regex = /\\S/;");
325 verifyFormat("var regex = /\\t/;");
326 verifyFormat("var regex = /\\v/;");
327 verifyFormat("var regex = /\\w/;");
328 verifyFormat("var regex = /\\W/;");
329 verifyFormat("var regex = /a(a)\\1/;");
330 verifyFormat("var regex = /\\0/;");
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000331 verifyFormat("var regex = /\\\\/g;");
332 verifyFormat("var regex = /\\a\\\\/g;");
333 verifyFormat("var regex = /\a\\//g;");
Daniel Jasper23376252014-09-09 14:37:39 +0000334 verifyFormat("var regex = /a\\//;\n"
335 "var x = 0;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000336}
337
338TEST_F(FormatTestJS, RegexLiteralModifiers) {
339 verifyFormat("var regex = /abc/g;");
340 verifyFormat("var regex = /abc/i;");
341 verifyFormat("var regex = /abc/m;");
342 verifyFormat("var regex = /abc/y;");
343}
344
345TEST_F(FormatTestJS, RegexLiteralLength) {
346 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
347 getGoogleJSStyleWithColumns(60));
348 verifyFormat("var regex =\n"
349 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
350 getGoogleJSStyleWithColumns(60));
351}
352
353TEST_F(FormatTestJS, RegexLiteralExamples) {
354 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
355}
356
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000357} // end namespace tooling
358} // end namespace clang