blob: 4378ded5c361b13785fd7e09055c669d6a40ad68 [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");
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000170 verifyFormat("goog.scope(function() {\n"
171 "// test\n"
172 "var x = 0;\n"
173 "// test\n"
174 "});");
Daniel Jasper4a39c842014-05-06 13:54:10 +0000175}
176
Daniel Jasper616de8642014-11-23 16:46:28 +0000177TEST_F(FormatTestJS, GoogModules) {
178 verifyFormat("goog.module('this.is.really.absurdly.long');",
179 getGoogleJSStyleWithColumns(40));
180 verifyFormat("goog.require('this.is.really.absurdly.long');",
181 getGoogleJSStyleWithColumns(40));
182 verifyFormat("goog.provide('this.is.really.absurdly.long');",
183 getGoogleJSStyleWithColumns(40));
184 verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
185 getGoogleJSStyleWithColumns(40));
Daniel Jasper53c38f42014-11-27 14:46:03 +0000186
187 // These should be wrapped normally.
188 verifyFormat(
189 "var MyLongClassName =\n"
190 " goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
Daniel Jasper616de8642014-11-23 16:46:28 +0000191}
192
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000193TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
194 verifyFormat("function outer1(a, b) {\n"
195 " function inner1(a, b) { return a; }\n"
196 " inner1(a, b);\n"
197 "}\n"
198 "function outer2(a, b) {\n"
199 " function inner2(a, b) { return a; }\n"
200 " inner2(a, b);\n"
201 "}");
202}
203
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000204TEST_F(FormatTestJS, FunctionLiterals) {
Daniel Jasper3f69ba12014-09-05 08:42:27 +0000205 verifyFormat("doFoo(function() {});");
Daniel Jasper79dffb42014-05-07 09:48:30 +0000206 verifyFormat("doFoo(function() { return 1; });");
Daniel Jasper67f8ad22014-09-30 17:57:06 +0000207 verifyFormat("var func = function() {\n"
208 " return 1;\n"
209 "};");
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000210 verifyFormat("return {\n"
211 " body: {\n"
212 " setAttribute: function(key, val) { this[key] = val; },\n"
213 " getAttribute: function(key) { return this[key]; },\n"
214 " style: {direction: ''}\n"
215 " }\n"
216 "};");
Daniel Jaspere551bb72014-11-05 17:22:31 +0000217 EXPECT_EQ("abc = xyz ?\n"
218 " function() {\n"
219 " return 1;\n"
220 " } :\n"
221 " function() {\n"
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000222 " return -1;\n"
223 " };",
Daniel Jasper069e5f42014-05-20 11:14:57 +0000224 format("abc=xyz?function(){return 1;}:function(){return -1;};"));
Daniel Jasperb16b9692014-05-21 12:51:23 +0000225
226 verifyFormat("var closure = goog.bind(\n"
227 " function() { // comment\n"
228 " foo();\n"
229 " bar();\n"
230 " },\n"
231 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
232 " arg3IsReallyLongAndNeeedsLineBreaks);");
233 verifyFormat("var closure = goog.bind(function() { // comment\n"
234 " foo();\n"
235 " bar();\n"
236 "}, this);");
Daniel Jasper58cb2ed2014-06-06 13:49:04 +0000237 verifyFormat("return {\n"
238 " a: 'E',\n"
239 " b: function() {\n"
240 " return function() {\n"
241 " f(); //\n"
242 " };\n"
243 " }\n"
244 "};");
Daniel Jasper41368e92014-11-27 15:37:42 +0000245 verifyFormat("{\n"
246 " var someVariable = function(x) {\n"
247 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
248 " };\n"
249 "}");
Daniel Jasper5f3ea472014-05-22 08:36:53 +0000250
Daniel Jaspera4e55f42014-12-04 16:07:17 +0000251 verifyFormat("f({a: function() { return 1; }});",
252 getGoogleJSStyleWithColumns(33));
253 verifyFormat("f({\n"
Daniel Jasper5f3ea472014-05-22 08:36:53 +0000254 " a: function() { return 1; }\n"
Daniel Jaspera4e55f42014-12-04 16:07:17 +0000255 "});",
256 getGoogleJSStyleWithColumns(32));
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000257
258 verifyFormat("return {\n"
259 " a: function SomeFunction() {\n"
260 " // ...\n"
261 " return 1;\n"
262 " }\n"
263 "};");
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000264 verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
265 " .then(goog.bind(function(aaaaaaaaaaa) {\n"
266 " someFunction();\n"
267 " someFunction();\n"
268 " }, this), aaaaaaaaaaaaaaaaa);");
269
270 // FIXME: This is not ideal yet.
271 verifyFormat("someFunction(goog.bind(\n"
272 " function() {\n"
273 " doSomething();\n"
274 " doSomething();\n"
275 " },\n"
276 " this),\n"
277 " goog.bind(function() {\n"
278 " doSomething();\n"
279 " doSomething();\n"
280 " }, this));");
Daniel Jasper79dffb42014-05-07 09:48:30 +0000281}
282
Daniel Jasper67f8ad22014-09-30 17:57:06 +0000283TEST_F(FormatTestJS, InliningFunctionLiterals) {
284 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
285 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
286 verifyFormat("var func = function() {\n"
287 " return 1;\n"
288 "};",
289 Style);
290 verifyFormat("var func = doSomething(function() { return 1; });", Style);
291 verifyFormat("var outer = function() {\n"
292 " var inner = function() { return 1; }\n"
293 "};",
294 Style);
295 verifyFormat("function outer1(a, b) {\n"
296 " function inner1(a, b) { return a; }\n"
297 "}",
298 Style);
299
300 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
301 verifyFormat("var func = function() { return 1; };", Style);
302 verifyFormat("var func = doSomething(function() { return 1; });", Style);
303 verifyFormat(
304 "var outer = function() { var inner = function() { return 1; } };",
305 Style);
306 verifyFormat("function outer1(a, b) {\n"
307 " function inner1(a, b) { return a; }\n"
308 "}",
309 Style);
310
311 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
312 verifyFormat("var func = function() {\n"
313 " return 1;\n"
314 "};",
315 Style);
316 verifyFormat("var func = doSomething(function() {\n"
317 " return 1;\n"
318 "});",
319 Style);
320 verifyFormat("var outer = function() {\n"
321 " var inner = function() {\n"
322 " return 1;\n"
323 " }\n"
324 "};",
325 Style);
326 verifyFormat("function outer1(a, b) {\n"
327 " function inner1(a, b) {\n"
328 " return a;\n"
329 " }\n"
330 "}",
331 Style);
332}
333
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000334TEST_F(FormatTestJS, MultipleFunctionLiterals) {
335 verifyFormat("promise.then(\n"
336 " function success() {\n"
337 " doFoo();\n"
338 " doBar();\n"
339 " },\n"
340 " function error() {\n"
341 " doFoo();\n"
342 " doBaz();\n"
343 " },\n"
344 " []);\n");
345 verifyFormat("promise.then(\n"
346 " function success() {\n"
347 " doFoo();\n"
348 " doBar();\n"
349 " },\n"
350 " [],\n"
351 " function error() {\n"
352 " doFoo();\n"
353 " doBaz();\n"
354 " });\n");
355 // FIXME: Here, we should probably break right after the "(" for consistency.
356 verifyFormat("promise.then([],\n"
357 " function success() {\n"
358 " doFoo();\n"
359 " doBar();\n"
360 " },\n"
361 " function error() {\n"
362 " doFoo();\n"
363 " doBaz();\n"
364 " });\n");
Daniel Jasper1779d432014-09-29 07:54:54 +0000365
366 verifyFormat("getSomeLongPromise()\n"
367 " .then(function(value) { body(); })\n"
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000368 " .thenCatch(function(error) {\n"
369 " body();\n"
370 " body();\n"
371 " });");
Daniel Jasper1779d432014-09-29 07:54:54 +0000372 verifyFormat("getSomeLongPromise()\n"
373 " .then(function(value) {\n"
374 " body();\n"
375 " body();\n"
376 " })\n"
377 " .thenCatch(function(error) {\n"
378 " body();\n"
379 " body();\n"
380 " });");
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000381
382 // FIXME: This is bad, but it used to be formatted correctly by accident.
383 verifyFormat("getSomeLongPromise().then(function(value) {\n"
384 " body();\n"
385 "}).thenCatch(function(error) { body(); });");
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000386}
387
Daniel Jasper166c19b2014-05-06 14:12:21 +0000388TEST_F(FormatTestJS, ReturnStatements) {
Daniel Jasper67f8ad22014-09-30 17:57:06 +0000389 verifyFormat("function() {\n"
390 " return [hello, world];\n"
391 "}");
Daniel Jasper166c19b2014-05-06 14:12:21 +0000392}
393
Daniel Jasper484033b2014-05-06 14:41:29 +0000394TEST_F(FormatTestJS, ClosureStyleComments) {
395 verifyFormat("var x = /** @type {foo} */ (bar);");
396}
397
Daniel Jasper04a71a42014-05-08 11:58:24 +0000398TEST_F(FormatTestJS, TryCatch) {
399 verifyFormat("try {\n"
400 " f();\n"
401 "} catch (e) {\n"
402 " g();\n"
403 "} finally {\n"
404 " h();\n"
405 "}");
Daniel Jasper8f2e94c2014-09-04 15:03:34 +0000406
407 // But, of course, "catch" is a perfectly fine function name in JavaScript.
408 verifyFormat("someObject.catch();");
Daniel Jasper79121232014-11-27 14:55:17 +0000409 verifyFormat("someObject.new();");
410 verifyFormat("someObject.delete();");
Daniel Jasper04a71a42014-05-08 11:58:24 +0000411}
412
Daniel Jasper49802ef2014-05-22 09:10:04 +0000413TEST_F(FormatTestJS, StringLiteralConcatenation) {
414 verifyFormat("var literal = 'hello ' +\n"
415 " 'world';");
416}
417
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000418TEST_F(FormatTestJS, RegexLiteralClassification) {
419 // Regex literals.
420 verifyFormat("var regex = /abc/;");
421 verifyFormat("f(/abc/);");
422 verifyFormat("f(abc, /abc/);");
423 verifyFormat("some_map[/abc/];");
424 verifyFormat("var x = a ? /abc/ : /abc/;");
425 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
426 verifyFormat("var x = !/abc/.test(y);");
427 verifyFormat("var x = a && /abc/.test(y);");
428 verifyFormat("var x = a || /abc/.test(y);");
429 verifyFormat("var x = a + /abc/.search(y);");
Daniel Jasperf7405c12014-05-08 07:45:18 +0000430 verifyFormat("var regexs = {/abc/, /abc/};");
431 verifyFormat("return /abc/;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000432
433 // Not regex literals.
434 verifyFormat("var a = a / 2 + b / 3;");
435}
436
437TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
438 verifyFormat("var regex = /a*/;");
439 verifyFormat("var regex = /a+/;");
440 verifyFormat("var regex = /a?/;");
441 verifyFormat("var regex = /.a./;");
442 verifyFormat("var regex = /a\\*/;");
443 verifyFormat("var regex = /^a$/;");
444 verifyFormat("var regex = /\\/a/;");
445 verifyFormat("var regex = /(?:x)/;");
446 verifyFormat("var regex = /x(?=y)/;");
447 verifyFormat("var regex = /x(?!y)/;");
448 verifyFormat("var regex = /x|y/;");
449 verifyFormat("var regex = /a{2}/;");
450 verifyFormat("var regex = /a{1,3}/;");
451 verifyFormat("var regex = /[abc]/;");
452 verifyFormat("var regex = /[^abc]/;");
453 verifyFormat("var regex = /[\\b]/;");
454 verifyFormat("var regex = /\\b/;");
455 verifyFormat("var regex = /\\B/;");
456 verifyFormat("var regex = /\\d/;");
457 verifyFormat("var regex = /\\D/;");
458 verifyFormat("var regex = /\\f/;");
459 verifyFormat("var regex = /\\n/;");
460 verifyFormat("var regex = /\\r/;");
461 verifyFormat("var regex = /\\s/;");
462 verifyFormat("var regex = /\\S/;");
463 verifyFormat("var regex = /\\t/;");
464 verifyFormat("var regex = /\\v/;");
465 verifyFormat("var regex = /\\w/;");
466 verifyFormat("var regex = /\\W/;");
467 verifyFormat("var regex = /a(a)\\1/;");
468 verifyFormat("var regex = /\\0/;");
Daniel Jasperfb4333b2014-05-12 11:29:50 +0000469 verifyFormat("var regex = /\\\\/g;");
470 verifyFormat("var regex = /\\a\\\\/g;");
471 verifyFormat("var regex = /\a\\//g;");
Daniel Jasper23376252014-09-09 14:37:39 +0000472 verifyFormat("var regex = /a\\//;\n"
473 "var x = 0;");
Daniel Jasper49a9a282014-10-29 16:51:38 +0000474 EXPECT_EQ("var regex = /\\/*/;\n"
475 "var x = 0;",
476 format("var regex = /\\/*/;\n"
477 "var x=0;"));
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000478}
479
480TEST_F(FormatTestJS, RegexLiteralModifiers) {
481 verifyFormat("var regex = /abc/g;");
482 verifyFormat("var regex = /abc/i;");
483 verifyFormat("var regex = /abc/m;");
484 verifyFormat("var regex = /abc/y;");
485}
486
487TEST_F(FormatTestJS, RegexLiteralLength) {
488 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
489 getGoogleJSStyleWithColumns(60));
490 verifyFormat("var regex =\n"
491 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
492 getGoogleJSStyleWithColumns(60));
Daniel Jasper0580ff02014-12-17 09:11:08 +0000493 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
494 getGoogleJSStyleWithColumns(50));
Daniel Jasperf9ae3122014-05-08 07:01:45 +0000495}
496
497TEST_F(FormatTestJS, RegexLiteralExamples) {
498 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
499}
500
Daniel Jasperb10bdff2015-02-18 17:09:53 +0000501TEST_F(FormatTestJS, TypeAnnotations) {
502 verifyFormat("var x: string;");
503 verifyFormat("function x(): string {\n return 'x';\n}");
504 verifyFormat("function x(y: string): string {\n return 'x';\n}");
505 verifyFormat("for (var y: string in x) {\n x();\n}");
506 verifyFormat("((a: string, b: number): string => a + b);");
507 verifyFormat("var x: (y: number) => string;");
508 verifyFormat("var x: P<string, (a: number) => string>;");
509}
510
Daniel Jasper83709082015-02-18 17:14:05 +0000511TEST_F(FormatTestJS, ClassDeclarations) {
512 verifyFormat("class C {\n x: string = 12;\n}");
513 verifyFormat("class C {\n x(): string => 12;\n}");
514 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}");
515 verifyFormat("class C {\n private x: string = 12;\n}");
516 verifyFormat("class C {\n private static x: string = 12;\n}");
517 verifyFormat("class C {\n static x(): string { return 'asd'; }\n}");
518 verifyFormat("class C extends P implements I {}");
519}
520
Daniel Jasper3c42dba2015-02-18 17:17:15 +0000521TEST_F(FormatTestJS, MetadataAnnotations) {
522 verifyFormat("@A\nclass C {\n}");
523 verifyFormat("@A({arg: 'value'})\nclass C {\n}");
524 verifyFormat("@A\n@B\nclass C {\n}");
525 verifyFormat("class C {\n @A x: string;\n}");
526 verifyFormat("class C {\n"
527 " @A\n"
528 " private x(): string {\n"
529 " return 'y';\n"
530 " }\n"
531 "}");
Daniel Jasper6fa9ec72015-02-19 16:03:16 +0000532 verifyFormat("class X {}\n"
533 "class Y {}");
Daniel Jasper3c42dba2015-02-18 17:17:15 +0000534}
535
Daniel Jasper354aa512015-02-19 16:07:32 +0000536TEST_F(FormatTestJS, Modules) {
537 verifyFormat("import SomeThing from 'some/module.js';");
538 verifyFormat("import {X, Y} from 'some/module.js';");
539 verifyFormat("import {\n"
540 " VeryLongImportsAreAnnoying,\n"
541 " VeryLongImportsAreAnnoying,\n"
542 " VeryLongImportsAreAnnoying,\n"
543 " VeryLongImportsAreAnnoying\n"
544 "} from 'some/module.js';");
545 verifyFormat("import {\n"
546 " X,\n"
547 " Y,\n"
548 "} from 'some/module.js';");
549 verifyFormat("import {\n"
550 " X,\n"
551 " Y,\n"
552 "} from 'some/long/module.js';",
553 getGoogleJSStyleWithColumns(20));
554 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
555 verifyFormat("import * as lib from 'some/module.js';");
Daniel Jasper60948b12015-03-15 13:55:54 +0000556 verifyFormat("var x = {import: 1};\nx.import = 2;");
Daniel Jasperfca735c2015-02-19 16:14:18 +0000557
558 verifyFormat("export function fn() {\n"
559 " return 'fn';\n"
560 "}");
Daniel Jasper354aa512015-02-19 16:07:32 +0000561 verifyFormat("export const x = 12;");
562 verifyFormat("export default class X {}");
Daniel Jasperfca735c2015-02-19 16:14:18 +0000563 verifyFormat("export {X, Y} from 'some/module.js';");
564 verifyFormat("export {\n"
565 " X,\n"
566 " Y,\n"
567 "} from 'some/module.js';");
568 verifyFormat("export class C {\n"
569 " x: number;\n"
570 " y: string;\n"
571 "}");
572 verifyFormat("export class X { y: number; }");
573 verifyFormat("export default class X { y: number }");
574 verifyFormat("export default function() {\n return 1;\n}");
575 verifyFormat("export var x = 12;");
576 verifyFormat("export var x: number = 12;");
577 verifyFormat("export const y = {\n"
578 " a: 1,\n"
579 " b: 2\n"
580 "};");
Daniel Jasper354aa512015-02-19 16:07:32 +0000581}
582
Daniel Jaspera0ef4f32015-02-20 13:47:38 +0000583TEST_F(FormatTestJS, TemplateStrings) {
584 // Keeps any whitespace/indentation within the template string.
585 EXPECT_EQ("var x = `hello\n"
586 " ${ name }\n"
587 " !`;",
588 format("var x = `hello\n"
589 " ${ name }\n"
590 " !`;"));
591
592 // FIXME: +1 / -1 offsets are to work around clang-format miscalculating
593 // widths for unknown tokens that are not whitespace (e.g. '`'). Remove when
594 // the code is corrected.
595
596 verifyFormat("var x =\n"
597 " `hello ${world}` >= some();",
598 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
599 verifyFormat("var x = `hello ${world}` >= some();",
600 getGoogleJSStyleWithColumns(35 + 1)); // Barely fits.
601 EXPECT_EQ("var x = `hello\n"
602 " ${world}` >=\n"
603 " some();",
604 format("var x =\n"
605 " `hello\n"
606 " ${world}` >= some();",
607 getGoogleJSStyleWithColumns(21))); // Barely doesn't fit.
608 EXPECT_EQ("var x = `hello\n"
609 " ${world}` >= some();",
610 format("var x =\n"
611 " `hello\n"
612 " ${world}` >= some();",
613 getGoogleJSStyleWithColumns(22))); // Barely fits.
614
615 verifyFormat("var x =\n `h`;", getGoogleJSStyleWithColumns(13 - 1));
616 EXPECT_EQ(
617 "var x =\n `multi\n line`;",
618 format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(14 - 1)));
619
620 // Two template strings.
621 verifyFormat("var x = `hello` == `hello`;");
622}
623
Daniel Jasperbc46b932015-03-15 13:59:51 +0000624TEST_F(FormatTestJS, CastSyntax) {
625 verifyFormat("var x = <type>foo;");
626}
627
628TEST_F(FormatTestJS, TypeArguments) {
629 verifyFormat("class X<Y> {}");
630 verifyFormat("new X<Y>();");
631 verifyFormat("foo<Y>(a);");
632 verifyFormat("var x: X<Y>[];");
633 verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
634}
635
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000636} // end namespace tooling
637} // end namespace clang