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