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