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