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