• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
format(llvm::StringRef Code,unsigned Offset,unsigned Length,const FormatStyle & Style)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 
format(llvm::StringRef Code,const FormatStyle & Style=getGoogleStyle (FormatStyle::LK_JavaScript))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 
getGoogleJSStyleWithColumns(unsigned ColumnLimit)40   static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
41     FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
42     Style.ColumnLimit = ColumnLimit;
43     return Style;
44   }
45 
verifyFormat(llvm::StringRef Code,const FormatStyle & Style=getGoogleStyle (FormatStyle::LK_JavaScript))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 
TEST_F(FormatTestJS,UnderstandsJavaScriptOperators)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 
TEST_F(FormatTestJS,UnderstandsAmpAmp)87 TEST_F(FormatTestJS, UnderstandsAmpAmp) {
88   verifyFormat("e && e.SomeFunction();");
89 }
90 
TEST_F(FormatTestJS,LiteralOperatorsCanBeKeywords)91 TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
92   verifyFormat("not.and.or.not_eq = 1;");
93 }
94 
TEST_F(FormatTestJS,ES6DestructuringAssignment)95 TEST_F(FormatTestJS, ES6DestructuringAssignment) {
96   verifyFormat("var [a, b, c] = [1, 2, 3];");
97   verifyFormat("var {a, b} = {a: 1, b: 2};");
98 }
99 
TEST_F(FormatTestJS,ContainerLiterals)100 TEST_F(FormatTestJS, ContainerLiterals) {
101   verifyFormat("return {\n"
102                "  link: function() {\n"
103                "    f();  //\n"
104                "  }\n"
105                "};");
106   verifyFormat("return {\n"
107                "  a: a,\n"
108                "  link: function() {\n"
109                "    f();  //\n"
110                "  }\n"
111                "};");
112   verifyFormat("return {\n"
113                "  a: a,\n"
114                "  link: function() {\n"
115                "    f();  //\n"
116                "  },\n"
117                "  link: function() {\n"
118                "    f();  //\n"
119                "  }\n"
120                "};");
121   verifyFormat("var stuff = {\n"
122                "  // comment for update\n"
123                "  update: false,\n"
124                "  // comment for modules\n"
125                "  modules: false,\n"
126                "  // comment for tasks\n"
127                "  tasks: false\n"
128                "};");
129   verifyFormat("return {\n"
130                "  'finish':\n"
131                "      //\n"
132                "      a\n"
133                "};");
134   verifyFormat("var obj = {\n"
135                "  fooooooooo: function(x) {\n"
136                "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
137                "  }\n"
138                "};");
139   // Simple object literal, as opposed to enum style below.
140   verifyFormat("var obj = {a: 123};");
141   // Enum style top level assignment.
142   verifyFormat("X = {\n  a: 123\n};");
143   verifyFormat("X.Y = {\n  a: 123\n};");
144   verifyFormat("x = foo && {a: 123};");
145 }
146 
TEST_F(FormatTestJS,MethodsInObjectLiterals)147 TEST_F(FormatTestJS, MethodsInObjectLiterals) {
148   verifyFormat("var o = {\n"
149                "  value: 'test',\n"
150                "  get value() {  // getter\n"
151                "    return this.value;\n"
152                "  }\n"
153                "};");
154   verifyFormat("var o = {\n"
155                "  value: 'test',\n"
156                "  set value(val) {  // setter\n"
157                "    this.value = val;\n"
158                "  }\n"
159                "};");
160   verifyFormat("var o = {\n"
161                "  value: 'test',\n"
162                "  someMethod(val) {  // method\n"
163                "    doSomething(this.value + val);\n"
164                "  }\n"
165                "};");
166   verifyFormat("var o = {\n"
167                "  someMethod(val) {  // method\n"
168                "    doSomething(this.value + val);\n"
169                "  },\n"
170                "  someOtherMethod(val) {  // method\n"
171                "    doSomething(this.value + val);\n"
172                "  }\n"
173                "};");
174 }
175 
TEST_F(FormatTestJS,SpacesInContainerLiterals)176 TEST_F(FormatTestJS, SpacesInContainerLiterals) {
177   verifyFormat("var arr = [1, 2, 3];");
178   verifyFormat("f({a: 1, b: 2, c: 3});");
179 
180   verifyFormat("var object_literal_with_long_name = {\n"
181                "  a: 'aaaaaaaaaaaaaaaaaa',\n"
182                "  b: 'bbbbbbbbbbbbbbbbbb'\n"
183                "};");
184 
185   verifyFormat("f({a: 1, b: 2, c: 3});",
186                getChromiumStyle(FormatStyle::LK_JavaScript));
187   verifyFormat("f({'a': [{}]});");
188 }
189 
TEST_F(FormatTestJS,SingleQuoteStrings)190 TEST_F(FormatTestJS, SingleQuoteStrings) {
191   verifyFormat("this.function('', true);");
192 }
193 
TEST_F(FormatTestJS,GoogScopes)194 TEST_F(FormatTestJS, GoogScopes) {
195   verifyFormat("goog.scope(function() {\n"
196                "var x = a.b;\n"
197                "var y = c.d;\n"
198                "});  // goog.scope");
199   verifyFormat("goog.scope(function() {\n"
200                "// test\n"
201                "var x = 0;\n"
202                "// test\n"
203                "});");
204 }
205 
TEST_F(FormatTestJS,GoogModules)206 TEST_F(FormatTestJS, GoogModules) {
207   verifyFormat("goog.module('this.is.really.absurdly.long');",
208                getGoogleJSStyleWithColumns(40));
209   verifyFormat("goog.require('this.is.really.absurdly.long');",
210                getGoogleJSStyleWithColumns(40));
211   verifyFormat("goog.provide('this.is.really.absurdly.long');",
212                getGoogleJSStyleWithColumns(40));
213   verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
214                getGoogleJSStyleWithColumns(40));
215 
216   // These should be wrapped normally.
217   verifyFormat(
218       "var MyLongClassName =\n"
219       "    goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
220 }
221 
TEST_F(FormatTestJS,FormatsFreestandingFunctions)222 TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
223   verifyFormat("function outer1(a, b) {\n"
224                "  function inner1(a, b) { return a; }\n"
225                "  inner1(a, b);\n"
226                "}\n"
227                "function outer2(a, b) {\n"
228                "  function inner2(a, b) { return a; }\n"
229                "  inner2(a, b);\n"
230                "}");
231 }
232 
TEST_F(FormatTestJS,FunctionLiterals)233 TEST_F(FormatTestJS, FunctionLiterals) {
234   verifyFormat("doFoo(function() {});");
235   verifyFormat("doFoo(function() { return 1; });");
236   verifyFormat("var func = function() {\n"
237                "  return 1;\n"
238                "};");
239   verifyFormat("return {\n"
240                "  body: {\n"
241                "    setAttribute: function(key, val) { this[key] = val; },\n"
242                "    getAttribute: function(key) { return this[key]; },\n"
243                "    style: {direction: ''}\n"
244                "  }\n"
245                "};");
246   EXPECT_EQ("abc = xyz ?\n"
247             "          function() {\n"
248             "            return 1;\n"
249             "          } :\n"
250             "          function() {\n"
251             "            return -1;\n"
252             "          };",
253             format("abc=xyz?function(){return 1;}:function(){return -1;};"));
254 
255   verifyFormat("var closure = goog.bind(\n"
256                "    function() {  // comment\n"
257                "      foo();\n"
258                "      bar();\n"
259                "    },\n"
260                "    this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
261                "    arg3IsReallyLongAndNeeedsLineBreaks);");
262   verifyFormat("var closure = goog.bind(function() {  // comment\n"
263                "  foo();\n"
264                "  bar();\n"
265                "}, this);");
266   verifyFormat("return {\n"
267                "  a: 'E',\n"
268                "  b: function() {\n"
269                "    return function() {\n"
270                "      f();  //\n"
271                "    };\n"
272                "  }\n"
273                "};");
274   verifyFormat("{\n"
275                "  var someVariable = function(x) {\n"
276                "    return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
277                "  };\n"
278                "}");
279 
280   verifyFormat("f({a: function() { return 1; }});",
281                getGoogleJSStyleWithColumns(33));
282   verifyFormat("f({\n"
283                "  a: function() { return 1; }\n"
284                "});",
285                getGoogleJSStyleWithColumns(32));
286 
287   verifyFormat("return {\n"
288                "  a: function SomeFunction() {\n"
289                "    // ...\n"
290                "    return 1;\n"
291                "  }\n"
292                "};");
293   verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
294                "    .then(goog.bind(function(aaaaaaaaaaa) {\n"
295                "      someFunction();\n"
296                "      someFunction();\n"
297                "    }, this), aaaaaaaaaaaaaaaaa);");
298 
299   // FIXME: This is not ideal yet.
300   verifyFormat("someFunction(goog.bind(\n"
301                "                 function() {\n"
302                "                   doSomething();\n"
303                "                   doSomething();\n"
304                "                 },\n"
305                "                 this),\n"
306                "             goog.bind(function() {\n"
307                "               doSomething();\n"
308                "               doSomething();\n"
309                "             }, this));");
310 }
311 
TEST_F(FormatTestJS,InliningFunctionLiterals)312 TEST_F(FormatTestJS, InliningFunctionLiterals) {
313   FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
314   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
315   verifyFormat("var func = function() {\n"
316                "  return 1;\n"
317                "};",
318                Style);
319   verifyFormat("var func = doSomething(function() { return 1; });", Style);
320   verifyFormat("var outer = function() {\n"
321                "  var inner = function() { return 1; }\n"
322                "};",
323                Style);
324   verifyFormat("function outer1(a, b) {\n"
325                "  function inner1(a, b) { return a; }\n"
326                "}",
327                Style);
328 
329   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
330   verifyFormat("var func = function() { return 1; };", Style);
331   verifyFormat("var func = doSomething(function() { return 1; });", Style);
332   verifyFormat(
333       "var outer = function() { var inner = function() { return 1; } };",
334       Style);
335   verifyFormat("function outer1(a, b) {\n"
336                "  function inner1(a, b) { return a; }\n"
337                "}",
338                Style);
339 
340   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
341   verifyFormat("var func = function() {\n"
342                "  return 1;\n"
343                "};",
344                Style);
345   verifyFormat("var func = doSomething(function() {\n"
346                "  return 1;\n"
347                "});",
348                Style);
349   verifyFormat("var outer = function() {\n"
350                "  var inner = function() {\n"
351                "    return 1;\n"
352                "  }\n"
353                "};",
354                Style);
355   verifyFormat("function outer1(a, b) {\n"
356                "  function inner1(a, b) {\n"
357                "    return a;\n"
358                "  }\n"
359                "}",
360                Style);
361 }
362 
TEST_F(FormatTestJS,MultipleFunctionLiterals)363 TEST_F(FormatTestJS, MultipleFunctionLiterals) {
364   verifyFormat("promise.then(\n"
365                "    function success() {\n"
366                "      doFoo();\n"
367                "      doBar();\n"
368                "    },\n"
369                "    function error() {\n"
370                "      doFoo();\n"
371                "      doBaz();\n"
372                "    },\n"
373                "    []);\n");
374   verifyFormat("promise.then(\n"
375                "    function success() {\n"
376                "      doFoo();\n"
377                "      doBar();\n"
378                "    },\n"
379                "    [],\n"
380                "    function error() {\n"
381                "      doFoo();\n"
382                "      doBaz();\n"
383                "    });\n");
384   // FIXME: Here, we should probably break right after the "(" for consistency.
385   verifyFormat("promise.then([],\n"
386                "             function success() {\n"
387                "               doFoo();\n"
388                "               doBar();\n"
389                "             },\n"
390                "             function error() {\n"
391                "               doFoo();\n"
392                "               doBaz();\n"
393                "             });\n");
394 
395   verifyFormat("getSomeLongPromise()\n"
396                "    .then(function(value) { body(); })\n"
397                "    .thenCatch(function(error) {\n"
398                "      body();\n"
399                "      body();\n"
400                "    });");
401   verifyFormat("getSomeLongPromise()\n"
402                "    .then(function(value) {\n"
403                "      body();\n"
404                "      body();\n"
405                "    })\n"
406                "    .thenCatch(function(error) {\n"
407                "      body();\n"
408                "      body();\n"
409                "    });");
410 
411   verifyFormat("getSomeLongPromise()\n"
412                "    .then(function(value) { body(); })\n"
413                "    .thenCatch(function(error) { body(); });");
414 }
415 
TEST_F(FormatTestJS,ReturnStatements)416 TEST_F(FormatTestJS, ReturnStatements) {
417   verifyFormat("function() {\n"
418                "  return [hello, world];\n"
419                "}");
420 }
421 
TEST_F(FormatTestJS,ClosureStyleComments)422 TEST_F(FormatTestJS, ClosureStyleComments) {
423   verifyFormat("var x = /** @type {foo} */ (bar);");
424 }
425 
TEST_F(FormatTestJS,TryCatch)426 TEST_F(FormatTestJS, TryCatch) {
427   verifyFormat("try {\n"
428                "  f();\n"
429                "} catch (e) {\n"
430                "  g();\n"
431                "} finally {\n"
432                "  h();\n"
433                "}");
434 
435   // But, of course, "catch" is a perfectly fine function name in JavaScript.
436   verifyFormat("someObject.catch();");
437   verifyFormat("someObject.new();");
438   verifyFormat("someObject.delete();");
439 }
440 
TEST_F(FormatTestJS,StringLiteralConcatenation)441 TEST_F(FormatTestJS, StringLiteralConcatenation) {
442   verifyFormat("var literal = 'hello ' +\n"
443                "              'world';");
444 }
445 
TEST_F(FormatTestJS,RegexLiteralClassification)446 TEST_F(FormatTestJS, RegexLiteralClassification) {
447   // Regex literals.
448   verifyFormat("var regex = /abc/;");
449   verifyFormat("f(/abc/);");
450   verifyFormat("f(abc, /abc/);");
451   verifyFormat("some_map[/abc/];");
452   verifyFormat("var x = a ? /abc/ : /abc/;");
453   verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
454   verifyFormat("var x = !/abc/.test(y);");
455   verifyFormat("var x = a && /abc/.test(y);");
456   verifyFormat("var x = a || /abc/.test(y);");
457   verifyFormat("var x = a + /abc/.search(y);");
458   verifyFormat("var regexs = {/abc/, /abc/};");
459   verifyFormat("return /abc/;");
460 
461   // Not regex literals.
462   verifyFormat("var a = a / 2 + b / 3;");
463 }
464 
TEST_F(FormatTestJS,RegexLiteralSpecialCharacters)465 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
466   verifyFormat("var regex = /a*/;");
467   verifyFormat("var regex = /a+/;");
468   verifyFormat("var regex = /a?/;");
469   verifyFormat("var regex = /.a./;");
470   verifyFormat("var regex = /a\\*/;");
471   verifyFormat("var regex = /^a$/;");
472   verifyFormat("var regex = /\\/a/;");
473   verifyFormat("var regex = /(?:x)/;");
474   verifyFormat("var regex = /x(?=y)/;");
475   verifyFormat("var regex = /x(?!y)/;");
476   verifyFormat("var regex = /x|y/;");
477   verifyFormat("var regex = /a{2}/;");
478   verifyFormat("var regex = /a{1,3}/;");
479   verifyFormat("var regex = /[abc]/;");
480   verifyFormat("var regex = /[^abc]/;");
481   verifyFormat("var regex = /[\\b]/;");
482   verifyFormat("var regex = /\\b/;");
483   verifyFormat("var regex = /\\B/;");
484   verifyFormat("var regex = /\\d/;");
485   verifyFormat("var regex = /\\D/;");
486   verifyFormat("var regex = /\\f/;");
487   verifyFormat("var regex = /\\n/;");
488   verifyFormat("var regex = /\\r/;");
489   verifyFormat("var regex = /\\s/;");
490   verifyFormat("var regex = /\\S/;");
491   verifyFormat("var regex = /\\t/;");
492   verifyFormat("var regex = /\\v/;");
493   verifyFormat("var regex = /\\w/;");
494   verifyFormat("var regex = /\\W/;");
495   verifyFormat("var regex = /a(a)\\1/;");
496   verifyFormat("var regex = /\\0/;");
497   verifyFormat("var regex = /\\\\/g;");
498   verifyFormat("var regex = /\\a\\\\/g;");
499   verifyFormat("var regex = /\a\\//g;");
500   verifyFormat("var regex = /a\\//;\n"
501                "var x = 0;");
502   EXPECT_EQ("var regex = /\\/*/;\n"
503             "var x = 0;",
504             format("var regex = /\\/*/;\n"
505                    "var x=0;"));
506 }
507 
TEST_F(FormatTestJS,RegexLiteralModifiers)508 TEST_F(FormatTestJS, RegexLiteralModifiers) {
509   verifyFormat("var regex = /abc/g;");
510   verifyFormat("var regex = /abc/i;");
511   verifyFormat("var regex = /abc/m;");
512   verifyFormat("var regex = /abc/y;");
513 }
514 
TEST_F(FormatTestJS,RegexLiteralLength)515 TEST_F(FormatTestJS, RegexLiteralLength) {
516   verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
517                getGoogleJSStyleWithColumns(60));
518   verifyFormat("var regex =\n"
519                "    /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
520                getGoogleJSStyleWithColumns(60));
521   verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
522                getGoogleJSStyleWithColumns(50));
523 }
524 
TEST_F(FormatTestJS,RegexLiteralExamples)525 TEST_F(FormatTestJS, RegexLiteralExamples) {
526   verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
527 }
528 
TEST_F(FormatTestJS,TypeAnnotations)529 TEST_F(FormatTestJS, TypeAnnotations) {
530   verifyFormat("var x: string;");
531   verifyFormat("function x(): string {\n  return 'x';\n}");
532   verifyFormat("function x(y: string): string {\n  return 'x';\n}");
533   verifyFormat("for (var y: string in x) {\n  x();\n}");
534   verifyFormat("((a: string, b: number): string => a + b);");
535   verifyFormat("var x: (y: number) => string;");
536   verifyFormat("var x: P<string, (a: number) => string>;");
537 }
538 
TEST_F(FormatTestJS,ClassDeclarations)539 TEST_F(FormatTestJS, ClassDeclarations) {
540   verifyFormat("class C {\n  x: string = 12;\n}");
541   verifyFormat("class C {\n  x(): string => 12;\n}");
542   verifyFormat("class C {\n  ['x' + 2]: string = 12;\n}");
543   verifyFormat("class C {\n  private x: string = 12;\n}");
544   verifyFormat("class C {\n  private static x: string = 12;\n}");
545   verifyFormat("class C {\n  static x(): string { return 'asd'; }\n}");
546   verifyFormat("class C extends P implements I {}");
547   verifyFormat("class C extends p.P implements i.I {}");
548 }
549 
TEST_F(FormatTestJS,MetadataAnnotations)550 TEST_F(FormatTestJS, MetadataAnnotations) {
551   verifyFormat("@A\nclass C {\n}");
552   verifyFormat("@A({arg: 'value'})\nclass C {\n}");
553   verifyFormat("@A\n@B\nclass C {\n}");
554   verifyFormat("class C {\n  @A x: string;\n}");
555   verifyFormat("class C {\n"
556                "  @A\n"
557                "  private x(): string {\n"
558                "    return 'y';\n"
559                "  }\n"
560                "}");
561   verifyFormat("class X {}\n"
562                "class Y {}");
563 }
564 
TEST_F(FormatTestJS,Modules)565 TEST_F(FormatTestJS, Modules) {
566   verifyFormat("import SomeThing from 'some/module.js';");
567   verifyFormat("import {X, Y} from 'some/module.js';");
568   verifyFormat("import {\n"
569                "  VeryLongImportsAreAnnoying,\n"
570                "  VeryLongImportsAreAnnoying,\n"
571                "  VeryLongImportsAreAnnoying,\n"
572                "  VeryLongImportsAreAnnoying\n"
573                "} from 'some/module.js';");
574   verifyFormat("import {\n"
575                "  X,\n"
576                "  Y,\n"
577                "} from 'some/module.js';");
578   verifyFormat("import {\n"
579                "  X,\n"
580                "  Y,\n"
581                "} from 'some/long/module.js';",
582                getGoogleJSStyleWithColumns(20));
583   verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
584   verifyFormat("import * as lib from 'some/module.js';");
585   verifyFormat("var x = {import: 1};\nx.import = 2;");
586 
587   verifyFormat("export function fn() {\n"
588                "  return 'fn';\n"
589                "}");
590   verifyFormat("export const x = 12;");
591   verifyFormat("export default class X {}");
592   verifyFormat("export {X, Y} from 'some/module.js';");
593   verifyFormat("export {\n"
594                "  X,\n"
595                "  Y,\n"
596                "} from 'some/module.js';");
597   verifyFormat("export class C {\n"
598                "  x: number;\n"
599                "  y: string;\n"
600                "}");
601   verifyFormat("export class X { y: number; }");
602   verifyFormat("export default class X { y: number }");
603   verifyFormat("export default function() {\n  return 1;\n}");
604   verifyFormat("export var x = 12;");
605   verifyFormat("export var x: number = 12;");
606   verifyFormat("export const y = {\n"
607                "  a: 1,\n"
608                "  b: 2\n"
609                "};");
610 }
611 
TEST_F(FormatTestJS,TemplateStrings)612 TEST_F(FormatTestJS, TemplateStrings) {
613   // Keeps any whitespace/indentation within the template string.
614   EXPECT_EQ("var x = `hello\n"
615             "     ${  name    }\n"
616             "  !`;",
617             format("var x    =    `hello\n"
618                    "     ${  name    }\n"
619                    "  !`;"));
620 
621   // FIXME: +1 / -1 offsets are to work around clang-format miscalculating
622   // widths for unknown tokens that are not whitespace (e.g. '`'). Remove when
623   // the code is corrected.
624 
625   verifyFormat("var x =\n"
626                "    `hello ${world}` >= some();",
627                getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
628   verifyFormat("var x = `hello ${world}` >= some();",
629                getGoogleJSStyleWithColumns(35 + 1)); // Barely fits.
630   EXPECT_EQ("var x = `hello\n"
631             "  ${world}` >=\n"
632             "        some();",
633             format("var x =\n"
634                    "    `hello\n"
635                    "  ${world}` >= some();",
636                    getGoogleJSStyleWithColumns(21))); // Barely doesn't fit.
637   EXPECT_EQ("var x = `hello\n"
638             "  ${world}` >= some();",
639             format("var x =\n"
640                    "    `hello\n"
641                    "  ${world}` >= some();",
642                    getGoogleJSStyleWithColumns(22))); // Barely fits.
643 
644   verifyFormat("var x =\n    `h`;", getGoogleJSStyleWithColumns(13 - 1));
645   EXPECT_EQ(
646       "var x =\n    `multi\n  line`;",
647       format("var x = `multi\n  line`;", getGoogleJSStyleWithColumns(14 - 1)));
648 
649   // Two template strings.
650   verifyFormat("var x = `hello` == `hello`;");
651 
652   // Comments in template strings.
653   EXPECT_EQ("var x = `//a`;\n"
654             "var y;",
655             format("var x =\n `//a`;\n"
656                    "var y  ;"));
657   EXPECT_EQ("var x = `/*a`;\n"
658             "var y;",
659             format("var x =\n `/*a`;\n"
660                    "var y;"));
661   // Backticks in a comment - not a template string.
662   EXPECT_EQ("var x = 1  // `/*a`;\n"
663             "    ;",
664             format("var x =\n 1  // `/*a`;\n"
665                    "    ;"));
666   EXPECT_EQ("/* ` */ var x = 1; /* ` */",
667             format("/* ` */ var x\n= 1; /* ` */"));
668   // Comment spans multiple template strings.
669   EXPECT_EQ("var x = `/*a`;\n"
670             "var y = ` */ `;",
671             format("var x =\n `/*a`;\n"
672                    "var y =\n ` */ `;"));
673   // Escaped backtick.
674   EXPECT_EQ("var x = ` \\` a`;\n"
675             "var y;",
676             format("var x = ` \\` a`;\n"
677                    "var y;"));
678 }
679 
TEST_F(FormatTestJS,CastSyntax)680 TEST_F(FormatTestJS, CastSyntax) {
681   verifyFormat("var x = <type>foo;");
682 }
683 
TEST_F(FormatTestJS,TypeArguments)684 TEST_F(FormatTestJS, TypeArguments) {
685   verifyFormat("class X<Y> {}");
686   verifyFormat("new X<Y>();");
687   verifyFormat("foo<Y>(a);");
688   verifyFormat("var x: X<Y>[];");
689   verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
690 }
691 
TEST_F(FormatTestJS,OptionalTypes)692 TEST_F(FormatTestJS, OptionalTypes) {
693   verifyFormat("function x(a?: b, c?, d?) {\n}");
694   verifyFormat("class X {\n"
695                "  y?: z;\n"
696                "  z?;\n"
697                "}");
698 }
699 
TEST_F(FormatTestJS,IndexSignature)700 TEST_F(FormatTestJS, IndexSignature) {
701   verifyFormat("var x: {[k: string]: v};");
702 }
703 
704 } // end namespace tooling
705 } // end namespace clang
706