xref: /llvm-project/clang/unittests/Format/FormatTestJS.cpp (revision 1699eca119e3488a21dfe50cbbfe8f1508b23eb3)
1 //===- 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 
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14 
15 #define DEBUG_TYPE "format-test"
16 
17 namespace clang {
18 namespace format {
19 
20 class FormatTestJS : public ::testing::Test {
21 protected:
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 
34   static std::string format(
35       llvm::StringRef Code,
36       const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
37     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 
53 TEST_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));
82 
83   verifyFormat("var b = a.map((x) => x + 1);");
84   verifyFormat("return ('aaa') in bbbb;");
85 
86   // ES6 spread operator.
87   verifyFormat("someFunction(...a);");
88   verifyFormat("var x = [1, ...a, 2];");
89 }
90 
91 TEST_F(FormatTestJS, UnderstandsAmpAmp) {
92   verifyFormat("e && e.SomeFunction();");
93 }
94 
95 TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
96   verifyFormat("not.and.or.not_eq = 1;");
97 }
98 
99 TEST_F(FormatTestJS, ES6DestructuringAssignment) {
100   verifyFormat("var [a, b, c] = [1, 2, 3];");
101   verifyFormat("var {a, b} = {a: 1, b: 2};");
102 }
103 
104 TEST_F(FormatTestJS, ContainerLiterals) {
105   verifyFormat("var x = {y: function(a) { return a; }};");
106   verifyFormat("return {\n"
107                "  link: function() {\n"
108                "    f();  //\n"
109                "  }\n"
110                "};");
111   verifyFormat("return {\n"
112                "  a: a,\n"
113                "  link: function() {\n"
114                "    f();  //\n"
115                "  }\n"
116                "};");
117   verifyFormat("return {\n"
118                "  a: a,\n"
119                "  link: function() {\n"
120                "    f();  //\n"
121                "  },\n"
122                "  link: function() {\n"
123                "    f();  //\n"
124                "  }\n"
125                "};");
126   verifyFormat("var stuff = {\n"
127                "  // comment for update\n"
128                "  update: false,\n"
129                "  // comment for modules\n"
130                "  modules: false,\n"
131                "  // comment for tasks\n"
132                "  tasks: false\n"
133                "};");
134   verifyFormat("return {\n"
135                "  'finish':\n"
136                "      //\n"
137                "      a\n"
138                "};");
139   verifyFormat("var obj = {\n"
140                "  fooooooooo: function(x) {\n"
141                "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
142                "  }\n"
143                "};");
144   // Simple object literal, as opposed to enum style below.
145   verifyFormat("var obj = {a: 123};");
146   // Enum style top level assignment.
147   verifyFormat("X = {\n  a: 123\n};");
148   verifyFormat("X.Y = {\n  a: 123\n};");
149   verifyFormat("x = foo && {a: 123};");
150 
151   // Arrow functions in object literals.
152   verifyFormat("var x = {y: (a) => { return a; }};");
153   verifyFormat("var x = {y: (a) => a};");
154 
155   // Computed keys.
156   verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};");
157   verifyFormat("var x = {\n"
158                "  [a]: 1,\n"
159                "  b: 2,\n"
160                "  [c]: 3,\n"
161                "};");
162 }
163 
164 TEST_F(FormatTestJS, MethodsInObjectLiterals) {
165   verifyFormat("var o = {\n"
166                "  value: 'test',\n"
167                "  get value() {  // getter\n"
168                "    return this.value;\n"
169                "  }\n"
170                "};");
171   verifyFormat("var o = {\n"
172                "  value: 'test',\n"
173                "  set value(val) {  // setter\n"
174                "    this.value = val;\n"
175                "  }\n"
176                "};");
177   verifyFormat("var o = {\n"
178                "  value: 'test',\n"
179                "  someMethod(val) {  // method\n"
180                "    doSomething(this.value + val);\n"
181                "  }\n"
182                "};");
183   verifyFormat("var o = {\n"
184                "  someMethod(val) {  // method\n"
185                "    doSomething(this.value + val);\n"
186                "  },\n"
187                "  someOtherMethod(val) {  // method\n"
188                "    doSomething(this.value + val);\n"
189                "  }\n"
190                "};");
191 }
192 
193 TEST_F(FormatTestJS, SpacesInContainerLiterals) {
194   verifyFormat("var arr = [1, 2, 3];");
195   verifyFormat("f({a: 1, b: 2, c: 3});");
196 
197   verifyFormat("var object_literal_with_long_name = {\n"
198                "  a: 'aaaaaaaaaaaaaaaaaa',\n"
199                "  b: 'bbbbbbbbbbbbbbbbbb'\n"
200                "};");
201 
202   verifyFormat("f({a: 1, b: 2, c: 3});",
203                getChromiumStyle(FormatStyle::LK_JavaScript));
204   verifyFormat("f({'a': [{}]});");
205 }
206 
207 TEST_F(FormatTestJS, SingleQuoteStrings) {
208   verifyFormat("this.function('', true);");
209 }
210 
211 TEST_F(FormatTestJS, GoogScopes) {
212   verifyFormat("goog.scope(function() {\n"
213                "var x = a.b;\n"
214                "var y = c.d;\n"
215                "});  // goog.scope");
216   verifyFormat("goog.scope(function() {\n"
217                "// test\n"
218                "var x = 0;\n"
219                "// test\n"
220                "});");
221 }
222 
223 TEST_F(FormatTestJS, GoogModules) {
224   verifyFormat("goog.module('this.is.really.absurdly.long');",
225                getGoogleJSStyleWithColumns(40));
226   verifyFormat("goog.require('this.is.really.absurdly.long');",
227                getGoogleJSStyleWithColumns(40));
228   verifyFormat("goog.provide('this.is.really.absurdly.long');",
229                getGoogleJSStyleWithColumns(40));
230   verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
231                getGoogleJSStyleWithColumns(40));
232 
233   // These should be wrapped normally.
234   verifyFormat(
235       "var MyLongClassName =\n"
236       "    goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
237 }
238 
239 TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
240   verifyFormat("function outer1(a, b) {\n"
241                "  function inner1(a, b) { return a; }\n"
242                "  inner1(a, b);\n"
243                "}\n"
244                "function outer2(a, b) {\n"
245                "  function inner2(a, b) { return a; }\n"
246                "  inner2(a, b);\n"
247                "}");
248 }
249 
250 TEST_F(FormatTestJS, ArrayLiterals) {
251   verifyFormat("var aaaaa: List<SomeThing> = [\n"
252                "  new SomeThingAAAAAAAAAAAA(),\n"
253                "  new SomeThingBBBBBBBBB()\n"
254                "];");
255 }
256 
257 TEST_F(FormatTestJS, FunctionLiterals) {
258   verifyFormat("doFoo(function() {});");
259   verifyFormat("doFoo(function() { return 1; });");
260   verifyFormat("var func = function() {\n"
261                "  return 1;\n"
262                "};");
263   verifyFormat("return {\n"
264                "  body: {\n"
265                "    setAttribute: function(key, val) { this[key] = val; },\n"
266                "    getAttribute: function(key) { return this[key]; },\n"
267                "    style: {direction: ''}\n"
268                "  }\n"
269                "};");
270   EXPECT_EQ("abc = xyz ?\n"
271             "          function() {\n"
272             "            return 1;\n"
273             "          } :\n"
274             "          function() {\n"
275             "            return -1;\n"
276             "          };",
277             format("abc=xyz?function(){return 1;}:function(){return -1;};"));
278 
279   verifyFormat("var closure = goog.bind(\n"
280                "    function() {  // comment\n"
281                "      foo();\n"
282                "      bar();\n"
283                "    },\n"
284                "    this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
285                "    arg3IsReallyLongAndNeeedsLineBreaks);");
286   verifyFormat("var closure = goog.bind(function() {  // comment\n"
287                "  foo();\n"
288                "  bar();\n"
289                "}, this);");
290   verifyFormat("return {\n"
291                "  a: 'E',\n"
292                "  b: function() {\n"
293                "    return function() {\n"
294                "      f();  //\n"
295                "    };\n"
296                "  }\n"
297                "};");
298   verifyFormat("{\n"
299                "  var someVariable = function(x) {\n"
300                "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
301                "  };\n"
302                "}");
303   verifyFormat("someLooooooooongFunction(\n"
304                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
305                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
306                "    function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
307                "      // code\n"
308                "    });");
309 
310   verifyFormat("f({a: function() { return 1; }});",
311                getGoogleJSStyleWithColumns(33));
312   verifyFormat("f({\n"
313                "  a: function() { return 1; }\n"
314                "});",
315                getGoogleJSStyleWithColumns(32));
316 
317   verifyFormat("return {\n"
318                "  a: function SomeFunction() {\n"
319                "    // ...\n"
320                "    return 1;\n"
321                "  }\n"
322                "};");
323   verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
324                "    .then(goog.bind(function(aaaaaaaaaaa) {\n"
325                "      someFunction();\n"
326                "      someFunction();\n"
327                "    }, this), aaaaaaaaaaaaaaaaa);");
328 
329   // FIXME: This is not ideal yet.
330   verifyFormat("someFunction(goog.bind(\n"
331                "                 function() {\n"
332                "                   doSomething();\n"
333                "                   doSomething();\n"
334                "                 },\n"
335                "                 this),\n"
336                "             goog.bind(function() {\n"
337                "               doSomething();\n"
338                "               doSomething();\n"
339                "             }, this));");
340 
341   // FIXME: This is bad, we should be wrapping before "function() {".
342   verifyFormat("someFunction(function() {\n"
343                "  doSomething();  // break\n"
344                "})\n"
345                "    .doSomethingElse(\n"
346                "        // break\n"
347                "        );");
348 }
349 
350 TEST_F(FormatTestJS, InliningFunctionLiterals) {
351   FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
352   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
353   verifyFormat("var func = function() {\n"
354                "  return 1;\n"
355                "};",
356                Style);
357   verifyFormat("var func = doSomething(function() { return 1; });", Style);
358   verifyFormat("var outer = function() {\n"
359                "  var inner = function() { return 1; }\n"
360                "};",
361                Style);
362   verifyFormat("function outer1(a, b) {\n"
363                "  function inner1(a, b) { return a; }\n"
364                "}",
365                Style);
366 
367   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
368   verifyFormat("var func = function() { return 1; };", Style);
369   verifyFormat("var func = doSomething(function() { return 1; });", Style);
370   verifyFormat(
371       "var outer = function() { var inner = function() { return 1; } };",
372       Style);
373   verifyFormat("function outer1(a, b) {\n"
374                "  function inner1(a, b) { return a; }\n"
375                "}",
376                Style);
377 
378   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
379   verifyFormat("var func = function() {\n"
380                "  return 1;\n"
381                "};",
382                Style);
383   verifyFormat("var func = doSomething(function() {\n"
384                "  return 1;\n"
385                "});",
386                Style);
387   verifyFormat("var outer = function() {\n"
388                "  var inner = function() {\n"
389                "    return 1;\n"
390                "  }\n"
391                "};",
392                Style);
393   verifyFormat("function outer1(a, b) {\n"
394                "  function inner1(a, b) {\n"
395                "    return a;\n"
396                "  }\n"
397                "}",
398                Style);
399 }
400 
401 TEST_F(FormatTestJS, MultipleFunctionLiterals) {
402   verifyFormat("promise.then(\n"
403                "    function success() {\n"
404                "      doFoo();\n"
405                "      doBar();\n"
406                "    },\n"
407                "    function error() {\n"
408                "      doFoo();\n"
409                "      doBaz();\n"
410                "    },\n"
411                "    []);\n");
412   verifyFormat("promise.then(\n"
413                "    function success() {\n"
414                "      doFoo();\n"
415                "      doBar();\n"
416                "    },\n"
417                "    [],\n"
418                "    function error() {\n"
419                "      doFoo();\n"
420                "      doBaz();\n"
421                "    });\n");
422   // FIXME: Here, we should probably break right after the "(" for consistency.
423   verifyFormat("promise.then([],\n"
424                "             function success() {\n"
425                "               doFoo();\n"
426                "               doBar();\n"
427                "             },\n"
428                "             function error() {\n"
429                "               doFoo();\n"
430                "               doBaz();\n"
431                "             });\n");
432 
433   verifyFormat("getSomeLongPromise()\n"
434                "    .then(function(value) { body(); })\n"
435                "    .thenCatch(function(error) {\n"
436                "      body();\n"
437                "      body();\n"
438                "    });");
439   verifyFormat("getSomeLongPromise()\n"
440                "    .then(function(value) {\n"
441                "      body();\n"
442                "      body();\n"
443                "    })\n"
444                "    .thenCatch(function(error) {\n"
445                "      body();\n"
446                "      body();\n"
447                "    });");
448 
449   verifyFormat("getSomeLongPromise()\n"
450                "    .then(function(value) { body(); })\n"
451                "    .thenCatch(function(error) { body(); });");
452 }
453 
454 TEST_F(FormatTestJS, ArrowFunctions) {
455   verifyFormat("var x = (a) => {\n"
456                "  return a;\n"
457                "};");
458   verifyFormat("var x = (a) => {\n"
459                "  function y() { return 42; }\n"
460                "  return a;\n"
461                "};");
462   verifyFormat("var x = (a: type): {some: type} => {\n"
463                "  return a;\n"
464                "};");
465   verifyFormat("var x = (a) => a;");
466 
467   // FIXME: This is bad, we should be wrapping before "() => {".
468   verifyFormat("someFunction(() => {\n"
469                "  doSomething();  // break\n"
470                "})\n"
471                "    .doSomethingElse(\n"
472                "        // break\n"
473                "        );");
474 }
475 
476 TEST_F(FormatTestJS, ReturnStatements) {
477   verifyFormat("function() {\n"
478                "  return [hello, world];\n"
479                "}");
480 }
481 
482 TEST_F(FormatTestJS, ClosureStyleCasts) {
483   verifyFormat("var x = /** @type {foo} */ (bar);");
484 }
485 
486 TEST_F(FormatTestJS, TryCatch) {
487   verifyFormat("try {\n"
488                "  f();\n"
489                "} catch (e) {\n"
490                "  g();\n"
491                "} finally {\n"
492                "  h();\n"
493                "}");
494 
495   // But, of course, "catch" is a perfectly fine function name in JavaScript.
496   verifyFormat("someObject.catch();");
497   verifyFormat("someObject.new();");
498   verifyFormat("someObject.delete();");
499 }
500 
501 TEST_F(FormatTestJS, StringLiteralConcatenation) {
502   verifyFormat("var literal = 'hello ' +\n"
503                "              'world';");
504 }
505 
506 TEST_F(FormatTestJS, RegexLiteralClassification) {
507   // Regex literals.
508   verifyFormat("var regex = /abc/;");
509   verifyFormat("f(/abc/);");
510   verifyFormat("f(abc, /abc/);");
511   verifyFormat("some_map[/abc/];");
512   verifyFormat("var x = a ? /abc/ : /abc/;");
513   verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
514   verifyFormat("var x = !/abc/.test(y);");
515   verifyFormat("var x = a && /abc/.test(y);");
516   verifyFormat("var x = a || /abc/.test(y);");
517   verifyFormat("var x = a + /abc/.search(y);");
518   verifyFormat("var regexs = {/abc/, /abc/};");
519   verifyFormat("return /abc/;");
520 
521   // Not regex literals.
522   verifyFormat("var a = a / 2 + b / 3;");
523 }
524 
525 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
526   verifyFormat("var regex = /=/;");
527   verifyFormat("var regex = /a*/;");
528   verifyFormat("var regex = /a+/;");
529   verifyFormat("var regex = /a?/;");
530   verifyFormat("var regex = /.a./;");
531   verifyFormat("var regex = /a\\*/;");
532   verifyFormat("var regex = /^a$/;");
533   verifyFormat("var regex = /\\/a/;");
534   verifyFormat("var regex = /(?:x)/;");
535   verifyFormat("var regex = /x(?=y)/;");
536   verifyFormat("var regex = /x(?!y)/;");
537   verifyFormat("var regex = /x|y/;");
538   verifyFormat("var regex = /a{2}/;");
539   verifyFormat("var regex = /a{1,3}/;");
540   verifyFormat("var regex = /[abc]/;");
541   verifyFormat("var regex = /[^abc]/;");
542   verifyFormat("var regex = /[\\b]/;");
543   verifyFormat("var regex = /\\b/;");
544   verifyFormat("var regex = /\\B/;");
545   verifyFormat("var regex = /\\d/;");
546   verifyFormat("var regex = /\\D/;");
547   verifyFormat("var regex = /\\f/;");
548   verifyFormat("var regex = /\\n/;");
549   verifyFormat("var regex = /\\r/;");
550   verifyFormat("var regex = /\\s/;");
551   verifyFormat("var regex = /\\S/;");
552   verifyFormat("var regex = /\\t/;");
553   verifyFormat("var regex = /\\v/;");
554   verifyFormat("var regex = /\\w/;");
555   verifyFormat("var regex = /\\W/;");
556   verifyFormat("var regex = /a(a)\\1/;");
557   verifyFormat("var regex = /\\0/;");
558   verifyFormat("var regex = /\\\\/g;");
559   verifyFormat("var regex = /\\a\\\\/g;");
560   verifyFormat("var regex = /\a\\//g;");
561   verifyFormat("var regex = /a\\//;\n"
562                "var x = 0;");
563   EXPECT_EQ("var regex = /\\/*/;\n"
564             "var x = 0;",
565             format("var regex = /\\/*/;\n"
566                    "var x=0;"));
567 }
568 
569 TEST_F(FormatTestJS, RegexLiteralModifiers) {
570   verifyFormat("var regex = /abc/g;");
571   verifyFormat("var regex = /abc/i;");
572   verifyFormat("var regex = /abc/m;");
573   verifyFormat("var regex = /abc/y;");
574 }
575 
576 TEST_F(FormatTestJS, RegexLiteralLength) {
577   verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
578                getGoogleJSStyleWithColumns(60));
579   verifyFormat("var regex =\n"
580                "    /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
581                getGoogleJSStyleWithColumns(60));
582   verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
583                getGoogleJSStyleWithColumns(50));
584 }
585 
586 TEST_F(FormatTestJS, RegexLiteralExamples) {
587   verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
588 }
589 
590 TEST_F(FormatTestJS, TypeAnnotations) {
591   verifyFormat("var x: string;");
592   verifyFormat("function x(): string {\n  return 'x';\n}");
593   verifyFormat("function x(): {x: string} {\n  return {x: 'x'};\n}");
594   verifyFormat("function x(y: string): string {\n  return 'x';\n}");
595   verifyFormat("for (var y: string in x) {\n  x();\n}");
596   verifyFormat("((a: string, b: number): string => a + b);");
597   verifyFormat("var x: (y: number) => string;");
598   verifyFormat("var x: P<string, (a: number) => string>;");
599   verifyFormat("var x = {y: function(): z { return 1; }};");
600   verifyFormat("var x = {y: function(): {a: number} { return 1; }};");
601 }
602 
603 TEST_F(FormatTestJS, ClassDeclarations) {
604   verifyFormat("class C {\n  x: string = 12;\n}");
605   verifyFormat("class C {\n  x(): string => 12;\n}");
606   verifyFormat("class C {\n  ['x' + 2]: string = 12;\n}");
607   verifyFormat("class C {\n  private x: string = 12;\n}");
608   verifyFormat("class C {\n  private static x: string = 12;\n}");
609   verifyFormat("class C {\n  static x(): string { return 'asd'; }\n}");
610   verifyFormat("class C extends P implements I {}");
611   verifyFormat("class C extends p.P implements i.I {}");
612 }
613 
614 TEST_F(FormatTestJS, InterfaceDeclarations) {
615   verifyFormat("interface I {\n"
616                "  x: string;\n"
617                "}");
618 }
619 
620 TEST_F(FormatTestJS, MetadataAnnotations) {
621   verifyFormat("@A\nclass C {\n}");
622   verifyFormat("@A({arg: 'value'})\nclass C {\n}");
623   verifyFormat("@A\n@B\nclass C {\n}");
624   verifyFormat("class C {\n  @A x: string;\n}");
625   verifyFormat("class C {\n"
626                "  @A\n"
627                "  private x(): string {\n"
628                "    return 'y';\n"
629                "  }\n"
630                "}");
631   verifyFormat("class X {}\n"
632                "class Y {}");
633 }
634 
635 TEST_F(FormatTestJS, Modules) {
636   verifyFormat("import SomeThing from 'some/module.js';");
637   verifyFormat("import {X, Y} from 'some/module.js';");
638   verifyFormat("import {\n"
639                "  VeryLongImportsAreAnnoying,\n"
640                "  VeryLongImportsAreAnnoying,\n"
641                "  VeryLongImportsAreAnnoying,\n"
642                "  VeryLongImportsAreAnnoying\n"
643                "} from 'some/module.js';");
644   verifyFormat("import {\n"
645                "  X,\n"
646                "  Y,\n"
647                "} from 'some/module.js';");
648   verifyFormat("import {\n"
649                "  X,\n"
650                "  Y,\n"
651                "} from 'some/long/module.js';",
652                getGoogleJSStyleWithColumns(20));
653   verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
654   verifyFormat("import * as lib from 'some/module.js';");
655   verifyFormat("var x = {import: 1};\nx.import = 2;");
656 
657   verifyFormat("export function fn() {\n"
658                "  return 'fn';\n"
659                "}");
660   verifyFormat("export function A() {\n"
661                "}\n"
662                "export default function B() {\n"
663                "}\n"
664                "export function C() {\n"
665                "}");
666   verifyFormat("export const x = 12;");
667   verifyFormat("export default class X {}");
668   verifyFormat("export {X, Y} from 'some/module.js';");
669   verifyFormat("export {\n"
670                "  X,\n"
671                "  Y,\n"
672                "} from 'some/module.js';");
673   verifyFormat("export class C {\n"
674                "  x: number;\n"
675                "  y: string;\n"
676                "}");
677   verifyFormat("export class X { y: number; }");
678   verifyFormat("export default class X { y: number }");
679   verifyFormat("export default function() {\n  return 1;\n}");
680   verifyFormat("export var x = 12;");
681   verifyFormat("export var x: number = 12;");
682   verifyFormat("export const y = {\n"
683                "  a: 1,\n"
684                "  b: 2\n"
685                "};");
686 }
687 
688 TEST_F(FormatTestJS, TemplateStrings) {
689   // Keeps any whitespace/indentation within the template string.
690   EXPECT_EQ("var x = `hello\n"
691             "     ${  name    }\n"
692             "  !`;",
693             format("var x    =    `hello\n"
694                    "     ${  name    }\n"
695                    "  !`;"));
696 
697   // FIXME: +1 / -1 offsets are to work around clang-format miscalculating
698   // widths for unknown tokens that are not whitespace (e.g. '`'). Remove when
699   // the code is corrected.
700 
701   verifyFormat("var x =\n"
702                "    `hello ${world}` >= some();",
703                getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
704   verifyFormat("var x = `hello ${world}` >= some();",
705                getGoogleJSStyleWithColumns(35 + 1)); // Barely fits.
706   EXPECT_EQ("var x = `hello\n"
707             "  ${world}` >=\n"
708             "        some();",
709             format("var x =\n"
710                    "    `hello\n"
711                    "  ${world}` >= some();",
712                    getGoogleJSStyleWithColumns(21))); // Barely doesn't fit.
713   EXPECT_EQ("var x = `hello\n"
714             "  ${world}` >= some();",
715             format("var x =\n"
716                    "    `hello\n"
717                    "  ${world}` >= some();",
718                    getGoogleJSStyleWithColumns(22))); // Barely fits.
719 
720   verifyFormat("var x =\n    `h`;", getGoogleJSStyleWithColumns(13 - 1));
721   EXPECT_EQ(
722       "var x =\n    `multi\n  line`;",
723       format("var x = `multi\n  line`;", getGoogleJSStyleWithColumns(14 - 1)));
724 
725   // Make sure template strings get a proper ColumnWidth assigned, even if they
726   // are first token in line.
727   verifyFormat(
728       "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
729       "        `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;");
730 
731   // Two template strings.
732   verifyFormat("var x = `hello` == `hello`;");
733 
734   // Comments in template strings.
735   EXPECT_EQ("var x = `//a`;\n"
736             "var y;",
737             format("var x =\n `//a`;\n"
738                    "var y  ;"));
739   EXPECT_EQ("var x = `/*a`;\n"
740             "var y;",
741             format("var x =\n `/*a`;\n"
742                    "var y;"));
743   // Backticks in a comment - not a template string.
744   EXPECT_EQ("var x = 1  // `/*a`;\n"
745             "    ;",
746             format("var x =\n 1  // `/*a`;\n"
747                    "    ;"));
748   EXPECT_EQ("/* ` */ var x = 1; /* ` */",
749             format("/* ` */ var x\n= 1; /* ` */"));
750   // Comment spans multiple template strings.
751   EXPECT_EQ("var x = `/*a`;\n"
752             "var y = ` */ `;",
753             format("var x =\n `/*a`;\n"
754                    "var y =\n ` */ `;"));
755   // Escaped backtick.
756   EXPECT_EQ("var x = ` \\` a`;\n"
757             "var y;",
758             format("var x = ` \\` a`;\n"
759                    "var y;"));
760 }
761 
762 TEST_F(FormatTestJS, CastSyntax) {
763   verifyFormat("var x = <type>foo;");
764 }
765 
766 TEST_F(FormatTestJS, TypeArguments) {
767   verifyFormat("class X<Y> {}");
768   verifyFormat("new X<Y>();");
769   verifyFormat("foo<Y>(a);");
770   verifyFormat("var x: X<Y>[];");
771   verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
772   verifyFormat("function f(a: List<any> = null) {\n}");
773   verifyFormat("function f(): List<any> {\n}");
774 }
775 
776 TEST_F(FormatTestJS, OptionalTypes) {
777   verifyFormat("function x(a?: b, c?, d?) {\n}");
778   verifyFormat("class X {\n"
779                "  y?: z;\n"
780                "  z?;\n"
781                "}");
782   verifyFormat("interface X {\n"
783                "  y?(): z;\n"
784                "}");
785   verifyFormat("x ? 1 : 2;");
786   verifyFormat("constructor({aa}: {\n"
787                "  aa?: string,\n"
788                "  aaaaaaaa?: string,\n"
789                "  aaaaaaaaaaaaaaa?: boolean,\n"
790                "  aaaaaa?: List<string>\n"
791                "}) {\n"
792                "}");
793 }
794 
795 TEST_F(FormatTestJS, IndexSignature) {
796   verifyFormat("var x: {[k: string]: v};");
797 }
798 
799 } // end namespace tooling
800 } // end namespace clang
801