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