blob: 3df01663e9d7b2640978812bcfdd6cb6aad5a685 [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));
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000027 FormattingAttemptStatus Status;
Daniel Jasper56691b82015-06-11 13:29:20 +000028 tooling::Replacements Replaces =
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000029 reformat(Style, Code, Ranges, "<stdin>", &Status);
30 EXPECT_TRUE(Status.FormatComplete);
Eric Liu4f8d9942016-07-11 13:53:12 +000031 auto Result = applyAllReplacements(Code, Replaces);
32 EXPECT_TRUE(static_cast<bool>(Result));
33 DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
34 return *Result;
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000035 }
36
Daniel Jasper069e5f42014-05-20 11:14:57 +000037 static std::string format(
38 llvm::StringRef Code,
39 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000040 return format(Code, 0, Code.size(), Style);
41 }
42
Alexander Kornienkoc1637f12013-12-10 11:28:13 +000043 static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
Nico Weber514ecc82014-02-02 20:50:45 +000044 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000045 Style.ColumnLimit = ColumnLimit;
46 return Style;
47 }
48
Nico Weber514ecc82014-02-02 20:50:45 +000049 static void verifyFormat(
50 llvm::StringRef Code,
51 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +000052 std::string Result = format(test::messUp(Code), Style);
53 EXPECT_EQ(Code.str(), Result) << "Formatted:\n" << Result;
54 }
55
56 static void verifyFormat(
57 llvm::StringRef Expected,
58 llvm::StringRef Code,
59 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
60 std::string Result = format(Code, Style);
61 EXPECT_EQ(Expected.str(), Result) << "Formatted:\n" << Result;
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000062 }
63};
64
Daniel Jaspere77f19c2016-05-29 22:07:22 +000065TEST_F(FormatTestJS, BlockComments) {
66 verifyFormat("/* aaaaaaaaaaaaa */ aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
67 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Krasimir Georgiev35599fd2017-10-16 09:08:53 +000068 // Breaks after a single line block comment.
69 EXPECT_EQ("aaaaa = bbbb.ccccccccccccccc(\n"
70 " /** @type_{!cccc.rrrrrrr.MMMMMMMMMMMM.LLLLLLLLLLL.lala} */\n"
71 " mediaMessage);",
72 format("aaaaa = bbbb.ccccccccccccccc(\n"
73 " /** "
74 "@type_{!cccc.rrrrrrr.MMMMMMMMMMMM.LLLLLLLLLLL.lala} */ "
75 "mediaMessage);",
76 getGoogleJSStyleWithColumns(70)));
77 // Breaks after a multiline block comment.
78 EXPECT_EQ(
79 "aaaaa = bbbb.ccccccccccccccc(\n"
80 " /**\n"
81 " * @type_{!cccc.rrrrrrr.MMMMMMMMMMMM.LLLLLLLLLLL.lala}\n"
82 " */\n"
83 " mediaMessage);",
84 format("aaaaa = bbbb.ccccccccccccccc(\n"
85 " /**\n"
86 " * @type_{!cccc.rrrrrrr.MMMMMMMMMMMM.LLLLLLLLLLL.lala}\n"
87 " */ mediaMessage);",
88 getGoogleJSStyleWithColumns(70)));
Daniel Jaspere77f19c2016-05-29 22:07:22 +000089}
90
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +000091TEST_F(FormatTestJS, JSDocComments) {
92 // Break the first line of a multiline jsdoc comment.
93 EXPECT_EQ("/**\n"
94 " * jsdoc line 1\n"
95 " * jsdoc line 2\n"
96 " */",
97 format("/** jsdoc line 1\n"
98 " * jsdoc line 2\n"
99 " */",
100 getGoogleJSStyleWithColumns(20)));
101 // Both break after '/**' and break the line itself.
102 EXPECT_EQ("/**\n"
103 " * jsdoc line long\n"
104 " * long jsdoc line 2\n"
105 " */",
106 format("/** jsdoc line long long\n"
107 " * jsdoc line 2\n"
108 " */",
109 getGoogleJSStyleWithColumns(20)));
110 // Break a short first line if the ending '*/' is on a newline.
111 EXPECT_EQ("/**\n"
112 " * jsdoc line 1\n"
113 " */",
114 format("/** jsdoc line 1\n"
115 " */", getGoogleJSStyleWithColumns(20)));
116 // Don't break the first line of a short single line jsdoc comment.
117 EXPECT_EQ("/** jsdoc line 1 */",
118 format("/** jsdoc line 1 */", getGoogleJSStyleWithColumns(20)));
119 // Don't break the first line of a single line jsdoc comment if it just fits
120 // the column limit.
121 EXPECT_EQ("/** jsdoc line 12 */",
122 format("/** jsdoc line 12 */", getGoogleJSStyleWithColumns(20)));
123 // Don't break after '/**' and before '*/' if there is no space between
124 // '/**' and the content.
125 EXPECT_EQ(
126 "/*** nonjsdoc long\n"
127 " * line */",
128 format("/*** nonjsdoc long line */", getGoogleJSStyleWithColumns(20)));
129 EXPECT_EQ(
130 "/**strange long long\n"
131 " * line */",
132 format("/**strange long long line */", getGoogleJSStyleWithColumns(20)));
133 // Break the first line of a single line jsdoc comment if it just exceeds the
134 // column limit.
135 EXPECT_EQ("/**\n"
136 " * jsdoc line 123\n"
137 " */",
138 format("/** jsdoc line 123 */", getGoogleJSStyleWithColumns(20)));
139 // Break also if the leading indent of the first line is more than 1 column.
140 EXPECT_EQ("/**\n"
141 " * jsdoc line 123\n"
142 " */",
143 format("/** jsdoc line 123 */", getGoogleJSStyleWithColumns(20)));
144 // Break also if the leading indent of the first line is more than 1 column.
145 EXPECT_EQ("/**\n"
146 " * jsdoc line 123\n"
147 " */",
148 format("/** jsdoc line 123 */", getGoogleJSStyleWithColumns(20)));
149 // Break after the content of the last line.
150 EXPECT_EQ("/**\n"
151 " * line 1\n"
152 " * line 2\n"
153 " */",
154 format("/**\n"
155 " * line 1\n"
156 " * line 2 */",
157 getGoogleJSStyleWithColumns(20)));
158 // Break both the content and after the content of the last line.
159 EXPECT_EQ("/**\n"
160 " * line 1\n"
161 " * line long long\n"
162 " * long\n"
163 " */",
164 format("/**\n"
165 " * line 1\n"
166 " * line long long long */",
167 getGoogleJSStyleWithColumns(20)));
168
169 // The comment block gets indented.
170 EXPECT_EQ("function f() {\n"
171 " /**\n"
172 " * comment about\n"
173 " * x\n"
174 " */\n"
175 " var x = 1;\n"
176 "}",
177 format("function f() {\n"
178 "/** comment about x */\n"
179 "var x = 1;\n"
180 "}",
181 getGoogleJSStyleWithColumns(20)));
Krasimir Georgiev3b865342017-08-09 09:42:32 +0000182
183 // Don't break the first line of a single line short jsdoc comment pragma.
184 EXPECT_EQ("/** @returns j */",
185 format("/** @returns j */",
186 getGoogleJSStyleWithColumns(20)));
187
188 // Break a single line long jsdoc comment pragma.
189 EXPECT_EQ("/**\n"
190 " * @returns {string} jsdoc line 12\n"
191 " */",
192 format("/** @returns {string} jsdoc line 12 */",
193 getGoogleJSStyleWithColumns(20)));
194
195 EXPECT_EQ("/**\n"
196 " * @returns {string} jsdoc line 12\n"
197 " */",
198 format("/** @returns {string} jsdoc line 12 */",
199 getGoogleJSStyleWithColumns(20)));
200
201 EXPECT_EQ("/**\n"
202 " * @returns {string} jsdoc line 12\n"
203 " */",
204 format("/** @returns {string} jsdoc line 12*/",
205 getGoogleJSStyleWithColumns(20)));
206
207 // Fix a multiline jsdoc comment ending in a comment pragma.
208 EXPECT_EQ("/**\n"
209 " * line 1\n"
210 " * line 2\n"
211 " * @returns {string} jsdoc line 12\n"
212 " */",
213 format("/** line 1\n"
214 " * line 2\n"
215 " * @returns {string} jsdoc line 12 */",
216 getGoogleJSStyleWithColumns(20)));
217
218 EXPECT_EQ("/**\n"
219 " * line 1\n"
220 " * line 2\n"
221 " *\n"
222 " * @returns j\n"
223 " */",
224 format("/** line 1\n"
225 " * line 2\n"
226 " *\n"
227 " * @returns j */",
228 getGoogleJSStyleWithColumns(20)));
Krasimir Georgiev22d7e6b2017-07-20 22:29:39 +0000229}
230
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000231TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
232 verifyFormat("a == = b;");
233 verifyFormat("a != = b;");
234
235 verifyFormat("a === b;");
Daniel Jaspere551bb72014-11-05 17:22:31 +0000236 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000237 verifyFormat("a !== b;");
Daniel Jaspere551bb72014-11-05 17:22:31 +0000238 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10));
239 verifyFormat("if (a + b + c +\n"
240 " d !==\n"
241 " e + f + g)\n"
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000242 " q();",
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000243 getGoogleJSStyleWithColumns(20));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000244
245 verifyFormat("a >> >= b;");
246
247 verifyFormat("a >>> b;");
Daniel Jaspere551bb72014-11-05 17:22:31 +0000248 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10));
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000249 verifyFormat("a >>>= b;");
Daniel Jaspere551bb72014-11-05 17:22:31 +0000250 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10));
251 verifyFormat("if (a + b + c +\n"
252 " d >>>\n"
253 " e + f + g)\n"
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000254 " q();",
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000255 getGoogleJSStyleWithColumns(20));
Daniel Jaspere551bb72014-11-05 17:22:31 +0000256 verifyFormat("var x = aaaaaaaaaa ?\n"
Daniel Jasper41a2bf72015-12-21 13:52:19 +0000257 " bbbbbb :\n"
258 " ccc;",
Alexander Kornienkoc1637f12013-12-10 11:28:13 +0000259 getGoogleJSStyleWithColumns(20));
Daniel Jasper78214392014-05-19 07:27:02 +0000260
261 verifyFormat("var b = a.map((x) => x + 1);");
Daniel Jasper3549ea12014-09-19 10:48:15 +0000262 verifyFormat("return ('aaa') in bbbb;");
Daniel Jasper1ce41112016-02-01 11:20:47 +0000263 verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n"
264 " aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
265 FormatStyle Style = getGoogleJSStyleWithColumns(80);
266 Style.AlignOperands = true;
267 verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n"
268 " aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
269 Style);
270 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
271 verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa()\n"
272 " in aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
273 Style);
Daniel Jasper08051992015-05-26 07:18:56 +0000274
275 // ES6 spread operator.
276 verifyFormat("someFunction(...a);");
277 verifyFormat("var x = [1, ...a, 2];");
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +0000278}
279
Daniel Jasper3a038de2014-09-05 08:53:45 +0000280TEST_F(FormatTestJS, UnderstandsAmpAmp) {
281 verifyFormat("e && e.SomeFunction();");
282}
283
Daniel Jasper4db69bd2014-09-04 18:23:42 +0000284TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
285 verifyFormat("not.and.or.not_eq = 1;");
286}
287
Daniel Jasperba52fcb2015-09-28 14:29:45 +0000288TEST_F(FormatTestJS, ReservedWords) {
289 // JavaScript reserved words (aka keywords) are only illegal when used as
290 // Identifiers, but are legal as IdentifierNames.
291 verifyFormat("x.class.struct = 1;");
292 verifyFormat("x.case = 1;");
293 verifyFormat("x.interface = 1;");
Daniel Jasper997cf2f2016-05-20 06:16:01 +0000294 verifyFormat("x.for = 1;");
Martin Probst83e02202017-08-14 16:08:16 +0000295 verifyFormat("x.of();");
Martin Probstc4a3d082017-04-05 10:56:07 +0000296 verifyFormat("of(null);");
297 verifyFormat("import {of} from 'x';");
Martin Probst83e02202017-08-14 16:08:16 +0000298 verifyFormat("x.in();");
299 verifyFormat("x.let();");
300 verifyFormat("x.var();");
301 verifyFormat("x.for();");
302 verifyFormat("x.as();");
303 verifyFormat("x.instanceof();");
304 verifyFormat("x.switch();");
305 verifyFormat("x.case();");
306 verifyFormat("x.delete();");
307 verifyFormat("x.throw();");
308 verifyFormat("x.throws();");
309 verifyFormat("x.if();");
Daniel Jasperba52fcb2015-09-28 14:29:45 +0000310 verifyFormat("x = {\n"
311 " a: 12,\n"
312 " interface: 1,\n"
313 " switch: 1,\n"
314 "};");
Daniel Jasper09840ef2015-11-20 15:58:50 +0000315 verifyFormat("var struct = 2;");
316 verifyFormat("var union = 2;");
Martin Probst1e8261e2016-04-19 18:18:59 +0000317 verifyFormat("var interface = 2;");
318 verifyFormat("interface = 2;");
319 verifyFormat("x = interface instanceof y;");
Martin Probstf785fd92017-08-04 17:07:15 +0000320 verifyFormat("interface Test {\n"
321 " x: string;\n"
322 " switch: string;\n"
323 " case: string;\n"
324 " default: string;\n"
325 "}\n");
Daniel Jasperba52fcb2015-09-28 14:29:45 +0000326}
327
Martin Probst4210d2f2016-09-22 07:18:00 +0000328TEST_F(FormatTestJS, ReservedWordsMethods) {
329 verifyFormat(
330 "class X {\n"
331 " delete() {\n"
332 " x();\n"
333 " }\n"
334 " interface() {\n"
335 " x();\n"
336 " }\n"
337 " let() {\n"
338 " x();\n"
339 " }\n"
340 "}\n");
341}
342
Martin Probst22e00f02017-08-01 17:22:15 +0000343TEST_F(FormatTestJS, ReservedWordsParenthesized) {
344 // All of these are statements using the keyword, not function calls.
345 verifyFormat("throw (x + y);\n"
346 "await (await x).y;\n"
Martin Probst9926abb2017-08-01 17:42:16 +0000347 "typeof (x) === 'string';\n"
Martin Probst22e00f02017-08-01 17:22:15 +0000348 "void (0);\n"
349 "delete (x.y);\n"
350 "return (x);\n");
351}
352
Daniel Jasper72a1b6a2015-12-22 15:47:56 +0000353TEST_F(FormatTestJS, CppKeywords) {
354 // Make sure we don't mess stuff up because of C++ keywords.
355 verifyFormat("return operator && (aa);");
Daniel Jasper72b33572017-03-31 12:04:37 +0000356 // .. or QT ones.
357 verifyFormat("slots: Slot[];");
Daniel Jasper72a1b6a2015-12-22 15:47:56 +0000358}
359
Daniel Jasper0dd52912014-05-19 07:37:07 +0000360TEST_F(FormatTestJS, ES6DestructuringAssignment) {
361 verifyFormat("var [a, b, c] = [1, 2, 3];");
Daniel Jasper5dbcd3b2016-05-19 07:18:07 +0000362 verifyFormat("const [a, b, c] = [1, 2, 3];");
Daniel Jasper9f642f72015-09-28 14:28:08 +0000363 verifyFormat("let [a, b, c] = [1, 2, 3];");
Daniel Jasper60948b12015-03-15 13:55:54 +0000364 verifyFormat("var {a, b} = {a: 1, b: 2};");
Daniel Jasper9f642f72015-09-28 14:28:08 +0000365 verifyFormat("let {a, b} = {a: 1, b: 2};");
Daniel Jasper0dd52912014-05-19 07:37:07 +0000366}
367
Daniel Jasper17062ff2014-06-10 14:44:02 +0000368TEST_F(FormatTestJS, ContainerLiterals) {
Daniel Jasper28d8a5a2016-09-07 23:01:13 +0000369 verifyFormat("var x = {\n"
370 " y: function(a) {\n"
371 " return a;\n"
372 " }\n"
373 "};");
Daniel Jasper17062ff2014-06-10 14:44:02 +0000374 verifyFormat("return {\n"
375 " link: function() {\n"
376 " f(); //\n"
377 " }\n"
378 "};");
379 verifyFormat("return {\n"
380 " a: a,\n"
381 " link: function() {\n"
382 " f(); //\n"
383 " }\n"
384 "};");
385 verifyFormat("return {\n"
386 " a: a,\n"
Daniel Jasper90ebc982014-09-05 09:27:38 +0000387 " link: function() {\n"
388 " f(); //\n"
389 " },\n"
390 " link: function() {\n"
391 " f(); //\n"
392 " }\n"
Daniel Jasper17062ff2014-06-10 14:44:02 +0000393 "};");
Daniel Jasper94e11d02014-09-04 14:58:30 +0000394 verifyFormat("var stuff = {\n"
395 " // comment for update\n"
396 " update: false,\n"
397 " // comment for modules\n"
398 " modules: false,\n"
399 " // comment for tasks\n"
400 " tasks: false\n"
401 "};");
Daniel Jasper97bfb7b2014-09-05 08:29:31 +0000402 verifyFormat("return {\n"
403 " 'finish':\n"
404 " //\n"
405 " a\n"
406 "};");
Daniel Jasper40874322014-11-27 15:24:48 +0000407 verifyFormat("var obj = {\n"
408 " fooooooooo: function(x) {\n"
409 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
410 " }\n"
411 "};");
Daniel Jasper60948b12015-03-15 13:55:54 +0000412 // Simple object literal, as opposed to enum style below.
413 verifyFormat("var obj = {a: 123};");
414 // Enum style top level assignment.
415 verifyFormat("X = {\n a: 123\n};");
416 verifyFormat("X.Y = {\n a: 123\n};");
Daniel Jasper11ca2632015-06-10 09:21:09 +0000417 // But only on the top level, otherwise its a plain object literal assignment.
418 verifyFormat("function x() {\n"
419 " y = {z: 1};\n"
420 "}");
Daniel Jasper60948b12015-03-15 13:55:54 +0000421 verifyFormat("x = foo && {a: 123};");
Manuel Klimek79e06082015-05-21 12:23:34 +0000422
423 // Arrow functions in object literals.
Daniel Jasper28d8a5a2016-09-07 23:01:13 +0000424 verifyFormat("var x = {\n"
425 " y: (a) => {\n"
426 " return a;\n"
427 " }\n"
428 "};");
Manuel Klimek79e06082015-05-21 12:23:34 +0000429 verifyFormat("var x = {y: (a) => a};");
Daniel Jasper8c42d442015-05-29 06:19:49 +0000430
Martin Probst8e3eba02017-02-07 16:33:13 +0000431 // Methods in object literals.
432 verifyFormat("var x = {\n"
433 " y(a: string): number {\n"
434 " return a;\n"
435 " }\n"
436 "};");
437 verifyFormat("var x = {\n"
438 " y(a: string) {\n"
439 " return a;\n"
440 " }\n"
441 "};");
442
Daniel Jasper8c42d442015-05-29 06:19:49 +0000443 // Computed keys.
Daniel Jaspercd8d4ff2015-05-31 08:40:37 +0000444 verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};");
Daniel Jasper8c42d442015-05-29 06:19:49 +0000445 verifyFormat("var x = {\n"
446 " [a]: 1,\n"
Daniel Jaspercd8d4ff2015-05-31 08:40:37 +0000447 " b: 2,\n"
448 " [c]: 3,\n"
Daniel Jasper8c42d442015-05-29 06:19:49 +0000449 "};");
Daniel Jasper9f4c9d42016-02-07 22:17:13 +0000450
451 // Object literals can leave out labels.
452 verifyFormat("f({a}, () => {\n"
453 " g(); //\n"
454 "});");
Daniel Jasperb18425b2016-03-01 04:19:47 +0000455
456 // Keys can be quoted.
457 verifyFormat("var x = {\n"
458 " a: a,\n"
459 " b: b,\n"
460 " 'c': c,\n"
461 "};");
Daniel Jaspere98e5812016-11-29 09:40:36 +0000462
463 // Dict literals can skip the label names.
464 verifyFormat("var x = {\n"
465 " aaa,\n"
466 " aaa,\n"
467 " aaa,\n"
468 "};");
Daniel Jasper893b8ad2017-03-01 19:47:28 +0000469 verifyFormat("return {\n"
470 " a,\n"
471 " b: 'b',\n"
472 " c,\n"
473 "};");
Daniel Jasper17062ff2014-06-10 14:44:02 +0000474}
475
Daniel Jasperf46dec82015-03-31 14:34:15 +0000476TEST_F(FormatTestJS, MethodsInObjectLiterals) {
477 verifyFormat("var o = {\n"
478 " value: 'test',\n"
479 " get value() { // getter\n"
480 " return this.value;\n"
481 " }\n"
482 "};");
483 verifyFormat("var o = {\n"
484 " value: 'test',\n"
485 " set value(val) { // setter\n"
486 " this.value = val;\n"
487 " }\n"
488 "};");
489 verifyFormat("var o = {\n"
490 " value: 'test',\n"
491 " someMethod(val) { // method\n"
492 " doSomething(this.value + val);\n"
493 " }\n"
494 "};");
Daniel Jasper739ec532015-04-04 07:56:55 +0000495 verifyFormat("var o = {\n"
496 " someMethod(val) { // method\n"
497 " doSomething(this.value + val);\n"
498 " },\n"
499 " someOtherMethod(val) { // method\n"
500 " doSomething(this.value + val);\n"
501 " }\n"
502 "};");
Daniel Jasperf46dec82015-03-31 14:34:15 +0000503}
504
Martin Probst19c7de02017-04-26 12:34:15 +0000505TEST_F(FormatTestJS, GettersSettersVisibilityKeywords) {
506 // Don't break after "protected"
507 verifyFormat("class X {\n"
508 " protected get getter():\n"
509 " number {\n"
510 " return 1;\n"
511 " }\n"
512 "}",
513 getGoogleJSStyleWithColumns(12));
514 // Don't break after "get"
515 verifyFormat("class X {\n"
516 " protected get someReallyLongGetterName():\n"
517 " number {\n"
518 " return 1;\n"
519 " }\n"
520 "}",
521 getGoogleJSStyleWithColumns(40));
522}
523
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000524TEST_F(FormatTestJS, SpacesInContainerLiterals) {
525 verifyFormat("var arr = [1, 2, 3];");
Daniel Jaspera4e55f42014-12-04 16:07:17 +0000526 verifyFormat("f({a: 1, b: 2, c: 3});");
Nico Weber514ecc82014-02-02 20:50:45 +0000527
Daniel Jasper2a958322014-05-21 13:26:58 +0000528 verifyFormat("var object_literal_with_long_name = {\n"
529 " a: 'aaaaaaaaaaaaaaaaaa',\n"
530 " b: 'bbbbbbbbbbbbbbbbbb'\n"
531 "};");
532
Daniel Jaspera4e55f42014-12-04 16:07:17 +0000533 verifyFormat("f({a: 1, b: 2, c: 3});",
Nico Weber514ecc82014-02-02 20:50:45 +0000534 getChromiumStyle(FormatStyle::LK_JavaScript));
Daniel Jaspera4e55f42014-12-04 16:07:17 +0000535 verifyFormat("f({'a': [{}]});");
Daniel Jasperb2e10a52014-01-15 15:09:08 +0000536}
537
Daniel Jasperabd1f572016-03-02 22:44:03 +0000538TEST_F(FormatTestJS, SingleQuotedStrings) {
Daniel Jasper86fee2f2014-01-31 12:49:42 +0000539 verifyFormat("this.function('', true);");
540}
541
Daniel Jasper4a39c842014-05-06 13:54:10 +0000542TEST_F(FormatTestJS, GoogScopes) {
543 verifyFormat("goog.scope(function() {\n"
544 "var x = a.b;\n"
545 "var y = c.d;\n"
546 "}); // goog.scope");
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000547 verifyFormat("goog.scope(function() {\n"
548 "// test\n"
549 "var x = 0;\n"
550 "// test\n"
551 "});");
Daniel Jasper4a39c842014-05-06 13:54:10 +0000552}
553
Martin Probst101ec892017-05-09 20:04:09 +0000554TEST_F(FormatTestJS, IIFEs) {
555 // Internal calling parens; no semi.
556 verifyFormat("(function() {\n"
557 "var a = 1;\n"
558 "}())");
559 // External calling parens; no semi.
560 verifyFormat("(function() {\n"
561 "var b = 2;\n"
562 "})()");
563 // Internal calling parens; with semi.
564 verifyFormat("(function() {\n"
565 "var c = 3;\n"
566 "}());");
567 // External calling parens; with semi.
568 verifyFormat("(function() {\n"
569 "var d = 4;\n"
570 "})();");
571}
572
Daniel Jasper616de8642014-11-23 16:46:28 +0000573TEST_F(FormatTestJS, GoogModules) {
574 verifyFormat("goog.module('this.is.really.absurdly.long');",
575 getGoogleJSStyleWithColumns(40));
576 verifyFormat("goog.require('this.is.really.absurdly.long');",
577 getGoogleJSStyleWithColumns(40));
578 verifyFormat("goog.provide('this.is.really.absurdly.long');",
579 getGoogleJSStyleWithColumns(40));
580 verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
581 getGoogleJSStyleWithColumns(40));
Daniel Jasper02cbeb22016-02-22 15:06:53 +0000582 verifyFormat("goog.forwardDeclare('this.is.really.absurdly.long');",
583 getGoogleJSStyleWithColumns(40));
Daniel Jasper53c38f42014-11-27 14:46:03 +0000584
585 // These should be wrapped normally.
586 verifyFormat(
587 "var MyLongClassName =\n"
588 " goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
Martin Probst103a7b52017-09-11 15:22:52 +0000589 verifyFormat("function a() {\n"
590 " goog.setTestOnly();\n"
591 "}\n",
592 "function a() {\n"
593 "goog.setTestOnly();\n"
594 "}\n");
Daniel Jasper616de8642014-11-23 16:46:28 +0000595}
596
Martin Probstece8c0c2016-06-13 16:41:28 +0000597TEST_F(FormatTestJS, FormatsNamespaces) {
598 verifyFormat("namespace Foo {\n"
599 " export let x = 1;\n"
600 "}\n");
601 verifyFormat("declare namespace Foo {\n"
602 " export let x: number;\n"
603 "}\n");
604}
605
Martin Probst72fd75a2016-11-10 16:20:58 +0000606TEST_F(FormatTestJS, NamespacesMayNotWrap) {
607 verifyFormat("declare namespace foobarbaz {\n"
608 "}\n", getGoogleJSStyleWithColumns(18));
609 verifyFormat("declare module foobarbaz {\n"
610 "}\n", getGoogleJSStyleWithColumns(15));
611 verifyFormat("namespace foobarbaz {\n"
612 "}\n", getGoogleJSStyleWithColumns(10));
613 verifyFormat("module foobarbaz {\n"
614 "}\n", getGoogleJSStyleWithColumns(7));
615}
616
617TEST_F(FormatTestJS, AmbientDeclarations) {
618 FormatStyle NineCols = getGoogleJSStyleWithColumns(9);
619 verifyFormat(
620 "declare class\n"
621 " X {}",
622 NineCols);
623 verifyFormat(
624 "declare function\n"
625 "x();", // TODO(martinprobst): should ideally be indented.
626 NineCols);
Martin Probstaf16c502017-01-04 13:36:43 +0000627 verifyFormat("declare function foo();\n"
628 "let x = 1;\n");
629 verifyFormat("declare function foo(): string;\n"
630 "let x = 1;\n");
631 verifyFormat("declare function foo(): {x: number};\n"
632 "let x = 1;\n");
633 verifyFormat("declare class X {}\n"
634 "let x = 1;\n");
635 verifyFormat("declare interface Y {}\n"
636 "let x = 1;\n");
Martin Probst72fd75a2016-11-10 16:20:58 +0000637 verifyFormat(
638 "declare enum X {\n"
639 "}",
640 NineCols);
641 verifyFormat(
642 "declare let\n"
643 " x: number;",
644 NineCols);
645}
646
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000647TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
648 verifyFormat("function outer1(a, b) {\n"
Daniel Jasper28d8a5a2016-09-07 23:01:13 +0000649 " function inner1(a, b) {\n"
650 " return a;\n"
651 " }\n"
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000652 " inner1(a, b);\n"
653 "}\n"
654 "function outer2(a, b) {\n"
Daniel Jasper28d8a5a2016-09-07 23:01:13 +0000655 " function inner2(a, b) {\n"
656 " return a;\n"
657 " }\n"
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000658 " inner2(a, b);\n"
659 "}");
Daniel Jasper20580fd2015-06-11 13:31:45 +0000660 verifyFormat("function f() {}");
Martin Probstb7fb2672017-05-10 13:53:29 +0000661 verifyFormat("function aFunction() {}\n"
662 "(function f() {\n"
663 " var x = 1;\n"
664 "}());\n");
Martin Probst95ed8e72017-05-31 09:29:40 +0000665 verifyFormat("function aFunction() {}\n"
666 "{\n"
Martin Probstb7fb2672017-05-10 13:53:29 +0000667 " let x = 1;\n"
668 " console.log(x);\n"
669 "}\n");
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000670}
671
Martin Probst5f8445b2016-04-24 22:05:09 +0000672TEST_F(FormatTestJS, GeneratorFunctions) {
673 verifyFormat("function* f() {\n"
674 " let x = 1;\n"
675 " yield x;\n"
676 " yield* something();\n"
Daniel Jaspereb886632016-10-31 13:18:25 +0000677 " yield [1, 2];\n"
678 " yield {a: 1};\n"
Martin Probst5f8445b2016-04-24 22:05:09 +0000679 "}");
680 verifyFormat("function*\n"
681 " f() {\n"
682 "}",
683 getGoogleJSStyleWithColumns(8));
684 verifyFormat("export function* f() {\n"
685 " yield 1;\n"
686 "}\n");
687 verifyFormat("class X {\n"
Daniel Jasper28d8a5a2016-09-07 23:01:13 +0000688 " * generatorMethod() {\n"
689 " yield x;\n"
690 " }\n"
Martin Probst5f8445b2016-04-24 22:05:09 +0000691 "}");
Daniel Jasper71e50af2016-11-01 06:22:59 +0000692 verifyFormat("var x = {\n"
693 " a: function*() {\n"
694 " //\n"
695 " }\n"
696 "}\n");
Martin Probst5f8445b2016-04-24 22:05:09 +0000697}
698
699TEST_F(FormatTestJS, AsyncFunctions) {
700 verifyFormat("async function f() {\n"
701 " let x = 1;\n"
702 " return fetch(x);\n"
703 "}");
Martin Probst973ff792017-04-27 13:07:24 +0000704 verifyFormat("async function f() {\n"
705 " return 1;\n"
706 "}\n"
707 "\n"
708 "function a() {\n"
709 " return 1;\n"
710 "}\n",
711 " async function f() {\n"
712 " return 1;\n"
713 "}\n"
714 "\n"
715 " function a() {\n"
716 " return 1;\n"
717 "} \n");
Martin Probst5f8445b2016-04-24 22:05:09 +0000718 verifyFormat("async function* f() {\n"
719 " yield fetch(x);\n"
720 "}");
721 verifyFormat("export async function f() {\n"
722 " return fetch(x);\n"
723 "}");
Martin Probst20371c32017-02-27 11:15:53 +0000724 verifyFormat("let x = async () => f();");
Martin Probst973ff792017-04-27 13:07:24 +0000725 verifyFormat("let x = async function() {\n"
726 " f();\n"
727 "};");
Martin Probst20371c32017-02-27 11:15:53 +0000728 verifyFormat("let x = async();");
Martin Probst5f8445b2016-04-24 22:05:09 +0000729 verifyFormat("class X {\n"
Daniel Jasper28d8a5a2016-09-07 23:01:13 +0000730 " async asyncMethod() {\n"
731 " return fetch(1);\n"
732 " }\n"
Martin Probst5f8445b2016-04-24 22:05:09 +0000733 "}");
Martin Probst409697e2016-05-29 14:41:07 +0000734 verifyFormat("function initialize() {\n"
735 " // Comment.\n"
736 " return async.then();\n"
737 "}\n");
Martin Probsta050f412017-05-18 21:19:29 +0000738 verifyFormat("for await (const x of y) {\n"
Martin Probstbd49e322017-05-15 19:33:20 +0000739 " console.log(x);\n"
740 "}\n");
741 verifyFormat("function asyncLoop() {\n"
Martin Probsta050f412017-05-18 21:19:29 +0000742 " for await (const x of y) {\n"
Martin Probstbd49e322017-05-15 19:33:20 +0000743 " console.log(x);\n"
744 " }\n"
745 "}\n");
Martin Probst5f8445b2016-04-24 22:05:09 +0000746}
747
Martin Probst2c1cdae2017-05-15 11:15:29 +0000748TEST_F(FormatTestJS, FunctionParametersTrailingComma) {
749 verifyFormat("function trailingComma(\n"
750 " p1,\n"
751 " p2,\n"
752 " p3,\n"
753 ") {\n"
754 " a; //\n"
755 "}\n",
756 "function trailingComma(p1, p2, p3,) {\n"
757 " a; //\n"
758 "}\n");
759 verifyFormat("trailingComma(\n"
760 " p1,\n"
761 " p2,\n"
762 " p3,\n"
763 ");\n",
764 "trailingComma(p1, p2, p3,);\n");
765 verifyFormat("trailingComma(\n"
766 " p1 // hello\n"
767 ");\n",
768 "trailingComma(p1 // hello\n"
769 ");\n");
770}
771
Daniel Jasperf841d3a2015-05-28 07:21:50 +0000772TEST_F(FormatTestJS, ArrayLiterals) {
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000773 verifyFormat("var aaaaa: List<SomeThing> =\n"
774 " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];");
Daniel Jasperd492b5e2015-06-02 21:57:51 +0000775 verifyFormat("return [\n"
Daniel Jasper50780ce2016-01-13 16:41:34 +0000776 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
Daniel Jasperd492b5e2015-06-02 21:57:51 +0000777 " ccccccccccccccccccccccccccc\n"
778 "];");
Daniel Jasper7bec87c2016-01-07 18:11:54 +0000779 verifyFormat("return [\n"
780 " aaaa().bbbbbbbb('A'),\n"
781 " aaaa().bbbbbbbb('B'),\n"
782 " aaaa().bbbbbbbb('C'),\n"
783 "];");
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000784 verifyFormat("var someVariable = SomeFunction([\n"
Daniel Jasper50780ce2016-01-13 16:41:34 +0000785 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
Daniel Jasper5ce80de2015-06-02 13:56:43 +0000786 " ccccccccccccccccccccccccccc\n"
787 "]);");
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000788 verifyFormat("var someVariable = SomeFunction([\n"
Daniel Jasper40432ce2015-06-02 15:04:29 +0000789 " [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n"
Daniel Jasper199d0c92015-06-02 15:14:21 +0000790 "]);",
791 getGoogleJSStyleWithColumns(51));
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000792 verifyFormat("var someVariable = SomeFunction(aaaa, [\n"
Daniel Jasper50780ce2016-01-13 16:41:34 +0000793 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
Daniel Jasper4284e362015-06-02 14:20:08 +0000794 " ccccccccccccccccccccccccccc\n"
795 "]);");
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000796 verifyFormat("var someVariable = SomeFunction(\n"
797 " aaaa,\n"
798 " [\n"
Daniel Jasper50780ce2016-01-13 16:41:34 +0000799 " aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
800 " cccccccccccccccccccccccccc\n"
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000801 " ],\n"
802 " aaaa);");
Daniel Jasperebcb71f2016-01-04 13:11:41 +0000803 verifyFormat("var aaaa = aaaaa || // wrap\n"
804 " [];");
Daniel Jasper1feab0f2015-06-02 15:31:37 +0000805
806 verifyFormat("someFunction([], {a: a});");
Daniel Jasper51c868e2017-01-30 07:08:40 +0000807
808 verifyFormat("var string = [\n"
809 " 'aaaaaa',\n"
810 " 'bbbbbb',\n"
811 "].join('+');");
Daniel Jasperf841d3a2015-05-28 07:21:50 +0000812}
813
Daniel Jaspere2deb592015-12-22 15:48:15 +0000814TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) {
815 verifyFormat("var array = [\n"
816 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
817 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
818 "];");
819 verifyFormat("var array = someFunction([\n"
820 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
821 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
822 "]);");
823}
824
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000825TEST_F(FormatTestJS, FunctionLiterals) {
Daniel Jasper28d8a5a2016-09-07 23:01:13 +0000826 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
827 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
Daniel Jasper3f69ba12014-09-05 08:42:27 +0000828 verifyFormat("doFoo(function() {});");
Daniel Jasper28d8a5a2016-09-07 23:01:13 +0000829 verifyFormat("doFoo(function() { return 1; });", Style);
Daniel Jasper67f8ad22014-09-30 17:57:06 +0000830 verifyFormat("var func = function() {\n"
831 " return 1;\n"
832 "};");
Daniel Jaspered3f3952015-06-18 12:32:59 +0000833 verifyFormat("var func = //\n"
834 " function() {\n"
835 " return 1;\n"
836 "};");
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000837 verifyFormat("return {\n"
838 " body: {\n"
839 " setAttribute: function(key, val) { this[key] = val; },\n"
840 " getAttribute: function(key) { return this[key]; },\n"
841 " style: {direction: ''}\n"
842 " }\n"
Daniel Jasper28d8a5a2016-09-07 23:01:13 +0000843 "};",
844 Style);
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000845 verifyFormat("abc = xyz ? function() {\n"
846 " return 1;\n"
847 "} : function() {\n"
848 " return -1;\n"
849 "};");
Daniel Jasperb16b9692014-05-21 12:51:23 +0000850
851 verifyFormat("var closure = goog.bind(\n"
852 " function() { // comment\n"
853 " foo();\n"
854 " bar();\n"
855 " },\n"
Joerg Sonnenbergere9987a12017-01-05 17:59:44 +0000856 " this, arg1IsReallyLongAndNeedsLineBreaks,\n"
857 " arg3IsReallyLongAndNeedsLineBreaks);");
Daniel Jasperb16b9692014-05-21 12:51:23 +0000858 verifyFormat("var closure = goog.bind(function() { // comment\n"
859 " foo();\n"
860 " bar();\n"
861 "}, this);");
Daniel Jasper58cb2ed2014-06-06 13:49:04 +0000862 verifyFormat("return {\n"
863 " a: 'E',\n"
864 " b: function() {\n"
865 " return function() {\n"
866 " f(); //\n"
867 " };\n"
868 " }\n"
869 "};");
Daniel Jasper41368e92014-11-27 15:37:42 +0000870 verifyFormat("{\n"
871 " var someVariable = function(x) {\n"
872 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
873 " };\n"
874 "}");
Daniel Jasper7325aee2015-05-08 08:38:52 +0000875 verifyFormat("someLooooooooongFunction(\n"
876 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
878 " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
879 " // code\n"
880 " });");
Daniel Jasper5f3ea472014-05-22 08:36:53 +0000881
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000882 verifyFormat("return {\n"
883 " a: function SomeFunction() {\n"
884 " // ...\n"
885 " return 1;\n"
886 " }\n"
887 "};");
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000888 verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
889 " .then(goog.bind(function(aaaaaaaaaaa) {\n"
890 " someFunction();\n"
891 " someFunction();\n"
892 " }, this), aaaaaaaaaaaaaaaaa);");
893
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000894 verifyFormat("someFunction(goog.bind(function() {\n"
895 " doSomething();\n"
896 " doSomething();\n"
897 "}, this), goog.bind(function() {\n"
898 " doSomething();\n"
899 " doSomething();\n"
900 "}, this));");
Daniel Jasper1699eca2015-06-01 09:56:32 +0000901
Daniel Jasper51c868e2017-01-30 07:08:40 +0000902 verifyFormat("SomeFunction(function() {\n"
903 " foo();\n"
904 " bar();\n"
905 "}.bind(this));");
906
Martin Probstb2f06ea2017-05-29 07:50:52 +0000907 verifyFormat("SomeFunction((function() {\n"
908 " foo();\n"
909 " bar();\n"
910 " }).bind(this));");
911
Daniel Jasper1699eca2015-06-01 09:56:32 +0000912 // FIXME: This is bad, we should be wrapping before "function() {".
913 verifyFormat("someFunction(function() {\n"
914 " doSomething(); // break\n"
915 "})\n"
916 " .doSomethingElse(\n"
917 " // break\n"
Martin Probst2c1cdae2017-05-15 11:15:29 +0000918 " );");
Daniel Jasper28d8a5a2016-09-07 23:01:13 +0000919
920 Style.ColumnLimit = 33;
921 verifyFormat("f({a: function() { return 1; }});", Style);
922 Style.ColumnLimit = 32;
923 verifyFormat("f({\n"
924 " a: function() { return 1; }\n"
925 "});",
926 Style);
927
Daniel Jasper79dffb42014-05-07 09:48:30 +0000928}
929
Martin Probstcde98152017-08-01 17:35:57 +0000930TEST_F(FormatTestJS, DontWrapEmptyLiterals) {
931 verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n"
932 " .and.returnValue(Observable.of([]));");
933 verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n"
934 " .and.returnValue(Observable.of({}));");
935 verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n"
936 " .and.returnValue(Observable.of(()));");
937}
938
Daniel Jasper67f8ad22014-09-30 17:57:06 +0000939TEST_F(FormatTestJS, InliningFunctionLiterals) {
940 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
941 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
942 verifyFormat("var func = function() {\n"
943 " return 1;\n"
944 "};",
945 Style);
946 verifyFormat("var func = doSomething(function() { return 1; });", Style);
947 verifyFormat("var outer = function() {\n"
948 " var inner = function() { return 1; }\n"
949 "};",
950 Style);
951 verifyFormat("function outer1(a, b) {\n"
952 " function inner1(a, b) { return a; }\n"
953 "}",
954 Style);
955
956 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
957 verifyFormat("var func = function() { return 1; };", Style);
958 verifyFormat("var func = doSomething(function() { return 1; });", Style);
959 verifyFormat(
960 "var outer = function() { var inner = function() { return 1; } };",
961 Style);
962 verifyFormat("function outer1(a, b) {\n"
963 " function inner1(a, b) { return a; }\n"
964 "}",
965 Style);
966
967 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
968 verifyFormat("var func = function() {\n"
969 " return 1;\n"
970 "};",
971 Style);
972 verifyFormat("var func = doSomething(function() {\n"
973 " return 1;\n"
974 "});",
975 Style);
976 verifyFormat("var outer = function() {\n"
977 " var inner = function() {\n"
978 " return 1;\n"
979 " }\n"
980 "};",
981 Style);
982 verifyFormat("function outer1(a, b) {\n"
983 " function inner1(a, b) {\n"
984 " return a;\n"
985 " }\n"
986 "}",
987 Style);
Daniel Jasper71e57452015-11-20 16:44:28 +0000988
989 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
990 verifyFormat("var func = function() {\n"
991 " return 1;\n"
992 "};",
993 Style);
Daniel Jasper67f8ad22014-09-30 17:57:06 +0000994}
995
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000996TEST_F(FormatTestJS, MultipleFunctionLiterals) {
Daniel Jasper28d8a5a2016-09-07 23:01:13 +0000997 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
998 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000999 verifyFormat("promise.then(\n"
1000 " function success() {\n"
1001 " doFoo();\n"
1002 " doBar();\n"
1003 " },\n"
1004 " function error() {\n"
1005 " doFoo();\n"
1006 " doBaz();\n"
1007 " },\n"
1008 " []);\n");
1009 verifyFormat("promise.then(\n"
1010 " function success() {\n"
1011 " doFoo();\n"
1012 " doBar();\n"
1013 " },\n"
1014 " [],\n"
1015 " function error() {\n"
1016 " doFoo();\n"
1017 " doBaz();\n"
1018 " });\n");
Daniel Jasper6501f7e2015-10-27 12:38:37 +00001019 verifyFormat("promise.then(\n"
1020 " [],\n"
1021 " function success() {\n"
1022 " doFoo();\n"
1023 " doBar();\n"
1024 " },\n"
1025 " function error() {\n"
1026 " doFoo();\n"
1027 " doBaz();\n"
1028 " });\n");
Daniel Jasper1779d432014-09-29 07:54:54 +00001029
1030 verifyFormat("getSomeLongPromise()\n"
1031 " .then(function(value) { body(); })\n"
Daniel Jasper11a0ac62014-12-12 09:40:58 +00001032 " .thenCatch(function(error) {\n"
1033 " body();\n"
1034 " body();\n"
Daniel Jasper28d8a5a2016-09-07 23:01:13 +00001035 " });",
1036 Style);
Daniel Jasper1779d432014-09-29 07:54:54 +00001037 verifyFormat("getSomeLongPromise()\n"
1038 " .then(function(value) {\n"
1039 " body();\n"
1040 " body();\n"
1041 " })\n"
1042 " .thenCatch(function(error) {\n"
1043 " body();\n"
1044 " body();\n"
1045 " });");
Daniel Jasper11a0ac62014-12-12 09:40:58 +00001046
Daniel Jasperacf67e32015-04-07 08:20:35 +00001047 verifyFormat("getSomeLongPromise()\n"
1048 " .then(function(value) { body(); })\n"
Daniel Jasper28d8a5a2016-09-07 23:01:13 +00001049 " .thenCatch(function(error) { body(); });",
1050 Style);
Daniel Jasper28024562016-01-11 11:00:58 +00001051
1052 verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n"
1053 " .aaaaaaa(function() {\n"
1054 " //\n"
1055 " })\n"
1056 " .bbbbbb();");
Daniel Jasper114a2bc2014-06-03 12:02:45 +00001057}
1058
Manuel Klimek79e06082015-05-21 12:23:34 +00001059TEST_F(FormatTestJS, ArrowFunctions) {
1060 verifyFormat("var x = (a) => {\n"
1061 " return a;\n"
1062 "};");
1063 verifyFormat("var x = (a) => {\n"
Daniel Jasper28d8a5a2016-09-07 23:01:13 +00001064 " function y() {\n"
1065 " return 42;\n"
1066 " }\n"
Manuel Klimek79e06082015-05-21 12:23:34 +00001067 " return a;\n"
1068 "};");
1069 verifyFormat("var x = (a: type): {some: type} => {\n"
1070 " return a;\n"
1071 "};");
1072 verifyFormat("var x = (a) => a;");
Daniel Jaspere497bed2015-06-02 22:06:07 +00001073 verifyFormat("return () => [];");
Daniel Jasper3c306e82015-06-03 17:08:40 +00001074 verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n"
1075 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
1076 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1077 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n"
Daniel Jasper3b0f3042015-06-05 08:25:37 +00001078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
Daniel Jasper3c306e82015-06-03 17:08:40 +00001079 "};");
Daniel Jasper6501f7e2015-10-27 12:38:37 +00001080 verifyFormat("var a = a.aaaaaaa(\n"
1081 " (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n"
Daniel Jasper41a2bf72015-12-21 13:52:19 +00001082 " aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
Daniel Jasper6501f7e2015-10-27 12:38:37 +00001083 verifyFormat("var a = a.aaaaaaa(\n"
1084 " (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n"
Daniel Jasper41a2bf72015-12-21 13:52:19 +00001085 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n"
1086 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
Daniel Jasper1699eca2015-06-01 09:56:32 +00001087
1088 // FIXME: This is bad, we should be wrapping before "() => {".
1089 verifyFormat("someFunction(() => {\n"
1090 " doSomething(); // break\n"
1091 "})\n"
1092 " .doSomethingElse(\n"
1093 " // break\n"
Martin Probst2c1cdae2017-05-15 11:15:29 +00001094 " );");
Martin Probstec363262017-08-01 17:19:32 +00001095 verifyFormat("const f = (x: string|null): string|null => {\n"
1096 " return x;\n"
1097 "}\n");
Manuel Klimek79e06082015-05-21 12:23:34 +00001098}
1099
Daniel Jasper166c19b2014-05-06 14:12:21 +00001100TEST_F(FormatTestJS, ReturnStatements) {
Daniel Jasper67f8ad22014-09-30 17:57:06 +00001101 verifyFormat("function() {\n"
1102 " return [hello, world];\n"
1103 "}");
Daniel Jasper166c19b2014-05-06 14:12:21 +00001104}
1105
Daniel Jasperf95b1f42015-11-20 16:18:42 +00001106TEST_F(FormatTestJS, ForLoops) {
1107 verifyFormat("for (var i in [2, 3]) {\n"
1108 "}");
Daniel Jasperb7fda112016-02-11 13:24:15 +00001109 verifyFormat("for (var i of [2, 3]) {\n"
1110 "}");
Daniel Jasperacffeb82016-03-05 18:34:26 +00001111 verifyFormat("for (let {a, b} of x) {\n"
1112 "}");
1113 verifyFormat("for (let {a, b} in x) {\n"
1114 "}");
Daniel Jasperf95b1f42015-11-20 16:18:42 +00001115}
1116
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001117TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) {
Daniel Jasper4c0bf702015-06-12 04:58:27 +00001118 // The following statements must not wrap, as otherwise the program meaning
1119 // would change due to automatic semicolon insertion.
1120 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1.
1121 verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10));
Martin Probst56ff7aa2016-09-06 18:39:30 +00001122 verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
Daniel Jasper4c0bf702015-06-12 04:58:27 +00001123 verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10));
Martin Probst56ff7aa2016-09-06 18:39:30 +00001124 verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
Daniel Jasper4c0bf702015-06-12 04:58:27 +00001125 verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10));
1126 verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10));
1127 verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10));
1128 verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10));
Daniel Jasper11a87652016-01-14 05:37:52 +00001129 verifyFormat("return [\n"
1130 " aaa\n"
1131 "];",
1132 getGoogleJSStyleWithColumns(12));
Martin Probsta81dd0b2017-07-07 13:17:10 +00001133 verifyFormat("class X {\n"
1134 " readonly ratherLongField =\n"
1135 " 1;\n"
1136 "}",
1137 "class X {\n"
1138 " readonly ratherLongField = 1;\n"
1139 "}",
1140 getGoogleJSStyleWithColumns(20));
Martin Probst0a19d432017-08-09 15:19:16 +00001141 verifyFormat("const x = (5 + 9)\n"
1142 "const y = 3\n",
1143 "const x = ( 5 + 9)\n"
1144 "const y = 3\n");
Daniel Jasper4c0bf702015-06-12 04:58:27 +00001145}
1146
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001147TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {
1148 verifyFormat("a\n"
1149 "b;",
1150 " a \n"
1151 " b ;");
1152 verifyFormat("a()\n"
1153 "b;",
1154 " a ()\n"
1155 " b ;");
1156 verifyFormat("a[b]\n"
1157 "c;",
1158 "a [b]\n"
1159 "c ;");
1160 verifyFormat("1\n"
1161 "a;",
1162 "1 \n"
1163 "a ;");
1164 verifyFormat("a\n"
1165 "1;",
1166 "a \n"
1167 "1 ;");
1168 verifyFormat("a\n"
1169 "'x';",
1170 "a \n"
1171 " 'x';");
1172 verifyFormat("a++\n"
1173 "b;",
1174 "a ++\n"
1175 "b ;");
1176 verifyFormat("a\n"
1177 "!b && c;",
1178 "a \n"
1179 " ! b && c;");
1180 verifyFormat("a\n"
1181 "if (1) f();",
1182 " a\n"
1183 " if (1) f();");
1184 verifyFormat("a\n"
1185 "class X {}",
1186 " a\n"
1187 " class X {}");
1188 verifyFormat("var a", "var\n"
1189 "a");
1190 verifyFormat("x instanceof String", "x\n"
1191 "instanceof\n"
1192 "String");
Martin Probstbbffeac2016-04-11 07:35:57 +00001193 verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n"
1194 " bar) {}");
Martin Probstb9316ff2016-09-18 17:21:52 +00001195 verifyFormat("a = true\n"
1196 "return 1",
1197 "a = true\n"
1198 " return 1");
1199 verifyFormat("a = 's'\n"
1200 "return 1",
1201 "a = 's'\n"
1202 " return 1");
1203 verifyFormat("a = null\n"
1204 "return 1",
1205 "a = null\n"
1206 " return 1");
Martin Probste6b5b342017-01-16 09:52:40 +00001207 // Below "class Y {}" should ideally be on its own line.
Martin Probstd40bca42017-01-09 08:56:36 +00001208 verifyFormat(
Martin Probstfd18ec52017-01-09 09:00:58 +00001209 "x = {\n"
1210 " a: 1\n"
Martin Probste6b5b342017-01-16 09:52:40 +00001211 "} class Y {}",
Martin Probstd40bca42017-01-09 08:56:36 +00001212 " x = {a : 1}\n"
1213 " class Y { }");
Martin Probste6b5b342017-01-16 09:52:40 +00001214 verifyFormat(
1215 "if (x) {\n"
1216 "}\n"
1217 "return 1",
1218 "if (x) {}\n"
1219 " return 1");
1220 verifyFormat(
1221 "if (x) {\n"
1222 "}\n"
1223 "class X {}",
1224 "if (x) {}\n"
1225 " class X {}");
Martin Probstd40bca42017-01-09 08:56:36 +00001226}
1227
1228TEST_F(FormatTestJS, ImportExportASI) {
1229 verifyFormat(
1230 "import {x} from 'y'\n"
1231 "export function z() {}",
1232 "import {x} from 'y'\n"
1233 " export function z() {}");
Martin Probste6b5b342017-01-16 09:52:40 +00001234 // Below "class Y {}" should ideally be on its own line.
Martin Probstd40bca42017-01-09 08:56:36 +00001235 verifyFormat(
Martin Probste6b5b342017-01-16 09:52:40 +00001236 "export {x} class Y {}",
Martin Probstd40bca42017-01-09 08:56:36 +00001237 " export {x}\n"
1238 " class Y {\n}");
Martin Probste6b5b342017-01-16 09:52:40 +00001239 verifyFormat(
1240 "if (x) {\n"
1241 "}\n"
1242 "export class Y {}",
1243 "if ( x ) { }\n"
1244 " export class Y {}");
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001245}
1246
Manuel Klimek79e06082015-05-21 12:23:34 +00001247TEST_F(FormatTestJS, ClosureStyleCasts) {
Daniel Jasper484033b2014-05-06 14:41:29 +00001248 verifyFormat("var x = /** @type {foo} */ (bar);");
1249}
1250
Daniel Jasper04a71a42014-05-08 11:58:24 +00001251TEST_F(FormatTestJS, TryCatch) {
1252 verifyFormat("try {\n"
1253 " f();\n"
1254 "} catch (e) {\n"
1255 " g();\n"
1256 "} finally {\n"
1257 " h();\n"
1258 "}");
Daniel Jasper8f2e94c2014-09-04 15:03:34 +00001259
1260 // But, of course, "catch" is a perfectly fine function name in JavaScript.
1261 verifyFormat("someObject.catch();");
Daniel Jasper79121232014-11-27 14:55:17 +00001262 verifyFormat("someObject.new();");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001263}
1264
Daniel Jasper49802ef2014-05-22 09:10:04 +00001265TEST_F(FormatTestJS, StringLiteralConcatenation) {
1266 verifyFormat("var literal = 'hello ' +\n"
Daniel Jasper41a2bf72015-12-21 13:52:19 +00001267 " 'world';");
Daniel Jasper49802ef2014-05-22 09:10:04 +00001268}
1269
Daniel Jasperf9ae3122014-05-08 07:01:45 +00001270TEST_F(FormatTestJS, RegexLiteralClassification) {
1271 // Regex literals.
1272 verifyFormat("var regex = /abc/;");
1273 verifyFormat("f(/abc/);");
1274 verifyFormat("f(abc, /abc/);");
1275 verifyFormat("some_map[/abc/];");
1276 verifyFormat("var x = a ? /abc/ : /abc/;");
1277 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
1278 verifyFormat("var x = !/abc/.test(y);");
Martin Probst16282992017-02-07 14:08:03 +00001279 verifyFormat("var x = foo()! / 10;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +00001280 verifyFormat("var x = a && /abc/.test(y);");
1281 verifyFormat("var x = a || /abc/.test(y);");
1282 verifyFormat("var x = a + /abc/.search(y);");
Daniel Jasperc553ae12015-07-02 13:20:45 +00001283 verifyFormat("/abc/.search(y);");
Daniel Jasperf7405c12014-05-08 07:45:18 +00001284 verifyFormat("var regexs = {/abc/, /abc/};");
1285 verifyFormat("return /abc/;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +00001286
1287 // Not regex literals.
1288 verifyFormat("var a = a / 2 + b / 3;");
Daniel Jasper265309e2015-10-18 07:02:28 +00001289 verifyFormat("var a = a++ / 2;");
1290 // Prefix unary can operate on regex literals, not that it makes sense.
1291 verifyFormat("var a = ++/a/;");
1292
1293 // This is a known issue, regular expressions are incorrectly detected if
1294 // directly following a closing parenthesis.
1295 verifyFormat("if (foo) / bar /.exec(baz);");
Daniel Jasperf9ae3122014-05-08 07:01:45 +00001296}
1297
1298TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
Daniel Jasper69694b02015-05-08 07:55:13 +00001299 verifyFormat("var regex = /=/;");
Daniel Jasperf9ae3122014-05-08 07:01:45 +00001300 verifyFormat("var regex = /a*/;");
1301 verifyFormat("var regex = /a+/;");
1302 verifyFormat("var regex = /a?/;");
1303 verifyFormat("var regex = /.a./;");
1304 verifyFormat("var regex = /a\\*/;");
1305 verifyFormat("var regex = /^a$/;");
1306 verifyFormat("var regex = /\\/a/;");
1307 verifyFormat("var regex = /(?:x)/;");
1308 verifyFormat("var regex = /x(?=y)/;");
1309 verifyFormat("var regex = /x(?!y)/;");
1310 verifyFormat("var regex = /x|y/;");
1311 verifyFormat("var regex = /a{2}/;");
1312 verifyFormat("var regex = /a{1,3}/;");
Daniel Jasper8d0e2232015-10-12 03:13:48 +00001313
Daniel Jasperf9ae3122014-05-08 07:01:45 +00001314 verifyFormat("var regex = /[abc]/;");
1315 verifyFormat("var regex = /[^abc]/;");
1316 verifyFormat("var regex = /[\\b]/;");
Daniel Jasper8d0e2232015-10-12 03:13:48 +00001317 verifyFormat("var regex = /[/]/;");
1318 verifyFormat("var regex = /[\\/]/;");
1319 verifyFormat("var regex = /\\[/;");
1320 verifyFormat("var regex = /\\\\[/]/;");
Daniel Jasper265309e2015-10-18 07:02:28 +00001321 verifyFormat("var regex = /}[\"]/;");
1322 verifyFormat("var regex = /}[/\"]/;");
1323 verifyFormat("var regex = /}[\"/]/;");
Daniel Jasper8d0e2232015-10-12 03:13:48 +00001324
Daniel Jasperf9ae3122014-05-08 07:01:45 +00001325 verifyFormat("var regex = /\\b/;");
1326 verifyFormat("var regex = /\\B/;");
1327 verifyFormat("var regex = /\\d/;");
1328 verifyFormat("var regex = /\\D/;");
1329 verifyFormat("var regex = /\\f/;");
1330 verifyFormat("var regex = /\\n/;");
1331 verifyFormat("var regex = /\\r/;");
1332 verifyFormat("var regex = /\\s/;");
1333 verifyFormat("var regex = /\\S/;");
1334 verifyFormat("var regex = /\\t/;");
1335 verifyFormat("var regex = /\\v/;");
1336 verifyFormat("var regex = /\\w/;");
1337 verifyFormat("var regex = /\\W/;");
1338 verifyFormat("var regex = /a(a)\\1/;");
1339 verifyFormat("var regex = /\\0/;");
Daniel Jasperfb4333b2014-05-12 11:29:50 +00001340 verifyFormat("var regex = /\\\\/g;");
1341 verifyFormat("var regex = /\\a\\\\/g;");
1342 verifyFormat("var regex = /\a\\//g;");
Daniel Jasper23376252014-09-09 14:37:39 +00001343 verifyFormat("var regex = /a\\//;\n"
1344 "var x = 0;");
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001345 verifyFormat("var regex = /'/g;", "var regex = /'/g ;");
1346 verifyFormat("var regex = /'/g; //'", "var regex = /'/g ; //'");
1347 verifyFormat("var regex = /\\/*/;\n"
1348 "var x = 0;",
1349 "var regex = /\\/*/;\n"
1350 "var x=0;");
1351 verifyFormat("var x = /a\\//;", "var x = /a\\// \n;");
Daniel Jasper6b8d26c2015-06-24 16:01:02 +00001352 verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16));
1353 verifyFormat("var regex =\n"
1354 " /\"/;",
1355 getGoogleJSStyleWithColumns(15));
Daniel Jasperf7372152015-07-02 14:14:04 +00001356 verifyFormat("var regex = //\n"
1357 " /a/;");
Daniel Jasperf1446202015-07-02 15:00:44 +00001358 verifyFormat("var regexs = [\n"
1359 " /d/, //\n"
1360 " /aa/, //\n"
1361 "];");
Daniel Jasperf9ae3122014-05-08 07:01:45 +00001362}
1363
1364TEST_F(FormatTestJS, RegexLiteralModifiers) {
1365 verifyFormat("var regex = /abc/g;");
1366 verifyFormat("var regex = /abc/i;");
1367 verifyFormat("var regex = /abc/m;");
1368 verifyFormat("var regex = /abc/y;");
1369}
1370
1371TEST_F(FormatTestJS, RegexLiteralLength) {
1372 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
1373 getGoogleJSStyleWithColumns(60));
1374 verifyFormat("var regex =\n"
1375 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
1376 getGoogleJSStyleWithColumns(60));
Daniel Jasper0580ff02014-12-17 09:11:08 +00001377 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
1378 getGoogleJSStyleWithColumns(50));
Daniel Jasperf9ae3122014-05-08 07:01:45 +00001379}
1380
1381TEST_F(FormatTestJS, RegexLiteralExamples) {
1382 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
1383}
1384
Martin Probstfa37b182017-01-27 09:09:11 +00001385TEST_F(FormatTestJS, IgnoresMpegTS) {
1386 std::string MpegTS(200, ' ');
1387 MpegTS.replace(0, strlen("nearlyLooks + like + ts + code; "),
1388 "nearlyLooks + like + ts + code; ");
1389 MpegTS[0] = 0x47;
1390 MpegTS[188] = 0x47;
1391 verifyFormat(MpegTS, MpegTS);
1392}
1393
Daniel Jasperb10bdff2015-02-18 17:09:53 +00001394TEST_F(FormatTestJS, TypeAnnotations) {
1395 verifyFormat("var x: string;");
Daniel Jasperb9a49902016-01-09 15:56:28 +00001396 verifyFormat("var x: {a: string; b: number;} = {};");
Daniel Jasperb10bdff2015-02-18 17:09:53 +00001397 verifyFormat("function x(): string {\n return 'x';\n}");
Manuel Klimek79e06082015-05-21 12:23:34 +00001398 verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}");
Daniel Jasperb10bdff2015-02-18 17:09:53 +00001399 verifyFormat("function x(y: string): string {\n return 'x';\n}");
1400 verifyFormat("for (var y: string in x) {\n x();\n}");
Daniel Jasperb7fda112016-02-11 13:24:15 +00001401 verifyFormat("for (var y: string of x) {\n x();\n}");
Daniel Jasperb9a49902016-01-09 15:56:28 +00001402 verifyFormat("function x(y: {a?: number;} = {}): number {\n"
1403 " return 12;\n"
1404 "}");
Daniel Jasperb10bdff2015-02-18 17:09:53 +00001405 verifyFormat("((a: string, b: number): string => a + b);");
1406 verifyFormat("var x: (y: number) => string;");
1407 verifyFormat("var x: P<string, (a: number) => string>;");
Daniel Jasper28d8a5a2016-09-07 23:01:13 +00001408 verifyFormat("var x = {\n"
1409 " y: function(): z {\n"
1410 " return 1;\n"
1411 " }\n"
1412 "};");
1413 verifyFormat("var x = {\n"
1414 " y: function(): {a: number} {\n"
1415 " return 1;\n"
1416 " }\n"
1417 "};");
Daniel Jasperf412e262016-01-08 10:51:24 +00001418 verifyFormat("function someFunc(args: string[]):\n"
1419 " {longReturnValue: string[]} {}",
1420 getGoogleJSStyleWithColumns(60));
Daniel Jasper3d934d32017-02-20 12:43:41 +00001421 verifyFormat(
1422 "var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])\n"
1423 " .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
Daniel Jasperb10bdff2015-02-18 17:09:53 +00001424}
1425
Daniel Jasper91b1d1a2016-03-21 17:57:31 +00001426TEST_F(FormatTestJS, UnionIntersectionTypes) {
1427 verifyFormat("let x: A|B = A | B;");
1428 verifyFormat("let x: A&B|C = A & B;");
1429 verifyFormat("let x: Foo<A|B> = new Foo<A|B>();");
1430 verifyFormat("function(x: A|B): C&D {}");
1431 verifyFormat("function(x: A|B = A | B): C&D {}");
Martin Probst2ec23242016-06-09 22:49:04 +00001432 verifyFormat("function x(path: number|string) {}");
1433 verifyFormat("function x(): string|number {}");
Martin Probst1b7f9802016-06-23 19:52:32 +00001434 verifyFormat("type Foo = Bar|Baz;");
1435 verifyFormat("type Foo = Bar<X>|Baz;");
1436 verifyFormat("type Foo = (Bar<X>|Baz);");
1437 verifyFormat("let x: Bar|Baz;");
1438 verifyFormat("let x: Bar<X>|Baz;");
1439 verifyFormat("let x: (Foo|Bar)[];");
Martin Probstd96a0522017-06-07 12:53:22 +00001440 verifyFormat("type X = {\n"
1441 " a: Foo|Bar;\n"
1442 "};");
1443 verifyFormat("export type X = {\n"
1444 " a: Foo|Bar;\n"
1445 "};");
Daniel Jasper91b1d1a2016-03-21 17:57:31 +00001446}
1447
Martin Probst0fb46bb2017-08-08 15:00:58 +00001448TEST_F(FormatTestJS, UnionIntersectionTypesInObjectType) {
1449 verifyFormat("let x: {x: number|null} = {x: number | null};");
1450 verifyFormat("let nested: {x: {y: number|null}};");
1451 verifyFormat("let mixed: {x: [number|null, {w: number}]};");
1452 verifyFormat("class X {\n"
1453 " contructor(x: {\n"
1454 " a: a|null,\n"
1455 " b: b|null,\n"
1456 " }) {}\n"
1457 "}");
1458}
1459
Daniel Jasper83709082015-02-18 17:14:05 +00001460TEST_F(FormatTestJS, ClassDeclarations) {
1461 verifyFormat("class C {\n x: string = 12;\n}");
1462 verifyFormat("class C {\n x(): string => 12;\n}");
1463 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}");
Martin Probst95ed8e72017-05-31 09:29:40 +00001464 verifyFormat("class C {\n"
1465 " foo() {}\n"
1466 " [bar]() {}\n"
1467 "}\n");
Daniel Jasper83709082015-02-18 17:14:05 +00001468 verifyFormat("class C {\n private x: string = 12;\n}");
1469 verifyFormat("class C {\n private static x: string = 12;\n}");
Daniel Jasper28d8a5a2016-09-07 23:01:13 +00001470 verifyFormat("class C {\n static x(): string {\n return 'asd';\n }\n}");
Daniel Jasper83709082015-02-18 17:14:05 +00001471 verifyFormat("class C extends P implements I {}");
Daniel Jasper6a5d38d2015-04-13 14:56:54 +00001472 verifyFormat("class C extends p.P implements i.I {}");
Martin Probst1027fb82017-02-07 14:05:30 +00001473 verifyFormat(
1474 "x(class {\n"
1475 " a(): A {}\n"
1476 "});");
Daniel Jasperb9a52812015-07-10 13:39:26 +00001477 verifyFormat("class Test {\n"
1478 " aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n"
1479 " aaaaaaaaaaaaaaaaaaaaaa {}\n"
1480 "}");
Daniel Jasperd5ec65b2016-01-08 07:06:07 +00001481 verifyFormat("foo = class Name {\n"
1482 " constructor() {}\n"
1483 "};");
1484 verifyFormat("foo = class {\n"
1485 " constructor() {}\n"
1486 "};");
Daniel Jasperb9a49902016-01-09 15:56:28 +00001487 verifyFormat("class C {\n"
1488 " x: {y: Z;} = {};\n"
1489 " private y: {y: Z;} = {};\n"
1490 "}");
Daniel Jasper81dbb562015-06-03 08:43:18 +00001491
1492 // ':' is not a type declaration here.
1493 verifyFormat("class X {\n"
1494 " subs = {\n"
1495 " 'b': {\n"
1496 " 'c': 1,\n"
1497 " },\n"
1498 " };\n"
1499 "}");
Daniel Jasper64650082016-06-14 11:28:02 +00001500 verifyFormat("@Component({\n"
1501 " moduleId: module.id,\n"
1502 "})\n"
1503 "class SessionListComponent implements OnDestroy, OnInit {\n"
1504 "}");
Daniel Jasper83709082015-02-18 17:14:05 +00001505}
1506
Daniel Jasper29647492015-05-05 08:12:50 +00001507TEST_F(FormatTestJS, InterfaceDeclarations) {
1508 verifyFormat("interface I {\n"
1509 " x: string;\n"
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001510 " enum: string[];\n"
Daniel Jasper87379302016-02-03 05:33:44 +00001511 " enum?: string[];\n"
Daniel Jasper259188b2015-06-12 04:56:34 +00001512 "}\n"
1513 "var y;");
Daniel Jasper5c235c02015-07-06 14:26:04 +00001514 // Ensure that state is reset after parsing the interface.
1515 verifyFormat("interface a {}\n"
1516 "export function b() {}\n"
1517 "var x;");
Daniel Jasperb542f9f2015-12-22 15:48:35 +00001518
1519 // Arrays of object type literals.
1520 verifyFormat("interface I {\n"
1521 " o: {}[];\n"
1522 "}");
Daniel Jasper29647492015-05-05 08:12:50 +00001523}
1524
Martin Probstcb870c52017-08-01 15:46:10 +00001525TEST_F(FormatTestJS, ObjectTypesInExtendsImplements) {
1526 verifyFormat("class C extends {} {}");
1527 verifyFormat("class C implements {bar: number} {}");
1528 // Somewhat odd, but probably closest to reasonable formatting?
1529 verifyFormat("class C implements {\n"
1530 " bar: number,\n"
1531 " baz: string,\n"
1532 "} {}");
1533 verifyFormat("class C<P extends {}> {}");
1534}
1535
Daniel Jasper90cf3802015-06-17 09:44:02 +00001536TEST_F(FormatTestJS, EnumDeclarations) {
1537 verifyFormat("enum Foo {\n"
Daniel Jasper9f4ec152015-06-17 09:44:07 +00001538 " A = 1,\n"
1539 " B\n"
1540 "}");
1541 verifyFormat("export /* somecomment*/ enum Foo {\n"
1542 " A = 1,\n"
1543 " B\n"
1544 "}");
1545 verifyFormat("enum Foo {\n"
Daniel Jasper90cf3802015-06-17 09:44:02 +00001546 " A = 1, // comment\n"
1547 " B\n"
1548 "}\n"
1549 "var x = 1;");
Martin Probstdb51cc52017-08-01 17:12:15 +00001550 verifyFormat("const enum Foo {\n"
1551 " A = 1,\n"
1552 " B\n"
1553 "}");
1554 verifyFormat("export const enum Foo {\n"
1555 " A = 1,\n"
1556 " B\n"
1557 "}");
Daniel Jasper90cf3802015-06-17 09:44:02 +00001558}
1559
Daniel Jasper3c42dba2015-02-18 17:17:15 +00001560TEST_F(FormatTestJS, MetadataAnnotations) {
1561 verifyFormat("@A\nclass C {\n}");
1562 verifyFormat("@A({arg: 'value'})\nclass C {\n}");
1563 verifyFormat("@A\n@B\nclass C {\n}");
1564 verifyFormat("class C {\n @A x: string;\n}");
1565 verifyFormat("class C {\n"
1566 " @A\n"
1567 " private x(): string {\n"
1568 " return 'y';\n"
1569 " }\n"
1570 "}");
Martin Probst716a5332016-04-11 09:17:57 +00001571 verifyFormat("class C {\n"
1572 " private x(@A x: string) {}\n"
1573 "}");
Daniel Jasper6fa9ec72015-02-19 16:03:16 +00001574 verifyFormat("class X {}\n"
1575 "class Y {}");
Martin Probstad063912017-04-26 12:36:49 +00001576 verifyFormat("class X {\n"
1577 " @property() private isReply = false;\n"
1578 "}\n");
Daniel Jasper3c42dba2015-02-18 17:17:15 +00001579}
1580
Martin Probst805c6162016-04-19 14:59:16 +00001581TEST_F(FormatTestJS, TypeAliases) {
1582 verifyFormat("type X = number;\n"
1583 "class C {}");
1584 verifyFormat("type X<Y> = Z<Y>;");
1585 verifyFormat("type X = {\n"
1586 " y: number\n"
1587 "};\n"
1588 "class C {}");
Martin Probst325ff7c2017-08-14 16:09:08 +00001589 verifyFormat("export type X = {\n"
1590 " a: string,\n"
1591 " b?: string,\n"
1592 "};\n");
Martin Probst805c6162016-04-19 14:59:16 +00001593}
1594
Martin Probstb98ab892017-03-13 07:10:18 +00001595TEST_F(FormatTestJS, TypeInterfaceLineWrapping) {
1596 const FormatStyle &Style = getGoogleJSStyleWithColumns(20);
1597 verifyFormat("type LongTypeIsReallyUnreasonablyLong =\n"
1598 " string;\n",
1599 "type LongTypeIsReallyUnreasonablyLong = string;\n",
1600 Style);
1601 verifyFormat(
1602 "interface AbstractStrategyFactoryProvider {\n"
1603 " a: number\n"
1604 "}\n",
1605 "interface AbstractStrategyFactoryProvider { a: number }\n",
1606 Style);
1607}
1608
Daniel Jasper354aa512015-02-19 16:07:32 +00001609TEST_F(FormatTestJS, Modules) {
1610 verifyFormat("import SomeThing from 'some/module.js';");
1611 verifyFormat("import {X, Y} from 'some/module.js';");
Daniel Jasperefc1a832016-01-07 08:53:35 +00001612 verifyFormat("import a, {X, Y} from 'some/module.js';");
Daniel Jasper8fc7a1e2016-03-22 14:32:20 +00001613 verifyFormat("import {X, Y,} from 'some/module.js';");
Daniel Jasper354aa512015-02-19 16:07:32 +00001614 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
Martin Probst48622092016-06-01 15:22:47 +00001615 // Ensure Automatic Semicolon Insertion does not break on "as\n".
1616 verifyFormat("import {X as myX} from 'm';", "import {X as\n"
1617 " myX} from 'm';");
Daniel Jasper354aa512015-02-19 16:07:32 +00001618 verifyFormat("import * as lib from 'some/module.js';");
Daniel Jasper60948b12015-03-15 13:55:54 +00001619 verifyFormat("var x = {import: 1};\nx.import = 2;");
Daniel Jasperfca735c2015-02-19 16:14:18 +00001620
1621 verifyFormat("export function fn() {\n"
1622 " return 'fn';\n"
1623 "}");
Daniel Jasper20580fd2015-06-11 13:31:45 +00001624 verifyFormat("export function A() {}\n"
1625 "export default function B() {}\n"
1626 "export function C() {}");
Martin Probst053f1aa2016-04-19 14:55:37 +00001627 verifyFormat("export default () => {\n"
1628 " let x = 1;\n"
1629 " return x;\n"
1630 "}");
Daniel Jasper354aa512015-02-19 16:07:32 +00001631 verifyFormat("export const x = 12;");
1632 verifyFormat("export default class X {}");
Daniel Jasperfca735c2015-02-19 16:14:18 +00001633 verifyFormat("export {X, Y} from 'some/module.js';");
Daniel Jasper8fc7a1e2016-03-22 14:32:20 +00001634 verifyFormat("export {X, Y,} from 'some/module.js';");
1635 verifyFormat("export {SomeVeryLongExport as X, "
1636 "SomeOtherVeryLongExport as Y} from 'some/module.js';");
1637 // export without 'from' is wrapped.
1638 verifyFormat("export let someRatherLongVariableName =\n"
1639 " someSurprisinglyLongVariable + someOtherRatherLongVar;");
1640 // ... but not if from is just an identifier.
Daniel Jasperfca735c2015-02-19 16:14:18 +00001641 verifyFormat("export {\n"
Daniel Jasper8fc7a1e2016-03-22 14:32:20 +00001642 " from as from,\n"
Martin Probstdce8e412016-06-22 14:35:14 +00001643 " someSurprisinglyLongVariable as\n"
1644 " from\n"
Daniel Jasper8fc7a1e2016-03-22 14:32:20 +00001645 "};",
1646 getGoogleJSStyleWithColumns(20));
Daniel Jasperfca735c2015-02-19 16:14:18 +00001647 verifyFormat("export class C {\n"
1648 " x: number;\n"
1649 " y: string;\n"
1650 "}");
1651 verifyFormat("export class X { y: number; }");
Daniel Jasper8620d4c2016-01-12 06:24:38 +00001652 verifyFormat("export abstract class X { y: number; }");
Daniel Jasperfca735c2015-02-19 16:14:18 +00001653 verifyFormat("export default class X { y: number }");
1654 verifyFormat("export default function() {\n return 1;\n}");
1655 verifyFormat("export var x = 12;");
Daniel Jasper910807d2015-06-12 04:52:02 +00001656 verifyFormat("class C {}\n"
1657 "export function f() {}\n"
1658 "var v;");
Daniel Jasperfca735c2015-02-19 16:14:18 +00001659 verifyFormat("export var x: number = 12;");
1660 verifyFormat("export const y = {\n"
1661 " a: 1,\n"
1662 " b: 2\n"
1663 "};");
Daniel Jasper216c9cd2015-06-12 05:08:18 +00001664 verifyFormat("export enum Foo {\n"
1665 " BAR,\n"
1666 " // adsdasd\n"
1667 " BAZ\n"
1668 "}");
Daniel Jasperb14f6672016-01-04 15:51:56 +00001669 verifyFormat("export default [\n"
1670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1671 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
1672 "];");
1673 verifyFormat("export default [];");
Daniel Jasper5e271462016-01-04 16:10:36 +00001674 verifyFormat("export default () => {};");
Daniel Jasperf55e4182016-01-11 22:57:40 +00001675 verifyFormat("export interface Foo { foo: number; }\n"
1676 "export class Bar {\n"
Daniel Jasper28d8a5a2016-09-07 23:01:13 +00001677 " blah(): string {\n"
1678 " return this.blah;\n"
1679 " };\n"
Daniel Jasperf55e4182016-01-11 22:57:40 +00001680 "}");
Daniel Jasper354aa512015-02-19 16:07:32 +00001681}
1682
Martin Probst0cd74ee2016-06-13 16:39:50 +00001683TEST_F(FormatTestJS, ImportWrapping) {
1684 verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,"
1685 " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying"
1686 "} from 'some/module.js';");
1687 FormatStyle Style = getGoogleJSStyleWithColumns(80);
1688 Style.JavaScriptWrapImports = true;
1689 verifyFormat("import {\n"
1690 " VeryLongImportsAreAnnoying,\n"
1691 " VeryLongImportsAreAnnoying,\n"
1692 " VeryLongImportsAreAnnoying,\n"
1693 "} from 'some/module.js';",
1694 Style);
1695 verifyFormat("import {\n"
1696 " A,\n"
1697 " A,\n"
1698 "} from 'some/module.js';",
1699 Style);
1700 verifyFormat("export {\n"
1701 " A,\n"
1702 " A,\n"
1703 "} from 'some/module.js';",
1704 Style);
Martin Probst93008f02017-07-18 14:00:19 +00001705 Style.ColumnLimit = 40;
1706 // Using this version of verifyFormat because test::messUp hides the issue.
1707 verifyFormat("import {\n"
1708 " A,\n"
1709 "} from\n"
1710 " 'some/path/longer/than/column/limit/module.js';",
1711 " import { \n"
1712 " A, \n"
1713 " } from\n"
1714 " 'some/path/longer/than/column/limit/module.js' ; ",
1715 Style);
Martin Probst0cd74ee2016-06-13 16:39:50 +00001716}
1717
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001718TEST_F(FormatTestJS, TemplateStrings) {
1719 // Keeps any whitespace/indentation within the template string.
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001720 verifyFormat("var x = `hello\n"
Martin Probst6181da42016-08-25 10:13:21 +00001721 " ${name}\n"
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001722 " !`;",
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001723 "var x = `hello\n"
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001724 " ${ name }\n"
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001725 " !`;");
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001726
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001727 verifyFormat("var x =\n"
1728 " `hello ${world}` >= some();",
1729 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
1730 verifyFormat("var x = `hello ${world}` >= some();",
Daniel Jasper553a5b02015-07-02 13:08:28 +00001731 getGoogleJSStyleWithColumns(35)); // Barely fits.
Martin Probste71b4cb2016-05-17 06:29:29 +00001732 verifyFormat("var x = `hellö ${wörld}` >= söme();",
1733 getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8.
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001734 verifyFormat("var x = `hello\n"
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001735 " ${world}` >=\n"
Daniel Jasper41a2bf72015-12-21 13:52:19 +00001736 " some();",
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001737 "var x =\n"
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001738 " `hello\n"
1739 " ${world}` >= some();",
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001740 getGoogleJSStyleWithColumns(21)); // Barely doesn't fit.
1741 verifyFormat("var x = `hello\n"
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001742 " ${world}` >= some();",
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001743 "var x =\n"
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001744 " `hello\n"
1745 " ${world}` >= some();",
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001746 getGoogleJSStyleWithColumns(22)); // Barely fits.
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001747
Daniel Jasper553a5b02015-07-02 13:08:28 +00001748 verifyFormat("var x =\n"
1749 " `h`;",
1750 getGoogleJSStyleWithColumns(11));
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001751 verifyFormat("var x =\n `multi\n line`;", "var x = `multi\n line`;",
1752 getGoogleJSStyleWithColumns(13));
Daniel Jasper553a5b02015-07-02 13:08:28 +00001753 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1754 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);");
Martin Probste71b4cb2016-05-17 06:29:29 +00001755 // Repro for an obscure width-miscounting issue with template strings.
1756 verifyFormat(
1757 "someLongVariable =\n"
1758 " "
1759 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;",
1760 "someLongVariable = "
1761 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;");
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001762
Daniel Jasperf69b9222015-05-02 08:05:38 +00001763 // Make sure template strings get a proper ColumnWidth assigned, even if they
1764 // are first token in line.
1765 verifyFormat(
1766 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
Daniel Jasper41a2bf72015-12-21 13:52:19 +00001767 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;");
Daniel Jasperf69b9222015-05-02 08:05:38 +00001768
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001769 // Two template strings.
1770 verifyFormat("var x = `hello` == `hello`;");
Daniel Jasper0d6ac272015-04-16 08:20:51 +00001771
1772 // Comments in template strings.
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001773 verifyFormat("var x = `//a`;\n"
Daniel Jasper0d6ac272015-04-16 08:20:51 +00001774 "var y;",
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001775 "var x =\n `//a`;\n"
1776 "var y ;");
1777 verifyFormat("var x = `/*a`;\n"
1778 "var y;",
1779 "var x =\n `/*a`;\n"
1780 "var y;");
Daniel Jasper2ebb0c52015-06-14 07:16:57 +00001781 // Unterminated string literals in a template string.
1782 verifyFormat("var x = `'`; // comment with matching quote '\n"
1783 "var y;");
1784 verifyFormat("var x = `\"`; // comment with matching quote \"\n"
1785 "var y;");
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001786 verifyFormat("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa);",
1787 "it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa) ;",
1788 getGoogleJSStyleWithColumns(40));
Daniel Jasper0d6ac272015-04-16 08:20:51 +00001789 // Backticks in a comment - not a template string.
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001790 verifyFormat("var x = 1 // `/*a`;\n"
1791 " ;",
1792 "var x =\n 1 // `/*a`;\n"
1793 " ;");
1794 verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */");
Daniel Jasper0d6ac272015-04-16 08:20:51 +00001795 // Comment spans multiple template strings.
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001796 verifyFormat("var x = `/*a`;\n"
1797 "var y = ` */ `;",
1798 "var x =\n `/*a`;\n"
1799 "var y =\n ` */ `;");
Daniel Jasper0d6ac272015-04-16 08:20:51 +00001800 // Escaped backtick.
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001801 verifyFormat("var x = ` \\` a`;\n"
1802 "var y;",
1803 "var x = ` \\` a`;\n"
1804 "var y;");
Martin Probst6181da42016-08-25 10:13:21 +00001805 // Escaped dollar.
1806 verifyFormat("var x = ` \\${foo}`;\n");
Daniel Jasperf2019292017-01-31 12:07:35 +00001807
1808 // The token stream can contain two string_literals in sequence, but that
1809 // doesn't mean that they are implicitly concatenated in JavaScript.
1810 verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;");
Daniel Jasper24de6fb2017-01-31 13:03:07 +00001811
1812 // Ensure that scopes are appropriately set around evaluated expressions in
1813 // template strings.
1814 verifyFormat("var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa\n"
1815 " aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;",
1816 "var f = `aaaaaaaaaaaaa:${aaaaaaa. aaaaa} aaaaaaaa\n"
1817 " aaaaaaaaaaaaa:${ aaaaaaa. aaaaa} aaaaaaaa`;");
Daniel Jasperc06f6da2017-02-03 14:32:38 +00001818 verifyFormat("var x = someFunction(`${})`) //\n"
1819 " .oooooooooooooooooon();");
Daniel Jasper98e0b122017-02-20 14:51:16 +00001820 verifyFormat("var x = someFunction(`${aaaa}${\n"
Martin Probstc10d97f2017-08-29 08:30:07 +00001821 " aaaaa( //\n"
1822 " aaaaa)})`);");
Martin Probst6181da42016-08-25 10:13:21 +00001823}
1824
Daniel Jasper3f119412017-01-31 14:39:33 +00001825TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
Daniel Jasper98e0b122017-02-20 14:51:16 +00001826 verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${\n"
Martin Probstc10d97f2017-08-29 08:30:07 +00001827 " aaaaa + //\n"
1828 " bbbb}`;",
Daniel Jasper3f119412017-01-31 14:39:33 +00001829 "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa + //\n"
1830 " bbbb}`;");
Daniel Jasper3f119412017-01-31 14:39:33 +00001831 verifyFormat("var f = `\n"
Daniel Jasper98e0b122017-02-20 14:51:16 +00001832 " aaaaaaaaaaaaaaaaaa: ${\n"
Martin Probstc10d97f2017-08-29 08:30:07 +00001833 " aaaaa + //\n"
1834 " bbbb}`;",
Daniel Jasper3f119412017-01-31 14:39:33 +00001835 "var f = `\n"
1836 " aaaaaaaaaaaaaaaaaa: ${ aaaaa + //\n"
1837 " bbbb }`;");
1838 verifyFormat("var f = `\n"
Daniel Jasper98e0b122017-02-20 14:51:16 +00001839 " aaaaaaaaaaaaaaaaaa: ${\n"
Martin Probstc10d97f2017-08-29 08:30:07 +00001840 " someFunction(\n"
1841 " aaaaa + //\n"
1842 " bbbb)}`;",
Daniel Jasper3f119412017-01-31 14:39:33 +00001843 "var f = `\n"
Daniel Jasper98e0b122017-02-20 14:51:16 +00001844 " aaaaaaaaaaaaaaaaaa: ${someFunction (\n"
Daniel Jasper3f119412017-01-31 14:39:33 +00001845 " aaaaa + //\n"
1846 " bbbb)}`;");
Daniel Jasper98e0b122017-02-20 14:51:16 +00001847
1848 // It might be preferable to wrap before "someFunction".
Daniel Jasper3f119412017-01-31 14:39:33 +00001849 verifyFormat("var f = `\n"
Daniel Jasper98e0b122017-02-20 14:51:16 +00001850 " aaaaaaaaaaaaaaaaaa: ${someFunction({\n"
Martin Probstc10d97f2017-08-29 08:30:07 +00001851 " aaaa: aaaaa,\n"
1852 " bbbb: bbbbb,\n"
1853 "})}`;",
Daniel Jasper3f119412017-01-31 14:39:33 +00001854 "var f = `\n"
Daniel Jasper98e0b122017-02-20 14:51:16 +00001855 " aaaaaaaaaaaaaaaaaa: ${someFunction ({\n"
Daniel Jasper3f119412017-01-31 14:39:33 +00001856 " aaaa: aaaaa,\n"
1857 " bbbb: bbbbb,\n"
1858 " })}`;");
1859}
1860
Martin Probst717f6dc2016-10-21 05:11:38 +00001861TEST_F(FormatTestJS, TemplateStringASI) {
1862 verifyFormat("var x = `hello${world}`;", "var x = `hello${\n"
1863 " world\n"
1864 "}`;");
1865}
1866
Martin Probst6181da42016-08-25 10:13:21 +00001867TEST_F(FormatTestJS, NestedTemplateStrings) {
1868 verifyFormat(
1869 "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;");
1870 verifyFormat("var x = `he${({text: 'll'}.text)}o`;");
Daniel Jasper58209dd2016-09-17 07:20:36 +00001871
1872 // Crashed at some point.
1873 verifyFormat("}");
Martin Probst6181da42016-08-25 10:13:21 +00001874}
1875
1876TEST_F(FormatTestJS, TaggedTemplateStrings) {
1877 verifyFormat("var x = html`<ul>`;");
Martin Probst37a7f912017-07-04 15:30:21 +00001878 verifyFormat("yield `hello`;");
Daniel Jaspera0ef4f32015-02-20 13:47:38 +00001879}
1880
Martin Probst805c6162016-04-19 14:59:16 +00001881TEST_F(FormatTestJS, CastSyntax) {
1882 verifyFormat("var x = <type>foo;");
1883 verifyFormat("var x = foo as type;");
Martin Probstdce8e412016-06-22 14:35:14 +00001884 verifyFormat("let x = (a + b) as\n"
1885 " LongTypeIsLong;",
1886 getGoogleJSStyleWithColumns(20));
Daniel Jasper887a3992016-06-14 13:54:38 +00001887 verifyFormat("foo = <Bar[]>[\n"
1888 " 1, //\n"
1889 " 2\n"
1890 "];");
Martin Probste1e12a72016-08-19 14:35:01 +00001891 verifyFormat("var x = [{x: 1} as type];");
Martin Probsted87d782016-08-22 14:23:30 +00001892 verifyFormat("x = x as [a, b];");
1893 verifyFormat("x = x as {a: string};");
1894 verifyFormat("x = x as (string);");
Martin Probst34ecf422016-09-06 18:55:34 +00001895 verifyFormat("x = x! as (string);");
Daniel Jasper01b87832017-03-01 19:26:12 +00001896 verifyFormat("var x = something.someFunction() as\n"
1897 " something;",
1898 getGoogleJSStyleWithColumns(40));
Martin Probst805c6162016-04-19 14:59:16 +00001899}
Daniel Jasperbc46b932015-03-15 13:59:51 +00001900
1901TEST_F(FormatTestJS, TypeArguments) {
1902 verifyFormat("class X<Y> {}");
1903 verifyFormat("new X<Y>();");
1904 verifyFormat("foo<Y>(a);");
1905 verifyFormat("var x: X<Y>[];");
1906 verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
Daniel Jasper20580fd2015-06-11 13:31:45 +00001907 verifyFormat("function f(a: List<any> = null) {}");
1908 verifyFormat("function f(): List<any> {}");
Daniel Jasper5a3de1d2015-07-03 10:37:23 +00001909 verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n"
1910 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}");
Daniel Jasper6501f7e2015-10-27 12:38:37 +00001911 verifyFormat("function aaaaaaaaaa(\n"
1912 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n"
1913 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n"
Daniel Jasperb2328b12015-07-06 14:07:51 +00001914 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}");
Daniel Jasperbc46b932015-03-15 13:59:51 +00001915}
1916
Daniel Jasper779c66f2015-12-30 08:00:58 +00001917TEST_F(FormatTestJS, UserDefinedTypeGuards) {
1918 verifyFormat(
1919 "function foo(check: Object):\n"
1920 " check is {foo: string, bar: string, baz: string, foobar: string} {\n"
1921 " return 'bar' in check;\n"
1922 "}\n");
1923}
1924
Daniel Jaspera74f5072015-04-13 15:01:40 +00001925TEST_F(FormatTestJS, OptionalTypes) {
Daniel Jasper20580fd2015-06-11 13:31:45 +00001926 verifyFormat("function x(a?: b, c?, d?) {}");
Daniel Jaspera74f5072015-04-13 15:01:40 +00001927 verifyFormat("class X {\n"
1928 " y?: z;\n"
1929 " z?;\n"
1930 "}");
Daniel Jasper9326f912015-05-05 08:40:32 +00001931 verifyFormat("interface X {\n"
1932 " y?(): z;\n"
1933 "}");
Daniel Jasper3e0dcc22015-05-27 05:37:40 +00001934 verifyFormat("constructor({aa}: {\n"
1935 " aa?: string,\n"
1936 " aaaaaaaa?: string,\n"
1937 " aaaaaaaaaaaaaaa?: boolean,\n"
1938 " aaaaaa?: List<string>\n"
Daniel Jasper20580fd2015-06-11 13:31:45 +00001939 "}) {}");
Daniel Jaspera74f5072015-04-13 15:01:40 +00001940}
1941
Daniel Jasperd9309772015-04-13 15:03:30 +00001942TEST_F(FormatTestJS, IndexSignature) {
1943 verifyFormat("var x: {[k: string]: v};");
1944}
1945
Daniel Jasper6501f7e2015-10-27 12:38:37 +00001946TEST_F(FormatTestJS, WrapAfterParen) {
1947 verifyFormat("xxxxxxxxxxx(\n"
1948 " aaa, aaa);",
1949 getGoogleJSStyleWithColumns(20));
1950 verifyFormat("xxxxxxxxxxx(\n"
1951 " aaa, aaa, aaa,\n"
1952 " aaa, aaa, aaa);",
1953 getGoogleJSStyleWithColumns(20));
1954 verifyFormat("xxxxxxxxxxx(\n"
1955 " aaaaaaaaaaaaaaaaaaaaaaaa,\n"
1956 " function(x) {\n"
1957 " y(); //\n"
1958 " });",
1959 getGoogleJSStyleWithColumns(40));
1960 verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
1961 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
1962}
1963
Daniel Jasperd196abb2016-01-08 08:14:58 +00001964TEST_F(FormatTestJS, JSDocAnnotations) {
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001965 verifyFormat("/**\n"
1966 " * @export {this.is.a.long.path.to.a.Type}\n"
1967 " */",
1968 "/**\n"
1969 " * @export {this.is.a.long.path.to.a.Type}\n"
1970 " */",
1971 getGoogleJSStyleWithColumns(20));
Martin Probstbb46a7d2017-02-28 11:08:24 +00001972 verifyFormat("/**\n"
1973 " * @mods {this.is.a.long.path.to.a.Type}\n"
1974 " */",
1975 "/**\n"
1976 " * @mods {this.is.a.long.path.to.a.Type}\n"
1977 " */",
1978 getGoogleJSStyleWithColumns(20));
1979 verifyFormat("/**\n"
1980 " * @param {this.is.a.long.path.to.a.Type}\n"
1981 " */",
1982 "/**\n"
1983 " * @param {this.is.a.long.path.to.a.Type}\n"
1984 " */",
1985 getGoogleJSStyleWithColumns(20));
Martin Probstb4cdb652017-03-13 09:39:23 +00001986 verifyFormat("/**\n"
1987 " * @see http://very/very/long/url/is/long\n"
1988 " */",
1989 "/**\n"
1990 " * @see http://very/very/long/url/is/long\n"
1991 " */",
1992 getGoogleJSStyleWithColumns(20));
Martin Probstbb46a7d2017-02-28 11:08:24 +00001993 verifyFormat(
1994 "/**\n"
1995 " * @param This is a\n"
1996 " * long comment but\n"
1997 " * no type\n"
1998 " */",
1999 "/**\n"
2000 " * @param This is a long comment but no type\n"
2001 " */",
2002 getGoogleJSStyleWithColumns(20));
Krasimir Georgiev17725d82017-03-08 08:55:12 +00002003 // Don't break @param line, but reindent it and reflow unrelated lines.
2004 verifyFormat("{\n"
2005 " /**\n"
2006 " * long long long\n"
2007 " * long\n"
2008 " * @param {this.is.a.long.path.to.a.Type} a\n"
2009 " * long long long\n"
2010 " * long long\n"
2011 " */\n"
2012 " function f(a) {}\n"
2013 "}",
2014 "{\n"
2015 "/**\n"
2016 " * long long long long\n"
2017 " * @param {this.is.a.long.path.to.a.Type} a\n"
2018 " * long long long long\n"
2019 " * long\n"
2020 " */\n"
2021 " function f(a) {}\n"
2022 "}",
2023 getGoogleJSStyleWithColumns(20));
Daniel Jasperd196abb2016-01-08 08:14:58 +00002024}
2025
Daniel Jasperabd1f572016-03-02 22:44:03 +00002026TEST_F(FormatTestJS, RequoteStringsSingle) {
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002027 verifyFormat("var x = 'foo';", "var x = \"foo\";");
2028 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";");
2029 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";");
2030 verifyFormat(
2031 "var x =\n"
2032 " 'foo\\'';",
2033 // Code below is 15 chars wide, doesn't fit into the line with the
2034 // \ escape added.
2035 "var x = \"foo'\";", getGoogleJSStyleWithColumns(15));
Daniel Jasperabd1f572016-03-02 22:44:03 +00002036 // Removes no-longer needed \ escape from ".
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002037 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";");
Daniel Jasperabd1f572016-03-02 22:44:03 +00002038 // Code below fits into 15 chars *after* removing the \ escape.
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002039 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";",
2040 getGoogleJSStyleWithColumns(15));
Martin Probsta1669792016-05-12 11:20:32 +00002041 verifyFormat("// clang-format off\n"
2042 "let x = \"double\";\n"
2043 "// clang-format on\n"
2044 "let x = 'single';\n",
2045 "// clang-format off\n"
2046 "let x = \"double\";\n"
2047 "// clang-format on\n"
2048 "let x = \"single\";\n");
Daniel Jasperabd1f572016-03-02 22:44:03 +00002049}
2050
Martin Probsta9855af2016-09-02 14:29:48 +00002051TEST_F(FormatTestJS, RequoteAndIndent) {
2052 verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n"
2053 " 'double quoted string that needs wrapping');",
2054 "let x = someVeryLongFunctionThatGoesOnAndOn("
2055 "\"double quoted string that needs wrapping\");");
Daniel Jasper496c1992016-09-07 22:48:53 +00002056
2057 verifyFormat("let x =\n"
2058 " 'foo\\'oo';\n"
2059 "let x =\n"
2060 " 'foo\\'oo';",
2061 "let x=\"foo'oo\";\n"
2062 "let x=\"foo'oo\";",
2063 getGoogleJSStyleWithColumns(15));
Martin Probsta9855af2016-09-02 14:29:48 +00002064}
2065
Daniel Jasperabd1f572016-03-02 22:44:03 +00002066TEST_F(FormatTestJS, RequoteStringsDouble) {
2067 FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
2068 DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double;
2069 verifyFormat("var x = \"foo\";", DoubleQuotes);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002070 verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes);
2071 verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes);
Daniel Jasperabd1f572016-03-02 22:44:03 +00002072}
2073
2074TEST_F(FormatTestJS, RequoteStringsLeave) {
2075 FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
2076 LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave;
2077 verifyFormat("var x = \"foo\";", LeaveQuotes);
2078 verifyFormat("var x = 'foo';", LeaveQuotes);
2079}
2080
Martin Probst7ea9b6d2016-05-29 14:41:36 +00002081TEST_F(FormatTestJS, SupportShebangLines) {
2082 verifyFormat("#!/usr/bin/env node\n"
2083 "var x = hello();",
2084 "#!/usr/bin/env node\n"
2085 "var x = hello();");
2086}
2087
Martin Probst0eb40cf2016-06-13 00:49:54 +00002088TEST_F(FormatTestJS, NonNullAssertionOperator) {
2089 verifyFormat("let x = foo!.bar();\n");
2090 verifyFormat("let x = foo ? bar! : baz;\n");
2091 verifyFormat("let x = !foo;\n");
2092 verifyFormat("let x = foo[0]!;\n");
2093 verifyFormat("let x = (foo)!;\n");
Martin Probstab60acb2017-05-22 14:58:26 +00002094 verifyFormat("let x = x(foo!);\n");
2095 verifyFormat(
2096 "a.aaaaaa(a.a!).then(\n"
2097 " x => x(x));\n",
2098 getGoogleJSStyleWithColumns(20));
Daniel Jasper8315ea12016-11-09 14:12:55 +00002099 verifyFormat("let x = foo! - 1;\n");
Martin Probst0eb40cf2016-06-13 00:49:54 +00002100 verifyFormat("let x = {foo: 1}!;\n");
Martin Probst22b8d2692017-03-13 09:14:23 +00002101 verifyFormat(
2102 "let x = hello.foo()!\n"
2103 " .foo()!\n"
2104 " .foo()!\n"
2105 " .foo()!;\n",
2106 getGoogleJSStyleWithColumns(20));
Martin Probst79f9c5f2017-05-12 13:00:33 +00002107 verifyFormat("let x = namespace!;\n");
Martin Probst82b3d902017-05-15 08:15:53 +00002108 verifyFormat("return !!x;\n");
Martin Probst0eb40cf2016-06-13 00:49:54 +00002109}
2110
Martin Probst31d6da72016-06-23 21:51:49 +00002111TEST_F(FormatTestJS, Conditional) {
2112 verifyFormat("y = x ? 1 : 2;");
Daniel Jasperd9d8da22016-07-12 15:45:53 +00002113 verifyFormat("x ? 1 : 2;");
Martin Probst31d6da72016-06-23 21:51:49 +00002114 verifyFormat("class Foo {\n"
2115 " field = true ? 1 : 2;\n"
2116 " method(a = true ? 1 : 2) {}\n"
2117 "}");
2118}
2119
Martin Probstfbbe75b2016-09-18 17:33:51 +00002120TEST_F(FormatTestJS, ImportComments) {
2121 verifyFormat("import {x} from 'x'; // from some location",
2122 getGoogleJSStyleWithColumns(25));
2123 verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10));
Martin Probst2083f312017-05-09 12:45:48 +00002124 verifyFormat("/// <reference path=\"some/location\" />", getGoogleJSStyleWithColumns(10));
Martin Probstfbbe75b2016-09-18 17:33:51 +00002125}
Martin Probst4ef03702017-05-04 15:04:04 +00002126
2127TEST_F(FormatTestJS, Exponentiation) {
2128 verifyFormat("squared = x ** 2;");
2129 verifyFormat("squared **= 2;");
2130}
2131
Martin Probst38423272017-06-06 12:38:29 +00002132TEST_F(FormatTestJS, NestedLiterals) {
2133 FormatStyle FourSpaces = getGoogleJSStyleWithColumns(15);
2134 FourSpaces.IndentWidth = 4;
2135 verifyFormat("var l = [\n"
2136 " [\n"
2137 " 1,\n"
2138 " ],\n"
2139 "];", FourSpaces);
2140 verifyFormat("var l = [\n"
2141 " {\n"
2142 " 1: 1,\n"
2143 " },\n"
2144 "];", FourSpaces);
2145 verifyFormat("someFunction(\n"
2146 " p1,\n"
2147 " [\n"
2148 " 1,\n"
2149 " ],\n"
2150 ");", FourSpaces);
2151 verifyFormat("someFunction(\n"
2152 " p1,\n"
2153 " {\n"
2154 " 1: 1,\n"
2155 " },\n"
2156 ");", FourSpaces);
2157 verifyFormat("var o = {\n"
2158 " 1: 1,\n"
2159 " 2: {\n"
2160 " 3: 3,\n"
2161 " },\n"
2162 "};", FourSpaces);
2163 verifyFormat("var o = {\n"
2164 " 1: 1,\n"
2165 " 2: [\n"
2166 " 3,\n"
2167 " ],\n"
2168 "};", FourSpaces);
2169}
2170
Martin Probst64d31ed2017-08-08 14:52:42 +00002171TEST_F(FormatTestJS, BackslashesInComments) {
2172 verifyFormat("// hello \\\n"
2173 "if (x) foo();\n",
2174 "// hello \\\n"
2175 " if ( x) \n"
2176 " foo();\n");
2177 verifyFormat("/* ignore \\\n"
2178 " */\n"
2179 "if (x) foo();\n",
2180 "/* ignore \\\n"
2181 " */\n"
2182 " if ( x) foo();\n");
2183 verifyFormat("// st \\ art\\\n"
2184 "// comment"
2185 "// continue \\\n"
2186 "formatMe();\n",
2187 "// st \\ art\\\n"
2188 "// comment"
2189 "// continue \\\n"
2190 "formatMe( );\n");
2191}
2192
Alexander Kornienkoab9db512015-06-22 23:07:51 +00002193} // end namespace tooling
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +00002194} // end namespace clang