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