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