xref: /llvm-project/clang/unittests/Format/FormatTestJS.cpp (revision 34ecf42bffe57ed1f4c45ac1ff0a3be2d5ed0dd1)
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     bool IncompleteFormat = false;
28     tooling::Replacements Replaces =
29         reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
30     EXPECT_FALSE(IncompleteFormat);
31     auto Result = applyAllReplacements(Code, Replaces);
32     EXPECT_TRUE(static_cast<bool>(Result));
33     DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
34     return *Result;
35   }
36 
37   static std::string format(
38       llvm::StringRef Code,
39       const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
40     return format(Code, 0, Code.size(), Style);
41   }
42 
43   static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
44     FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
45     Style.ColumnLimit = ColumnLimit;
46     return Style;
47   }
48 
49   static void verifyFormat(
50       llvm::StringRef Code,
51       const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
52     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;
62   }
63 };
64 
65 TEST_F(FormatTestJS, BlockComments) {
66   verifyFormat("/* aaaaaaaaaaaaa */ aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
67                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
68 }
69 
70 TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
71   verifyFormat("a == = b;");
72   verifyFormat("a != = b;");
73 
74   verifyFormat("a === b;");
75   verifyFormat("aaaaaaa ===\n    b;", getGoogleJSStyleWithColumns(10));
76   verifyFormat("a !== b;");
77   verifyFormat("aaaaaaa !==\n    b;", getGoogleJSStyleWithColumns(10));
78   verifyFormat("if (a + b + c +\n"
79                "        d !==\n"
80                "    e + f + g)\n"
81                "  q();",
82                getGoogleJSStyleWithColumns(20));
83 
84   verifyFormat("a >> >= b;");
85 
86   verifyFormat("a >>> b;");
87   verifyFormat("aaaaaaa >>>\n    b;", getGoogleJSStyleWithColumns(10));
88   verifyFormat("a >>>= b;");
89   verifyFormat("aaaaaaa >>>=\n    b;", getGoogleJSStyleWithColumns(10));
90   verifyFormat("if (a + b + c +\n"
91                "        d >>>\n"
92                "    e + f + g)\n"
93                "  q();",
94                getGoogleJSStyleWithColumns(20));
95   verifyFormat("var x = aaaaaaaaaa ?\n"
96                "    bbbbbb :\n"
97                "    ccc;",
98                getGoogleJSStyleWithColumns(20));
99 
100   verifyFormat("var b = a.map((x) => x + 1);");
101   verifyFormat("return ('aaa') in bbbb;");
102   verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n"
103                "    aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
104   FormatStyle Style = getGoogleJSStyleWithColumns(80);
105   Style.AlignOperands = true;
106   verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n"
107                "        aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
108                Style);
109   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
110   verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa()\n"
111                "            in aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
112                Style);
113 
114   // ES6 spread operator.
115   verifyFormat("someFunction(...a);");
116   verifyFormat("var x = [1, ...a, 2];");
117 }
118 
119 TEST_F(FormatTestJS, UnderstandsAmpAmp) {
120   verifyFormat("e && e.SomeFunction();");
121 }
122 
123 TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
124   verifyFormat("not.and.or.not_eq = 1;");
125 }
126 
127 TEST_F(FormatTestJS, ReservedWords) {
128   // JavaScript reserved words (aka keywords) are only illegal when used as
129   // Identifiers, but are legal as IdentifierNames.
130   verifyFormat("x.class.struct = 1;");
131   verifyFormat("x.case = 1;");
132   verifyFormat("x.interface = 1;");
133   verifyFormat("x.for = 1;");
134   verifyFormat("x.of() = 1;");
135   verifyFormat("x.in() = 1;");
136   verifyFormat("x.let() = 1;");
137   verifyFormat("x.var() = 1;");
138   verifyFormat("x = {\n"
139                "  a: 12,\n"
140                "  interface: 1,\n"
141                "  switch: 1,\n"
142                "};");
143   verifyFormat("var struct = 2;");
144   verifyFormat("var union = 2;");
145   verifyFormat("var interface = 2;");
146   verifyFormat("interface = 2;");
147   verifyFormat("x = interface instanceof y;");
148 }
149 
150 TEST_F(FormatTestJS, CppKeywords) {
151   // Make sure we don't mess stuff up because of C++ keywords.
152   verifyFormat("return operator && (aa);");
153 }
154 
155 TEST_F(FormatTestJS, ES6DestructuringAssignment) {
156   verifyFormat("var [a, b, c] = [1, 2, 3];");
157   verifyFormat("const [a, b, c] = [1, 2, 3];");
158   verifyFormat("let [a, b, c] = [1, 2, 3];");
159   verifyFormat("var {a, b} = {a: 1, b: 2};");
160   verifyFormat("let {a, b} = {a: 1, b: 2};");
161 }
162 
163 TEST_F(FormatTestJS, ContainerLiterals) {
164   verifyFormat("var x = {y: function(a) { return a; }};");
165   verifyFormat("return {\n"
166                "  link: function() {\n"
167                "    f();  //\n"
168                "  }\n"
169                "};");
170   verifyFormat("return {\n"
171                "  a: a,\n"
172                "  link: function() {\n"
173                "    f();  //\n"
174                "  }\n"
175                "};");
176   verifyFormat("return {\n"
177                "  a: a,\n"
178                "  link: function() {\n"
179                "    f();  //\n"
180                "  },\n"
181                "  link: function() {\n"
182                "    f();  //\n"
183                "  }\n"
184                "};");
185   verifyFormat("var stuff = {\n"
186                "  // comment for update\n"
187                "  update: false,\n"
188                "  // comment for modules\n"
189                "  modules: false,\n"
190                "  // comment for tasks\n"
191                "  tasks: false\n"
192                "};");
193   verifyFormat("return {\n"
194                "  'finish':\n"
195                "      //\n"
196                "      a\n"
197                "};");
198   verifyFormat("var obj = {\n"
199                "  fooooooooo: function(x) {\n"
200                "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
201                "  }\n"
202                "};");
203   // Simple object literal, as opposed to enum style below.
204   verifyFormat("var obj = {a: 123};");
205   // Enum style top level assignment.
206   verifyFormat("X = {\n  a: 123\n};");
207   verifyFormat("X.Y = {\n  a: 123\n};");
208   // But only on the top level, otherwise its a plain object literal assignment.
209   verifyFormat("function x() {\n"
210                "  y = {z: 1};\n"
211                "}");
212   verifyFormat("x = foo && {a: 123};");
213 
214   // Arrow functions in object literals.
215   verifyFormat("var x = {y: (a) => { return a; }};");
216   verifyFormat("var x = {y: (a) => a};");
217 
218   // Computed keys.
219   verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};");
220   verifyFormat("var x = {\n"
221                "  [a]: 1,\n"
222                "  b: 2,\n"
223                "  [c]: 3,\n"
224                "};");
225 
226   // Object literals can leave out labels.
227   verifyFormat("f({a}, () => {\n"
228                "  g();  //\n"
229                "});");
230 
231   // Keys can be quoted.
232   verifyFormat("var x = {\n"
233                "  a: a,\n"
234                "  b: b,\n"
235                "  'c': c,\n"
236                "};");
237 }
238 
239 TEST_F(FormatTestJS, MethodsInObjectLiterals) {
240   verifyFormat("var o = {\n"
241                "  value: 'test',\n"
242                "  get value() {  // getter\n"
243                "    return this.value;\n"
244                "  }\n"
245                "};");
246   verifyFormat("var o = {\n"
247                "  value: 'test',\n"
248                "  set value(val) {  // setter\n"
249                "    this.value = val;\n"
250                "  }\n"
251                "};");
252   verifyFormat("var o = {\n"
253                "  value: 'test',\n"
254                "  someMethod(val) {  // method\n"
255                "    doSomething(this.value + val);\n"
256                "  }\n"
257                "};");
258   verifyFormat("var o = {\n"
259                "  someMethod(val) {  // method\n"
260                "    doSomething(this.value + val);\n"
261                "  },\n"
262                "  someOtherMethod(val) {  // method\n"
263                "    doSomething(this.value + val);\n"
264                "  }\n"
265                "};");
266 }
267 
268 TEST_F(FormatTestJS, SpacesInContainerLiterals) {
269   verifyFormat("var arr = [1, 2, 3];");
270   verifyFormat("f({a: 1, b: 2, c: 3});");
271 
272   verifyFormat("var object_literal_with_long_name = {\n"
273                "  a: 'aaaaaaaaaaaaaaaaaa',\n"
274                "  b: 'bbbbbbbbbbbbbbbbbb'\n"
275                "};");
276 
277   verifyFormat("f({a: 1, b: 2, c: 3});",
278                getChromiumStyle(FormatStyle::LK_JavaScript));
279   verifyFormat("f({'a': [{}]});");
280 }
281 
282 TEST_F(FormatTestJS, SingleQuotedStrings) {
283   verifyFormat("this.function('', true);");
284 }
285 
286 TEST_F(FormatTestJS, GoogScopes) {
287   verifyFormat("goog.scope(function() {\n"
288                "var x = a.b;\n"
289                "var y = c.d;\n"
290                "});  // goog.scope");
291   verifyFormat("goog.scope(function() {\n"
292                "// test\n"
293                "var x = 0;\n"
294                "// test\n"
295                "});");
296 }
297 
298 TEST_F(FormatTestJS, GoogModules) {
299   verifyFormat("goog.module('this.is.really.absurdly.long');",
300                getGoogleJSStyleWithColumns(40));
301   verifyFormat("goog.require('this.is.really.absurdly.long');",
302                getGoogleJSStyleWithColumns(40));
303   verifyFormat("goog.provide('this.is.really.absurdly.long');",
304                getGoogleJSStyleWithColumns(40));
305   verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
306                getGoogleJSStyleWithColumns(40));
307   verifyFormat("goog.setTestOnly('this.is.really.absurdly.long');",
308                getGoogleJSStyleWithColumns(40));
309   verifyFormat("goog.forwardDeclare('this.is.really.absurdly.long');",
310                getGoogleJSStyleWithColumns(40));
311 
312   // These should be wrapped normally.
313   verifyFormat(
314       "var MyLongClassName =\n"
315       "    goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
316 }
317 
318 TEST_F(FormatTestJS, FormatsNamespaces) {
319   verifyFormat("namespace Foo {\n"
320                "  export let x = 1;\n"
321                "}\n");
322   verifyFormat("declare namespace Foo {\n"
323                "  export let x: number;\n"
324                "}\n");
325 }
326 
327 TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
328   verifyFormat("function outer1(a, b) {\n"
329                "  function inner1(a, b) { return a; }\n"
330                "  inner1(a, b);\n"
331                "}\n"
332                "function outer2(a, b) {\n"
333                "  function inner2(a, b) { return a; }\n"
334                "  inner2(a, b);\n"
335                "}");
336   verifyFormat("function f() {}");
337 }
338 
339 TEST_F(FormatTestJS, GeneratorFunctions) {
340   verifyFormat("function* f() {\n"
341                "  let x = 1;\n"
342                "  yield x;\n"
343                "  yield* something();\n"
344                "}");
345   verifyFormat("function*\n"
346                "    f() {\n"
347                "}",
348                getGoogleJSStyleWithColumns(8));
349   verifyFormat("export function* f() {\n"
350                "  yield 1;\n"
351                "}\n");
352   verifyFormat("class X {\n"
353                "  * generatorMethod() { yield x; }\n"
354                "}");
355 }
356 
357 TEST_F(FormatTestJS, AsyncFunctions) {
358   verifyFormat("async function f() {\n"
359                "  let x = 1;\n"
360                "  return fetch(x);\n"
361                "}");
362   verifyFormat("async function* f() {\n"
363                "  yield fetch(x);\n"
364                "}");
365   verifyFormat("export async function f() {\n"
366                "  return fetch(x);\n"
367                "}");
368   verifyFormat("class X {\n"
369                "  async asyncMethod() { return fetch(1); }\n"
370                "}");
371   verifyFormat("function initialize() {\n"
372                "  // Comment.\n"
373                "  return async.then();\n"
374                "}\n");
375 }
376 
377 TEST_F(FormatTestJS, ArrayLiterals) {
378   verifyFormat("var aaaaa: List<SomeThing> =\n"
379                "    [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];");
380   verifyFormat("return [\n"
381                "  aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
382                "  ccccccccccccccccccccccccccc\n"
383                "];");
384   verifyFormat("return [\n"
385                "  aaaa().bbbbbbbb('A'),\n"
386                "  aaaa().bbbbbbbb('B'),\n"
387                "  aaaa().bbbbbbbb('C'),\n"
388                "];");
389   verifyFormat("var someVariable = SomeFunction([\n"
390                "  aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
391                "  ccccccccccccccccccccccccccc\n"
392                "]);");
393   verifyFormat("var someVariable = SomeFunction([\n"
394                "  [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n"
395                "]);",
396                getGoogleJSStyleWithColumns(51));
397   verifyFormat("var someVariable = SomeFunction(aaaa, [\n"
398                "  aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
399                "  ccccccccccccccccccccccccccc\n"
400                "]);");
401   verifyFormat("var someVariable = SomeFunction(\n"
402                "    aaaa,\n"
403                "    [\n"
404                "      aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
405                "      cccccccccccccccccccccccccc\n"
406                "    ],\n"
407                "    aaaa);");
408   verifyFormat("var aaaa = aaaaa ||  // wrap\n"
409                "    [];");
410 
411   verifyFormat("someFunction([], {a: a});");
412 }
413 
414 TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) {
415   verifyFormat("var array = [\n"
416                "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
417                "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
418                "];");
419   verifyFormat("var array = someFunction([\n"
420                "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
421                "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
422                "]);");
423 }
424 
425 TEST_F(FormatTestJS, FunctionLiterals) {
426   verifyFormat("doFoo(function() {});");
427   verifyFormat("doFoo(function() { return 1; });");
428   verifyFormat("var func = function() {\n"
429                "  return 1;\n"
430                "};");
431   verifyFormat("var func =  //\n"
432                "    function() {\n"
433                "  return 1;\n"
434                "};");
435   verifyFormat("return {\n"
436                "  body: {\n"
437                "    setAttribute: function(key, val) { this[key] = val; },\n"
438                "    getAttribute: function(key) { return this[key]; },\n"
439                "    style: {direction: ''}\n"
440                "  }\n"
441                "};");
442   verifyFormat("abc = xyz ? function() {\n"
443                "  return 1;\n"
444                "} : function() {\n"
445                "  return -1;\n"
446                "};");
447 
448   verifyFormat("var closure = goog.bind(\n"
449                "    function() {  // comment\n"
450                "      foo();\n"
451                "      bar();\n"
452                "    },\n"
453                "    this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
454                "    arg3IsReallyLongAndNeeedsLineBreaks);");
455   verifyFormat("var closure = goog.bind(function() {  // comment\n"
456                "  foo();\n"
457                "  bar();\n"
458                "}, this);");
459   verifyFormat("return {\n"
460                "  a: 'E',\n"
461                "  b: function() {\n"
462                "    return function() {\n"
463                "      f();  //\n"
464                "    };\n"
465                "  }\n"
466                "};");
467   verifyFormat("{\n"
468                "  var someVariable = function(x) {\n"
469                "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
470                "  };\n"
471                "}");
472   verifyFormat("someLooooooooongFunction(\n"
473                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
474                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
475                "    function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
476                "      // code\n"
477                "    });");
478 
479   verifyFormat("f({a: function() { return 1; }});",
480                getGoogleJSStyleWithColumns(33));
481   verifyFormat("f({\n"
482                "  a: function() { return 1; }\n"
483                "});",
484                getGoogleJSStyleWithColumns(32));
485 
486   verifyFormat("return {\n"
487                "  a: function SomeFunction() {\n"
488                "    // ...\n"
489                "    return 1;\n"
490                "  }\n"
491                "};");
492   verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
493                "    .then(goog.bind(function(aaaaaaaaaaa) {\n"
494                "      someFunction();\n"
495                "      someFunction();\n"
496                "    }, this), aaaaaaaaaaaaaaaaa);");
497 
498   verifyFormat("someFunction(goog.bind(function() {\n"
499                "  doSomething();\n"
500                "  doSomething();\n"
501                "}, this), goog.bind(function() {\n"
502                "  doSomething();\n"
503                "  doSomething();\n"
504                "}, this));");
505 
506   // FIXME: This is bad, we should be wrapping before "function() {".
507   verifyFormat("someFunction(function() {\n"
508                "  doSomething();  // break\n"
509                "})\n"
510                "    .doSomethingElse(\n"
511                "        // break\n"
512                "        );");
513 }
514 
515 TEST_F(FormatTestJS, InliningFunctionLiterals) {
516   FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
517   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
518   verifyFormat("var func = function() {\n"
519                "  return 1;\n"
520                "};",
521                Style);
522   verifyFormat("var func = doSomething(function() { return 1; });", Style);
523   verifyFormat("var outer = function() {\n"
524                "  var inner = function() { return 1; }\n"
525                "};",
526                Style);
527   verifyFormat("function outer1(a, b) {\n"
528                "  function inner1(a, b) { return a; }\n"
529                "}",
530                Style);
531 
532   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
533   verifyFormat("var func = function() { return 1; };", Style);
534   verifyFormat("var func = doSomething(function() { return 1; });", Style);
535   verifyFormat(
536       "var outer = function() { var inner = function() { return 1; } };",
537       Style);
538   verifyFormat("function outer1(a, b) {\n"
539                "  function inner1(a, b) { return a; }\n"
540                "}",
541                Style);
542 
543   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
544   verifyFormat("var func = function() {\n"
545                "  return 1;\n"
546                "};",
547                Style);
548   verifyFormat("var func = doSomething(function() {\n"
549                "  return 1;\n"
550                "});",
551                Style);
552   verifyFormat("var outer = function() {\n"
553                "  var inner = function() {\n"
554                "    return 1;\n"
555                "  }\n"
556                "};",
557                Style);
558   verifyFormat("function outer1(a, b) {\n"
559                "  function inner1(a, b) {\n"
560                "    return a;\n"
561                "  }\n"
562                "}",
563                Style);
564 
565   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
566   verifyFormat("var func = function() {\n"
567                "  return 1;\n"
568                "};",
569                Style);
570 }
571 
572 TEST_F(FormatTestJS, MultipleFunctionLiterals) {
573   verifyFormat("promise.then(\n"
574                "    function success() {\n"
575                "      doFoo();\n"
576                "      doBar();\n"
577                "    },\n"
578                "    function error() {\n"
579                "      doFoo();\n"
580                "      doBaz();\n"
581                "    },\n"
582                "    []);\n");
583   verifyFormat("promise.then(\n"
584                "    function success() {\n"
585                "      doFoo();\n"
586                "      doBar();\n"
587                "    },\n"
588                "    [],\n"
589                "    function error() {\n"
590                "      doFoo();\n"
591                "      doBaz();\n"
592                "    });\n");
593   verifyFormat("promise.then(\n"
594                "    [],\n"
595                "    function success() {\n"
596                "      doFoo();\n"
597                "      doBar();\n"
598                "    },\n"
599                "    function error() {\n"
600                "      doFoo();\n"
601                "      doBaz();\n"
602                "    });\n");
603 
604   verifyFormat("getSomeLongPromise()\n"
605                "    .then(function(value) { body(); })\n"
606                "    .thenCatch(function(error) {\n"
607                "      body();\n"
608                "      body();\n"
609                "    });");
610   verifyFormat("getSomeLongPromise()\n"
611                "    .then(function(value) {\n"
612                "      body();\n"
613                "      body();\n"
614                "    })\n"
615                "    .thenCatch(function(error) {\n"
616                "      body();\n"
617                "      body();\n"
618                "    });");
619 
620   verifyFormat("getSomeLongPromise()\n"
621                "    .then(function(value) { body(); })\n"
622                "    .thenCatch(function(error) { body(); });");
623 
624   verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n"
625                "    .aaaaaaa(function() {\n"
626                "      //\n"
627                "    })\n"
628                "    .bbbbbb();");
629 }
630 
631 TEST_F(FormatTestJS, ArrowFunctions) {
632   verifyFormat("var x = (a) => {\n"
633                "  return a;\n"
634                "};");
635   verifyFormat("var x = (a) => {\n"
636                "  function y() { return 42; }\n"
637                "  return a;\n"
638                "};");
639   verifyFormat("var x = (a: type): {some: type} => {\n"
640                "  return a;\n"
641                "};");
642   verifyFormat("var x = (a) => a;");
643   verifyFormat("return () => [];");
644   verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n"
645                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
646                "      (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
647                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n"
648                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
649                "};");
650   verifyFormat("var a = a.aaaaaaa(\n"
651                "    (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n"
652                "        aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
653   verifyFormat("var a = a.aaaaaaa(\n"
654                "    (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n"
655                "        aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n"
656                "        aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
657 
658   // FIXME: This is bad, we should be wrapping before "() => {".
659   verifyFormat("someFunction(() => {\n"
660                "  doSomething();  // break\n"
661                "})\n"
662                "    .doSomethingElse(\n"
663                "        // break\n"
664                "        );");
665 }
666 
667 TEST_F(FormatTestJS, ReturnStatements) {
668   verifyFormat("function() {\n"
669                "  return [hello, world];\n"
670                "}");
671 }
672 
673 TEST_F(FormatTestJS, ForLoops) {
674   verifyFormat("for (var i in [2, 3]) {\n"
675                "}");
676   verifyFormat("for (var i of [2, 3]) {\n"
677                "}");
678   verifyFormat("for (let {a, b} of x) {\n"
679                "}");
680   verifyFormat("for (let {a, b} in x) {\n"
681                "}");
682 }
683 
684 TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) {
685   // The following statements must not wrap, as otherwise the program meaning
686   // would change due to automatic semicolon insertion.
687   // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1.
688   verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10));
689   verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
690   verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10));
691   verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
692   verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10));
693   verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10));
694   verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10));
695   verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10));
696   verifyFormat("return [\n"
697                "  aaa\n"
698                "];",
699                getGoogleJSStyleWithColumns(12));
700 }
701 
702 TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {
703   verifyFormat("a\n"
704                "b;",
705                " a \n"
706                " b ;");
707   verifyFormat("a()\n"
708                "b;",
709                " a ()\n"
710                " b ;");
711   verifyFormat("a[b]\n"
712                "c;",
713                "a [b]\n"
714                "c ;");
715   verifyFormat("1\n"
716                "a;",
717                "1 \n"
718                "a ;");
719   verifyFormat("a\n"
720                "1;",
721                "a \n"
722                "1 ;");
723   verifyFormat("a\n"
724                "'x';",
725                "a \n"
726                " 'x';");
727   verifyFormat("a++\n"
728                "b;",
729                "a ++\n"
730                "b ;");
731   verifyFormat("a\n"
732                "!b && c;",
733                "a \n"
734                " ! b && c;");
735   verifyFormat("a\n"
736                "if (1) f();",
737                " a\n"
738                " if (1) f();");
739   verifyFormat("a\n"
740                "class X {}",
741                " a\n"
742                " class X {}");
743   verifyFormat("var a", "var\n"
744                         "a");
745   verifyFormat("x instanceof String", "x\n"
746                                       "instanceof\n"
747                                       "String");
748   verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n"
749                                           "  bar) {}");
750 }
751 
752 TEST_F(FormatTestJS, ClosureStyleCasts) {
753   verifyFormat("var x = /** @type {foo} */ (bar);");
754 }
755 
756 TEST_F(FormatTestJS, TryCatch) {
757   verifyFormat("try {\n"
758                "  f();\n"
759                "} catch (e) {\n"
760                "  g();\n"
761                "} finally {\n"
762                "  h();\n"
763                "}");
764 
765   // But, of course, "catch" is a perfectly fine function name in JavaScript.
766   verifyFormat("someObject.catch();");
767   verifyFormat("someObject.new();");
768   verifyFormat("someObject.delete();");
769 }
770 
771 TEST_F(FormatTestJS, StringLiteralConcatenation) {
772   verifyFormat("var literal = 'hello ' +\n"
773                "    'world';");
774 }
775 
776 TEST_F(FormatTestJS, RegexLiteralClassification) {
777   // Regex literals.
778   verifyFormat("var regex = /abc/;");
779   verifyFormat("f(/abc/);");
780   verifyFormat("f(abc, /abc/);");
781   verifyFormat("some_map[/abc/];");
782   verifyFormat("var x = a ? /abc/ : /abc/;");
783   verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
784   verifyFormat("var x = !/abc/.test(y);");
785   verifyFormat("var x = a && /abc/.test(y);");
786   verifyFormat("var x = a || /abc/.test(y);");
787   verifyFormat("var x = a + /abc/.search(y);");
788   verifyFormat("/abc/.search(y);");
789   verifyFormat("var regexs = {/abc/, /abc/};");
790   verifyFormat("return /abc/;");
791 
792   // Not regex literals.
793   verifyFormat("var a = a / 2 + b / 3;");
794   verifyFormat("var a = a++ / 2;");
795   // Prefix unary can operate on regex literals, not that it makes sense.
796   verifyFormat("var a = ++/a/;");
797 
798   // This is a known issue, regular expressions are incorrectly detected if
799   // directly following a closing parenthesis.
800   verifyFormat("if (foo) / bar /.exec(baz);");
801 }
802 
803 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
804   verifyFormat("var regex = /=/;");
805   verifyFormat("var regex = /a*/;");
806   verifyFormat("var regex = /a+/;");
807   verifyFormat("var regex = /a?/;");
808   verifyFormat("var regex = /.a./;");
809   verifyFormat("var regex = /a\\*/;");
810   verifyFormat("var regex = /^a$/;");
811   verifyFormat("var regex = /\\/a/;");
812   verifyFormat("var regex = /(?:x)/;");
813   verifyFormat("var regex = /x(?=y)/;");
814   verifyFormat("var regex = /x(?!y)/;");
815   verifyFormat("var regex = /x|y/;");
816   verifyFormat("var regex = /a{2}/;");
817   verifyFormat("var regex = /a{1,3}/;");
818 
819   verifyFormat("var regex = /[abc]/;");
820   verifyFormat("var regex = /[^abc]/;");
821   verifyFormat("var regex = /[\\b]/;");
822   verifyFormat("var regex = /[/]/;");
823   verifyFormat("var regex = /[\\/]/;");
824   verifyFormat("var regex = /\\[/;");
825   verifyFormat("var regex = /\\\\[/]/;");
826   verifyFormat("var regex = /}[\"]/;");
827   verifyFormat("var regex = /}[/\"]/;");
828   verifyFormat("var regex = /}[\"/]/;");
829 
830   verifyFormat("var regex = /\\b/;");
831   verifyFormat("var regex = /\\B/;");
832   verifyFormat("var regex = /\\d/;");
833   verifyFormat("var regex = /\\D/;");
834   verifyFormat("var regex = /\\f/;");
835   verifyFormat("var regex = /\\n/;");
836   verifyFormat("var regex = /\\r/;");
837   verifyFormat("var regex = /\\s/;");
838   verifyFormat("var regex = /\\S/;");
839   verifyFormat("var regex = /\\t/;");
840   verifyFormat("var regex = /\\v/;");
841   verifyFormat("var regex = /\\w/;");
842   verifyFormat("var regex = /\\W/;");
843   verifyFormat("var regex = /a(a)\\1/;");
844   verifyFormat("var regex = /\\0/;");
845   verifyFormat("var regex = /\\\\/g;");
846   verifyFormat("var regex = /\\a\\\\/g;");
847   verifyFormat("var regex = /\a\\//g;");
848   verifyFormat("var regex = /a\\//;\n"
849                "var x = 0;");
850   verifyFormat("var regex = /'/g;", "var regex = /'/g ;");
851   verifyFormat("var regex = /'/g;  //'", "var regex = /'/g ; //'");
852   verifyFormat("var regex = /\\/*/;\n"
853                "var x = 0;",
854                "var regex = /\\/*/;\n"
855                "var x=0;");
856   verifyFormat("var x = /a\\//;", "var x = /a\\//  \n;");
857   verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16));
858   verifyFormat("var regex =\n"
859                "    /\"/;",
860                getGoogleJSStyleWithColumns(15));
861   verifyFormat("var regex =  //\n"
862                "    /a/;");
863   verifyFormat("var regexs = [\n"
864                "  /d/,   //\n"
865                "  /aa/,  //\n"
866                "];");
867 }
868 
869 TEST_F(FormatTestJS, RegexLiteralModifiers) {
870   verifyFormat("var regex = /abc/g;");
871   verifyFormat("var regex = /abc/i;");
872   verifyFormat("var regex = /abc/m;");
873   verifyFormat("var regex = /abc/y;");
874 }
875 
876 TEST_F(FormatTestJS, RegexLiteralLength) {
877   verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
878                getGoogleJSStyleWithColumns(60));
879   verifyFormat("var regex =\n"
880                "    /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
881                getGoogleJSStyleWithColumns(60));
882   verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
883                getGoogleJSStyleWithColumns(50));
884 }
885 
886 TEST_F(FormatTestJS, RegexLiteralExamples) {
887   verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
888 }
889 
890 TEST_F(FormatTestJS, TypeAnnotations) {
891   verifyFormat("var x: string;");
892   verifyFormat("var x: {a: string; b: number;} = {};");
893   verifyFormat("function x(): string {\n  return 'x';\n}");
894   verifyFormat("function x(): {x: string} {\n  return {x: 'x'};\n}");
895   verifyFormat("function x(y: string): string {\n  return 'x';\n}");
896   verifyFormat("for (var y: string in x) {\n  x();\n}");
897   verifyFormat("for (var y: string of x) {\n  x();\n}");
898   verifyFormat("function x(y: {a?: number;} = {}): number {\n"
899                "  return 12;\n"
900                "}");
901   verifyFormat("((a: string, b: number): string => a + b);");
902   verifyFormat("var x: (y: number) => string;");
903   verifyFormat("var x: P<string, (a: number) => string>;");
904   verifyFormat("var x = {y: function(): z { return 1; }};");
905   verifyFormat("var x = {y: function(): {a: number} { return 1; }};");
906   verifyFormat("function someFunc(args: string[]):\n"
907                "    {longReturnValue: string[]} {}",
908                getGoogleJSStyleWithColumns(60));
909 }
910 
911 TEST_F(FormatTestJS, UnionIntersectionTypes) {
912   verifyFormat("let x: A|B = A | B;");
913   verifyFormat("let x: A&B|C = A & B;");
914   verifyFormat("let x: Foo<A|B> = new Foo<A|B>();");
915   verifyFormat("function(x: A|B): C&D {}");
916   verifyFormat("function(x: A|B = A | B): C&D {}");
917   verifyFormat("function x(path: number|string) {}");
918   verifyFormat("function x(): string|number {}");
919   verifyFormat("type Foo = Bar|Baz;");
920   verifyFormat("type Foo = Bar<X>|Baz;");
921   verifyFormat("type Foo = (Bar<X>|Baz);");
922   verifyFormat("let x: Bar|Baz;");
923   verifyFormat("let x: Bar<X>|Baz;");
924   verifyFormat("let x: (Foo|Bar)[];");
925 }
926 
927 TEST_F(FormatTestJS, ClassDeclarations) {
928   verifyFormat("class C {\n  x: string = 12;\n}");
929   verifyFormat("class C {\n  x(): string => 12;\n}");
930   verifyFormat("class C {\n  ['x' + 2]: string = 12;\n}");
931   verifyFormat("class C {\n  private x: string = 12;\n}");
932   verifyFormat("class C {\n  private static x: string = 12;\n}");
933   verifyFormat("class C {\n  static x(): string { return 'asd'; }\n}");
934   verifyFormat("class C extends P implements I {}");
935   verifyFormat("class C extends p.P implements i.I {}");
936   verifyFormat("class Test {\n"
937                "  aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n"
938                "      aaaaaaaaaaaaaaaaaaaaaa {}\n"
939                "}");
940   verifyFormat("foo = class Name {\n"
941                "  constructor() {}\n"
942                "};");
943   verifyFormat("foo = class {\n"
944                "  constructor() {}\n"
945                "};");
946   verifyFormat("class C {\n"
947                "  x: {y: Z;} = {};\n"
948                "  private y: {y: Z;} = {};\n"
949                "}");
950 
951   // ':' is not a type declaration here.
952   verifyFormat("class X {\n"
953                "  subs = {\n"
954                "    'b': {\n"
955                "      'c': 1,\n"
956                "    },\n"
957                "  };\n"
958                "}");
959   verifyFormat("@Component({\n"
960                "  moduleId: module.id,\n"
961                "})\n"
962                "class SessionListComponent implements OnDestroy, OnInit {\n"
963                "}");
964 }
965 
966 TEST_F(FormatTestJS, InterfaceDeclarations) {
967   verifyFormat("interface I {\n"
968                "  x: string;\n"
969                "  enum: string[];\n"
970                "  enum?: string[];\n"
971                "}\n"
972                "var y;");
973   // Ensure that state is reset after parsing the interface.
974   verifyFormat("interface a {}\n"
975                "export function b() {}\n"
976                "var x;");
977 
978   // Arrays of object type literals.
979   verifyFormat("interface I {\n"
980                "  o: {}[];\n"
981                "}");
982 }
983 
984 TEST_F(FormatTestJS, EnumDeclarations) {
985   verifyFormat("enum Foo {\n"
986                "  A = 1,\n"
987                "  B\n"
988                "}");
989   verifyFormat("export /* somecomment*/ enum Foo {\n"
990                "  A = 1,\n"
991                "  B\n"
992                "}");
993   verifyFormat("enum Foo {\n"
994                "  A = 1,  // comment\n"
995                "  B\n"
996                "}\n"
997                "var x = 1;");
998 }
999 
1000 TEST_F(FormatTestJS, MetadataAnnotations) {
1001   verifyFormat("@A\nclass C {\n}");
1002   verifyFormat("@A({arg: 'value'})\nclass C {\n}");
1003   verifyFormat("@A\n@B\nclass C {\n}");
1004   verifyFormat("class C {\n  @A x: string;\n}");
1005   verifyFormat("class C {\n"
1006                "  @A\n"
1007                "  private x(): string {\n"
1008                "    return 'y';\n"
1009                "  }\n"
1010                "}");
1011   verifyFormat("class C {\n"
1012                "  private x(@A x: string) {}\n"
1013                "}");
1014   verifyFormat("class X {}\n"
1015                "class Y {}");
1016 }
1017 
1018 TEST_F(FormatTestJS, TypeAliases) {
1019   verifyFormat("type X = number;\n"
1020                "class C {}");
1021   verifyFormat("type X<Y> = Z<Y>;");
1022   verifyFormat("type X = {\n"
1023                "  y: number\n"
1024                "};\n"
1025                "class C {}");
1026 }
1027 
1028 TEST_F(FormatTestJS, Modules) {
1029   verifyFormat("import SomeThing from 'some/module.js';");
1030   verifyFormat("import {X, Y} from 'some/module.js';");
1031   verifyFormat("import a, {X, Y} from 'some/module.js';");
1032   verifyFormat("import {X, Y,} from 'some/module.js';");
1033   verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
1034   // Ensure Automatic Semicolon Insertion does not break on "as\n".
1035   verifyFormat("import {X as myX} from 'm';", "import {X as\n"
1036                                               " myX} from 'm';");
1037   verifyFormat("import * as lib from 'some/module.js';");
1038   verifyFormat("var x = {import: 1};\nx.import = 2;");
1039 
1040   verifyFormat("export function fn() {\n"
1041                "  return 'fn';\n"
1042                "}");
1043   verifyFormat("export function A() {}\n"
1044                "export default function B() {}\n"
1045                "export function C() {}");
1046   verifyFormat("export default () => {\n"
1047                "  let x = 1;\n"
1048                "  return x;\n"
1049                "}");
1050   verifyFormat("export const x = 12;");
1051   verifyFormat("export default class X {}");
1052   verifyFormat("export {X, Y} from 'some/module.js';");
1053   verifyFormat("export {X, Y,} from 'some/module.js';");
1054   verifyFormat("export {SomeVeryLongExport as X, "
1055                "SomeOtherVeryLongExport as Y} from 'some/module.js';");
1056   // export without 'from' is wrapped.
1057   verifyFormat("export let someRatherLongVariableName =\n"
1058                "    someSurprisinglyLongVariable + someOtherRatherLongVar;");
1059   // ... but not if from is just an identifier.
1060   verifyFormat("export {\n"
1061                "  from as from,\n"
1062                "  someSurprisinglyLongVariable as\n"
1063                "      from\n"
1064                "};",
1065                getGoogleJSStyleWithColumns(20));
1066   verifyFormat("export class C {\n"
1067                "  x: number;\n"
1068                "  y: string;\n"
1069                "}");
1070   verifyFormat("export class X { y: number; }");
1071   verifyFormat("export abstract class X { y: number; }");
1072   verifyFormat("export default class X { y: number }");
1073   verifyFormat("export default function() {\n  return 1;\n}");
1074   verifyFormat("export var x = 12;");
1075   verifyFormat("class C {}\n"
1076                "export function f() {}\n"
1077                "var v;");
1078   verifyFormat("export var x: number = 12;");
1079   verifyFormat("export const y = {\n"
1080                "  a: 1,\n"
1081                "  b: 2\n"
1082                "};");
1083   verifyFormat("export enum Foo {\n"
1084                "  BAR,\n"
1085                "  // adsdasd\n"
1086                "  BAZ\n"
1087                "}");
1088   verifyFormat("export default [\n"
1089                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1090                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
1091                "];");
1092   verifyFormat("export default [];");
1093   verifyFormat("export default () => {};");
1094   verifyFormat("export interface Foo { foo: number; }\n"
1095                "export class Bar {\n"
1096                "  blah(): string { return this.blah; };\n"
1097                "}");
1098 }
1099 
1100 TEST_F(FormatTestJS, ImportWrapping) {
1101   verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,"
1102                " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying"
1103                "} from 'some/module.js';");
1104   FormatStyle Style = getGoogleJSStyleWithColumns(80);
1105   Style.JavaScriptWrapImports = true;
1106   verifyFormat("import {\n"
1107                "  VeryLongImportsAreAnnoying,\n"
1108                "  VeryLongImportsAreAnnoying,\n"
1109                "  VeryLongImportsAreAnnoying,\n"
1110                "} from 'some/module.js';",
1111                Style);
1112   verifyFormat("import {\n"
1113                "  A,\n"
1114                "  A,\n"
1115                "} from 'some/module.js';",
1116                Style);
1117   verifyFormat("export {\n"
1118                "  A,\n"
1119                "  A,\n"
1120                "} from 'some/module.js';",
1121                Style);
1122 }
1123 
1124 TEST_F(FormatTestJS, TemplateStrings) {
1125   // Keeps any whitespace/indentation within the template string.
1126   verifyFormat("var x = `hello\n"
1127             "     ${name}\n"
1128             "  !`;",
1129             "var x    =    `hello\n"
1130                    "     ${  name    }\n"
1131                    "  !`;");
1132 
1133   verifyFormat("var x =\n"
1134                "    `hello ${world}` >= some();",
1135                getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
1136   verifyFormat("var x = `hello ${world}` >= some();",
1137                getGoogleJSStyleWithColumns(35)); // Barely fits.
1138   verifyFormat("var x = `hellö ${wörld}` >= söme();",
1139                getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8.
1140   verifyFormat("var x = `hello\n"
1141             "  ${world}` >=\n"
1142             "    some();",
1143             "var x =\n"
1144                    "    `hello\n"
1145                    "  ${world}` >= some();",
1146                    getGoogleJSStyleWithColumns(21)); // Barely doesn't fit.
1147   verifyFormat("var x = `hello\n"
1148             "  ${world}` >= some();",
1149             "var x =\n"
1150                    "    `hello\n"
1151                    "  ${world}` >= some();",
1152                    getGoogleJSStyleWithColumns(22)); // Barely fits.
1153 
1154   verifyFormat("var x =\n"
1155                "    `h`;",
1156                getGoogleJSStyleWithColumns(11));
1157   verifyFormat("var x =\n    `multi\n  line`;", "var x = `multi\n  line`;",
1158                getGoogleJSStyleWithColumns(13));
1159   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1160                "    `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);");
1161   // Repro for an obscure width-miscounting issue with template strings.
1162   verifyFormat(
1163       "someLongVariable =\n"
1164       "    "
1165       "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;",
1166       "someLongVariable = "
1167       "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;");
1168 
1169   // Make sure template strings get a proper ColumnWidth assigned, even if they
1170   // are first token in line.
1171   verifyFormat(
1172       "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
1173       "    `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;");
1174 
1175   // Two template strings.
1176   verifyFormat("var x = `hello` == `hello`;");
1177 
1178   // Comments in template strings.
1179   verifyFormat("var x = `//a`;\n"
1180             "var y;",
1181             "var x =\n `//a`;\n"
1182                    "var y  ;");
1183   verifyFormat("var x = `/*a`;\n"
1184                "var y;",
1185                "var x =\n `/*a`;\n"
1186                "var y;");
1187   // Unterminated string literals in a template string.
1188   verifyFormat("var x = `'`;  // comment with matching quote '\n"
1189                "var y;");
1190   verifyFormat("var x = `\"`;  // comment with matching quote \"\n"
1191                "var y;");
1192   verifyFormat("it(`'aaaaaaaaaaaaaaa   `, aaaaaaaaa);",
1193                "it(`'aaaaaaaaaaaaaaa   `,   aaaaaaaaa) ;",
1194                getGoogleJSStyleWithColumns(40));
1195   // Backticks in a comment - not a template string.
1196   verifyFormat("var x = 1  // `/*a`;\n"
1197                "    ;",
1198                "var x =\n 1  // `/*a`;\n"
1199                "    ;");
1200   verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */");
1201   // Comment spans multiple template strings.
1202   verifyFormat("var x = `/*a`;\n"
1203                "var y = ` */ `;",
1204                "var x =\n `/*a`;\n"
1205                "var y =\n ` */ `;");
1206   // Escaped backtick.
1207   verifyFormat("var x = ` \\` a`;\n"
1208                "var y;",
1209                "var x = ` \\` a`;\n"
1210                "var y;");
1211   // Escaped dollar.
1212   verifyFormat("var x = ` \\${foo}`;\n");
1213 }
1214 
1215 TEST_F(FormatTestJS, NestedTemplateStrings) {
1216   verifyFormat(
1217       "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;");
1218   verifyFormat("var x = `he${({text: 'll'}.text)}o`;");
1219 }
1220 
1221 TEST_F(FormatTestJS, TaggedTemplateStrings) {
1222   verifyFormat("var x = html`<ul>`;");
1223 }
1224 
1225 TEST_F(FormatTestJS, CastSyntax) {
1226   verifyFormat("var x = <type>foo;");
1227   verifyFormat("var x = foo as type;");
1228   verifyFormat("let x = (a + b) as\n"
1229                "    LongTypeIsLong;",
1230                getGoogleJSStyleWithColumns(20));
1231   verifyFormat("foo = <Bar[]>[\n"
1232                "  1,  //\n"
1233                "  2\n"
1234                "];");
1235   verifyFormat("var x = [{x: 1} as type];");
1236   verifyFormat("x = x as [a, b];");
1237   verifyFormat("x = x as {a: string};");
1238   verifyFormat("x = x as (string);");
1239   verifyFormat("x = x! as (string);");
1240 }
1241 
1242 TEST_F(FormatTestJS, TypeArguments) {
1243   verifyFormat("class X<Y> {}");
1244   verifyFormat("new X<Y>();");
1245   verifyFormat("foo<Y>(a);");
1246   verifyFormat("var x: X<Y>[];");
1247   verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
1248   verifyFormat("function f(a: List<any> = null) {}");
1249   verifyFormat("function f(): List<any> {}");
1250   verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n"
1251                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}");
1252   verifyFormat("function aaaaaaaaaa(\n"
1253                "    aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n"
1254                "    aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n"
1255                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}");
1256 }
1257 
1258 TEST_F(FormatTestJS, UserDefinedTypeGuards) {
1259   verifyFormat(
1260       "function foo(check: Object):\n"
1261       "    check is {foo: string, bar: string, baz: string, foobar: string} {\n"
1262       "  return 'bar' in check;\n"
1263       "}\n");
1264 }
1265 
1266 TEST_F(FormatTestJS, OptionalTypes) {
1267   verifyFormat("function x(a?: b, c?, d?) {}");
1268   verifyFormat("class X {\n"
1269                "  y?: z;\n"
1270                "  z?;\n"
1271                "}");
1272   verifyFormat("interface X {\n"
1273                "  y?(): z;\n"
1274                "}");
1275   verifyFormat("constructor({aa}: {\n"
1276                "  aa?: string,\n"
1277                "  aaaaaaaa?: string,\n"
1278                "  aaaaaaaaaaaaaaa?: boolean,\n"
1279                "  aaaaaa?: List<string>\n"
1280                "}) {}");
1281 }
1282 
1283 TEST_F(FormatTestJS, IndexSignature) {
1284   verifyFormat("var x: {[k: string]: v};");
1285 }
1286 
1287 TEST_F(FormatTestJS, WrapAfterParen) {
1288   verifyFormat("xxxxxxxxxxx(\n"
1289                "    aaa, aaa);",
1290                getGoogleJSStyleWithColumns(20));
1291   verifyFormat("xxxxxxxxxxx(\n"
1292                "    aaa, aaa, aaa,\n"
1293                "    aaa, aaa, aaa);",
1294                getGoogleJSStyleWithColumns(20));
1295   verifyFormat("xxxxxxxxxxx(\n"
1296                "    aaaaaaaaaaaaaaaaaaaaaaaa,\n"
1297                "    function(x) {\n"
1298                "      y();  //\n"
1299                "    });",
1300                getGoogleJSStyleWithColumns(40));
1301   verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
1302                "       bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
1303 }
1304 
1305 TEST_F(FormatTestJS, JSDocAnnotations) {
1306   verifyFormat("/**\n"
1307                " * @export {this.is.a.long.path.to.a.Type}\n"
1308                " */",
1309                "/**\n"
1310                " * @export {this.is.a.long.path.to.a.Type}\n"
1311                " */",
1312                getGoogleJSStyleWithColumns(20));
1313 }
1314 
1315 TEST_F(FormatTestJS, RequoteStringsSingle) {
1316   verifyFormat("var x = 'foo';", "var x = \"foo\";");
1317   verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";");
1318   verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";");
1319   verifyFormat(
1320       "var x =\n"
1321       "    'foo\\'';",
1322       // Code below is 15 chars wide, doesn't fit into the line with the
1323       // \ escape added.
1324       "var x = \"foo'\";", getGoogleJSStyleWithColumns(15));
1325   // Removes no-longer needed \ escape from ".
1326   verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";");
1327   // Code below fits into 15 chars *after* removing the \ escape.
1328   verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";",
1329                getGoogleJSStyleWithColumns(15));
1330   verifyFormat("// clang-format off\n"
1331                "let x = \"double\";\n"
1332                "// clang-format on\n"
1333                "let x = 'single';\n",
1334                "// clang-format off\n"
1335                "let x = \"double\";\n"
1336                "// clang-format on\n"
1337                "let x = \"single\";\n");
1338 }
1339 
1340 TEST_F(FormatTestJS, RequoteAndIndent) {
1341   verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n"
1342                "    'double quoted string that needs wrapping');",
1343                "let x = someVeryLongFunctionThatGoesOnAndOn("
1344                "\"double quoted string that needs wrapping\");");
1345 }
1346 
1347 TEST_F(FormatTestJS, RequoteStringsDouble) {
1348   FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
1349   DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double;
1350   verifyFormat("var x = \"foo\";", DoubleQuotes);
1351   verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes);
1352   verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes);
1353 }
1354 
1355 TEST_F(FormatTestJS, RequoteStringsLeave) {
1356   FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
1357   LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave;
1358   verifyFormat("var x = \"foo\";", LeaveQuotes);
1359   verifyFormat("var x = 'foo';", LeaveQuotes);
1360 }
1361 
1362 TEST_F(FormatTestJS, SupportShebangLines) {
1363   verifyFormat("#!/usr/bin/env node\n"
1364                "var x = hello();",
1365                "#!/usr/bin/env node\n"
1366                "var x   =  hello();");
1367 }
1368 
1369 TEST_F(FormatTestJS, NonNullAssertionOperator) {
1370   verifyFormat("let x = foo!.bar();\n");
1371   verifyFormat("let x = foo ? bar! : baz;\n");
1372   verifyFormat("let x = !foo;\n");
1373   verifyFormat("let x = foo[0]!;\n");
1374   verifyFormat("let x = (foo)!;\n");
1375   verifyFormat("let x = {foo: 1}!;\n");
1376 }
1377 
1378 TEST_F(FormatTestJS, Conditional) {
1379   verifyFormat("y = x ? 1 : 2;");
1380   verifyFormat("x ? 1 : 2;");
1381   verifyFormat("class Foo {\n"
1382                "  field = true ? 1 : 2;\n"
1383                "  method(a = true ? 1 : 2) {}\n"
1384                "}");
1385 }
1386 
1387 } // end namespace tooling
1388 } // end namespace clang
1389