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 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 492 "};"); 493 verifyFormat( 494 "var a = a.aaaaaaa((a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n" 495 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 496 verifyFormat( 497 "var a = a.aaaaaaa((a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n" 498 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n" 499 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 500 501 // FIXME: This is bad, we should be wrapping before "() => {". 502 verifyFormat("someFunction(() => {\n" 503 " doSomething(); // break\n" 504 "})\n" 505 " .doSomethingElse(\n" 506 " // break\n" 507 " );"); 508 } 509 510 TEST_F(FormatTestJS, ReturnStatements) { 511 verifyFormat("function() {\n" 512 " return [hello, world];\n" 513 "}"); 514 } 515 516 TEST_F(FormatTestJS, ClosureStyleCasts) { 517 verifyFormat("var x = /** @type {foo} */ (bar);"); 518 } 519 520 TEST_F(FormatTestJS, TryCatch) { 521 verifyFormat("try {\n" 522 " f();\n" 523 "} catch (e) {\n" 524 " g();\n" 525 "} finally {\n" 526 " h();\n" 527 "}"); 528 529 // But, of course, "catch" is a perfectly fine function name in JavaScript. 530 verifyFormat("someObject.catch();"); 531 verifyFormat("someObject.new();"); 532 verifyFormat("someObject.delete();"); 533 } 534 535 TEST_F(FormatTestJS, StringLiteralConcatenation) { 536 verifyFormat("var literal = 'hello ' +\n" 537 " 'world';"); 538 } 539 540 TEST_F(FormatTestJS, RegexLiteralClassification) { 541 // Regex literals. 542 verifyFormat("var regex = /abc/;"); 543 verifyFormat("f(/abc/);"); 544 verifyFormat("f(abc, /abc/);"); 545 verifyFormat("some_map[/abc/];"); 546 verifyFormat("var x = a ? /abc/ : /abc/;"); 547 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 548 verifyFormat("var x = !/abc/.test(y);"); 549 verifyFormat("var x = a && /abc/.test(y);"); 550 verifyFormat("var x = a || /abc/.test(y);"); 551 verifyFormat("var x = a + /abc/.search(y);"); 552 verifyFormat("var regexs = {/abc/, /abc/};"); 553 verifyFormat("return /abc/;"); 554 555 // Not regex literals. 556 verifyFormat("var a = a / 2 + b / 3;"); 557 } 558 559 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 560 verifyFormat("var regex = /=/;"); 561 verifyFormat("var regex = /a*/;"); 562 verifyFormat("var regex = /a+/;"); 563 verifyFormat("var regex = /a?/;"); 564 verifyFormat("var regex = /.a./;"); 565 verifyFormat("var regex = /a\\*/;"); 566 verifyFormat("var regex = /^a$/;"); 567 verifyFormat("var regex = /\\/a/;"); 568 verifyFormat("var regex = /(?:x)/;"); 569 verifyFormat("var regex = /x(?=y)/;"); 570 verifyFormat("var regex = /x(?!y)/;"); 571 verifyFormat("var regex = /x|y/;"); 572 verifyFormat("var regex = /a{2}/;"); 573 verifyFormat("var regex = /a{1,3}/;"); 574 verifyFormat("var regex = /[abc]/;"); 575 verifyFormat("var regex = /[^abc]/;"); 576 verifyFormat("var regex = /[\\b]/;"); 577 verifyFormat("var regex = /\\b/;"); 578 verifyFormat("var regex = /\\B/;"); 579 verifyFormat("var regex = /\\d/;"); 580 verifyFormat("var regex = /\\D/;"); 581 verifyFormat("var regex = /\\f/;"); 582 verifyFormat("var regex = /\\n/;"); 583 verifyFormat("var regex = /\\r/;"); 584 verifyFormat("var regex = /\\s/;"); 585 verifyFormat("var regex = /\\S/;"); 586 verifyFormat("var regex = /\\t/;"); 587 verifyFormat("var regex = /\\v/;"); 588 verifyFormat("var regex = /\\w/;"); 589 verifyFormat("var regex = /\\W/;"); 590 verifyFormat("var regex = /a(a)\\1/;"); 591 verifyFormat("var regex = /\\0/;"); 592 verifyFormat("var regex = /\\\\/g;"); 593 verifyFormat("var regex = /\\a\\\\/g;"); 594 verifyFormat("var regex = /\a\\//g;"); 595 verifyFormat("var regex = /a\\//;\n" 596 "var x = 0;"); 597 EXPECT_EQ("var regex = /\\/*/;\n" 598 "var x = 0;", 599 format("var regex = /\\/*/;\n" 600 "var x=0;")); 601 } 602 603 TEST_F(FormatTestJS, RegexLiteralModifiers) { 604 verifyFormat("var regex = /abc/g;"); 605 verifyFormat("var regex = /abc/i;"); 606 verifyFormat("var regex = /abc/m;"); 607 verifyFormat("var regex = /abc/y;"); 608 } 609 610 TEST_F(FormatTestJS, RegexLiteralLength) { 611 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 612 getGoogleJSStyleWithColumns(60)); 613 verifyFormat("var regex =\n" 614 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 615 getGoogleJSStyleWithColumns(60)); 616 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 617 getGoogleJSStyleWithColumns(50)); 618 } 619 620 TEST_F(FormatTestJS, RegexLiteralExamples) { 621 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 622 } 623 624 TEST_F(FormatTestJS, TypeAnnotations) { 625 verifyFormat("var x: string;"); 626 verifyFormat("function x(): string {\n return 'x';\n}"); 627 verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}"); 628 verifyFormat("function x(y: string): string {\n return 'x';\n}"); 629 verifyFormat("for (var y: string in x) {\n x();\n}"); 630 verifyFormat("((a: string, b: number): string => a + b);"); 631 verifyFormat("var x: (y: number) => string;"); 632 verifyFormat("var x: P<string, (a: number) => string>;"); 633 verifyFormat("var x = {y: function(): z { return 1; }};"); 634 verifyFormat("var x = {y: function(): {a: number} { return 1; }};"); 635 } 636 637 TEST_F(FormatTestJS, ClassDeclarations) { 638 verifyFormat("class C {\n x: string = 12;\n}"); 639 verifyFormat("class C {\n x(): string => 12;\n}"); 640 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); 641 verifyFormat("class C {\n private x: string = 12;\n}"); 642 verifyFormat("class C {\n private static x: string = 12;\n}"); 643 verifyFormat("class C {\n static x(): string { return 'asd'; }\n}"); 644 verifyFormat("class C extends P implements I {}"); 645 verifyFormat("class C extends p.P implements i.I {}"); 646 647 // ':' is not a type declaration here. 648 verifyFormat("class X {\n" 649 " subs = {\n" 650 " 'b': {\n" 651 " 'c': 1,\n" 652 " },\n" 653 " };\n" 654 "}"); 655 } 656 657 TEST_F(FormatTestJS, InterfaceDeclarations) { 658 verifyFormat("interface I {\n" 659 " x: string;\n" 660 "}"); 661 } 662 663 TEST_F(FormatTestJS, MetadataAnnotations) { 664 verifyFormat("@A\nclass C {\n}"); 665 verifyFormat("@A({arg: 'value'})\nclass C {\n}"); 666 verifyFormat("@A\n@B\nclass C {\n}"); 667 verifyFormat("class C {\n @A x: string;\n}"); 668 verifyFormat("class C {\n" 669 " @A\n" 670 " private x(): string {\n" 671 " return 'y';\n" 672 " }\n" 673 "}"); 674 verifyFormat("class X {}\n" 675 "class Y {}"); 676 } 677 678 TEST_F(FormatTestJS, Modules) { 679 verifyFormat("import SomeThing from 'some/module.js';"); 680 verifyFormat("import {X, Y} from 'some/module.js';"); 681 verifyFormat("import {\n" 682 " VeryLongImportsAreAnnoying,\n" 683 " VeryLongImportsAreAnnoying,\n" 684 " VeryLongImportsAreAnnoying,\n" 685 " VeryLongImportsAreAnnoying\n" 686 "} from 'some/module.js';"); 687 verifyFormat("import {\n" 688 " X,\n" 689 " Y,\n" 690 "} from 'some/module.js';"); 691 verifyFormat("import {\n" 692 " X,\n" 693 " Y,\n" 694 "} from 'some/long/module.js';", 695 getGoogleJSStyleWithColumns(20)); 696 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); 697 verifyFormat("import * as lib from 'some/module.js';"); 698 verifyFormat("var x = {import: 1};\nx.import = 2;"); 699 700 verifyFormat("export function fn() {\n" 701 " return 'fn';\n" 702 "}"); 703 verifyFormat("export function A() {\n" 704 "}\n" 705 "export default function B() {\n" 706 "}\n" 707 "export function C() {\n" 708 "}"); 709 verifyFormat("export const x = 12;"); 710 verifyFormat("export default class X {}"); 711 verifyFormat("export {X, Y} from 'some/module.js';"); 712 verifyFormat("export {\n" 713 " X,\n" 714 " Y,\n" 715 "} from 'some/module.js';"); 716 verifyFormat("export class C {\n" 717 " x: number;\n" 718 " y: string;\n" 719 "}"); 720 verifyFormat("export class X { y: number; }"); 721 verifyFormat("export default class X { y: number }"); 722 verifyFormat("export default function() {\n return 1;\n}"); 723 verifyFormat("export var x = 12;"); 724 verifyFormat("export var x: number = 12;"); 725 verifyFormat("export const y = {\n" 726 " a: 1,\n" 727 " b: 2\n" 728 "};"); 729 } 730 731 TEST_F(FormatTestJS, TemplateStrings) { 732 // Keeps any whitespace/indentation within the template string. 733 EXPECT_EQ("var x = `hello\n" 734 " ${ name }\n" 735 " !`;", 736 format("var x = `hello\n" 737 " ${ name }\n" 738 " !`;")); 739 740 // FIXME: +1 / -1 offsets are to work around clang-format miscalculating 741 // widths for unknown tokens that are not whitespace (e.g. '`'). Remove when 742 // the code is corrected. 743 744 verifyFormat("var x =\n" 745 " `hello ${world}` >= some();", 746 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. 747 verifyFormat("var x = `hello ${world}` >= some();", 748 getGoogleJSStyleWithColumns(35 + 1)); // Barely fits. 749 EXPECT_EQ("var x = `hello\n" 750 " ${world}` >=\n" 751 " some();", 752 format("var x =\n" 753 " `hello\n" 754 " ${world}` >= some();", 755 getGoogleJSStyleWithColumns(21))); // Barely doesn't fit. 756 EXPECT_EQ("var x = `hello\n" 757 " ${world}` >= some();", 758 format("var x =\n" 759 " `hello\n" 760 " ${world}` >= some();", 761 getGoogleJSStyleWithColumns(22))); // Barely fits. 762 763 verifyFormat("var x =\n `h`;", getGoogleJSStyleWithColumns(13 - 1)); 764 EXPECT_EQ( 765 "var x =\n `multi\n line`;", 766 format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(14 - 1))); 767 768 // Make sure template strings get a proper ColumnWidth assigned, even if they 769 // are first token in line. 770 verifyFormat( 771 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 772 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); 773 774 // Two template strings. 775 verifyFormat("var x = `hello` == `hello`;"); 776 777 // Comments in template strings. 778 EXPECT_EQ("var x = `//a`;\n" 779 "var y;", 780 format("var x =\n `//a`;\n" 781 "var y ;")); 782 EXPECT_EQ("var x = `/*a`;\n" 783 "var y;", 784 format("var x =\n `/*a`;\n" 785 "var y;")); 786 // Backticks in a comment - not a template string. 787 EXPECT_EQ("var x = 1 // `/*a`;\n" 788 " ;", 789 format("var x =\n 1 // `/*a`;\n" 790 " ;")); 791 EXPECT_EQ("/* ` */ var x = 1; /* ` */", 792 format("/* ` */ var x\n= 1; /* ` */")); 793 // Comment spans multiple template strings. 794 EXPECT_EQ("var x = `/*a`;\n" 795 "var y = ` */ `;", 796 format("var x =\n `/*a`;\n" 797 "var y =\n ` */ `;")); 798 // Escaped backtick. 799 EXPECT_EQ("var x = ` \\` a`;\n" 800 "var y;", 801 format("var x = ` \\` a`;\n" 802 "var y;")); 803 } 804 805 TEST_F(FormatTestJS, CastSyntax) { 806 verifyFormat("var x = <type>foo;"); 807 } 808 809 TEST_F(FormatTestJS, TypeArguments) { 810 verifyFormat("class X<Y> {}"); 811 verifyFormat("new X<Y>();"); 812 verifyFormat("foo<Y>(a);"); 813 verifyFormat("var x: X<Y>[];"); 814 verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); 815 verifyFormat("function f(a: List<any> = null) {\n}"); 816 verifyFormat("function f(): List<any> {\n}"); 817 } 818 819 TEST_F(FormatTestJS, OptionalTypes) { 820 verifyFormat("function x(a?: b, c?, d?) {\n}"); 821 verifyFormat("class X {\n" 822 " y?: z;\n" 823 " z?;\n" 824 "}"); 825 verifyFormat("interface X {\n" 826 " y?(): z;\n" 827 "}"); 828 verifyFormat("x ? 1 : 2;"); 829 verifyFormat("constructor({aa}: {\n" 830 " aa?: string,\n" 831 " aaaaaaaa?: string,\n" 832 " aaaaaaaaaaaaaaa?: boolean,\n" 833 " aaaaaa?: List<string>\n" 834 "}) {\n" 835 "}"); 836 } 837 838 TEST_F(FormatTestJS, IndexSignature) { 839 verifyFormat("var x: {[k: string]: v};"); 840 } 841 842 } // end namespace tooling 843 } // end namespace clang 844