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