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