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