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 FormattingAttemptStatus Status; 28 tooling::Replacements Replaces = 29 reformat(Style, Code, Ranges, "<stdin>", &Status); 30 EXPECT_TRUE(Status.FormatComplete); 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, JSDocComments) { 71 // Break the first line of a multiline jsdoc comment. 72 EXPECT_EQ("/**\n" 73 " * jsdoc line 1\n" 74 " * jsdoc line 2\n" 75 " */", 76 format("/** jsdoc line 1\n" 77 " * jsdoc line 2\n" 78 " */", 79 getGoogleJSStyleWithColumns(20))); 80 // Both break after '/**' and break the line itself. 81 EXPECT_EQ("/**\n" 82 " * jsdoc line long\n" 83 " * long jsdoc line 2\n" 84 " */", 85 format("/** jsdoc line long long\n" 86 " * jsdoc line 2\n" 87 " */", 88 getGoogleJSStyleWithColumns(20))); 89 // Break a short first line if the ending '*/' is on a newline. 90 EXPECT_EQ("/**\n" 91 " * jsdoc line 1\n" 92 " */", 93 format("/** jsdoc line 1\n" 94 " */", getGoogleJSStyleWithColumns(20))); 95 // Don't break the first line of a short single line jsdoc comment. 96 EXPECT_EQ("/** jsdoc line 1 */", 97 format("/** jsdoc line 1 */", getGoogleJSStyleWithColumns(20))); 98 // Don't break the first line of a single line jsdoc comment if it just fits 99 // the column limit. 100 EXPECT_EQ("/** jsdoc line 12 */", 101 format("/** jsdoc line 12 */", getGoogleJSStyleWithColumns(20))); 102 // Don't break after '/**' and before '*/' if there is no space between 103 // '/**' and the content. 104 EXPECT_EQ( 105 "/*** nonjsdoc long\n" 106 " * line */", 107 format("/*** nonjsdoc long line */", getGoogleJSStyleWithColumns(20))); 108 EXPECT_EQ( 109 "/**strange long long\n" 110 " * line */", 111 format("/**strange long long line */", getGoogleJSStyleWithColumns(20))); 112 // Break the first line of a single line jsdoc comment if it just exceeds the 113 // column limit. 114 EXPECT_EQ("/**\n" 115 " * jsdoc line 123\n" 116 " */", 117 format("/** jsdoc line 123 */", getGoogleJSStyleWithColumns(20))); 118 // Break also if the leading indent of the first line is more than 1 column. 119 EXPECT_EQ("/**\n" 120 " * jsdoc line 123\n" 121 " */", 122 format("/** jsdoc line 123 */", getGoogleJSStyleWithColumns(20))); 123 // Break also if the leading indent of the first line is more than 1 column. 124 EXPECT_EQ("/**\n" 125 " * jsdoc line 123\n" 126 " */", 127 format("/** jsdoc line 123 */", getGoogleJSStyleWithColumns(20))); 128 // Break after the content of the last line. 129 EXPECT_EQ("/**\n" 130 " * line 1\n" 131 " * line 2\n" 132 " */", 133 format("/**\n" 134 " * line 1\n" 135 " * line 2 */", 136 getGoogleJSStyleWithColumns(20))); 137 // Break both the content and after the content of the last line. 138 EXPECT_EQ("/**\n" 139 " * line 1\n" 140 " * line long long\n" 141 " * long\n" 142 " */", 143 format("/**\n" 144 " * line 1\n" 145 " * line long long long */", 146 getGoogleJSStyleWithColumns(20))); 147 148 // The comment block gets indented. 149 EXPECT_EQ("function f() {\n" 150 " /**\n" 151 " * comment about\n" 152 " * x\n" 153 " */\n" 154 " var x = 1;\n" 155 "}", 156 format("function f() {\n" 157 "/** comment about x */\n" 158 "var x = 1;\n" 159 "}", 160 getGoogleJSStyleWithColumns(20))); 161 } 162 163 TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) { 164 verifyFormat("a == = b;"); 165 verifyFormat("a != = b;"); 166 167 verifyFormat("a === b;"); 168 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10)); 169 verifyFormat("a !== b;"); 170 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10)); 171 verifyFormat("if (a + b + c +\n" 172 " d !==\n" 173 " e + f + g)\n" 174 " q();", 175 getGoogleJSStyleWithColumns(20)); 176 177 verifyFormat("a >> >= b;"); 178 179 verifyFormat("a >>> b;"); 180 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10)); 181 verifyFormat("a >>>= b;"); 182 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10)); 183 verifyFormat("if (a + b + c +\n" 184 " d >>>\n" 185 " e + f + g)\n" 186 " q();", 187 getGoogleJSStyleWithColumns(20)); 188 verifyFormat("var x = aaaaaaaaaa ?\n" 189 " bbbbbb :\n" 190 " ccc;", 191 getGoogleJSStyleWithColumns(20)); 192 193 verifyFormat("var b = a.map((x) => x + 1);"); 194 verifyFormat("return ('aaa') in bbbb;"); 195 verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n" 196 " aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 197 FormatStyle Style = getGoogleJSStyleWithColumns(80); 198 Style.AlignOperands = true; 199 verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n" 200 " aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 201 Style); 202 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 203 verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa()\n" 204 " in aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 205 Style); 206 207 // ES6 spread operator. 208 verifyFormat("someFunction(...a);"); 209 verifyFormat("var x = [1, ...a, 2];"); 210 } 211 212 TEST_F(FormatTestJS, UnderstandsAmpAmp) { 213 verifyFormat("e && e.SomeFunction();"); 214 } 215 216 TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) { 217 verifyFormat("not.and.or.not_eq = 1;"); 218 } 219 220 TEST_F(FormatTestJS, ReservedWords) { 221 // JavaScript reserved words (aka keywords) are only illegal when used as 222 // Identifiers, but are legal as IdentifierNames. 223 verifyFormat("x.class.struct = 1;"); 224 verifyFormat("x.case = 1;"); 225 verifyFormat("x.interface = 1;"); 226 verifyFormat("x.for = 1;"); 227 verifyFormat("x.of() = 1;"); 228 verifyFormat("of(null);"); 229 verifyFormat("import {of} from 'x';"); 230 verifyFormat("x.in() = 1;"); 231 verifyFormat("x.let() = 1;"); 232 verifyFormat("x.var() = 1;"); 233 verifyFormat("x.for() = 1;"); 234 verifyFormat("x.as() = 1;"); 235 verifyFormat("x = {\n" 236 " a: 12,\n" 237 " interface: 1,\n" 238 " switch: 1,\n" 239 "};"); 240 verifyFormat("var struct = 2;"); 241 verifyFormat("var union = 2;"); 242 verifyFormat("var interface = 2;"); 243 verifyFormat("interface = 2;"); 244 verifyFormat("x = interface instanceof y;"); 245 } 246 247 TEST_F(FormatTestJS, ReservedWordsMethods) { 248 verifyFormat( 249 "class X {\n" 250 " delete() {\n" 251 " x();\n" 252 " }\n" 253 " interface() {\n" 254 " x();\n" 255 " }\n" 256 " let() {\n" 257 " x();\n" 258 " }\n" 259 "}\n"); 260 } 261 262 TEST_F(FormatTestJS, ReservedWordsParenthesized) { 263 // All of these are statements using the keyword, not function calls. 264 verifyFormat("throw (x + y);\n" 265 "await (await x).y;\n" 266 "void (0);\n" 267 "delete (x.y);\n" 268 "return (x);\n"); 269 } 270 271 TEST_F(FormatTestJS, CppKeywords) { 272 // Make sure we don't mess stuff up because of C++ keywords. 273 verifyFormat("return operator && (aa);"); 274 // .. or QT ones. 275 verifyFormat("slots: Slot[];"); 276 } 277 278 TEST_F(FormatTestJS, ES6DestructuringAssignment) { 279 verifyFormat("var [a, b, c] = [1, 2, 3];"); 280 verifyFormat("const [a, b, c] = [1, 2, 3];"); 281 verifyFormat("let [a, b, c] = [1, 2, 3];"); 282 verifyFormat("var {a, b} = {a: 1, b: 2};"); 283 verifyFormat("let {a, b} = {a: 1, b: 2};"); 284 } 285 286 TEST_F(FormatTestJS, ContainerLiterals) { 287 verifyFormat("var x = {\n" 288 " y: function(a) {\n" 289 " return a;\n" 290 " }\n" 291 "};"); 292 verifyFormat("return {\n" 293 " link: function() {\n" 294 " f(); //\n" 295 " }\n" 296 "};"); 297 verifyFormat("return {\n" 298 " a: a,\n" 299 " link: function() {\n" 300 " f(); //\n" 301 " }\n" 302 "};"); 303 verifyFormat("return {\n" 304 " a: a,\n" 305 " link: function() {\n" 306 " f(); //\n" 307 " },\n" 308 " link: function() {\n" 309 " f(); //\n" 310 " }\n" 311 "};"); 312 verifyFormat("var stuff = {\n" 313 " // comment for update\n" 314 " update: false,\n" 315 " // comment for modules\n" 316 " modules: false,\n" 317 " // comment for tasks\n" 318 " tasks: false\n" 319 "};"); 320 verifyFormat("return {\n" 321 " 'finish':\n" 322 " //\n" 323 " a\n" 324 "};"); 325 verifyFormat("var obj = {\n" 326 " fooooooooo: function(x) {\n" 327 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 328 " }\n" 329 "};"); 330 // Simple object literal, as opposed to enum style below. 331 verifyFormat("var obj = {a: 123};"); 332 // Enum style top level assignment. 333 verifyFormat("X = {\n a: 123\n};"); 334 verifyFormat("X.Y = {\n a: 123\n};"); 335 // But only on the top level, otherwise its a plain object literal assignment. 336 verifyFormat("function x() {\n" 337 " y = {z: 1};\n" 338 "}"); 339 verifyFormat("x = foo && {a: 123};"); 340 341 // Arrow functions in object literals. 342 verifyFormat("var x = {\n" 343 " y: (a) => {\n" 344 " return a;\n" 345 " }\n" 346 "};"); 347 verifyFormat("var x = {y: (a) => a};"); 348 349 // Methods in object literals. 350 verifyFormat("var x = {\n" 351 " y(a: string): number {\n" 352 " return a;\n" 353 " }\n" 354 "};"); 355 verifyFormat("var x = {\n" 356 " y(a: string) {\n" 357 " return a;\n" 358 " }\n" 359 "};"); 360 361 // Computed keys. 362 verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};"); 363 verifyFormat("var x = {\n" 364 " [a]: 1,\n" 365 " b: 2,\n" 366 " [c]: 3,\n" 367 "};"); 368 369 // Object literals can leave out labels. 370 verifyFormat("f({a}, () => {\n" 371 " g(); //\n" 372 "});"); 373 374 // Keys can be quoted. 375 verifyFormat("var x = {\n" 376 " a: a,\n" 377 " b: b,\n" 378 " 'c': c,\n" 379 "};"); 380 381 // Dict literals can skip the label names. 382 verifyFormat("var x = {\n" 383 " aaa,\n" 384 " aaa,\n" 385 " aaa,\n" 386 "};"); 387 verifyFormat("return {\n" 388 " a,\n" 389 " b: 'b',\n" 390 " c,\n" 391 "};"); 392 } 393 394 TEST_F(FormatTestJS, MethodsInObjectLiterals) { 395 verifyFormat("var o = {\n" 396 " value: 'test',\n" 397 " get value() { // getter\n" 398 " return this.value;\n" 399 " }\n" 400 "};"); 401 verifyFormat("var o = {\n" 402 " value: 'test',\n" 403 " set value(val) { // setter\n" 404 " this.value = val;\n" 405 " }\n" 406 "};"); 407 verifyFormat("var o = {\n" 408 " value: 'test',\n" 409 " someMethod(val) { // method\n" 410 " doSomething(this.value + val);\n" 411 " }\n" 412 "};"); 413 verifyFormat("var o = {\n" 414 " someMethod(val) { // method\n" 415 " doSomething(this.value + val);\n" 416 " },\n" 417 " someOtherMethod(val) { // method\n" 418 " doSomething(this.value + val);\n" 419 " }\n" 420 "};"); 421 } 422 423 TEST_F(FormatTestJS, GettersSettersVisibilityKeywords) { 424 // Don't break after "protected" 425 verifyFormat("class X {\n" 426 " protected get getter():\n" 427 " number {\n" 428 " return 1;\n" 429 " }\n" 430 "}", 431 getGoogleJSStyleWithColumns(12)); 432 // Don't break after "get" 433 verifyFormat("class X {\n" 434 " protected get someReallyLongGetterName():\n" 435 " number {\n" 436 " return 1;\n" 437 " }\n" 438 "}", 439 getGoogleJSStyleWithColumns(40)); 440 } 441 442 TEST_F(FormatTestJS, SpacesInContainerLiterals) { 443 verifyFormat("var arr = [1, 2, 3];"); 444 verifyFormat("f({a: 1, b: 2, c: 3});"); 445 446 verifyFormat("var object_literal_with_long_name = {\n" 447 " a: 'aaaaaaaaaaaaaaaaaa',\n" 448 " b: 'bbbbbbbbbbbbbbbbbb'\n" 449 "};"); 450 451 verifyFormat("f({a: 1, b: 2, c: 3});", 452 getChromiumStyle(FormatStyle::LK_JavaScript)); 453 verifyFormat("f({'a': [{}]});"); 454 } 455 456 TEST_F(FormatTestJS, SingleQuotedStrings) { 457 verifyFormat("this.function('', true);"); 458 } 459 460 TEST_F(FormatTestJS, GoogScopes) { 461 verifyFormat("goog.scope(function() {\n" 462 "var x = a.b;\n" 463 "var y = c.d;\n" 464 "}); // goog.scope"); 465 verifyFormat("goog.scope(function() {\n" 466 "// test\n" 467 "var x = 0;\n" 468 "// test\n" 469 "});"); 470 } 471 472 TEST_F(FormatTestJS, IIFEs) { 473 // Internal calling parens; no semi. 474 verifyFormat("(function() {\n" 475 "var a = 1;\n" 476 "}())"); 477 // External calling parens; no semi. 478 verifyFormat("(function() {\n" 479 "var b = 2;\n" 480 "})()"); 481 // Internal calling parens; with semi. 482 verifyFormat("(function() {\n" 483 "var c = 3;\n" 484 "}());"); 485 // External calling parens; with semi. 486 verifyFormat("(function() {\n" 487 "var d = 4;\n" 488 "})();"); 489 } 490 491 TEST_F(FormatTestJS, GoogModules) { 492 verifyFormat("goog.module('this.is.really.absurdly.long');", 493 getGoogleJSStyleWithColumns(40)); 494 verifyFormat("goog.require('this.is.really.absurdly.long');", 495 getGoogleJSStyleWithColumns(40)); 496 verifyFormat("goog.provide('this.is.really.absurdly.long');", 497 getGoogleJSStyleWithColumns(40)); 498 verifyFormat("var long = goog.require('this.is.really.absurdly.long');", 499 getGoogleJSStyleWithColumns(40)); 500 verifyFormat("goog.setTestOnly('this.is.really.absurdly.long');", 501 getGoogleJSStyleWithColumns(40)); 502 verifyFormat("goog.forwardDeclare('this.is.really.absurdly.long');", 503 getGoogleJSStyleWithColumns(40)); 504 505 // These should be wrapped normally. 506 verifyFormat( 507 "var MyLongClassName =\n" 508 " goog.module.get('my.long.module.name.followedBy.MyLongClassName');"); 509 } 510 511 TEST_F(FormatTestJS, FormatsNamespaces) { 512 verifyFormat("namespace Foo {\n" 513 " export let x = 1;\n" 514 "}\n"); 515 verifyFormat("declare namespace Foo {\n" 516 " export let x: number;\n" 517 "}\n"); 518 } 519 520 TEST_F(FormatTestJS, NamespacesMayNotWrap) { 521 verifyFormat("declare namespace foobarbaz {\n" 522 "}\n", getGoogleJSStyleWithColumns(18)); 523 verifyFormat("declare module foobarbaz {\n" 524 "}\n", getGoogleJSStyleWithColumns(15)); 525 verifyFormat("namespace foobarbaz {\n" 526 "}\n", getGoogleJSStyleWithColumns(10)); 527 verifyFormat("module foobarbaz {\n" 528 "}\n", getGoogleJSStyleWithColumns(7)); 529 } 530 531 TEST_F(FormatTestJS, AmbientDeclarations) { 532 FormatStyle NineCols = getGoogleJSStyleWithColumns(9); 533 verifyFormat( 534 "declare class\n" 535 " X {}", 536 NineCols); 537 verifyFormat( 538 "declare function\n" 539 "x();", // TODO(martinprobst): should ideally be indented. 540 NineCols); 541 verifyFormat("declare function foo();\n" 542 "let x = 1;\n"); 543 verifyFormat("declare function foo(): string;\n" 544 "let x = 1;\n"); 545 verifyFormat("declare function foo(): {x: number};\n" 546 "let x = 1;\n"); 547 verifyFormat("declare class X {}\n" 548 "let x = 1;\n"); 549 verifyFormat("declare interface Y {}\n" 550 "let x = 1;\n"); 551 verifyFormat( 552 "declare enum X {\n" 553 "}", 554 NineCols); 555 verifyFormat( 556 "declare let\n" 557 " x: number;", 558 NineCols); 559 } 560 561 TEST_F(FormatTestJS, FormatsFreestandingFunctions) { 562 verifyFormat("function outer1(a, b) {\n" 563 " function inner1(a, b) {\n" 564 " return a;\n" 565 " }\n" 566 " inner1(a, b);\n" 567 "}\n" 568 "function outer2(a, b) {\n" 569 " function inner2(a, b) {\n" 570 " return a;\n" 571 " }\n" 572 " inner2(a, b);\n" 573 "}"); 574 verifyFormat("function f() {}"); 575 verifyFormat("function aFunction() {}\n" 576 "(function f() {\n" 577 " var x = 1;\n" 578 "}());\n"); 579 verifyFormat("function aFunction() {}\n" 580 "{\n" 581 " let x = 1;\n" 582 " console.log(x);\n" 583 "}\n"); 584 } 585 586 TEST_F(FormatTestJS, GeneratorFunctions) { 587 verifyFormat("function* f() {\n" 588 " let x = 1;\n" 589 " yield x;\n" 590 " yield* something();\n" 591 " yield [1, 2];\n" 592 " yield {a: 1};\n" 593 "}"); 594 verifyFormat("function*\n" 595 " f() {\n" 596 "}", 597 getGoogleJSStyleWithColumns(8)); 598 verifyFormat("export function* f() {\n" 599 " yield 1;\n" 600 "}\n"); 601 verifyFormat("class X {\n" 602 " * generatorMethod() {\n" 603 " yield x;\n" 604 " }\n" 605 "}"); 606 verifyFormat("var x = {\n" 607 " a: function*() {\n" 608 " //\n" 609 " }\n" 610 "}\n"); 611 } 612 613 TEST_F(FormatTestJS, AsyncFunctions) { 614 verifyFormat("async function f() {\n" 615 " let x = 1;\n" 616 " return fetch(x);\n" 617 "}"); 618 verifyFormat("async function f() {\n" 619 " return 1;\n" 620 "}\n" 621 "\n" 622 "function a() {\n" 623 " return 1;\n" 624 "}\n", 625 " async function f() {\n" 626 " return 1;\n" 627 "}\n" 628 "\n" 629 " function a() {\n" 630 " return 1;\n" 631 "} \n"); 632 verifyFormat("async function* f() {\n" 633 " yield fetch(x);\n" 634 "}"); 635 verifyFormat("export async function f() {\n" 636 " return fetch(x);\n" 637 "}"); 638 verifyFormat("let x = async () => f();"); 639 verifyFormat("let x = async function() {\n" 640 " f();\n" 641 "};"); 642 verifyFormat("let x = async();"); 643 verifyFormat("class X {\n" 644 " async asyncMethod() {\n" 645 " return fetch(1);\n" 646 " }\n" 647 "}"); 648 verifyFormat("function initialize() {\n" 649 " // Comment.\n" 650 " return async.then();\n" 651 "}\n"); 652 verifyFormat("for await (const x of y) {\n" 653 " console.log(x);\n" 654 "}\n"); 655 verifyFormat("function asyncLoop() {\n" 656 " for await (const x of y) {\n" 657 " console.log(x);\n" 658 " }\n" 659 "}\n"); 660 } 661 662 TEST_F(FormatTestJS, FunctionParametersTrailingComma) { 663 verifyFormat("function trailingComma(\n" 664 " p1,\n" 665 " p2,\n" 666 " p3,\n" 667 ") {\n" 668 " a; //\n" 669 "}\n", 670 "function trailingComma(p1, p2, p3,) {\n" 671 " a; //\n" 672 "}\n"); 673 verifyFormat("trailingComma(\n" 674 " p1,\n" 675 " p2,\n" 676 " p3,\n" 677 ");\n", 678 "trailingComma(p1, p2, p3,);\n"); 679 verifyFormat("trailingComma(\n" 680 " p1 // hello\n" 681 ");\n", 682 "trailingComma(p1 // hello\n" 683 ");\n"); 684 } 685 686 TEST_F(FormatTestJS, ArrayLiterals) { 687 verifyFormat("var aaaaa: List<SomeThing> =\n" 688 " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];"); 689 verifyFormat("return [\n" 690 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 691 " ccccccccccccccccccccccccccc\n" 692 "];"); 693 verifyFormat("return [\n" 694 " aaaa().bbbbbbbb('A'),\n" 695 " aaaa().bbbbbbbb('B'),\n" 696 " aaaa().bbbbbbbb('C'),\n" 697 "];"); 698 verifyFormat("var someVariable = SomeFunction([\n" 699 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 700 " ccccccccccccccccccccccccccc\n" 701 "]);"); 702 verifyFormat("var someVariable = SomeFunction([\n" 703 " [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n" 704 "]);", 705 getGoogleJSStyleWithColumns(51)); 706 verifyFormat("var someVariable = SomeFunction(aaaa, [\n" 707 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 708 " ccccccccccccccccccccccccccc\n" 709 "]);"); 710 verifyFormat("var someVariable = SomeFunction(\n" 711 " aaaa,\n" 712 " [\n" 713 " aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 714 " cccccccccccccccccccccccccc\n" 715 " ],\n" 716 " aaaa);"); 717 verifyFormat("var aaaa = aaaaa || // wrap\n" 718 " [];"); 719 720 verifyFormat("someFunction([], {a: a});"); 721 722 verifyFormat("var string = [\n" 723 " 'aaaaaa',\n" 724 " 'bbbbbb',\n" 725 "].join('+');"); 726 } 727 728 TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) { 729 verifyFormat("var array = [\n" 730 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 731 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 732 "];"); 733 verifyFormat("var array = someFunction([\n" 734 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 735 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 736 "]);"); 737 } 738 739 TEST_F(FormatTestJS, FunctionLiterals) { 740 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 741 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 742 verifyFormat("doFoo(function() {});"); 743 verifyFormat("doFoo(function() { return 1; });", Style); 744 verifyFormat("var func = function() {\n" 745 " return 1;\n" 746 "};"); 747 verifyFormat("var func = //\n" 748 " function() {\n" 749 " return 1;\n" 750 "};"); 751 verifyFormat("return {\n" 752 " body: {\n" 753 " setAttribute: function(key, val) { this[key] = val; },\n" 754 " getAttribute: function(key) { return this[key]; },\n" 755 " style: {direction: ''}\n" 756 " }\n" 757 "};", 758 Style); 759 verifyFormat("abc = xyz ? function() {\n" 760 " return 1;\n" 761 "} : function() {\n" 762 " return -1;\n" 763 "};"); 764 765 verifyFormat("var closure = goog.bind(\n" 766 " function() { // comment\n" 767 " foo();\n" 768 " bar();\n" 769 " },\n" 770 " this, arg1IsReallyLongAndNeedsLineBreaks,\n" 771 " arg3IsReallyLongAndNeedsLineBreaks);"); 772 verifyFormat("var closure = goog.bind(function() { // comment\n" 773 " foo();\n" 774 " bar();\n" 775 "}, this);"); 776 verifyFormat("return {\n" 777 " a: 'E',\n" 778 " b: function() {\n" 779 " return function() {\n" 780 " f(); //\n" 781 " };\n" 782 " }\n" 783 "};"); 784 verifyFormat("{\n" 785 " var someVariable = function(x) {\n" 786 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 787 " };\n" 788 "}"); 789 verifyFormat("someLooooooooongFunction(\n" 790 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 791 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 792 " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 793 " // code\n" 794 " });"); 795 796 verifyFormat("return {\n" 797 " a: function SomeFunction() {\n" 798 " // ...\n" 799 " return 1;\n" 800 " }\n" 801 "};"); 802 verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 803 " .then(goog.bind(function(aaaaaaaaaaa) {\n" 804 " someFunction();\n" 805 " someFunction();\n" 806 " }, this), aaaaaaaaaaaaaaaaa);"); 807 808 verifyFormat("someFunction(goog.bind(function() {\n" 809 " doSomething();\n" 810 " doSomething();\n" 811 "}, this), goog.bind(function() {\n" 812 " doSomething();\n" 813 " doSomething();\n" 814 "}, this));"); 815 816 verifyFormat("SomeFunction(function() {\n" 817 " foo();\n" 818 " bar();\n" 819 "}.bind(this));"); 820 821 verifyFormat("SomeFunction((function() {\n" 822 " foo();\n" 823 " bar();\n" 824 " }).bind(this));"); 825 826 // FIXME: This is bad, we should be wrapping before "function() {". 827 verifyFormat("someFunction(function() {\n" 828 " doSomething(); // break\n" 829 "})\n" 830 " .doSomethingElse(\n" 831 " // break\n" 832 " );"); 833 834 Style.ColumnLimit = 33; 835 verifyFormat("f({a: function() { return 1; }});", Style); 836 Style.ColumnLimit = 32; 837 verifyFormat("f({\n" 838 " a: function() { return 1; }\n" 839 "});", 840 Style); 841 842 } 843 844 TEST_F(FormatTestJS, InliningFunctionLiterals) { 845 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 846 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 847 verifyFormat("var func = function() {\n" 848 " return 1;\n" 849 "};", 850 Style); 851 verifyFormat("var func = doSomething(function() { return 1; });", Style); 852 verifyFormat("var outer = function() {\n" 853 " var inner = function() { return 1; }\n" 854 "};", 855 Style); 856 verifyFormat("function outer1(a, b) {\n" 857 " function inner1(a, b) { return a; }\n" 858 "}", 859 Style); 860 861 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 862 verifyFormat("var func = function() { return 1; };", Style); 863 verifyFormat("var func = doSomething(function() { return 1; });", Style); 864 verifyFormat( 865 "var outer = function() { var inner = function() { return 1; } };", 866 Style); 867 verifyFormat("function outer1(a, b) {\n" 868 " function inner1(a, b) { return a; }\n" 869 "}", 870 Style); 871 872 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 873 verifyFormat("var func = function() {\n" 874 " return 1;\n" 875 "};", 876 Style); 877 verifyFormat("var func = doSomething(function() {\n" 878 " return 1;\n" 879 "});", 880 Style); 881 verifyFormat("var outer = function() {\n" 882 " var inner = function() {\n" 883 " return 1;\n" 884 " }\n" 885 "};", 886 Style); 887 verifyFormat("function outer1(a, b) {\n" 888 " function inner1(a, b) {\n" 889 " return a;\n" 890 " }\n" 891 "}", 892 Style); 893 894 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 895 verifyFormat("var func = function() {\n" 896 " return 1;\n" 897 "};", 898 Style); 899 } 900 901 TEST_F(FormatTestJS, MultipleFunctionLiterals) { 902 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 903 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 904 verifyFormat("promise.then(\n" 905 " function success() {\n" 906 " doFoo();\n" 907 " doBar();\n" 908 " },\n" 909 " function error() {\n" 910 " doFoo();\n" 911 " doBaz();\n" 912 " },\n" 913 " []);\n"); 914 verifyFormat("promise.then(\n" 915 " function success() {\n" 916 " doFoo();\n" 917 " doBar();\n" 918 " },\n" 919 " [],\n" 920 " function error() {\n" 921 " doFoo();\n" 922 " doBaz();\n" 923 " });\n"); 924 verifyFormat("promise.then(\n" 925 " [],\n" 926 " function success() {\n" 927 " doFoo();\n" 928 " doBar();\n" 929 " },\n" 930 " function error() {\n" 931 " doFoo();\n" 932 " doBaz();\n" 933 " });\n"); 934 935 verifyFormat("getSomeLongPromise()\n" 936 " .then(function(value) { body(); })\n" 937 " .thenCatch(function(error) {\n" 938 " body();\n" 939 " body();\n" 940 " });", 941 Style); 942 verifyFormat("getSomeLongPromise()\n" 943 " .then(function(value) {\n" 944 " body();\n" 945 " body();\n" 946 " })\n" 947 " .thenCatch(function(error) {\n" 948 " body();\n" 949 " body();\n" 950 " });"); 951 952 verifyFormat("getSomeLongPromise()\n" 953 " .then(function(value) { body(); })\n" 954 " .thenCatch(function(error) { body(); });", 955 Style); 956 957 verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n" 958 " .aaaaaaa(function() {\n" 959 " //\n" 960 " })\n" 961 " .bbbbbb();"); 962 } 963 964 TEST_F(FormatTestJS, ArrowFunctions) { 965 verifyFormat("var x = (a) => {\n" 966 " return a;\n" 967 "};"); 968 verifyFormat("var x = (a) => {\n" 969 " function y() {\n" 970 " return 42;\n" 971 " }\n" 972 " return a;\n" 973 "};"); 974 verifyFormat("var x = (a: type): {some: type} => {\n" 975 " return a;\n" 976 "};"); 977 verifyFormat("var x = (a) => a;"); 978 verifyFormat("return () => [];"); 979 verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n" 980 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 981 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 982 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n" 983 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 984 "};"); 985 verifyFormat("var a = a.aaaaaaa(\n" 986 " (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n" 987 " aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 988 verifyFormat("var a = a.aaaaaaa(\n" 989 " (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n" 990 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n" 991 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 992 993 // FIXME: This is bad, we should be wrapping before "() => {". 994 verifyFormat("someFunction(() => {\n" 995 " doSomething(); // break\n" 996 "})\n" 997 " .doSomethingElse(\n" 998 " // break\n" 999 " );"); 1000 verifyFormat("const f = (x: string|null): string|null => {\n" 1001 " return x;\n" 1002 "}\n"); 1003 } 1004 1005 TEST_F(FormatTestJS, ReturnStatements) { 1006 verifyFormat("function() {\n" 1007 " return [hello, world];\n" 1008 "}"); 1009 } 1010 1011 TEST_F(FormatTestJS, ForLoops) { 1012 verifyFormat("for (var i in [2, 3]) {\n" 1013 "}"); 1014 verifyFormat("for (var i of [2, 3]) {\n" 1015 "}"); 1016 verifyFormat("for (let {a, b} of x) {\n" 1017 "}"); 1018 verifyFormat("for (let {a, b} in x) {\n" 1019 "}"); 1020 } 1021 1022 TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) { 1023 // The following statements must not wrap, as otherwise the program meaning 1024 // would change due to automatic semicolon insertion. 1025 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1. 1026 verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10)); 1027 verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10)); 1028 verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10)); 1029 verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10)); 1030 verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10)); 1031 verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10)); 1032 verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10)); 1033 verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10)); 1034 verifyFormat("return [\n" 1035 " aaa\n" 1036 "];", 1037 getGoogleJSStyleWithColumns(12)); 1038 verifyFormat("class X {\n" 1039 " readonly ratherLongField =\n" 1040 " 1;\n" 1041 "}", 1042 "class X {\n" 1043 " readonly ratherLongField = 1;\n" 1044 "}", 1045 getGoogleJSStyleWithColumns(20)); 1046 } 1047 1048 TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) { 1049 verifyFormat("a\n" 1050 "b;", 1051 " a \n" 1052 " b ;"); 1053 verifyFormat("a()\n" 1054 "b;", 1055 " a ()\n" 1056 " b ;"); 1057 verifyFormat("a[b]\n" 1058 "c;", 1059 "a [b]\n" 1060 "c ;"); 1061 verifyFormat("1\n" 1062 "a;", 1063 "1 \n" 1064 "a ;"); 1065 verifyFormat("a\n" 1066 "1;", 1067 "a \n" 1068 "1 ;"); 1069 verifyFormat("a\n" 1070 "'x';", 1071 "a \n" 1072 " 'x';"); 1073 verifyFormat("a++\n" 1074 "b;", 1075 "a ++\n" 1076 "b ;"); 1077 verifyFormat("a\n" 1078 "!b && c;", 1079 "a \n" 1080 " ! b && c;"); 1081 verifyFormat("a\n" 1082 "if (1) f();", 1083 " a\n" 1084 " if (1) f();"); 1085 verifyFormat("a\n" 1086 "class X {}", 1087 " a\n" 1088 " class X {}"); 1089 verifyFormat("var a", "var\n" 1090 "a"); 1091 verifyFormat("x instanceof String", "x\n" 1092 "instanceof\n" 1093 "String"); 1094 verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n" 1095 " bar) {}"); 1096 verifyFormat("a = true\n" 1097 "return 1", 1098 "a = true\n" 1099 " return 1"); 1100 verifyFormat("a = 's'\n" 1101 "return 1", 1102 "a = 's'\n" 1103 " return 1"); 1104 verifyFormat("a = null\n" 1105 "return 1", 1106 "a = null\n" 1107 " return 1"); 1108 // Below "class Y {}" should ideally be on its own line. 1109 verifyFormat( 1110 "x = {\n" 1111 " a: 1\n" 1112 "} class Y {}", 1113 " x = {a : 1}\n" 1114 " class Y { }"); 1115 verifyFormat( 1116 "if (x) {\n" 1117 "}\n" 1118 "return 1", 1119 "if (x) {}\n" 1120 " return 1"); 1121 verifyFormat( 1122 "if (x) {\n" 1123 "}\n" 1124 "class X {}", 1125 "if (x) {}\n" 1126 " class X {}"); 1127 } 1128 1129 TEST_F(FormatTestJS, ImportExportASI) { 1130 verifyFormat( 1131 "import {x} from 'y'\n" 1132 "export function z() {}", 1133 "import {x} from 'y'\n" 1134 " export function z() {}"); 1135 // Below "class Y {}" should ideally be on its own line. 1136 verifyFormat( 1137 "export {x} class Y {}", 1138 " export {x}\n" 1139 " class Y {\n}"); 1140 verifyFormat( 1141 "if (x) {\n" 1142 "}\n" 1143 "export class Y {}", 1144 "if ( x ) { }\n" 1145 " export class Y {}"); 1146 } 1147 1148 TEST_F(FormatTestJS, ClosureStyleCasts) { 1149 verifyFormat("var x = /** @type {foo} */ (bar);"); 1150 } 1151 1152 TEST_F(FormatTestJS, TryCatch) { 1153 verifyFormat("try {\n" 1154 " f();\n" 1155 "} catch (e) {\n" 1156 " g();\n" 1157 "} finally {\n" 1158 " h();\n" 1159 "}"); 1160 1161 // But, of course, "catch" is a perfectly fine function name in JavaScript. 1162 verifyFormat("someObject.catch();"); 1163 verifyFormat("someObject.new();"); 1164 verifyFormat("someObject.delete();"); 1165 } 1166 1167 TEST_F(FormatTestJS, StringLiteralConcatenation) { 1168 verifyFormat("var literal = 'hello ' +\n" 1169 " 'world';"); 1170 } 1171 1172 TEST_F(FormatTestJS, RegexLiteralClassification) { 1173 // Regex literals. 1174 verifyFormat("var regex = /abc/;"); 1175 verifyFormat("f(/abc/);"); 1176 verifyFormat("f(abc, /abc/);"); 1177 verifyFormat("some_map[/abc/];"); 1178 verifyFormat("var x = a ? /abc/ : /abc/;"); 1179 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 1180 verifyFormat("var x = !/abc/.test(y);"); 1181 verifyFormat("var x = foo()! / 10;"); 1182 verifyFormat("var x = a && /abc/.test(y);"); 1183 verifyFormat("var x = a || /abc/.test(y);"); 1184 verifyFormat("var x = a + /abc/.search(y);"); 1185 verifyFormat("/abc/.search(y);"); 1186 verifyFormat("var regexs = {/abc/, /abc/};"); 1187 verifyFormat("return /abc/;"); 1188 1189 // Not regex literals. 1190 verifyFormat("var a = a / 2 + b / 3;"); 1191 verifyFormat("var a = a++ / 2;"); 1192 // Prefix unary can operate on regex literals, not that it makes sense. 1193 verifyFormat("var a = ++/a/;"); 1194 1195 // This is a known issue, regular expressions are incorrectly detected if 1196 // directly following a closing parenthesis. 1197 verifyFormat("if (foo) / bar /.exec(baz);"); 1198 } 1199 1200 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 1201 verifyFormat("var regex = /=/;"); 1202 verifyFormat("var regex = /a*/;"); 1203 verifyFormat("var regex = /a+/;"); 1204 verifyFormat("var regex = /a?/;"); 1205 verifyFormat("var regex = /.a./;"); 1206 verifyFormat("var regex = /a\\*/;"); 1207 verifyFormat("var regex = /^a$/;"); 1208 verifyFormat("var regex = /\\/a/;"); 1209 verifyFormat("var regex = /(?:x)/;"); 1210 verifyFormat("var regex = /x(?=y)/;"); 1211 verifyFormat("var regex = /x(?!y)/;"); 1212 verifyFormat("var regex = /x|y/;"); 1213 verifyFormat("var regex = /a{2}/;"); 1214 verifyFormat("var regex = /a{1,3}/;"); 1215 1216 verifyFormat("var regex = /[abc]/;"); 1217 verifyFormat("var regex = /[^abc]/;"); 1218 verifyFormat("var regex = /[\\b]/;"); 1219 verifyFormat("var regex = /[/]/;"); 1220 verifyFormat("var regex = /[\\/]/;"); 1221 verifyFormat("var regex = /\\[/;"); 1222 verifyFormat("var regex = /\\\\[/]/;"); 1223 verifyFormat("var regex = /}[\"]/;"); 1224 verifyFormat("var regex = /}[/\"]/;"); 1225 verifyFormat("var regex = /}[\"/]/;"); 1226 1227 verifyFormat("var regex = /\\b/;"); 1228 verifyFormat("var regex = /\\B/;"); 1229 verifyFormat("var regex = /\\d/;"); 1230 verifyFormat("var regex = /\\D/;"); 1231 verifyFormat("var regex = /\\f/;"); 1232 verifyFormat("var regex = /\\n/;"); 1233 verifyFormat("var regex = /\\r/;"); 1234 verifyFormat("var regex = /\\s/;"); 1235 verifyFormat("var regex = /\\S/;"); 1236 verifyFormat("var regex = /\\t/;"); 1237 verifyFormat("var regex = /\\v/;"); 1238 verifyFormat("var regex = /\\w/;"); 1239 verifyFormat("var regex = /\\W/;"); 1240 verifyFormat("var regex = /a(a)\\1/;"); 1241 verifyFormat("var regex = /\\0/;"); 1242 verifyFormat("var regex = /\\\\/g;"); 1243 verifyFormat("var regex = /\\a\\\\/g;"); 1244 verifyFormat("var regex = /\a\\//g;"); 1245 verifyFormat("var regex = /a\\//;\n" 1246 "var x = 0;"); 1247 verifyFormat("var regex = /'/g;", "var regex = /'/g ;"); 1248 verifyFormat("var regex = /'/g; //'", "var regex = /'/g ; //'"); 1249 verifyFormat("var regex = /\\/*/;\n" 1250 "var x = 0;", 1251 "var regex = /\\/*/;\n" 1252 "var x=0;"); 1253 verifyFormat("var x = /a\\//;", "var x = /a\\// \n;"); 1254 verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16)); 1255 verifyFormat("var regex =\n" 1256 " /\"/;", 1257 getGoogleJSStyleWithColumns(15)); 1258 verifyFormat("var regex = //\n" 1259 " /a/;"); 1260 verifyFormat("var regexs = [\n" 1261 " /d/, //\n" 1262 " /aa/, //\n" 1263 "];"); 1264 } 1265 1266 TEST_F(FormatTestJS, RegexLiteralModifiers) { 1267 verifyFormat("var regex = /abc/g;"); 1268 verifyFormat("var regex = /abc/i;"); 1269 verifyFormat("var regex = /abc/m;"); 1270 verifyFormat("var regex = /abc/y;"); 1271 } 1272 1273 TEST_F(FormatTestJS, RegexLiteralLength) { 1274 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 1275 getGoogleJSStyleWithColumns(60)); 1276 verifyFormat("var regex =\n" 1277 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 1278 getGoogleJSStyleWithColumns(60)); 1279 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 1280 getGoogleJSStyleWithColumns(50)); 1281 } 1282 1283 TEST_F(FormatTestJS, RegexLiteralExamples) { 1284 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 1285 } 1286 1287 TEST_F(FormatTestJS, IgnoresMpegTS) { 1288 std::string MpegTS(200, ' '); 1289 MpegTS.replace(0, strlen("nearlyLooks + like + ts + code; "), 1290 "nearlyLooks + like + ts + code; "); 1291 MpegTS[0] = 0x47; 1292 MpegTS[188] = 0x47; 1293 verifyFormat(MpegTS, MpegTS); 1294 } 1295 1296 TEST_F(FormatTestJS, TypeAnnotations) { 1297 verifyFormat("var x: string;"); 1298 verifyFormat("var x: {a: string; b: number;} = {};"); 1299 verifyFormat("function x(): string {\n return 'x';\n}"); 1300 verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}"); 1301 verifyFormat("function x(y: string): string {\n return 'x';\n}"); 1302 verifyFormat("for (var y: string in x) {\n x();\n}"); 1303 verifyFormat("for (var y: string of x) {\n x();\n}"); 1304 verifyFormat("function x(y: {a?: number;} = {}): number {\n" 1305 " return 12;\n" 1306 "}"); 1307 verifyFormat("((a: string, b: number): string => a + b);"); 1308 verifyFormat("var x: (y: number) => string;"); 1309 verifyFormat("var x: P<string, (a: number) => string>;"); 1310 verifyFormat("var x = {\n" 1311 " y: function(): z {\n" 1312 " return 1;\n" 1313 " }\n" 1314 "};"); 1315 verifyFormat("var x = {\n" 1316 " y: function(): {a: number} {\n" 1317 " return 1;\n" 1318 " }\n" 1319 "};"); 1320 verifyFormat("function someFunc(args: string[]):\n" 1321 " {longReturnValue: string[]} {}", 1322 getGoogleJSStyleWithColumns(60)); 1323 verifyFormat( 1324 "var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])\n" 1325 " .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1326 } 1327 1328 TEST_F(FormatTestJS, UnionIntersectionTypes) { 1329 verifyFormat("let x: A|B = A | B;"); 1330 verifyFormat("let x: A&B|C = A & B;"); 1331 verifyFormat("let x: Foo<A|B> = new Foo<A|B>();"); 1332 verifyFormat("function(x: A|B): C&D {}"); 1333 verifyFormat("function(x: A|B = A | B): C&D {}"); 1334 verifyFormat("function x(path: number|string) {}"); 1335 verifyFormat("function x(): string|number {}"); 1336 verifyFormat("type Foo = Bar|Baz;"); 1337 verifyFormat("type Foo = Bar<X>|Baz;"); 1338 verifyFormat("type Foo = (Bar<X>|Baz);"); 1339 verifyFormat("let x: Bar|Baz;"); 1340 verifyFormat("let x: Bar<X>|Baz;"); 1341 verifyFormat("let x: (Foo|Bar)[];"); 1342 verifyFormat("type X = {\n" 1343 " a: Foo|Bar;\n" 1344 "};"); 1345 verifyFormat("export type X = {\n" 1346 " a: Foo|Bar;\n" 1347 "};"); 1348 } 1349 1350 TEST_F(FormatTestJS, ClassDeclarations) { 1351 verifyFormat("class C {\n x: string = 12;\n}"); 1352 verifyFormat("class C {\n x(): string => 12;\n}"); 1353 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); 1354 verifyFormat("class C {\n" 1355 " foo() {}\n" 1356 " [bar]() {}\n" 1357 "}\n"); 1358 verifyFormat("class C {\n private x: string = 12;\n}"); 1359 verifyFormat("class C {\n private static x: string = 12;\n}"); 1360 verifyFormat("class C {\n static x(): string {\n return 'asd';\n }\n}"); 1361 verifyFormat("class C extends P implements I {}"); 1362 verifyFormat("class C extends p.P implements i.I {}"); 1363 verifyFormat( 1364 "x(class {\n" 1365 " a(): A {}\n" 1366 "});"); 1367 verifyFormat("class Test {\n" 1368 " aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n" 1369 " aaaaaaaaaaaaaaaaaaaaaa {}\n" 1370 "}"); 1371 verifyFormat("foo = class Name {\n" 1372 " constructor() {}\n" 1373 "};"); 1374 verifyFormat("foo = class {\n" 1375 " constructor() {}\n" 1376 "};"); 1377 verifyFormat("class C {\n" 1378 " x: {y: Z;} = {};\n" 1379 " private y: {y: Z;} = {};\n" 1380 "}"); 1381 1382 // ':' is not a type declaration here. 1383 verifyFormat("class X {\n" 1384 " subs = {\n" 1385 " 'b': {\n" 1386 " 'c': 1,\n" 1387 " },\n" 1388 " };\n" 1389 "}"); 1390 verifyFormat("@Component({\n" 1391 " moduleId: module.id,\n" 1392 "})\n" 1393 "class SessionListComponent implements OnDestroy, OnInit {\n" 1394 "}"); 1395 } 1396 1397 TEST_F(FormatTestJS, InterfaceDeclarations) { 1398 verifyFormat("interface I {\n" 1399 " x: string;\n" 1400 " enum: string[];\n" 1401 " enum?: string[];\n" 1402 "}\n" 1403 "var y;"); 1404 // Ensure that state is reset after parsing the interface. 1405 verifyFormat("interface a {}\n" 1406 "export function b() {}\n" 1407 "var x;"); 1408 1409 // Arrays of object type literals. 1410 verifyFormat("interface I {\n" 1411 " o: {}[];\n" 1412 "}"); 1413 } 1414 1415 TEST_F(FormatTestJS, ObjectTypesInExtendsImplements) { 1416 verifyFormat("class C extends {} {}"); 1417 verifyFormat("class C implements {bar: number} {}"); 1418 // Somewhat odd, but probably closest to reasonable formatting? 1419 verifyFormat("class C implements {\n" 1420 " bar: number,\n" 1421 " baz: string,\n" 1422 "} {}"); 1423 verifyFormat("class C<P extends {}> {}"); 1424 } 1425 1426 TEST_F(FormatTestJS, EnumDeclarations) { 1427 verifyFormat("enum Foo {\n" 1428 " A = 1,\n" 1429 " B\n" 1430 "}"); 1431 verifyFormat("export /* somecomment*/ enum Foo {\n" 1432 " A = 1,\n" 1433 " B\n" 1434 "}"); 1435 verifyFormat("enum Foo {\n" 1436 " A = 1, // comment\n" 1437 " B\n" 1438 "}\n" 1439 "var x = 1;"); 1440 verifyFormat("const enum Foo {\n" 1441 " A = 1,\n" 1442 " B\n" 1443 "}"); 1444 verifyFormat("export const enum Foo {\n" 1445 " A = 1,\n" 1446 " B\n" 1447 "}"); 1448 } 1449 1450 TEST_F(FormatTestJS, MetadataAnnotations) { 1451 verifyFormat("@A\nclass C {\n}"); 1452 verifyFormat("@A({arg: 'value'})\nclass C {\n}"); 1453 verifyFormat("@A\n@B\nclass C {\n}"); 1454 verifyFormat("class C {\n @A x: string;\n}"); 1455 verifyFormat("class C {\n" 1456 " @A\n" 1457 " private x(): string {\n" 1458 " return 'y';\n" 1459 " }\n" 1460 "}"); 1461 verifyFormat("class C {\n" 1462 " private x(@A x: string) {}\n" 1463 "}"); 1464 verifyFormat("class X {}\n" 1465 "class Y {}"); 1466 verifyFormat("class X {\n" 1467 " @property() private isReply = false;\n" 1468 "}\n"); 1469 } 1470 1471 TEST_F(FormatTestJS, TypeAliases) { 1472 verifyFormat("type X = number;\n" 1473 "class C {}"); 1474 verifyFormat("type X<Y> = Z<Y>;"); 1475 verifyFormat("type X = {\n" 1476 " y: number\n" 1477 "};\n" 1478 "class C {}"); 1479 } 1480 1481 TEST_F(FormatTestJS, TypeInterfaceLineWrapping) { 1482 const FormatStyle &Style = getGoogleJSStyleWithColumns(20); 1483 verifyFormat("type LongTypeIsReallyUnreasonablyLong =\n" 1484 " string;\n", 1485 "type LongTypeIsReallyUnreasonablyLong = string;\n", 1486 Style); 1487 verifyFormat( 1488 "interface AbstractStrategyFactoryProvider {\n" 1489 " a: number\n" 1490 "}\n", 1491 "interface AbstractStrategyFactoryProvider { a: number }\n", 1492 Style); 1493 } 1494 1495 TEST_F(FormatTestJS, Modules) { 1496 verifyFormat("import SomeThing from 'some/module.js';"); 1497 verifyFormat("import {X, Y} from 'some/module.js';"); 1498 verifyFormat("import a, {X, Y} from 'some/module.js';"); 1499 verifyFormat("import {X, Y,} from 'some/module.js';"); 1500 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); 1501 // Ensure Automatic Semicolon Insertion does not break on "as\n". 1502 verifyFormat("import {X as myX} from 'm';", "import {X as\n" 1503 " myX} from 'm';"); 1504 verifyFormat("import * as lib from 'some/module.js';"); 1505 verifyFormat("var x = {import: 1};\nx.import = 2;"); 1506 1507 verifyFormat("export function fn() {\n" 1508 " return 'fn';\n" 1509 "}"); 1510 verifyFormat("export function A() {}\n" 1511 "export default function B() {}\n" 1512 "export function C() {}"); 1513 verifyFormat("export default () => {\n" 1514 " let x = 1;\n" 1515 " return x;\n" 1516 "}"); 1517 verifyFormat("export const x = 12;"); 1518 verifyFormat("export default class X {}"); 1519 verifyFormat("export {X, Y} from 'some/module.js';"); 1520 verifyFormat("export {X, Y,} from 'some/module.js';"); 1521 verifyFormat("export {SomeVeryLongExport as X, " 1522 "SomeOtherVeryLongExport as Y} from 'some/module.js';"); 1523 // export without 'from' is wrapped. 1524 verifyFormat("export let someRatherLongVariableName =\n" 1525 " someSurprisinglyLongVariable + someOtherRatherLongVar;"); 1526 // ... but not if from is just an identifier. 1527 verifyFormat("export {\n" 1528 " from as from,\n" 1529 " someSurprisinglyLongVariable as\n" 1530 " from\n" 1531 "};", 1532 getGoogleJSStyleWithColumns(20)); 1533 verifyFormat("export class C {\n" 1534 " x: number;\n" 1535 " y: string;\n" 1536 "}"); 1537 verifyFormat("export class X { y: number; }"); 1538 verifyFormat("export abstract class X { y: number; }"); 1539 verifyFormat("export default class X { y: number }"); 1540 verifyFormat("export default function() {\n return 1;\n}"); 1541 verifyFormat("export var x = 12;"); 1542 verifyFormat("class C {}\n" 1543 "export function f() {}\n" 1544 "var v;"); 1545 verifyFormat("export var x: number = 12;"); 1546 verifyFormat("export const y = {\n" 1547 " a: 1,\n" 1548 " b: 2\n" 1549 "};"); 1550 verifyFormat("export enum Foo {\n" 1551 " BAR,\n" 1552 " // adsdasd\n" 1553 " BAZ\n" 1554 "}"); 1555 verifyFormat("export default [\n" 1556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1557 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 1558 "];"); 1559 verifyFormat("export default [];"); 1560 verifyFormat("export default () => {};"); 1561 verifyFormat("export interface Foo { foo: number; }\n" 1562 "export class Bar {\n" 1563 " blah(): string {\n" 1564 " return this.blah;\n" 1565 " };\n" 1566 "}"); 1567 } 1568 1569 TEST_F(FormatTestJS, ImportWrapping) { 1570 verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying," 1571 " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying" 1572 "} from 'some/module.js';"); 1573 FormatStyle Style = getGoogleJSStyleWithColumns(80); 1574 Style.JavaScriptWrapImports = true; 1575 verifyFormat("import {\n" 1576 " VeryLongImportsAreAnnoying,\n" 1577 " VeryLongImportsAreAnnoying,\n" 1578 " VeryLongImportsAreAnnoying,\n" 1579 "} from 'some/module.js';", 1580 Style); 1581 verifyFormat("import {\n" 1582 " A,\n" 1583 " A,\n" 1584 "} from 'some/module.js';", 1585 Style); 1586 verifyFormat("export {\n" 1587 " A,\n" 1588 " A,\n" 1589 "} from 'some/module.js';", 1590 Style); 1591 Style.ColumnLimit = 40; 1592 // Using this version of verifyFormat because test::messUp hides the issue. 1593 verifyFormat("import {\n" 1594 " A,\n" 1595 "} from\n" 1596 " 'some/path/longer/than/column/limit/module.js';", 1597 " import { \n" 1598 " A, \n" 1599 " } from\n" 1600 " 'some/path/longer/than/column/limit/module.js' ; ", 1601 Style); 1602 } 1603 1604 TEST_F(FormatTestJS, TemplateStrings) { 1605 // Keeps any whitespace/indentation within the template string. 1606 verifyFormat("var x = `hello\n" 1607 " ${name}\n" 1608 " !`;", 1609 "var x = `hello\n" 1610 " ${ name }\n" 1611 " !`;"); 1612 1613 verifyFormat("var x =\n" 1614 " `hello ${world}` >= some();", 1615 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. 1616 verifyFormat("var x = `hello ${world}` >= some();", 1617 getGoogleJSStyleWithColumns(35)); // Barely fits. 1618 verifyFormat("var x = `hellö ${wörld}` >= söme();", 1619 getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8. 1620 verifyFormat("var x = `hello\n" 1621 " ${world}` >=\n" 1622 " some();", 1623 "var x =\n" 1624 " `hello\n" 1625 " ${world}` >= some();", 1626 getGoogleJSStyleWithColumns(21)); // Barely doesn't fit. 1627 verifyFormat("var x = `hello\n" 1628 " ${world}` >= some();", 1629 "var x =\n" 1630 " `hello\n" 1631 " ${world}` >= some();", 1632 getGoogleJSStyleWithColumns(22)); // Barely fits. 1633 1634 verifyFormat("var x =\n" 1635 " `h`;", 1636 getGoogleJSStyleWithColumns(11)); 1637 verifyFormat("var x =\n `multi\n line`;", "var x = `multi\n line`;", 1638 getGoogleJSStyleWithColumns(13)); 1639 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1640 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);"); 1641 // Repro for an obscure width-miscounting issue with template strings. 1642 verifyFormat( 1643 "someLongVariable =\n" 1644 " " 1645 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;", 1646 "someLongVariable = " 1647 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;"); 1648 1649 // Make sure template strings get a proper ColumnWidth assigned, even if they 1650 // are first token in line. 1651 verifyFormat( 1652 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1653 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); 1654 1655 // Two template strings. 1656 verifyFormat("var x = `hello` == `hello`;"); 1657 1658 // Comments in template strings. 1659 verifyFormat("var x = `//a`;\n" 1660 "var y;", 1661 "var x =\n `//a`;\n" 1662 "var y ;"); 1663 verifyFormat("var x = `/*a`;\n" 1664 "var y;", 1665 "var x =\n `/*a`;\n" 1666 "var y;"); 1667 // Unterminated string literals in a template string. 1668 verifyFormat("var x = `'`; // comment with matching quote '\n" 1669 "var y;"); 1670 verifyFormat("var x = `\"`; // comment with matching quote \"\n" 1671 "var y;"); 1672 verifyFormat("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa);", 1673 "it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa) ;", 1674 getGoogleJSStyleWithColumns(40)); 1675 // Backticks in a comment - not a template string. 1676 verifyFormat("var x = 1 // `/*a`;\n" 1677 " ;", 1678 "var x =\n 1 // `/*a`;\n" 1679 " ;"); 1680 verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */"); 1681 // Comment spans multiple template strings. 1682 verifyFormat("var x = `/*a`;\n" 1683 "var y = ` */ `;", 1684 "var x =\n `/*a`;\n" 1685 "var y =\n ` */ `;"); 1686 // Escaped backtick. 1687 verifyFormat("var x = ` \\` a`;\n" 1688 "var y;", 1689 "var x = ` \\` a`;\n" 1690 "var y;"); 1691 // Escaped dollar. 1692 verifyFormat("var x = ` \\${foo}`;\n"); 1693 1694 // The token stream can contain two string_literals in sequence, but that 1695 // doesn't mean that they are implicitly concatenated in JavaScript. 1696 verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;"); 1697 1698 // Ensure that scopes are appropriately set around evaluated expressions in 1699 // template strings. 1700 verifyFormat("var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa\n" 1701 " aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;", 1702 "var f = `aaaaaaaaaaaaa:${aaaaaaa. aaaaa} aaaaaaaa\n" 1703 " aaaaaaaaaaaaa:${ aaaaaaa. aaaaa} aaaaaaaa`;"); 1704 verifyFormat("var x = someFunction(`${})`) //\n" 1705 " .oooooooooooooooooon();"); 1706 verifyFormat("var x = someFunction(`${aaaa}${\n" 1707 " aaaaa( //\n" 1708 " aaaaa)\n" 1709 " })`);"); 1710 } 1711 1712 TEST_F(FormatTestJS, TemplateStringMultiLineExpression) { 1713 verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${\n" 1714 " aaaaa + //\n" 1715 " bbbb\n" 1716 " }`;", 1717 "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa + //\n" 1718 " bbbb}`;"); 1719 verifyFormat("var f = `\n" 1720 " aaaaaaaaaaaaaaaaaa: ${\n" 1721 " aaaaa + //\n" 1722 " bbbb\n" 1723 " }`;", 1724 "var f = `\n" 1725 " aaaaaaaaaaaaaaaaaa: ${ aaaaa + //\n" 1726 " bbbb }`;"); 1727 verifyFormat("var f = `\n" 1728 " aaaaaaaaaaaaaaaaaa: ${\n" 1729 " someFunction(\n" 1730 " aaaaa + //\n" 1731 " bbbb)\n" 1732 " }`;", 1733 "var f = `\n" 1734 " aaaaaaaaaaaaaaaaaa: ${someFunction (\n" 1735 " aaaaa + //\n" 1736 " bbbb)}`;"); 1737 1738 // It might be preferable to wrap before "someFunction". 1739 verifyFormat("var f = `\n" 1740 " aaaaaaaaaaaaaaaaaa: ${someFunction({\n" 1741 " aaaa: aaaaa,\n" 1742 " bbbb: bbbbb,\n" 1743 " })}`;", 1744 "var f = `\n" 1745 " aaaaaaaaaaaaaaaaaa: ${someFunction ({\n" 1746 " aaaa: aaaaa,\n" 1747 " bbbb: bbbbb,\n" 1748 " })}`;"); 1749 } 1750 1751 TEST_F(FormatTestJS, TemplateStringASI) { 1752 verifyFormat("var x = `hello${world}`;", "var x = `hello${\n" 1753 " world\n" 1754 "}`;"); 1755 } 1756 1757 TEST_F(FormatTestJS, NestedTemplateStrings) { 1758 verifyFormat( 1759 "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;"); 1760 verifyFormat("var x = `he${({text: 'll'}.text)}o`;"); 1761 1762 // Crashed at some point. 1763 verifyFormat("}"); 1764 } 1765 1766 TEST_F(FormatTestJS, TaggedTemplateStrings) { 1767 verifyFormat("var x = html`<ul>`;"); 1768 verifyFormat("yield `hello`;"); 1769 } 1770 1771 TEST_F(FormatTestJS, CastSyntax) { 1772 verifyFormat("var x = <type>foo;"); 1773 verifyFormat("var x = foo as type;"); 1774 verifyFormat("let x = (a + b) as\n" 1775 " LongTypeIsLong;", 1776 getGoogleJSStyleWithColumns(20)); 1777 verifyFormat("foo = <Bar[]>[\n" 1778 " 1, //\n" 1779 " 2\n" 1780 "];"); 1781 verifyFormat("var x = [{x: 1} as type];"); 1782 verifyFormat("x = x as [a, b];"); 1783 verifyFormat("x = x as {a: string};"); 1784 verifyFormat("x = x as (string);"); 1785 verifyFormat("x = x! as (string);"); 1786 verifyFormat("var x = something.someFunction() as\n" 1787 " something;", 1788 getGoogleJSStyleWithColumns(40)); 1789 } 1790 1791 TEST_F(FormatTestJS, TypeArguments) { 1792 verifyFormat("class X<Y> {}"); 1793 verifyFormat("new X<Y>();"); 1794 verifyFormat("foo<Y>(a);"); 1795 verifyFormat("var x: X<Y>[];"); 1796 verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); 1797 verifyFormat("function f(a: List<any> = null) {}"); 1798 verifyFormat("function f(): List<any> {}"); 1799 verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n" 1800 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}"); 1801 verifyFormat("function aaaaaaaaaa(\n" 1802 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n" 1803 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n" 1804 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}"); 1805 } 1806 1807 TEST_F(FormatTestJS, UserDefinedTypeGuards) { 1808 verifyFormat( 1809 "function foo(check: Object):\n" 1810 " check is {foo: string, bar: string, baz: string, foobar: string} {\n" 1811 " return 'bar' in check;\n" 1812 "}\n"); 1813 } 1814 1815 TEST_F(FormatTestJS, OptionalTypes) { 1816 verifyFormat("function x(a?: b, c?, d?) {}"); 1817 verifyFormat("class X {\n" 1818 " y?: z;\n" 1819 " z?;\n" 1820 "}"); 1821 verifyFormat("interface X {\n" 1822 " y?(): z;\n" 1823 "}"); 1824 verifyFormat("constructor({aa}: {\n" 1825 " aa?: string,\n" 1826 " aaaaaaaa?: string,\n" 1827 " aaaaaaaaaaaaaaa?: boolean,\n" 1828 " aaaaaa?: List<string>\n" 1829 "}) {}"); 1830 } 1831 1832 TEST_F(FormatTestJS, IndexSignature) { 1833 verifyFormat("var x: {[k: string]: v};"); 1834 } 1835 1836 TEST_F(FormatTestJS, WrapAfterParen) { 1837 verifyFormat("xxxxxxxxxxx(\n" 1838 " aaa, aaa);", 1839 getGoogleJSStyleWithColumns(20)); 1840 verifyFormat("xxxxxxxxxxx(\n" 1841 " aaa, aaa, aaa,\n" 1842 " aaa, aaa, aaa);", 1843 getGoogleJSStyleWithColumns(20)); 1844 verifyFormat("xxxxxxxxxxx(\n" 1845 " aaaaaaaaaaaaaaaaaaaaaaaa,\n" 1846 " function(x) {\n" 1847 " y(); //\n" 1848 " });", 1849 getGoogleJSStyleWithColumns(40)); 1850 verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 1851 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 1852 } 1853 1854 TEST_F(FormatTestJS, JSDocAnnotations) { 1855 verifyFormat("/**\n" 1856 " * @export {this.is.a.long.path.to.a.Type}\n" 1857 " */", 1858 "/**\n" 1859 " * @export {this.is.a.long.path.to.a.Type}\n" 1860 " */", 1861 getGoogleJSStyleWithColumns(20)); 1862 verifyFormat("/**\n" 1863 " * @mods {this.is.a.long.path.to.a.Type}\n" 1864 " */", 1865 "/**\n" 1866 " * @mods {this.is.a.long.path.to.a.Type}\n" 1867 " */", 1868 getGoogleJSStyleWithColumns(20)); 1869 verifyFormat("/**\n" 1870 " * @param {this.is.a.long.path.to.a.Type}\n" 1871 " */", 1872 "/**\n" 1873 " * @param {this.is.a.long.path.to.a.Type}\n" 1874 " */", 1875 getGoogleJSStyleWithColumns(20)); 1876 verifyFormat("/**\n" 1877 " * @see http://very/very/long/url/is/long\n" 1878 " */", 1879 "/**\n" 1880 " * @see http://very/very/long/url/is/long\n" 1881 " */", 1882 getGoogleJSStyleWithColumns(20)); 1883 verifyFormat( 1884 "/**\n" 1885 " * @param This is a\n" 1886 " * long comment but\n" 1887 " * no type\n" 1888 " */", 1889 "/**\n" 1890 " * @param This is a long comment but no type\n" 1891 " */", 1892 getGoogleJSStyleWithColumns(20)); 1893 // Don't break @param line, but reindent it and reflow unrelated lines. 1894 verifyFormat("{\n" 1895 " /**\n" 1896 " * long long long\n" 1897 " * long\n" 1898 " * @param {this.is.a.long.path.to.a.Type} a\n" 1899 " * long long long\n" 1900 " * long long\n" 1901 " */\n" 1902 " function f(a) {}\n" 1903 "}", 1904 "{\n" 1905 "/**\n" 1906 " * long long long long\n" 1907 " * @param {this.is.a.long.path.to.a.Type} a\n" 1908 " * long long long long\n" 1909 " * long\n" 1910 " */\n" 1911 " function f(a) {}\n" 1912 "}", 1913 getGoogleJSStyleWithColumns(20)); 1914 } 1915 1916 TEST_F(FormatTestJS, RequoteStringsSingle) { 1917 verifyFormat("var x = 'foo';", "var x = \"foo\";"); 1918 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";"); 1919 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";"); 1920 verifyFormat( 1921 "var x =\n" 1922 " 'foo\\'';", 1923 // Code below is 15 chars wide, doesn't fit into the line with the 1924 // \ escape added. 1925 "var x = \"foo'\";", getGoogleJSStyleWithColumns(15)); 1926 // Removes no-longer needed \ escape from ". 1927 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";"); 1928 // Code below fits into 15 chars *after* removing the \ escape. 1929 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";", 1930 getGoogleJSStyleWithColumns(15)); 1931 verifyFormat("// clang-format off\n" 1932 "let x = \"double\";\n" 1933 "// clang-format on\n" 1934 "let x = 'single';\n", 1935 "// clang-format off\n" 1936 "let x = \"double\";\n" 1937 "// clang-format on\n" 1938 "let x = \"single\";\n"); 1939 } 1940 1941 TEST_F(FormatTestJS, RequoteAndIndent) { 1942 verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n" 1943 " 'double quoted string that needs wrapping');", 1944 "let x = someVeryLongFunctionThatGoesOnAndOn(" 1945 "\"double quoted string that needs wrapping\");"); 1946 1947 verifyFormat("let x =\n" 1948 " 'foo\\'oo';\n" 1949 "let x =\n" 1950 " 'foo\\'oo';", 1951 "let x=\"foo'oo\";\n" 1952 "let x=\"foo'oo\";", 1953 getGoogleJSStyleWithColumns(15)); 1954 } 1955 1956 TEST_F(FormatTestJS, RequoteStringsDouble) { 1957 FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1958 DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double; 1959 verifyFormat("var x = \"foo\";", DoubleQuotes); 1960 verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes); 1961 verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes); 1962 } 1963 1964 TEST_F(FormatTestJS, RequoteStringsLeave) { 1965 FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1966 LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave; 1967 verifyFormat("var x = \"foo\";", LeaveQuotes); 1968 verifyFormat("var x = 'foo';", LeaveQuotes); 1969 } 1970 1971 TEST_F(FormatTestJS, SupportShebangLines) { 1972 verifyFormat("#!/usr/bin/env node\n" 1973 "var x = hello();", 1974 "#!/usr/bin/env node\n" 1975 "var x = hello();"); 1976 } 1977 1978 TEST_F(FormatTestJS, NonNullAssertionOperator) { 1979 verifyFormat("let x = foo!.bar();\n"); 1980 verifyFormat("let x = foo ? bar! : baz;\n"); 1981 verifyFormat("let x = !foo;\n"); 1982 verifyFormat("let x = foo[0]!;\n"); 1983 verifyFormat("let x = (foo)!;\n"); 1984 verifyFormat("let x = x(foo!);\n"); 1985 verifyFormat( 1986 "a.aaaaaa(a.a!).then(\n" 1987 " x => x(x));\n", 1988 getGoogleJSStyleWithColumns(20)); 1989 verifyFormat("let x = foo! - 1;\n"); 1990 verifyFormat("let x = {foo: 1}!;\n"); 1991 verifyFormat( 1992 "let x = hello.foo()!\n" 1993 " .foo()!\n" 1994 " .foo()!\n" 1995 " .foo()!;\n", 1996 getGoogleJSStyleWithColumns(20)); 1997 verifyFormat("let x = namespace!;\n"); 1998 verifyFormat("return !!x;\n"); 1999 } 2000 2001 TEST_F(FormatTestJS, Conditional) { 2002 verifyFormat("y = x ? 1 : 2;"); 2003 verifyFormat("x ? 1 : 2;"); 2004 verifyFormat("class Foo {\n" 2005 " field = true ? 1 : 2;\n" 2006 " method(a = true ? 1 : 2) {}\n" 2007 "}"); 2008 } 2009 2010 TEST_F(FormatTestJS, ImportComments) { 2011 verifyFormat("import {x} from 'x'; // from some location", 2012 getGoogleJSStyleWithColumns(25)); 2013 verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10)); 2014 verifyFormat("/// <reference path=\"some/location\" />", getGoogleJSStyleWithColumns(10)); 2015 } 2016 2017 TEST_F(FormatTestJS, Exponentiation) { 2018 verifyFormat("squared = x ** 2;"); 2019 verifyFormat("squared **= 2;"); 2020 } 2021 2022 TEST_F(FormatTestJS, NestedLiterals) { 2023 FormatStyle FourSpaces = getGoogleJSStyleWithColumns(15); 2024 FourSpaces.IndentWidth = 4; 2025 verifyFormat("var l = [\n" 2026 " [\n" 2027 " 1,\n" 2028 " ],\n" 2029 "];", FourSpaces); 2030 verifyFormat("var l = [\n" 2031 " {\n" 2032 " 1: 1,\n" 2033 " },\n" 2034 "];", FourSpaces); 2035 verifyFormat("someFunction(\n" 2036 " p1,\n" 2037 " [\n" 2038 " 1,\n" 2039 " ],\n" 2040 ");", FourSpaces); 2041 verifyFormat("someFunction(\n" 2042 " p1,\n" 2043 " {\n" 2044 " 1: 1,\n" 2045 " },\n" 2046 ");", FourSpaces); 2047 verifyFormat("var o = {\n" 2048 " 1: 1,\n" 2049 " 2: {\n" 2050 " 3: 3,\n" 2051 " },\n" 2052 "};", FourSpaces); 2053 verifyFormat("var o = {\n" 2054 " 1: 1,\n" 2055 " 2: [\n" 2056 " 3,\n" 2057 " ],\n" 2058 "};", FourSpaces); 2059 } 2060 2061 } // end namespace tooling 2062 } // end namespace clang 2063