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