xref: /llvm-project/clang/unittests/Format/FormatTestJS.cpp (revision 5ce80de4deb139fb8d89335eae8d82de7e2ef594)
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 }
261 
262 TEST_F(FormatTestJS, FunctionLiterals) {
263   verifyFormat("doFoo(function() {});");
264   verifyFormat("doFoo(function() { return 1; });");
265   verifyFormat("var func = function() {\n"
266                "  return 1;\n"
267                "};");
268   verifyFormat("return {\n"
269                "  body: {\n"
270                "    setAttribute: function(key, val) { this[key] = val; },\n"
271                "    getAttribute: function(key) { return this[key]; },\n"
272                "    style: {direction: ''}\n"
273                "  }\n"
274                "};");
275   EXPECT_EQ("abc = xyz ?\n"
276             "          function() {\n"
277             "            return 1;\n"
278             "          } :\n"
279             "          function() {\n"
280             "            return -1;\n"
281             "          };",
282             format("abc=xyz?function(){return 1;}:function(){return -1;};"));
283 
284   verifyFormat("var closure = goog.bind(\n"
285                "    function() {  // comment\n"
286                "      foo();\n"
287                "      bar();\n"
288                "    },\n"
289                "    this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
290                "    arg3IsReallyLongAndNeeedsLineBreaks);");
291   verifyFormat("var closure = goog.bind(function() {  // comment\n"
292                "  foo();\n"
293                "  bar();\n"
294                "}, this);");
295   verifyFormat("return {\n"
296                "  a: 'E',\n"
297                "  b: function() {\n"
298                "    return function() {\n"
299                "      f();  //\n"
300                "    };\n"
301                "  }\n"
302                "};");
303   verifyFormat("{\n"
304                "  var someVariable = function(x) {\n"
305                "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
306                "  };\n"
307                "}");
308   verifyFormat("someLooooooooongFunction(\n"
309                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
310                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
311                "    function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
312                "      // code\n"
313                "    });");
314 
315   verifyFormat("f({a: function() { return 1; }});",
316                getGoogleJSStyleWithColumns(33));
317   verifyFormat("f({\n"
318                "  a: function() { return 1; }\n"
319                "});",
320                getGoogleJSStyleWithColumns(32));
321 
322   verifyFormat("return {\n"
323                "  a: function SomeFunction() {\n"
324                "    // ...\n"
325                "    return 1;\n"
326                "  }\n"
327                "};");
328   verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
329                "    .then(goog.bind(function(aaaaaaaaaaa) {\n"
330                "      someFunction();\n"
331                "      someFunction();\n"
332                "    }, this), aaaaaaaaaaaaaaaaa);");
333 
334   // FIXME: This is not ideal yet.
335   verifyFormat("someFunction(goog.bind(\n"
336                "                 function() {\n"
337                "                   doSomething();\n"
338                "                   doSomething();\n"
339                "                 },\n"
340                "                 this),\n"
341                "             goog.bind(function() {\n"
342                "               doSomething();\n"
343                "               doSomething();\n"
344                "             }, this));");
345 
346   // FIXME: This is bad, we should be wrapping before "function() {".
347   verifyFormat("someFunction(function() {\n"
348                "  doSomething();  // break\n"
349                "})\n"
350                "    .doSomethingElse(\n"
351                "        // break\n"
352                "        );");
353 }
354 
355 TEST_F(FormatTestJS, InliningFunctionLiterals) {
356   FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
357   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
358   verifyFormat("var func = function() {\n"
359                "  return 1;\n"
360                "};",
361                Style);
362   verifyFormat("var func = doSomething(function() { return 1; });", Style);
363   verifyFormat("var outer = function() {\n"
364                "  var inner = function() { return 1; }\n"
365                "};",
366                Style);
367   verifyFormat("function outer1(a, b) {\n"
368                "  function inner1(a, b) { return a; }\n"
369                "}",
370                Style);
371 
372   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
373   verifyFormat("var func = function() { return 1; };", Style);
374   verifyFormat("var func = doSomething(function() { return 1; });", Style);
375   verifyFormat(
376       "var outer = function() { var inner = function() { return 1; } };",
377       Style);
378   verifyFormat("function outer1(a, b) {\n"
379                "  function inner1(a, b) { return a; }\n"
380                "}",
381                Style);
382 
383   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
384   verifyFormat("var func = function() {\n"
385                "  return 1;\n"
386                "};",
387                Style);
388   verifyFormat("var func = doSomething(function() {\n"
389                "  return 1;\n"
390                "});",
391                Style);
392   verifyFormat("var outer = function() {\n"
393                "  var inner = function() {\n"
394                "    return 1;\n"
395                "  }\n"
396                "};",
397                Style);
398   verifyFormat("function outer1(a, b) {\n"
399                "  function inner1(a, b) {\n"
400                "    return a;\n"
401                "  }\n"
402                "}",
403                Style);
404 }
405 
406 TEST_F(FormatTestJS, MultipleFunctionLiterals) {
407   verifyFormat("promise.then(\n"
408                "    function success() {\n"
409                "      doFoo();\n"
410                "      doBar();\n"
411                "    },\n"
412                "    function error() {\n"
413                "      doFoo();\n"
414                "      doBaz();\n"
415                "    },\n"
416                "    []);\n");
417   verifyFormat("promise.then(\n"
418                "    function success() {\n"
419                "      doFoo();\n"
420                "      doBar();\n"
421                "    },\n"
422                "    [],\n"
423                "    function error() {\n"
424                "      doFoo();\n"
425                "      doBaz();\n"
426                "    });\n");
427   // FIXME: Here, we should probably break right after the "(" for consistency.
428   verifyFormat("promise.then([],\n"
429                "             function success() {\n"
430                "               doFoo();\n"
431                "               doBar();\n"
432                "             },\n"
433                "             function error() {\n"
434                "               doFoo();\n"
435                "               doBaz();\n"
436                "             });\n");
437 
438   verifyFormat("getSomeLongPromise()\n"
439                "    .then(function(value) { body(); })\n"
440                "    .thenCatch(function(error) {\n"
441                "      body();\n"
442                "      body();\n"
443                "    });");
444   verifyFormat("getSomeLongPromise()\n"
445                "    .then(function(value) {\n"
446                "      body();\n"
447                "      body();\n"
448                "    })\n"
449                "    .thenCatch(function(error) {\n"
450                "      body();\n"
451                "      body();\n"
452                "    });");
453 
454   verifyFormat("getSomeLongPromise()\n"
455                "    .then(function(value) { body(); })\n"
456                "    .thenCatch(function(error) { body(); });");
457 }
458 
459 TEST_F(FormatTestJS, ArrowFunctions) {
460   verifyFormat("var x = (a) => {\n"
461                "  return a;\n"
462                "};");
463   verifyFormat("var x = (a) => {\n"
464                "  function y() { return 42; }\n"
465                "  return a;\n"
466                "};");
467   verifyFormat("var x = (a: type): {some: type} => {\n"
468                "  return a;\n"
469                "};");
470   verifyFormat("var x = (a) => a;");
471 
472   // FIXME: This is bad, we should be wrapping before "() => {".
473   verifyFormat("someFunction(() => {\n"
474                "  doSomething();  // break\n"
475                "})\n"
476                "    .doSomethingElse(\n"
477                "        // break\n"
478                "        );");
479 }
480 
481 TEST_F(FormatTestJS, ReturnStatements) {
482   verifyFormat("function() {\n"
483                "  return [hello, world];\n"
484                "}");
485 }
486 
487 TEST_F(FormatTestJS, ClosureStyleCasts) {
488   verifyFormat("var x = /** @type {foo} */ (bar);");
489 }
490 
491 TEST_F(FormatTestJS, TryCatch) {
492   verifyFormat("try {\n"
493                "  f();\n"
494                "} catch (e) {\n"
495                "  g();\n"
496                "} finally {\n"
497                "  h();\n"
498                "}");
499 
500   // But, of course, "catch" is a perfectly fine function name in JavaScript.
501   verifyFormat("someObject.catch();");
502   verifyFormat("someObject.new();");
503   verifyFormat("someObject.delete();");
504 }
505 
506 TEST_F(FormatTestJS, StringLiteralConcatenation) {
507   verifyFormat("var literal = 'hello ' +\n"
508                "              'world';");
509 }
510 
511 TEST_F(FormatTestJS, RegexLiteralClassification) {
512   // Regex literals.
513   verifyFormat("var regex = /abc/;");
514   verifyFormat("f(/abc/);");
515   verifyFormat("f(abc, /abc/);");
516   verifyFormat("some_map[/abc/];");
517   verifyFormat("var x = a ? /abc/ : /abc/;");
518   verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
519   verifyFormat("var x = !/abc/.test(y);");
520   verifyFormat("var x = a && /abc/.test(y);");
521   verifyFormat("var x = a || /abc/.test(y);");
522   verifyFormat("var x = a + /abc/.search(y);");
523   verifyFormat("var regexs = {/abc/, /abc/};");
524   verifyFormat("return /abc/;");
525 
526   // Not regex literals.
527   verifyFormat("var a = a / 2 + b / 3;");
528 }
529 
530 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
531   verifyFormat("var regex = /=/;");
532   verifyFormat("var regex = /a*/;");
533   verifyFormat("var regex = /a+/;");
534   verifyFormat("var regex = /a?/;");
535   verifyFormat("var regex = /.a./;");
536   verifyFormat("var regex = /a\\*/;");
537   verifyFormat("var regex = /^a$/;");
538   verifyFormat("var regex = /\\/a/;");
539   verifyFormat("var regex = /(?:x)/;");
540   verifyFormat("var regex = /x(?=y)/;");
541   verifyFormat("var regex = /x(?!y)/;");
542   verifyFormat("var regex = /x|y/;");
543   verifyFormat("var regex = /a{2}/;");
544   verifyFormat("var regex = /a{1,3}/;");
545   verifyFormat("var regex = /[abc]/;");
546   verifyFormat("var regex = /[^abc]/;");
547   verifyFormat("var regex = /[\\b]/;");
548   verifyFormat("var regex = /\\b/;");
549   verifyFormat("var regex = /\\B/;");
550   verifyFormat("var regex = /\\d/;");
551   verifyFormat("var regex = /\\D/;");
552   verifyFormat("var regex = /\\f/;");
553   verifyFormat("var regex = /\\n/;");
554   verifyFormat("var regex = /\\r/;");
555   verifyFormat("var regex = /\\s/;");
556   verifyFormat("var regex = /\\S/;");
557   verifyFormat("var regex = /\\t/;");
558   verifyFormat("var regex = /\\v/;");
559   verifyFormat("var regex = /\\w/;");
560   verifyFormat("var regex = /\\W/;");
561   verifyFormat("var regex = /a(a)\\1/;");
562   verifyFormat("var regex = /\\0/;");
563   verifyFormat("var regex = /\\\\/g;");
564   verifyFormat("var regex = /\\a\\\\/g;");
565   verifyFormat("var regex = /\a\\//g;");
566   verifyFormat("var regex = /a\\//;\n"
567                "var x = 0;");
568   EXPECT_EQ("var regex = /\\/*/;\n"
569             "var x = 0;",
570             format("var regex = /\\/*/;\n"
571                    "var x=0;"));
572 }
573 
574 TEST_F(FormatTestJS, RegexLiteralModifiers) {
575   verifyFormat("var regex = /abc/g;");
576   verifyFormat("var regex = /abc/i;");
577   verifyFormat("var regex = /abc/m;");
578   verifyFormat("var regex = /abc/y;");
579 }
580 
581 TEST_F(FormatTestJS, RegexLiteralLength) {
582   verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
583                getGoogleJSStyleWithColumns(60));
584   verifyFormat("var regex =\n"
585                "    /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
586                getGoogleJSStyleWithColumns(60));
587   verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
588                getGoogleJSStyleWithColumns(50));
589 }
590 
591 TEST_F(FormatTestJS, RegexLiteralExamples) {
592   verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
593 }
594 
595 TEST_F(FormatTestJS, TypeAnnotations) {
596   verifyFormat("var x: string;");
597   verifyFormat("function x(): string {\n  return 'x';\n}");
598   verifyFormat("function x(): {x: string} {\n  return {x: 'x'};\n}");
599   verifyFormat("function x(y: string): string {\n  return 'x';\n}");
600   verifyFormat("for (var y: string in x) {\n  x();\n}");
601   verifyFormat("((a: string, b: number): string => a + b);");
602   verifyFormat("var x: (y: number) => string;");
603   verifyFormat("var x: P<string, (a: number) => string>;");
604   verifyFormat("var x = {y: function(): z { return 1; }};");
605   verifyFormat("var x = {y: function(): {a: number} { return 1; }};");
606 }
607 
608 TEST_F(FormatTestJS, ClassDeclarations) {
609   verifyFormat("class C {\n  x: string = 12;\n}");
610   verifyFormat("class C {\n  x(): string => 12;\n}");
611   verifyFormat("class C {\n  ['x' + 2]: string = 12;\n}");
612   verifyFormat("class C {\n  private x: string = 12;\n}");
613   verifyFormat("class C {\n  private static x: string = 12;\n}");
614   verifyFormat("class C {\n  static x(): string { return 'asd'; }\n}");
615   verifyFormat("class C extends P implements I {}");
616   verifyFormat("class C extends p.P implements i.I {}");
617 }
618 
619 TEST_F(FormatTestJS, InterfaceDeclarations) {
620   verifyFormat("interface I {\n"
621                "  x: string;\n"
622                "}");
623 }
624 
625 TEST_F(FormatTestJS, MetadataAnnotations) {
626   verifyFormat("@A\nclass C {\n}");
627   verifyFormat("@A({arg: 'value'})\nclass C {\n}");
628   verifyFormat("@A\n@B\nclass C {\n}");
629   verifyFormat("class C {\n  @A x: string;\n}");
630   verifyFormat("class C {\n"
631                "  @A\n"
632                "  private x(): string {\n"
633                "    return 'y';\n"
634                "  }\n"
635                "}");
636   verifyFormat("class X {}\n"
637                "class Y {}");
638 }
639 
640 TEST_F(FormatTestJS, Modules) {
641   verifyFormat("import SomeThing from 'some/module.js';");
642   verifyFormat("import {X, Y} from 'some/module.js';");
643   verifyFormat("import {\n"
644                "  VeryLongImportsAreAnnoying,\n"
645                "  VeryLongImportsAreAnnoying,\n"
646                "  VeryLongImportsAreAnnoying,\n"
647                "  VeryLongImportsAreAnnoying\n"
648                "} from 'some/module.js';");
649   verifyFormat("import {\n"
650                "  X,\n"
651                "  Y,\n"
652                "} from 'some/module.js';");
653   verifyFormat("import {\n"
654                "  X,\n"
655                "  Y,\n"
656                "} from 'some/long/module.js';",
657                getGoogleJSStyleWithColumns(20));
658   verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
659   verifyFormat("import * as lib from 'some/module.js';");
660   verifyFormat("var x = {import: 1};\nx.import = 2;");
661 
662   verifyFormat("export function fn() {\n"
663                "  return 'fn';\n"
664                "}");
665   verifyFormat("export function A() {\n"
666                "}\n"
667                "export default function B() {\n"
668                "}\n"
669                "export function C() {\n"
670                "}");
671   verifyFormat("export const x = 12;");
672   verifyFormat("export default class X {}");
673   verifyFormat("export {X, Y} from 'some/module.js';");
674   verifyFormat("export {\n"
675                "  X,\n"
676                "  Y,\n"
677                "} from 'some/module.js';");
678   verifyFormat("export class C {\n"
679                "  x: number;\n"
680                "  y: string;\n"
681                "}");
682   verifyFormat("export class X { y: number; }");
683   verifyFormat("export default class X { y: number }");
684   verifyFormat("export default function() {\n  return 1;\n}");
685   verifyFormat("export var x = 12;");
686   verifyFormat("export var x: number = 12;");
687   verifyFormat("export const y = {\n"
688                "  a: 1,\n"
689                "  b: 2\n"
690                "};");
691 }
692 
693 TEST_F(FormatTestJS, TemplateStrings) {
694   // Keeps any whitespace/indentation within the template string.
695   EXPECT_EQ("var x = `hello\n"
696             "     ${  name    }\n"
697             "  !`;",
698             format("var x    =    `hello\n"
699                    "     ${  name    }\n"
700                    "  !`;"));
701 
702   // FIXME: +1 / -1 offsets are to work around clang-format miscalculating
703   // widths for unknown tokens that are not whitespace (e.g. '`'). Remove when
704   // the code is corrected.
705 
706   verifyFormat("var x =\n"
707                "    `hello ${world}` >= some();",
708                getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
709   verifyFormat("var x = `hello ${world}` >= some();",
710                getGoogleJSStyleWithColumns(35 + 1)); // Barely fits.
711   EXPECT_EQ("var x = `hello\n"
712             "  ${world}` >=\n"
713             "        some();",
714             format("var x =\n"
715                    "    `hello\n"
716                    "  ${world}` >= some();",
717                    getGoogleJSStyleWithColumns(21))); // Barely doesn't fit.
718   EXPECT_EQ("var x = `hello\n"
719             "  ${world}` >= some();",
720             format("var x =\n"
721                    "    `hello\n"
722                    "  ${world}` >= some();",
723                    getGoogleJSStyleWithColumns(22))); // Barely fits.
724 
725   verifyFormat("var x =\n    `h`;", getGoogleJSStyleWithColumns(13 - 1));
726   EXPECT_EQ(
727       "var x =\n    `multi\n  line`;",
728       format("var x = `multi\n  line`;", getGoogleJSStyleWithColumns(14 - 1)));
729 
730   // Make sure template strings get a proper ColumnWidth assigned, even if they
731   // are first token in line.
732   verifyFormat(
733       "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
734       "        `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;");
735 
736   // Two template strings.
737   verifyFormat("var x = `hello` == `hello`;");
738 
739   // Comments in template strings.
740   EXPECT_EQ("var x = `//a`;\n"
741             "var y;",
742             format("var x =\n `//a`;\n"
743                    "var y  ;"));
744   EXPECT_EQ("var x = `/*a`;\n"
745             "var y;",
746             format("var x =\n `/*a`;\n"
747                    "var y;"));
748   // Backticks in a comment - not a template string.
749   EXPECT_EQ("var x = 1  // `/*a`;\n"
750             "    ;",
751             format("var x =\n 1  // `/*a`;\n"
752                    "    ;"));
753   EXPECT_EQ("/* ` */ var x = 1; /* ` */",
754             format("/* ` */ var x\n= 1; /* ` */"));
755   // Comment spans multiple template strings.
756   EXPECT_EQ("var x = `/*a`;\n"
757             "var y = ` */ `;",
758             format("var x =\n `/*a`;\n"
759                    "var y =\n ` */ `;"));
760   // Escaped backtick.
761   EXPECT_EQ("var x = ` \\` a`;\n"
762             "var y;",
763             format("var x = ` \\` a`;\n"
764                    "var y;"));
765 }
766 
767 TEST_F(FormatTestJS, CastSyntax) {
768   verifyFormat("var x = <type>foo;");
769 }
770 
771 TEST_F(FormatTestJS, TypeArguments) {
772   verifyFormat("class X<Y> {}");
773   verifyFormat("new X<Y>();");
774   verifyFormat("foo<Y>(a);");
775   verifyFormat("var x: X<Y>[];");
776   verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
777   verifyFormat("function f(a: List<any> = null) {\n}");
778   verifyFormat("function f(): List<any> {\n}");
779 }
780 
781 TEST_F(FormatTestJS, OptionalTypes) {
782   verifyFormat("function x(a?: b, c?, d?) {\n}");
783   verifyFormat("class X {\n"
784                "  y?: z;\n"
785                "  z?;\n"
786                "}");
787   verifyFormat("interface X {\n"
788                "  y?(): z;\n"
789                "}");
790   verifyFormat("x ? 1 : 2;");
791   verifyFormat("constructor({aa}: {\n"
792                "  aa?: string,\n"
793                "  aaaaaaaa?: string,\n"
794                "  aaaaaaaaaaaaaaa?: boolean,\n"
795                "  aaaaaa?: List<string>\n"
796                "}) {\n"
797                "}");
798 }
799 
800 TEST_F(FormatTestJS, IndexSignature) {
801   verifyFormat("var x: {[k: string]: v};");
802 }
803 
804 } // end namespace tooling
805 } // end namespace clang
806