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