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