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