blob: a4bd84a3242a65f9c46abf810d374eae098a9d07 [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);");
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000084}
85
Daniel Jasper4db69bd2014-09-04 18:23:42 +000086TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
87 verifyFormat("not.and.or.not_eq = 1;");
88}
89
Daniel Jasper0dd52912014-05-19 07:37:07 +000090TEST_F(FormatTestJS, ES6DestructuringAssignment) {
91 verifyFormat("var [a, b, c] = [1, 2, 3];");
92 verifyFormat("var {a, b} = {a: 1, b: 2};");
93}
94
Daniel Jasper17062ff2014-06-10 14:44:02 +000095TEST_F(FormatTestJS, ContainerLiterals) {
96 verifyFormat("return {\n"
97 " link: function() {\n"
98 " f(); //\n"
99 " }\n"
100 "};");
101 verifyFormat("return {\n"
102 " a: a,\n"
103 " link: function() {\n"
104 " f(); //\n"
105 " }\n"
106 "};");
107 verifyFormat("return {\n"
108 " a: a,\n"
109 " link:\n"
110 " function() {\n"
111 " f(); //\n"
112 " },\n"
113 " link:\n"
114 " function() {\n"
115 " f(); //\n"
116 " }\n"
117 "};");
Daniel Jasper94e11d02014-09-04 14:58:30 +0000118 verifyFormat("var stuff = {\n"
119 " // comment for update\n"
120 " update: false,\n"
121 " // comment for modules\n"
122 " modules: false,\n"
123 " // comment for tasks\n"
124 " tasks: false\n"
125 "};");
Daniel Jasper97bfb7b2014-09-05 08:29:31 +0000126 verifyFormat("return {\n"
127 " 'finish':\n"
128 " //\n"
129 " a\n"
130 "};");
Daniel Jasper17062ff2014-06-10 14:44:02 +0000131}
132
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000133TEST_F(FormatTestJS, SpacesInContainerLiterals) {
134 verifyFormat("var arr = [1, 2, 3];");
135 verifyFormat("var obj = {a: 1, b: 2, c: 3};");
Nico Weber514ecc82014-02-02 20:50:45 +0000136
Daniel Jasper2a958322014-05-21 13:26:58 +0000137 verifyFormat("var object_literal_with_long_name = {\n"
138 " a: 'aaaaaaaaaaaaaaaaaa',\n"
139 " b: 'bbbbbbbbbbbbbbbbbb'\n"
140 "};");
141
Nico Weber514ecc82014-02-02 20:50:45 +0000142 verifyFormat("var obj = {a: 1, b: 2, c: 3};",
143 getChromiumStyle(FormatStyle::LK_JavaScript));
Daniel Jasper89519082014-05-09 10:26:08 +0000144 verifyFormat("someVariable = {'a': [{}]};");
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000145}
146
Daniel Jasper86fee2f2014-01-31 12:49:42 +0000147TEST_F(FormatTestJS, SingleQuoteStrings) {
148 verifyFormat("this.function('', true);");
149}
150
Daniel Jasper4a39c842014-05-06 13:54:10 +0000151TEST_F(FormatTestJS, GoogScopes) {
152 verifyFormat("goog.scope(function() {\n"
153 "var x = a.b;\n"
154 "var y = c.d;\n"
155 "}); // goog.scope");
156}
157
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000158TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
159 verifyFormat("function outer1(a, b) {\n"
160 " function inner1(a, b) { return a; }\n"
161 " inner1(a, b);\n"
162 "}\n"
163 "function outer2(a, b) {\n"
164 " function inner2(a, b) { return a; }\n"
165 " inner2(a, b);\n"
166 "}");
167}
168
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000169TEST_F(FormatTestJS, FunctionLiterals) {
Daniel Jasper3f69ba12014-09-05 08:42:27 +0000170 verifyFormat("doFoo(function() {});");
Daniel Jasper79dffb42014-05-07 09:48:30 +0000171 verifyFormat("doFoo(function() { return 1; });");
172 verifyFormat("var func = function() { return 1; };");
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000173 verifyFormat("return {\n"
174 " body: {\n"
175 " setAttribute: function(key, val) { this[key] = val; },\n"
176 " getAttribute: function(key) { return this[key]; },\n"
177 " style: {direction: ''}\n"
178 " }\n"
179 "};");
Daniel Jasper069e5f42014-05-20 11:14:57 +0000180 EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };",
181 format("abc=xyz?function(){return 1;}:function(){return -1;};"));
Daniel Jasperb16b9692014-05-21 12:51:23 +0000182
183 verifyFormat("var closure = goog.bind(\n"
184 " function() { // comment\n"
185 " foo();\n"
186 " bar();\n"
187 " },\n"
188 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
189 " arg3IsReallyLongAndNeeedsLineBreaks);");
190 verifyFormat("var closure = goog.bind(function() { // comment\n"
191 " foo();\n"
192 " bar();\n"
193 "}, this);");
Daniel Jasper58cb2ed2014-06-06 13:49:04 +0000194 verifyFormat("return {\n"
195 " a: 'E',\n"
196 " b: function() {\n"
197 " return function() {\n"
198 " f(); //\n"
199 " };\n"
200 " }\n"
201 "};");
Daniel Jasper5f3ea472014-05-22 08:36:53 +0000202
203 verifyFormat("var x = {a: function() { return 1; }};",
204 getGoogleJSStyleWithColumns(38));
205 verifyFormat("var x = {\n"
206 " a: function() { return 1; }\n"
207 "};",
208 getGoogleJSStyleWithColumns(37));
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000209
210 verifyFormat("return {\n"
211 " a: function SomeFunction() {\n"
212 " // ...\n"
213 " return 1;\n"
214 " }\n"
215 "};");
Daniel Jasper79dffb42014-05-07 09:48:30 +0000216}
217
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000218TEST_F(FormatTestJS, MultipleFunctionLiterals) {
219 verifyFormat("promise.then(\n"
220 " function success() {\n"
221 " doFoo();\n"
222 " doBar();\n"
223 " },\n"
224 " function error() {\n"
225 " doFoo();\n"
226 " doBaz();\n"
227 " },\n"
228 " []);\n");
229 verifyFormat("promise.then(\n"
230 " function success() {\n"
231 " doFoo();\n"
232 " doBar();\n"
233 " },\n"
234 " [],\n"
235 " function error() {\n"
236 " doFoo();\n"
237 " doBaz();\n"
238 " });\n");
239 // FIXME: Here, we should probably break right after the "(" for consistency.
240 verifyFormat("promise.then([],\n"
241 " function success() {\n"
242 " doFoo();\n"
243 " doBar();\n"
244 " },\n"
245 " function error() {\n"
246 " doFoo();\n"
247 " doBaz();\n"
248 " });\n");
249}
250
Daniel Jasper166c19b2014-05-06 14:12:21 +0000251TEST_F(FormatTestJS, ReturnStatements) {
252 verifyFormat("function() { return [hello, world]; }");
253}
254
Daniel Jasper484033b2014-05-06 14:41:29 +0000255TEST_F(FormatTestJS, ClosureStyleComments) {
256 verifyFormat("var x = /** @type {foo} */ (bar);");
257}
258
Daniel Jasper04a71a42014-05-08 11:58:24 +0000259TEST_F(FormatTestJS, TryCatch) {
260 verifyFormat("try {\n"
261 " f();\n"
262 "} catch (e) {\n"
263 " g();\n"
264 "} finally {\n"
265 " h();\n"
266 "}");
Daniel Jasper8f2e94c2014-09-04 15:03:34 +0000267
268 // But, of course, "catch" is a perfectly fine function name in JavaScript.
269 verifyFormat("someObject.catch();");
Daniel Jasper04a71a42014-05-08 11:58:24 +0000270}
271
Daniel Jasper49802ef2014-05-22 09:10:04 +0000272TEST_F(FormatTestJS, StringLiteralConcatenation) {
273 verifyFormat("var literal = 'hello ' +\n"
274 " 'world';");
275}
276
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000277TEST_F(FormatTestJS, RegexLiteralClassification) {
278 // Regex literals.
279 verifyFormat("var regex = /abc/;");
280 verifyFormat("f(/abc/);");
281 verifyFormat("f(abc, /abc/);");
282 verifyFormat("some_map[/abc/];");
283 verifyFormat("var x = a ? /abc/ : /abc/;");
284 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
285 verifyFormat("var x = !/abc/.test(y);");
286 verifyFormat("var x = a && /abc/.test(y);");
287 verifyFormat("var x = a || /abc/.test(y);");
288 verifyFormat("var x = a + /abc/.search(y);");
Daniel Jasperf7405c12014-05-08 07:45:18 +0000289 verifyFormat("var regexs = {/abc/, /abc/};");
290 verifyFormat("return /abc/;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000291
292 // Not regex literals.
293 verifyFormat("var a = a / 2 + b / 3;");
294}
295
296TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
297 verifyFormat("var regex = /a*/;");
298 verifyFormat("var regex = /a+/;");
299 verifyFormat("var regex = /a?/;");
300 verifyFormat("var regex = /.a./;");
301 verifyFormat("var regex = /a\\*/;");
302 verifyFormat("var regex = /^a$/;");
303 verifyFormat("var regex = /\\/a/;");
304 verifyFormat("var regex = /(?:x)/;");
305 verifyFormat("var regex = /x(?=y)/;");
306 verifyFormat("var regex = /x(?!y)/;");
307 verifyFormat("var regex = /x|y/;");
308 verifyFormat("var regex = /a{2}/;");
309 verifyFormat("var regex = /a{1,3}/;");
310 verifyFormat("var regex = /[abc]/;");
311 verifyFormat("var regex = /[^abc]/;");
312 verifyFormat("var regex = /[\\b]/;");
313 verifyFormat("var regex = /\\b/;");
314 verifyFormat("var regex = /\\B/;");
315 verifyFormat("var regex = /\\d/;");
316 verifyFormat("var regex = /\\D/;");
317 verifyFormat("var regex = /\\f/;");
318 verifyFormat("var regex = /\\n/;");
319 verifyFormat("var regex = /\\r/;");
320 verifyFormat("var regex = /\\s/;");
321 verifyFormat("var regex = /\\S/;");
322 verifyFormat("var regex = /\\t/;");
323 verifyFormat("var regex = /\\v/;");
324 verifyFormat("var regex = /\\w/;");
325 verifyFormat("var regex = /\\W/;");
326 verifyFormat("var regex = /a(a)\\1/;");
327 verifyFormat("var regex = /\\0/;");
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000328 verifyFormat("var regex = /\\\\/g;");
329 verifyFormat("var regex = /\\a\\\\/g;");
330 verifyFormat("var regex = /\a\\//g;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000331}
332
333TEST_F(FormatTestJS, RegexLiteralModifiers) {
334 verifyFormat("var regex = /abc/g;");
335 verifyFormat("var regex = /abc/i;");
336 verifyFormat("var regex = /abc/m;");
337 verifyFormat("var regex = /abc/y;");
338}
339
340TEST_F(FormatTestJS, RegexLiteralLength) {
341 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
342 getGoogleJSStyleWithColumns(60));
343 verifyFormat("var regex =\n"
344 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
345 getGoogleJSStyleWithColumns(60));
346}
347
348TEST_F(FormatTestJS, RegexLiteralExamples) {
349 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
350}
351
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000352} // end namespace tooling
353} // end namespace clang