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