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