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