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