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