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