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 auto Result = applyAllReplacements(Code, Replaces); 32 EXPECT_TRUE(static_cast<bool>(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 static void verifyFormat( 57 llvm::StringRef Expected, 58 llvm::StringRef Code, 59 const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) { 60 std::string Result = format(Code, Style); 61 EXPECT_EQ(Expected.str(), Result) << "Formatted:\n" << Result; 62 } 63 }; 64 65 TEST_F(FormatTestJS, BlockComments) { 66 verifyFormat("/* aaaaaaaaaaaaa */ aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 67 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 68 } 69 70 TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) { 71 verifyFormat("a == = b;"); 72 verifyFormat("a != = b;"); 73 74 verifyFormat("a === b;"); 75 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10)); 76 verifyFormat("a !== b;"); 77 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10)); 78 verifyFormat("if (a + b + c +\n" 79 " d !==\n" 80 " e + f + g)\n" 81 " q();", 82 getGoogleJSStyleWithColumns(20)); 83 84 verifyFormat("a >> >= b;"); 85 86 verifyFormat("a >>> b;"); 87 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10)); 88 verifyFormat("a >>>= b;"); 89 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10)); 90 verifyFormat("if (a + b + c +\n" 91 " d >>>\n" 92 " e + f + g)\n" 93 " q();", 94 getGoogleJSStyleWithColumns(20)); 95 verifyFormat("var x = aaaaaaaaaa ?\n" 96 " bbbbbb :\n" 97 " ccc;", 98 getGoogleJSStyleWithColumns(20)); 99 100 verifyFormat("var b = a.map((x) => x + 1);"); 101 verifyFormat("return ('aaa') in bbbb;"); 102 verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n" 103 " aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 104 FormatStyle Style = getGoogleJSStyleWithColumns(80); 105 Style.AlignOperands = true; 106 verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n" 107 " aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 108 Style); 109 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 110 verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa()\n" 111 " in aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 112 Style); 113 114 // ES6 spread operator. 115 verifyFormat("someFunction(...a);"); 116 verifyFormat("var x = [1, ...a, 2];"); 117 } 118 119 TEST_F(FormatTestJS, UnderstandsAmpAmp) { 120 verifyFormat("e && e.SomeFunction();"); 121 } 122 123 TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) { 124 verifyFormat("not.and.or.not_eq = 1;"); 125 } 126 127 TEST_F(FormatTestJS, ReservedWords) { 128 // JavaScript reserved words (aka keywords) are only illegal when used as 129 // Identifiers, but are legal as IdentifierNames. 130 verifyFormat("x.class.struct = 1;"); 131 verifyFormat("x.case = 1;"); 132 verifyFormat("x.interface = 1;"); 133 verifyFormat("x.for = 1;"); 134 verifyFormat("x.of() = 1;"); 135 verifyFormat("x.in() = 1;"); 136 verifyFormat("x.let() = 1;"); 137 verifyFormat("x.var() = 1;"); 138 verifyFormat("x = {\n" 139 " a: 12,\n" 140 " interface: 1,\n" 141 " switch: 1,\n" 142 "};"); 143 verifyFormat("var struct = 2;"); 144 verifyFormat("var union = 2;"); 145 verifyFormat("var interface = 2;"); 146 verifyFormat("interface = 2;"); 147 verifyFormat("x = interface instanceof y;"); 148 } 149 150 TEST_F(FormatTestJS, ReservedWordsMethods) { 151 verifyFormat( 152 "class X {\n" 153 " delete() {\n" 154 " x();\n" 155 " }\n" 156 " interface() {\n" 157 " x();\n" 158 " }\n" 159 " let() {\n" 160 " x();\n" 161 " }\n" 162 "}\n"); 163 } 164 165 TEST_F(FormatTestJS, CppKeywords) { 166 // Make sure we don't mess stuff up because of C++ keywords. 167 verifyFormat("return operator && (aa);"); 168 } 169 170 TEST_F(FormatTestJS, ES6DestructuringAssignment) { 171 verifyFormat("var [a, b, c] = [1, 2, 3];"); 172 verifyFormat("const [a, b, c] = [1, 2, 3];"); 173 verifyFormat("let [a, b, c] = [1, 2, 3];"); 174 verifyFormat("var {a, b} = {a: 1, b: 2};"); 175 verifyFormat("let {a, b} = {a: 1, b: 2};"); 176 } 177 178 TEST_F(FormatTestJS, ContainerLiterals) { 179 verifyFormat("var x = {\n" 180 " y: function(a) {\n" 181 " return a;\n" 182 " }\n" 183 "};"); 184 verifyFormat("return {\n" 185 " link: function() {\n" 186 " f(); //\n" 187 " }\n" 188 "};"); 189 verifyFormat("return {\n" 190 " a: a,\n" 191 " link: function() {\n" 192 " f(); //\n" 193 " }\n" 194 "};"); 195 verifyFormat("return {\n" 196 " a: a,\n" 197 " link: function() {\n" 198 " f(); //\n" 199 " },\n" 200 " link: function() {\n" 201 " f(); //\n" 202 " }\n" 203 "};"); 204 verifyFormat("var stuff = {\n" 205 " // comment for update\n" 206 " update: false,\n" 207 " // comment for modules\n" 208 " modules: false,\n" 209 " // comment for tasks\n" 210 " tasks: false\n" 211 "};"); 212 verifyFormat("return {\n" 213 " 'finish':\n" 214 " //\n" 215 " a\n" 216 "};"); 217 verifyFormat("var obj = {\n" 218 " fooooooooo: function(x) {\n" 219 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 220 " }\n" 221 "};"); 222 // Simple object literal, as opposed to enum style below. 223 verifyFormat("var obj = {a: 123};"); 224 // Enum style top level assignment. 225 verifyFormat("X = {\n a: 123\n};"); 226 verifyFormat("X.Y = {\n a: 123\n};"); 227 // But only on the top level, otherwise its a plain object literal assignment. 228 verifyFormat("function x() {\n" 229 " y = {z: 1};\n" 230 "}"); 231 verifyFormat("x = foo && {a: 123};"); 232 233 // Arrow functions in object literals. 234 verifyFormat("var x = {\n" 235 " y: (a) => {\n" 236 " return a;\n" 237 " }\n" 238 "};"); 239 verifyFormat("var x = {y: (a) => a};"); 240 241 // Computed keys. 242 verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};"); 243 verifyFormat("var x = {\n" 244 " [a]: 1,\n" 245 " b: 2,\n" 246 " [c]: 3,\n" 247 "};"); 248 249 // Object literals can leave out labels. 250 verifyFormat("f({a}, () => {\n" 251 " g(); //\n" 252 "});"); 253 254 // Keys can be quoted. 255 verifyFormat("var x = {\n" 256 " a: a,\n" 257 " b: b,\n" 258 " 'c': c,\n" 259 "};"); 260 } 261 262 TEST_F(FormatTestJS, MethodsInObjectLiterals) { 263 verifyFormat("var o = {\n" 264 " value: 'test',\n" 265 " get value() { // getter\n" 266 " return this.value;\n" 267 " }\n" 268 "};"); 269 verifyFormat("var o = {\n" 270 " value: 'test',\n" 271 " set value(val) { // setter\n" 272 " this.value = val;\n" 273 " }\n" 274 "};"); 275 verifyFormat("var o = {\n" 276 " value: 'test',\n" 277 " someMethod(val) { // method\n" 278 " doSomething(this.value + val);\n" 279 " }\n" 280 "};"); 281 verifyFormat("var o = {\n" 282 " someMethod(val) { // method\n" 283 " doSomething(this.value + val);\n" 284 " },\n" 285 " someOtherMethod(val) { // method\n" 286 " doSomething(this.value + val);\n" 287 " }\n" 288 "};"); 289 } 290 291 TEST_F(FormatTestJS, SpacesInContainerLiterals) { 292 verifyFormat("var arr = [1, 2, 3];"); 293 verifyFormat("f({a: 1, b: 2, c: 3});"); 294 295 verifyFormat("var object_literal_with_long_name = {\n" 296 " a: 'aaaaaaaaaaaaaaaaaa',\n" 297 " b: 'bbbbbbbbbbbbbbbbbb'\n" 298 "};"); 299 300 verifyFormat("f({a: 1, b: 2, c: 3});", 301 getChromiumStyle(FormatStyle::LK_JavaScript)); 302 verifyFormat("f({'a': [{}]});"); 303 } 304 305 TEST_F(FormatTestJS, SingleQuotedStrings) { 306 verifyFormat("this.function('', true);"); 307 } 308 309 TEST_F(FormatTestJS, GoogScopes) { 310 verifyFormat("goog.scope(function() {\n" 311 "var x = a.b;\n" 312 "var y = c.d;\n" 313 "}); // goog.scope"); 314 verifyFormat("goog.scope(function() {\n" 315 "// test\n" 316 "var x = 0;\n" 317 "// test\n" 318 "});"); 319 } 320 321 TEST_F(FormatTestJS, GoogModules) { 322 verifyFormat("goog.module('this.is.really.absurdly.long');", 323 getGoogleJSStyleWithColumns(40)); 324 verifyFormat("goog.require('this.is.really.absurdly.long');", 325 getGoogleJSStyleWithColumns(40)); 326 verifyFormat("goog.provide('this.is.really.absurdly.long');", 327 getGoogleJSStyleWithColumns(40)); 328 verifyFormat("var long = goog.require('this.is.really.absurdly.long');", 329 getGoogleJSStyleWithColumns(40)); 330 verifyFormat("goog.setTestOnly('this.is.really.absurdly.long');", 331 getGoogleJSStyleWithColumns(40)); 332 verifyFormat("goog.forwardDeclare('this.is.really.absurdly.long');", 333 getGoogleJSStyleWithColumns(40)); 334 335 // These should be wrapped normally. 336 verifyFormat( 337 "var MyLongClassName =\n" 338 " goog.module.get('my.long.module.name.followedBy.MyLongClassName');"); 339 } 340 341 TEST_F(FormatTestJS, FormatsNamespaces) { 342 verifyFormat("namespace Foo {\n" 343 " export let x = 1;\n" 344 "}\n"); 345 verifyFormat("declare namespace Foo {\n" 346 " export let x: number;\n" 347 "}\n"); 348 } 349 350 TEST_F(FormatTestJS, FormatsFreestandingFunctions) { 351 verifyFormat("function outer1(a, b) {\n" 352 " function inner1(a, b) {\n" 353 " return a;\n" 354 " }\n" 355 " inner1(a, b);\n" 356 "}\n" 357 "function outer2(a, b) {\n" 358 " function inner2(a, b) {\n" 359 " return a;\n" 360 " }\n" 361 " inner2(a, b);\n" 362 "}"); 363 verifyFormat("function f() {}"); 364 } 365 366 TEST_F(FormatTestJS, GeneratorFunctions) { 367 verifyFormat("function* f() {\n" 368 " let x = 1;\n" 369 " yield x;\n" 370 " yield* something();\n" 371 " yield [1, 2];\n" 372 " yield {a: 1};\n" 373 "}"); 374 verifyFormat("function*\n" 375 " f() {\n" 376 "}", 377 getGoogleJSStyleWithColumns(8)); 378 verifyFormat("export function* f() {\n" 379 " yield 1;\n" 380 "}\n"); 381 verifyFormat("class X {\n" 382 " * generatorMethod() {\n" 383 " yield x;\n" 384 " }\n" 385 "}"); 386 } 387 388 TEST_F(FormatTestJS, AsyncFunctions) { 389 verifyFormat("async function f() {\n" 390 " let x = 1;\n" 391 " return fetch(x);\n" 392 "}"); 393 verifyFormat("async function* f() {\n" 394 " yield fetch(x);\n" 395 "}"); 396 verifyFormat("export async function f() {\n" 397 " return fetch(x);\n" 398 "}"); 399 verifyFormat("class X {\n" 400 " async asyncMethod() {\n" 401 " return fetch(1);\n" 402 " }\n" 403 "}"); 404 verifyFormat("function initialize() {\n" 405 " // Comment.\n" 406 " return async.then();\n" 407 "}\n"); 408 } 409 410 TEST_F(FormatTestJS, ArrayLiterals) { 411 verifyFormat("var aaaaa: List<SomeThing> =\n" 412 " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];"); 413 verifyFormat("return [\n" 414 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 415 " ccccccccccccccccccccccccccc\n" 416 "];"); 417 verifyFormat("return [\n" 418 " aaaa().bbbbbbbb('A'),\n" 419 " aaaa().bbbbbbbb('B'),\n" 420 " aaaa().bbbbbbbb('C'),\n" 421 "];"); 422 verifyFormat("var someVariable = SomeFunction([\n" 423 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 424 " ccccccccccccccccccccccccccc\n" 425 "]);"); 426 verifyFormat("var someVariable = SomeFunction([\n" 427 " [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n" 428 "]);", 429 getGoogleJSStyleWithColumns(51)); 430 verifyFormat("var someVariable = SomeFunction(aaaa, [\n" 431 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 432 " ccccccccccccccccccccccccccc\n" 433 "]);"); 434 verifyFormat("var someVariable = SomeFunction(\n" 435 " aaaa,\n" 436 " [\n" 437 " aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 438 " cccccccccccccccccccccccccc\n" 439 " ],\n" 440 " aaaa);"); 441 verifyFormat("var aaaa = aaaaa || // wrap\n" 442 " [];"); 443 444 verifyFormat("someFunction([], {a: a});"); 445 } 446 447 TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) { 448 verifyFormat("var array = [\n" 449 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 450 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 451 "];"); 452 verifyFormat("var array = someFunction([\n" 453 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 454 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 455 "]);"); 456 } 457 458 TEST_F(FormatTestJS, FunctionLiterals) { 459 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 460 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 461 verifyFormat("doFoo(function() {});"); 462 verifyFormat("doFoo(function() { return 1; });", Style); 463 verifyFormat("var func = function() {\n" 464 " return 1;\n" 465 "};"); 466 verifyFormat("var func = //\n" 467 " function() {\n" 468 " return 1;\n" 469 "};"); 470 verifyFormat("return {\n" 471 " body: {\n" 472 " setAttribute: function(key, val) { this[key] = val; },\n" 473 " getAttribute: function(key) { return this[key]; },\n" 474 " style: {direction: ''}\n" 475 " }\n" 476 "};", 477 Style); 478 verifyFormat("abc = xyz ? function() {\n" 479 " return 1;\n" 480 "} : function() {\n" 481 " return -1;\n" 482 "};"); 483 484 verifyFormat("var closure = goog.bind(\n" 485 " function() { // comment\n" 486 " foo();\n" 487 " bar();\n" 488 " },\n" 489 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n" 490 " arg3IsReallyLongAndNeeedsLineBreaks);"); 491 verifyFormat("var closure = goog.bind(function() { // comment\n" 492 " foo();\n" 493 " bar();\n" 494 "}, this);"); 495 verifyFormat("return {\n" 496 " a: 'E',\n" 497 " b: function() {\n" 498 " return function() {\n" 499 " f(); //\n" 500 " };\n" 501 " }\n" 502 "};"); 503 verifyFormat("{\n" 504 " var someVariable = function(x) {\n" 505 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 506 " };\n" 507 "}"); 508 verifyFormat("someLooooooooongFunction(\n" 509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 511 " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 512 " // code\n" 513 " });"); 514 515 verifyFormat("return {\n" 516 " a: function SomeFunction() {\n" 517 " // ...\n" 518 " return 1;\n" 519 " }\n" 520 "};"); 521 verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 522 " .then(goog.bind(function(aaaaaaaaaaa) {\n" 523 " someFunction();\n" 524 " someFunction();\n" 525 " }, this), aaaaaaaaaaaaaaaaa);"); 526 527 verifyFormat("someFunction(goog.bind(function() {\n" 528 " doSomething();\n" 529 " doSomething();\n" 530 "}, this), goog.bind(function() {\n" 531 " doSomething();\n" 532 " doSomething();\n" 533 "}, this));"); 534 535 // FIXME: This is bad, we should be wrapping before "function() {". 536 verifyFormat("someFunction(function() {\n" 537 " doSomething(); // break\n" 538 "})\n" 539 " .doSomethingElse(\n" 540 " // break\n" 541 " );"); 542 543 Style.ColumnLimit = 33; 544 verifyFormat("f({a: function() { return 1; }});", Style); 545 Style.ColumnLimit = 32; 546 verifyFormat("f({\n" 547 " a: function() { return 1; }\n" 548 "});", 549 Style); 550 551 } 552 553 TEST_F(FormatTestJS, InliningFunctionLiterals) { 554 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 555 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 556 verifyFormat("var func = function() {\n" 557 " return 1;\n" 558 "};", 559 Style); 560 verifyFormat("var func = doSomething(function() { return 1; });", Style); 561 verifyFormat("var outer = function() {\n" 562 " var inner = function() { return 1; }\n" 563 "};", 564 Style); 565 verifyFormat("function outer1(a, b) {\n" 566 " function inner1(a, b) { return a; }\n" 567 "}", 568 Style); 569 570 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 571 verifyFormat("var func = function() { return 1; };", Style); 572 verifyFormat("var func = doSomething(function() { return 1; });", Style); 573 verifyFormat( 574 "var outer = function() { var inner = function() { return 1; } };", 575 Style); 576 verifyFormat("function outer1(a, b) {\n" 577 " function inner1(a, b) { return a; }\n" 578 "}", 579 Style); 580 581 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 582 verifyFormat("var func = function() {\n" 583 " return 1;\n" 584 "};", 585 Style); 586 verifyFormat("var func = doSomething(function() {\n" 587 " return 1;\n" 588 "});", 589 Style); 590 verifyFormat("var outer = function() {\n" 591 " var inner = function() {\n" 592 " return 1;\n" 593 " }\n" 594 "};", 595 Style); 596 verifyFormat("function outer1(a, b) {\n" 597 " function inner1(a, b) {\n" 598 " return a;\n" 599 " }\n" 600 "}", 601 Style); 602 603 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 604 verifyFormat("var func = function() {\n" 605 " return 1;\n" 606 "};", 607 Style); 608 } 609 610 TEST_F(FormatTestJS, MultipleFunctionLiterals) { 611 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 612 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 613 verifyFormat("promise.then(\n" 614 " function success() {\n" 615 " doFoo();\n" 616 " doBar();\n" 617 " },\n" 618 " function error() {\n" 619 " doFoo();\n" 620 " doBaz();\n" 621 " },\n" 622 " []);\n"); 623 verifyFormat("promise.then(\n" 624 " function success() {\n" 625 " doFoo();\n" 626 " doBar();\n" 627 " },\n" 628 " [],\n" 629 " function error() {\n" 630 " doFoo();\n" 631 " doBaz();\n" 632 " });\n"); 633 verifyFormat("promise.then(\n" 634 " [],\n" 635 " function success() {\n" 636 " doFoo();\n" 637 " doBar();\n" 638 " },\n" 639 " function error() {\n" 640 " doFoo();\n" 641 " doBaz();\n" 642 " });\n"); 643 644 verifyFormat("getSomeLongPromise()\n" 645 " .then(function(value) { body(); })\n" 646 " .thenCatch(function(error) {\n" 647 " body();\n" 648 " body();\n" 649 " });", 650 Style); 651 verifyFormat("getSomeLongPromise()\n" 652 " .then(function(value) {\n" 653 " body();\n" 654 " body();\n" 655 " })\n" 656 " .thenCatch(function(error) {\n" 657 " body();\n" 658 " body();\n" 659 " });"); 660 661 verifyFormat("getSomeLongPromise()\n" 662 " .then(function(value) { body(); })\n" 663 " .thenCatch(function(error) { body(); });", 664 Style); 665 666 verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n" 667 " .aaaaaaa(function() {\n" 668 " //\n" 669 " })\n" 670 " .bbbbbb();"); 671 } 672 673 TEST_F(FormatTestJS, ArrowFunctions) { 674 verifyFormat("var x = (a) => {\n" 675 " return a;\n" 676 "};"); 677 verifyFormat("var x = (a) => {\n" 678 " function y() {\n" 679 " return 42;\n" 680 " }\n" 681 " return a;\n" 682 "};"); 683 verifyFormat("var x = (a: type): {some: type} => {\n" 684 " return a;\n" 685 "};"); 686 verifyFormat("var x = (a) => a;"); 687 verifyFormat("return () => [];"); 688 verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n" 689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 690 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 691 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n" 692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 693 "};"); 694 verifyFormat("var a = a.aaaaaaa(\n" 695 " (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n" 696 " aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 697 verifyFormat("var a = a.aaaaaaa(\n" 698 " (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n" 699 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n" 700 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 701 702 // FIXME: This is bad, we should be wrapping before "() => {". 703 verifyFormat("someFunction(() => {\n" 704 " doSomething(); // break\n" 705 "})\n" 706 " .doSomethingElse(\n" 707 " // break\n" 708 " );"); 709 } 710 711 TEST_F(FormatTestJS, ReturnStatements) { 712 verifyFormat("function() {\n" 713 " return [hello, world];\n" 714 "}"); 715 } 716 717 TEST_F(FormatTestJS, ForLoops) { 718 verifyFormat("for (var i in [2, 3]) {\n" 719 "}"); 720 verifyFormat("for (var i of [2, 3]) {\n" 721 "}"); 722 verifyFormat("for (let {a, b} of x) {\n" 723 "}"); 724 verifyFormat("for (let {a, b} in x) {\n" 725 "}"); 726 } 727 728 TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) { 729 // The following statements must not wrap, as otherwise the program meaning 730 // would change due to automatic semicolon insertion. 731 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1. 732 verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10)); 733 verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10)); 734 verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10)); 735 verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10)); 736 verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10)); 737 verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10)); 738 verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10)); 739 verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10)); 740 verifyFormat("return [\n" 741 " aaa\n" 742 "];", 743 getGoogleJSStyleWithColumns(12)); 744 } 745 746 TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) { 747 verifyFormat("a\n" 748 "b;", 749 " a \n" 750 " b ;"); 751 verifyFormat("a()\n" 752 "b;", 753 " a ()\n" 754 " b ;"); 755 verifyFormat("a[b]\n" 756 "c;", 757 "a [b]\n" 758 "c ;"); 759 verifyFormat("1\n" 760 "a;", 761 "1 \n" 762 "a ;"); 763 verifyFormat("a\n" 764 "1;", 765 "a \n" 766 "1 ;"); 767 verifyFormat("a\n" 768 "'x';", 769 "a \n" 770 " 'x';"); 771 verifyFormat("a++\n" 772 "b;", 773 "a ++\n" 774 "b ;"); 775 verifyFormat("a\n" 776 "!b && c;", 777 "a \n" 778 " ! b && c;"); 779 verifyFormat("a\n" 780 "if (1) f();", 781 " a\n" 782 " if (1) f();"); 783 verifyFormat("a\n" 784 "class X {}", 785 " a\n" 786 " class X {}"); 787 verifyFormat("var a", "var\n" 788 "a"); 789 verifyFormat("x instanceof String", "x\n" 790 "instanceof\n" 791 "String"); 792 verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n" 793 " bar) {}"); 794 verifyFormat("a = true\n" 795 "return 1", 796 "a = true\n" 797 " return 1"); 798 verifyFormat("a = 's'\n" 799 "return 1", 800 "a = 's'\n" 801 " return 1"); 802 verifyFormat("a = null\n" 803 "return 1", 804 "a = null\n" 805 " return 1"); 806 } 807 808 TEST_F(FormatTestJS, ClosureStyleCasts) { 809 verifyFormat("var x = /** @type {foo} */ (bar);"); 810 } 811 812 TEST_F(FormatTestJS, TryCatch) { 813 verifyFormat("try {\n" 814 " f();\n" 815 "} catch (e) {\n" 816 " g();\n" 817 "} finally {\n" 818 " h();\n" 819 "}"); 820 821 // But, of course, "catch" is a perfectly fine function name in JavaScript. 822 verifyFormat("someObject.catch();"); 823 verifyFormat("someObject.new();"); 824 verifyFormat("someObject.delete();"); 825 } 826 827 TEST_F(FormatTestJS, StringLiteralConcatenation) { 828 verifyFormat("var literal = 'hello ' +\n" 829 " 'world';"); 830 } 831 832 TEST_F(FormatTestJS, RegexLiteralClassification) { 833 // Regex literals. 834 verifyFormat("var regex = /abc/;"); 835 verifyFormat("f(/abc/);"); 836 verifyFormat("f(abc, /abc/);"); 837 verifyFormat("some_map[/abc/];"); 838 verifyFormat("var x = a ? /abc/ : /abc/;"); 839 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 840 verifyFormat("var x = !/abc/.test(y);"); 841 verifyFormat("var x = a && /abc/.test(y);"); 842 verifyFormat("var x = a || /abc/.test(y);"); 843 verifyFormat("var x = a + /abc/.search(y);"); 844 verifyFormat("/abc/.search(y);"); 845 verifyFormat("var regexs = {/abc/, /abc/};"); 846 verifyFormat("return /abc/;"); 847 848 // Not regex literals. 849 verifyFormat("var a = a / 2 + b / 3;"); 850 verifyFormat("var a = a++ / 2;"); 851 // Prefix unary can operate on regex literals, not that it makes sense. 852 verifyFormat("var a = ++/a/;"); 853 854 // This is a known issue, regular expressions are incorrectly detected if 855 // directly following a closing parenthesis. 856 verifyFormat("if (foo) / bar /.exec(baz);"); 857 } 858 859 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 860 verifyFormat("var regex = /=/;"); 861 verifyFormat("var regex = /a*/;"); 862 verifyFormat("var regex = /a+/;"); 863 verifyFormat("var regex = /a?/;"); 864 verifyFormat("var regex = /.a./;"); 865 verifyFormat("var regex = /a\\*/;"); 866 verifyFormat("var regex = /^a$/;"); 867 verifyFormat("var regex = /\\/a/;"); 868 verifyFormat("var regex = /(?:x)/;"); 869 verifyFormat("var regex = /x(?=y)/;"); 870 verifyFormat("var regex = /x(?!y)/;"); 871 verifyFormat("var regex = /x|y/;"); 872 verifyFormat("var regex = /a{2}/;"); 873 verifyFormat("var regex = /a{1,3}/;"); 874 875 verifyFormat("var regex = /[abc]/;"); 876 verifyFormat("var regex = /[^abc]/;"); 877 verifyFormat("var regex = /[\\b]/;"); 878 verifyFormat("var regex = /[/]/;"); 879 verifyFormat("var regex = /[\\/]/;"); 880 verifyFormat("var regex = /\\[/;"); 881 verifyFormat("var regex = /\\\\[/]/;"); 882 verifyFormat("var regex = /}[\"]/;"); 883 verifyFormat("var regex = /}[/\"]/;"); 884 verifyFormat("var regex = /}[\"/]/;"); 885 886 verifyFormat("var regex = /\\b/;"); 887 verifyFormat("var regex = /\\B/;"); 888 verifyFormat("var regex = /\\d/;"); 889 verifyFormat("var regex = /\\D/;"); 890 verifyFormat("var regex = /\\f/;"); 891 verifyFormat("var regex = /\\n/;"); 892 verifyFormat("var regex = /\\r/;"); 893 verifyFormat("var regex = /\\s/;"); 894 verifyFormat("var regex = /\\S/;"); 895 verifyFormat("var regex = /\\t/;"); 896 verifyFormat("var regex = /\\v/;"); 897 verifyFormat("var regex = /\\w/;"); 898 verifyFormat("var regex = /\\W/;"); 899 verifyFormat("var regex = /a(a)\\1/;"); 900 verifyFormat("var regex = /\\0/;"); 901 verifyFormat("var regex = /\\\\/g;"); 902 verifyFormat("var regex = /\\a\\\\/g;"); 903 verifyFormat("var regex = /\a\\//g;"); 904 verifyFormat("var regex = /a\\//;\n" 905 "var x = 0;"); 906 verifyFormat("var regex = /'/g;", "var regex = /'/g ;"); 907 verifyFormat("var regex = /'/g; //'", "var regex = /'/g ; //'"); 908 verifyFormat("var regex = /\\/*/;\n" 909 "var x = 0;", 910 "var regex = /\\/*/;\n" 911 "var x=0;"); 912 verifyFormat("var x = /a\\//;", "var x = /a\\// \n;"); 913 verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16)); 914 verifyFormat("var regex =\n" 915 " /\"/;", 916 getGoogleJSStyleWithColumns(15)); 917 verifyFormat("var regex = //\n" 918 " /a/;"); 919 verifyFormat("var regexs = [\n" 920 " /d/, //\n" 921 " /aa/, //\n" 922 "];"); 923 } 924 925 TEST_F(FormatTestJS, RegexLiteralModifiers) { 926 verifyFormat("var regex = /abc/g;"); 927 verifyFormat("var regex = /abc/i;"); 928 verifyFormat("var regex = /abc/m;"); 929 verifyFormat("var regex = /abc/y;"); 930 } 931 932 TEST_F(FormatTestJS, RegexLiteralLength) { 933 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 934 getGoogleJSStyleWithColumns(60)); 935 verifyFormat("var regex =\n" 936 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 937 getGoogleJSStyleWithColumns(60)); 938 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 939 getGoogleJSStyleWithColumns(50)); 940 } 941 942 TEST_F(FormatTestJS, RegexLiteralExamples) { 943 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 944 } 945 946 TEST_F(FormatTestJS, TypeAnnotations) { 947 verifyFormat("var x: string;"); 948 verifyFormat("var x: {a: string; b: number;} = {};"); 949 verifyFormat("function x(): string {\n return 'x';\n}"); 950 verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}"); 951 verifyFormat("function x(y: string): string {\n return 'x';\n}"); 952 verifyFormat("for (var y: string in x) {\n x();\n}"); 953 verifyFormat("for (var y: string of x) {\n x();\n}"); 954 verifyFormat("function x(y: {a?: number;} = {}): number {\n" 955 " return 12;\n" 956 "}"); 957 verifyFormat("((a: string, b: number): string => a + b);"); 958 verifyFormat("var x: (y: number) => string;"); 959 verifyFormat("var x: P<string, (a: number) => string>;"); 960 verifyFormat("var x = {\n" 961 " y: function(): z {\n" 962 " return 1;\n" 963 " }\n" 964 "};"); 965 verifyFormat("var x = {\n" 966 " y: function(): {a: number} {\n" 967 " return 1;\n" 968 " }\n" 969 "};"); 970 verifyFormat("function someFunc(args: string[]):\n" 971 " {longReturnValue: string[]} {}", 972 getGoogleJSStyleWithColumns(60)); 973 } 974 975 TEST_F(FormatTestJS, UnionIntersectionTypes) { 976 verifyFormat("let x: A|B = A | B;"); 977 verifyFormat("let x: A&B|C = A & B;"); 978 verifyFormat("let x: Foo<A|B> = new Foo<A|B>();"); 979 verifyFormat("function(x: A|B): C&D {}"); 980 verifyFormat("function(x: A|B = A | B): C&D {}"); 981 verifyFormat("function x(path: number|string) {}"); 982 verifyFormat("function x(): string|number {}"); 983 verifyFormat("type Foo = Bar|Baz;"); 984 verifyFormat("type Foo = Bar<X>|Baz;"); 985 verifyFormat("type Foo = (Bar<X>|Baz);"); 986 verifyFormat("let x: Bar|Baz;"); 987 verifyFormat("let x: Bar<X>|Baz;"); 988 verifyFormat("let x: (Foo|Bar)[];"); 989 } 990 991 TEST_F(FormatTestJS, ClassDeclarations) { 992 verifyFormat("class C {\n x: string = 12;\n}"); 993 verifyFormat("class C {\n x(): string => 12;\n}"); 994 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); 995 verifyFormat("class C {\n private x: string = 12;\n}"); 996 verifyFormat("class C {\n private static x: string = 12;\n}"); 997 verifyFormat("class C {\n static x(): string {\n return 'asd';\n }\n}"); 998 verifyFormat("class C extends P implements I {}"); 999 verifyFormat("class C extends p.P implements i.I {}"); 1000 verifyFormat("class Test {\n" 1001 " aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n" 1002 " aaaaaaaaaaaaaaaaaaaaaa {}\n" 1003 "}"); 1004 verifyFormat("foo = class Name {\n" 1005 " constructor() {}\n" 1006 "};"); 1007 verifyFormat("foo = class {\n" 1008 " constructor() {}\n" 1009 "};"); 1010 verifyFormat("class C {\n" 1011 " x: {y: Z;} = {};\n" 1012 " private y: {y: Z;} = {};\n" 1013 "}"); 1014 1015 // ':' is not a type declaration here. 1016 verifyFormat("class X {\n" 1017 " subs = {\n" 1018 " 'b': {\n" 1019 " 'c': 1,\n" 1020 " },\n" 1021 " };\n" 1022 "}"); 1023 verifyFormat("@Component({\n" 1024 " moduleId: module.id,\n" 1025 "})\n" 1026 "class SessionListComponent implements OnDestroy, OnInit {\n" 1027 "}"); 1028 } 1029 1030 TEST_F(FormatTestJS, InterfaceDeclarations) { 1031 verifyFormat("interface I {\n" 1032 " x: string;\n" 1033 " enum: string[];\n" 1034 " enum?: string[];\n" 1035 "}\n" 1036 "var y;"); 1037 // Ensure that state is reset after parsing the interface. 1038 verifyFormat("interface a {}\n" 1039 "export function b() {}\n" 1040 "var x;"); 1041 1042 // Arrays of object type literals. 1043 verifyFormat("interface I {\n" 1044 " o: {}[];\n" 1045 "}"); 1046 } 1047 1048 TEST_F(FormatTestJS, EnumDeclarations) { 1049 verifyFormat("enum Foo {\n" 1050 " A = 1,\n" 1051 " B\n" 1052 "}"); 1053 verifyFormat("export /* somecomment*/ enum Foo {\n" 1054 " A = 1,\n" 1055 " B\n" 1056 "}"); 1057 verifyFormat("enum Foo {\n" 1058 " A = 1, // comment\n" 1059 " B\n" 1060 "}\n" 1061 "var x = 1;"); 1062 } 1063 1064 TEST_F(FormatTestJS, MetadataAnnotations) { 1065 verifyFormat("@A\nclass C {\n}"); 1066 verifyFormat("@A({arg: 'value'})\nclass C {\n}"); 1067 verifyFormat("@A\n@B\nclass C {\n}"); 1068 verifyFormat("class C {\n @A x: string;\n}"); 1069 verifyFormat("class C {\n" 1070 " @A\n" 1071 " private x(): string {\n" 1072 " return 'y';\n" 1073 " }\n" 1074 "}"); 1075 verifyFormat("class C {\n" 1076 " private x(@A x: string) {}\n" 1077 "}"); 1078 verifyFormat("class X {}\n" 1079 "class Y {}"); 1080 } 1081 1082 TEST_F(FormatTestJS, TypeAliases) { 1083 verifyFormat("type X = number;\n" 1084 "class C {}"); 1085 verifyFormat("type X<Y> = Z<Y>;"); 1086 verifyFormat("type X = {\n" 1087 " y: number\n" 1088 "};\n" 1089 "class C {}"); 1090 } 1091 1092 TEST_F(FormatTestJS, Modules) { 1093 verifyFormat("import SomeThing from 'some/module.js';"); 1094 verifyFormat("import {X, Y} from 'some/module.js';"); 1095 verifyFormat("import a, {X, Y} from 'some/module.js';"); 1096 verifyFormat("import {X, Y,} from 'some/module.js';"); 1097 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); 1098 // Ensure Automatic Semicolon Insertion does not break on "as\n". 1099 verifyFormat("import {X as myX} from 'm';", "import {X as\n" 1100 " myX} from 'm';"); 1101 verifyFormat("import * as lib from 'some/module.js';"); 1102 verifyFormat("var x = {import: 1};\nx.import = 2;"); 1103 1104 verifyFormat("export function fn() {\n" 1105 " return 'fn';\n" 1106 "}"); 1107 verifyFormat("export function A() {}\n" 1108 "export default function B() {}\n" 1109 "export function C() {}"); 1110 verifyFormat("export default () => {\n" 1111 " let x = 1;\n" 1112 " return x;\n" 1113 "}"); 1114 verifyFormat("export const x = 12;"); 1115 verifyFormat("export default class X {}"); 1116 verifyFormat("export {X, Y} from 'some/module.js';"); 1117 verifyFormat("export {X, Y,} from 'some/module.js';"); 1118 verifyFormat("export {SomeVeryLongExport as X, " 1119 "SomeOtherVeryLongExport as Y} from 'some/module.js';"); 1120 // export without 'from' is wrapped. 1121 verifyFormat("export let someRatherLongVariableName =\n" 1122 " someSurprisinglyLongVariable + someOtherRatherLongVar;"); 1123 // ... but not if from is just an identifier. 1124 verifyFormat("export {\n" 1125 " from as from,\n" 1126 " someSurprisinglyLongVariable as\n" 1127 " from\n" 1128 "};", 1129 getGoogleJSStyleWithColumns(20)); 1130 verifyFormat("export class C {\n" 1131 " x: number;\n" 1132 " y: string;\n" 1133 "}"); 1134 verifyFormat("export class X { y: number; }"); 1135 verifyFormat("export abstract class X { y: number; }"); 1136 verifyFormat("export default class X { y: number }"); 1137 verifyFormat("export default function() {\n return 1;\n}"); 1138 verifyFormat("export var x = 12;"); 1139 verifyFormat("class C {}\n" 1140 "export function f() {}\n" 1141 "var v;"); 1142 verifyFormat("export var x: number = 12;"); 1143 verifyFormat("export const y = {\n" 1144 " a: 1,\n" 1145 " b: 2\n" 1146 "};"); 1147 verifyFormat("export enum Foo {\n" 1148 " BAR,\n" 1149 " // adsdasd\n" 1150 " BAZ\n" 1151 "}"); 1152 verifyFormat("export default [\n" 1153 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1154 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 1155 "];"); 1156 verifyFormat("export default [];"); 1157 verifyFormat("export default () => {};"); 1158 verifyFormat("export interface Foo { foo: number; }\n" 1159 "export class Bar {\n" 1160 " blah(): string {\n" 1161 " return this.blah;\n" 1162 " };\n" 1163 "}"); 1164 } 1165 1166 TEST_F(FormatTestJS, ImportWrapping) { 1167 verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying," 1168 " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying" 1169 "} from 'some/module.js';"); 1170 FormatStyle Style = getGoogleJSStyleWithColumns(80); 1171 Style.JavaScriptWrapImports = true; 1172 verifyFormat("import {\n" 1173 " VeryLongImportsAreAnnoying,\n" 1174 " VeryLongImportsAreAnnoying,\n" 1175 " VeryLongImportsAreAnnoying,\n" 1176 "} from 'some/module.js';", 1177 Style); 1178 verifyFormat("import {\n" 1179 " A,\n" 1180 " A,\n" 1181 "} from 'some/module.js';", 1182 Style); 1183 verifyFormat("export {\n" 1184 " A,\n" 1185 " A,\n" 1186 "} from 'some/module.js';", 1187 Style); 1188 } 1189 1190 TEST_F(FormatTestJS, TemplateStrings) { 1191 // Keeps any whitespace/indentation within the template string. 1192 verifyFormat("var x = `hello\n" 1193 " ${name}\n" 1194 " !`;", 1195 "var x = `hello\n" 1196 " ${ name }\n" 1197 " !`;"); 1198 1199 verifyFormat("var x =\n" 1200 " `hello ${world}` >= some();", 1201 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. 1202 verifyFormat("var x = `hello ${world}` >= some();", 1203 getGoogleJSStyleWithColumns(35)); // Barely fits. 1204 verifyFormat("var x = `hellö ${wörld}` >= söme();", 1205 getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8. 1206 verifyFormat("var x = `hello\n" 1207 " ${world}` >=\n" 1208 " some();", 1209 "var x =\n" 1210 " `hello\n" 1211 " ${world}` >= some();", 1212 getGoogleJSStyleWithColumns(21)); // Barely doesn't fit. 1213 verifyFormat("var x = `hello\n" 1214 " ${world}` >= some();", 1215 "var x =\n" 1216 " `hello\n" 1217 " ${world}` >= some();", 1218 getGoogleJSStyleWithColumns(22)); // Barely fits. 1219 1220 verifyFormat("var x =\n" 1221 " `h`;", 1222 getGoogleJSStyleWithColumns(11)); 1223 verifyFormat("var x =\n `multi\n line`;", "var x = `multi\n line`;", 1224 getGoogleJSStyleWithColumns(13)); 1225 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1226 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);"); 1227 // Repro for an obscure width-miscounting issue with template strings. 1228 verifyFormat( 1229 "someLongVariable =\n" 1230 " " 1231 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;", 1232 "someLongVariable = " 1233 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;"); 1234 1235 // Make sure template strings get a proper ColumnWidth assigned, even if they 1236 // are first token in line. 1237 verifyFormat( 1238 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1239 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); 1240 1241 // Two template strings. 1242 verifyFormat("var x = `hello` == `hello`;"); 1243 1244 // Comments in template strings. 1245 verifyFormat("var x = `//a`;\n" 1246 "var y;", 1247 "var x =\n `//a`;\n" 1248 "var y ;"); 1249 verifyFormat("var x = `/*a`;\n" 1250 "var y;", 1251 "var x =\n `/*a`;\n" 1252 "var y;"); 1253 // Unterminated string literals in a template string. 1254 verifyFormat("var x = `'`; // comment with matching quote '\n" 1255 "var y;"); 1256 verifyFormat("var x = `\"`; // comment with matching quote \"\n" 1257 "var y;"); 1258 verifyFormat("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa);", 1259 "it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa) ;", 1260 getGoogleJSStyleWithColumns(40)); 1261 // Backticks in a comment - not a template string. 1262 verifyFormat("var x = 1 // `/*a`;\n" 1263 " ;", 1264 "var x =\n 1 // `/*a`;\n" 1265 " ;"); 1266 verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */"); 1267 // Comment spans multiple template strings. 1268 verifyFormat("var x = `/*a`;\n" 1269 "var y = ` */ `;", 1270 "var x =\n `/*a`;\n" 1271 "var y =\n ` */ `;"); 1272 // Escaped backtick. 1273 verifyFormat("var x = ` \\` a`;\n" 1274 "var y;", 1275 "var x = ` \\` a`;\n" 1276 "var y;"); 1277 // Escaped dollar. 1278 verifyFormat("var x = ` \\${foo}`;\n"); 1279 } 1280 1281 TEST_F(FormatTestJS, TemplateStringASI) { 1282 verifyFormat("var x = `hello${world}`;", "var x = `hello${\n" 1283 " world\n" 1284 "}`;"); 1285 } 1286 1287 TEST_F(FormatTestJS, NestedTemplateStrings) { 1288 verifyFormat( 1289 "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;"); 1290 verifyFormat("var x = `he${({text: 'll'}.text)}o`;"); 1291 1292 // Crashed at some point. 1293 verifyFormat("}"); 1294 } 1295 1296 TEST_F(FormatTestJS, TaggedTemplateStrings) { 1297 verifyFormat("var x = html`<ul>`;"); 1298 } 1299 1300 TEST_F(FormatTestJS, CastSyntax) { 1301 verifyFormat("var x = <type>foo;"); 1302 verifyFormat("var x = foo as type;"); 1303 verifyFormat("let x = (a + b) as\n" 1304 " LongTypeIsLong;", 1305 getGoogleJSStyleWithColumns(20)); 1306 verifyFormat("foo = <Bar[]>[\n" 1307 " 1, //\n" 1308 " 2\n" 1309 "];"); 1310 verifyFormat("var x = [{x: 1} as type];"); 1311 verifyFormat("x = x as [a, b];"); 1312 verifyFormat("x = x as {a: string};"); 1313 verifyFormat("x = x as (string);"); 1314 verifyFormat("x = x! as (string);"); 1315 } 1316 1317 TEST_F(FormatTestJS, TypeArguments) { 1318 verifyFormat("class X<Y> {}"); 1319 verifyFormat("new X<Y>();"); 1320 verifyFormat("foo<Y>(a);"); 1321 verifyFormat("var x: X<Y>[];"); 1322 verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); 1323 verifyFormat("function f(a: List<any> = null) {}"); 1324 verifyFormat("function f(): List<any> {}"); 1325 verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n" 1326 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}"); 1327 verifyFormat("function aaaaaaaaaa(\n" 1328 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n" 1329 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n" 1330 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}"); 1331 } 1332 1333 TEST_F(FormatTestJS, UserDefinedTypeGuards) { 1334 verifyFormat( 1335 "function foo(check: Object):\n" 1336 " check is {foo: string, bar: string, baz: string, foobar: string} {\n" 1337 " return 'bar' in check;\n" 1338 "}\n"); 1339 } 1340 1341 TEST_F(FormatTestJS, OptionalTypes) { 1342 verifyFormat("function x(a?: b, c?, d?) {}"); 1343 verifyFormat("class X {\n" 1344 " y?: z;\n" 1345 " z?;\n" 1346 "}"); 1347 verifyFormat("interface X {\n" 1348 " y?(): z;\n" 1349 "}"); 1350 verifyFormat("constructor({aa}: {\n" 1351 " aa?: string,\n" 1352 " aaaaaaaa?: string,\n" 1353 " aaaaaaaaaaaaaaa?: boolean,\n" 1354 " aaaaaa?: List<string>\n" 1355 "}) {}"); 1356 } 1357 1358 TEST_F(FormatTestJS, IndexSignature) { 1359 verifyFormat("var x: {[k: string]: v};"); 1360 } 1361 1362 TEST_F(FormatTestJS, WrapAfterParen) { 1363 verifyFormat("xxxxxxxxxxx(\n" 1364 " aaa, aaa);", 1365 getGoogleJSStyleWithColumns(20)); 1366 verifyFormat("xxxxxxxxxxx(\n" 1367 " aaa, aaa, aaa,\n" 1368 " aaa, aaa, aaa);", 1369 getGoogleJSStyleWithColumns(20)); 1370 verifyFormat("xxxxxxxxxxx(\n" 1371 " aaaaaaaaaaaaaaaaaaaaaaaa,\n" 1372 " function(x) {\n" 1373 " y(); //\n" 1374 " });", 1375 getGoogleJSStyleWithColumns(40)); 1376 verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 1377 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 1378 } 1379 1380 TEST_F(FormatTestJS, JSDocAnnotations) { 1381 verifyFormat("/**\n" 1382 " * @export {this.is.a.long.path.to.a.Type}\n" 1383 " */", 1384 "/**\n" 1385 " * @export {this.is.a.long.path.to.a.Type}\n" 1386 " */", 1387 getGoogleJSStyleWithColumns(20)); 1388 } 1389 1390 TEST_F(FormatTestJS, RequoteStringsSingle) { 1391 verifyFormat("var x = 'foo';", "var x = \"foo\";"); 1392 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";"); 1393 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";"); 1394 verifyFormat( 1395 "var x =\n" 1396 " 'foo\\'';", 1397 // Code below is 15 chars wide, doesn't fit into the line with the 1398 // \ escape added. 1399 "var x = \"foo'\";", getGoogleJSStyleWithColumns(15)); 1400 // Removes no-longer needed \ escape from ". 1401 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";"); 1402 // Code below fits into 15 chars *after* removing the \ escape. 1403 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";", 1404 getGoogleJSStyleWithColumns(15)); 1405 verifyFormat("// clang-format off\n" 1406 "let x = \"double\";\n" 1407 "// clang-format on\n" 1408 "let x = 'single';\n", 1409 "// clang-format off\n" 1410 "let x = \"double\";\n" 1411 "// clang-format on\n" 1412 "let x = \"single\";\n"); 1413 } 1414 1415 TEST_F(FormatTestJS, RequoteAndIndent) { 1416 verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n" 1417 " 'double quoted string that needs wrapping');", 1418 "let x = someVeryLongFunctionThatGoesOnAndOn(" 1419 "\"double quoted string that needs wrapping\");"); 1420 1421 verifyFormat("let x =\n" 1422 " 'foo\\'oo';\n" 1423 "let x =\n" 1424 " 'foo\\'oo';", 1425 "let x=\"foo'oo\";\n" 1426 "let x=\"foo'oo\";", 1427 getGoogleJSStyleWithColumns(15)); 1428 } 1429 1430 TEST_F(FormatTestJS, RequoteStringsDouble) { 1431 FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1432 DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double; 1433 verifyFormat("var x = \"foo\";", DoubleQuotes); 1434 verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes); 1435 verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes); 1436 } 1437 1438 TEST_F(FormatTestJS, RequoteStringsLeave) { 1439 FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1440 LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave; 1441 verifyFormat("var x = \"foo\";", LeaveQuotes); 1442 verifyFormat("var x = 'foo';", LeaveQuotes); 1443 } 1444 1445 TEST_F(FormatTestJS, SupportShebangLines) { 1446 verifyFormat("#!/usr/bin/env node\n" 1447 "var x = hello();", 1448 "#!/usr/bin/env node\n" 1449 "var x = hello();"); 1450 } 1451 1452 TEST_F(FormatTestJS, NonNullAssertionOperator) { 1453 verifyFormat("let x = foo!.bar();\n"); 1454 verifyFormat("let x = foo ? bar! : baz;\n"); 1455 verifyFormat("let x = !foo;\n"); 1456 verifyFormat("let x = foo[0]!;\n"); 1457 verifyFormat("let x = (foo)!;\n"); 1458 verifyFormat("let x = {foo: 1}!;\n"); 1459 } 1460 1461 TEST_F(FormatTestJS, Conditional) { 1462 verifyFormat("y = x ? 1 : 2;"); 1463 verifyFormat("x ? 1 : 2;"); 1464 verifyFormat("class Foo {\n" 1465 " field = true ? 1 : 2;\n" 1466 " method(a = true ? 1 : 2) {}\n" 1467 "}"); 1468 } 1469 1470 TEST_F(FormatTestJS, ImportComments) { 1471 verifyFormat("import {x} from 'x'; // from some location", 1472 getGoogleJSStyleWithColumns(25)); 1473 verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10)); 1474 } 1475 1476 } // end namespace tooling 1477 } // end namespace clang 1478