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