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