blob: 5707024544759f321a707f39e987f97972220579 [file] [log] [blame]
Stephen Hines651f13c2014-04-23 16:59:28 -07001//===- unittest/Format/FormatTestJS.cpp - Formatting unit tests for JS ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Stephen Hines651f13c2014-04-23 16:59:28 -070010#include "FormatTestUtils.h"
11#include "clang/Format/Format.h"
12#include "llvm/Support/Debug.h"
13#include "gtest/gtest.h"
14
Stephen Hines6bcf27b2014-05-29 04:14:42 -070015#define DEBUG_TYPE "format-test"
16
Stephen Hines651f13c2014-04-23 16:59:28 -070017namespace clang {
18namespace format {
19
20class FormatTestJS : public ::testing::Test {
21protected:
22 static std::string format(llvm::StringRef Code, unsigned Offset,
23 unsigned Length, const FormatStyle &Style) {
24 DEBUG(llvm::errs() << "---\n");
25 DEBUG(llvm::errs() << Code << "\n\n");
26 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27 tooling::Replacements Replaces = reformat(Style, Code, Ranges);
28 std::string Result = applyAllReplacements(Code, Replaces);
29 EXPECT_NE("", Result);
30 DEBUG(llvm::errs() << "\n" << Result << "\n\n");
31 return Result;
32 }
33
Stephen Hines6bcf27b2014-05-29 04:14:42 -070034 static std::string format(
35 llvm::StringRef Code,
36 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
Stephen Hines651f13c2014-04-23 16:59:28 -070037 return format(Code, 0, Code.size(), Style);
38 }
39
40 static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
41 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
42 Style.ColumnLimit = ColumnLimit;
43 return Style;
44 }
45
46 static void verifyFormat(
47 llvm::StringRef Code,
48 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
49 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
50 }
51};
52
53TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
54 verifyFormat("a == = b;");
55 verifyFormat("a != = b;");
56
57 verifyFormat("a === b;");
58 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10));
59 verifyFormat("a !== b;");
60 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10));
61 verifyFormat("if (a + b + c +\n"
62 " d !==\n"
63 " e + f + g)\n"
64 " q();",
65 getGoogleJSStyleWithColumns(20));
66
67 verifyFormat("a >> >= b;");
68
69 verifyFormat("a >>> b;");
70 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10));
71 verifyFormat("a >>>= b;");
72 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10));
73 verifyFormat("if (a + b + c +\n"
74 " d >>>\n"
75 " e + f + g)\n"
76 " q();",
77 getGoogleJSStyleWithColumns(20));
78 verifyFormat("var x = aaaaaaaaaa ?\n"
79 " bbbbbb :\n"
80 " ccc;",
81 getGoogleJSStyleWithColumns(20));
Stephen Hines6bcf27b2014-05-29 04:14:42 -070082
83 verifyFormat("var b = a.map((x) => x + 1);");
Stephen Hines176edba2014-12-01 14:53:08 -080084 verifyFormat("return ('aaa') in bbbb;");
85}
86
87TEST_F(FormatTestJS, UnderstandsAmpAmp) {
88 verifyFormat("e && e.SomeFunction();");
89}
90
91TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
92 verifyFormat("not.and.or.not_eq = 1;");
Stephen Hines6bcf27b2014-05-29 04:14:42 -070093}
94
95TEST_F(FormatTestJS, ES6DestructuringAssignment) {
96 verifyFormat("var [a, b, c] = [1, 2, 3];");
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070097 verifyFormat("var {a, b} = {a: 1, b: 2};");
Stephen Hines651f13c2014-04-23 16:59:28 -070098}
99
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700100TEST_F(FormatTestJS, ContainerLiterals) {
101 verifyFormat("return {\n"
102 " link: function() {\n"
103 " f(); //\n"
104 " }\n"
105 "};");
106 verifyFormat("return {\n"
107 " a: a,\n"
108 " link: function() {\n"
109 " f(); //\n"
110 " }\n"
111 "};");
112 verifyFormat("return {\n"
113 " a: a,\n"
Stephen Hines176edba2014-12-01 14:53:08 -0800114 " link: function() {\n"
115 " f(); //\n"
116 " },\n"
117 " link: function() {\n"
118 " f(); //\n"
119 " }\n"
120 "};");
121 verifyFormat("var stuff = {\n"
122 " // comment for update\n"
123 " update: false,\n"
124 " // comment for modules\n"
125 " modules: false,\n"
126 " // comment for tasks\n"
127 " tasks: false\n"
128 "};");
129 verifyFormat("return {\n"
130 " 'finish':\n"
131 " //\n"
132 " a\n"
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700133 "};");
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700134 verifyFormat("var obj = {\n"
135 " fooooooooo: function(x) {\n"
136 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
137 " }\n"
138 "};");
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700139 // 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};");
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700145}
146
Stephen Hines651f13c2014-04-23 16:59:28 -0700147TEST_F(FormatTestJS, SpacesInContainerLiterals) {
148 verifyFormat("var arr = [1, 2, 3];");
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700149 verifyFormat("f({a: 1, b: 2, c: 3});");
Stephen Hines651f13c2014-04-23 16:59:28 -0700150
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700151 verifyFormat("var object_literal_with_long_name = {\n"
152 " a: 'aaaaaaaaaaaaaaaaaa',\n"
153 " b: 'bbbbbbbbbbbbbbbbbb'\n"
154 "};");
155
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700156 verifyFormat("f({a: 1, b: 2, c: 3});",
Stephen Hines651f13c2014-04-23 16:59:28 -0700157 getChromiumStyle(FormatStyle::LK_JavaScript));
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700158 verifyFormat("f({'a': [{}]});");
Stephen Hines651f13c2014-04-23 16:59:28 -0700159}
160
161TEST_F(FormatTestJS, SingleQuoteStrings) {
162 verifyFormat("this.function('', true);");
163}
164
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700165TEST_F(FormatTestJS, GoogScopes) {
166 verifyFormat("goog.scope(function() {\n"
167 "var x = a.b;\n"
168 "var y = c.d;\n"
169 "}); // goog.scope");
170}
171
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700172TEST_F(FormatTestJS, GoogModules) {
173 verifyFormat("goog.module('this.is.really.absurdly.long');",
174 getGoogleJSStyleWithColumns(40));
175 verifyFormat("goog.require('this.is.really.absurdly.long');",
176 getGoogleJSStyleWithColumns(40));
177 verifyFormat("goog.provide('this.is.really.absurdly.long');",
178 getGoogleJSStyleWithColumns(40));
179 verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
180 getGoogleJSStyleWithColumns(40));
181
182 // These should be wrapped normally.
183 verifyFormat(
184 "var MyLongClassName =\n"
185 " goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
186}
187
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700188TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
189 verifyFormat("function outer1(a, b) {\n"
190 " function inner1(a, b) { return a; }\n"
191 " inner1(a, b);\n"
192 "}\n"
193 "function outer2(a, b) {\n"
194 " function inner2(a, b) { return a; }\n"
195 " inner2(a, b);\n"
196 "}");
197}
198
199TEST_F(FormatTestJS, FunctionLiterals) {
Stephen Hines176edba2014-12-01 14:53:08 -0800200 verifyFormat("doFoo(function() {});");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700201 verifyFormat("doFoo(function() { return 1; });");
Stephen Hines176edba2014-12-01 14:53:08 -0800202 verifyFormat("var func = function() {\n"
203 " return 1;\n"
204 "};");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700205 verifyFormat("return {\n"
206 " body: {\n"
207 " setAttribute: function(key, val) { this[key] = val; },\n"
208 " getAttribute: function(key) { return this[key]; },\n"
209 " style: {direction: ''}\n"
210 " }\n"
211 "};");
Stephen Hines176edba2014-12-01 14:53:08 -0800212 EXPECT_EQ("abc = xyz ?\n"
213 " function() {\n"
214 " return 1;\n"
215 " } :\n"
216 " function() {\n"
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700217 " return -1;\n"
218 " };",
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700219 format("abc=xyz?function(){return 1;}:function(){return -1;};"));
220
221 verifyFormat("var closure = goog.bind(\n"
222 " function() { // comment\n"
223 " foo();\n"
224 " bar();\n"
225 " },\n"
226 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
227 " arg3IsReallyLongAndNeeedsLineBreaks);");
228 verifyFormat("var closure = goog.bind(function() { // comment\n"
229 " foo();\n"
230 " bar();\n"
231 "}, this);");
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700232 verifyFormat("return {\n"
233 " a: 'E',\n"
234 " b: function() {\n"
235 " return function() {\n"
236 " f(); //\n"
237 " };\n"
238 " }\n"
239 "};");
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700240 verifyFormat("{\n"
241 " var someVariable = function(x) {\n"
242 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
243 " };\n"
244 "}");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700245
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700246 verifyFormat("f({a: function() { return 1; }});",
247 getGoogleJSStyleWithColumns(33));
248 verifyFormat("f({\n"
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700249 " a: function() { return 1; }\n"
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700250 "});",
251 getGoogleJSStyleWithColumns(32));
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700252
253 verifyFormat("return {\n"
254 " a: function SomeFunction() {\n"
255 " // ...\n"
256 " return 1;\n"
257 " }\n"
258 "};");
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700259 verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
260 " .then(goog.bind(function(aaaaaaaaaaa) {\n"
261 " someFunction();\n"
262 " someFunction();\n"
263 " }, this), aaaaaaaaaaaaaaaaa);");
264
265 // FIXME: This is not ideal yet.
266 verifyFormat("someFunction(goog.bind(\n"
267 " function() {\n"
268 " doSomething();\n"
269 " doSomething();\n"
270 " },\n"
271 " this),\n"
272 " goog.bind(function() {\n"
273 " doSomething();\n"
274 " doSomething();\n"
275 " }, this));");
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700276}
277
Stephen Hines176edba2014-12-01 14:53:08 -0800278TEST_F(FormatTestJS, InliningFunctionLiterals) {
279 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
280 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
281 verifyFormat("var func = function() {\n"
282 " return 1;\n"
283 "};",
284 Style);
285 verifyFormat("var func = doSomething(function() { return 1; });", Style);
286 verifyFormat("var outer = function() {\n"
287 " var inner = function() { return 1; }\n"
288 "};",
289 Style);
290 verifyFormat("function outer1(a, b) {\n"
291 " function inner1(a, b) { return a; }\n"
292 "}",
293 Style);
294
295 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
296 verifyFormat("var func = function() { return 1; };", Style);
297 verifyFormat("var func = doSomething(function() { return 1; });", Style);
298 verifyFormat(
299 "var outer = function() { var inner = function() { return 1; } };",
300 Style);
301 verifyFormat("function outer1(a, b) {\n"
302 " function inner1(a, b) { return a; }\n"
303 "}",
304 Style);
305
306 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
307 verifyFormat("var func = function() {\n"
308 " return 1;\n"
309 "};",
310 Style);
311 verifyFormat("var func = doSomething(function() {\n"
312 " return 1;\n"
313 "});",
314 Style);
315 verifyFormat("var outer = function() {\n"
316 " var inner = function() {\n"
317 " return 1;\n"
318 " }\n"
319 "};",
320 Style);
321 verifyFormat("function outer1(a, b) {\n"
322 " function inner1(a, b) {\n"
323 " return a;\n"
324 " }\n"
325 "}",
326 Style);
327}
328
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700329TEST_F(FormatTestJS, MultipleFunctionLiterals) {
330 verifyFormat("promise.then(\n"
331 " function success() {\n"
332 " doFoo();\n"
333 " doBar();\n"
334 " },\n"
335 " function error() {\n"
336 " doFoo();\n"
337 " doBaz();\n"
338 " },\n"
339 " []);\n");
340 verifyFormat("promise.then(\n"
341 " function success() {\n"
342 " doFoo();\n"
343 " doBar();\n"
344 " },\n"
345 " [],\n"
346 " function error() {\n"
347 " doFoo();\n"
348 " doBaz();\n"
349 " });\n");
350 // FIXME: Here, we should probably break right after the "(" for consistency.
351 verifyFormat("promise.then([],\n"
352 " function success() {\n"
353 " doFoo();\n"
354 " doBar();\n"
355 " },\n"
356 " function error() {\n"
357 " doFoo();\n"
358 " doBaz();\n"
359 " });\n");
Stephen Hines176edba2014-12-01 14:53:08 -0800360
361 verifyFormat("getSomeLongPromise()\n"
362 " .then(function(value) { body(); })\n"
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700363 " .thenCatch(function(error) {\n"
364 " body();\n"
365 " body();\n"
366 " });");
Stephen Hines176edba2014-12-01 14:53:08 -0800367 verifyFormat("getSomeLongPromise()\n"
368 " .then(function(value) {\n"
369 " body();\n"
370 " body();\n"
371 " })\n"
372 " .thenCatch(function(error) {\n"
373 " body();\n"
374 " body();\n"
375 " });");
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700376
377 // FIXME: This is bad, but it used to be formatted correctly by accident.
378 verifyFormat("getSomeLongPromise().then(function(value) {\n"
379 " body();\n"
380 "}).thenCatch(function(error) { body(); });");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700381}
382
383TEST_F(FormatTestJS, ReturnStatements) {
Stephen Hines176edba2014-12-01 14:53:08 -0800384 verifyFormat("function() {\n"
385 " return [hello, world];\n"
386 "}");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700387}
388
389TEST_F(FormatTestJS, ClosureStyleComments) {
390 verifyFormat("var x = /** @type {foo} */ (bar);");
391}
392
393TEST_F(FormatTestJS, TryCatch) {
394 verifyFormat("try {\n"
395 " f();\n"
396 "} catch (e) {\n"
397 " g();\n"
398 "} finally {\n"
399 " h();\n"
400 "}");
Stephen Hines176edba2014-12-01 14:53:08 -0800401
402 // But, of course, "catch" is a perfectly fine function name in JavaScript.
403 verifyFormat("someObject.catch();");
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700404 verifyFormat("someObject.new();");
405 verifyFormat("someObject.delete();");
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700406}
407
408TEST_F(FormatTestJS, StringLiteralConcatenation) {
409 verifyFormat("var literal = 'hello ' +\n"
410 " 'world';");
411}
412
413TEST_F(FormatTestJS, RegexLiteralClassification) {
414 // Regex literals.
415 verifyFormat("var regex = /abc/;");
416 verifyFormat("f(/abc/);");
417 verifyFormat("f(abc, /abc/);");
418 verifyFormat("some_map[/abc/];");
419 verifyFormat("var x = a ? /abc/ : /abc/;");
420 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
421 verifyFormat("var x = !/abc/.test(y);");
422 verifyFormat("var x = a && /abc/.test(y);");
423 verifyFormat("var x = a || /abc/.test(y);");
424 verifyFormat("var x = a + /abc/.search(y);");
425 verifyFormat("var regexs = {/abc/, /abc/};");
426 verifyFormat("return /abc/;");
427
428 // Not regex literals.
429 verifyFormat("var a = a / 2 + b / 3;");
430}
431
432TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
433 verifyFormat("var regex = /a*/;");
434 verifyFormat("var regex = /a+/;");
435 verifyFormat("var regex = /a?/;");
436 verifyFormat("var regex = /.a./;");
437 verifyFormat("var regex = /a\\*/;");
438 verifyFormat("var regex = /^a$/;");
439 verifyFormat("var regex = /\\/a/;");
440 verifyFormat("var regex = /(?:x)/;");
441 verifyFormat("var regex = /x(?=y)/;");
442 verifyFormat("var regex = /x(?!y)/;");
443 verifyFormat("var regex = /x|y/;");
444 verifyFormat("var regex = /a{2}/;");
445 verifyFormat("var regex = /a{1,3}/;");
446 verifyFormat("var regex = /[abc]/;");
447 verifyFormat("var regex = /[^abc]/;");
448 verifyFormat("var regex = /[\\b]/;");
449 verifyFormat("var regex = /\\b/;");
450 verifyFormat("var regex = /\\B/;");
451 verifyFormat("var regex = /\\d/;");
452 verifyFormat("var regex = /\\D/;");
453 verifyFormat("var regex = /\\f/;");
454 verifyFormat("var regex = /\\n/;");
455 verifyFormat("var regex = /\\r/;");
456 verifyFormat("var regex = /\\s/;");
457 verifyFormat("var regex = /\\S/;");
458 verifyFormat("var regex = /\\t/;");
459 verifyFormat("var regex = /\\v/;");
460 verifyFormat("var regex = /\\w/;");
461 verifyFormat("var regex = /\\W/;");
462 verifyFormat("var regex = /a(a)\\1/;");
463 verifyFormat("var regex = /\\0/;");
464 verifyFormat("var regex = /\\\\/g;");
465 verifyFormat("var regex = /\\a\\\\/g;");
466 verifyFormat("var regex = /\a\\//g;");
Stephen Hines176edba2014-12-01 14:53:08 -0800467 verifyFormat("var regex = /a\\//;\n"
468 "var x = 0;");
469 EXPECT_EQ("var regex = /\\/*/;\n"
470 "var x = 0;",
471 format("var regex = /\\/*/;\n"
472 "var x=0;"));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700473}
474
475TEST_F(FormatTestJS, RegexLiteralModifiers) {
476 verifyFormat("var regex = /abc/g;");
477 verifyFormat("var regex = /abc/i;");
478 verifyFormat("var regex = /abc/m;");
479 verifyFormat("var regex = /abc/y;");
480}
481
482TEST_F(FormatTestJS, RegexLiteralLength) {
483 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
484 getGoogleJSStyleWithColumns(60));
485 verifyFormat("var regex =\n"
486 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
487 getGoogleJSStyleWithColumns(60));
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700488 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
489 getGoogleJSStyleWithColumns(50));
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700490}
491
492TEST_F(FormatTestJS, RegexLiteralExamples) {
493 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
494}
495
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700496TEST_F(FormatTestJS, TypeAnnotations) {
497 verifyFormat("var x: string;");
498 verifyFormat("function x(): string {\n return 'x';\n}");
499 verifyFormat("function x(y: string): string {\n return 'x';\n}");
500 verifyFormat("for (var y: string in x) {\n x();\n}");
501 verifyFormat("((a: string, b: number): string => a + b);");
502 verifyFormat("var x: (y: number) => string;");
503 verifyFormat("var x: P<string, (a: number) => string>;");
504}
505
506TEST_F(FormatTestJS, ClassDeclarations) {
507 verifyFormat("class C {\n x: string = 12;\n}");
508 verifyFormat("class C {\n x(): string => 12;\n}");
509 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}");
510 verifyFormat("class C {\n private x: string = 12;\n}");
511 verifyFormat("class C {\n private static x: string = 12;\n}");
512 verifyFormat("class C {\n static x(): string { return 'asd'; }\n}");
513 verifyFormat("class C extends P implements I {}");
514}
515
516TEST_F(FormatTestJS, MetadataAnnotations) {
517 verifyFormat("@A\nclass C {\n}");
518 verifyFormat("@A({arg: 'value'})\nclass C {\n}");
519 verifyFormat("@A\n@B\nclass C {\n}");
520 verifyFormat("class C {\n @A x: string;\n}");
521 verifyFormat("class C {\n"
522 " @A\n"
523 " private x(): string {\n"
524 " return 'y';\n"
525 " }\n"
526 "}");
527 verifyFormat("class X {}\n"
528 "class Y {}");
529}
530
531TEST_F(FormatTestJS, Modules) {
532 verifyFormat("import SomeThing from 'some/module.js';");
533 verifyFormat("import {X, Y} from 'some/module.js';");
534 verifyFormat("import {\n"
535 " VeryLongImportsAreAnnoying,\n"
536 " VeryLongImportsAreAnnoying,\n"
537 " VeryLongImportsAreAnnoying,\n"
538 " VeryLongImportsAreAnnoying\n"
539 "} from 'some/module.js';");
540 verifyFormat("import {\n"
541 " X,\n"
542 " Y,\n"
543 "} from 'some/module.js';");
544 verifyFormat("import {\n"
545 " X,\n"
546 " Y,\n"
547 "} from 'some/long/module.js';",
548 getGoogleJSStyleWithColumns(20));
549 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
550 verifyFormat("import * as lib from 'some/module.js';");
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700551 verifyFormat("var x = {import: 1};\nx.import = 2;");
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700552
553 verifyFormat("export function fn() {\n"
554 " return 'fn';\n"
555 "}");
556 verifyFormat("export const x = 12;");
557 verifyFormat("export default class X {}");
558 verifyFormat("export {X, Y} from 'some/module.js';");
559 verifyFormat("export {\n"
560 " X,\n"
561 " Y,\n"
562 "} from 'some/module.js';");
563 verifyFormat("export class C {\n"
564 " x: number;\n"
565 " y: string;\n"
566 "}");
567 verifyFormat("export class X { y: number; }");
568 verifyFormat("export default class X { y: number }");
569 verifyFormat("export default function() {\n return 1;\n}");
570 verifyFormat("export var x = 12;");
571 verifyFormat("export var x: number = 12;");
572 verifyFormat("export const y = {\n"
573 " a: 1,\n"
574 " b: 2\n"
575 "};");
576}
577
578TEST_F(FormatTestJS, TemplateStrings) {
579 // Keeps any whitespace/indentation within the template string.
580 EXPECT_EQ("var x = `hello\n"
581 " ${ name }\n"
582 " !`;",
583 format("var x = `hello\n"
584 " ${ name }\n"
585 " !`;"));
586
587 // FIXME: +1 / -1 offsets are to work around clang-format miscalculating
588 // widths for unknown tokens that are not whitespace (e.g. '`'). Remove when
589 // the code is corrected.
590
591 verifyFormat("var x =\n"
592 " `hello ${world}` >= some();",
593 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
594 verifyFormat("var x = `hello ${world}` >= some();",
595 getGoogleJSStyleWithColumns(35 + 1)); // Barely fits.
596 EXPECT_EQ("var x = `hello\n"
597 " ${world}` >=\n"
598 " some();",
599 format("var x =\n"
600 " `hello\n"
601 " ${world}` >= some();",
602 getGoogleJSStyleWithColumns(21))); // Barely doesn't fit.
603 EXPECT_EQ("var x = `hello\n"
604 " ${world}` >= some();",
605 format("var x =\n"
606 " `hello\n"
607 " ${world}` >= some();",
608 getGoogleJSStyleWithColumns(22))); // Barely fits.
609
610 verifyFormat("var x =\n `h`;", getGoogleJSStyleWithColumns(13 - 1));
611 EXPECT_EQ(
612 "var x =\n `multi\n line`;",
613 format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(14 - 1)));
614
615 // Two template strings.
616 verifyFormat("var x = `hello` == `hello`;");
617}
618
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700619TEST_F(FormatTestJS, CastSyntax) {
620 verifyFormat("var x = <type>foo;");
621}
622
623TEST_F(FormatTestJS, TypeArguments) {
624 verifyFormat("class X<Y> {}");
625 verifyFormat("new X<Y>();");
626 verifyFormat("foo<Y>(a);");
627 verifyFormat("var x: X<Y>[];");
628 verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
629}
630
Stephen Hines651f13c2014-04-23 16:59:28 -0700631} // end namespace tooling
632} // end namespace clang