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