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