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