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     FormattingAttemptStatus Status;
28     tooling::Replacements Replaces =
29         reformat(Style, Code, Ranges, "<stdin>", &Status);
30     EXPECT_TRUE(Status.FormatComplete);
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("of(null);");
136   verifyFormat("import {of} from 'x';");
137   verifyFormat("x.in() = 1;");
138   verifyFormat("x.let() = 1;");
139   verifyFormat("x.var() = 1;");
140   verifyFormat("x.for() = 1;");
141   verifyFormat("x.as() = 1;");
142   verifyFormat("x = {\n"
143                "  a: 12,\n"
144                "  interface: 1,\n"
145                "  switch: 1,\n"
146                "};");
147   verifyFormat("var struct = 2;");
148   verifyFormat("var union = 2;");
149   verifyFormat("var interface = 2;");
150   verifyFormat("interface = 2;");
151   verifyFormat("x = interface instanceof y;");
152 }
153 
154 TEST_F(FormatTestJS, ReservedWordsMethods) {
155   verifyFormat(
156       "class X {\n"
157       "  delete() {\n"
158       "    x();\n"
159       "  }\n"
160       "  interface() {\n"
161       "    x();\n"
162       "  }\n"
163       "  let() {\n"
164       "    x();\n"
165       "  }\n"
166       "}\n");
167 }
168 
169 TEST_F(FormatTestJS, CppKeywords) {
170   // Make sure we don't mess stuff up because of C++ keywords.
171   verifyFormat("return operator && (aa);");
172   // .. or QT ones.
173   verifyFormat("slots: Slot[];");
174 }
175 
176 TEST_F(FormatTestJS, ES6DestructuringAssignment) {
177   verifyFormat("var [a, b, c] = [1, 2, 3];");
178   verifyFormat("const [a, b, c] = [1, 2, 3];");
179   verifyFormat("let [a, b, c] = [1, 2, 3];");
180   verifyFormat("var {a, b} = {a: 1, b: 2};");
181   verifyFormat("let {a, b} = {a: 1, b: 2};");
182 }
183 
184 TEST_F(FormatTestJS, ContainerLiterals) {
185   verifyFormat("var x = {\n"
186                "  y: function(a) {\n"
187                "    return a;\n"
188                "  }\n"
189                "};");
190   verifyFormat("return {\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                "};");
201   verifyFormat("return {\n"
202                "  a: a,\n"
203                "  link: function() {\n"
204                "    f();  //\n"
205                "  },\n"
206                "  link: function() {\n"
207                "    f();  //\n"
208                "  }\n"
209                "};");
210   verifyFormat("var stuff = {\n"
211                "  // comment for update\n"
212                "  update: false,\n"
213                "  // comment for modules\n"
214                "  modules: false,\n"
215                "  // comment for tasks\n"
216                "  tasks: false\n"
217                "};");
218   verifyFormat("return {\n"
219                "  'finish':\n"
220                "      //\n"
221                "      a\n"
222                "};");
223   verifyFormat("var obj = {\n"
224                "  fooooooooo: function(x) {\n"
225                "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
226                "  }\n"
227                "};");
228   // Simple object literal, as opposed to enum style below.
229   verifyFormat("var obj = {a: 123};");
230   // Enum style top level assignment.
231   verifyFormat("X = {\n  a: 123\n};");
232   verifyFormat("X.Y = {\n  a: 123\n};");
233   // But only on the top level, otherwise its a plain object literal assignment.
234   verifyFormat("function x() {\n"
235                "  y = {z: 1};\n"
236                "}");
237   verifyFormat("x = foo && {a: 123};");
238 
239   // Arrow functions in object literals.
240   verifyFormat("var x = {\n"
241                "  y: (a) => {\n"
242                "    return a;\n"
243                "  }\n"
244                "};");
245   verifyFormat("var x = {y: (a) => a};");
246 
247   // Methods in object literals.
248   verifyFormat("var x = {\n"
249                "  y(a: string): number {\n"
250                "    return a;\n"
251                "  }\n"
252                "};");
253   verifyFormat("var x = {\n"
254                "  y(a: string) {\n"
255                "    return a;\n"
256                "  }\n"
257                "};");
258 
259   // Computed keys.
260   verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};");
261   verifyFormat("var x = {\n"
262                "  [a]: 1,\n"
263                "  b: 2,\n"
264                "  [c]: 3,\n"
265                "};");
266 
267   // Object literals can leave out labels.
268   verifyFormat("f({a}, () => {\n"
269                "  g();  //\n"
270                "});");
271 
272   // Keys can be quoted.
273   verifyFormat("var x = {\n"
274                "  a: a,\n"
275                "  b: b,\n"
276                "  'c': c,\n"
277                "};");
278 
279   // Dict literals can skip the label names.
280   verifyFormat("var x = {\n"
281                "  aaa,\n"
282                "  aaa,\n"
283                "  aaa,\n"
284                "};");
285   verifyFormat("return {\n"
286                "  a,\n"
287                "  b: 'b',\n"
288                "  c,\n"
289                "};");
290 }
291 
292 TEST_F(FormatTestJS, MethodsInObjectLiterals) {
293   verifyFormat("var o = {\n"
294                "  value: 'test',\n"
295                "  get value() {  // getter\n"
296                "    return this.value;\n"
297                "  }\n"
298                "};");
299   verifyFormat("var o = {\n"
300                "  value: 'test',\n"
301                "  set value(val) {  // setter\n"
302                "    this.value = val;\n"
303                "  }\n"
304                "};");
305   verifyFormat("var o = {\n"
306                "  value: 'test',\n"
307                "  someMethod(val) {  // method\n"
308                "    doSomething(this.value + val);\n"
309                "  }\n"
310                "};");
311   verifyFormat("var o = {\n"
312                "  someMethod(val) {  // method\n"
313                "    doSomething(this.value + val);\n"
314                "  },\n"
315                "  someOtherMethod(val) {  // method\n"
316                "    doSomething(this.value + val);\n"
317                "  }\n"
318                "};");
319 }
320 
321 TEST_F(FormatTestJS, GettersSettersVisibilityKeywords) {
322   // Don't break after "protected"
323   verifyFormat("class X {\n"
324                "  protected get getter():\n"
325                "      number {\n"
326                "    return 1;\n"
327                "  }\n"
328                "}",
329                getGoogleJSStyleWithColumns(12));
330   // Don't break after "get"
331   verifyFormat("class X {\n"
332                "  protected get someReallyLongGetterName():\n"
333                "      number {\n"
334                "    return 1;\n"
335                "  }\n"
336                "}",
337                getGoogleJSStyleWithColumns(40));
338 }
339 
340 TEST_F(FormatTestJS, SpacesInContainerLiterals) {
341   verifyFormat("var arr = [1, 2, 3];");
342   verifyFormat("f({a: 1, b: 2, c: 3});");
343 
344   verifyFormat("var object_literal_with_long_name = {\n"
345                "  a: 'aaaaaaaaaaaaaaaaaa',\n"
346                "  b: 'bbbbbbbbbbbbbbbbbb'\n"
347                "};");
348 
349   verifyFormat("f({a: 1, b: 2, c: 3});",
350                getChromiumStyle(FormatStyle::LK_JavaScript));
351   verifyFormat("f({'a': [{}]});");
352 }
353 
354 TEST_F(FormatTestJS, SingleQuotedStrings) {
355   verifyFormat("this.function('', true);");
356 }
357 
358 TEST_F(FormatTestJS, GoogScopes) {
359   verifyFormat("goog.scope(function() {\n"
360                "var x = a.b;\n"
361                "var y = c.d;\n"
362                "});  // goog.scope");
363   verifyFormat("goog.scope(function() {\n"
364                "// test\n"
365                "var x = 0;\n"
366                "// test\n"
367                "});");
368 }
369 
370 TEST_F(FormatTestJS, IIFEs) {
371   // Internal calling parens; no semi.
372   verifyFormat("(function() {\n"
373                "var a = 1;\n"
374                "}())");
375   // External calling parens; no semi.
376   verifyFormat("(function() {\n"
377                "var b = 2;\n"
378                "})()");
379   // Internal calling parens; with semi.
380   verifyFormat("(function() {\n"
381                "var c = 3;\n"
382                "}());");
383   // External calling parens; with semi.
384   verifyFormat("(function() {\n"
385                "var d = 4;\n"
386                "})();");
387 }
388 
389 TEST_F(FormatTestJS, GoogModules) {
390   verifyFormat("goog.module('this.is.really.absurdly.long');",
391                getGoogleJSStyleWithColumns(40));
392   verifyFormat("goog.require('this.is.really.absurdly.long');",
393                getGoogleJSStyleWithColumns(40));
394   verifyFormat("goog.provide('this.is.really.absurdly.long');",
395                getGoogleJSStyleWithColumns(40));
396   verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
397                getGoogleJSStyleWithColumns(40));
398   verifyFormat("goog.setTestOnly('this.is.really.absurdly.long');",
399                getGoogleJSStyleWithColumns(40));
400   verifyFormat("goog.forwardDeclare('this.is.really.absurdly.long');",
401                getGoogleJSStyleWithColumns(40));
402 
403   // These should be wrapped normally.
404   verifyFormat(
405       "var MyLongClassName =\n"
406       "    goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
407 }
408 
409 TEST_F(FormatTestJS, FormatsNamespaces) {
410   verifyFormat("namespace Foo {\n"
411                "  export let x = 1;\n"
412                "}\n");
413   verifyFormat("declare namespace Foo {\n"
414                "  export let x: number;\n"
415                "}\n");
416 }
417 
418 TEST_F(FormatTestJS, NamespacesMayNotWrap) {
419   verifyFormat("declare namespace foobarbaz {\n"
420                "}\n", getGoogleJSStyleWithColumns(18));
421   verifyFormat("declare module foobarbaz {\n"
422                "}\n", getGoogleJSStyleWithColumns(15));
423   verifyFormat("namespace foobarbaz {\n"
424                "}\n", getGoogleJSStyleWithColumns(10));
425   verifyFormat("module foobarbaz {\n"
426                "}\n", getGoogleJSStyleWithColumns(7));
427 }
428 
429 TEST_F(FormatTestJS, AmbientDeclarations) {
430   FormatStyle NineCols = getGoogleJSStyleWithColumns(9);
431   verifyFormat(
432       "declare class\n"
433       "    X {}",
434       NineCols);
435   verifyFormat(
436       "declare function\n"
437       "x();",  // TODO(martinprobst): should ideally be indented.
438       NineCols);
439   verifyFormat("declare function foo();\n"
440                "let x = 1;\n");
441   verifyFormat("declare function foo(): string;\n"
442                "let x = 1;\n");
443   verifyFormat("declare function foo(): {x: number};\n"
444                "let x = 1;\n");
445   verifyFormat("declare class X {}\n"
446                "let x = 1;\n");
447   verifyFormat("declare interface Y {}\n"
448                "let x = 1;\n");
449   verifyFormat(
450       "declare enum X {\n"
451       "}",
452       NineCols);
453   verifyFormat(
454       "declare let\n"
455       "    x: number;",
456       NineCols);
457 }
458 
459 TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
460   verifyFormat("function outer1(a, b) {\n"
461                "  function inner1(a, b) {\n"
462                "    return a;\n"
463                "  }\n"
464                "  inner1(a, b);\n"
465                "}\n"
466                "function outer2(a, b) {\n"
467                "  function inner2(a, b) {\n"
468                "    return a;\n"
469                "  }\n"
470                "  inner2(a, b);\n"
471                "}");
472   verifyFormat("function f() {}");
473   verifyFormat("function aFunction() {}\n"
474                "(function f() {\n"
475                "  var x = 1;\n"
476                "}());\n");
477   // Known issue: this should wrap after {}, but calculateBraceTypes
478   // misclassifies the first braces as a BK_BracedInit.
479   verifyFormat("function aFunction(){} {\n"
480                "  let x = 1;\n"
481                "  console.log(x);\n"
482                "}\n");
483 }
484 
485 TEST_F(FormatTestJS, GeneratorFunctions) {
486   verifyFormat("function* f() {\n"
487                "  let x = 1;\n"
488                "  yield x;\n"
489                "  yield* something();\n"
490                "  yield [1, 2];\n"
491                "  yield {a: 1};\n"
492                "}");
493   verifyFormat("function*\n"
494                "    f() {\n"
495                "}",
496                getGoogleJSStyleWithColumns(8));
497   verifyFormat("export function* f() {\n"
498                "  yield 1;\n"
499                "}\n");
500   verifyFormat("class X {\n"
501                "  * generatorMethod() {\n"
502                "    yield x;\n"
503                "  }\n"
504                "}");
505   verifyFormat("var x = {\n"
506                "  a: function*() {\n"
507                "    //\n"
508                "  }\n"
509                "}\n");
510 }
511 
512 TEST_F(FormatTestJS, AsyncFunctions) {
513   verifyFormat("async function f() {\n"
514                "  let x = 1;\n"
515                "  return fetch(x);\n"
516                "}");
517   verifyFormat("async function f() {\n"
518                "  return 1;\n"
519                "}\n"
520                "\n"
521                "function a() {\n"
522                "  return 1;\n"
523                "}\n",
524                "  async   function f() {\n"
525                "   return 1;\n"
526                "}\n"
527                "\n"
528                "   function a() {\n"
529                "  return   1;\n"
530                "}  \n");
531   verifyFormat("async function* f() {\n"
532                "  yield fetch(x);\n"
533                "}");
534   verifyFormat("export async function f() {\n"
535                "  return fetch(x);\n"
536                "}");
537   verifyFormat("let x = async () => f();");
538   verifyFormat("let x = async function() {\n"
539                "  f();\n"
540                "};");
541   verifyFormat("let x = async();");
542   verifyFormat("class X {\n"
543                "  async asyncMethod() {\n"
544                "    return fetch(1);\n"
545                "  }\n"
546                "}");
547   verifyFormat("function initialize() {\n"
548                "  // Comment.\n"
549                "  return async.then();\n"
550                "}\n");
551   verifyFormat("for await (const x of y) {\n"
552                "  console.log(x);\n"
553                "}\n");
554   verifyFormat("function asyncLoop() {\n"
555                "  for await (const x of y) {\n"
556                "    console.log(x);\n"
557                "  }\n"
558                "}\n");
559 }
560 
561 TEST_F(FormatTestJS, FunctionParametersTrailingComma) {
562   verifyFormat("function trailingComma(\n"
563                "    p1,\n"
564                "    p2,\n"
565                "    p3,\n"
566                ") {\n"
567                "  a;  //\n"
568                "}\n",
569                "function trailingComma(p1, p2, p3,) {\n"
570                "  a;  //\n"
571                "}\n");
572   verifyFormat("trailingComma(\n"
573                "    p1,\n"
574                "    p2,\n"
575                "    p3,\n"
576                ");\n",
577                "trailingComma(p1, p2, p3,);\n");
578   verifyFormat("trailingComma(\n"
579                "    p1  // hello\n"
580                ");\n",
581                "trailingComma(p1 // hello\n"
582                ");\n");
583 }
584 
585 TEST_F(FormatTestJS, ArrayLiterals) {
586   verifyFormat("var aaaaa: List<SomeThing> =\n"
587                "    [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];");
588   verifyFormat("return [\n"
589                "  aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
590                "  ccccccccccccccccccccccccccc\n"
591                "];");
592   verifyFormat("return [\n"
593                "  aaaa().bbbbbbbb('A'),\n"
594                "  aaaa().bbbbbbbb('B'),\n"
595                "  aaaa().bbbbbbbb('C'),\n"
596                "];");
597   verifyFormat("var someVariable = SomeFunction([\n"
598                "  aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
599                "  ccccccccccccccccccccccccccc\n"
600                "]);");
601   verifyFormat("var someVariable = SomeFunction([\n"
602                "  [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n"
603                "]);",
604                getGoogleJSStyleWithColumns(51));
605   verifyFormat("var someVariable = SomeFunction(aaaa, [\n"
606                "  aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
607                "  ccccccccccccccccccccccccccc\n"
608                "]);");
609   verifyFormat("var someVariable = SomeFunction(\n"
610                "    aaaa,\n"
611                "    [\n"
612                "      aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
613                "      cccccccccccccccccccccccccc\n"
614                "    ],\n"
615                "    aaaa);");
616   verifyFormat("var aaaa = aaaaa ||  // wrap\n"
617                "    [];");
618 
619   verifyFormat("someFunction([], {a: a});");
620 
621   verifyFormat("var string = [\n"
622                "  'aaaaaa',\n"
623                "  'bbbbbb',\n"
624                "].join('+');");
625 }
626 
627 TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) {
628   verifyFormat("var array = [\n"
629                "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
630                "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
631                "];");
632   verifyFormat("var array = someFunction([\n"
633                "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
634                "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
635                "]);");
636 }
637 
638 TEST_F(FormatTestJS, FunctionLiterals) {
639   FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
640   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
641   verifyFormat("doFoo(function() {});");
642   verifyFormat("doFoo(function() { return 1; });", Style);
643   verifyFormat("var func = function() {\n"
644                "  return 1;\n"
645                "};");
646   verifyFormat("var func =  //\n"
647                "    function() {\n"
648                "  return 1;\n"
649                "};");
650   verifyFormat("return {\n"
651                "  body: {\n"
652                "    setAttribute: function(key, val) { this[key] = val; },\n"
653                "    getAttribute: function(key) { return this[key]; },\n"
654                "    style: {direction: ''}\n"
655                "  }\n"
656                "};",
657                Style);
658   verifyFormat("abc = xyz ? function() {\n"
659                "  return 1;\n"
660                "} : function() {\n"
661                "  return -1;\n"
662                "};");
663 
664   verifyFormat("var closure = goog.bind(\n"
665                "    function() {  // comment\n"
666                "      foo();\n"
667                "      bar();\n"
668                "    },\n"
669                "    this, arg1IsReallyLongAndNeedsLineBreaks,\n"
670                "    arg3IsReallyLongAndNeedsLineBreaks);");
671   verifyFormat("var closure = goog.bind(function() {  // comment\n"
672                "  foo();\n"
673                "  bar();\n"
674                "}, this);");
675   verifyFormat("return {\n"
676                "  a: 'E',\n"
677                "  b: function() {\n"
678                "    return function() {\n"
679                "      f();  //\n"
680                "    };\n"
681                "  }\n"
682                "};");
683   verifyFormat("{\n"
684                "  var someVariable = function(x) {\n"
685                "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
686                "  };\n"
687                "}");
688   verifyFormat("someLooooooooongFunction(\n"
689                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
690                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
691                "    function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
692                "      // code\n"
693                "    });");
694 
695   verifyFormat("return {\n"
696                "  a: function SomeFunction() {\n"
697                "    // ...\n"
698                "    return 1;\n"
699                "  }\n"
700                "};");
701   verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
702                "    .then(goog.bind(function(aaaaaaaaaaa) {\n"
703                "      someFunction();\n"
704                "      someFunction();\n"
705                "    }, this), aaaaaaaaaaaaaaaaa);");
706 
707   verifyFormat("someFunction(goog.bind(function() {\n"
708                "  doSomething();\n"
709                "  doSomething();\n"
710                "}, this), goog.bind(function() {\n"
711                "  doSomething();\n"
712                "  doSomething();\n"
713                "}, this));");
714 
715   verifyFormat("SomeFunction(function() {\n"
716                "  foo();\n"
717                "  bar();\n"
718                "}.bind(this));");
719 
720   verifyFormat("SomeFunction((function() {\n"
721                "               foo();\n"
722                "               bar();\n"
723                "             }).bind(this));");
724 
725   // FIXME: This is bad, we should be wrapping before "function() {".
726   verifyFormat("someFunction(function() {\n"
727                "  doSomething();  // break\n"
728                "})\n"
729                "    .doSomethingElse(\n"
730                "        // break\n"
731                "    );");
732 
733   Style.ColumnLimit = 33;
734   verifyFormat("f({a: function() { return 1; }});", Style);
735   Style.ColumnLimit = 32;
736   verifyFormat("f({\n"
737                "  a: function() { return 1; }\n"
738                "});",
739                Style);
740 
741 }
742 
743 TEST_F(FormatTestJS, InliningFunctionLiterals) {
744   FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
745   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
746   verifyFormat("var func = function() {\n"
747                "  return 1;\n"
748                "};",
749                Style);
750   verifyFormat("var func = doSomething(function() { return 1; });", Style);
751   verifyFormat("var outer = function() {\n"
752                "  var inner = function() { return 1; }\n"
753                "};",
754                Style);
755   verifyFormat("function outer1(a, b) {\n"
756                "  function inner1(a, b) { return a; }\n"
757                "}",
758                Style);
759 
760   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
761   verifyFormat("var func = function() { return 1; };", Style);
762   verifyFormat("var func = doSomething(function() { return 1; });", Style);
763   verifyFormat(
764       "var outer = function() { var inner = function() { return 1; } };",
765       Style);
766   verifyFormat("function outer1(a, b) {\n"
767                "  function inner1(a, b) { return a; }\n"
768                "}",
769                Style);
770 
771   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
772   verifyFormat("var func = function() {\n"
773                "  return 1;\n"
774                "};",
775                Style);
776   verifyFormat("var func = doSomething(function() {\n"
777                "  return 1;\n"
778                "});",
779                Style);
780   verifyFormat("var outer = function() {\n"
781                "  var inner = function() {\n"
782                "    return 1;\n"
783                "  }\n"
784                "};",
785                Style);
786   verifyFormat("function outer1(a, b) {\n"
787                "  function inner1(a, b) {\n"
788                "    return a;\n"
789                "  }\n"
790                "}",
791                Style);
792 
793   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
794   verifyFormat("var func = function() {\n"
795                "  return 1;\n"
796                "};",
797                Style);
798 }
799 
800 TEST_F(FormatTestJS, MultipleFunctionLiterals) {
801   FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
802   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
803   verifyFormat("promise.then(\n"
804                "    function success() {\n"
805                "      doFoo();\n"
806                "      doBar();\n"
807                "    },\n"
808                "    function error() {\n"
809                "      doFoo();\n"
810                "      doBaz();\n"
811                "    },\n"
812                "    []);\n");
813   verifyFormat("promise.then(\n"
814                "    function success() {\n"
815                "      doFoo();\n"
816                "      doBar();\n"
817                "    },\n"
818                "    [],\n"
819                "    function error() {\n"
820                "      doFoo();\n"
821                "      doBaz();\n"
822                "    });\n");
823   verifyFormat("promise.then(\n"
824                "    [],\n"
825                "    function success() {\n"
826                "      doFoo();\n"
827                "      doBar();\n"
828                "    },\n"
829                "    function error() {\n"
830                "      doFoo();\n"
831                "      doBaz();\n"
832                "    });\n");
833 
834   verifyFormat("getSomeLongPromise()\n"
835                "    .then(function(value) { body(); })\n"
836                "    .thenCatch(function(error) {\n"
837                "      body();\n"
838                "      body();\n"
839                "    });",
840                Style);
841   verifyFormat("getSomeLongPromise()\n"
842                "    .then(function(value) {\n"
843                "      body();\n"
844                "      body();\n"
845                "    })\n"
846                "    .thenCatch(function(error) {\n"
847                "      body();\n"
848                "      body();\n"
849                "    });");
850 
851   verifyFormat("getSomeLongPromise()\n"
852                "    .then(function(value) { body(); })\n"
853                "    .thenCatch(function(error) { body(); });",
854                Style);
855 
856   verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n"
857                "    .aaaaaaa(function() {\n"
858                "      //\n"
859                "    })\n"
860                "    .bbbbbb();");
861 }
862 
863 TEST_F(FormatTestJS, ArrowFunctions) {
864   verifyFormat("var x = (a) => {\n"
865                "  return a;\n"
866                "};");
867   verifyFormat("var x = (a) => {\n"
868                "  function y() {\n"
869                "    return 42;\n"
870                "  }\n"
871                "  return a;\n"
872                "};");
873   verifyFormat("var x = (a: type): {some: type} => {\n"
874                "  return a;\n"
875                "};");
876   verifyFormat("var x = (a) => a;");
877   verifyFormat("return () => [];");
878   verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n"
879                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
880                "      (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
881                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n"
882                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
883                "};");
884   verifyFormat("var a = a.aaaaaaa(\n"
885                "    (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n"
886                "        aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
887   verifyFormat("var a = a.aaaaaaa(\n"
888                "    (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n"
889                "        aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n"
890                "        aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
891 
892   // FIXME: This is bad, we should be wrapping before "() => {".
893   verifyFormat("someFunction(() => {\n"
894                "  doSomething();  // break\n"
895                "})\n"
896                "    .doSomethingElse(\n"
897                "        // break\n"
898                "    );");
899 }
900 
901 TEST_F(FormatTestJS, ReturnStatements) {
902   verifyFormat("function() {\n"
903                "  return [hello, world];\n"
904                "}");
905 }
906 
907 TEST_F(FormatTestJS, ForLoops) {
908   verifyFormat("for (var i in [2, 3]) {\n"
909                "}");
910   verifyFormat("for (var i of [2, 3]) {\n"
911                "}");
912   verifyFormat("for (let {a, b} of x) {\n"
913                "}");
914   verifyFormat("for (let {a, b} in x) {\n"
915                "}");
916 }
917 
918 TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) {
919   // The following statements must not wrap, as otherwise the program meaning
920   // would change due to automatic semicolon insertion.
921   // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1.
922   verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10));
923   verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
924   verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10));
925   verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
926   verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10));
927   verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10));
928   verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10));
929   verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10));
930   verifyFormat("return [\n"
931                "  aaa\n"
932                "];",
933                getGoogleJSStyleWithColumns(12));
934 }
935 
936 TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {
937   verifyFormat("a\n"
938                "b;",
939                " a \n"
940                " b ;");
941   verifyFormat("a()\n"
942                "b;",
943                " a ()\n"
944                " b ;");
945   verifyFormat("a[b]\n"
946                "c;",
947                "a [b]\n"
948                "c ;");
949   verifyFormat("1\n"
950                "a;",
951                "1 \n"
952                "a ;");
953   verifyFormat("a\n"
954                "1;",
955                "a \n"
956                "1 ;");
957   verifyFormat("a\n"
958                "'x';",
959                "a \n"
960                " 'x';");
961   verifyFormat("a++\n"
962                "b;",
963                "a ++\n"
964                "b ;");
965   verifyFormat("a\n"
966                "!b && c;",
967                "a \n"
968                " ! b && c;");
969   verifyFormat("a\n"
970                "if (1) f();",
971                " a\n"
972                " if (1) f();");
973   verifyFormat("a\n"
974                "class X {}",
975                " a\n"
976                " class X {}");
977   verifyFormat("var a", "var\n"
978                         "a");
979   verifyFormat("x instanceof String", "x\n"
980                                       "instanceof\n"
981                                       "String");
982   verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n"
983                                           "  bar) {}");
984   verifyFormat("a = true\n"
985                "return 1",
986                "a = true\n"
987                "  return   1");
988   verifyFormat("a = 's'\n"
989                "return 1",
990                "a = 's'\n"
991                "  return   1");
992   verifyFormat("a = null\n"
993                "return 1",
994                "a = null\n"
995                "  return   1");
996   // Below "class Y {}" should ideally be on its own line.
997   verifyFormat(
998       "x = {\n"
999       "  a: 1\n"
1000       "} class Y {}",
1001       "  x  =  {a  : 1}\n"
1002       "   class  Y {  }");
1003   verifyFormat(
1004       "if (x) {\n"
1005       "}\n"
1006       "return 1",
1007       "if (x) {}\n"
1008       " return   1");
1009   verifyFormat(
1010       "if (x) {\n"
1011       "}\n"
1012       "class X {}",
1013       "if (x) {}\n"
1014       " class X {}");
1015 }
1016 
1017 TEST_F(FormatTestJS, ImportExportASI) {
1018   verifyFormat(
1019       "import {x} from 'y'\n"
1020       "export function z() {}",
1021       "import   {x} from 'y'\n"
1022       "  export function z() {}");
1023   // Below "class Y {}" should ideally be on its own line.
1024   verifyFormat(
1025       "export {x} class Y {}",
1026       "  export {x}\n"
1027       "  class  Y {\n}");
1028   verifyFormat(
1029       "if (x) {\n"
1030       "}\n"
1031       "export class Y {}",
1032       "if ( x ) { }\n"
1033       " export class Y {}");
1034 }
1035 
1036 TEST_F(FormatTestJS, ClosureStyleCasts) {
1037   verifyFormat("var x = /** @type {foo} */ (bar);");
1038 }
1039 
1040 TEST_F(FormatTestJS, TryCatch) {
1041   verifyFormat("try {\n"
1042                "  f();\n"
1043                "} catch (e) {\n"
1044                "  g();\n"
1045                "} finally {\n"
1046                "  h();\n"
1047                "}");
1048 
1049   // But, of course, "catch" is a perfectly fine function name in JavaScript.
1050   verifyFormat("someObject.catch();");
1051   verifyFormat("someObject.new();");
1052   verifyFormat("someObject.delete();");
1053 }
1054 
1055 TEST_F(FormatTestJS, StringLiteralConcatenation) {
1056   verifyFormat("var literal = 'hello ' +\n"
1057                "    'world';");
1058 }
1059 
1060 TEST_F(FormatTestJS, RegexLiteralClassification) {
1061   // Regex literals.
1062   verifyFormat("var regex = /abc/;");
1063   verifyFormat("f(/abc/);");
1064   verifyFormat("f(abc, /abc/);");
1065   verifyFormat("some_map[/abc/];");
1066   verifyFormat("var x = a ? /abc/ : /abc/;");
1067   verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
1068   verifyFormat("var x = !/abc/.test(y);");
1069   verifyFormat("var x = foo()! / 10;");
1070   verifyFormat("var x = a && /abc/.test(y);");
1071   verifyFormat("var x = a || /abc/.test(y);");
1072   verifyFormat("var x = a + /abc/.search(y);");
1073   verifyFormat("/abc/.search(y);");
1074   verifyFormat("var regexs = {/abc/, /abc/};");
1075   verifyFormat("return /abc/;");
1076 
1077   // Not regex literals.
1078   verifyFormat("var a = a / 2 + b / 3;");
1079   verifyFormat("var a = a++ / 2;");
1080   // Prefix unary can operate on regex literals, not that it makes sense.
1081   verifyFormat("var a = ++/a/;");
1082 
1083   // This is a known issue, regular expressions are incorrectly detected if
1084   // directly following a closing parenthesis.
1085   verifyFormat("if (foo) / bar /.exec(baz);");
1086 }
1087 
1088 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
1089   verifyFormat("var regex = /=/;");
1090   verifyFormat("var regex = /a*/;");
1091   verifyFormat("var regex = /a+/;");
1092   verifyFormat("var regex = /a?/;");
1093   verifyFormat("var regex = /.a./;");
1094   verifyFormat("var regex = /a\\*/;");
1095   verifyFormat("var regex = /^a$/;");
1096   verifyFormat("var regex = /\\/a/;");
1097   verifyFormat("var regex = /(?:x)/;");
1098   verifyFormat("var regex = /x(?=y)/;");
1099   verifyFormat("var regex = /x(?!y)/;");
1100   verifyFormat("var regex = /x|y/;");
1101   verifyFormat("var regex = /a{2}/;");
1102   verifyFormat("var regex = /a{1,3}/;");
1103 
1104   verifyFormat("var regex = /[abc]/;");
1105   verifyFormat("var regex = /[^abc]/;");
1106   verifyFormat("var regex = /[\\b]/;");
1107   verifyFormat("var regex = /[/]/;");
1108   verifyFormat("var regex = /[\\/]/;");
1109   verifyFormat("var regex = /\\[/;");
1110   verifyFormat("var regex = /\\\\[/]/;");
1111   verifyFormat("var regex = /}[\"]/;");
1112   verifyFormat("var regex = /}[/\"]/;");
1113   verifyFormat("var regex = /}[\"/]/;");
1114 
1115   verifyFormat("var regex = /\\b/;");
1116   verifyFormat("var regex = /\\B/;");
1117   verifyFormat("var regex = /\\d/;");
1118   verifyFormat("var regex = /\\D/;");
1119   verifyFormat("var regex = /\\f/;");
1120   verifyFormat("var regex = /\\n/;");
1121   verifyFormat("var regex = /\\r/;");
1122   verifyFormat("var regex = /\\s/;");
1123   verifyFormat("var regex = /\\S/;");
1124   verifyFormat("var regex = /\\t/;");
1125   verifyFormat("var regex = /\\v/;");
1126   verifyFormat("var regex = /\\w/;");
1127   verifyFormat("var regex = /\\W/;");
1128   verifyFormat("var regex = /a(a)\\1/;");
1129   verifyFormat("var regex = /\\0/;");
1130   verifyFormat("var regex = /\\\\/g;");
1131   verifyFormat("var regex = /\\a\\\\/g;");
1132   verifyFormat("var regex = /\a\\//g;");
1133   verifyFormat("var regex = /a\\//;\n"
1134                "var x = 0;");
1135   verifyFormat("var regex = /'/g;", "var regex = /'/g ;");
1136   verifyFormat("var regex = /'/g;  //'", "var regex = /'/g ; //'");
1137   verifyFormat("var regex = /\\/*/;\n"
1138                "var x = 0;",
1139                "var regex = /\\/*/;\n"
1140                "var x=0;");
1141   verifyFormat("var x = /a\\//;", "var x = /a\\//  \n;");
1142   verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16));
1143   verifyFormat("var regex =\n"
1144                "    /\"/;",
1145                getGoogleJSStyleWithColumns(15));
1146   verifyFormat("var regex =  //\n"
1147                "    /a/;");
1148   verifyFormat("var regexs = [\n"
1149                "  /d/,   //\n"
1150                "  /aa/,  //\n"
1151                "];");
1152 }
1153 
1154 TEST_F(FormatTestJS, RegexLiteralModifiers) {
1155   verifyFormat("var regex = /abc/g;");
1156   verifyFormat("var regex = /abc/i;");
1157   verifyFormat("var regex = /abc/m;");
1158   verifyFormat("var regex = /abc/y;");
1159 }
1160 
1161 TEST_F(FormatTestJS, RegexLiteralLength) {
1162   verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
1163                getGoogleJSStyleWithColumns(60));
1164   verifyFormat("var regex =\n"
1165                "    /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
1166                getGoogleJSStyleWithColumns(60));
1167   verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
1168                getGoogleJSStyleWithColumns(50));
1169 }
1170 
1171 TEST_F(FormatTestJS, RegexLiteralExamples) {
1172   verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
1173 }
1174 
1175 TEST_F(FormatTestJS, IgnoresMpegTS) {
1176   std::string MpegTS(200, ' ');
1177   MpegTS.replace(0, strlen("nearlyLooks  +   like +   ts + code;  "),
1178                  "nearlyLooks  +   like +   ts + code;  ");
1179   MpegTS[0] = 0x47;
1180   MpegTS[188] = 0x47;
1181   verifyFormat(MpegTS, MpegTS);
1182 }
1183 
1184 TEST_F(FormatTestJS, TypeAnnotations) {
1185   verifyFormat("var x: string;");
1186   verifyFormat("var x: {a: string; b: number;} = {};");
1187   verifyFormat("function x(): string {\n  return 'x';\n}");
1188   verifyFormat("function x(): {x: string} {\n  return {x: 'x'};\n}");
1189   verifyFormat("function x(y: string): string {\n  return 'x';\n}");
1190   verifyFormat("for (var y: string in x) {\n  x();\n}");
1191   verifyFormat("for (var y: string of x) {\n  x();\n}");
1192   verifyFormat("function x(y: {a?: number;} = {}): number {\n"
1193                "  return 12;\n"
1194                "}");
1195   verifyFormat("((a: string, b: number): string => a + b);");
1196   verifyFormat("var x: (y: number) => string;");
1197   verifyFormat("var x: P<string, (a: number) => string>;");
1198   verifyFormat("var x = {\n"
1199                "  y: function(): z {\n"
1200                "    return 1;\n"
1201                "  }\n"
1202                "};");
1203   verifyFormat("var x = {\n"
1204                "  y: function(): {a: number} {\n"
1205                "    return 1;\n"
1206                "  }\n"
1207                "};");
1208   verifyFormat("function someFunc(args: string[]):\n"
1209                "    {longReturnValue: string[]} {}",
1210                getGoogleJSStyleWithColumns(60));
1211   verifyFormat(
1212       "var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])\n"
1213       "                    .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1214 }
1215 
1216 TEST_F(FormatTestJS, UnionIntersectionTypes) {
1217   verifyFormat("let x: A|B = A | B;");
1218   verifyFormat("let x: A&B|C = A & B;");
1219   verifyFormat("let x: Foo<A|B> = new Foo<A|B>();");
1220   verifyFormat("function(x: A|B): C&D {}");
1221   verifyFormat("function(x: A|B = A | B): C&D {}");
1222   verifyFormat("function x(path: number|string) {}");
1223   verifyFormat("function x(): string|number {}");
1224   verifyFormat("type Foo = Bar|Baz;");
1225   verifyFormat("type Foo = Bar<X>|Baz;");
1226   verifyFormat("type Foo = (Bar<X>|Baz);");
1227   verifyFormat("let x: Bar|Baz;");
1228   verifyFormat("let x: Bar<X>|Baz;");
1229   verifyFormat("let x: (Foo|Bar)[];");
1230 }
1231 
1232 TEST_F(FormatTestJS, ClassDeclarations) {
1233   verifyFormat("class C {\n  x: string = 12;\n}");
1234   verifyFormat("class C {\n  x(): string => 12;\n}");
1235   verifyFormat("class C {\n  ['x' + 2]: string = 12;\n}");
1236   verifyFormat("class C {\n  private x: string = 12;\n}");
1237   verifyFormat("class C {\n  private static x: string = 12;\n}");
1238   verifyFormat("class C {\n  static x(): string {\n    return 'asd';\n  }\n}");
1239   verifyFormat("class C extends P implements I {}");
1240   verifyFormat("class C extends p.P implements i.I {}");
1241   verifyFormat(
1242       "x(class {\n"
1243       "  a(): A {}\n"
1244       "});");
1245   verifyFormat("class Test {\n"
1246                "  aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n"
1247                "      aaaaaaaaaaaaaaaaaaaaaa {}\n"
1248                "}");
1249   verifyFormat("foo = class Name {\n"
1250                "  constructor() {}\n"
1251                "};");
1252   verifyFormat("foo = class {\n"
1253                "  constructor() {}\n"
1254                "};");
1255   verifyFormat("class C {\n"
1256                "  x: {y: Z;} = {};\n"
1257                "  private y: {y: Z;} = {};\n"
1258                "}");
1259 
1260   // ':' is not a type declaration here.
1261   verifyFormat("class X {\n"
1262                "  subs = {\n"
1263                "    'b': {\n"
1264                "      'c': 1,\n"
1265                "    },\n"
1266                "  };\n"
1267                "}");
1268   verifyFormat("@Component({\n"
1269                "  moduleId: module.id,\n"
1270                "})\n"
1271                "class SessionListComponent implements OnDestroy, OnInit {\n"
1272                "}");
1273 }
1274 
1275 TEST_F(FormatTestJS, InterfaceDeclarations) {
1276   verifyFormat("interface I {\n"
1277                "  x: string;\n"
1278                "  enum: string[];\n"
1279                "  enum?: string[];\n"
1280                "}\n"
1281                "var y;");
1282   // Ensure that state is reset after parsing the interface.
1283   verifyFormat("interface a {}\n"
1284                "export function b() {}\n"
1285                "var x;");
1286 
1287   // Arrays of object type literals.
1288   verifyFormat("interface I {\n"
1289                "  o: {}[];\n"
1290                "}");
1291 }
1292 
1293 TEST_F(FormatTestJS, EnumDeclarations) {
1294   verifyFormat("enum Foo {\n"
1295                "  A = 1,\n"
1296                "  B\n"
1297                "}");
1298   verifyFormat("export /* somecomment*/ enum Foo {\n"
1299                "  A = 1,\n"
1300                "  B\n"
1301                "}");
1302   verifyFormat("enum Foo {\n"
1303                "  A = 1,  // comment\n"
1304                "  B\n"
1305                "}\n"
1306                "var x = 1;");
1307 }
1308 
1309 TEST_F(FormatTestJS, MetadataAnnotations) {
1310   verifyFormat("@A\nclass C {\n}");
1311   verifyFormat("@A({arg: 'value'})\nclass C {\n}");
1312   verifyFormat("@A\n@B\nclass C {\n}");
1313   verifyFormat("class C {\n  @A x: string;\n}");
1314   verifyFormat("class C {\n"
1315                "  @A\n"
1316                "  private x(): string {\n"
1317                "    return 'y';\n"
1318                "  }\n"
1319                "}");
1320   verifyFormat("class C {\n"
1321                "  private x(@A x: string) {}\n"
1322                "}");
1323   verifyFormat("class X {}\n"
1324                "class Y {}");
1325   verifyFormat("class X {\n"
1326                "  @property() private isReply = false;\n"
1327                "}\n");
1328 }
1329 
1330 TEST_F(FormatTestJS, TypeAliases) {
1331   verifyFormat("type X = number;\n"
1332                "class C {}");
1333   verifyFormat("type X<Y> = Z<Y>;");
1334   verifyFormat("type X = {\n"
1335                "  y: number\n"
1336                "};\n"
1337                "class C {}");
1338 }
1339 
1340 TEST_F(FormatTestJS, TypeInterfaceLineWrapping) {
1341   const FormatStyle &Style = getGoogleJSStyleWithColumns(20);
1342   verifyFormat("type LongTypeIsReallyUnreasonablyLong =\n"
1343                "    string;\n",
1344                "type LongTypeIsReallyUnreasonablyLong = string;\n",
1345                Style);
1346   verifyFormat(
1347       "interface AbstractStrategyFactoryProvider {\n"
1348       "  a: number\n"
1349       "}\n",
1350       "interface AbstractStrategyFactoryProvider { a: number }\n",
1351       Style);
1352 }
1353 
1354 TEST_F(FormatTestJS, Modules) {
1355   verifyFormat("import SomeThing from 'some/module.js';");
1356   verifyFormat("import {X, Y} from 'some/module.js';");
1357   verifyFormat("import a, {X, Y} from 'some/module.js';");
1358   verifyFormat("import {X, Y,} from 'some/module.js';");
1359   verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
1360   // Ensure Automatic Semicolon Insertion does not break on "as\n".
1361   verifyFormat("import {X as myX} from 'm';", "import {X as\n"
1362                                               " myX} from 'm';");
1363   verifyFormat("import * as lib from 'some/module.js';");
1364   verifyFormat("var x = {import: 1};\nx.import = 2;");
1365 
1366   verifyFormat("export function fn() {\n"
1367                "  return 'fn';\n"
1368                "}");
1369   verifyFormat("export function A() {}\n"
1370                "export default function B() {}\n"
1371                "export function C() {}");
1372   verifyFormat("export default () => {\n"
1373                "  let x = 1;\n"
1374                "  return x;\n"
1375                "}");
1376   verifyFormat("export const x = 12;");
1377   verifyFormat("export default class X {}");
1378   verifyFormat("export {X, Y} from 'some/module.js';");
1379   verifyFormat("export {X, Y,} from 'some/module.js';");
1380   verifyFormat("export {SomeVeryLongExport as X, "
1381                "SomeOtherVeryLongExport as Y} from 'some/module.js';");
1382   // export without 'from' is wrapped.
1383   verifyFormat("export let someRatherLongVariableName =\n"
1384                "    someSurprisinglyLongVariable + someOtherRatherLongVar;");
1385   // ... but not if from is just an identifier.
1386   verifyFormat("export {\n"
1387                "  from as from,\n"
1388                "  someSurprisinglyLongVariable as\n"
1389                "      from\n"
1390                "};",
1391                getGoogleJSStyleWithColumns(20));
1392   verifyFormat("export class C {\n"
1393                "  x: number;\n"
1394                "  y: string;\n"
1395                "}");
1396   verifyFormat("export class X { y: number; }");
1397   verifyFormat("export abstract class X { y: number; }");
1398   verifyFormat("export default class X { y: number }");
1399   verifyFormat("export default function() {\n  return 1;\n}");
1400   verifyFormat("export var x = 12;");
1401   verifyFormat("class C {}\n"
1402                "export function f() {}\n"
1403                "var v;");
1404   verifyFormat("export var x: number = 12;");
1405   verifyFormat("export const y = {\n"
1406                "  a: 1,\n"
1407                "  b: 2\n"
1408                "};");
1409   verifyFormat("export enum Foo {\n"
1410                "  BAR,\n"
1411                "  // adsdasd\n"
1412                "  BAZ\n"
1413                "}");
1414   verifyFormat("export default [\n"
1415                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1416                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
1417                "];");
1418   verifyFormat("export default [];");
1419   verifyFormat("export default () => {};");
1420   verifyFormat("export interface Foo { foo: number; }\n"
1421                "export class Bar {\n"
1422                "  blah(): string {\n"
1423                "    return this.blah;\n"
1424                "  };\n"
1425                "}");
1426 }
1427 
1428 TEST_F(FormatTestJS, ImportWrapping) {
1429   verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,"
1430                " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying"
1431                "} from 'some/module.js';");
1432   FormatStyle Style = getGoogleJSStyleWithColumns(80);
1433   Style.JavaScriptWrapImports = true;
1434   verifyFormat("import {\n"
1435                "  VeryLongImportsAreAnnoying,\n"
1436                "  VeryLongImportsAreAnnoying,\n"
1437                "  VeryLongImportsAreAnnoying,\n"
1438                "} from 'some/module.js';",
1439                Style);
1440   verifyFormat("import {\n"
1441                "  A,\n"
1442                "  A,\n"
1443                "} from 'some/module.js';",
1444                Style);
1445   verifyFormat("export {\n"
1446                "  A,\n"
1447                "  A,\n"
1448                "} from 'some/module.js';",
1449                Style);
1450 }
1451 
1452 TEST_F(FormatTestJS, TemplateStrings) {
1453   // Keeps any whitespace/indentation within the template string.
1454   verifyFormat("var x = `hello\n"
1455             "     ${name}\n"
1456             "  !`;",
1457             "var x    =    `hello\n"
1458                    "     ${  name    }\n"
1459                    "  !`;");
1460 
1461   verifyFormat("var x =\n"
1462                "    `hello ${world}` >= some();",
1463                getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
1464   verifyFormat("var x = `hello ${world}` >= some();",
1465                getGoogleJSStyleWithColumns(35)); // Barely fits.
1466   verifyFormat("var x = `hellö ${wörld}` >= söme();",
1467                getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8.
1468   verifyFormat("var x = `hello\n"
1469             "  ${world}` >=\n"
1470             "    some();",
1471             "var x =\n"
1472                    "    `hello\n"
1473                    "  ${world}` >= some();",
1474                    getGoogleJSStyleWithColumns(21)); // Barely doesn't fit.
1475   verifyFormat("var x = `hello\n"
1476             "  ${world}` >= some();",
1477             "var x =\n"
1478                    "    `hello\n"
1479                    "  ${world}` >= some();",
1480                    getGoogleJSStyleWithColumns(22)); // Barely fits.
1481 
1482   verifyFormat("var x =\n"
1483                "    `h`;",
1484                getGoogleJSStyleWithColumns(11));
1485   verifyFormat("var x =\n    `multi\n  line`;", "var x = `multi\n  line`;",
1486                getGoogleJSStyleWithColumns(13));
1487   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1488                "    `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);");
1489   // Repro for an obscure width-miscounting issue with template strings.
1490   verifyFormat(
1491       "someLongVariable =\n"
1492       "    "
1493       "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;",
1494       "someLongVariable = "
1495       "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;");
1496 
1497   // Make sure template strings get a proper ColumnWidth assigned, even if they
1498   // are first token in line.
1499   verifyFormat(
1500       "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
1501       "    `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;");
1502 
1503   // Two template strings.
1504   verifyFormat("var x = `hello` == `hello`;");
1505 
1506   // Comments in template strings.
1507   verifyFormat("var x = `//a`;\n"
1508             "var y;",
1509             "var x =\n `//a`;\n"
1510                    "var y  ;");
1511   verifyFormat("var x = `/*a`;\n"
1512                "var y;",
1513                "var x =\n `/*a`;\n"
1514                "var y;");
1515   // Unterminated string literals in a template string.
1516   verifyFormat("var x = `'`;  // comment with matching quote '\n"
1517                "var y;");
1518   verifyFormat("var x = `\"`;  // comment with matching quote \"\n"
1519                "var y;");
1520   verifyFormat("it(`'aaaaaaaaaaaaaaa   `, aaaaaaaaa);",
1521                "it(`'aaaaaaaaaaaaaaa   `,   aaaaaaaaa) ;",
1522                getGoogleJSStyleWithColumns(40));
1523   // Backticks in a comment - not a template string.
1524   verifyFormat("var x = 1  // `/*a`;\n"
1525                "    ;",
1526                "var x =\n 1  // `/*a`;\n"
1527                "    ;");
1528   verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */");
1529   // Comment spans multiple template strings.
1530   verifyFormat("var x = `/*a`;\n"
1531                "var y = ` */ `;",
1532                "var x =\n `/*a`;\n"
1533                "var y =\n ` */ `;");
1534   // Escaped backtick.
1535   verifyFormat("var x = ` \\` a`;\n"
1536                "var y;",
1537                "var x = ` \\` a`;\n"
1538                "var y;");
1539   // Escaped dollar.
1540   verifyFormat("var x = ` \\${foo}`;\n");
1541 
1542   // The token stream can contain two string_literals in sequence, but that
1543   // doesn't mean that they are implicitly concatenated in JavaScript.
1544   verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;");
1545 
1546   // Ensure that scopes are appropriately set around evaluated expressions in
1547   // template strings.
1548   verifyFormat("var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa\n"
1549                "         aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;",
1550                "var f = `aaaaaaaaaaaaa:${aaaaaaa.  aaaaa} aaaaaaaa\n"
1551                "         aaaaaaaaaaaaa:${  aaaaaaa. aaaaa} aaaaaaaa`;");
1552   verifyFormat("var x = someFunction(`${})`)  //\n"
1553                "            .oooooooooooooooooon();");
1554   verifyFormat("var x = someFunction(`${aaaa}${\n"
1555                "                               aaaaa(  //\n"
1556                "                                   aaaaa)\n"
1557                "                             })`);");
1558 }
1559 
1560 TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
1561   verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${\n"
1562                "                               aaaaa +  //\n"
1563                "                               bbbb\n"
1564                "                             }`;",
1565                "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa +  //\n"
1566                "                               bbbb}`;");
1567   verifyFormat("var f = `\n"
1568                "  aaaaaaaaaaaaaaaaaa: ${\n"
1569                "                        aaaaa +  //\n"
1570                "                        bbbb\n"
1571                "                      }`;",
1572                "var f  =  `\n"
1573                "  aaaaaaaaaaaaaaaaaa: ${   aaaaa  +  //\n"
1574                "                        bbbb }`;");
1575   verifyFormat("var f = `\n"
1576                "  aaaaaaaaaaaaaaaaaa: ${\n"
1577                "                        someFunction(\n"
1578                "                            aaaaa +  //\n"
1579                "                            bbbb)\n"
1580                "                      }`;",
1581                "var f  =  `\n"
1582                "  aaaaaaaaaaaaaaaaaa: ${someFunction (\n"
1583                "                            aaaaa  +   //\n"
1584                "                            bbbb)}`;");
1585 
1586   // It might be preferable to wrap before "someFunction".
1587   verifyFormat("var f = `\n"
1588                "  aaaaaaaaaaaaaaaaaa: ${someFunction({\n"
1589                "                          aaaa: aaaaa,\n"
1590                "                          bbbb: bbbbb,\n"
1591                "                        })}`;",
1592                "var f  =  `\n"
1593                "  aaaaaaaaaaaaaaaaaa: ${someFunction ({\n"
1594                "                          aaaa:  aaaaa,\n"
1595                "                          bbbb:  bbbbb,\n"
1596                "                        })}`;");
1597 }
1598 
1599 TEST_F(FormatTestJS, TemplateStringASI) {
1600   verifyFormat("var x = `hello${world}`;", "var x = `hello${\n"
1601                                            "    world\n"
1602                                            "}`;");
1603 }
1604 
1605 TEST_F(FormatTestJS, NestedTemplateStrings) {
1606   verifyFormat(
1607       "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;");
1608   verifyFormat("var x = `he${({text: 'll'}.text)}o`;");
1609 
1610   // Crashed at some point.
1611   verifyFormat("}");
1612 }
1613 
1614 TEST_F(FormatTestJS, TaggedTemplateStrings) {
1615   verifyFormat("var x = html`<ul>`;");
1616 }
1617 
1618 TEST_F(FormatTestJS, CastSyntax) {
1619   verifyFormat("var x = <type>foo;");
1620   verifyFormat("var x = foo as type;");
1621   verifyFormat("let x = (a + b) as\n"
1622                "    LongTypeIsLong;",
1623                getGoogleJSStyleWithColumns(20));
1624   verifyFormat("foo = <Bar[]>[\n"
1625                "  1,  //\n"
1626                "  2\n"
1627                "];");
1628   verifyFormat("var x = [{x: 1} as type];");
1629   verifyFormat("x = x as [a, b];");
1630   verifyFormat("x = x as {a: string};");
1631   verifyFormat("x = x as (string);");
1632   verifyFormat("x = x! as (string);");
1633   verifyFormat("var x = something.someFunction() as\n"
1634                "    something;",
1635                getGoogleJSStyleWithColumns(40));
1636 }
1637 
1638 TEST_F(FormatTestJS, TypeArguments) {
1639   verifyFormat("class X<Y> {}");
1640   verifyFormat("new X<Y>();");
1641   verifyFormat("foo<Y>(a);");
1642   verifyFormat("var x: X<Y>[];");
1643   verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
1644   verifyFormat("function f(a: List<any> = null) {}");
1645   verifyFormat("function f(): List<any> {}");
1646   verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n"
1647                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}");
1648   verifyFormat("function aaaaaaaaaa(\n"
1649                "    aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n"
1650                "    aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n"
1651                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}");
1652 }
1653 
1654 TEST_F(FormatTestJS, UserDefinedTypeGuards) {
1655   verifyFormat(
1656       "function foo(check: Object):\n"
1657       "    check is {foo: string, bar: string, baz: string, foobar: string} {\n"
1658       "  return 'bar' in check;\n"
1659       "}\n");
1660 }
1661 
1662 TEST_F(FormatTestJS, OptionalTypes) {
1663   verifyFormat("function x(a?: b, c?, d?) {}");
1664   verifyFormat("class X {\n"
1665                "  y?: z;\n"
1666                "  z?;\n"
1667                "}");
1668   verifyFormat("interface X {\n"
1669                "  y?(): z;\n"
1670                "}");
1671   verifyFormat("constructor({aa}: {\n"
1672                "  aa?: string,\n"
1673                "  aaaaaaaa?: string,\n"
1674                "  aaaaaaaaaaaaaaa?: boolean,\n"
1675                "  aaaaaa?: List<string>\n"
1676                "}) {}");
1677 }
1678 
1679 TEST_F(FormatTestJS, IndexSignature) {
1680   verifyFormat("var x: {[k: string]: v};");
1681 }
1682 
1683 TEST_F(FormatTestJS, WrapAfterParen) {
1684   verifyFormat("xxxxxxxxxxx(\n"
1685                "    aaa, aaa);",
1686                getGoogleJSStyleWithColumns(20));
1687   verifyFormat("xxxxxxxxxxx(\n"
1688                "    aaa, aaa, aaa,\n"
1689                "    aaa, aaa, aaa);",
1690                getGoogleJSStyleWithColumns(20));
1691   verifyFormat("xxxxxxxxxxx(\n"
1692                "    aaaaaaaaaaaaaaaaaaaaaaaa,\n"
1693                "    function(x) {\n"
1694                "      y();  //\n"
1695                "    });",
1696                getGoogleJSStyleWithColumns(40));
1697   verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
1698                "       bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
1699 }
1700 
1701 TEST_F(FormatTestJS, JSDocAnnotations) {
1702   verifyFormat("/**\n"
1703                " * @export {this.is.a.long.path.to.a.Type}\n"
1704                " */",
1705                "/**\n"
1706                " * @export {this.is.a.long.path.to.a.Type}\n"
1707                " */",
1708                getGoogleJSStyleWithColumns(20));
1709   verifyFormat("/**\n"
1710                " * @mods {this.is.a.long.path.to.a.Type}\n"
1711                " */",
1712                "/**\n"
1713                " * @mods {this.is.a.long.path.to.a.Type}\n"
1714                " */",
1715                getGoogleJSStyleWithColumns(20));
1716   verifyFormat("/**\n"
1717                " * @param {this.is.a.long.path.to.a.Type}\n"
1718                " */",
1719                "/**\n"
1720                " * @param {this.is.a.long.path.to.a.Type}\n"
1721                " */",
1722                getGoogleJSStyleWithColumns(20));
1723   verifyFormat("/**\n"
1724                " * @see http://very/very/long/url/is/long\n"
1725                " */",
1726                "/**\n"
1727                " * @see http://very/very/long/url/is/long\n"
1728                " */",
1729                getGoogleJSStyleWithColumns(20));
1730   verifyFormat(
1731       "/**\n"
1732       " * @param This is a\n"
1733       " * long comment but\n"
1734       " * no type\n"
1735       " */",
1736       "/**\n"
1737       " * @param This is a long comment but no type\n"
1738       " */",
1739       getGoogleJSStyleWithColumns(20));
1740   // Don't break @param line, but reindent it and reflow unrelated lines.
1741   verifyFormat("{\n"
1742                "  /**\n"
1743                "   * long long long\n"
1744                "   * long\n"
1745                "   * @param {this.is.a.long.path.to.a.Type} a\n"
1746                "   * long long long\n"
1747                "   * long long\n"
1748                "   */\n"
1749                "  function f(a) {}\n"
1750                "}",
1751                "{\n"
1752                "/**\n"
1753                " * long long long long\n"
1754                " * @param {this.is.a.long.path.to.a.Type} a\n"
1755                " * long long long long\n"
1756                " * long\n"
1757                " */\n"
1758                "  function f(a) {}\n"
1759                "}",
1760                getGoogleJSStyleWithColumns(20));
1761 }
1762 
1763 TEST_F(FormatTestJS, RequoteStringsSingle) {
1764   verifyFormat("var x = 'foo';", "var x = \"foo\";");
1765   verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";");
1766   verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";");
1767   verifyFormat(
1768       "var x =\n"
1769       "    'foo\\'';",
1770       // Code below is 15 chars wide, doesn't fit into the line with the
1771       // \ escape added.
1772       "var x = \"foo'\";", getGoogleJSStyleWithColumns(15));
1773   // Removes no-longer needed \ escape from ".
1774   verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";");
1775   // Code below fits into 15 chars *after* removing the \ escape.
1776   verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";",
1777                getGoogleJSStyleWithColumns(15));
1778   verifyFormat("// clang-format off\n"
1779                "let x = \"double\";\n"
1780                "// clang-format on\n"
1781                "let x = 'single';\n",
1782                "// clang-format off\n"
1783                "let x = \"double\";\n"
1784                "// clang-format on\n"
1785                "let x = \"single\";\n");
1786 }
1787 
1788 TEST_F(FormatTestJS, RequoteAndIndent) {
1789   verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n"
1790                "    'double quoted string that needs wrapping');",
1791                "let x = someVeryLongFunctionThatGoesOnAndOn("
1792                "\"double quoted string that needs wrapping\");");
1793 
1794   verifyFormat("let x =\n"
1795                "    'foo\\'oo';\n"
1796                "let x =\n"
1797                "    'foo\\'oo';",
1798                "let x=\"foo'oo\";\n"
1799                "let x=\"foo'oo\";",
1800                getGoogleJSStyleWithColumns(15));
1801 }
1802 
1803 TEST_F(FormatTestJS, RequoteStringsDouble) {
1804   FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
1805   DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double;
1806   verifyFormat("var x = \"foo\";", DoubleQuotes);
1807   verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes);
1808   verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes);
1809 }
1810 
1811 TEST_F(FormatTestJS, RequoteStringsLeave) {
1812   FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
1813   LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave;
1814   verifyFormat("var x = \"foo\";", LeaveQuotes);
1815   verifyFormat("var x = 'foo';", LeaveQuotes);
1816 }
1817 
1818 TEST_F(FormatTestJS, SupportShebangLines) {
1819   verifyFormat("#!/usr/bin/env node\n"
1820                "var x = hello();",
1821                "#!/usr/bin/env node\n"
1822                "var x   =  hello();");
1823 }
1824 
1825 TEST_F(FormatTestJS, NonNullAssertionOperator) {
1826   verifyFormat("let x = foo!.bar();\n");
1827   verifyFormat("let x = foo ? bar! : baz;\n");
1828   verifyFormat("let x = !foo;\n");
1829   verifyFormat("let x = foo[0]!;\n");
1830   verifyFormat("let x = (foo)!;\n");
1831   verifyFormat("let x = x(foo!);\n");
1832   verifyFormat(
1833       "a.aaaaaa(a.a!).then(\n"
1834       "    x => x(x));\n",
1835       getGoogleJSStyleWithColumns(20));
1836   verifyFormat("let x = foo! - 1;\n");
1837   verifyFormat("let x = {foo: 1}!;\n");
1838   verifyFormat(
1839       "let x = hello.foo()!\n"
1840       "            .foo()!\n"
1841       "            .foo()!\n"
1842       "            .foo()!;\n",
1843       getGoogleJSStyleWithColumns(20));
1844   verifyFormat("let x = namespace!;\n");
1845   verifyFormat("return !!x;\n");
1846 }
1847 
1848 TEST_F(FormatTestJS, Conditional) {
1849   verifyFormat("y = x ? 1 : 2;");
1850   verifyFormat("x ? 1 : 2;");
1851   verifyFormat("class Foo {\n"
1852                "  field = true ? 1 : 2;\n"
1853                "  method(a = true ? 1 : 2) {}\n"
1854                "}");
1855 }
1856 
1857 TEST_F(FormatTestJS, ImportComments) {
1858   verifyFormat("import {x} from 'x';  // from some location",
1859                getGoogleJSStyleWithColumns(25));
1860   verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10));
1861   verifyFormat("/// <reference path=\"some/location\" />", getGoogleJSStyleWithColumns(10));
1862 }
1863 
1864 TEST_F(FormatTestJS, Exponentiation) {
1865   verifyFormat("squared = x ** 2;");
1866   verifyFormat("squared **= 2;");
1867 }
1868 
1869 } // end namespace tooling
1870 } // end namespace clang
1871