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 verifyFormat("for (let {a, b} of x) {\n" 610 "}"); 611 verifyFormat("for (let {a, b} in x) {\n" 612 "}"); 613 } 614 615 TEST_F(FormatTestJS, AutomaticSemicolonInsertion) { 616 // The following statements must not wrap, as otherwise the program meaning 617 // would change due to automatic semicolon insertion. 618 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1. 619 verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10)); 620 verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10)); 621 verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10)); 622 verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10)); 623 verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10)); 624 verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10)); 625 verifyFormat("return [\n" 626 " aaa\n" 627 "];", 628 getGoogleJSStyleWithColumns(12)); 629 } 630 631 TEST_F(FormatTestJS, ClosureStyleCasts) { 632 verifyFormat("var x = /** @type {foo} */ (bar);"); 633 } 634 635 TEST_F(FormatTestJS, TryCatch) { 636 verifyFormat("try {\n" 637 " f();\n" 638 "} catch (e) {\n" 639 " g();\n" 640 "} finally {\n" 641 " h();\n" 642 "}"); 643 644 // But, of course, "catch" is a perfectly fine function name in JavaScript. 645 verifyFormat("someObject.catch();"); 646 verifyFormat("someObject.new();"); 647 verifyFormat("someObject.delete();"); 648 } 649 650 TEST_F(FormatTestJS, StringLiteralConcatenation) { 651 verifyFormat("var literal = 'hello ' +\n" 652 " 'world';"); 653 } 654 655 TEST_F(FormatTestJS, RegexLiteralClassification) { 656 // Regex literals. 657 verifyFormat("var regex = /abc/;"); 658 verifyFormat("f(/abc/);"); 659 verifyFormat("f(abc, /abc/);"); 660 verifyFormat("some_map[/abc/];"); 661 verifyFormat("var x = a ? /abc/ : /abc/;"); 662 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 663 verifyFormat("var x = !/abc/.test(y);"); 664 verifyFormat("var x = a && /abc/.test(y);"); 665 verifyFormat("var x = a || /abc/.test(y);"); 666 verifyFormat("var x = a + /abc/.search(y);"); 667 verifyFormat("/abc/.search(y);"); 668 verifyFormat("var regexs = {/abc/, /abc/};"); 669 verifyFormat("return /abc/;"); 670 671 // Not regex literals. 672 verifyFormat("var a = a / 2 + b / 3;"); 673 verifyFormat("var a = a++ / 2;"); 674 // Prefix unary can operate on regex literals, not that it makes sense. 675 verifyFormat("var a = ++/a/;"); 676 677 // This is a known issue, regular expressions are incorrectly detected if 678 // directly following a closing parenthesis. 679 verifyFormat("if (foo) / bar /.exec(baz);"); 680 } 681 682 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 683 verifyFormat("var regex = /=/;"); 684 verifyFormat("var regex = /a*/;"); 685 verifyFormat("var regex = /a+/;"); 686 verifyFormat("var regex = /a?/;"); 687 verifyFormat("var regex = /.a./;"); 688 verifyFormat("var regex = /a\\*/;"); 689 verifyFormat("var regex = /^a$/;"); 690 verifyFormat("var regex = /\\/a/;"); 691 verifyFormat("var regex = /(?:x)/;"); 692 verifyFormat("var regex = /x(?=y)/;"); 693 verifyFormat("var regex = /x(?!y)/;"); 694 verifyFormat("var regex = /x|y/;"); 695 verifyFormat("var regex = /a{2}/;"); 696 verifyFormat("var regex = /a{1,3}/;"); 697 698 verifyFormat("var regex = /[abc]/;"); 699 verifyFormat("var regex = /[^abc]/;"); 700 verifyFormat("var regex = /[\\b]/;"); 701 verifyFormat("var regex = /[/]/;"); 702 verifyFormat("var regex = /[\\/]/;"); 703 verifyFormat("var regex = /\\[/;"); 704 verifyFormat("var regex = /\\\\[/]/;"); 705 verifyFormat("var regex = /}[\"]/;"); 706 verifyFormat("var regex = /}[/\"]/;"); 707 verifyFormat("var regex = /}[\"/]/;"); 708 709 verifyFormat("var regex = /\\b/;"); 710 verifyFormat("var regex = /\\B/;"); 711 verifyFormat("var regex = /\\d/;"); 712 verifyFormat("var regex = /\\D/;"); 713 verifyFormat("var regex = /\\f/;"); 714 verifyFormat("var regex = /\\n/;"); 715 verifyFormat("var regex = /\\r/;"); 716 verifyFormat("var regex = /\\s/;"); 717 verifyFormat("var regex = /\\S/;"); 718 verifyFormat("var regex = /\\t/;"); 719 verifyFormat("var regex = /\\v/;"); 720 verifyFormat("var regex = /\\w/;"); 721 verifyFormat("var regex = /\\W/;"); 722 verifyFormat("var regex = /a(a)\\1/;"); 723 verifyFormat("var regex = /\\0/;"); 724 verifyFormat("var regex = /\\\\/g;"); 725 verifyFormat("var regex = /\\a\\\\/g;"); 726 verifyFormat("var regex = /\a\\//g;"); 727 verifyFormat("var regex = /a\\//;\n" 728 "var x = 0;"); 729 EXPECT_EQ("var regex = /'/g;", format("var regex = /'/g ;")); 730 EXPECT_EQ("var regex = /'/g; //'", format("var regex = /'/g ; //'")); 731 EXPECT_EQ("var regex = /\\/*/;\n" 732 "var x = 0;", 733 format("var regex = /\\/*/;\n" 734 "var x=0;")); 735 EXPECT_EQ("var x = /a\\//;", format("var x = /a\\// \n;")); 736 verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16)); 737 verifyFormat("var regex =\n" 738 " /\"/;", 739 getGoogleJSStyleWithColumns(15)); 740 verifyFormat("var regex = //\n" 741 " /a/;"); 742 verifyFormat("var regexs = [\n" 743 " /d/, //\n" 744 " /aa/, //\n" 745 "];"); 746 } 747 748 TEST_F(FormatTestJS, RegexLiteralModifiers) { 749 verifyFormat("var regex = /abc/g;"); 750 verifyFormat("var regex = /abc/i;"); 751 verifyFormat("var regex = /abc/m;"); 752 verifyFormat("var regex = /abc/y;"); 753 } 754 755 TEST_F(FormatTestJS, RegexLiteralLength) { 756 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 757 getGoogleJSStyleWithColumns(60)); 758 verifyFormat("var regex =\n" 759 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 760 getGoogleJSStyleWithColumns(60)); 761 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 762 getGoogleJSStyleWithColumns(50)); 763 } 764 765 TEST_F(FormatTestJS, RegexLiteralExamples) { 766 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 767 } 768 769 TEST_F(FormatTestJS, TypeAnnotations) { 770 verifyFormat("var x: string;"); 771 verifyFormat("var x: {a: string; b: number;} = {};"); 772 verifyFormat("function x(): string {\n return 'x';\n}"); 773 verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}"); 774 verifyFormat("function x(y: string): string {\n return 'x';\n}"); 775 verifyFormat("for (var y: string in x) {\n x();\n}"); 776 verifyFormat("for (var y: string of x) {\n x();\n}"); 777 verifyFormat("function x(y: {a?: number;} = {}): number {\n" 778 " return 12;\n" 779 "}"); 780 verifyFormat("((a: string, b: number): string => a + b);"); 781 verifyFormat("var x: (y: number) => string;"); 782 verifyFormat("var x: P<string, (a: number) => string>;"); 783 verifyFormat("var x = {y: function(): z { return 1; }};"); 784 verifyFormat("var x = {y: function(): {a: number} { return 1; }};"); 785 verifyFormat("function someFunc(args: string[]):\n" 786 " {longReturnValue: string[]} {}", 787 getGoogleJSStyleWithColumns(60)); 788 } 789 790 TEST_F(FormatTestJS, ClassDeclarations) { 791 verifyFormat("class C {\n x: string = 12;\n}"); 792 verifyFormat("class C {\n x(): string => 12;\n}"); 793 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); 794 verifyFormat("class C {\n private x: string = 12;\n}"); 795 verifyFormat("class C {\n private static x: string = 12;\n}"); 796 verifyFormat("class C {\n static x(): string { return 'asd'; }\n}"); 797 verifyFormat("class C extends P implements I {}"); 798 verifyFormat("class C extends p.P implements i.I {}"); 799 verifyFormat("class Test {\n" 800 " aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n" 801 " aaaaaaaaaaaaaaaaaaaaaa {}\n" 802 "}"); 803 verifyFormat("foo = class Name {\n" 804 " constructor() {}\n" 805 "};"); 806 verifyFormat("foo = class {\n" 807 " constructor() {}\n" 808 "};"); 809 verifyFormat("class C {\n" 810 " x: {y: Z;} = {};\n" 811 " private y: {y: Z;} = {};\n" 812 "}"); 813 814 // ':' is not a type declaration here. 815 verifyFormat("class X {\n" 816 " subs = {\n" 817 " 'b': {\n" 818 " 'c': 1,\n" 819 " },\n" 820 " };\n" 821 "}"); 822 } 823 824 TEST_F(FormatTestJS, InterfaceDeclarations) { 825 verifyFormat("interface I {\n" 826 " x: string;\n" 827 " enum: string[];\n" 828 " enum?: string[];\n" 829 "}\n" 830 "var y;"); 831 // Ensure that state is reset after parsing the interface. 832 verifyFormat("interface a {}\n" 833 "export function b() {}\n" 834 "var x;"); 835 836 // Arrays of object type literals. 837 verifyFormat("interface I {\n" 838 " o: {}[];\n" 839 "}"); 840 } 841 842 TEST_F(FormatTestJS, EnumDeclarations) { 843 verifyFormat("enum Foo {\n" 844 " A = 1,\n" 845 " B\n" 846 "}"); 847 verifyFormat("export /* somecomment*/ enum Foo {\n" 848 " A = 1,\n" 849 " B\n" 850 "}"); 851 verifyFormat("enum Foo {\n" 852 " A = 1, // comment\n" 853 " B\n" 854 "}\n" 855 "var x = 1;"); 856 } 857 858 TEST_F(FormatTestJS, MetadataAnnotations) { 859 verifyFormat("@A\nclass C {\n}"); 860 verifyFormat("@A({arg: 'value'})\nclass C {\n}"); 861 verifyFormat("@A\n@B\nclass C {\n}"); 862 verifyFormat("class C {\n @A x: string;\n}"); 863 verifyFormat("class C {\n" 864 " @A\n" 865 " private x(): string {\n" 866 " return 'y';\n" 867 " }\n" 868 "}"); 869 verifyFormat("class X {}\n" 870 "class Y {}"); 871 } 872 873 TEST_F(FormatTestJS, Modules) { 874 verifyFormat("import SomeThing from 'some/module.js';"); 875 verifyFormat("import {X, Y} from 'some/module.js';"); 876 verifyFormat("import a, {X, Y} from 'some/module.js';"); 877 verifyFormat("import {\n" 878 " VeryLongImportsAreAnnoying,\n" 879 " VeryLongImportsAreAnnoying,\n" 880 " VeryLongImportsAreAnnoying,\n" 881 " VeryLongImportsAreAnnoying\n" 882 "} from 'some/module.js';"); 883 verifyFormat("import {\n" 884 " X,\n" 885 " Y,\n" 886 "} from 'some/module.js';"); 887 verifyFormat("import {\n" 888 " X,\n" 889 " Y,\n" 890 "} from\n 'some/long/module.js';", 891 getGoogleJSStyleWithColumns(20)); 892 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); 893 verifyFormat("import * as lib from 'some/module.js';"); 894 verifyFormat("var x = {import: 1};\nx.import = 2;"); 895 896 verifyFormat("export function fn() {\n" 897 " return 'fn';\n" 898 "}"); 899 verifyFormat("export function A() {}\n" 900 "export default function B() {}\n" 901 "export function C() {}"); 902 verifyFormat("export const x = 12;"); 903 verifyFormat("export default class X {}"); 904 verifyFormat("export {X, Y} from 'some/module.js';"); 905 verifyFormat("export {\n" 906 " X,\n" 907 " Y,\n" 908 "} from 'some/module.js';"); 909 verifyFormat("export class C {\n" 910 " x: number;\n" 911 " y: string;\n" 912 "}"); 913 verifyFormat("export class X { y: number; }"); 914 verifyFormat("export abstract class X { y: number; }"); 915 verifyFormat("export default class X { y: number }"); 916 verifyFormat("export default function() {\n return 1;\n}"); 917 verifyFormat("export var x = 12;"); 918 verifyFormat("class C {}\n" 919 "export function f() {}\n" 920 "var v;"); 921 verifyFormat("export var x: number = 12;"); 922 verifyFormat("export const y = {\n" 923 " a: 1,\n" 924 " b: 2\n" 925 "};"); 926 verifyFormat("export enum Foo {\n" 927 " BAR,\n" 928 " // adsdasd\n" 929 " BAZ\n" 930 "}"); 931 verifyFormat("export default [\n" 932 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 933 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 934 "];"); 935 verifyFormat("export default [];"); 936 verifyFormat("export default () => {};"); 937 verifyFormat("export interface Foo { foo: number; }\n" 938 "export class Bar {\n" 939 " blah(): string { return this.blah; };\n" 940 "}"); 941 } 942 943 TEST_F(FormatTestJS, TemplateStrings) { 944 // Keeps any whitespace/indentation within the template string. 945 EXPECT_EQ("var x = `hello\n" 946 " ${ name }\n" 947 " !`;", 948 format("var x = `hello\n" 949 " ${ name }\n" 950 " !`;")); 951 952 verifyFormat("var x =\n" 953 " `hello ${world}` >= some();", 954 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. 955 verifyFormat("var x = `hello ${world}` >= some();", 956 getGoogleJSStyleWithColumns(35)); // Barely fits. 957 EXPECT_EQ("var x = `hello\n" 958 " ${world}` >=\n" 959 " some();", 960 format("var x =\n" 961 " `hello\n" 962 " ${world}` >= some();", 963 getGoogleJSStyleWithColumns(21))); // Barely doesn't fit. 964 EXPECT_EQ("var x = `hello\n" 965 " ${world}` >= some();", 966 format("var x =\n" 967 " `hello\n" 968 " ${world}` >= some();", 969 getGoogleJSStyleWithColumns(22))); // Barely fits. 970 971 verifyFormat("var x =\n" 972 " `h`;", 973 getGoogleJSStyleWithColumns(11)); 974 EXPECT_EQ( 975 "var x =\n `multi\n line`;", 976 format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(13))); 977 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 978 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);"); 979 980 // Make sure template strings get a proper ColumnWidth assigned, even if they 981 // are first token in line. 982 verifyFormat( 983 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 984 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); 985 986 // Two template strings. 987 verifyFormat("var x = `hello` == `hello`;"); 988 989 // Comments in template strings. 990 EXPECT_EQ("var x = `//a`;\n" 991 "var y;", 992 format("var x =\n `//a`;\n" 993 "var y ;")); 994 EXPECT_EQ("var x = `/*a`;\n" 995 "var y;", 996 format("var x =\n `/*a`;\n" 997 "var y;")); 998 // Unterminated string literals in a template string. 999 verifyFormat("var x = `'`; // comment with matching quote '\n" 1000 "var y;"); 1001 verifyFormat("var x = `\"`; // comment with matching quote \"\n" 1002 "var y;"); 1003 EXPECT_EQ("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa);", 1004 format("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa) ;", 1005 getGoogleJSStyleWithColumns(40))); 1006 // Backticks in a comment - not a template string. 1007 EXPECT_EQ("var x = 1 // `/*a`;\n" 1008 " ;", 1009 format("var x =\n 1 // `/*a`;\n" 1010 " ;")); 1011 EXPECT_EQ("/* ` */ var x = 1; /* ` */", 1012 format("/* ` */ var x\n= 1; /* ` */")); 1013 // Comment spans multiple template strings. 1014 EXPECT_EQ("var x = `/*a`;\n" 1015 "var y = ` */ `;", 1016 format("var x =\n `/*a`;\n" 1017 "var y =\n ` */ `;")); 1018 // Escaped backtick. 1019 EXPECT_EQ("var x = ` \\` a`;\n" 1020 "var y;", 1021 format("var x = ` \\` a`;\n" 1022 "var y;")); 1023 } 1024 1025 TEST_F(FormatTestJS, CastSyntax) { verifyFormat("var x = <type>foo;"); } 1026 1027 TEST_F(FormatTestJS, TypeArguments) { 1028 verifyFormat("class X<Y> {}"); 1029 verifyFormat("new X<Y>();"); 1030 verifyFormat("foo<Y>(a);"); 1031 verifyFormat("var x: X<Y>[];"); 1032 verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); 1033 verifyFormat("function f(a: List<any> = null) {}"); 1034 verifyFormat("function f(): List<any> {}"); 1035 verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n" 1036 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}"); 1037 verifyFormat("function aaaaaaaaaa(\n" 1038 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n" 1039 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n" 1040 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}"); 1041 } 1042 1043 TEST_F(FormatTestJS, UserDefinedTypeGuards) { 1044 verifyFormat( 1045 "function foo(check: Object):\n" 1046 " check is {foo: string, bar: string, baz: string, foobar: string} {\n" 1047 " return 'bar' in check;\n" 1048 "}\n"); 1049 } 1050 1051 TEST_F(FormatTestJS, OptionalTypes) { 1052 verifyFormat("function x(a?: b, c?, d?) {}"); 1053 verifyFormat("class X {\n" 1054 " y?: z;\n" 1055 " z?;\n" 1056 "}"); 1057 verifyFormat("interface X {\n" 1058 " y?(): z;\n" 1059 "}"); 1060 verifyFormat("x ? 1 : 2;"); 1061 verifyFormat("constructor({aa}: {\n" 1062 " aa?: string,\n" 1063 " aaaaaaaa?: string,\n" 1064 " aaaaaaaaaaaaaaa?: boolean,\n" 1065 " aaaaaa?: List<string>\n" 1066 "}) {}"); 1067 } 1068 1069 TEST_F(FormatTestJS, IndexSignature) { 1070 verifyFormat("var x: {[k: string]: v};"); 1071 } 1072 1073 TEST_F(FormatTestJS, WrapAfterParen) { 1074 verifyFormat("xxxxxxxxxxx(\n" 1075 " aaa, aaa);", 1076 getGoogleJSStyleWithColumns(20)); 1077 verifyFormat("xxxxxxxxxxx(\n" 1078 " aaa, aaa, aaa,\n" 1079 " aaa, aaa, aaa);", 1080 getGoogleJSStyleWithColumns(20)); 1081 verifyFormat("xxxxxxxxxxx(\n" 1082 " aaaaaaaaaaaaaaaaaaaaaaaa,\n" 1083 " function(x) {\n" 1084 " y(); //\n" 1085 " });", 1086 getGoogleJSStyleWithColumns(40)); 1087 verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 1088 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 1089 } 1090 1091 TEST_F(FormatTestJS, JSDocAnnotations) { 1092 EXPECT_EQ("/**\n" 1093 " * @export {this.is.a.long.path.to.a.Type}\n" 1094 " */", 1095 format("/**\n" 1096 " * @export {this.is.a.long.path.to.a.Type}\n" 1097 " */", 1098 getGoogleJSStyleWithColumns(20))); 1099 } 1100 1101 TEST_F(FormatTestJS, RequoteStringsSingle) { 1102 EXPECT_EQ("var x = 'foo';", format("var x = \"foo\";")); 1103 EXPECT_EQ("var x = 'fo\\'o\\'';", format("var x = \"fo'o'\";")); 1104 EXPECT_EQ("var x = 'fo\\'o\\'';", format("var x = \"fo\\'o'\";")); 1105 EXPECT_EQ("var x =\n" 1106 " 'foo\\'';", 1107 // Code below is 15 chars wide, doesn't fit into the line with the 1108 // \ escape added. 1109 format("var x = \"foo'\";", getGoogleJSStyleWithColumns(15))); 1110 // Removes no-longer needed \ escape from ". 1111 EXPECT_EQ("var x = 'fo\"o';", format("var x = \"fo\\\"o\";")); 1112 // Code below fits into 15 chars *after* removing the \ escape. 1113 EXPECT_EQ("var x = 'fo\"o';", 1114 format("var x = \"fo\\\"o\";", getGoogleJSStyleWithColumns(15))); 1115 } 1116 1117 TEST_F(FormatTestJS, RequoteStringsDouble) { 1118 FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1119 DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double; 1120 verifyFormat("var x = \"foo\";", DoubleQuotes); 1121 EXPECT_EQ("var x = \"foo\";", format("var x = 'foo';", DoubleQuotes)); 1122 EXPECT_EQ("var x = \"fo'o\";", format("var x = 'fo\\'o';", DoubleQuotes)); 1123 } 1124 1125 TEST_F(FormatTestJS, RequoteStringsLeave) { 1126 FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1127 LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave; 1128 verifyFormat("var x = \"foo\";", LeaveQuotes); 1129 verifyFormat("var x = 'foo';", LeaveQuotes); 1130 } 1131 1132 } // end namespace tooling 1133 } // end namespace clang 1134