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