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 } 992 993 TEST_F(FormatTestJS, ReturnStatements) { 994 verifyFormat("function() {\n" 995 " return [hello, world];\n" 996 "}"); 997 } 998 999 TEST_F(FormatTestJS, ForLoops) { 1000 verifyFormat("for (var i in [2, 3]) {\n" 1001 "}"); 1002 verifyFormat("for (var i of [2, 3]) {\n" 1003 "}"); 1004 verifyFormat("for (let {a, b} of x) {\n" 1005 "}"); 1006 verifyFormat("for (let {a, b} in x) {\n" 1007 "}"); 1008 } 1009 1010 TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) { 1011 // The following statements must not wrap, as otherwise the program meaning 1012 // would change due to automatic semicolon insertion. 1013 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1. 1014 verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10)); 1015 verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10)); 1016 verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10)); 1017 verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10)); 1018 verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10)); 1019 verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10)); 1020 verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10)); 1021 verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10)); 1022 verifyFormat("return [\n" 1023 " aaa\n" 1024 "];", 1025 getGoogleJSStyleWithColumns(12)); 1026 verifyFormat("class X {\n" 1027 " readonly ratherLongField =\n" 1028 " 1;\n" 1029 "}", 1030 "class X {\n" 1031 " readonly ratherLongField = 1;\n" 1032 "}", 1033 getGoogleJSStyleWithColumns(20)); 1034 } 1035 1036 TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) { 1037 verifyFormat("a\n" 1038 "b;", 1039 " a \n" 1040 " b ;"); 1041 verifyFormat("a()\n" 1042 "b;", 1043 " a ()\n" 1044 " b ;"); 1045 verifyFormat("a[b]\n" 1046 "c;", 1047 "a [b]\n" 1048 "c ;"); 1049 verifyFormat("1\n" 1050 "a;", 1051 "1 \n" 1052 "a ;"); 1053 verifyFormat("a\n" 1054 "1;", 1055 "a \n" 1056 "1 ;"); 1057 verifyFormat("a\n" 1058 "'x';", 1059 "a \n" 1060 " 'x';"); 1061 verifyFormat("a++\n" 1062 "b;", 1063 "a ++\n" 1064 "b ;"); 1065 verifyFormat("a\n" 1066 "!b && c;", 1067 "a \n" 1068 " ! b && c;"); 1069 verifyFormat("a\n" 1070 "if (1) f();", 1071 " a\n" 1072 " if (1) f();"); 1073 verifyFormat("a\n" 1074 "class X {}", 1075 " a\n" 1076 " class X {}"); 1077 verifyFormat("var a", "var\n" 1078 "a"); 1079 verifyFormat("x instanceof String", "x\n" 1080 "instanceof\n" 1081 "String"); 1082 verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n" 1083 " bar) {}"); 1084 verifyFormat("a = true\n" 1085 "return 1", 1086 "a = true\n" 1087 " return 1"); 1088 verifyFormat("a = 's'\n" 1089 "return 1", 1090 "a = 's'\n" 1091 " return 1"); 1092 verifyFormat("a = null\n" 1093 "return 1", 1094 "a = null\n" 1095 " return 1"); 1096 // Below "class Y {}" should ideally be on its own line. 1097 verifyFormat( 1098 "x = {\n" 1099 " a: 1\n" 1100 "} class Y {}", 1101 " x = {a : 1}\n" 1102 " class Y { }"); 1103 verifyFormat( 1104 "if (x) {\n" 1105 "}\n" 1106 "return 1", 1107 "if (x) {}\n" 1108 " return 1"); 1109 verifyFormat( 1110 "if (x) {\n" 1111 "}\n" 1112 "class X {}", 1113 "if (x) {}\n" 1114 " class X {}"); 1115 } 1116 1117 TEST_F(FormatTestJS, ImportExportASI) { 1118 verifyFormat( 1119 "import {x} from 'y'\n" 1120 "export function z() {}", 1121 "import {x} from 'y'\n" 1122 " export function z() {}"); 1123 // Below "class Y {}" should ideally be on its own line. 1124 verifyFormat( 1125 "export {x} class Y {}", 1126 " export {x}\n" 1127 " class Y {\n}"); 1128 verifyFormat( 1129 "if (x) {\n" 1130 "}\n" 1131 "export class Y {}", 1132 "if ( x ) { }\n" 1133 " export class Y {}"); 1134 } 1135 1136 TEST_F(FormatTestJS, ClosureStyleCasts) { 1137 verifyFormat("var x = /** @type {foo} */ (bar);"); 1138 } 1139 1140 TEST_F(FormatTestJS, TryCatch) { 1141 verifyFormat("try {\n" 1142 " f();\n" 1143 "} catch (e) {\n" 1144 " g();\n" 1145 "} finally {\n" 1146 " h();\n" 1147 "}"); 1148 1149 // But, of course, "catch" is a perfectly fine function name in JavaScript. 1150 verifyFormat("someObject.catch();"); 1151 verifyFormat("someObject.new();"); 1152 verifyFormat("someObject.delete();"); 1153 } 1154 1155 TEST_F(FormatTestJS, StringLiteralConcatenation) { 1156 verifyFormat("var literal = 'hello ' +\n" 1157 " 'world';"); 1158 } 1159 1160 TEST_F(FormatTestJS, RegexLiteralClassification) { 1161 // Regex literals. 1162 verifyFormat("var regex = /abc/;"); 1163 verifyFormat("f(/abc/);"); 1164 verifyFormat("f(abc, /abc/);"); 1165 verifyFormat("some_map[/abc/];"); 1166 verifyFormat("var x = a ? /abc/ : /abc/;"); 1167 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 1168 verifyFormat("var x = !/abc/.test(y);"); 1169 verifyFormat("var x = foo()! / 10;"); 1170 verifyFormat("var x = a && /abc/.test(y);"); 1171 verifyFormat("var x = a || /abc/.test(y);"); 1172 verifyFormat("var x = a + /abc/.search(y);"); 1173 verifyFormat("/abc/.search(y);"); 1174 verifyFormat("var regexs = {/abc/, /abc/};"); 1175 verifyFormat("return /abc/;"); 1176 1177 // Not regex literals. 1178 verifyFormat("var a = a / 2 + b / 3;"); 1179 verifyFormat("var a = a++ / 2;"); 1180 // Prefix unary can operate on regex literals, not that it makes sense. 1181 verifyFormat("var a = ++/a/;"); 1182 1183 // This is a known issue, regular expressions are incorrectly detected if 1184 // directly following a closing parenthesis. 1185 verifyFormat("if (foo) / bar /.exec(baz);"); 1186 } 1187 1188 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 1189 verifyFormat("var regex = /=/;"); 1190 verifyFormat("var regex = /a*/;"); 1191 verifyFormat("var regex = /a+/;"); 1192 verifyFormat("var regex = /a?/;"); 1193 verifyFormat("var regex = /.a./;"); 1194 verifyFormat("var regex = /a\\*/;"); 1195 verifyFormat("var regex = /^a$/;"); 1196 verifyFormat("var regex = /\\/a/;"); 1197 verifyFormat("var regex = /(?:x)/;"); 1198 verifyFormat("var regex = /x(?=y)/;"); 1199 verifyFormat("var regex = /x(?!y)/;"); 1200 verifyFormat("var regex = /x|y/;"); 1201 verifyFormat("var regex = /a{2}/;"); 1202 verifyFormat("var regex = /a{1,3}/;"); 1203 1204 verifyFormat("var regex = /[abc]/;"); 1205 verifyFormat("var regex = /[^abc]/;"); 1206 verifyFormat("var regex = /[\\b]/;"); 1207 verifyFormat("var regex = /[/]/;"); 1208 verifyFormat("var regex = /[\\/]/;"); 1209 verifyFormat("var regex = /\\[/;"); 1210 verifyFormat("var regex = /\\\\[/]/;"); 1211 verifyFormat("var regex = /}[\"]/;"); 1212 verifyFormat("var regex = /}[/\"]/;"); 1213 verifyFormat("var regex = /}[\"/]/;"); 1214 1215 verifyFormat("var regex = /\\b/;"); 1216 verifyFormat("var regex = /\\B/;"); 1217 verifyFormat("var regex = /\\d/;"); 1218 verifyFormat("var regex = /\\D/;"); 1219 verifyFormat("var regex = /\\f/;"); 1220 verifyFormat("var regex = /\\n/;"); 1221 verifyFormat("var regex = /\\r/;"); 1222 verifyFormat("var regex = /\\s/;"); 1223 verifyFormat("var regex = /\\S/;"); 1224 verifyFormat("var regex = /\\t/;"); 1225 verifyFormat("var regex = /\\v/;"); 1226 verifyFormat("var regex = /\\w/;"); 1227 verifyFormat("var regex = /\\W/;"); 1228 verifyFormat("var regex = /a(a)\\1/;"); 1229 verifyFormat("var regex = /\\0/;"); 1230 verifyFormat("var regex = /\\\\/g;"); 1231 verifyFormat("var regex = /\\a\\\\/g;"); 1232 verifyFormat("var regex = /\a\\//g;"); 1233 verifyFormat("var regex = /a\\//;\n" 1234 "var x = 0;"); 1235 verifyFormat("var regex = /'/g;", "var regex = /'/g ;"); 1236 verifyFormat("var regex = /'/g; //'", "var regex = /'/g ; //'"); 1237 verifyFormat("var regex = /\\/*/;\n" 1238 "var x = 0;", 1239 "var regex = /\\/*/;\n" 1240 "var x=0;"); 1241 verifyFormat("var x = /a\\//;", "var x = /a\\// \n;"); 1242 verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16)); 1243 verifyFormat("var regex =\n" 1244 " /\"/;", 1245 getGoogleJSStyleWithColumns(15)); 1246 verifyFormat("var regex = //\n" 1247 " /a/;"); 1248 verifyFormat("var regexs = [\n" 1249 " /d/, //\n" 1250 " /aa/, //\n" 1251 "];"); 1252 } 1253 1254 TEST_F(FormatTestJS, RegexLiteralModifiers) { 1255 verifyFormat("var regex = /abc/g;"); 1256 verifyFormat("var regex = /abc/i;"); 1257 verifyFormat("var regex = /abc/m;"); 1258 verifyFormat("var regex = /abc/y;"); 1259 } 1260 1261 TEST_F(FormatTestJS, RegexLiteralLength) { 1262 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 1263 getGoogleJSStyleWithColumns(60)); 1264 verifyFormat("var regex =\n" 1265 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 1266 getGoogleJSStyleWithColumns(60)); 1267 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 1268 getGoogleJSStyleWithColumns(50)); 1269 } 1270 1271 TEST_F(FormatTestJS, RegexLiteralExamples) { 1272 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 1273 } 1274 1275 TEST_F(FormatTestJS, IgnoresMpegTS) { 1276 std::string MpegTS(200, ' '); 1277 MpegTS.replace(0, strlen("nearlyLooks + like + ts + code; "), 1278 "nearlyLooks + like + ts + code; "); 1279 MpegTS[0] = 0x47; 1280 MpegTS[188] = 0x47; 1281 verifyFormat(MpegTS, MpegTS); 1282 } 1283 1284 TEST_F(FormatTestJS, TypeAnnotations) { 1285 verifyFormat("var x: string;"); 1286 verifyFormat("var x: {a: string; b: number;} = {};"); 1287 verifyFormat("function x(): string {\n return 'x';\n}"); 1288 verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}"); 1289 verifyFormat("function x(y: string): string {\n return 'x';\n}"); 1290 verifyFormat("for (var y: string in x) {\n x();\n}"); 1291 verifyFormat("for (var y: string of x) {\n x();\n}"); 1292 verifyFormat("function x(y: {a?: number;} = {}): number {\n" 1293 " return 12;\n" 1294 "}"); 1295 verifyFormat("((a: string, b: number): string => a + b);"); 1296 verifyFormat("var x: (y: number) => string;"); 1297 verifyFormat("var x: P<string, (a: number) => string>;"); 1298 verifyFormat("var x = {\n" 1299 " y: function(): z {\n" 1300 " return 1;\n" 1301 " }\n" 1302 "};"); 1303 verifyFormat("var x = {\n" 1304 " y: function(): {a: number} {\n" 1305 " return 1;\n" 1306 " }\n" 1307 "};"); 1308 verifyFormat("function someFunc(args: string[]):\n" 1309 " {longReturnValue: string[]} {}", 1310 getGoogleJSStyleWithColumns(60)); 1311 verifyFormat( 1312 "var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])\n" 1313 " .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1314 } 1315 1316 TEST_F(FormatTestJS, UnionIntersectionTypes) { 1317 verifyFormat("let x: A|B = A | B;"); 1318 verifyFormat("let x: A&B|C = A & B;"); 1319 verifyFormat("let x: Foo<A|B> = new Foo<A|B>();"); 1320 verifyFormat("function(x: A|B): C&D {}"); 1321 verifyFormat("function(x: A|B = A | B): C&D {}"); 1322 verifyFormat("function x(path: number|string) {}"); 1323 verifyFormat("function x(): string|number {}"); 1324 verifyFormat("type Foo = Bar|Baz;"); 1325 verifyFormat("type Foo = Bar<X>|Baz;"); 1326 verifyFormat("type Foo = (Bar<X>|Baz);"); 1327 verifyFormat("let x: Bar|Baz;"); 1328 verifyFormat("let x: Bar<X>|Baz;"); 1329 verifyFormat("let x: (Foo|Bar)[];"); 1330 verifyFormat("type X = {\n" 1331 " a: Foo|Bar;\n" 1332 "};"); 1333 verifyFormat("export type X = {\n" 1334 " a: Foo|Bar;\n" 1335 "};"); 1336 } 1337 1338 TEST_F(FormatTestJS, ClassDeclarations) { 1339 verifyFormat("class C {\n x: string = 12;\n}"); 1340 verifyFormat("class C {\n x(): string => 12;\n}"); 1341 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); 1342 verifyFormat("class C {\n" 1343 " foo() {}\n" 1344 " [bar]() {}\n" 1345 "}\n"); 1346 verifyFormat("class C {\n private x: string = 12;\n}"); 1347 verifyFormat("class C {\n private static x: string = 12;\n}"); 1348 verifyFormat("class C {\n static x(): string {\n return 'asd';\n }\n}"); 1349 verifyFormat("class C extends P implements I {}"); 1350 verifyFormat("class C extends p.P implements i.I {}"); 1351 verifyFormat( 1352 "x(class {\n" 1353 " a(): A {}\n" 1354 "});"); 1355 verifyFormat("class Test {\n" 1356 " aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n" 1357 " aaaaaaaaaaaaaaaaaaaaaa {}\n" 1358 "}"); 1359 verifyFormat("foo = class Name {\n" 1360 " constructor() {}\n" 1361 "};"); 1362 verifyFormat("foo = class {\n" 1363 " constructor() {}\n" 1364 "};"); 1365 verifyFormat("class C {\n" 1366 " x: {y: Z;} = {};\n" 1367 " private y: {y: Z;} = {};\n" 1368 "}"); 1369 1370 // ':' is not a type declaration here. 1371 verifyFormat("class X {\n" 1372 " subs = {\n" 1373 " 'b': {\n" 1374 " 'c': 1,\n" 1375 " },\n" 1376 " };\n" 1377 "}"); 1378 verifyFormat("@Component({\n" 1379 " moduleId: module.id,\n" 1380 "})\n" 1381 "class SessionListComponent implements OnDestroy, OnInit {\n" 1382 "}"); 1383 } 1384 1385 TEST_F(FormatTestJS, InterfaceDeclarations) { 1386 verifyFormat("interface I {\n" 1387 " x: string;\n" 1388 " enum: string[];\n" 1389 " enum?: string[];\n" 1390 "}\n" 1391 "var y;"); 1392 // Ensure that state is reset after parsing the interface. 1393 verifyFormat("interface a {}\n" 1394 "export function b() {}\n" 1395 "var x;"); 1396 1397 // Arrays of object type literals. 1398 verifyFormat("interface I {\n" 1399 " o: {}[];\n" 1400 "}"); 1401 } 1402 1403 TEST_F(FormatTestJS, ObjectTypesInExtendsImplements) { 1404 verifyFormat("class C extends {} {}"); 1405 verifyFormat("class C implements {bar: number} {}"); 1406 // Somewhat odd, but probably closest to reasonable formatting? 1407 verifyFormat("class C implements {\n" 1408 " bar: number,\n" 1409 " baz: string,\n" 1410 "} {}"); 1411 verifyFormat("class C<P extends {}> {}"); 1412 } 1413 1414 TEST_F(FormatTestJS, EnumDeclarations) { 1415 verifyFormat("enum Foo {\n" 1416 " A = 1,\n" 1417 " B\n" 1418 "}"); 1419 verifyFormat("export /* somecomment*/ enum Foo {\n" 1420 " A = 1,\n" 1421 " B\n" 1422 "}"); 1423 verifyFormat("enum Foo {\n" 1424 " A = 1, // comment\n" 1425 " B\n" 1426 "}\n" 1427 "var x = 1;"); 1428 verifyFormat("const enum Foo {\n" 1429 " A = 1,\n" 1430 " B\n" 1431 "}"); 1432 verifyFormat("export const enum Foo {\n" 1433 " A = 1,\n" 1434 " B\n" 1435 "}"); 1436 } 1437 1438 TEST_F(FormatTestJS, MetadataAnnotations) { 1439 verifyFormat("@A\nclass C {\n}"); 1440 verifyFormat("@A({arg: 'value'})\nclass C {\n}"); 1441 verifyFormat("@A\n@B\nclass C {\n}"); 1442 verifyFormat("class C {\n @A x: string;\n}"); 1443 verifyFormat("class C {\n" 1444 " @A\n" 1445 " private x(): string {\n" 1446 " return 'y';\n" 1447 " }\n" 1448 "}"); 1449 verifyFormat("class C {\n" 1450 " private x(@A x: string) {}\n" 1451 "}"); 1452 verifyFormat("class X {}\n" 1453 "class Y {}"); 1454 verifyFormat("class X {\n" 1455 " @property() private isReply = false;\n" 1456 "}\n"); 1457 } 1458 1459 TEST_F(FormatTestJS, TypeAliases) { 1460 verifyFormat("type X = number;\n" 1461 "class C {}"); 1462 verifyFormat("type X<Y> = Z<Y>;"); 1463 verifyFormat("type X = {\n" 1464 " y: number\n" 1465 "};\n" 1466 "class C {}"); 1467 } 1468 1469 TEST_F(FormatTestJS, TypeInterfaceLineWrapping) { 1470 const FormatStyle &Style = getGoogleJSStyleWithColumns(20); 1471 verifyFormat("type LongTypeIsReallyUnreasonablyLong =\n" 1472 " string;\n", 1473 "type LongTypeIsReallyUnreasonablyLong = string;\n", 1474 Style); 1475 verifyFormat( 1476 "interface AbstractStrategyFactoryProvider {\n" 1477 " a: number\n" 1478 "}\n", 1479 "interface AbstractStrategyFactoryProvider { a: number }\n", 1480 Style); 1481 } 1482 1483 TEST_F(FormatTestJS, Modules) { 1484 verifyFormat("import SomeThing from 'some/module.js';"); 1485 verifyFormat("import {X, Y} from 'some/module.js';"); 1486 verifyFormat("import a, {X, Y} from 'some/module.js';"); 1487 verifyFormat("import {X, Y,} from 'some/module.js';"); 1488 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); 1489 // Ensure Automatic Semicolon Insertion does not break on "as\n". 1490 verifyFormat("import {X as myX} from 'm';", "import {X as\n" 1491 " myX} from 'm';"); 1492 verifyFormat("import * as lib from 'some/module.js';"); 1493 verifyFormat("var x = {import: 1};\nx.import = 2;"); 1494 1495 verifyFormat("export function fn() {\n" 1496 " return 'fn';\n" 1497 "}"); 1498 verifyFormat("export function A() {}\n" 1499 "export default function B() {}\n" 1500 "export function C() {}"); 1501 verifyFormat("export default () => {\n" 1502 " let x = 1;\n" 1503 " return x;\n" 1504 "}"); 1505 verifyFormat("export const x = 12;"); 1506 verifyFormat("export default class X {}"); 1507 verifyFormat("export {X, Y} from 'some/module.js';"); 1508 verifyFormat("export {X, Y,} from 'some/module.js';"); 1509 verifyFormat("export {SomeVeryLongExport as X, " 1510 "SomeOtherVeryLongExport as Y} from 'some/module.js';"); 1511 // export without 'from' is wrapped. 1512 verifyFormat("export let someRatherLongVariableName =\n" 1513 " someSurprisinglyLongVariable + someOtherRatherLongVar;"); 1514 // ... but not if from is just an identifier. 1515 verifyFormat("export {\n" 1516 " from as from,\n" 1517 " someSurprisinglyLongVariable as\n" 1518 " from\n" 1519 "};", 1520 getGoogleJSStyleWithColumns(20)); 1521 verifyFormat("export class C {\n" 1522 " x: number;\n" 1523 " y: string;\n" 1524 "}"); 1525 verifyFormat("export class X { y: number; }"); 1526 verifyFormat("export abstract class X { y: number; }"); 1527 verifyFormat("export default class X { y: number }"); 1528 verifyFormat("export default function() {\n return 1;\n}"); 1529 verifyFormat("export var x = 12;"); 1530 verifyFormat("class C {}\n" 1531 "export function f() {}\n" 1532 "var v;"); 1533 verifyFormat("export var x: number = 12;"); 1534 verifyFormat("export const y = {\n" 1535 " a: 1,\n" 1536 " b: 2\n" 1537 "};"); 1538 verifyFormat("export enum Foo {\n" 1539 " BAR,\n" 1540 " // adsdasd\n" 1541 " BAZ\n" 1542 "}"); 1543 verifyFormat("export default [\n" 1544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1545 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 1546 "];"); 1547 verifyFormat("export default [];"); 1548 verifyFormat("export default () => {};"); 1549 verifyFormat("export interface Foo { foo: number; }\n" 1550 "export class Bar {\n" 1551 " blah(): string {\n" 1552 " return this.blah;\n" 1553 " };\n" 1554 "}"); 1555 } 1556 1557 TEST_F(FormatTestJS, ImportWrapping) { 1558 verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying," 1559 " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying" 1560 "} from 'some/module.js';"); 1561 FormatStyle Style = getGoogleJSStyleWithColumns(80); 1562 Style.JavaScriptWrapImports = true; 1563 verifyFormat("import {\n" 1564 " VeryLongImportsAreAnnoying,\n" 1565 " VeryLongImportsAreAnnoying,\n" 1566 " VeryLongImportsAreAnnoying,\n" 1567 "} from 'some/module.js';", 1568 Style); 1569 verifyFormat("import {\n" 1570 " A,\n" 1571 " A,\n" 1572 "} from 'some/module.js';", 1573 Style); 1574 verifyFormat("export {\n" 1575 " A,\n" 1576 " A,\n" 1577 "} from 'some/module.js';", 1578 Style); 1579 Style.ColumnLimit = 40; 1580 // Using this version of verifyFormat because test::messUp hides the issue. 1581 verifyFormat("import {\n" 1582 " A,\n" 1583 "} from\n" 1584 " 'some/path/longer/than/column/limit/module.js';", 1585 " import { \n" 1586 " A, \n" 1587 " } from\n" 1588 " 'some/path/longer/than/column/limit/module.js' ; ", 1589 Style); 1590 } 1591 1592 TEST_F(FormatTestJS, TemplateStrings) { 1593 // Keeps any whitespace/indentation within the template string. 1594 verifyFormat("var x = `hello\n" 1595 " ${name}\n" 1596 " !`;", 1597 "var x = `hello\n" 1598 " ${ name }\n" 1599 " !`;"); 1600 1601 verifyFormat("var x =\n" 1602 " `hello ${world}` >= some();", 1603 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. 1604 verifyFormat("var x = `hello ${world}` >= some();", 1605 getGoogleJSStyleWithColumns(35)); // Barely fits. 1606 verifyFormat("var x = `hellö ${wörld}` >= söme();", 1607 getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8. 1608 verifyFormat("var x = `hello\n" 1609 " ${world}` >=\n" 1610 " some();", 1611 "var x =\n" 1612 " `hello\n" 1613 " ${world}` >= some();", 1614 getGoogleJSStyleWithColumns(21)); // Barely doesn't fit. 1615 verifyFormat("var x = `hello\n" 1616 " ${world}` >= some();", 1617 "var x =\n" 1618 " `hello\n" 1619 " ${world}` >= some();", 1620 getGoogleJSStyleWithColumns(22)); // Barely fits. 1621 1622 verifyFormat("var x =\n" 1623 " `h`;", 1624 getGoogleJSStyleWithColumns(11)); 1625 verifyFormat("var x =\n `multi\n line`;", "var x = `multi\n line`;", 1626 getGoogleJSStyleWithColumns(13)); 1627 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1628 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);"); 1629 // Repro for an obscure width-miscounting issue with template strings. 1630 verifyFormat( 1631 "someLongVariable =\n" 1632 " " 1633 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;", 1634 "someLongVariable = " 1635 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;"); 1636 1637 // Make sure template strings get a proper ColumnWidth assigned, even if they 1638 // are first token in line. 1639 verifyFormat( 1640 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1641 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); 1642 1643 // Two template strings. 1644 verifyFormat("var x = `hello` == `hello`;"); 1645 1646 // Comments in template strings. 1647 verifyFormat("var x = `//a`;\n" 1648 "var y;", 1649 "var x =\n `//a`;\n" 1650 "var y ;"); 1651 verifyFormat("var x = `/*a`;\n" 1652 "var y;", 1653 "var x =\n `/*a`;\n" 1654 "var y;"); 1655 // Unterminated string literals in a template string. 1656 verifyFormat("var x = `'`; // comment with matching quote '\n" 1657 "var y;"); 1658 verifyFormat("var x = `\"`; // comment with matching quote \"\n" 1659 "var y;"); 1660 verifyFormat("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa);", 1661 "it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa) ;", 1662 getGoogleJSStyleWithColumns(40)); 1663 // Backticks in a comment - not a template string. 1664 verifyFormat("var x = 1 // `/*a`;\n" 1665 " ;", 1666 "var x =\n 1 // `/*a`;\n" 1667 " ;"); 1668 verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */"); 1669 // Comment spans multiple template strings. 1670 verifyFormat("var x = `/*a`;\n" 1671 "var y = ` */ `;", 1672 "var x =\n `/*a`;\n" 1673 "var y =\n ` */ `;"); 1674 // Escaped backtick. 1675 verifyFormat("var x = ` \\` a`;\n" 1676 "var y;", 1677 "var x = ` \\` a`;\n" 1678 "var y;"); 1679 // Escaped dollar. 1680 verifyFormat("var x = ` \\${foo}`;\n"); 1681 1682 // The token stream can contain two string_literals in sequence, but that 1683 // doesn't mean that they are implicitly concatenated in JavaScript. 1684 verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;"); 1685 1686 // Ensure that scopes are appropriately set around evaluated expressions in 1687 // template strings. 1688 verifyFormat("var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa\n" 1689 " aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;", 1690 "var f = `aaaaaaaaaaaaa:${aaaaaaa. aaaaa} aaaaaaaa\n" 1691 " aaaaaaaaaaaaa:${ aaaaaaa. aaaaa} aaaaaaaa`;"); 1692 verifyFormat("var x = someFunction(`${})`) //\n" 1693 " .oooooooooooooooooon();"); 1694 verifyFormat("var x = someFunction(`${aaaa}${\n" 1695 " aaaaa( //\n" 1696 " aaaaa)\n" 1697 " })`);"); 1698 } 1699 1700 TEST_F(FormatTestJS, TemplateStringMultiLineExpression) { 1701 verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${\n" 1702 " aaaaa + //\n" 1703 " bbbb\n" 1704 " }`;", 1705 "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa + //\n" 1706 " bbbb}`;"); 1707 verifyFormat("var f = `\n" 1708 " aaaaaaaaaaaaaaaaaa: ${\n" 1709 " aaaaa + //\n" 1710 " bbbb\n" 1711 " }`;", 1712 "var f = `\n" 1713 " aaaaaaaaaaaaaaaaaa: ${ aaaaa + //\n" 1714 " bbbb }`;"); 1715 verifyFormat("var f = `\n" 1716 " aaaaaaaaaaaaaaaaaa: ${\n" 1717 " someFunction(\n" 1718 " aaaaa + //\n" 1719 " bbbb)\n" 1720 " }`;", 1721 "var f = `\n" 1722 " aaaaaaaaaaaaaaaaaa: ${someFunction (\n" 1723 " aaaaa + //\n" 1724 " bbbb)}`;"); 1725 1726 // It might be preferable to wrap before "someFunction". 1727 verifyFormat("var f = `\n" 1728 " aaaaaaaaaaaaaaaaaa: ${someFunction({\n" 1729 " aaaa: aaaaa,\n" 1730 " bbbb: bbbbb,\n" 1731 " })}`;", 1732 "var f = `\n" 1733 " aaaaaaaaaaaaaaaaaa: ${someFunction ({\n" 1734 " aaaa: aaaaa,\n" 1735 " bbbb: bbbbb,\n" 1736 " })}`;"); 1737 } 1738 1739 TEST_F(FormatTestJS, TemplateStringASI) { 1740 verifyFormat("var x = `hello${world}`;", "var x = `hello${\n" 1741 " world\n" 1742 "}`;"); 1743 } 1744 1745 TEST_F(FormatTestJS, NestedTemplateStrings) { 1746 verifyFormat( 1747 "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;"); 1748 verifyFormat("var x = `he${({text: 'll'}.text)}o`;"); 1749 1750 // Crashed at some point. 1751 verifyFormat("}"); 1752 } 1753 1754 TEST_F(FormatTestJS, TaggedTemplateStrings) { 1755 verifyFormat("var x = html`<ul>`;"); 1756 verifyFormat("yield `hello`;"); 1757 } 1758 1759 TEST_F(FormatTestJS, CastSyntax) { 1760 verifyFormat("var x = <type>foo;"); 1761 verifyFormat("var x = foo as type;"); 1762 verifyFormat("let x = (a + b) as\n" 1763 " LongTypeIsLong;", 1764 getGoogleJSStyleWithColumns(20)); 1765 verifyFormat("foo = <Bar[]>[\n" 1766 " 1, //\n" 1767 " 2\n" 1768 "];"); 1769 verifyFormat("var x = [{x: 1} as type];"); 1770 verifyFormat("x = x as [a, b];"); 1771 verifyFormat("x = x as {a: string};"); 1772 verifyFormat("x = x as (string);"); 1773 verifyFormat("x = x! as (string);"); 1774 verifyFormat("var x = something.someFunction() as\n" 1775 " something;", 1776 getGoogleJSStyleWithColumns(40)); 1777 } 1778 1779 TEST_F(FormatTestJS, TypeArguments) { 1780 verifyFormat("class X<Y> {}"); 1781 verifyFormat("new X<Y>();"); 1782 verifyFormat("foo<Y>(a);"); 1783 verifyFormat("var x: X<Y>[];"); 1784 verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); 1785 verifyFormat("function f(a: List<any> = null) {}"); 1786 verifyFormat("function f(): List<any> {}"); 1787 verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n" 1788 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}"); 1789 verifyFormat("function aaaaaaaaaa(\n" 1790 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n" 1791 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n" 1792 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}"); 1793 } 1794 1795 TEST_F(FormatTestJS, UserDefinedTypeGuards) { 1796 verifyFormat( 1797 "function foo(check: Object):\n" 1798 " check is {foo: string, bar: string, baz: string, foobar: string} {\n" 1799 " return 'bar' in check;\n" 1800 "}\n"); 1801 } 1802 1803 TEST_F(FormatTestJS, OptionalTypes) { 1804 verifyFormat("function x(a?: b, c?, d?) {}"); 1805 verifyFormat("class X {\n" 1806 " y?: z;\n" 1807 " z?;\n" 1808 "}"); 1809 verifyFormat("interface X {\n" 1810 " y?(): z;\n" 1811 "}"); 1812 verifyFormat("constructor({aa}: {\n" 1813 " aa?: string,\n" 1814 " aaaaaaaa?: string,\n" 1815 " aaaaaaaaaaaaaaa?: boolean,\n" 1816 " aaaaaa?: List<string>\n" 1817 "}) {}"); 1818 } 1819 1820 TEST_F(FormatTestJS, IndexSignature) { 1821 verifyFormat("var x: {[k: string]: v};"); 1822 } 1823 1824 TEST_F(FormatTestJS, WrapAfterParen) { 1825 verifyFormat("xxxxxxxxxxx(\n" 1826 " aaa, aaa);", 1827 getGoogleJSStyleWithColumns(20)); 1828 verifyFormat("xxxxxxxxxxx(\n" 1829 " aaa, aaa, aaa,\n" 1830 " aaa, aaa, aaa);", 1831 getGoogleJSStyleWithColumns(20)); 1832 verifyFormat("xxxxxxxxxxx(\n" 1833 " aaaaaaaaaaaaaaaaaaaaaaaa,\n" 1834 " function(x) {\n" 1835 " y(); //\n" 1836 " });", 1837 getGoogleJSStyleWithColumns(40)); 1838 verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 1839 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 1840 } 1841 1842 TEST_F(FormatTestJS, JSDocAnnotations) { 1843 verifyFormat("/**\n" 1844 " * @export {this.is.a.long.path.to.a.Type}\n" 1845 " */", 1846 "/**\n" 1847 " * @export {this.is.a.long.path.to.a.Type}\n" 1848 " */", 1849 getGoogleJSStyleWithColumns(20)); 1850 verifyFormat("/**\n" 1851 " * @mods {this.is.a.long.path.to.a.Type}\n" 1852 " */", 1853 "/**\n" 1854 " * @mods {this.is.a.long.path.to.a.Type}\n" 1855 " */", 1856 getGoogleJSStyleWithColumns(20)); 1857 verifyFormat("/**\n" 1858 " * @param {this.is.a.long.path.to.a.Type}\n" 1859 " */", 1860 "/**\n" 1861 " * @param {this.is.a.long.path.to.a.Type}\n" 1862 " */", 1863 getGoogleJSStyleWithColumns(20)); 1864 verifyFormat("/**\n" 1865 " * @see http://very/very/long/url/is/long\n" 1866 " */", 1867 "/**\n" 1868 " * @see http://very/very/long/url/is/long\n" 1869 " */", 1870 getGoogleJSStyleWithColumns(20)); 1871 verifyFormat( 1872 "/**\n" 1873 " * @param This is a\n" 1874 " * long comment but\n" 1875 " * no type\n" 1876 " */", 1877 "/**\n" 1878 " * @param This is a long comment but no type\n" 1879 " */", 1880 getGoogleJSStyleWithColumns(20)); 1881 // Don't break @param line, but reindent it and reflow unrelated lines. 1882 verifyFormat("{\n" 1883 " /**\n" 1884 " * long long long\n" 1885 " * long\n" 1886 " * @param {this.is.a.long.path.to.a.Type} a\n" 1887 " * long long long\n" 1888 " * long long\n" 1889 " */\n" 1890 " function f(a) {}\n" 1891 "}", 1892 "{\n" 1893 "/**\n" 1894 " * long long long long\n" 1895 " * @param {this.is.a.long.path.to.a.Type} a\n" 1896 " * long long long long\n" 1897 " * long\n" 1898 " */\n" 1899 " function f(a) {}\n" 1900 "}", 1901 getGoogleJSStyleWithColumns(20)); 1902 } 1903 1904 TEST_F(FormatTestJS, RequoteStringsSingle) { 1905 verifyFormat("var x = 'foo';", "var x = \"foo\";"); 1906 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";"); 1907 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";"); 1908 verifyFormat( 1909 "var x =\n" 1910 " 'foo\\'';", 1911 // Code below is 15 chars wide, doesn't fit into the line with the 1912 // \ escape added. 1913 "var x = \"foo'\";", getGoogleJSStyleWithColumns(15)); 1914 // Removes no-longer needed \ escape from ". 1915 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";"); 1916 // Code below fits into 15 chars *after* removing the \ escape. 1917 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";", 1918 getGoogleJSStyleWithColumns(15)); 1919 verifyFormat("// clang-format off\n" 1920 "let x = \"double\";\n" 1921 "// clang-format on\n" 1922 "let x = 'single';\n", 1923 "// clang-format off\n" 1924 "let x = \"double\";\n" 1925 "// clang-format on\n" 1926 "let x = \"single\";\n"); 1927 } 1928 1929 TEST_F(FormatTestJS, RequoteAndIndent) { 1930 verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n" 1931 " 'double quoted string that needs wrapping');", 1932 "let x = someVeryLongFunctionThatGoesOnAndOn(" 1933 "\"double quoted string that needs wrapping\");"); 1934 1935 verifyFormat("let x =\n" 1936 " 'foo\\'oo';\n" 1937 "let x =\n" 1938 " 'foo\\'oo';", 1939 "let x=\"foo'oo\";\n" 1940 "let x=\"foo'oo\";", 1941 getGoogleJSStyleWithColumns(15)); 1942 } 1943 1944 TEST_F(FormatTestJS, RequoteStringsDouble) { 1945 FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1946 DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double; 1947 verifyFormat("var x = \"foo\";", DoubleQuotes); 1948 verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes); 1949 verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes); 1950 } 1951 1952 TEST_F(FormatTestJS, RequoteStringsLeave) { 1953 FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1954 LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave; 1955 verifyFormat("var x = \"foo\";", LeaveQuotes); 1956 verifyFormat("var x = 'foo';", LeaveQuotes); 1957 } 1958 1959 TEST_F(FormatTestJS, SupportShebangLines) { 1960 verifyFormat("#!/usr/bin/env node\n" 1961 "var x = hello();", 1962 "#!/usr/bin/env node\n" 1963 "var x = hello();"); 1964 } 1965 1966 TEST_F(FormatTestJS, NonNullAssertionOperator) { 1967 verifyFormat("let x = foo!.bar();\n"); 1968 verifyFormat("let x = foo ? bar! : baz;\n"); 1969 verifyFormat("let x = !foo;\n"); 1970 verifyFormat("let x = foo[0]!;\n"); 1971 verifyFormat("let x = (foo)!;\n"); 1972 verifyFormat("let x = x(foo!);\n"); 1973 verifyFormat( 1974 "a.aaaaaa(a.a!).then(\n" 1975 " x => x(x));\n", 1976 getGoogleJSStyleWithColumns(20)); 1977 verifyFormat("let x = foo! - 1;\n"); 1978 verifyFormat("let x = {foo: 1}!;\n"); 1979 verifyFormat( 1980 "let x = hello.foo()!\n" 1981 " .foo()!\n" 1982 " .foo()!\n" 1983 " .foo()!;\n", 1984 getGoogleJSStyleWithColumns(20)); 1985 verifyFormat("let x = namespace!;\n"); 1986 verifyFormat("return !!x;\n"); 1987 } 1988 1989 TEST_F(FormatTestJS, Conditional) { 1990 verifyFormat("y = x ? 1 : 2;"); 1991 verifyFormat("x ? 1 : 2;"); 1992 verifyFormat("class Foo {\n" 1993 " field = true ? 1 : 2;\n" 1994 " method(a = true ? 1 : 2) {}\n" 1995 "}"); 1996 } 1997 1998 TEST_F(FormatTestJS, ImportComments) { 1999 verifyFormat("import {x} from 'x'; // from some location", 2000 getGoogleJSStyleWithColumns(25)); 2001 verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10)); 2002 verifyFormat("/// <reference path=\"some/location\" />", getGoogleJSStyleWithColumns(10)); 2003 } 2004 2005 TEST_F(FormatTestJS, Exponentiation) { 2006 verifyFormat("squared = x ** 2;"); 2007 verifyFormat("squared **= 2;"); 2008 } 2009 2010 TEST_F(FormatTestJS, NestedLiterals) { 2011 FormatStyle FourSpaces = getGoogleJSStyleWithColumns(15); 2012 FourSpaces.IndentWidth = 4; 2013 verifyFormat("var l = [\n" 2014 " [\n" 2015 " 1,\n" 2016 " ],\n" 2017 "];", FourSpaces); 2018 verifyFormat("var l = [\n" 2019 " {\n" 2020 " 1: 1,\n" 2021 " },\n" 2022 "];", FourSpaces); 2023 verifyFormat("someFunction(\n" 2024 " p1,\n" 2025 " [\n" 2026 " 1,\n" 2027 " ],\n" 2028 ");", FourSpaces); 2029 verifyFormat("someFunction(\n" 2030 " p1,\n" 2031 " {\n" 2032 " 1: 1,\n" 2033 " },\n" 2034 ");", FourSpaces); 2035 verifyFormat("var o = {\n" 2036 " 1: 1,\n" 2037 " 2: {\n" 2038 " 3: 3,\n" 2039 " },\n" 2040 "};", FourSpaces); 2041 verifyFormat("var o = {\n" 2042 " 1: 1,\n" 2043 " 2: [\n" 2044 " 3,\n" 2045 " ],\n" 2046 "};", FourSpaces); 2047 } 2048 2049 } // end namespace tooling 2050 } // end namespace clang 2051