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 // ':' is not a type declaration here. 636 verifyFormat("class X {\n" 637 " subs = {\n" 638 " 'b': {\n" 639 " 'c': 1,\n" 640 " },\n" 641 " };\n" 642 "}"); 643 } 644 645 TEST_F(FormatTestJS, InterfaceDeclarations) { 646 verifyFormat("interface I {\n" 647 " x: string;\n" 648 "}"); 649 } 650 651 TEST_F(FormatTestJS, MetadataAnnotations) { 652 verifyFormat("@A\nclass C {\n}"); 653 verifyFormat("@A({arg: 'value'})\nclass C {\n}"); 654 verifyFormat("@A\n@B\nclass C {\n}"); 655 verifyFormat("class C {\n @A x: string;\n}"); 656 verifyFormat("class C {\n" 657 " @A\n" 658 " private x(): string {\n" 659 " return 'y';\n" 660 " }\n" 661 "}"); 662 verifyFormat("class X {}\n" 663 "class Y {}"); 664 } 665 666 TEST_F(FormatTestJS, Modules) { 667 verifyFormat("import SomeThing from 'some/module.js';"); 668 verifyFormat("import {X, Y} from 'some/module.js';"); 669 verifyFormat("import {\n" 670 " VeryLongImportsAreAnnoying,\n" 671 " VeryLongImportsAreAnnoying,\n" 672 " VeryLongImportsAreAnnoying,\n" 673 " VeryLongImportsAreAnnoying\n" 674 "} from 'some/module.js';"); 675 verifyFormat("import {\n" 676 " X,\n" 677 " Y,\n" 678 "} from 'some/module.js';"); 679 verifyFormat("import {\n" 680 " X,\n" 681 " Y,\n" 682 "} from 'some/long/module.js';", 683 getGoogleJSStyleWithColumns(20)); 684 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); 685 verifyFormat("import * as lib from 'some/module.js';"); 686 verifyFormat("var x = {import: 1};\nx.import = 2;"); 687 688 verifyFormat("export function fn() {\n" 689 " return 'fn';\n" 690 "}"); 691 verifyFormat("export function A() {\n" 692 "}\n" 693 "export default function B() {\n" 694 "}\n" 695 "export function C() {\n" 696 "}"); 697 verifyFormat("export const x = 12;"); 698 verifyFormat("export default class X {}"); 699 verifyFormat("export {X, Y} from 'some/module.js';"); 700 verifyFormat("export {\n" 701 " X,\n" 702 " Y,\n" 703 "} from 'some/module.js';"); 704 verifyFormat("export class C {\n" 705 " x: number;\n" 706 " y: string;\n" 707 "}"); 708 verifyFormat("export class X { y: number; }"); 709 verifyFormat("export default class X { y: number }"); 710 verifyFormat("export default function() {\n return 1;\n}"); 711 verifyFormat("export var x = 12;"); 712 verifyFormat("export var x: number = 12;"); 713 verifyFormat("export const y = {\n" 714 " a: 1,\n" 715 " b: 2\n" 716 "};"); 717 } 718 719 TEST_F(FormatTestJS, TemplateStrings) { 720 // Keeps any whitespace/indentation within the template string. 721 EXPECT_EQ("var x = `hello\n" 722 " ${ name }\n" 723 " !`;", 724 format("var x = `hello\n" 725 " ${ name }\n" 726 " !`;")); 727 728 // FIXME: +1 / -1 offsets are to work around clang-format miscalculating 729 // widths for unknown tokens that are not whitespace (e.g. '`'). Remove when 730 // the code is corrected. 731 732 verifyFormat("var x =\n" 733 " `hello ${world}` >= some();", 734 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. 735 verifyFormat("var x = `hello ${world}` >= some();", 736 getGoogleJSStyleWithColumns(35 + 1)); // Barely fits. 737 EXPECT_EQ("var x = `hello\n" 738 " ${world}` >=\n" 739 " some();", 740 format("var x =\n" 741 " `hello\n" 742 " ${world}` >= some();", 743 getGoogleJSStyleWithColumns(21))); // Barely doesn't fit. 744 EXPECT_EQ("var x = `hello\n" 745 " ${world}` >= some();", 746 format("var x =\n" 747 " `hello\n" 748 " ${world}` >= some();", 749 getGoogleJSStyleWithColumns(22))); // Barely fits. 750 751 verifyFormat("var x =\n `h`;", getGoogleJSStyleWithColumns(13 - 1)); 752 EXPECT_EQ( 753 "var x =\n `multi\n line`;", 754 format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(14 - 1))); 755 756 // Make sure template strings get a proper ColumnWidth assigned, even if they 757 // are first token in line. 758 verifyFormat( 759 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 760 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); 761 762 // Two template strings. 763 verifyFormat("var x = `hello` == `hello`;"); 764 765 // Comments in template strings. 766 EXPECT_EQ("var x = `//a`;\n" 767 "var y;", 768 format("var x =\n `//a`;\n" 769 "var y ;")); 770 EXPECT_EQ("var x = `/*a`;\n" 771 "var y;", 772 format("var x =\n `/*a`;\n" 773 "var y;")); 774 // Backticks in a comment - not a template string. 775 EXPECT_EQ("var x = 1 // `/*a`;\n" 776 " ;", 777 format("var x =\n 1 // `/*a`;\n" 778 " ;")); 779 EXPECT_EQ("/* ` */ var x = 1; /* ` */", 780 format("/* ` */ var x\n= 1; /* ` */")); 781 // Comment spans multiple template strings. 782 EXPECT_EQ("var x = `/*a`;\n" 783 "var y = ` */ `;", 784 format("var x =\n `/*a`;\n" 785 "var y =\n ` */ `;")); 786 // Escaped backtick. 787 EXPECT_EQ("var x = ` \\` a`;\n" 788 "var y;", 789 format("var x = ` \\` a`;\n" 790 "var y;")); 791 } 792 793 TEST_F(FormatTestJS, CastSyntax) { 794 verifyFormat("var x = <type>foo;"); 795 } 796 797 TEST_F(FormatTestJS, TypeArguments) { 798 verifyFormat("class X<Y> {}"); 799 verifyFormat("new X<Y>();"); 800 verifyFormat("foo<Y>(a);"); 801 verifyFormat("var x: X<Y>[];"); 802 verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); 803 verifyFormat("function f(a: List<any> = null) {\n}"); 804 verifyFormat("function f(): List<any> {\n}"); 805 } 806 807 TEST_F(FormatTestJS, OptionalTypes) { 808 verifyFormat("function x(a?: b, c?, d?) {\n}"); 809 verifyFormat("class X {\n" 810 " y?: z;\n" 811 " z?;\n" 812 "}"); 813 verifyFormat("interface X {\n" 814 " y?(): z;\n" 815 "}"); 816 verifyFormat("x ? 1 : 2;"); 817 verifyFormat("constructor({aa}: {\n" 818 " aa?: string,\n" 819 " aaaaaaaa?: string,\n" 820 " aaaaaaaaaaaaaaa?: boolean,\n" 821 " aaaaaa?: List<string>\n" 822 "}) {\n" 823 "}"); 824 } 825 826 TEST_F(FormatTestJS, IndexSignature) { 827 verifyFormat("var x: {[k: string]: v};"); 828 } 829 830 } // end namespace tooling 831 } // end namespace clang 832