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