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