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