blob: 6ee150c6624f72cba58704404dd65cc85691d862 [file] [log] [blame]
Stephen Hines651f13c2014-04-23 16:59:28 -07001//===- 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
Stephen Hines651f13c2014-04-23 16:59:28 -070010#include "FormatTestUtils.h"
11#include "clang/Format/Format.h"
12#include "llvm/Support/Debug.h"
13#include "gtest/gtest.h"
14
Stephen Hines6bcf27b2014-05-29 04:14:42 -070015#define DEBUG_TYPE "format-test"
16
Stephen Hines651f13c2014-04-23 16:59:28 -070017namespace 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
Stephen Hines6bcf27b2014-05-29 04:14:42 -070034 static std::string format(
35 llvm::StringRef Code,
36 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
Stephen Hines651f13c2014-04-23 16:59:28 -070037 return format(Code, 0, Code.size(), Style);
38 }
39
40 static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
41 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
42 Style.ColumnLimit = ColumnLimit;
43 return Style;
44 }
45
46 static void verifyFormat(
47 llvm::StringRef Code,
48 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
49 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;");
58 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10));
59 verifyFormat("a !== b;");
60 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10));
61 verifyFormat("if (a + b + c +\n"
62 " d !==\n"
63 " e + f + g)\n"
64 " q();",
65 getGoogleJSStyleWithColumns(20));
66
67 verifyFormat("a >> >= b;");
68
69 verifyFormat("a >>> b;");
70 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10));
71 verifyFormat("a >>>= b;");
72 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10));
73 verifyFormat("if (a + b + c +\n"
74 " d >>>\n"
75 " e + f + g)\n"
76 " q();",
77 getGoogleJSStyleWithColumns(20));
78 verifyFormat("var x = aaaaaaaaaa ?\n"
79 " bbbbbb :\n"
80 " ccc;",
81 getGoogleJSStyleWithColumns(20));
Stephen Hines6bcf27b2014-05-29 04:14:42 -070082
83 verifyFormat("var b = a.map((x) => x + 1);");
Stephen Hines176edba2014-12-01 14:53:08 -080084 verifyFormat("return ('aaa') in bbbb;");
85}
86
87TEST_F(FormatTestJS, UnderstandsAmpAmp) {
88 verifyFormat("e && e.SomeFunction();");
89}
90
91TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
92 verifyFormat("not.and.or.not_eq = 1;");
Stephen Hines6bcf27b2014-05-29 04:14:42 -070093}
94
95TEST_F(FormatTestJS, ES6DestructuringAssignment) {
96 verifyFormat("var [a, b, c] = [1, 2, 3];");
97 verifyFormat("var {a, b} = {a: 1, b: 2};");
Stephen Hines651f13c2014-04-23 16:59:28 -070098}
99
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700100TEST_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"
Stephen Hines176edba2014-12-01 14:53:08 -0800114 " link: function() {\n"
115 " f(); //\n"
116 " },\n"
117 " link: function() {\n"
118 " f(); //\n"
119 " }\n"
120 "};");
121 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 "};");
129 verifyFormat("return {\n"
130 " 'finish':\n"
131 " //\n"
132 " a\n"
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700133 "};");
134}
135
Stephen Hines651f13c2014-04-23 16:59:28 -0700136TEST_F(FormatTestJS, SpacesInContainerLiterals) {
137 verifyFormat("var arr = [1, 2, 3];");
138 verifyFormat("var obj = {a: 1, b: 2, c: 3};");
139
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700140 verifyFormat("var object_literal_with_long_name = {\n"
141 " a: 'aaaaaaaaaaaaaaaaaa',\n"
142 " b: 'bbbbbbbbbbbbbbbbbb'\n"
143 "};");
144
Stephen Hines651f13c2014-04-23 16:59:28 -0700145 verifyFormat("var obj = {a: 1, b: 2, c: 3};",
146 getChromiumStyle(FormatStyle::LK_JavaScript));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700147 verifyFormat("someVariable = {'a': [{}]};");
Stephen Hines651f13c2014-04-23 16:59:28 -0700148}
149
150TEST_F(FormatTestJS, SingleQuoteStrings) {
151 verifyFormat("this.function('', true);");
152}
153
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700154TEST_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
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700161TEST_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
172TEST_F(FormatTestJS, FunctionLiterals) {
Stephen Hines176edba2014-12-01 14:53:08 -0800173 verifyFormat("doFoo(function() {});");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700174 verifyFormat("doFoo(function() { return 1; });");
Stephen Hines176edba2014-12-01 14:53:08 -0800175 verifyFormat("var func = function() {\n"
176 " return 1;\n"
177 "};");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700178 verifyFormat("return {\n"
179 " body: {\n"
180 " setAttribute: function(key, val) { this[key] = val; },\n"
181 " getAttribute: function(key) { return this[key]; },\n"
182 " style: {direction: ''}\n"
183 " }\n"
184 "};");
Stephen Hines176edba2014-12-01 14:53:08 -0800185 // FIXME: The formatting here probably isn't ideal.
186 EXPECT_EQ("abc = xyz ?\n"
187 " function() {\n"
188 " return 1;\n"
189 " } :\n"
190 " function() {\n"
191 " return -1;\n"
192 "};",
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700193 format("abc=xyz?function(){return 1;}:function(){return -1;};"));
194
195 verifyFormat("var closure = goog.bind(\n"
196 " function() { // comment\n"
197 " foo();\n"
198 " bar();\n"
199 " },\n"
200 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
201 " arg3IsReallyLongAndNeeedsLineBreaks);");
202 verifyFormat("var closure = goog.bind(function() { // comment\n"
203 " foo();\n"
204 " bar();\n"
205 "}, this);");
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700206 verifyFormat("return {\n"
207 " a: 'E',\n"
208 " b: function() {\n"
209 " return function() {\n"
210 " f(); //\n"
211 " };\n"
212 " }\n"
213 "};");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700214
215 verifyFormat("var x = {a: function() { return 1; }};",
216 getGoogleJSStyleWithColumns(38));
217 verifyFormat("var x = {\n"
218 " a: function() { return 1; }\n"
219 "};",
220 getGoogleJSStyleWithColumns(37));
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700221
222 verifyFormat("return {\n"
223 " a: function SomeFunction() {\n"
224 " // ...\n"
225 " return 1;\n"
226 " }\n"
227 "};");
228}
229
Stephen Hines176edba2014-12-01 14:53:08 -0800230TEST_F(FormatTestJS, InliningFunctionLiterals) {
231 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
232 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
233 verifyFormat("var func = function() {\n"
234 " return 1;\n"
235 "};",
236 Style);
237 verifyFormat("var func = doSomething(function() { return 1; });", Style);
238 verifyFormat("var outer = function() {\n"
239 " var inner = function() { return 1; }\n"
240 "};",
241 Style);
242 verifyFormat("function outer1(a, b) {\n"
243 " function inner1(a, b) { return a; }\n"
244 "}",
245 Style);
246
247 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
248 verifyFormat("var func = function() { return 1; };", Style);
249 verifyFormat("var func = doSomething(function() { return 1; });", Style);
250 verifyFormat(
251 "var outer = function() { var inner = function() { return 1; } };",
252 Style);
253 verifyFormat("function outer1(a, b) {\n"
254 " function inner1(a, b) { return a; }\n"
255 "}",
256 Style);
257
258 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
259 verifyFormat("var func = function() {\n"
260 " return 1;\n"
261 "};",
262 Style);
263 verifyFormat("var func = doSomething(function() {\n"
264 " return 1;\n"
265 "});",
266 Style);
267 verifyFormat("var outer = function() {\n"
268 " var inner = function() {\n"
269 " return 1;\n"
270 " }\n"
271 "};",
272 Style);
273 verifyFormat("function outer1(a, b) {\n"
274 " function inner1(a, b) {\n"
275 " return a;\n"
276 " }\n"
277 "}",
278 Style);
279}
280
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700281TEST_F(FormatTestJS, MultipleFunctionLiterals) {
282 verifyFormat("promise.then(\n"
283 " function success() {\n"
284 " doFoo();\n"
285 " doBar();\n"
286 " },\n"
287 " function error() {\n"
288 " doFoo();\n"
289 " doBaz();\n"
290 " },\n"
291 " []);\n");
292 verifyFormat("promise.then(\n"
293 " function success() {\n"
294 " doFoo();\n"
295 " doBar();\n"
296 " },\n"
297 " [],\n"
298 " function error() {\n"
299 " doFoo();\n"
300 " doBaz();\n"
301 " });\n");
302 // FIXME: Here, we should probably break right after the "(" for consistency.
303 verifyFormat("promise.then([],\n"
304 " function success() {\n"
305 " doFoo();\n"
306 " doBar();\n"
307 " },\n"
308 " function error() {\n"
309 " doFoo();\n"
310 " doBaz();\n"
311 " });\n");
Stephen Hines176edba2014-12-01 14:53:08 -0800312
313 verifyFormat("getSomeLongPromise()\n"
314 " .then(function(value) { body(); })\n"
315 " .thenCatch(function(error) { body(); });");
316 verifyFormat("getSomeLongPromise()\n"
317 " .then(function(value) {\n"
318 " body();\n"
319 " body();\n"
320 " })\n"
321 " .thenCatch(function(error) {\n"
322 " body();\n"
323 " body();\n"
324 " });");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700325}
326
327TEST_F(FormatTestJS, ReturnStatements) {
Stephen Hines176edba2014-12-01 14:53:08 -0800328 verifyFormat("function() {\n"
329 " return [hello, world];\n"
330 "}");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700331}
332
333TEST_F(FormatTestJS, ClosureStyleComments) {
334 verifyFormat("var x = /** @type {foo} */ (bar);");
335}
336
337TEST_F(FormatTestJS, TryCatch) {
338 verifyFormat("try {\n"
339 " f();\n"
340 "} catch (e) {\n"
341 " g();\n"
342 "} finally {\n"
343 " h();\n"
344 "}");
Stephen Hines176edba2014-12-01 14:53:08 -0800345
346 // But, of course, "catch" is a perfectly fine function name in JavaScript.
347 verifyFormat("someObject.catch();");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700348}
349
350TEST_F(FormatTestJS, StringLiteralConcatenation) {
351 verifyFormat("var literal = 'hello ' +\n"
352 " 'world';");
353}
354
355TEST_F(FormatTestJS, RegexLiteralClassification) {
356 // Regex literals.
357 verifyFormat("var regex = /abc/;");
358 verifyFormat("f(/abc/);");
359 verifyFormat("f(abc, /abc/);");
360 verifyFormat("some_map[/abc/];");
361 verifyFormat("var x = a ? /abc/ : /abc/;");
362 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
363 verifyFormat("var x = !/abc/.test(y);");
364 verifyFormat("var x = a && /abc/.test(y);");
365 verifyFormat("var x = a || /abc/.test(y);");
366 verifyFormat("var x = a + /abc/.search(y);");
367 verifyFormat("var regexs = {/abc/, /abc/};");
368 verifyFormat("return /abc/;");
369
370 // Not regex literals.
371 verifyFormat("var a = a / 2 + b / 3;");
372}
373
374TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
375 verifyFormat("var regex = /a*/;");
376 verifyFormat("var regex = /a+/;");
377 verifyFormat("var regex = /a?/;");
378 verifyFormat("var regex = /.a./;");
379 verifyFormat("var regex = /a\\*/;");
380 verifyFormat("var regex = /^a$/;");
381 verifyFormat("var regex = /\\/a/;");
382 verifyFormat("var regex = /(?:x)/;");
383 verifyFormat("var regex = /x(?=y)/;");
384 verifyFormat("var regex = /x(?!y)/;");
385 verifyFormat("var regex = /x|y/;");
386 verifyFormat("var regex = /a{2}/;");
387 verifyFormat("var regex = /a{1,3}/;");
388 verifyFormat("var regex = /[abc]/;");
389 verifyFormat("var regex = /[^abc]/;");
390 verifyFormat("var regex = /[\\b]/;");
391 verifyFormat("var regex = /\\b/;");
392 verifyFormat("var regex = /\\B/;");
393 verifyFormat("var regex = /\\d/;");
394 verifyFormat("var regex = /\\D/;");
395 verifyFormat("var regex = /\\f/;");
396 verifyFormat("var regex = /\\n/;");
397 verifyFormat("var regex = /\\r/;");
398 verifyFormat("var regex = /\\s/;");
399 verifyFormat("var regex = /\\S/;");
400 verifyFormat("var regex = /\\t/;");
401 verifyFormat("var regex = /\\v/;");
402 verifyFormat("var regex = /\\w/;");
403 verifyFormat("var regex = /\\W/;");
404 verifyFormat("var regex = /a(a)\\1/;");
405 verifyFormat("var regex = /\\0/;");
406 verifyFormat("var regex = /\\\\/g;");
407 verifyFormat("var regex = /\\a\\\\/g;");
408 verifyFormat("var regex = /\a\\//g;");
Stephen Hines176edba2014-12-01 14:53:08 -0800409 verifyFormat("var regex = /a\\//;\n"
410 "var x = 0;");
411 EXPECT_EQ("var regex = /\\/*/;\n"
412 "var x = 0;",
413 format("var regex = /\\/*/;\n"
414 "var x=0;"));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700415}
416
417TEST_F(FormatTestJS, RegexLiteralModifiers) {
418 verifyFormat("var regex = /abc/g;");
419 verifyFormat("var regex = /abc/i;");
420 verifyFormat("var regex = /abc/m;");
421 verifyFormat("var regex = /abc/y;");
422}
423
424TEST_F(FormatTestJS, RegexLiteralLength) {
425 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
426 getGoogleJSStyleWithColumns(60));
427 verifyFormat("var regex =\n"
428 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
429 getGoogleJSStyleWithColumns(60));
430}
431
432TEST_F(FormatTestJS, RegexLiteralExamples) {
433 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
434}
435
Stephen Hines651f13c2014-04-23 16:59:28 -0700436} // end namespace tooling
437} // end namespace clang