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 verifyFormat("var aaaa = aaaaa || // wrap\n" 314 " [];"); 315 316 verifyFormat("someFunction([], {a: a});"); 317 } 318 319 TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) { 320 verifyFormat("var array = [\n" 321 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 322 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 323 "];"); 324 verifyFormat("var array = someFunction([\n" 325 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 326 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 327 "]);"); 328 } 329 330 TEST_F(FormatTestJS, FunctionLiterals) { 331 verifyFormat("doFoo(function() {});"); 332 verifyFormat("doFoo(function() { return 1; });"); 333 verifyFormat("var func = function() {\n" 334 " return 1;\n" 335 "};"); 336 verifyFormat("var func = //\n" 337 " function() {\n" 338 " return 1;\n" 339 "};"); 340 verifyFormat("return {\n" 341 " body: {\n" 342 " setAttribute: function(key, val) { this[key] = val; },\n" 343 " getAttribute: function(key) { return this[key]; },\n" 344 " style: {direction: ''}\n" 345 " }\n" 346 "};"); 347 verifyFormat("abc = xyz ? function() {\n" 348 " return 1;\n" 349 "} : function() {\n" 350 " return -1;\n" 351 "};"); 352 353 verifyFormat("var closure = goog.bind(\n" 354 " function() { // comment\n" 355 " foo();\n" 356 " bar();\n" 357 " },\n" 358 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n" 359 " arg3IsReallyLongAndNeeedsLineBreaks);"); 360 verifyFormat("var closure = goog.bind(function() { // comment\n" 361 " foo();\n" 362 " bar();\n" 363 "}, this);"); 364 verifyFormat("return {\n" 365 " a: 'E',\n" 366 " b: function() {\n" 367 " return function() {\n" 368 " f(); //\n" 369 " };\n" 370 " }\n" 371 "};"); 372 verifyFormat("{\n" 373 " var someVariable = function(x) {\n" 374 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 375 " };\n" 376 "}"); 377 verifyFormat("someLooooooooongFunction(\n" 378 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 380 " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 381 " // code\n" 382 " });"); 383 384 verifyFormat("f({a: function() { return 1; }});", 385 getGoogleJSStyleWithColumns(33)); 386 verifyFormat("f({\n" 387 " a: function() { return 1; }\n" 388 "});", 389 getGoogleJSStyleWithColumns(32)); 390 391 verifyFormat("return {\n" 392 " a: function SomeFunction() {\n" 393 " // ...\n" 394 " return 1;\n" 395 " }\n" 396 "};"); 397 verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 398 " .then(goog.bind(function(aaaaaaaaaaa) {\n" 399 " someFunction();\n" 400 " someFunction();\n" 401 " }, this), aaaaaaaaaaaaaaaaa);"); 402 403 verifyFormat("someFunction(goog.bind(function() {\n" 404 " doSomething();\n" 405 " doSomething();\n" 406 "}, this), goog.bind(function() {\n" 407 " doSomething();\n" 408 " doSomething();\n" 409 "}, this));"); 410 411 // FIXME: This is bad, we should be wrapping before "function() {". 412 verifyFormat("someFunction(function() {\n" 413 " doSomething(); // break\n" 414 "})\n" 415 " .doSomethingElse(\n" 416 " // break\n" 417 " );"); 418 } 419 420 TEST_F(FormatTestJS, InliningFunctionLiterals) { 421 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 422 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 423 verifyFormat("var func = function() {\n" 424 " return 1;\n" 425 "};", 426 Style); 427 verifyFormat("var func = doSomething(function() { return 1; });", Style); 428 verifyFormat("var outer = function() {\n" 429 " var inner = function() { return 1; }\n" 430 "};", 431 Style); 432 verifyFormat("function outer1(a, b) {\n" 433 " function inner1(a, b) { return a; }\n" 434 "}", 435 Style); 436 437 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 438 verifyFormat("var func = function() { return 1; };", Style); 439 verifyFormat("var func = doSomething(function() { return 1; });", Style); 440 verifyFormat( 441 "var outer = function() { var inner = function() { return 1; } };", 442 Style); 443 verifyFormat("function outer1(a, b) {\n" 444 " function inner1(a, b) { return a; }\n" 445 "}", 446 Style); 447 448 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 449 verifyFormat("var func = function() {\n" 450 " return 1;\n" 451 "};", 452 Style); 453 verifyFormat("var func = doSomething(function() {\n" 454 " return 1;\n" 455 "});", 456 Style); 457 verifyFormat("var outer = function() {\n" 458 " var inner = function() {\n" 459 " return 1;\n" 460 " }\n" 461 "};", 462 Style); 463 verifyFormat("function outer1(a, b) {\n" 464 " function inner1(a, b) {\n" 465 " return a;\n" 466 " }\n" 467 "}", 468 Style); 469 470 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 471 verifyFormat("var func = function() {\n" 472 " return 1;\n" 473 "};", 474 Style); 475 } 476 477 TEST_F(FormatTestJS, MultipleFunctionLiterals) { 478 verifyFormat("promise.then(\n" 479 " function success() {\n" 480 " doFoo();\n" 481 " doBar();\n" 482 " },\n" 483 " function error() {\n" 484 " doFoo();\n" 485 " doBaz();\n" 486 " },\n" 487 " []);\n"); 488 verifyFormat("promise.then(\n" 489 " function success() {\n" 490 " doFoo();\n" 491 " doBar();\n" 492 " },\n" 493 " [],\n" 494 " function error() {\n" 495 " doFoo();\n" 496 " doBaz();\n" 497 " });\n"); 498 verifyFormat("promise.then(\n" 499 " [],\n" 500 " function success() {\n" 501 " doFoo();\n" 502 " doBar();\n" 503 " },\n" 504 " function error() {\n" 505 " doFoo();\n" 506 " doBaz();\n" 507 " });\n"); 508 509 verifyFormat("getSomeLongPromise()\n" 510 " .then(function(value) { body(); })\n" 511 " .thenCatch(function(error) {\n" 512 " body();\n" 513 " body();\n" 514 " });"); 515 verifyFormat("getSomeLongPromise()\n" 516 " .then(function(value) {\n" 517 " body();\n" 518 " body();\n" 519 " })\n" 520 " .thenCatch(function(error) {\n" 521 " body();\n" 522 " body();\n" 523 " });"); 524 525 verifyFormat("getSomeLongPromise()\n" 526 " .then(function(value) { body(); })\n" 527 " .thenCatch(function(error) { body(); });"); 528 } 529 530 TEST_F(FormatTestJS, ArrowFunctions) { 531 verifyFormat("var x = (a) => {\n" 532 " return a;\n" 533 "};"); 534 verifyFormat("var x = (a) => {\n" 535 " function y() { return 42; }\n" 536 " return a;\n" 537 "};"); 538 verifyFormat("var x = (a: type): {some: type} => {\n" 539 " return a;\n" 540 "};"); 541 verifyFormat("var x = (a) => a;"); 542 verifyFormat("return () => [];"); 543 verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n" 544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 545 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n" 547 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 548 "};"); 549 verifyFormat("var a = a.aaaaaaa(\n" 550 " (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n" 551 " aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 552 verifyFormat("var a = a.aaaaaaa(\n" 553 " (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n" 554 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n" 555 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 556 557 // FIXME: This is bad, we should be wrapping before "() => {". 558 verifyFormat("someFunction(() => {\n" 559 " doSomething(); // break\n" 560 "})\n" 561 " .doSomethingElse(\n" 562 " // break\n" 563 " );"); 564 } 565 566 TEST_F(FormatTestJS, ReturnStatements) { 567 verifyFormat("function() {\n" 568 " return [hello, world];\n" 569 "}"); 570 } 571 572 TEST_F(FormatTestJS, ForLoops) { 573 verifyFormat("for (var i in [2, 3]) {\n" 574 "}"); 575 } 576 577 TEST_F(FormatTestJS, AutomaticSemicolonInsertion) { 578 // The following statements must not wrap, as otherwise the program meaning 579 // would change due to automatic semicolon insertion. 580 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1. 581 verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10)); 582 verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10)); 583 verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10)); 584 verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10)); 585 verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10)); 586 verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10)); 587 } 588 589 TEST_F(FormatTestJS, ClosureStyleCasts) { 590 verifyFormat("var x = /** @type {foo} */ (bar);"); 591 } 592 593 TEST_F(FormatTestJS, TryCatch) { 594 verifyFormat("try {\n" 595 " f();\n" 596 "} catch (e) {\n" 597 " g();\n" 598 "} finally {\n" 599 " h();\n" 600 "}"); 601 602 // But, of course, "catch" is a perfectly fine function name in JavaScript. 603 verifyFormat("someObject.catch();"); 604 verifyFormat("someObject.new();"); 605 verifyFormat("someObject.delete();"); 606 } 607 608 TEST_F(FormatTestJS, StringLiteralConcatenation) { 609 verifyFormat("var literal = 'hello ' +\n" 610 " 'world';"); 611 } 612 613 TEST_F(FormatTestJS, RegexLiteralClassification) { 614 // Regex literals. 615 verifyFormat("var regex = /abc/;"); 616 verifyFormat("f(/abc/);"); 617 verifyFormat("f(abc, /abc/);"); 618 verifyFormat("some_map[/abc/];"); 619 verifyFormat("var x = a ? /abc/ : /abc/;"); 620 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 621 verifyFormat("var x = !/abc/.test(y);"); 622 verifyFormat("var x = a && /abc/.test(y);"); 623 verifyFormat("var x = a || /abc/.test(y);"); 624 verifyFormat("var x = a + /abc/.search(y);"); 625 verifyFormat("/abc/.search(y);"); 626 verifyFormat("var regexs = {/abc/, /abc/};"); 627 verifyFormat("return /abc/;"); 628 629 // Not regex literals. 630 verifyFormat("var a = a / 2 + b / 3;"); 631 verifyFormat("var a = a++ / 2;"); 632 // Prefix unary can operate on regex literals, not that it makes sense. 633 verifyFormat("var a = ++/a/;"); 634 635 // This is a known issue, regular expressions are incorrectly detected if 636 // directly following a closing parenthesis. 637 verifyFormat("if (foo) / bar /.exec(baz);"); 638 } 639 640 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 641 verifyFormat("var regex = /=/;"); 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 = /^a$/;"); 648 verifyFormat("var regex = /\\/a/;"); 649 verifyFormat("var regex = /(?:x)/;"); 650 verifyFormat("var regex = /x(?=y)/;"); 651 verifyFormat("var regex = /x(?!y)/;"); 652 verifyFormat("var regex = /x|y/;"); 653 verifyFormat("var regex = /a{2}/;"); 654 verifyFormat("var regex = /a{1,3}/;"); 655 656 verifyFormat("var regex = /[abc]/;"); 657 verifyFormat("var regex = /[^abc]/;"); 658 verifyFormat("var regex = /[\\b]/;"); 659 verifyFormat("var regex = /[/]/;"); 660 verifyFormat("var regex = /[\\/]/;"); 661 verifyFormat("var regex = /\\[/;"); 662 verifyFormat("var regex = /\\\\[/]/;"); 663 verifyFormat("var regex = /}[\"]/;"); 664 verifyFormat("var regex = /}[/\"]/;"); 665 verifyFormat("var regex = /}[\"/]/;"); 666 667 verifyFormat("var regex = /\\b/;"); 668 verifyFormat("var regex = /\\B/;"); 669 verifyFormat("var regex = /\\d/;"); 670 verifyFormat("var regex = /\\D/;"); 671 verifyFormat("var regex = /\\f/;"); 672 verifyFormat("var regex = /\\n/;"); 673 verifyFormat("var regex = /\\r/;"); 674 verifyFormat("var regex = /\\s/;"); 675 verifyFormat("var regex = /\\S/;"); 676 verifyFormat("var regex = /\\t/;"); 677 verifyFormat("var regex = /\\v/;"); 678 verifyFormat("var regex = /\\w/;"); 679 verifyFormat("var regex = /\\W/;"); 680 verifyFormat("var regex = /a(a)\\1/;"); 681 verifyFormat("var regex = /\\0/;"); 682 verifyFormat("var regex = /\\\\/g;"); 683 verifyFormat("var regex = /\\a\\\\/g;"); 684 verifyFormat("var regex = /\a\\//g;"); 685 verifyFormat("var regex = /a\\//;\n" 686 "var x = 0;"); 687 EXPECT_EQ("var regex = /'/g;", format("var regex = /'/g ;")); 688 EXPECT_EQ("var regex = /'/g; //'", format("var regex = /'/g ; //'")); 689 EXPECT_EQ("var regex = /\\/*/;\n" 690 "var x = 0;", 691 format("var regex = /\\/*/;\n" 692 "var x=0;")); 693 EXPECT_EQ("var x = /a\\//;", format("var x = /a\\// \n;")); 694 verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16)); 695 verifyFormat("var regex =\n" 696 " /\"/;", 697 getGoogleJSStyleWithColumns(15)); 698 verifyFormat("var regex = //\n" 699 " /a/;"); 700 verifyFormat("var regexs = [\n" 701 " /d/, //\n" 702 " /aa/, //\n" 703 "];"); 704 } 705 706 TEST_F(FormatTestJS, RegexLiteralModifiers) { 707 verifyFormat("var regex = /abc/g;"); 708 verifyFormat("var regex = /abc/i;"); 709 verifyFormat("var regex = /abc/m;"); 710 verifyFormat("var regex = /abc/y;"); 711 } 712 713 TEST_F(FormatTestJS, RegexLiteralLength) { 714 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 715 getGoogleJSStyleWithColumns(60)); 716 verifyFormat("var regex =\n" 717 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 718 getGoogleJSStyleWithColumns(60)); 719 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 720 getGoogleJSStyleWithColumns(50)); 721 } 722 723 TEST_F(FormatTestJS, RegexLiteralExamples) { 724 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 725 } 726 727 TEST_F(FormatTestJS, TypeAnnotations) { 728 verifyFormat("var x: string;"); 729 verifyFormat("function x(): string {\n return 'x';\n}"); 730 verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}"); 731 verifyFormat("function x(y: string): string {\n return 'x';\n}"); 732 verifyFormat("for (var y: string in x) {\n x();\n}"); 733 verifyFormat("((a: string, b: number): string => a + b);"); 734 verifyFormat("var x: (y: number) => string;"); 735 verifyFormat("var x: P<string, (a: number) => string>;"); 736 verifyFormat("var x = {y: function(): z { return 1; }};"); 737 verifyFormat("var x = {y: function(): {a: number} { return 1; }};"); 738 } 739 740 TEST_F(FormatTestJS, ClassDeclarations) { 741 verifyFormat("class C {\n x: string = 12;\n}"); 742 verifyFormat("class C {\n x(): string => 12;\n}"); 743 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); 744 verifyFormat("class C {\n private x: string = 12;\n}"); 745 verifyFormat("class C {\n private static x: string = 12;\n}"); 746 verifyFormat("class C {\n static x(): string { return 'asd'; }\n}"); 747 verifyFormat("class C extends P implements I {}"); 748 verifyFormat("class C extends p.P implements i.I {}"); 749 verifyFormat("class Test {\n" 750 " aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n" 751 " aaaaaaaaaaaaaaaaaaaaaa {}\n" 752 "}"); 753 754 // ':' is not a type declaration here. 755 verifyFormat("class X {\n" 756 " subs = {\n" 757 " 'b': {\n" 758 " 'c': 1,\n" 759 " },\n" 760 " };\n" 761 "}"); 762 } 763 764 TEST_F(FormatTestJS, InterfaceDeclarations) { 765 verifyFormat("interface I {\n" 766 " x: string;\n" 767 " enum: string[];\n" 768 "}\n" 769 "var y;"); 770 // Ensure that state is reset after parsing the interface. 771 verifyFormat("interface a {}\n" 772 "export function b() {}\n" 773 "var x;"); 774 775 // Arrays of object type literals. 776 verifyFormat("interface I {\n" 777 " o: {}[];\n" 778 "}"); 779 } 780 781 TEST_F(FormatTestJS, EnumDeclarations) { 782 verifyFormat("enum Foo {\n" 783 " A = 1,\n" 784 " B\n" 785 "}"); 786 verifyFormat("export /* somecomment*/ enum Foo {\n" 787 " A = 1,\n" 788 " B\n" 789 "}"); 790 verifyFormat("enum Foo {\n" 791 " A = 1, // comment\n" 792 " B\n" 793 "}\n" 794 "var x = 1;"); 795 } 796 797 TEST_F(FormatTestJS, MetadataAnnotations) { 798 verifyFormat("@A\nclass C {\n}"); 799 verifyFormat("@A({arg: 'value'})\nclass C {\n}"); 800 verifyFormat("@A\n@B\nclass C {\n}"); 801 verifyFormat("class C {\n @A x: string;\n}"); 802 verifyFormat("class C {\n" 803 " @A\n" 804 " private x(): string {\n" 805 " return 'y';\n" 806 " }\n" 807 "}"); 808 verifyFormat("class X {}\n" 809 "class Y {}"); 810 } 811 812 TEST_F(FormatTestJS, Modules) { 813 verifyFormat("import SomeThing from 'some/module.js';"); 814 verifyFormat("import {X, Y} from 'some/module.js';"); 815 verifyFormat("import a, {X, Y} from 'some/module.js';"); 816 verifyFormat("import {\n" 817 " VeryLongImportsAreAnnoying,\n" 818 " VeryLongImportsAreAnnoying,\n" 819 " VeryLongImportsAreAnnoying,\n" 820 " VeryLongImportsAreAnnoying\n" 821 "} from 'some/module.js';"); 822 verifyFormat("import {\n" 823 " X,\n" 824 " Y,\n" 825 "} from 'some/module.js';"); 826 verifyFormat("import {\n" 827 " X,\n" 828 " Y,\n" 829 "} from 'some/long/module.js';", 830 getGoogleJSStyleWithColumns(20)); 831 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); 832 verifyFormat("import * as lib from 'some/module.js';"); 833 verifyFormat("var x = {import: 1};\nx.import = 2;"); 834 835 verifyFormat("export function fn() {\n" 836 " return 'fn';\n" 837 "}"); 838 verifyFormat("export function A() {}\n" 839 "export default function B() {}\n" 840 "export function C() {}"); 841 verifyFormat("export const x = 12;"); 842 verifyFormat("export default class X {}"); 843 verifyFormat("export {X, Y} from 'some/module.js';"); 844 verifyFormat("export {\n" 845 " X,\n" 846 " Y,\n" 847 "} from 'some/module.js';"); 848 verifyFormat("export class C {\n" 849 " x: number;\n" 850 " y: string;\n" 851 "}"); 852 verifyFormat("export class X { y: number; }"); 853 verifyFormat("export default class X { y: number }"); 854 verifyFormat("export default function() {\n return 1;\n}"); 855 verifyFormat("export var x = 12;"); 856 verifyFormat("class C {}\n" 857 "export function f() {}\n" 858 "var v;"); 859 verifyFormat("export var x: number = 12;"); 860 verifyFormat("export const y = {\n" 861 " a: 1,\n" 862 " b: 2\n" 863 "};"); 864 verifyFormat("export enum Foo {\n" 865 " BAR,\n" 866 " // adsdasd\n" 867 " BAZ\n" 868 "}"); 869 verifyFormat("export default [\n" 870 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 871 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 872 "];"); 873 verifyFormat("export default [];"); 874 verifyFormat("export default () => {};"); 875 } 876 877 TEST_F(FormatTestJS, TemplateStrings) { 878 // Keeps any whitespace/indentation within the template string. 879 EXPECT_EQ("var x = `hello\n" 880 " ${ name }\n" 881 " !`;", 882 format("var x = `hello\n" 883 " ${ name }\n" 884 " !`;")); 885 886 verifyFormat("var x =\n" 887 " `hello ${world}` >= some();", 888 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. 889 verifyFormat("var x = `hello ${world}` >= some();", 890 getGoogleJSStyleWithColumns(35)); // Barely fits. 891 EXPECT_EQ("var x = `hello\n" 892 " ${world}` >=\n" 893 " some();", 894 format("var x =\n" 895 " `hello\n" 896 " ${world}` >= some();", 897 getGoogleJSStyleWithColumns(21))); // Barely doesn't fit. 898 EXPECT_EQ("var x = `hello\n" 899 " ${world}` >= some();", 900 format("var x =\n" 901 " `hello\n" 902 " ${world}` >= some();", 903 getGoogleJSStyleWithColumns(22))); // Barely fits. 904 905 verifyFormat("var x =\n" 906 " `h`;", 907 getGoogleJSStyleWithColumns(11)); 908 EXPECT_EQ( 909 "var x =\n `multi\n line`;", 910 format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(13))); 911 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 912 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);"); 913 914 // Make sure template strings get a proper ColumnWidth assigned, even if they 915 // are first token in line. 916 verifyFormat( 917 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 918 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); 919 920 // Two template strings. 921 verifyFormat("var x = `hello` == `hello`;"); 922 923 // Comments in template strings. 924 EXPECT_EQ("var x = `//a`;\n" 925 "var y;", 926 format("var x =\n `//a`;\n" 927 "var y ;")); 928 EXPECT_EQ("var x = `/*a`;\n" 929 "var y;", 930 format("var x =\n `/*a`;\n" 931 "var y;")); 932 // Unterminated string literals in a template string. 933 verifyFormat("var x = `'`; // comment with matching quote '\n" 934 "var y;"); 935 verifyFormat("var x = `\"`; // comment with matching quote \"\n" 936 "var y;"); 937 // Backticks in a comment - not a template string. 938 EXPECT_EQ("var x = 1 // `/*a`;\n" 939 " ;", 940 format("var x =\n 1 // `/*a`;\n" 941 " ;")); 942 EXPECT_EQ("/* ` */ var x = 1; /* ` */", 943 format("/* ` */ var x\n= 1; /* ` */")); 944 // Comment spans multiple template strings. 945 EXPECT_EQ("var x = `/*a`;\n" 946 "var y = ` */ `;", 947 format("var x =\n `/*a`;\n" 948 "var y =\n ` */ `;")); 949 // Escaped backtick. 950 EXPECT_EQ("var x = ` \\` a`;\n" 951 "var y;", 952 format("var x = ` \\` a`;\n" 953 "var y;")); 954 } 955 956 TEST_F(FormatTestJS, CastSyntax) { verifyFormat("var x = <type>foo;"); } 957 958 TEST_F(FormatTestJS, TypeArguments) { 959 verifyFormat("class X<Y> {}"); 960 verifyFormat("new X<Y>();"); 961 verifyFormat("foo<Y>(a);"); 962 verifyFormat("var x: X<Y>[];"); 963 verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); 964 verifyFormat("function f(a: List<any> = null) {}"); 965 verifyFormat("function f(): List<any> {}"); 966 verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n" 967 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}"); 968 verifyFormat("function aaaaaaaaaa(\n" 969 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n" 970 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n" 971 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}"); 972 } 973 974 TEST_F(FormatTestJS, UserDefinedTypeGuards) { 975 verifyFormat( 976 "function foo(check: Object):\n" 977 " check is {foo: string, bar: string, baz: string, foobar: string} {\n" 978 " return 'bar' in check;\n" 979 "}\n"); 980 } 981 982 TEST_F(FormatTestJS, OptionalTypes) { 983 verifyFormat("function x(a?: b, c?, d?) {}"); 984 verifyFormat("class X {\n" 985 " y?: z;\n" 986 " z?;\n" 987 "}"); 988 verifyFormat("interface X {\n" 989 " y?(): z;\n" 990 "}"); 991 verifyFormat("x ? 1 : 2;"); 992 verifyFormat("constructor({aa}: {\n" 993 " aa?: string,\n" 994 " aaaaaaaa?: string,\n" 995 " aaaaaaaaaaaaaaa?: boolean,\n" 996 " aaaaaa?: List<string>\n" 997 "}) {}"); 998 } 999 1000 TEST_F(FormatTestJS, IndexSignature) { 1001 verifyFormat("var x: {[k: string]: v};"); 1002 } 1003 1004 TEST_F(FormatTestJS, WrapAfterParen) { 1005 verifyFormat("xxxxxxxxxxx(\n" 1006 " aaa, aaa);", 1007 getGoogleJSStyleWithColumns(20)); 1008 verifyFormat("xxxxxxxxxxx(\n" 1009 " aaa, aaa, aaa,\n" 1010 " aaa, aaa, aaa);", 1011 getGoogleJSStyleWithColumns(20)); 1012 verifyFormat("xxxxxxxxxxx(\n" 1013 " aaaaaaaaaaaaaaaaaaaaaaaa,\n" 1014 " function(x) {\n" 1015 " y(); //\n" 1016 " });", 1017 getGoogleJSStyleWithColumns(40)); 1018 verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 1019 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 1020 } 1021 1022 } // end namespace tooling 1023 } // end namespace clang 1024