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