xref: /llvm-project/clang/unittests/Format/FormatTestJS.cpp (revision 1e8261ea0430ecc0bed0ace31e0ec724a5a69270)
1 //===- unittest/Format/FormatTestJS.cpp - Formatting unit tests for JS ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14 
15 #define DEBUG_TYPE "format-test"
16 
17 namespace clang {
18 namespace format {
19 
20 class FormatTestJS : public ::testing::Test {
21 protected:
22   static std::string format(llvm::StringRef Code, unsigned Offset,
23                             unsigned Length, const FormatStyle &Style) {
24     DEBUG(llvm::errs() << "---\n");
25     DEBUG(llvm::errs() << Code << "\n\n");
26     std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27     bool IncompleteFormat = false;
28     tooling::Replacements Replaces =
29         reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
30     EXPECT_FALSE(IncompleteFormat);
31     std::string Result = applyAllReplacements(Code, Replaces);
32     EXPECT_NE("", 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, UnderstandsJavaScriptOperators) {
66   verifyFormat("a == = b;");
67   verifyFormat("a != = b;");
68 
69   verifyFormat("a === b;");
70   verifyFormat("aaaaaaa ===\n    b;", getGoogleJSStyleWithColumns(10));
71   verifyFormat("a !== b;");
72   verifyFormat("aaaaaaa !==\n    b;", getGoogleJSStyleWithColumns(10));
73   verifyFormat("if (a + b + c +\n"
74                "        d !==\n"
75                "    e + f + g)\n"
76                "  q();",
77                getGoogleJSStyleWithColumns(20));
78 
79   verifyFormat("a >> >= b;");
80 
81   verifyFormat("a >>> b;");
82   verifyFormat("aaaaaaa >>>\n    b;", getGoogleJSStyleWithColumns(10));
83   verifyFormat("a >>>= b;");
84   verifyFormat("aaaaaaa >>>=\n    b;", getGoogleJSStyleWithColumns(10));
85   verifyFormat("if (a + b + c +\n"
86                "        d >>>\n"
87                "    e + f + g)\n"
88                "  q();",
89                getGoogleJSStyleWithColumns(20));
90   verifyFormat("var x = aaaaaaaaaa ?\n"
91                "    bbbbbb :\n"
92                "    ccc;",
93                getGoogleJSStyleWithColumns(20));
94 
95   verifyFormat("var b = a.map((x) => x + 1);");
96   verifyFormat("return ('aaa') in bbbb;");
97   verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n"
98                "    aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
99   FormatStyle Style = getGoogleJSStyleWithColumns(80);
100   Style.AlignOperands = true;
101   verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n"
102                "        aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
103                Style);
104   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
105   verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa()\n"
106                "            in aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
107                Style);
108 
109   // ES6 spread operator.
110   verifyFormat("someFunction(...a);");
111   verifyFormat("var x = [1, ...a, 2];");
112 }
113 
114 TEST_F(FormatTestJS, UnderstandsAmpAmp) {
115   verifyFormat("e && e.SomeFunction();");
116 }
117 
118 TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
119   verifyFormat("not.and.or.not_eq = 1;");
120 }
121 
122 TEST_F(FormatTestJS, ReservedWords) {
123   // JavaScript reserved words (aka keywords) are only illegal when used as
124   // Identifiers, but are legal as IdentifierNames.
125   verifyFormat("x.class.struct = 1;");
126   verifyFormat("x.case = 1;");
127   verifyFormat("x.interface = 1;");
128   verifyFormat("x.of() = 1;");
129   verifyFormat("x.in() = 1;");
130   verifyFormat("x.let() = 1;");
131   verifyFormat("x.var() = 1;");
132   verifyFormat("x = {\n"
133                "  a: 12,\n"
134                "  interface: 1,\n"
135                "  switch: 1,\n"
136                "};");
137   verifyFormat("var struct = 2;");
138   verifyFormat("var union = 2;");
139   verifyFormat("var interface = 2;");
140   verifyFormat("interface = 2;");
141   verifyFormat("x = interface instanceof y;");
142 }
143 
144 TEST_F(FormatTestJS, CppKeywords) {
145   // Make sure we don't mess stuff up because of C++ keywords.
146   verifyFormat("return operator && (aa);");
147 }
148 
149 TEST_F(FormatTestJS, ES6DestructuringAssignment) {
150   verifyFormat("var [a, b, c] = [1, 2, 3];");
151   verifyFormat("let [a, b, c] = [1, 2, 3];");
152   verifyFormat("var {a, b} = {a: 1, b: 2};");
153   verifyFormat("let {a, b} = {a: 1, b: 2};");
154 }
155 
156 TEST_F(FormatTestJS, ContainerLiterals) {
157   verifyFormat("var x = {y: function(a) { return a; }};");
158   verifyFormat("return {\n"
159                "  link: function() {\n"
160                "    f();  //\n"
161                "  }\n"
162                "};");
163   verifyFormat("return {\n"
164                "  a: a,\n"
165                "  link: function() {\n"
166                "    f();  //\n"
167                "  }\n"
168                "};");
169   verifyFormat("return {\n"
170                "  a: a,\n"
171                "  link: function() {\n"
172                "    f();  //\n"
173                "  },\n"
174                "  link: function() {\n"
175                "    f();  //\n"
176                "  }\n"
177                "};");
178   verifyFormat("var stuff = {\n"
179                "  // comment for update\n"
180                "  update: false,\n"
181                "  // comment for modules\n"
182                "  modules: false,\n"
183                "  // comment for tasks\n"
184                "  tasks: false\n"
185                "};");
186   verifyFormat("return {\n"
187                "  'finish':\n"
188                "      //\n"
189                "      a\n"
190                "};");
191   verifyFormat("var obj = {\n"
192                "  fooooooooo: function(x) {\n"
193                "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
194                "  }\n"
195                "};");
196   // Simple object literal, as opposed to enum style below.
197   verifyFormat("var obj = {a: 123};");
198   // Enum style top level assignment.
199   verifyFormat("X = {\n  a: 123\n};");
200   verifyFormat("X.Y = {\n  a: 123\n};");
201   // But only on the top level, otherwise its a plain object literal assignment.
202   verifyFormat("function x() {\n"
203                "  y = {z: 1};\n"
204                "}");
205   verifyFormat("x = foo && {a: 123};");
206 
207   // Arrow functions in object literals.
208   verifyFormat("var x = {y: (a) => { return a; }};");
209   verifyFormat("var x = {y: (a) => a};");
210 
211   // Computed keys.
212   verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};");
213   verifyFormat("var x = {\n"
214                "  [a]: 1,\n"
215                "  b: 2,\n"
216                "  [c]: 3,\n"
217                "};");
218 
219   // Object literals can leave out labels.
220   verifyFormat("f({a}, () => {\n"
221                "  g();  //\n"
222                "});");
223 
224   // Keys can be quoted.
225   verifyFormat("var x = {\n"
226                "  a: a,\n"
227                "  b: b,\n"
228                "  'c': c,\n"
229                "};");
230 }
231 
232 TEST_F(FormatTestJS, MethodsInObjectLiterals) {
233   verifyFormat("var o = {\n"
234                "  value: 'test',\n"
235                "  get value() {  // getter\n"
236                "    return this.value;\n"
237                "  }\n"
238                "};");
239   verifyFormat("var o = {\n"
240                "  value: 'test',\n"
241                "  set value(val) {  // setter\n"
242                "    this.value = val;\n"
243                "  }\n"
244                "};");
245   verifyFormat("var o = {\n"
246                "  value: 'test',\n"
247                "  someMethod(val) {  // method\n"
248                "    doSomething(this.value + val);\n"
249                "  }\n"
250                "};");
251   verifyFormat("var o = {\n"
252                "  someMethod(val) {  // method\n"
253                "    doSomething(this.value + val);\n"
254                "  },\n"
255                "  someOtherMethod(val) {  // method\n"
256                "    doSomething(this.value + val);\n"
257                "  }\n"
258                "};");
259 }
260 
261 TEST_F(FormatTestJS, SpacesInContainerLiterals) {
262   verifyFormat("var arr = [1, 2, 3];");
263   verifyFormat("f({a: 1, b: 2, c: 3});");
264 
265   verifyFormat("var object_literal_with_long_name = {\n"
266                "  a: 'aaaaaaaaaaaaaaaaaa',\n"
267                "  b: 'bbbbbbbbbbbbbbbbbb'\n"
268                "};");
269 
270   verifyFormat("f({a: 1, b: 2, c: 3});",
271                getChromiumStyle(FormatStyle::LK_JavaScript));
272   verifyFormat("f({'a': [{}]});");
273 }
274 
275 TEST_F(FormatTestJS, SingleQuotedStrings) {
276   verifyFormat("this.function('', true);");
277 }
278 
279 TEST_F(FormatTestJS, GoogScopes) {
280   verifyFormat("goog.scope(function() {\n"
281                "var x = a.b;\n"
282                "var y = c.d;\n"
283                "});  // goog.scope");
284   verifyFormat("goog.scope(function() {\n"
285                "// test\n"
286                "var x = 0;\n"
287                "// test\n"
288                "});");
289 }
290 
291 TEST_F(FormatTestJS, GoogModules) {
292   verifyFormat("goog.module('this.is.really.absurdly.long');",
293                getGoogleJSStyleWithColumns(40));
294   verifyFormat("goog.require('this.is.really.absurdly.long');",
295                getGoogleJSStyleWithColumns(40));
296   verifyFormat("goog.provide('this.is.really.absurdly.long');",
297                getGoogleJSStyleWithColumns(40));
298   verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
299                getGoogleJSStyleWithColumns(40));
300   verifyFormat("goog.setTestOnly('this.is.really.absurdly.long');",
301                getGoogleJSStyleWithColumns(40));
302   verifyFormat("goog.forwardDeclare('this.is.really.absurdly.long');",
303                getGoogleJSStyleWithColumns(40));
304 
305   // These should be wrapped normally.
306   verifyFormat(
307       "var MyLongClassName =\n"
308       "    goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
309 }
310 
311 TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
312   verifyFormat("function outer1(a, b) {\n"
313                "  function inner1(a, b) { return a; }\n"
314                "  inner1(a, b);\n"
315                "}\n"
316                "function outer2(a, b) {\n"
317                "  function inner2(a, b) { return a; }\n"
318                "  inner2(a, b);\n"
319                "}");
320   verifyFormat("function f() {}");
321 }
322 
323 TEST_F(FormatTestJS, ArrayLiterals) {
324   verifyFormat("var aaaaa: List<SomeThing> =\n"
325                "    [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];");
326   verifyFormat("return [\n"
327                "  aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
328                "  ccccccccccccccccccccccccccc\n"
329                "];");
330   verifyFormat("return [\n"
331                "  aaaa().bbbbbbbb('A'),\n"
332                "  aaaa().bbbbbbbb('B'),\n"
333                "  aaaa().bbbbbbbb('C'),\n"
334                "];");
335   verifyFormat("var someVariable = SomeFunction([\n"
336                "  aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
337                "  ccccccccccccccccccccccccccc\n"
338                "]);");
339   verifyFormat("var someVariable = SomeFunction([\n"
340                "  [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n"
341                "]);",
342                getGoogleJSStyleWithColumns(51));
343   verifyFormat("var someVariable = SomeFunction(aaaa, [\n"
344                "  aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
345                "  ccccccccccccccccccccccccccc\n"
346                "]);");
347   verifyFormat("var someVariable = SomeFunction(\n"
348                "    aaaa,\n"
349                "    [\n"
350                "      aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
351                "      cccccccccccccccccccccccccc\n"
352                "    ],\n"
353                "    aaaa);");
354   verifyFormat("var aaaa = aaaaa ||  // wrap\n"
355                "    [];");
356 
357   verifyFormat("someFunction([], {a: a});");
358 }
359 
360 TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) {
361   verifyFormat("var array = [\n"
362                "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
363                "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
364                "];");
365   verifyFormat("var array = someFunction([\n"
366                "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
367                "  a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
368                "]);");
369 }
370 
371 TEST_F(FormatTestJS, FunctionLiterals) {
372   verifyFormat("doFoo(function() {});");
373   verifyFormat("doFoo(function() { return 1; });");
374   verifyFormat("var func = function() {\n"
375                "  return 1;\n"
376                "};");
377   verifyFormat("var func =  //\n"
378                "    function() {\n"
379                "  return 1;\n"
380                "};");
381   verifyFormat("return {\n"
382                "  body: {\n"
383                "    setAttribute: function(key, val) { this[key] = val; },\n"
384                "    getAttribute: function(key) { return this[key]; },\n"
385                "    style: {direction: ''}\n"
386                "  }\n"
387                "};");
388   verifyFormat("abc = xyz ? function() {\n"
389                "  return 1;\n"
390                "} : function() {\n"
391                "  return -1;\n"
392                "};");
393 
394   verifyFormat("var closure = goog.bind(\n"
395                "    function() {  // comment\n"
396                "      foo();\n"
397                "      bar();\n"
398                "    },\n"
399                "    this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
400                "    arg3IsReallyLongAndNeeedsLineBreaks);");
401   verifyFormat("var closure = goog.bind(function() {  // comment\n"
402                "  foo();\n"
403                "  bar();\n"
404                "}, this);");
405   verifyFormat("return {\n"
406                "  a: 'E',\n"
407                "  b: function() {\n"
408                "    return function() {\n"
409                "      f();  //\n"
410                "    };\n"
411                "  }\n"
412                "};");
413   verifyFormat("{\n"
414                "  var someVariable = function(x) {\n"
415                "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
416                "  };\n"
417                "}");
418   verifyFormat("someLooooooooongFunction(\n"
419                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
420                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
421                "    function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
422                "      // code\n"
423                "    });");
424 
425   verifyFormat("f({a: function() { return 1; }});",
426                getGoogleJSStyleWithColumns(33));
427   verifyFormat("f({\n"
428                "  a: function() { return 1; }\n"
429                "});",
430                getGoogleJSStyleWithColumns(32));
431 
432   verifyFormat("return {\n"
433                "  a: function SomeFunction() {\n"
434                "    // ...\n"
435                "    return 1;\n"
436                "  }\n"
437                "};");
438   verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
439                "    .then(goog.bind(function(aaaaaaaaaaa) {\n"
440                "      someFunction();\n"
441                "      someFunction();\n"
442                "    }, this), aaaaaaaaaaaaaaaaa);");
443 
444   verifyFormat("someFunction(goog.bind(function() {\n"
445                "  doSomething();\n"
446                "  doSomething();\n"
447                "}, this), goog.bind(function() {\n"
448                "  doSomething();\n"
449                "  doSomething();\n"
450                "}, this));");
451 
452   // FIXME: This is bad, we should be wrapping before "function() {".
453   verifyFormat("someFunction(function() {\n"
454                "  doSomething();  // break\n"
455                "})\n"
456                "    .doSomethingElse(\n"
457                "        // break\n"
458                "        );");
459 }
460 
461 TEST_F(FormatTestJS, InliningFunctionLiterals) {
462   FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
463   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
464   verifyFormat("var func = function() {\n"
465                "  return 1;\n"
466                "};",
467                Style);
468   verifyFormat("var func = doSomething(function() { return 1; });", Style);
469   verifyFormat("var outer = function() {\n"
470                "  var inner = function() { return 1; }\n"
471                "};",
472                Style);
473   verifyFormat("function outer1(a, b) {\n"
474                "  function inner1(a, b) { return a; }\n"
475                "}",
476                Style);
477 
478   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
479   verifyFormat("var func = function() { return 1; };", Style);
480   verifyFormat("var func = doSomething(function() { return 1; });", Style);
481   verifyFormat(
482       "var outer = function() { var inner = function() { return 1; } };",
483       Style);
484   verifyFormat("function outer1(a, b) {\n"
485                "  function inner1(a, b) { return a; }\n"
486                "}",
487                Style);
488 
489   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
490   verifyFormat("var func = function() {\n"
491                "  return 1;\n"
492                "};",
493                Style);
494   verifyFormat("var func = doSomething(function() {\n"
495                "  return 1;\n"
496                "});",
497                Style);
498   verifyFormat("var outer = function() {\n"
499                "  var inner = function() {\n"
500                "    return 1;\n"
501                "  }\n"
502                "};",
503                Style);
504   verifyFormat("function outer1(a, b) {\n"
505                "  function inner1(a, b) {\n"
506                "    return a;\n"
507                "  }\n"
508                "}",
509                Style);
510 
511   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
512   verifyFormat("var func = function() {\n"
513                "  return 1;\n"
514                "};",
515                Style);
516 }
517 
518 TEST_F(FormatTestJS, MultipleFunctionLiterals) {
519   verifyFormat("promise.then(\n"
520                "    function success() {\n"
521                "      doFoo();\n"
522                "      doBar();\n"
523                "    },\n"
524                "    function error() {\n"
525                "      doFoo();\n"
526                "      doBaz();\n"
527                "    },\n"
528                "    []);\n");
529   verifyFormat("promise.then(\n"
530                "    function success() {\n"
531                "      doFoo();\n"
532                "      doBar();\n"
533                "    },\n"
534                "    [],\n"
535                "    function error() {\n"
536                "      doFoo();\n"
537                "      doBaz();\n"
538                "    });\n");
539   verifyFormat("promise.then(\n"
540                "    [],\n"
541                "    function success() {\n"
542                "      doFoo();\n"
543                "      doBar();\n"
544                "    },\n"
545                "    function error() {\n"
546                "      doFoo();\n"
547                "      doBaz();\n"
548                "    });\n");
549 
550   verifyFormat("getSomeLongPromise()\n"
551                "    .then(function(value) { body(); })\n"
552                "    .thenCatch(function(error) {\n"
553                "      body();\n"
554                "      body();\n"
555                "    });");
556   verifyFormat("getSomeLongPromise()\n"
557                "    .then(function(value) {\n"
558                "      body();\n"
559                "      body();\n"
560                "    })\n"
561                "    .thenCatch(function(error) {\n"
562                "      body();\n"
563                "      body();\n"
564                "    });");
565 
566   verifyFormat("getSomeLongPromise()\n"
567                "    .then(function(value) { body(); })\n"
568                "    .thenCatch(function(error) { body(); });");
569 
570   verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n"
571                "    .aaaaaaa(function() {\n"
572                "      //\n"
573                "    })\n"
574                "    .bbbbbb();");
575 }
576 
577 TEST_F(FormatTestJS, ArrowFunctions) {
578   verifyFormat("var x = (a) => {\n"
579                "  return a;\n"
580                "};");
581   verifyFormat("var x = (a) => {\n"
582                "  function y() { return 42; }\n"
583                "  return a;\n"
584                "};");
585   verifyFormat("var x = (a: type): {some: type} => {\n"
586                "  return a;\n"
587                "};");
588   verifyFormat("var x = (a) => a;");
589   verifyFormat("return () => [];");
590   verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n"
591                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
592                "      (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
593                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n"
594                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
595                "};");
596   verifyFormat("var a = a.aaaaaaa(\n"
597                "    (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n"
598                "        aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
599   verifyFormat("var a = a.aaaaaaa(\n"
600                "    (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n"
601                "        aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n"
602                "        aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
603 
604   // FIXME: This is bad, we should be wrapping before "() => {".
605   verifyFormat("someFunction(() => {\n"
606                "  doSomething();  // break\n"
607                "})\n"
608                "    .doSomethingElse(\n"
609                "        // break\n"
610                "        );");
611 }
612 
613 TEST_F(FormatTestJS, ReturnStatements) {
614   verifyFormat("function() {\n"
615                "  return [hello, world];\n"
616                "}");
617 }
618 
619 TEST_F(FormatTestJS, ForLoops) {
620   verifyFormat("for (var i in [2, 3]) {\n"
621                "}");
622   verifyFormat("for (var i of [2, 3]) {\n"
623                "}");
624   verifyFormat("for (let {a, b} of x) {\n"
625                "}");
626   verifyFormat("for (let {a, b} in x) {\n"
627                "}");
628 }
629 
630 TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) {
631   // The following statements must not wrap, as otherwise the program meaning
632   // would change due to automatic semicolon insertion.
633   // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1.
634   verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10));
635   verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10));
636   verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10));
637   verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10));
638   verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10));
639   verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10));
640   verifyFormat("return [\n"
641                "  aaa\n"
642                "];",
643                getGoogleJSStyleWithColumns(12));
644 }
645 
646 TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {
647   verifyFormat("a\n"
648                "b;",
649                " a \n"
650                " b ;");
651   verifyFormat("a()\n"
652                "b;",
653                " a ()\n"
654                " b ;");
655   verifyFormat("a[b]\n"
656                "c;",
657                "a [b]\n"
658                "c ;");
659   verifyFormat("1\n"
660                "a;",
661                "1 \n"
662                "a ;");
663   verifyFormat("a\n"
664                "1;",
665                "a \n"
666                "1 ;");
667   verifyFormat("a\n"
668                "'x';",
669                "a \n"
670                " 'x';");
671   verifyFormat("a++\n"
672                "b;",
673                "a ++\n"
674                "b ;");
675   verifyFormat("a\n"
676                "!b && c;",
677                "a \n"
678                " ! b && c;");
679   verifyFormat("a\n"
680                "if (1) f();",
681                " a\n"
682                " if (1) f();");
683   verifyFormat("a\n"
684                "class X {}",
685                " a\n"
686                " class X {}");
687   verifyFormat("var a", "var\n"
688                         "a");
689   verifyFormat("x instanceof String", "x\n"
690                                       "instanceof\n"
691                                       "String");
692   verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n"
693                                           "  bar) {}");
694 }
695 
696 TEST_F(FormatTestJS, ClosureStyleCasts) {
697   verifyFormat("var x = /** @type {foo} */ (bar);");
698 }
699 
700 TEST_F(FormatTestJS, TryCatch) {
701   verifyFormat("try {\n"
702                "  f();\n"
703                "} catch (e) {\n"
704                "  g();\n"
705                "} finally {\n"
706                "  h();\n"
707                "}");
708 
709   // But, of course, "catch" is a perfectly fine function name in JavaScript.
710   verifyFormat("someObject.catch();");
711   verifyFormat("someObject.new();");
712   verifyFormat("someObject.delete();");
713 }
714 
715 TEST_F(FormatTestJS, StringLiteralConcatenation) {
716   verifyFormat("var literal = 'hello ' +\n"
717                "    'world';");
718 }
719 
720 TEST_F(FormatTestJS, RegexLiteralClassification) {
721   // Regex literals.
722   verifyFormat("var regex = /abc/;");
723   verifyFormat("f(/abc/);");
724   verifyFormat("f(abc, /abc/);");
725   verifyFormat("some_map[/abc/];");
726   verifyFormat("var x = a ? /abc/ : /abc/;");
727   verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
728   verifyFormat("var x = !/abc/.test(y);");
729   verifyFormat("var x = a && /abc/.test(y);");
730   verifyFormat("var x = a || /abc/.test(y);");
731   verifyFormat("var x = a + /abc/.search(y);");
732   verifyFormat("/abc/.search(y);");
733   verifyFormat("var regexs = {/abc/, /abc/};");
734   verifyFormat("return /abc/;");
735 
736   // Not regex literals.
737   verifyFormat("var a = a / 2 + b / 3;");
738   verifyFormat("var a = a++ / 2;");
739   // Prefix unary can operate on regex literals, not that it makes sense.
740   verifyFormat("var a = ++/a/;");
741 
742   // This is a known issue, regular expressions are incorrectly detected if
743   // directly following a closing parenthesis.
744   verifyFormat("if (foo) / bar /.exec(baz);");
745 }
746 
747 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
748   verifyFormat("var regex = /=/;");
749   verifyFormat("var regex = /a*/;");
750   verifyFormat("var regex = /a+/;");
751   verifyFormat("var regex = /a?/;");
752   verifyFormat("var regex = /.a./;");
753   verifyFormat("var regex = /a\\*/;");
754   verifyFormat("var regex = /^a$/;");
755   verifyFormat("var regex = /\\/a/;");
756   verifyFormat("var regex = /(?:x)/;");
757   verifyFormat("var regex = /x(?=y)/;");
758   verifyFormat("var regex = /x(?!y)/;");
759   verifyFormat("var regex = /x|y/;");
760   verifyFormat("var regex = /a{2}/;");
761   verifyFormat("var regex = /a{1,3}/;");
762 
763   verifyFormat("var regex = /[abc]/;");
764   verifyFormat("var regex = /[^abc]/;");
765   verifyFormat("var regex = /[\\b]/;");
766   verifyFormat("var regex = /[/]/;");
767   verifyFormat("var regex = /[\\/]/;");
768   verifyFormat("var regex = /\\[/;");
769   verifyFormat("var regex = /\\\\[/]/;");
770   verifyFormat("var regex = /}[\"]/;");
771   verifyFormat("var regex = /}[/\"]/;");
772   verifyFormat("var regex = /}[\"/]/;");
773 
774   verifyFormat("var regex = /\\b/;");
775   verifyFormat("var regex = /\\B/;");
776   verifyFormat("var regex = /\\d/;");
777   verifyFormat("var regex = /\\D/;");
778   verifyFormat("var regex = /\\f/;");
779   verifyFormat("var regex = /\\n/;");
780   verifyFormat("var regex = /\\r/;");
781   verifyFormat("var regex = /\\s/;");
782   verifyFormat("var regex = /\\S/;");
783   verifyFormat("var regex = /\\t/;");
784   verifyFormat("var regex = /\\v/;");
785   verifyFormat("var regex = /\\w/;");
786   verifyFormat("var regex = /\\W/;");
787   verifyFormat("var regex = /a(a)\\1/;");
788   verifyFormat("var regex = /\\0/;");
789   verifyFormat("var regex = /\\\\/g;");
790   verifyFormat("var regex = /\\a\\\\/g;");
791   verifyFormat("var regex = /\a\\//g;");
792   verifyFormat("var regex = /a\\//;\n"
793                "var x = 0;");
794   verifyFormat("var regex = /'/g;", "var regex = /'/g ;");
795   verifyFormat("var regex = /'/g;  //'", "var regex = /'/g ; //'");
796   verifyFormat("var regex = /\\/*/;\n"
797                "var x = 0;",
798                "var regex = /\\/*/;\n"
799                "var x=0;");
800   verifyFormat("var x = /a\\//;", "var x = /a\\//  \n;");
801   verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16));
802   verifyFormat("var regex =\n"
803                "    /\"/;",
804                getGoogleJSStyleWithColumns(15));
805   verifyFormat("var regex =  //\n"
806                "    /a/;");
807   verifyFormat("var regexs = [\n"
808                "  /d/,   //\n"
809                "  /aa/,  //\n"
810                "];");
811 }
812 
813 TEST_F(FormatTestJS, RegexLiteralModifiers) {
814   verifyFormat("var regex = /abc/g;");
815   verifyFormat("var regex = /abc/i;");
816   verifyFormat("var regex = /abc/m;");
817   verifyFormat("var regex = /abc/y;");
818 }
819 
820 TEST_F(FormatTestJS, RegexLiteralLength) {
821   verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
822                getGoogleJSStyleWithColumns(60));
823   verifyFormat("var regex =\n"
824                "    /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
825                getGoogleJSStyleWithColumns(60));
826   verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
827                getGoogleJSStyleWithColumns(50));
828 }
829 
830 TEST_F(FormatTestJS, RegexLiteralExamples) {
831   verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
832 }
833 
834 TEST_F(FormatTestJS, TypeAnnotations) {
835   verifyFormat("var x: string;");
836   verifyFormat("var x: {a: string; b: number;} = {};");
837   verifyFormat("function x(): string {\n  return 'x';\n}");
838   verifyFormat("function x(): {x: string} {\n  return {x: 'x'};\n}");
839   verifyFormat("function x(y: string): string {\n  return 'x';\n}");
840   verifyFormat("for (var y: string in x) {\n  x();\n}");
841   verifyFormat("for (var y: string of x) {\n  x();\n}");
842   verifyFormat("function x(y: {a?: number;} = {}): number {\n"
843                "  return 12;\n"
844                "}");
845   verifyFormat("((a: string, b: number): string => a + b);");
846   verifyFormat("var x: (y: number) => string;");
847   verifyFormat("var x: P<string, (a: number) => string>;");
848   verifyFormat("var x = {y: function(): z { return 1; }};");
849   verifyFormat("var x = {y: function(): {a: number} { return 1; }};");
850   verifyFormat("function someFunc(args: string[]):\n"
851                "    {longReturnValue: string[]} {}",
852                getGoogleJSStyleWithColumns(60));
853 }
854 
855 TEST_F(FormatTestJS, UnionIntersectionTypes) {
856   verifyFormat("let x: A|B = A | B;");
857   verifyFormat("let x: A&B|C = A & B;");
858   verifyFormat("let x: Foo<A|B> = new Foo<A|B>();");
859   verifyFormat("function(x: A|B): C&D {}");
860   verifyFormat("function(x: A|B = A | B): C&D {}");
861 }
862 
863 TEST_F(FormatTestJS, ClassDeclarations) {
864   verifyFormat("class C {\n  x: string = 12;\n}");
865   verifyFormat("class C {\n  x(): string => 12;\n}");
866   verifyFormat("class C {\n  ['x' + 2]: string = 12;\n}");
867   verifyFormat("class C {\n  private x: string = 12;\n}");
868   verifyFormat("class C {\n  private static x: string = 12;\n}");
869   verifyFormat("class C {\n  static x(): string { return 'asd'; }\n}");
870   verifyFormat("class C extends P implements I {}");
871   verifyFormat("class C extends p.P implements i.I {}");
872   verifyFormat("class Test {\n"
873                "  aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n"
874                "      aaaaaaaaaaaaaaaaaaaaaa {}\n"
875                "}");
876   verifyFormat("foo = class Name {\n"
877                "  constructor() {}\n"
878                "};");
879   verifyFormat("foo = class {\n"
880                "  constructor() {}\n"
881                "};");
882   verifyFormat("class C {\n"
883                "  x: {y: Z;} = {};\n"
884                "  private y: {y: Z;} = {};\n"
885                "}");
886 
887   // ':' is not a type declaration here.
888   verifyFormat("class X {\n"
889                "  subs = {\n"
890                "    'b': {\n"
891                "      'c': 1,\n"
892                "    },\n"
893                "  };\n"
894                "}");
895 }
896 
897 TEST_F(FormatTestJS, InterfaceDeclarations) {
898   verifyFormat("interface I {\n"
899                "  x: string;\n"
900                "  enum: string[];\n"
901                "  enum?: string[];\n"
902                "}\n"
903                "var y;");
904   // Ensure that state is reset after parsing the interface.
905   verifyFormat("interface a {}\n"
906                "export function b() {}\n"
907                "var x;");
908 
909   // Arrays of object type literals.
910   verifyFormat("interface I {\n"
911                "  o: {}[];\n"
912                "}");
913 }
914 
915 TEST_F(FormatTestJS, EnumDeclarations) {
916   verifyFormat("enum Foo {\n"
917                "  A = 1,\n"
918                "  B\n"
919                "}");
920   verifyFormat("export /* somecomment*/ enum Foo {\n"
921                "  A = 1,\n"
922                "  B\n"
923                "}");
924   verifyFormat("enum Foo {\n"
925                "  A = 1,  // comment\n"
926                "  B\n"
927                "}\n"
928                "var x = 1;");
929 }
930 
931 TEST_F(FormatTestJS, MetadataAnnotations) {
932   verifyFormat("@A\nclass C {\n}");
933   verifyFormat("@A({arg: 'value'})\nclass C {\n}");
934   verifyFormat("@A\n@B\nclass C {\n}");
935   verifyFormat("class C {\n  @A x: string;\n}");
936   verifyFormat("class C {\n"
937                "  @A\n"
938                "  private x(): string {\n"
939                "    return 'y';\n"
940                "  }\n"
941                "}");
942   verifyFormat("class C {\n"
943                "  private x(@A x: string) {}\n"
944                "}");
945   verifyFormat("class X {}\n"
946                "class Y {}");
947 }
948 
949 TEST_F(FormatTestJS, TypeAliases) {
950   verifyFormat("type X = number;\n"
951                "class C {}");
952   verifyFormat("type X<Y> = Z<Y>;");
953   verifyFormat("type X = {\n"
954                "  y: number\n"
955                "};\n"
956                "class C {}");
957 }
958 
959 TEST_F(FormatTestJS, Modules) {
960   verifyFormat("import SomeThing from 'some/module.js';");
961   verifyFormat("import {X, Y} from 'some/module.js';");
962   verifyFormat("import a, {X, Y} from 'some/module.js';");
963   verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,"
964                " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying"
965                "} from 'some/module.js';");
966   verifyFormat("import {X, Y,} from 'some/module.js';");
967   verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
968   verifyFormat("import * as lib from 'some/module.js';");
969   verifyFormat("var x = {import: 1};\nx.import = 2;");
970 
971   verifyFormat("export function fn() {\n"
972                "  return 'fn';\n"
973                "}");
974   verifyFormat("export function A() {}\n"
975                "export default function B() {}\n"
976                "export function C() {}");
977   verifyFormat("export default () => {\n"
978                "  let x = 1;\n"
979                "  return x;\n"
980                "}");
981   verifyFormat("export const x = 12;");
982   verifyFormat("export default class X {}");
983   verifyFormat("export {X, Y} from 'some/module.js';");
984   verifyFormat("export {X, Y,} from 'some/module.js';");
985   verifyFormat("export {SomeVeryLongExport as X, "
986                "SomeOtherVeryLongExport as Y} from 'some/module.js';");
987   // export without 'from' is wrapped.
988   verifyFormat("export let someRatherLongVariableName =\n"
989                "    someSurprisinglyLongVariable + someOtherRatherLongVar;");
990   // ... but not if from is just an identifier.
991   verifyFormat("export {\n"
992                "  from as from,\n"
993                "  someSurprisinglyLongVariable\n"
994                "      as from\n"
995                "};",
996                getGoogleJSStyleWithColumns(20));
997   verifyFormat("export class C {\n"
998                "  x: number;\n"
999                "  y: string;\n"
1000                "}");
1001   verifyFormat("export class X { y: number; }");
1002   verifyFormat("export abstract class X { y: number; }");
1003   verifyFormat("export default class X { y: number }");
1004   verifyFormat("export default function() {\n  return 1;\n}");
1005   verifyFormat("export var x = 12;");
1006   verifyFormat("class C {}\n"
1007                "export function f() {}\n"
1008                "var v;");
1009   verifyFormat("export var x: number = 12;");
1010   verifyFormat("export const y = {\n"
1011                "  a: 1,\n"
1012                "  b: 2\n"
1013                "};");
1014   verifyFormat("export enum Foo {\n"
1015                "  BAR,\n"
1016                "  // adsdasd\n"
1017                "  BAZ\n"
1018                "}");
1019   verifyFormat("export default [\n"
1020                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1021                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
1022                "];");
1023   verifyFormat("export default [];");
1024   verifyFormat("export default () => {};");
1025   verifyFormat("export interface Foo { foo: number; }\n"
1026                "export class Bar {\n"
1027                "  blah(): string { return this.blah; };\n"
1028                "}");
1029 }
1030 
1031 TEST_F(FormatTestJS, TemplateStrings) {
1032   // Keeps any whitespace/indentation within the template string.
1033   verifyFormat("var x = `hello\n"
1034             "     ${  name    }\n"
1035             "  !`;",
1036             "var x    =    `hello\n"
1037                    "     ${  name    }\n"
1038                    "  !`;");
1039 
1040   verifyFormat("var x =\n"
1041                "    `hello ${world}` >= some();",
1042                getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
1043   verifyFormat("var x = `hello ${world}` >= some();",
1044                getGoogleJSStyleWithColumns(35)); // Barely fits.
1045   verifyFormat("var x = `hello\n"
1046             "  ${world}` >=\n"
1047             "    some();",
1048             "var x =\n"
1049                    "    `hello\n"
1050                    "  ${world}` >= some();",
1051                    getGoogleJSStyleWithColumns(21)); // Barely doesn't fit.
1052   verifyFormat("var x = `hello\n"
1053             "  ${world}` >= some();",
1054             "var x =\n"
1055                    "    `hello\n"
1056                    "  ${world}` >= some();",
1057                    getGoogleJSStyleWithColumns(22)); // Barely fits.
1058 
1059   verifyFormat("var x =\n"
1060                "    `h`;",
1061                getGoogleJSStyleWithColumns(11));
1062   verifyFormat("var x =\n    `multi\n  line`;", "var x = `multi\n  line`;",
1063                getGoogleJSStyleWithColumns(13));
1064   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1065                "    `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);");
1066 
1067   // Make sure template strings get a proper ColumnWidth assigned, even if they
1068   // are first token in line.
1069   verifyFormat(
1070       "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
1071       "    `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;");
1072 
1073   // Two template strings.
1074   verifyFormat("var x = `hello` == `hello`;");
1075 
1076   // Comments in template strings.
1077   verifyFormat("var x = `//a`;\n"
1078             "var y;",
1079             "var x =\n `//a`;\n"
1080                    "var y  ;");
1081   verifyFormat("var x = `/*a`;\n"
1082                "var y;",
1083                "var x =\n `/*a`;\n"
1084                "var y;");
1085   // Unterminated string literals in a template string.
1086   verifyFormat("var x = `'`;  // comment with matching quote '\n"
1087                "var y;");
1088   verifyFormat("var x = `\"`;  // comment with matching quote \"\n"
1089                "var y;");
1090   verifyFormat("it(`'aaaaaaaaaaaaaaa   `, aaaaaaaaa);",
1091                "it(`'aaaaaaaaaaaaaaa   `,   aaaaaaaaa) ;",
1092                getGoogleJSStyleWithColumns(40));
1093   // Backticks in a comment - not a template string.
1094   verifyFormat("var x = 1  // `/*a`;\n"
1095                "    ;",
1096                "var x =\n 1  // `/*a`;\n"
1097                "    ;");
1098   verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */");
1099   // Comment spans multiple template strings.
1100   verifyFormat("var x = `/*a`;\n"
1101                "var y = ` */ `;",
1102                "var x =\n `/*a`;\n"
1103                "var y =\n ` */ `;");
1104   // Escaped backtick.
1105   verifyFormat("var x = ` \\` a`;\n"
1106                "var y;",
1107                "var x = ` \\` a`;\n"
1108                "var y;");
1109 }
1110 
1111 TEST_F(FormatTestJS, CastSyntax) {
1112   verifyFormat("var x = <type>foo;");
1113   verifyFormat("var x = foo as type;");
1114 }
1115 
1116 TEST_F(FormatTestJS, TypeArguments) {
1117   verifyFormat("class X<Y> {}");
1118   verifyFormat("new X<Y>();");
1119   verifyFormat("foo<Y>(a);");
1120   verifyFormat("var x: X<Y>[];");
1121   verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
1122   verifyFormat("function f(a: List<any> = null) {}");
1123   verifyFormat("function f(): List<any> {}");
1124   verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n"
1125                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}");
1126   verifyFormat("function aaaaaaaaaa(\n"
1127                "    aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n"
1128                "    aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n"
1129                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}");
1130 }
1131 
1132 TEST_F(FormatTestJS, UserDefinedTypeGuards) {
1133   verifyFormat(
1134       "function foo(check: Object):\n"
1135       "    check is {foo: string, bar: string, baz: string, foobar: string} {\n"
1136       "  return 'bar' in check;\n"
1137       "}\n");
1138 }
1139 
1140 TEST_F(FormatTestJS, OptionalTypes) {
1141   verifyFormat("function x(a?: b, c?, d?) {}");
1142   verifyFormat("class X {\n"
1143                "  y?: z;\n"
1144                "  z?;\n"
1145                "}");
1146   verifyFormat("interface X {\n"
1147                "  y?(): z;\n"
1148                "}");
1149   verifyFormat("x ? 1 : 2;");
1150   verifyFormat("constructor({aa}: {\n"
1151                "  aa?: string,\n"
1152                "  aaaaaaaa?: string,\n"
1153                "  aaaaaaaaaaaaaaa?: boolean,\n"
1154                "  aaaaaa?: List<string>\n"
1155                "}) {}");
1156 }
1157 
1158 TEST_F(FormatTestJS, IndexSignature) {
1159   verifyFormat("var x: {[k: string]: v};");
1160 }
1161 
1162 TEST_F(FormatTestJS, WrapAfterParen) {
1163   verifyFormat("xxxxxxxxxxx(\n"
1164                "    aaa, aaa);",
1165                getGoogleJSStyleWithColumns(20));
1166   verifyFormat("xxxxxxxxxxx(\n"
1167                "    aaa, aaa, aaa,\n"
1168                "    aaa, aaa, aaa);",
1169                getGoogleJSStyleWithColumns(20));
1170   verifyFormat("xxxxxxxxxxx(\n"
1171                "    aaaaaaaaaaaaaaaaaaaaaaaa,\n"
1172                "    function(x) {\n"
1173                "      y();  //\n"
1174                "    });",
1175                getGoogleJSStyleWithColumns(40));
1176   verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
1177                "       bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
1178 }
1179 
1180 TEST_F(FormatTestJS, JSDocAnnotations) {
1181   verifyFormat("/**\n"
1182                " * @export {this.is.a.long.path.to.a.Type}\n"
1183                " */",
1184                "/**\n"
1185                " * @export {this.is.a.long.path.to.a.Type}\n"
1186                " */",
1187                getGoogleJSStyleWithColumns(20));
1188 }
1189 
1190 TEST_F(FormatTestJS, RequoteStringsSingle) {
1191   verifyFormat("var x = 'foo';", "var x = \"foo\";");
1192   verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";");
1193   verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";");
1194   verifyFormat(
1195       "var x =\n"
1196       "    'foo\\'';",
1197       // Code below is 15 chars wide, doesn't fit into the line with the
1198       // \ escape added.
1199       "var x = \"foo'\";", getGoogleJSStyleWithColumns(15));
1200   // Removes no-longer needed \ escape from ".
1201   verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";");
1202   // Code below fits into 15 chars *after* removing the \ escape.
1203   verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";",
1204                getGoogleJSStyleWithColumns(15));
1205 }
1206 
1207 TEST_F(FormatTestJS, RequoteStringsDouble) {
1208   FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
1209   DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double;
1210   verifyFormat("var x = \"foo\";", DoubleQuotes);
1211   verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes);
1212   verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes);
1213 }
1214 
1215 TEST_F(FormatTestJS, RequoteStringsLeave) {
1216   FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
1217   LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave;
1218   verifyFormat("var x = \"foo\";", LeaveQuotes);
1219   verifyFormat("var x = 'foo';", LeaveQuotes);
1220 }
1221 
1222 } // end namespace tooling
1223 } // end namespace clang
1224