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