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