xref: /llvm-project/clang/unittests/Format/FormatTestJS.cpp (revision ab60acb6983250940093d81117f6bb3fe873e8df)
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   // FIXME: This is bad, we should be wrapping before "function() {".
721   verifyFormat("someFunction(function() {\n"
722                "  doSomething();  // break\n"
723                "})\n"
724                "    .doSomethingElse(\n"
725                "        // break\n"
726                "    );");
727 
728   Style.ColumnLimit = 33;
729   verifyFormat("f({a: function() { return 1; }});", Style);
730   Style.ColumnLimit = 32;
731   verifyFormat("f({\n"
732                "  a: function() { return 1; }\n"
733                "});",
734                Style);
735 
736 }
737 
738 TEST_F(FormatTestJS, InliningFunctionLiterals) {
739   FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
740   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
741   verifyFormat("var func = function() {\n"
742                "  return 1;\n"
743                "};",
744                Style);
745   verifyFormat("var func = doSomething(function() { return 1; });", Style);
746   verifyFormat("var outer = function() {\n"
747                "  var inner = function() { return 1; }\n"
748                "};",
749                Style);
750   verifyFormat("function outer1(a, b) {\n"
751                "  function inner1(a, b) { return a; }\n"
752                "}",
753                Style);
754 
755   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
756   verifyFormat("var func = function() { return 1; };", Style);
757   verifyFormat("var func = doSomething(function() { return 1; });", Style);
758   verifyFormat(
759       "var outer = function() { var inner = function() { return 1; } };",
760       Style);
761   verifyFormat("function outer1(a, b) {\n"
762                "  function inner1(a, b) { return a; }\n"
763                "}",
764                Style);
765 
766   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
767   verifyFormat("var func = function() {\n"
768                "  return 1;\n"
769                "};",
770                Style);
771   verifyFormat("var func = doSomething(function() {\n"
772                "  return 1;\n"
773                "});",
774                Style);
775   verifyFormat("var outer = function() {\n"
776                "  var inner = function() {\n"
777                "    return 1;\n"
778                "  }\n"
779                "};",
780                Style);
781   verifyFormat("function outer1(a, b) {\n"
782                "  function inner1(a, b) {\n"
783                "    return a;\n"
784                "  }\n"
785                "}",
786                Style);
787 
788   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
789   verifyFormat("var func = function() {\n"
790                "  return 1;\n"
791                "};",
792                Style);
793 }
794 
795 TEST_F(FormatTestJS, MultipleFunctionLiterals) {
796   FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
797   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
798   verifyFormat("promise.then(\n"
799                "    function success() {\n"
800                "      doFoo();\n"
801                "      doBar();\n"
802                "    },\n"
803                "    function error() {\n"
804                "      doFoo();\n"
805                "      doBaz();\n"
806                "    },\n"
807                "    []);\n");
808   verifyFormat("promise.then(\n"
809                "    function success() {\n"
810                "      doFoo();\n"
811                "      doBar();\n"
812                "    },\n"
813                "    [],\n"
814                "    function error() {\n"
815                "      doFoo();\n"
816                "      doBaz();\n"
817                "    });\n");
818   verifyFormat("promise.then(\n"
819                "    [],\n"
820                "    function success() {\n"
821                "      doFoo();\n"
822                "      doBar();\n"
823                "    },\n"
824                "    function error() {\n"
825                "      doFoo();\n"
826                "      doBaz();\n"
827                "    });\n");
828 
829   verifyFormat("getSomeLongPromise()\n"
830                "    .then(function(value) { body(); })\n"
831                "    .thenCatch(function(error) {\n"
832                "      body();\n"
833                "      body();\n"
834                "    });",
835                Style);
836   verifyFormat("getSomeLongPromise()\n"
837                "    .then(function(value) {\n"
838                "      body();\n"
839                "      body();\n"
840                "    })\n"
841                "    .thenCatch(function(error) {\n"
842                "      body();\n"
843                "      body();\n"
844                "    });");
845 
846   verifyFormat("getSomeLongPromise()\n"
847                "    .then(function(value) { body(); })\n"
848                "    .thenCatch(function(error) { body(); });",
849                Style);
850 
851   verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n"
852                "    .aaaaaaa(function() {\n"
853                "      //\n"
854                "    })\n"
855                "    .bbbbbb();");
856 }
857 
858 TEST_F(FormatTestJS, ArrowFunctions) {
859   verifyFormat("var x = (a) => {\n"
860                "  return a;\n"
861                "};");
862   verifyFormat("var x = (a) => {\n"
863                "  function y() {\n"
864                "    return 42;\n"
865                "  }\n"
866                "  return a;\n"
867                "};");
868   verifyFormat("var x = (a: type): {some: type} => {\n"
869                "  return a;\n"
870                "};");
871   verifyFormat("var x = (a) => a;");
872   verifyFormat("return () => [];");
873   verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n"
874                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
875                "      (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
876                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n"
877                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
878                "};");
879   verifyFormat("var a = a.aaaaaaa(\n"
880                "    (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n"
881                "        aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
882   verifyFormat("var a = a.aaaaaaa(\n"
883                "    (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n"
884                "        aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n"
885                "        aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
886 
887   // FIXME: This is bad, we should be wrapping before "() => {".
888   verifyFormat("someFunction(() => {\n"
889                "  doSomething();  // break\n"
890                "})\n"
891                "    .doSomethingElse(\n"
892                "        // break\n"
893                "    );");
894 }
895 
896 TEST_F(FormatTestJS, ReturnStatements) {
897   verifyFormat("function() {\n"
898                "  return [hello, world];\n"
899                "}");
900 }
901 
902 TEST_F(FormatTestJS, ForLoops) {
903   verifyFormat("for (var i in [2, 3]) {\n"
904                "}");
905   verifyFormat("for (var i of [2, 3]) {\n"
906                "}");
907   verifyFormat("for (let {a, b} of x) {\n"
908                "}");
909   verifyFormat("for (let {a, b} in x) {\n"
910                "}");
911 }
912 
913 TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) {
914   // The following statements must not wrap, as otherwise the program meaning
915   // would change due to automatic semicolon insertion.
916   // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1.
917   verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10));
918   verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
919   verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10));
920   verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
921   verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10));
922   verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10));
923   verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10));
924   verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10));
925   verifyFormat("return [\n"
926                "  aaa\n"
927                "];",
928                getGoogleJSStyleWithColumns(12));
929 }
930 
931 TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {
932   verifyFormat("a\n"
933                "b;",
934                " a \n"
935                " b ;");
936   verifyFormat("a()\n"
937                "b;",
938                " a ()\n"
939                " b ;");
940   verifyFormat("a[b]\n"
941                "c;",
942                "a [b]\n"
943                "c ;");
944   verifyFormat("1\n"
945                "a;",
946                "1 \n"
947                "a ;");
948   verifyFormat("a\n"
949                "1;",
950                "a \n"
951                "1 ;");
952   verifyFormat("a\n"
953                "'x';",
954                "a \n"
955                " 'x';");
956   verifyFormat("a++\n"
957                "b;",
958                "a ++\n"
959                "b ;");
960   verifyFormat("a\n"
961                "!b && c;",
962                "a \n"
963                " ! b && c;");
964   verifyFormat("a\n"
965                "if (1) f();",
966                " a\n"
967                " if (1) f();");
968   verifyFormat("a\n"
969                "class X {}",
970                " a\n"
971                " class X {}");
972   verifyFormat("var a", "var\n"
973                         "a");
974   verifyFormat("x instanceof String", "x\n"
975                                       "instanceof\n"
976                                       "String");
977   verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n"
978                                           "  bar) {}");
979   verifyFormat("a = true\n"
980                "return 1",
981                "a = true\n"
982                "  return   1");
983   verifyFormat("a = 's'\n"
984                "return 1",
985                "a = 's'\n"
986                "  return   1");
987   verifyFormat("a = null\n"
988                "return 1",
989                "a = null\n"
990                "  return   1");
991   // Below "class Y {}" should ideally be on its own line.
992   verifyFormat(
993       "x = {\n"
994       "  a: 1\n"
995       "} class Y {}",
996       "  x  =  {a  : 1}\n"
997       "   class  Y {  }");
998   verifyFormat(
999       "if (x) {\n"
1000       "}\n"
1001       "return 1",
1002       "if (x) {}\n"
1003       " return   1");
1004   verifyFormat(
1005       "if (x) {\n"
1006       "}\n"
1007       "class X {}",
1008       "if (x) {}\n"
1009       " class X {}");
1010 }
1011 
1012 TEST_F(FormatTestJS, ImportExportASI) {
1013   verifyFormat(
1014       "import {x} from 'y'\n"
1015       "export function z() {}",
1016       "import   {x} from 'y'\n"
1017       "  export function z() {}");
1018   // Below "class Y {}" should ideally be on its own line.
1019   verifyFormat(
1020       "export {x} class Y {}",
1021       "  export {x}\n"
1022       "  class  Y {\n}");
1023   verifyFormat(
1024       "if (x) {\n"
1025       "}\n"
1026       "export class Y {}",
1027       "if ( x ) { }\n"
1028       " export class Y {}");
1029 }
1030 
1031 TEST_F(FormatTestJS, ClosureStyleCasts) {
1032   verifyFormat("var x = /** @type {foo} */ (bar);");
1033 }
1034 
1035 TEST_F(FormatTestJS, TryCatch) {
1036   verifyFormat("try {\n"
1037                "  f();\n"
1038                "} catch (e) {\n"
1039                "  g();\n"
1040                "} finally {\n"
1041                "  h();\n"
1042                "}");
1043 
1044   // But, of course, "catch" is a perfectly fine function name in JavaScript.
1045   verifyFormat("someObject.catch();");
1046   verifyFormat("someObject.new();");
1047   verifyFormat("someObject.delete();");
1048 }
1049 
1050 TEST_F(FormatTestJS, StringLiteralConcatenation) {
1051   verifyFormat("var literal = 'hello ' +\n"
1052                "    'world';");
1053 }
1054 
1055 TEST_F(FormatTestJS, RegexLiteralClassification) {
1056   // Regex literals.
1057   verifyFormat("var regex = /abc/;");
1058   verifyFormat("f(/abc/);");
1059   verifyFormat("f(abc, /abc/);");
1060   verifyFormat("some_map[/abc/];");
1061   verifyFormat("var x = a ? /abc/ : /abc/;");
1062   verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
1063   verifyFormat("var x = !/abc/.test(y);");
1064   verifyFormat("var x = foo()! / 10;");
1065   verifyFormat("var x = a && /abc/.test(y);");
1066   verifyFormat("var x = a || /abc/.test(y);");
1067   verifyFormat("var x = a + /abc/.search(y);");
1068   verifyFormat("/abc/.search(y);");
1069   verifyFormat("var regexs = {/abc/, /abc/};");
1070   verifyFormat("return /abc/;");
1071 
1072   // Not regex literals.
1073   verifyFormat("var a = a / 2 + b / 3;");
1074   verifyFormat("var a = a++ / 2;");
1075   // Prefix unary can operate on regex literals, not that it makes sense.
1076   verifyFormat("var a = ++/a/;");
1077 
1078   // This is a known issue, regular expressions are incorrectly detected if
1079   // directly following a closing parenthesis.
1080   verifyFormat("if (foo) / bar /.exec(baz);");
1081 }
1082 
1083 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
1084   verifyFormat("var regex = /=/;");
1085   verifyFormat("var regex = /a*/;");
1086   verifyFormat("var regex = /a+/;");
1087   verifyFormat("var regex = /a?/;");
1088   verifyFormat("var regex = /.a./;");
1089   verifyFormat("var regex = /a\\*/;");
1090   verifyFormat("var regex = /^a$/;");
1091   verifyFormat("var regex = /\\/a/;");
1092   verifyFormat("var regex = /(?:x)/;");
1093   verifyFormat("var regex = /x(?=y)/;");
1094   verifyFormat("var regex = /x(?!y)/;");
1095   verifyFormat("var regex = /x|y/;");
1096   verifyFormat("var regex = /a{2}/;");
1097   verifyFormat("var regex = /a{1,3}/;");
1098 
1099   verifyFormat("var regex = /[abc]/;");
1100   verifyFormat("var regex = /[^abc]/;");
1101   verifyFormat("var regex = /[\\b]/;");
1102   verifyFormat("var regex = /[/]/;");
1103   verifyFormat("var regex = /[\\/]/;");
1104   verifyFormat("var regex = /\\[/;");
1105   verifyFormat("var regex = /\\\\[/]/;");
1106   verifyFormat("var regex = /}[\"]/;");
1107   verifyFormat("var regex = /}[/\"]/;");
1108   verifyFormat("var regex = /}[\"/]/;");
1109 
1110   verifyFormat("var regex = /\\b/;");
1111   verifyFormat("var regex = /\\B/;");
1112   verifyFormat("var regex = /\\d/;");
1113   verifyFormat("var regex = /\\D/;");
1114   verifyFormat("var regex = /\\f/;");
1115   verifyFormat("var regex = /\\n/;");
1116   verifyFormat("var regex = /\\r/;");
1117   verifyFormat("var regex = /\\s/;");
1118   verifyFormat("var regex = /\\S/;");
1119   verifyFormat("var regex = /\\t/;");
1120   verifyFormat("var regex = /\\v/;");
1121   verifyFormat("var regex = /\\w/;");
1122   verifyFormat("var regex = /\\W/;");
1123   verifyFormat("var regex = /a(a)\\1/;");
1124   verifyFormat("var regex = /\\0/;");
1125   verifyFormat("var regex = /\\\\/g;");
1126   verifyFormat("var regex = /\\a\\\\/g;");
1127   verifyFormat("var regex = /\a\\//g;");
1128   verifyFormat("var regex = /a\\//;\n"
1129                "var x = 0;");
1130   verifyFormat("var regex = /'/g;", "var regex = /'/g ;");
1131   verifyFormat("var regex = /'/g;  //'", "var regex = /'/g ; //'");
1132   verifyFormat("var regex = /\\/*/;\n"
1133                "var x = 0;",
1134                "var regex = /\\/*/;\n"
1135                "var x=0;");
1136   verifyFormat("var x = /a\\//;", "var x = /a\\//  \n;");
1137   verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16));
1138   verifyFormat("var regex =\n"
1139                "    /\"/;",
1140                getGoogleJSStyleWithColumns(15));
1141   verifyFormat("var regex =  //\n"
1142                "    /a/;");
1143   verifyFormat("var regexs = [\n"
1144                "  /d/,   //\n"
1145                "  /aa/,  //\n"
1146                "];");
1147 }
1148 
1149 TEST_F(FormatTestJS, RegexLiteralModifiers) {
1150   verifyFormat("var regex = /abc/g;");
1151   verifyFormat("var regex = /abc/i;");
1152   verifyFormat("var regex = /abc/m;");
1153   verifyFormat("var regex = /abc/y;");
1154 }
1155 
1156 TEST_F(FormatTestJS, RegexLiteralLength) {
1157   verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
1158                getGoogleJSStyleWithColumns(60));
1159   verifyFormat("var regex =\n"
1160                "    /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
1161                getGoogleJSStyleWithColumns(60));
1162   verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
1163                getGoogleJSStyleWithColumns(50));
1164 }
1165 
1166 TEST_F(FormatTestJS, RegexLiteralExamples) {
1167   verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
1168 }
1169 
1170 TEST_F(FormatTestJS, IgnoresMpegTS) {
1171   std::string MpegTS(200, ' ');
1172   MpegTS.replace(0, strlen("nearlyLooks  +   like +   ts + code;  "),
1173                  "nearlyLooks  +   like +   ts + code;  ");
1174   MpegTS[0] = 0x47;
1175   MpegTS[188] = 0x47;
1176   verifyFormat(MpegTS, MpegTS);
1177 }
1178 
1179 TEST_F(FormatTestJS, TypeAnnotations) {
1180   verifyFormat("var x: string;");
1181   verifyFormat("var x: {a: string; b: number;} = {};");
1182   verifyFormat("function x(): string {\n  return 'x';\n}");
1183   verifyFormat("function x(): {x: string} {\n  return {x: 'x'};\n}");
1184   verifyFormat("function x(y: string): string {\n  return 'x';\n}");
1185   verifyFormat("for (var y: string in x) {\n  x();\n}");
1186   verifyFormat("for (var y: string of x) {\n  x();\n}");
1187   verifyFormat("function x(y: {a?: number;} = {}): number {\n"
1188                "  return 12;\n"
1189                "}");
1190   verifyFormat("((a: string, b: number): string => a + b);");
1191   verifyFormat("var x: (y: number) => string;");
1192   verifyFormat("var x: P<string, (a: number) => string>;");
1193   verifyFormat("var x = {\n"
1194                "  y: function(): z {\n"
1195                "    return 1;\n"
1196                "  }\n"
1197                "};");
1198   verifyFormat("var x = {\n"
1199                "  y: function(): {a: number} {\n"
1200                "    return 1;\n"
1201                "  }\n"
1202                "};");
1203   verifyFormat("function someFunc(args: string[]):\n"
1204                "    {longReturnValue: string[]} {}",
1205                getGoogleJSStyleWithColumns(60));
1206   verifyFormat(
1207       "var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])\n"
1208       "                    .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1209 }
1210 
1211 TEST_F(FormatTestJS, UnionIntersectionTypes) {
1212   verifyFormat("let x: A|B = A | B;");
1213   verifyFormat("let x: A&B|C = A & B;");
1214   verifyFormat("let x: Foo<A|B> = new Foo<A|B>();");
1215   verifyFormat("function(x: A|B): C&D {}");
1216   verifyFormat("function(x: A|B = A | B): C&D {}");
1217   verifyFormat("function x(path: number|string) {}");
1218   verifyFormat("function x(): string|number {}");
1219   verifyFormat("type Foo = Bar|Baz;");
1220   verifyFormat("type Foo = Bar<X>|Baz;");
1221   verifyFormat("type Foo = (Bar<X>|Baz);");
1222   verifyFormat("let x: Bar|Baz;");
1223   verifyFormat("let x: Bar<X>|Baz;");
1224   verifyFormat("let x: (Foo|Bar)[];");
1225 }
1226 
1227 TEST_F(FormatTestJS, ClassDeclarations) {
1228   verifyFormat("class C {\n  x: string = 12;\n}");
1229   verifyFormat("class C {\n  x(): string => 12;\n}");
1230   verifyFormat("class C {\n  ['x' + 2]: string = 12;\n}");
1231   verifyFormat("class C {\n  private x: string = 12;\n}");
1232   verifyFormat("class C {\n  private static x: string = 12;\n}");
1233   verifyFormat("class C {\n  static x(): string {\n    return 'asd';\n  }\n}");
1234   verifyFormat("class C extends P implements I {}");
1235   verifyFormat("class C extends p.P implements i.I {}");
1236   verifyFormat(
1237       "x(class {\n"
1238       "  a(): A {}\n"
1239       "});");
1240   verifyFormat("class Test {\n"
1241                "  aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n"
1242                "      aaaaaaaaaaaaaaaaaaaaaa {}\n"
1243                "}");
1244   verifyFormat("foo = class Name {\n"
1245                "  constructor() {}\n"
1246                "};");
1247   verifyFormat("foo = class {\n"
1248                "  constructor() {}\n"
1249                "};");
1250   verifyFormat("class C {\n"
1251                "  x: {y: Z;} = {};\n"
1252                "  private y: {y: Z;} = {};\n"
1253                "}");
1254 
1255   // ':' is not a type declaration here.
1256   verifyFormat("class X {\n"
1257                "  subs = {\n"
1258                "    'b': {\n"
1259                "      'c': 1,\n"
1260                "    },\n"
1261                "  };\n"
1262                "}");
1263   verifyFormat("@Component({\n"
1264                "  moduleId: module.id,\n"
1265                "})\n"
1266                "class SessionListComponent implements OnDestroy, OnInit {\n"
1267                "}");
1268 }
1269 
1270 TEST_F(FormatTestJS, InterfaceDeclarations) {
1271   verifyFormat("interface I {\n"
1272                "  x: string;\n"
1273                "  enum: string[];\n"
1274                "  enum?: string[];\n"
1275                "}\n"
1276                "var y;");
1277   // Ensure that state is reset after parsing the interface.
1278   verifyFormat("interface a {}\n"
1279                "export function b() {}\n"
1280                "var x;");
1281 
1282   // Arrays of object type literals.
1283   verifyFormat("interface I {\n"
1284                "  o: {}[];\n"
1285                "}");
1286 }
1287 
1288 TEST_F(FormatTestJS, EnumDeclarations) {
1289   verifyFormat("enum Foo {\n"
1290                "  A = 1,\n"
1291                "  B\n"
1292                "}");
1293   verifyFormat("export /* somecomment*/ enum Foo {\n"
1294                "  A = 1,\n"
1295                "  B\n"
1296                "}");
1297   verifyFormat("enum Foo {\n"
1298                "  A = 1,  // comment\n"
1299                "  B\n"
1300                "}\n"
1301                "var x = 1;");
1302 }
1303 
1304 TEST_F(FormatTestJS, MetadataAnnotations) {
1305   verifyFormat("@A\nclass C {\n}");
1306   verifyFormat("@A({arg: 'value'})\nclass C {\n}");
1307   verifyFormat("@A\n@B\nclass C {\n}");
1308   verifyFormat("class C {\n  @A x: string;\n}");
1309   verifyFormat("class C {\n"
1310                "  @A\n"
1311                "  private x(): string {\n"
1312                "    return 'y';\n"
1313                "  }\n"
1314                "}");
1315   verifyFormat("class C {\n"
1316                "  private x(@A x: string) {}\n"
1317                "}");
1318   verifyFormat("class X {}\n"
1319                "class Y {}");
1320   verifyFormat("class X {\n"
1321                "  @property() private isReply = false;\n"
1322                "}\n");
1323 }
1324 
1325 TEST_F(FormatTestJS, TypeAliases) {
1326   verifyFormat("type X = number;\n"
1327                "class C {}");
1328   verifyFormat("type X<Y> = Z<Y>;");
1329   verifyFormat("type X = {\n"
1330                "  y: number\n"
1331                "};\n"
1332                "class C {}");
1333 }
1334 
1335 TEST_F(FormatTestJS, TypeInterfaceLineWrapping) {
1336   const FormatStyle &Style = getGoogleJSStyleWithColumns(20);
1337   verifyFormat("type LongTypeIsReallyUnreasonablyLong =\n"
1338                "    string;\n",
1339                "type LongTypeIsReallyUnreasonablyLong = string;\n",
1340                Style);
1341   verifyFormat(
1342       "interface AbstractStrategyFactoryProvider {\n"
1343       "  a: number\n"
1344       "}\n",
1345       "interface AbstractStrategyFactoryProvider { a: number }\n",
1346       Style);
1347 }
1348 
1349 TEST_F(FormatTestJS, Modules) {
1350   verifyFormat("import SomeThing from 'some/module.js';");
1351   verifyFormat("import {X, Y} from 'some/module.js';");
1352   verifyFormat("import a, {X, Y} from 'some/module.js';");
1353   verifyFormat("import {X, Y,} from 'some/module.js';");
1354   verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
1355   // Ensure Automatic Semicolon Insertion does not break on "as\n".
1356   verifyFormat("import {X as myX} from 'm';", "import {X as\n"
1357                                               " myX} from 'm';");
1358   verifyFormat("import * as lib from 'some/module.js';");
1359   verifyFormat("var x = {import: 1};\nx.import = 2;");
1360 
1361   verifyFormat("export function fn() {\n"
1362                "  return 'fn';\n"
1363                "}");
1364   verifyFormat("export function A() {}\n"
1365                "export default function B() {}\n"
1366                "export function C() {}");
1367   verifyFormat("export default () => {\n"
1368                "  let x = 1;\n"
1369                "  return x;\n"
1370                "}");
1371   verifyFormat("export const x = 12;");
1372   verifyFormat("export default class X {}");
1373   verifyFormat("export {X, Y} from 'some/module.js';");
1374   verifyFormat("export {X, Y,} from 'some/module.js';");
1375   verifyFormat("export {SomeVeryLongExport as X, "
1376                "SomeOtherVeryLongExport as Y} from 'some/module.js';");
1377   // export without 'from' is wrapped.
1378   verifyFormat("export let someRatherLongVariableName =\n"
1379                "    someSurprisinglyLongVariable + someOtherRatherLongVar;");
1380   // ... but not if from is just an identifier.
1381   verifyFormat("export {\n"
1382                "  from as from,\n"
1383                "  someSurprisinglyLongVariable as\n"
1384                "      from\n"
1385                "};",
1386                getGoogleJSStyleWithColumns(20));
1387   verifyFormat("export class C {\n"
1388                "  x: number;\n"
1389                "  y: string;\n"
1390                "}");
1391   verifyFormat("export class X { y: number; }");
1392   verifyFormat("export abstract class X { y: number; }");
1393   verifyFormat("export default class X { y: number }");
1394   verifyFormat("export default function() {\n  return 1;\n}");
1395   verifyFormat("export var x = 12;");
1396   verifyFormat("class C {}\n"
1397                "export function f() {}\n"
1398                "var v;");
1399   verifyFormat("export var x: number = 12;");
1400   verifyFormat("export const y = {\n"
1401                "  a: 1,\n"
1402                "  b: 2\n"
1403                "};");
1404   verifyFormat("export enum Foo {\n"
1405                "  BAR,\n"
1406                "  // adsdasd\n"
1407                "  BAZ\n"
1408                "}");
1409   verifyFormat("export default [\n"
1410                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1411                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
1412                "];");
1413   verifyFormat("export default [];");
1414   verifyFormat("export default () => {};");
1415   verifyFormat("export interface Foo { foo: number; }\n"
1416                "export class Bar {\n"
1417                "  blah(): string {\n"
1418                "    return this.blah;\n"
1419                "  };\n"
1420                "}");
1421 }
1422 
1423 TEST_F(FormatTestJS, ImportWrapping) {
1424   verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,"
1425                " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying"
1426                "} from 'some/module.js';");
1427   FormatStyle Style = getGoogleJSStyleWithColumns(80);
1428   Style.JavaScriptWrapImports = true;
1429   verifyFormat("import {\n"
1430                "  VeryLongImportsAreAnnoying,\n"
1431                "  VeryLongImportsAreAnnoying,\n"
1432                "  VeryLongImportsAreAnnoying,\n"
1433                "} from 'some/module.js';",
1434                Style);
1435   verifyFormat("import {\n"
1436                "  A,\n"
1437                "  A,\n"
1438                "} from 'some/module.js';",
1439                Style);
1440   verifyFormat("export {\n"
1441                "  A,\n"
1442                "  A,\n"
1443                "} from 'some/module.js';",
1444                Style);
1445 }
1446 
1447 TEST_F(FormatTestJS, TemplateStrings) {
1448   // Keeps any whitespace/indentation within the template string.
1449   verifyFormat("var x = `hello\n"
1450             "     ${name}\n"
1451             "  !`;",
1452             "var x    =    `hello\n"
1453                    "     ${  name    }\n"
1454                    "  !`;");
1455 
1456   verifyFormat("var x =\n"
1457                "    `hello ${world}` >= some();",
1458                getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
1459   verifyFormat("var x = `hello ${world}` >= some();",
1460                getGoogleJSStyleWithColumns(35)); // Barely fits.
1461   verifyFormat("var x = `hellö ${wörld}` >= söme();",
1462                getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8.
1463   verifyFormat("var x = `hello\n"
1464             "  ${world}` >=\n"
1465             "    some();",
1466             "var x =\n"
1467                    "    `hello\n"
1468                    "  ${world}` >= some();",
1469                    getGoogleJSStyleWithColumns(21)); // Barely doesn't fit.
1470   verifyFormat("var x = `hello\n"
1471             "  ${world}` >= some();",
1472             "var x =\n"
1473                    "    `hello\n"
1474                    "  ${world}` >= some();",
1475                    getGoogleJSStyleWithColumns(22)); // Barely fits.
1476 
1477   verifyFormat("var x =\n"
1478                "    `h`;",
1479                getGoogleJSStyleWithColumns(11));
1480   verifyFormat("var x =\n    `multi\n  line`;", "var x = `multi\n  line`;",
1481                getGoogleJSStyleWithColumns(13));
1482   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1483                "    `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);");
1484   // Repro for an obscure width-miscounting issue with template strings.
1485   verifyFormat(
1486       "someLongVariable =\n"
1487       "    "
1488       "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;",
1489       "someLongVariable = "
1490       "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;");
1491 
1492   // Make sure template strings get a proper ColumnWidth assigned, even if they
1493   // are first token in line.
1494   verifyFormat(
1495       "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
1496       "    `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;");
1497 
1498   // Two template strings.
1499   verifyFormat("var x = `hello` == `hello`;");
1500 
1501   // Comments in template strings.
1502   verifyFormat("var x = `//a`;\n"
1503             "var y;",
1504             "var x =\n `//a`;\n"
1505                    "var y  ;");
1506   verifyFormat("var x = `/*a`;\n"
1507                "var y;",
1508                "var x =\n `/*a`;\n"
1509                "var y;");
1510   // Unterminated string literals in a template string.
1511   verifyFormat("var x = `'`;  // comment with matching quote '\n"
1512                "var y;");
1513   verifyFormat("var x = `\"`;  // comment with matching quote \"\n"
1514                "var y;");
1515   verifyFormat("it(`'aaaaaaaaaaaaaaa   `, aaaaaaaaa);",
1516                "it(`'aaaaaaaaaaaaaaa   `,   aaaaaaaaa) ;",
1517                getGoogleJSStyleWithColumns(40));
1518   // Backticks in a comment - not a template string.
1519   verifyFormat("var x = 1  // `/*a`;\n"
1520                "    ;",
1521                "var x =\n 1  // `/*a`;\n"
1522                "    ;");
1523   verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */");
1524   // Comment spans multiple template strings.
1525   verifyFormat("var x = `/*a`;\n"
1526                "var y = ` */ `;",
1527                "var x =\n `/*a`;\n"
1528                "var y =\n ` */ `;");
1529   // Escaped backtick.
1530   verifyFormat("var x = ` \\` a`;\n"
1531                "var y;",
1532                "var x = ` \\` a`;\n"
1533                "var y;");
1534   // Escaped dollar.
1535   verifyFormat("var x = ` \\${foo}`;\n");
1536 
1537   // The token stream can contain two string_literals in sequence, but that
1538   // doesn't mean that they are implicitly concatenated in JavaScript.
1539   verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;");
1540 
1541   // Ensure that scopes are appropriately set around evaluated expressions in
1542   // template strings.
1543   verifyFormat("var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa\n"
1544                "         aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;",
1545                "var f = `aaaaaaaaaaaaa:${aaaaaaa.  aaaaa} aaaaaaaa\n"
1546                "         aaaaaaaaaaaaa:${  aaaaaaa. aaaaa} aaaaaaaa`;");
1547   verifyFormat("var x = someFunction(`${})`)  //\n"
1548                "            .oooooooooooooooooon();");
1549   verifyFormat("var x = someFunction(`${aaaa}${\n"
1550                "                               aaaaa(  //\n"
1551                "                                   aaaaa)\n"
1552                "                             })`);");
1553 }
1554 
1555 TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
1556   verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${\n"
1557                "                               aaaaa +  //\n"
1558                "                               bbbb\n"
1559                "                             }`;",
1560                "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa +  //\n"
1561                "                               bbbb}`;");
1562   verifyFormat("var f = `\n"
1563                "  aaaaaaaaaaaaaaaaaa: ${\n"
1564                "                        aaaaa +  //\n"
1565                "                        bbbb\n"
1566                "                      }`;",
1567                "var f  =  `\n"
1568                "  aaaaaaaaaaaaaaaaaa: ${   aaaaa  +  //\n"
1569                "                        bbbb }`;");
1570   verifyFormat("var f = `\n"
1571                "  aaaaaaaaaaaaaaaaaa: ${\n"
1572                "                        someFunction(\n"
1573                "                            aaaaa +  //\n"
1574                "                            bbbb)\n"
1575                "                      }`;",
1576                "var f  =  `\n"
1577                "  aaaaaaaaaaaaaaaaaa: ${someFunction (\n"
1578                "                            aaaaa  +   //\n"
1579                "                            bbbb)}`;");
1580 
1581   // It might be preferable to wrap before "someFunction".
1582   verifyFormat("var f = `\n"
1583                "  aaaaaaaaaaaaaaaaaa: ${someFunction({\n"
1584                "                          aaaa: aaaaa,\n"
1585                "                          bbbb: bbbbb,\n"
1586                "                        })}`;",
1587                "var f  =  `\n"
1588                "  aaaaaaaaaaaaaaaaaa: ${someFunction ({\n"
1589                "                          aaaa:  aaaaa,\n"
1590                "                          bbbb:  bbbbb,\n"
1591                "                        })}`;");
1592 }
1593 
1594 TEST_F(FormatTestJS, TemplateStringASI) {
1595   verifyFormat("var x = `hello${world}`;", "var x = `hello${\n"
1596                                            "    world\n"
1597                                            "}`;");
1598 }
1599 
1600 TEST_F(FormatTestJS, NestedTemplateStrings) {
1601   verifyFormat(
1602       "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;");
1603   verifyFormat("var x = `he${({text: 'll'}.text)}o`;");
1604 
1605   // Crashed at some point.
1606   verifyFormat("}");
1607 }
1608 
1609 TEST_F(FormatTestJS, TaggedTemplateStrings) {
1610   verifyFormat("var x = html`<ul>`;");
1611 }
1612 
1613 TEST_F(FormatTestJS, CastSyntax) {
1614   verifyFormat("var x = <type>foo;");
1615   verifyFormat("var x = foo as type;");
1616   verifyFormat("let x = (a + b) as\n"
1617                "    LongTypeIsLong;",
1618                getGoogleJSStyleWithColumns(20));
1619   verifyFormat("foo = <Bar[]>[\n"
1620                "  1,  //\n"
1621                "  2\n"
1622                "];");
1623   verifyFormat("var x = [{x: 1} as type];");
1624   verifyFormat("x = x as [a, b];");
1625   verifyFormat("x = x as {a: string};");
1626   verifyFormat("x = x as (string);");
1627   verifyFormat("x = x! as (string);");
1628   verifyFormat("var x = something.someFunction() as\n"
1629                "    something;",
1630                getGoogleJSStyleWithColumns(40));
1631 }
1632 
1633 TEST_F(FormatTestJS, TypeArguments) {
1634   verifyFormat("class X<Y> {}");
1635   verifyFormat("new X<Y>();");
1636   verifyFormat("foo<Y>(a);");
1637   verifyFormat("var x: X<Y>[];");
1638   verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
1639   verifyFormat("function f(a: List<any> = null) {}");
1640   verifyFormat("function f(): List<any> {}");
1641   verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n"
1642                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}");
1643   verifyFormat("function aaaaaaaaaa(\n"
1644                "    aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n"
1645                "    aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n"
1646                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}");
1647 }
1648 
1649 TEST_F(FormatTestJS, UserDefinedTypeGuards) {
1650   verifyFormat(
1651       "function foo(check: Object):\n"
1652       "    check is {foo: string, bar: string, baz: string, foobar: string} {\n"
1653       "  return 'bar' in check;\n"
1654       "}\n");
1655 }
1656 
1657 TEST_F(FormatTestJS, OptionalTypes) {
1658   verifyFormat("function x(a?: b, c?, d?) {}");
1659   verifyFormat("class X {\n"
1660                "  y?: z;\n"
1661                "  z?;\n"
1662                "}");
1663   verifyFormat("interface X {\n"
1664                "  y?(): z;\n"
1665                "}");
1666   verifyFormat("constructor({aa}: {\n"
1667                "  aa?: string,\n"
1668                "  aaaaaaaa?: string,\n"
1669                "  aaaaaaaaaaaaaaa?: boolean,\n"
1670                "  aaaaaa?: List<string>\n"
1671                "}) {}");
1672 }
1673 
1674 TEST_F(FormatTestJS, IndexSignature) {
1675   verifyFormat("var x: {[k: string]: v};");
1676 }
1677 
1678 TEST_F(FormatTestJS, WrapAfterParen) {
1679   verifyFormat("xxxxxxxxxxx(\n"
1680                "    aaa, aaa);",
1681                getGoogleJSStyleWithColumns(20));
1682   verifyFormat("xxxxxxxxxxx(\n"
1683                "    aaa, aaa, aaa,\n"
1684                "    aaa, aaa, aaa);",
1685                getGoogleJSStyleWithColumns(20));
1686   verifyFormat("xxxxxxxxxxx(\n"
1687                "    aaaaaaaaaaaaaaaaaaaaaaaa,\n"
1688                "    function(x) {\n"
1689                "      y();  //\n"
1690                "    });",
1691                getGoogleJSStyleWithColumns(40));
1692   verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
1693                "       bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
1694 }
1695 
1696 TEST_F(FormatTestJS, JSDocAnnotations) {
1697   verifyFormat("/**\n"
1698                " * @export {this.is.a.long.path.to.a.Type}\n"
1699                " */",
1700                "/**\n"
1701                " * @export {this.is.a.long.path.to.a.Type}\n"
1702                " */",
1703                getGoogleJSStyleWithColumns(20));
1704   verifyFormat("/**\n"
1705                " * @mods {this.is.a.long.path.to.a.Type}\n"
1706                " */",
1707                "/**\n"
1708                " * @mods {this.is.a.long.path.to.a.Type}\n"
1709                " */",
1710                getGoogleJSStyleWithColumns(20));
1711   verifyFormat("/**\n"
1712                " * @param {this.is.a.long.path.to.a.Type}\n"
1713                " */",
1714                "/**\n"
1715                " * @param {this.is.a.long.path.to.a.Type}\n"
1716                " */",
1717                getGoogleJSStyleWithColumns(20));
1718   verifyFormat("/**\n"
1719                " * @see http://very/very/long/url/is/long\n"
1720                " */",
1721                "/**\n"
1722                " * @see http://very/very/long/url/is/long\n"
1723                " */",
1724                getGoogleJSStyleWithColumns(20));
1725   verifyFormat(
1726       "/**\n"
1727       " * @param This is a\n"
1728       " * long comment but\n"
1729       " * no type\n"
1730       " */",
1731       "/**\n"
1732       " * @param This is a long comment but no type\n"
1733       " */",
1734       getGoogleJSStyleWithColumns(20));
1735   // Don't break @param line, but reindent it and reflow unrelated lines.
1736   verifyFormat("{\n"
1737                "  /**\n"
1738                "   * long long long\n"
1739                "   * long\n"
1740                "   * @param {this.is.a.long.path.to.a.Type} a\n"
1741                "   * long long long\n"
1742                "   * long long\n"
1743                "   */\n"
1744                "  function f(a) {}\n"
1745                "}",
1746                "{\n"
1747                "/**\n"
1748                " * long long long long\n"
1749                " * @param {this.is.a.long.path.to.a.Type} a\n"
1750                " * long long long long\n"
1751                " * long\n"
1752                " */\n"
1753                "  function f(a) {}\n"
1754                "}",
1755                getGoogleJSStyleWithColumns(20));
1756 }
1757 
1758 TEST_F(FormatTestJS, RequoteStringsSingle) {
1759   verifyFormat("var x = 'foo';", "var x = \"foo\";");
1760   verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";");
1761   verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";");
1762   verifyFormat(
1763       "var x =\n"
1764       "    'foo\\'';",
1765       // Code below is 15 chars wide, doesn't fit into the line with the
1766       // \ escape added.
1767       "var x = \"foo'\";", getGoogleJSStyleWithColumns(15));
1768   // Removes no-longer needed \ escape from ".
1769   verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";");
1770   // Code below fits into 15 chars *after* removing the \ escape.
1771   verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";",
1772                getGoogleJSStyleWithColumns(15));
1773   verifyFormat("// clang-format off\n"
1774                "let x = \"double\";\n"
1775                "// clang-format on\n"
1776                "let x = 'single';\n",
1777                "// clang-format off\n"
1778                "let x = \"double\";\n"
1779                "// clang-format on\n"
1780                "let x = \"single\";\n");
1781 }
1782 
1783 TEST_F(FormatTestJS, RequoteAndIndent) {
1784   verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n"
1785                "    'double quoted string that needs wrapping');",
1786                "let x = someVeryLongFunctionThatGoesOnAndOn("
1787                "\"double quoted string that needs wrapping\");");
1788 
1789   verifyFormat("let x =\n"
1790                "    'foo\\'oo';\n"
1791                "let x =\n"
1792                "    'foo\\'oo';",
1793                "let x=\"foo'oo\";\n"
1794                "let x=\"foo'oo\";",
1795                getGoogleJSStyleWithColumns(15));
1796 }
1797 
1798 TEST_F(FormatTestJS, RequoteStringsDouble) {
1799   FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
1800   DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double;
1801   verifyFormat("var x = \"foo\";", DoubleQuotes);
1802   verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes);
1803   verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes);
1804 }
1805 
1806 TEST_F(FormatTestJS, RequoteStringsLeave) {
1807   FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
1808   LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave;
1809   verifyFormat("var x = \"foo\";", LeaveQuotes);
1810   verifyFormat("var x = 'foo';", LeaveQuotes);
1811 }
1812 
1813 TEST_F(FormatTestJS, SupportShebangLines) {
1814   verifyFormat("#!/usr/bin/env node\n"
1815                "var x = hello();",
1816                "#!/usr/bin/env node\n"
1817                "var x   =  hello();");
1818 }
1819 
1820 TEST_F(FormatTestJS, NonNullAssertionOperator) {
1821   verifyFormat("let x = foo!.bar();\n");
1822   verifyFormat("let x = foo ? bar! : baz;\n");
1823   verifyFormat("let x = !foo;\n");
1824   verifyFormat("let x = foo[0]!;\n");
1825   verifyFormat("let x = (foo)!;\n");
1826   verifyFormat("let x = x(foo!);\n");
1827   verifyFormat(
1828       "a.aaaaaa(a.a!).then(\n"
1829       "    x => x(x));\n",
1830       getGoogleJSStyleWithColumns(20));
1831   verifyFormat("let x = foo! - 1;\n");
1832   verifyFormat("let x = {foo: 1}!;\n");
1833   verifyFormat(
1834       "let x = hello.foo()!\n"
1835       "            .foo()!\n"
1836       "            .foo()!\n"
1837       "            .foo()!;\n",
1838       getGoogleJSStyleWithColumns(20));
1839   verifyFormat("let x = namespace!;\n");
1840   verifyFormat("return !!x;\n");
1841 }
1842 
1843 TEST_F(FormatTestJS, Conditional) {
1844   verifyFormat("y = x ? 1 : 2;");
1845   verifyFormat("x ? 1 : 2;");
1846   verifyFormat("class Foo {\n"
1847                "  field = true ? 1 : 2;\n"
1848                "  method(a = true ? 1 : 2) {}\n"
1849                "}");
1850 }
1851 
1852 TEST_F(FormatTestJS, ImportComments) {
1853   verifyFormat("import {x} from 'x';  // from some location",
1854                getGoogleJSStyleWithColumns(25));
1855   verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10));
1856   verifyFormat("/// <reference path=\"some/location\" />", getGoogleJSStyleWithColumns(10));
1857 }
1858 
1859 TEST_F(FormatTestJS, Exponentiation) {
1860   verifyFormat("squared = x ** 2;");
1861   verifyFormat("squared **= 2;");
1862 }
1863 
1864 } // end namespace tooling
1865 } // end namespace clang
1866