blob: 5707024544759f321a707f39e987f97972220579 [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;");
Daniel Jaspere551bb72014-11-05 17:22:31 +000058 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000059 verifyFormat("a !== b;");
Daniel Jaspere551bb72014-11-05 17:22:31 +000060 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10));
61 verifyFormat("if (a + b + c +\n"
62 " d !==\n"
63 " e + f + g)\n"
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000064 " 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;");
Daniel Jaspere551bb72014-11-05 17:22:31 +000070 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000071 verifyFormat("a >>>= b;");
Daniel Jaspere551bb72014-11-05 17:22:31 +000072 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10));
73 verifyFormat("if (a + b + c +\n"
74 " d >>>\n"
75 " e + f + g)\n"
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000076 " q();",
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000077 getGoogleJSStyleWithColumns(20));
Daniel Jaspere551bb72014-11-05 17:22:31 +000078 verifyFormat("var x = aaaaaaaaaa ?\n"
79 " bbbbbb :\n"
80 " ccc;",
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000081 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];");
Daniel Jasper60948b12015-03-15 13:55:54 +000097 verifyFormat("var {a, b} = {a: 1, b: 2};");
Daniel Jasper0dd52912014-05-19 07:37:07 +000098}
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 Jasper40874322014-11-27 15:24:48 +0000134 verifyFormat("var obj = {\n"
135 " fooooooooo: function(x) {\n"
136 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
137 " }\n"
138 "};");
Daniel Jasper60948b12015-03-15 13:55:54 +0000139 // Simple object literal, as opposed to enum style below.
140 verifyFormat("var obj = {a: 123};");
141 // Enum style top level assignment.
142 verifyFormat("X = {\n a: 123\n};");
143 verifyFormat("X.Y = {\n a: 123\n};");
144 verifyFormat("x = foo && {a: 123};");
Daniel Jasper17062ff2014-06-10 14:44:02 +0000145}
146
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000147TEST_F(FormatTestJS, SpacesInContainerLiterals) {
148 verifyFormat("var arr = [1, 2, 3];");
Daniel Jaspera4e55f42014-12-04 16:07:17 +0000149 verifyFormat("f({a: 1, b: 2, c: 3});");
Nico Weber514ecc82014-02-02 20:50:45 +0000150
Daniel Jasper2a958322014-05-21 13:26:58 +0000151 verifyFormat("var object_literal_with_long_name = {\n"
152 " a: 'aaaaaaaaaaaaaaaaaa',\n"
153 " b: 'bbbbbbbbbbbbbbbbbb'\n"
154 "};");
155
Daniel Jaspera4e55f42014-12-04 16:07:17 +0000156 verifyFormat("f({a: 1, b: 2, c: 3});",
Nico Weber514ecc82014-02-02 20:50:45 +0000157 getChromiumStyle(FormatStyle::LK_JavaScript));
Daniel Jaspera4e55f42014-12-04 16:07:17 +0000158 verifyFormat("f({'a': [{}]});");
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000159}
160
Daniel Jasper86fee2f2014-01-31 12:49:42 +0000161TEST_F(FormatTestJS, SingleQuoteStrings) {
162 verifyFormat("this.function('', true);");
163}
164
Daniel Jasper4a39c842014-05-06 13:54:10 +0000165TEST_F(FormatTestJS, GoogScopes) {
166 verifyFormat("goog.scope(function() {\n"
167 "var x = a.b;\n"
168 "var y = c.d;\n"
169 "}); // goog.scope");
170}
171
Daniel Jasper616de8642014-11-23 16:46:28 +0000172TEST_F(FormatTestJS, GoogModules) {
173 verifyFormat("goog.module('this.is.really.absurdly.long');",
174 getGoogleJSStyleWithColumns(40));
175 verifyFormat("goog.require('this.is.really.absurdly.long');",
176 getGoogleJSStyleWithColumns(40));
177 verifyFormat("goog.provide('this.is.really.absurdly.long');",
178 getGoogleJSStyleWithColumns(40));
179 verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
180 getGoogleJSStyleWithColumns(40));
Daniel Jasper53c38f42014-11-27 14:46:03 +0000181
182 // These should be wrapped normally.
183 verifyFormat(
184 "var MyLongClassName =\n"
185 " goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
Daniel Jasper616de8642014-11-23 16:46:28 +0000186}
187
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000188TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
189 verifyFormat("function outer1(a, b) {\n"
190 " function inner1(a, b) { return a; }\n"
191 " inner1(a, b);\n"
192 "}\n"
193 "function outer2(a, b) {\n"
194 " function inner2(a, b) { return a; }\n"
195 " inner2(a, b);\n"
196 "}");
197}
198
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000199TEST_F(FormatTestJS, FunctionLiterals) {
Daniel Jasper3f69ba12014-09-05 08:42:27 +0000200 verifyFormat("doFoo(function() {});");
Daniel Jasper79dffb42014-05-07 09:48:30 +0000201 verifyFormat("doFoo(function() { return 1; });");
Daniel Jasper67f8ad22014-09-30 17:57:06 +0000202 verifyFormat("var func = function() {\n"
203 " return 1;\n"
204 "};");
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000205 verifyFormat("return {\n"
206 " body: {\n"
207 " setAttribute: function(key, val) { this[key] = val; },\n"
208 " getAttribute: function(key) { return this[key]; },\n"
209 " style: {direction: ''}\n"
210 " }\n"
211 "};");
Daniel Jaspere551bb72014-11-05 17:22:31 +0000212 EXPECT_EQ("abc = xyz ?\n"
213 " function() {\n"
214 " return 1;\n"
215 " } :\n"
216 " function() {\n"
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000217 " return -1;\n"
218 " };",
Daniel Jasper069e5f42014-05-20 11:14:57 +0000219 format("abc=xyz?function(){return 1;}:function(){return -1;};"));
Daniel Jasperb16b9692014-05-21 12:51:23 +0000220
221 verifyFormat("var closure = goog.bind(\n"
222 " function() { // comment\n"
223 " foo();\n"
224 " bar();\n"
225 " },\n"
226 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
227 " arg3IsReallyLongAndNeeedsLineBreaks);");
228 verifyFormat("var closure = goog.bind(function() { // comment\n"
229 " foo();\n"
230 " bar();\n"
231 "}, this);");
Daniel Jasper58cb2ed2014-06-06 13:49:04 +0000232 verifyFormat("return {\n"
233 " a: 'E',\n"
234 " b: function() {\n"
235 " return function() {\n"
236 " f(); //\n"
237 " };\n"
238 " }\n"
239 "};");
Daniel Jasper41368e92014-11-27 15:37:42 +0000240 verifyFormat("{\n"
241 " var someVariable = function(x) {\n"
242 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
243 " };\n"
244 "}");
Daniel Jasper5f3ea472014-05-22 08:36:53 +0000245
Daniel Jaspera4e55f42014-12-04 16:07:17 +0000246 verifyFormat("f({a: function() { return 1; }});",
247 getGoogleJSStyleWithColumns(33));
248 verifyFormat("f({\n"
Daniel Jasper5f3ea472014-05-22 08:36:53 +0000249 " a: function() { return 1; }\n"
Daniel Jaspera4e55f42014-12-04 16:07:17 +0000250 "});",
251 getGoogleJSStyleWithColumns(32));
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000252
253 verifyFormat("return {\n"
254 " a: function SomeFunction() {\n"
255 " // ...\n"
256 " return 1;\n"
257 " }\n"
258 "};");
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000259 verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
260 " .then(goog.bind(function(aaaaaaaaaaa) {\n"
261 " someFunction();\n"
262 " someFunction();\n"
263 " }, this), aaaaaaaaaaaaaaaaa);");
264
265 // FIXME: This is not ideal yet.
266 verifyFormat("someFunction(goog.bind(\n"
267 " function() {\n"
268 " doSomething();\n"
269 " doSomething();\n"
270 " },\n"
271 " this),\n"
272 " goog.bind(function() {\n"
273 " doSomething();\n"
274 " doSomething();\n"
275 " }, this));");
Daniel Jasper79dffb42014-05-07 09:48:30 +0000276}
277
Daniel Jasper67f8ad22014-09-30 17:57:06 +0000278TEST_F(FormatTestJS, InliningFunctionLiterals) {
279 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
280 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
281 verifyFormat("var func = function() {\n"
282 " return 1;\n"
283 "};",
284 Style);
285 verifyFormat("var func = doSomething(function() { return 1; });", Style);
286 verifyFormat("var outer = function() {\n"
287 " var inner = function() { return 1; }\n"
288 "};",
289 Style);
290 verifyFormat("function outer1(a, b) {\n"
291 " function inner1(a, b) { return a; }\n"
292 "}",
293 Style);
294
295 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
296 verifyFormat("var func = function() { return 1; };", Style);
297 verifyFormat("var func = doSomething(function() { return 1; });", Style);
298 verifyFormat(
299 "var outer = function() { var inner = function() { return 1; } };",
300 Style);
301 verifyFormat("function outer1(a, b) {\n"
302 " function inner1(a, b) { return a; }\n"
303 "}",
304 Style);
305
306 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
307 verifyFormat("var func = function() {\n"
308 " return 1;\n"
309 "};",
310 Style);
311 verifyFormat("var func = doSomething(function() {\n"
312 " return 1;\n"
313 "});",
314 Style);
315 verifyFormat("var outer = function() {\n"
316 " var inner = function() {\n"
317 " return 1;\n"
318 " }\n"
319 "};",
320 Style);
321 verifyFormat("function outer1(a, b) {\n"
322 " function inner1(a, b) {\n"
323 " return a;\n"
324 " }\n"
325 "}",
326 Style);
327}
328
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000329TEST_F(FormatTestJS, MultipleFunctionLiterals) {
330 verifyFormat("promise.then(\n"
331 " function success() {\n"
332 " doFoo();\n"
333 " doBar();\n"
334 " },\n"
335 " function error() {\n"
336 " doFoo();\n"
337 " doBaz();\n"
338 " },\n"
339 " []);\n");
340 verifyFormat("promise.then(\n"
341 " function success() {\n"
342 " doFoo();\n"
343 " doBar();\n"
344 " },\n"
345 " [],\n"
346 " function error() {\n"
347 " doFoo();\n"
348 " doBaz();\n"
349 " });\n");
350 // FIXME: Here, we should probably break right after the "(" for consistency.
351 verifyFormat("promise.then([],\n"
352 " function success() {\n"
353 " doFoo();\n"
354 " doBar();\n"
355 " },\n"
356 " function error() {\n"
357 " doFoo();\n"
358 " doBaz();\n"
359 " });\n");
Daniel Jasper1779d432014-09-29 07:54:54 +0000360
361 verifyFormat("getSomeLongPromise()\n"
362 " .then(function(value) { body(); })\n"
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000363 " .thenCatch(function(error) {\n"
364 " body();\n"
365 " body();\n"
366 " });");
Daniel Jasper1779d432014-09-29 07:54:54 +0000367 verifyFormat("getSomeLongPromise()\n"
368 " .then(function(value) {\n"
369 " body();\n"
370 " body();\n"
371 " })\n"
372 " .thenCatch(function(error) {\n"
373 " body();\n"
374 " body();\n"
375 " });");
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000376
377 // FIXME: This is bad, but it used to be formatted correctly by accident.
378 verifyFormat("getSomeLongPromise().then(function(value) {\n"
379 " body();\n"
380 "}).thenCatch(function(error) { body(); });");
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000381}
382
Daniel Jasper166c19b2014-05-06 14:12:21 +0000383TEST_F(FormatTestJS, ReturnStatements) {
Daniel Jasper67f8ad22014-09-30 17:57:06 +0000384 verifyFormat("function() {\n"
385 " return [hello, world];\n"
386 "}");
Daniel Jasper166c19b2014-05-06 14:12:21 +0000387}
388
Daniel Jasper484033b2014-05-06 14:41:29 +0000389TEST_F(FormatTestJS, ClosureStyleComments) {
390 verifyFormat("var x = /** @type {foo} */ (bar);");
391}
392
Daniel Jasper04a71a42014-05-08 11:58:24 +0000393TEST_F(FormatTestJS, TryCatch) {
394 verifyFormat("try {\n"
395 " f();\n"
396 "} catch (e) {\n"
397 " g();\n"
398 "} finally {\n"
399 " h();\n"
400 "}");
Daniel Jasper8f2e94c2014-09-04 15:03:34 +0000401
402 // But, of course, "catch" is a perfectly fine function name in JavaScript.
403 verifyFormat("someObject.catch();");
Daniel Jasper79121232014-11-27 14:55:17 +0000404 verifyFormat("someObject.new();");
405 verifyFormat("someObject.delete();");
Daniel Jasper04a71a42014-05-08 11:58:24 +0000406}
407
Daniel Jasper49802ef2014-05-22 09:10:04 +0000408TEST_F(FormatTestJS, StringLiteralConcatenation) {
409 verifyFormat("var literal = 'hello ' +\n"
410 " 'world';");
411}
412
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000413TEST_F(FormatTestJS, RegexLiteralClassification) {
414 // Regex literals.
415 verifyFormat("var regex = /abc/;");
416 verifyFormat("f(/abc/);");
417 verifyFormat("f(abc, /abc/);");
418 verifyFormat("some_map[/abc/];");
419 verifyFormat("var x = a ? /abc/ : /abc/;");
420 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
421 verifyFormat("var x = !/abc/.test(y);");
422 verifyFormat("var x = a && /abc/.test(y);");
423 verifyFormat("var x = a || /abc/.test(y);");
424 verifyFormat("var x = a + /abc/.search(y);");
Daniel Jasperf7405c12014-05-08 07:45:18 +0000425 verifyFormat("var regexs = {/abc/, /abc/};");
426 verifyFormat("return /abc/;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000427
428 // Not regex literals.
429 verifyFormat("var a = a / 2 + b / 3;");
430}
431
432TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
433 verifyFormat("var regex = /a*/;");
434 verifyFormat("var regex = /a+/;");
435 verifyFormat("var regex = /a?/;");
436 verifyFormat("var regex = /.a./;");
437 verifyFormat("var regex = /a\\*/;");
438 verifyFormat("var regex = /^a$/;");
439 verifyFormat("var regex = /\\/a/;");
440 verifyFormat("var regex = /(?:x)/;");
441 verifyFormat("var regex = /x(?=y)/;");
442 verifyFormat("var regex = /x(?!y)/;");
443 verifyFormat("var regex = /x|y/;");
444 verifyFormat("var regex = /a{2}/;");
445 verifyFormat("var regex = /a{1,3}/;");
446 verifyFormat("var regex = /[abc]/;");
447 verifyFormat("var regex = /[^abc]/;");
448 verifyFormat("var regex = /[\\b]/;");
449 verifyFormat("var regex = /\\b/;");
450 verifyFormat("var regex = /\\B/;");
451 verifyFormat("var regex = /\\d/;");
452 verifyFormat("var regex = /\\D/;");
453 verifyFormat("var regex = /\\f/;");
454 verifyFormat("var regex = /\\n/;");
455 verifyFormat("var regex = /\\r/;");
456 verifyFormat("var regex = /\\s/;");
457 verifyFormat("var regex = /\\S/;");
458 verifyFormat("var regex = /\\t/;");
459 verifyFormat("var regex = /\\v/;");
460 verifyFormat("var regex = /\\w/;");
461 verifyFormat("var regex = /\\W/;");
462 verifyFormat("var regex = /a(a)\\1/;");
463 verifyFormat("var regex = /\\0/;");
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000464 verifyFormat("var regex = /\\\\/g;");
465 verifyFormat("var regex = /\\a\\\\/g;");
466 verifyFormat("var regex = /\a\\//g;");
Daniel Jasper23376252014-09-09 14:37:39 +0000467 verifyFormat("var regex = /a\\//;\n"
468 "var x = 0;");
Daniel Jasper49a9a282014-10-29 16:51:38 +0000469 EXPECT_EQ("var regex = /\\/*/;\n"
470 "var x = 0;",
471 format("var regex = /\\/*/;\n"
472 "var x=0;"));
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000473}
474
475TEST_F(FormatTestJS, RegexLiteralModifiers) {
476 verifyFormat("var regex = /abc/g;");
477 verifyFormat("var regex = /abc/i;");
478 verifyFormat("var regex = /abc/m;");
479 verifyFormat("var regex = /abc/y;");
480}
481
482TEST_F(FormatTestJS, RegexLiteralLength) {
483 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
484 getGoogleJSStyleWithColumns(60));
485 verifyFormat("var regex =\n"
486 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
487 getGoogleJSStyleWithColumns(60));
Daniel Jasper0580ff02014-12-17 09:11:08 +0000488 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
489 getGoogleJSStyleWithColumns(50));
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000490}
491
492TEST_F(FormatTestJS, RegexLiteralExamples) {
493 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
494}
495
Daniel Jasperb10bdff2015-02-18 17:09:53 +0000496TEST_F(FormatTestJS, TypeAnnotations) {
497 verifyFormat("var x: string;");
498 verifyFormat("function x(): string {\n return 'x';\n}");
499 verifyFormat("function x(y: string): string {\n return 'x';\n}");
500 verifyFormat("for (var y: string in x) {\n x();\n}");
501 verifyFormat("((a: string, b: number): string => a + b);");
502 verifyFormat("var x: (y: number) => string;");
503 verifyFormat("var x: P<string, (a: number) => string>;");
504}
505
Daniel Jasper83709082015-02-18 17:14:05 +0000506TEST_F(FormatTestJS, ClassDeclarations) {
507 verifyFormat("class C {\n x: string = 12;\n}");
508 verifyFormat("class C {\n x(): string => 12;\n}");
509 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}");
510 verifyFormat("class C {\n private x: string = 12;\n}");
511 verifyFormat("class C {\n private static x: string = 12;\n}");
512 verifyFormat("class C {\n static x(): string { return 'asd'; }\n}");
513 verifyFormat("class C extends P implements I {}");
514}
515
Daniel Jasper3c42dba2015-02-18 17:17:15 +0000516TEST_F(FormatTestJS, MetadataAnnotations) {
517 verifyFormat("@A\nclass C {\n}");
518 verifyFormat("@A({arg: 'value'})\nclass C {\n}");
519 verifyFormat("@A\n@B\nclass C {\n}");
520 verifyFormat("class C {\n @A x: string;\n}");
521 verifyFormat("class C {\n"
522 " @A\n"
523 " private x(): string {\n"
524 " return 'y';\n"
525 " }\n"
526 "}");
Daniel Jasper6fa9ec72015-02-19 16:03:16 +0000527 verifyFormat("class X {}\n"
528 "class Y {}");
Daniel Jasper3c42dba2015-02-18 17:17:15 +0000529}
530
Daniel Jasper354aa512015-02-19 16:07:32 +0000531TEST_F(FormatTestJS, Modules) {
532 verifyFormat("import SomeThing from 'some/module.js';");
533 verifyFormat("import {X, Y} from 'some/module.js';");
534 verifyFormat("import {\n"
535 " VeryLongImportsAreAnnoying,\n"
536 " VeryLongImportsAreAnnoying,\n"
537 " VeryLongImportsAreAnnoying,\n"
538 " VeryLongImportsAreAnnoying\n"
539 "} from 'some/module.js';");
540 verifyFormat("import {\n"
541 " X,\n"
542 " Y,\n"
543 "} from 'some/module.js';");
544 verifyFormat("import {\n"
545 " X,\n"
546 " Y,\n"
547 "} from 'some/long/module.js';",
548 getGoogleJSStyleWithColumns(20));
549 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
550 verifyFormat("import * as lib from 'some/module.js';");
Daniel Jasper60948b12015-03-15 13:55:54 +0000551 verifyFormat("var x = {import: 1};\nx.import = 2;");
Daniel Jasperfca735c2015-02-19 16:14:18 +0000552
553 verifyFormat("export function fn() {\n"
554 " return 'fn';\n"
555 "}");
Daniel Jasper354aa512015-02-19 16:07:32 +0000556 verifyFormat("export const x = 12;");
557 verifyFormat("export default class X {}");
Daniel Jasperfca735c2015-02-19 16:14:18 +0000558 verifyFormat("export {X, Y} from 'some/module.js';");
559 verifyFormat("export {\n"
560 " X,\n"
561 " Y,\n"
562 "} from 'some/module.js';");
563 verifyFormat("export class C {\n"
564 " x: number;\n"
565 " y: string;\n"
566 "}");
567 verifyFormat("export class X { y: number; }");
568 verifyFormat("export default class X { y: number }");
569 verifyFormat("export default function() {\n return 1;\n}");
570 verifyFormat("export var x = 12;");
571 verifyFormat("export var x: number = 12;");
572 verifyFormat("export const y = {\n"
573 " a: 1,\n"
574 " b: 2\n"
575 "};");
Daniel Jasper354aa512015-02-19 16:07:32 +0000576}
577
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000578TEST_F(FormatTestJS, TemplateStrings) {
579 // Keeps any whitespace/indentation within the template string.
580 EXPECT_EQ("var x = `hello\n"
581 " ${ name }\n"
582 " !`;",
583 format("var x = `hello\n"
584 " ${ name }\n"
585 " !`;"));
586
587 // FIXME: +1 / -1 offsets are to work around clang-format miscalculating
588 // widths for unknown tokens that are not whitespace (e.g. '`'). Remove when
589 // the code is corrected.
590
591 verifyFormat("var x =\n"
592 " `hello ${world}` >= some();",
593 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
594 verifyFormat("var x = `hello ${world}` >= some();",
595 getGoogleJSStyleWithColumns(35 + 1)); // Barely fits.
596 EXPECT_EQ("var x = `hello\n"
597 " ${world}` >=\n"
598 " some();",
599 format("var x =\n"
600 " `hello\n"
601 " ${world}` >= some();",
602 getGoogleJSStyleWithColumns(21))); // Barely doesn't fit.
603 EXPECT_EQ("var x = `hello\n"
604 " ${world}` >= some();",
605 format("var x =\n"
606 " `hello\n"
607 " ${world}` >= some();",
608 getGoogleJSStyleWithColumns(22))); // Barely fits.
609
610 verifyFormat("var x =\n `h`;", getGoogleJSStyleWithColumns(13 - 1));
611 EXPECT_EQ(
612 "var x =\n `multi\n line`;",
613 format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(14 - 1)));
614
615 // Two template strings.
616 verifyFormat("var x = `hello` == `hello`;");
617}
618
Daniel Jasperbc46b932015-03-15 13:59:51 +0000619TEST_F(FormatTestJS, CastSyntax) {
620 verifyFormat("var x = <type>foo;");
621}
622
623TEST_F(FormatTestJS, TypeArguments) {
624 verifyFormat("class X<Y> {}");
625 verifyFormat("new X<Y>();");
626 verifyFormat("foo<Y>(a);");
627 verifyFormat("var x: X<Y>[];");
628 verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
629}
630
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000631} // end namespace tooling
632} // end namespace clang