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 // Known issue: this should wrap after {}, but calculateBraceTypes 478 // misclassifies the first braces as a BK_BracedInit. 479 verifyFormat("function aFunction(){} {\n" 480 " let x = 1;\n" 481 " console.log(x);\n" 482 "}\n"); 483 } 484 485 TEST_F(FormatTestJS, GeneratorFunctions) { 486 verifyFormat("function* f() {\n" 487 " let x = 1;\n" 488 " yield x;\n" 489 " yield* something();\n" 490 " yield [1, 2];\n" 491 " yield {a: 1};\n" 492 "}"); 493 verifyFormat("function*\n" 494 " f() {\n" 495 "}", 496 getGoogleJSStyleWithColumns(8)); 497 verifyFormat("export function* f() {\n" 498 " yield 1;\n" 499 "}\n"); 500 verifyFormat("class X {\n" 501 " * generatorMethod() {\n" 502 " yield x;\n" 503 " }\n" 504 "}"); 505 verifyFormat("var x = {\n" 506 " a: function*() {\n" 507 " //\n" 508 " }\n" 509 "}\n"); 510 } 511 512 TEST_F(FormatTestJS, AsyncFunctions) { 513 verifyFormat("async function f() {\n" 514 " let x = 1;\n" 515 " return fetch(x);\n" 516 "}"); 517 verifyFormat("async function f() {\n" 518 " return 1;\n" 519 "}\n" 520 "\n" 521 "function a() {\n" 522 " return 1;\n" 523 "}\n", 524 " async function f() {\n" 525 " return 1;\n" 526 "}\n" 527 "\n" 528 " function a() {\n" 529 " return 1;\n" 530 "} \n"); 531 verifyFormat("async function* f() {\n" 532 " yield fetch(x);\n" 533 "}"); 534 verifyFormat("export async function f() {\n" 535 " return fetch(x);\n" 536 "}"); 537 verifyFormat("let x = async () => f();"); 538 verifyFormat("let x = async function() {\n" 539 " f();\n" 540 "};"); 541 verifyFormat("let x = async();"); 542 verifyFormat("class X {\n" 543 " async asyncMethod() {\n" 544 " return fetch(1);\n" 545 " }\n" 546 "}"); 547 verifyFormat("function initialize() {\n" 548 " // Comment.\n" 549 " return async.then();\n" 550 "}\n"); 551 verifyFormat("for async (const x of y) {\n" 552 " console.log(x);\n" 553 "}\n"); 554 verifyFormat("function asyncLoop() {\n" 555 " for async (const x of y) {\n" 556 " console.log(x);\n" 557 " }\n" 558 "}\n"); 559 560 } 561 562 TEST_F(FormatTestJS, FunctionParametersTrailingComma) { 563 verifyFormat("function trailingComma(\n" 564 " p1,\n" 565 " p2,\n" 566 " p3,\n" 567 ") {\n" 568 " a; //\n" 569 "}\n", 570 "function trailingComma(p1, p2, p3,) {\n" 571 " a; //\n" 572 "}\n"); 573 verifyFormat("trailingComma(\n" 574 " p1,\n" 575 " p2,\n" 576 " p3,\n" 577 ");\n", 578 "trailingComma(p1, p2, p3,);\n"); 579 verifyFormat("trailingComma(\n" 580 " p1 // hello\n" 581 ");\n", 582 "trailingComma(p1 // hello\n" 583 ");\n"); 584 } 585 586 TEST_F(FormatTestJS, ArrayLiterals) { 587 verifyFormat("var aaaaa: List<SomeThing> =\n" 588 " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];"); 589 verifyFormat("return [\n" 590 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 591 " ccccccccccccccccccccccccccc\n" 592 "];"); 593 verifyFormat("return [\n" 594 " aaaa().bbbbbbbb('A'),\n" 595 " aaaa().bbbbbbbb('B'),\n" 596 " aaaa().bbbbbbbb('C'),\n" 597 "];"); 598 verifyFormat("var someVariable = SomeFunction([\n" 599 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 600 " ccccccccccccccccccccccccccc\n" 601 "]);"); 602 verifyFormat("var someVariable = SomeFunction([\n" 603 " [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n" 604 "]);", 605 getGoogleJSStyleWithColumns(51)); 606 verifyFormat("var someVariable = SomeFunction(aaaa, [\n" 607 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 608 " ccccccccccccccccccccccccccc\n" 609 "]);"); 610 verifyFormat("var someVariable = SomeFunction(\n" 611 " aaaa,\n" 612 " [\n" 613 " aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 614 " cccccccccccccccccccccccccc\n" 615 " ],\n" 616 " aaaa);"); 617 verifyFormat("var aaaa = aaaaa || // wrap\n" 618 " [];"); 619 620 verifyFormat("someFunction([], {a: a});"); 621 622 verifyFormat("var string = [\n" 623 " 'aaaaaa',\n" 624 " 'bbbbbb',\n" 625 "].join('+');"); 626 } 627 628 TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) { 629 verifyFormat("var array = [\n" 630 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 631 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 632 "];"); 633 verifyFormat("var array = someFunction([\n" 634 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 635 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 636 "]);"); 637 } 638 639 TEST_F(FormatTestJS, FunctionLiterals) { 640 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 641 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 642 verifyFormat("doFoo(function() {});"); 643 verifyFormat("doFoo(function() { return 1; });", Style); 644 verifyFormat("var func = function() {\n" 645 " return 1;\n" 646 "};"); 647 verifyFormat("var func = //\n" 648 " function() {\n" 649 " return 1;\n" 650 "};"); 651 verifyFormat("return {\n" 652 " body: {\n" 653 " setAttribute: function(key, val) { this[key] = val; },\n" 654 " getAttribute: function(key) { return this[key]; },\n" 655 " style: {direction: ''}\n" 656 " }\n" 657 "};", 658 Style); 659 verifyFormat("abc = xyz ? function() {\n" 660 " return 1;\n" 661 "} : function() {\n" 662 " return -1;\n" 663 "};"); 664 665 verifyFormat("var closure = goog.bind(\n" 666 " function() { // comment\n" 667 " foo();\n" 668 " bar();\n" 669 " },\n" 670 " this, arg1IsReallyLongAndNeedsLineBreaks,\n" 671 " arg3IsReallyLongAndNeedsLineBreaks);"); 672 verifyFormat("var closure = goog.bind(function() { // comment\n" 673 " foo();\n" 674 " bar();\n" 675 "}, this);"); 676 verifyFormat("return {\n" 677 " a: 'E',\n" 678 " b: function() {\n" 679 " return function() {\n" 680 " f(); //\n" 681 " };\n" 682 " }\n" 683 "};"); 684 verifyFormat("{\n" 685 " var someVariable = function(x) {\n" 686 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 687 " };\n" 688 "}"); 689 verifyFormat("someLooooooooongFunction(\n" 690 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 691 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 692 " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 693 " // code\n" 694 " });"); 695 696 verifyFormat("return {\n" 697 " a: function SomeFunction() {\n" 698 " // ...\n" 699 " return 1;\n" 700 " }\n" 701 "};"); 702 verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 703 " .then(goog.bind(function(aaaaaaaaaaa) {\n" 704 " someFunction();\n" 705 " someFunction();\n" 706 " }, this), aaaaaaaaaaaaaaaaa);"); 707 708 verifyFormat("someFunction(goog.bind(function() {\n" 709 " doSomething();\n" 710 " doSomething();\n" 711 "}, this), goog.bind(function() {\n" 712 " doSomething();\n" 713 " doSomething();\n" 714 "}, this));"); 715 716 verifyFormat("SomeFunction(function() {\n" 717 " foo();\n" 718 " bar();\n" 719 "}.bind(this));"); 720 721 // FIXME: This is bad, we should be wrapping before "function() {". 722 verifyFormat("someFunction(function() {\n" 723 " doSomething(); // break\n" 724 "})\n" 725 " .doSomethingElse(\n" 726 " // break\n" 727 " );"); 728 729 Style.ColumnLimit = 33; 730 verifyFormat("f({a: function() { return 1; }});", Style); 731 Style.ColumnLimit = 32; 732 verifyFormat("f({\n" 733 " a: function() { return 1; }\n" 734 "});", 735 Style); 736 737 } 738 739 TEST_F(FormatTestJS, InliningFunctionLiterals) { 740 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 741 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 742 verifyFormat("var func = function() {\n" 743 " return 1;\n" 744 "};", 745 Style); 746 verifyFormat("var func = doSomething(function() { return 1; });", Style); 747 verifyFormat("var outer = function() {\n" 748 " var inner = function() { return 1; }\n" 749 "};", 750 Style); 751 verifyFormat("function outer1(a, b) {\n" 752 " function inner1(a, b) { return a; }\n" 753 "}", 754 Style); 755 756 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 757 verifyFormat("var func = function() { return 1; };", Style); 758 verifyFormat("var func = doSomething(function() { return 1; });", Style); 759 verifyFormat( 760 "var outer = function() { var inner = function() { return 1; } };", 761 Style); 762 verifyFormat("function outer1(a, b) {\n" 763 " function inner1(a, b) { return a; }\n" 764 "}", 765 Style); 766 767 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 768 verifyFormat("var func = function() {\n" 769 " return 1;\n" 770 "};", 771 Style); 772 verifyFormat("var func = doSomething(function() {\n" 773 " return 1;\n" 774 "});", 775 Style); 776 verifyFormat("var outer = function() {\n" 777 " var inner = function() {\n" 778 " return 1;\n" 779 " }\n" 780 "};", 781 Style); 782 verifyFormat("function outer1(a, b) {\n" 783 " function inner1(a, b) {\n" 784 " return a;\n" 785 " }\n" 786 "}", 787 Style); 788 789 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 790 verifyFormat("var func = function() {\n" 791 " return 1;\n" 792 "};", 793 Style); 794 } 795 796 TEST_F(FormatTestJS, MultipleFunctionLiterals) { 797 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 798 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 799 verifyFormat("promise.then(\n" 800 " function success() {\n" 801 " doFoo();\n" 802 " doBar();\n" 803 " },\n" 804 " function error() {\n" 805 " doFoo();\n" 806 " doBaz();\n" 807 " },\n" 808 " []);\n"); 809 verifyFormat("promise.then(\n" 810 " function success() {\n" 811 " doFoo();\n" 812 " doBar();\n" 813 " },\n" 814 " [],\n" 815 " function error() {\n" 816 " doFoo();\n" 817 " doBaz();\n" 818 " });\n"); 819 verifyFormat("promise.then(\n" 820 " [],\n" 821 " function success() {\n" 822 " doFoo();\n" 823 " doBar();\n" 824 " },\n" 825 " function error() {\n" 826 " doFoo();\n" 827 " doBaz();\n" 828 " });\n"); 829 830 verifyFormat("getSomeLongPromise()\n" 831 " .then(function(value) { body(); })\n" 832 " .thenCatch(function(error) {\n" 833 " body();\n" 834 " body();\n" 835 " });", 836 Style); 837 verifyFormat("getSomeLongPromise()\n" 838 " .then(function(value) {\n" 839 " body();\n" 840 " body();\n" 841 " })\n" 842 " .thenCatch(function(error) {\n" 843 " body();\n" 844 " body();\n" 845 " });"); 846 847 verifyFormat("getSomeLongPromise()\n" 848 " .then(function(value) { body(); })\n" 849 " .thenCatch(function(error) { body(); });", 850 Style); 851 852 verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n" 853 " .aaaaaaa(function() {\n" 854 " //\n" 855 " })\n" 856 " .bbbbbb();"); 857 } 858 859 TEST_F(FormatTestJS, ArrowFunctions) { 860 verifyFormat("var x = (a) => {\n" 861 " return a;\n" 862 "};"); 863 verifyFormat("var x = (a) => {\n" 864 " function y() {\n" 865 " return 42;\n" 866 " }\n" 867 " return a;\n" 868 "};"); 869 verifyFormat("var x = (a: type): {some: type} => {\n" 870 " return a;\n" 871 "};"); 872 verifyFormat("var x = (a) => a;"); 873 verifyFormat("return () => [];"); 874 verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n" 875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 876 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n" 878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 879 "};"); 880 verifyFormat("var a = a.aaaaaaa(\n" 881 " (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n" 882 " aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 883 verifyFormat("var a = a.aaaaaaa(\n" 884 " (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n" 885 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n" 886 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 887 888 // FIXME: This is bad, we should be wrapping before "() => {". 889 verifyFormat("someFunction(() => {\n" 890 " doSomething(); // break\n" 891 "})\n" 892 " .doSomethingElse(\n" 893 " // break\n" 894 " );"); 895 } 896 897 TEST_F(FormatTestJS, ReturnStatements) { 898 verifyFormat("function() {\n" 899 " return [hello, world];\n" 900 "}"); 901 } 902 903 TEST_F(FormatTestJS, ForLoops) { 904 verifyFormat("for (var i in [2, 3]) {\n" 905 "}"); 906 verifyFormat("for (var i of [2, 3]) {\n" 907 "}"); 908 verifyFormat("for (let {a, b} of x) {\n" 909 "}"); 910 verifyFormat("for (let {a, b} in x) {\n" 911 "}"); 912 } 913 914 TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) { 915 // The following statements must not wrap, as otherwise the program meaning 916 // would change due to automatic semicolon insertion. 917 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1. 918 verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10)); 919 verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10)); 920 verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10)); 921 verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10)); 922 verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10)); 923 verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10)); 924 verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10)); 925 verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10)); 926 verifyFormat("return [\n" 927 " aaa\n" 928 "];", 929 getGoogleJSStyleWithColumns(12)); 930 } 931 932 TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) { 933 verifyFormat("a\n" 934 "b;", 935 " a \n" 936 " b ;"); 937 verifyFormat("a()\n" 938 "b;", 939 " a ()\n" 940 " b ;"); 941 verifyFormat("a[b]\n" 942 "c;", 943 "a [b]\n" 944 "c ;"); 945 verifyFormat("1\n" 946 "a;", 947 "1 \n" 948 "a ;"); 949 verifyFormat("a\n" 950 "1;", 951 "a \n" 952 "1 ;"); 953 verifyFormat("a\n" 954 "'x';", 955 "a \n" 956 " 'x';"); 957 verifyFormat("a++\n" 958 "b;", 959 "a ++\n" 960 "b ;"); 961 verifyFormat("a\n" 962 "!b && c;", 963 "a \n" 964 " ! b && c;"); 965 verifyFormat("a\n" 966 "if (1) f();", 967 " a\n" 968 " if (1) f();"); 969 verifyFormat("a\n" 970 "class X {}", 971 " a\n" 972 " class X {}"); 973 verifyFormat("var a", "var\n" 974 "a"); 975 verifyFormat("x instanceof String", "x\n" 976 "instanceof\n" 977 "String"); 978 verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n" 979 " bar) {}"); 980 verifyFormat("a = true\n" 981 "return 1", 982 "a = true\n" 983 " return 1"); 984 verifyFormat("a = 's'\n" 985 "return 1", 986 "a = 's'\n" 987 " return 1"); 988 verifyFormat("a = null\n" 989 "return 1", 990 "a = null\n" 991 " return 1"); 992 // Below "class Y {}" should ideally be on its own line. 993 verifyFormat( 994 "x = {\n" 995 " a: 1\n" 996 "} class Y {}", 997 " x = {a : 1}\n" 998 " class Y { }"); 999 verifyFormat( 1000 "if (x) {\n" 1001 "}\n" 1002 "return 1", 1003 "if (x) {}\n" 1004 " return 1"); 1005 verifyFormat( 1006 "if (x) {\n" 1007 "}\n" 1008 "class X {}", 1009 "if (x) {}\n" 1010 " class X {}"); 1011 } 1012 1013 TEST_F(FormatTestJS, ImportExportASI) { 1014 verifyFormat( 1015 "import {x} from 'y'\n" 1016 "export function z() {}", 1017 "import {x} from 'y'\n" 1018 " export function z() {}"); 1019 // Below "class Y {}" should ideally be on its own line. 1020 verifyFormat( 1021 "export {x} class Y {}", 1022 " export {x}\n" 1023 " class Y {\n}"); 1024 verifyFormat( 1025 "if (x) {\n" 1026 "}\n" 1027 "export class Y {}", 1028 "if ( x ) { }\n" 1029 " export class Y {}"); 1030 } 1031 1032 TEST_F(FormatTestJS, ClosureStyleCasts) { 1033 verifyFormat("var x = /** @type {foo} */ (bar);"); 1034 } 1035 1036 TEST_F(FormatTestJS, TryCatch) { 1037 verifyFormat("try {\n" 1038 " f();\n" 1039 "} catch (e) {\n" 1040 " g();\n" 1041 "} finally {\n" 1042 " h();\n" 1043 "}"); 1044 1045 // But, of course, "catch" is a perfectly fine function name in JavaScript. 1046 verifyFormat("someObject.catch();"); 1047 verifyFormat("someObject.new();"); 1048 verifyFormat("someObject.delete();"); 1049 } 1050 1051 TEST_F(FormatTestJS, StringLiteralConcatenation) { 1052 verifyFormat("var literal = 'hello ' +\n" 1053 " 'world';"); 1054 } 1055 1056 TEST_F(FormatTestJS, RegexLiteralClassification) { 1057 // Regex literals. 1058 verifyFormat("var regex = /abc/;"); 1059 verifyFormat("f(/abc/);"); 1060 verifyFormat("f(abc, /abc/);"); 1061 verifyFormat("some_map[/abc/];"); 1062 verifyFormat("var x = a ? /abc/ : /abc/;"); 1063 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 1064 verifyFormat("var x = !/abc/.test(y);"); 1065 verifyFormat("var x = foo()! / 10;"); 1066 verifyFormat("var x = a && /abc/.test(y);"); 1067 verifyFormat("var x = a || /abc/.test(y);"); 1068 verifyFormat("var x = a + /abc/.search(y);"); 1069 verifyFormat("/abc/.search(y);"); 1070 verifyFormat("var regexs = {/abc/, /abc/};"); 1071 verifyFormat("return /abc/;"); 1072 1073 // Not regex literals. 1074 verifyFormat("var a = a / 2 + b / 3;"); 1075 verifyFormat("var a = a++ / 2;"); 1076 // Prefix unary can operate on regex literals, not that it makes sense. 1077 verifyFormat("var a = ++/a/;"); 1078 1079 // This is a known issue, regular expressions are incorrectly detected if 1080 // directly following a closing parenthesis. 1081 verifyFormat("if (foo) / bar /.exec(baz);"); 1082 } 1083 1084 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 1085 verifyFormat("var regex = /=/;"); 1086 verifyFormat("var regex = /a*/;"); 1087 verifyFormat("var regex = /a+/;"); 1088 verifyFormat("var regex = /a?/;"); 1089 verifyFormat("var regex = /.a./;"); 1090 verifyFormat("var regex = /a\\*/;"); 1091 verifyFormat("var regex = /^a$/;"); 1092 verifyFormat("var regex = /\\/a/;"); 1093 verifyFormat("var regex = /(?:x)/;"); 1094 verifyFormat("var regex = /x(?=y)/;"); 1095 verifyFormat("var regex = /x(?!y)/;"); 1096 verifyFormat("var regex = /x|y/;"); 1097 verifyFormat("var regex = /a{2}/;"); 1098 verifyFormat("var regex = /a{1,3}/;"); 1099 1100 verifyFormat("var regex = /[abc]/;"); 1101 verifyFormat("var regex = /[^abc]/;"); 1102 verifyFormat("var regex = /[\\b]/;"); 1103 verifyFormat("var regex = /[/]/;"); 1104 verifyFormat("var regex = /[\\/]/;"); 1105 verifyFormat("var regex = /\\[/;"); 1106 verifyFormat("var regex = /\\\\[/]/;"); 1107 verifyFormat("var regex = /}[\"]/;"); 1108 verifyFormat("var regex = /}[/\"]/;"); 1109 verifyFormat("var regex = /}[\"/]/;"); 1110 1111 verifyFormat("var regex = /\\b/;"); 1112 verifyFormat("var regex = /\\B/;"); 1113 verifyFormat("var regex = /\\d/;"); 1114 verifyFormat("var regex = /\\D/;"); 1115 verifyFormat("var regex = /\\f/;"); 1116 verifyFormat("var regex = /\\n/;"); 1117 verifyFormat("var regex = /\\r/;"); 1118 verifyFormat("var regex = /\\s/;"); 1119 verifyFormat("var regex = /\\S/;"); 1120 verifyFormat("var regex = /\\t/;"); 1121 verifyFormat("var regex = /\\v/;"); 1122 verifyFormat("var regex = /\\w/;"); 1123 verifyFormat("var regex = /\\W/;"); 1124 verifyFormat("var regex = /a(a)\\1/;"); 1125 verifyFormat("var regex = /\\0/;"); 1126 verifyFormat("var regex = /\\\\/g;"); 1127 verifyFormat("var regex = /\\a\\\\/g;"); 1128 verifyFormat("var regex = /\a\\//g;"); 1129 verifyFormat("var regex = /a\\//;\n" 1130 "var x = 0;"); 1131 verifyFormat("var regex = /'/g;", "var regex = /'/g ;"); 1132 verifyFormat("var regex = /'/g; //'", "var regex = /'/g ; //'"); 1133 verifyFormat("var regex = /\\/*/;\n" 1134 "var x = 0;", 1135 "var regex = /\\/*/;\n" 1136 "var x=0;"); 1137 verifyFormat("var x = /a\\//;", "var x = /a\\// \n;"); 1138 verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16)); 1139 verifyFormat("var regex =\n" 1140 " /\"/;", 1141 getGoogleJSStyleWithColumns(15)); 1142 verifyFormat("var regex = //\n" 1143 " /a/;"); 1144 verifyFormat("var regexs = [\n" 1145 " /d/, //\n" 1146 " /aa/, //\n" 1147 "];"); 1148 } 1149 1150 TEST_F(FormatTestJS, RegexLiteralModifiers) { 1151 verifyFormat("var regex = /abc/g;"); 1152 verifyFormat("var regex = /abc/i;"); 1153 verifyFormat("var regex = /abc/m;"); 1154 verifyFormat("var regex = /abc/y;"); 1155 } 1156 1157 TEST_F(FormatTestJS, RegexLiteralLength) { 1158 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 1159 getGoogleJSStyleWithColumns(60)); 1160 verifyFormat("var regex =\n" 1161 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 1162 getGoogleJSStyleWithColumns(60)); 1163 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 1164 getGoogleJSStyleWithColumns(50)); 1165 } 1166 1167 TEST_F(FormatTestJS, RegexLiteralExamples) { 1168 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 1169 } 1170 1171 TEST_F(FormatTestJS, IgnoresMpegTS) { 1172 std::string MpegTS(200, ' '); 1173 MpegTS.replace(0, strlen("nearlyLooks + like + ts + code; "), 1174 "nearlyLooks + like + ts + code; "); 1175 MpegTS[0] = 0x47; 1176 MpegTS[188] = 0x47; 1177 verifyFormat(MpegTS, MpegTS); 1178 } 1179 1180 TEST_F(FormatTestJS, TypeAnnotations) { 1181 verifyFormat("var x: string;"); 1182 verifyFormat("var x: {a: string; b: number;} = {};"); 1183 verifyFormat("function x(): string {\n return 'x';\n}"); 1184 verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}"); 1185 verifyFormat("function x(y: string): string {\n return 'x';\n}"); 1186 verifyFormat("for (var y: string in x) {\n x();\n}"); 1187 verifyFormat("for (var y: string of x) {\n x();\n}"); 1188 verifyFormat("function x(y: {a?: number;} = {}): number {\n" 1189 " return 12;\n" 1190 "}"); 1191 verifyFormat("((a: string, b: number): string => a + b);"); 1192 verifyFormat("var x: (y: number) => string;"); 1193 verifyFormat("var x: P<string, (a: number) => string>;"); 1194 verifyFormat("var x = {\n" 1195 " y: function(): z {\n" 1196 " return 1;\n" 1197 " }\n" 1198 "};"); 1199 verifyFormat("var x = {\n" 1200 " y: function(): {a: number} {\n" 1201 " return 1;\n" 1202 " }\n" 1203 "};"); 1204 verifyFormat("function someFunc(args: string[]):\n" 1205 " {longReturnValue: string[]} {}", 1206 getGoogleJSStyleWithColumns(60)); 1207 verifyFormat( 1208 "var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])\n" 1209 " .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1210 } 1211 1212 TEST_F(FormatTestJS, UnionIntersectionTypes) { 1213 verifyFormat("let x: A|B = A | B;"); 1214 verifyFormat("let x: A&B|C = A & B;"); 1215 verifyFormat("let x: Foo<A|B> = new Foo<A|B>();"); 1216 verifyFormat("function(x: A|B): C&D {}"); 1217 verifyFormat("function(x: A|B = A | B): C&D {}"); 1218 verifyFormat("function x(path: number|string) {}"); 1219 verifyFormat("function x(): string|number {}"); 1220 verifyFormat("type Foo = Bar|Baz;"); 1221 verifyFormat("type Foo = Bar<X>|Baz;"); 1222 verifyFormat("type Foo = (Bar<X>|Baz);"); 1223 verifyFormat("let x: Bar|Baz;"); 1224 verifyFormat("let x: Bar<X>|Baz;"); 1225 verifyFormat("let x: (Foo|Bar)[];"); 1226 } 1227 1228 TEST_F(FormatTestJS, ClassDeclarations) { 1229 verifyFormat("class C {\n x: string = 12;\n}"); 1230 verifyFormat("class C {\n x(): string => 12;\n}"); 1231 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); 1232 verifyFormat("class C {\n private x: string = 12;\n}"); 1233 verifyFormat("class C {\n private static x: string = 12;\n}"); 1234 verifyFormat("class C {\n static x(): string {\n return 'asd';\n }\n}"); 1235 verifyFormat("class C extends P implements I {}"); 1236 verifyFormat("class C extends p.P implements i.I {}"); 1237 verifyFormat( 1238 "x(class {\n" 1239 " a(): A {}\n" 1240 "});"); 1241 verifyFormat("class Test {\n" 1242 " aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n" 1243 " aaaaaaaaaaaaaaaaaaaaaa {}\n" 1244 "}"); 1245 verifyFormat("foo = class Name {\n" 1246 " constructor() {}\n" 1247 "};"); 1248 verifyFormat("foo = class {\n" 1249 " constructor() {}\n" 1250 "};"); 1251 verifyFormat("class C {\n" 1252 " x: {y: Z;} = {};\n" 1253 " private y: {y: Z;} = {};\n" 1254 "}"); 1255 1256 // ':' is not a type declaration here. 1257 verifyFormat("class X {\n" 1258 " subs = {\n" 1259 " 'b': {\n" 1260 " 'c': 1,\n" 1261 " },\n" 1262 " };\n" 1263 "}"); 1264 verifyFormat("@Component({\n" 1265 " moduleId: module.id,\n" 1266 "})\n" 1267 "class SessionListComponent implements OnDestroy, OnInit {\n" 1268 "}"); 1269 } 1270 1271 TEST_F(FormatTestJS, InterfaceDeclarations) { 1272 verifyFormat("interface I {\n" 1273 " x: string;\n" 1274 " enum: string[];\n" 1275 " enum?: string[];\n" 1276 "}\n" 1277 "var y;"); 1278 // Ensure that state is reset after parsing the interface. 1279 verifyFormat("interface a {}\n" 1280 "export function b() {}\n" 1281 "var x;"); 1282 1283 // Arrays of object type literals. 1284 verifyFormat("interface I {\n" 1285 " o: {}[];\n" 1286 "}"); 1287 } 1288 1289 TEST_F(FormatTestJS, EnumDeclarations) { 1290 verifyFormat("enum Foo {\n" 1291 " A = 1,\n" 1292 " B\n" 1293 "}"); 1294 verifyFormat("export /* somecomment*/ enum Foo {\n" 1295 " A = 1,\n" 1296 " B\n" 1297 "}"); 1298 verifyFormat("enum Foo {\n" 1299 " A = 1, // comment\n" 1300 " B\n" 1301 "}\n" 1302 "var x = 1;"); 1303 } 1304 1305 TEST_F(FormatTestJS, MetadataAnnotations) { 1306 verifyFormat("@A\nclass C {\n}"); 1307 verifyFormat("@A({arg: 'value'})\nclass C {\n}"); 1308 verifyFormat("@A\n@B\nclass C {\n}"); 1309 verifyFormat("class C {\n @A x: string;\n}"); 1310 verifyFormat("class C {\n" 1311 " @A\n" 1312 " private x(): string {\n" 1313 " return 'y';\n" 1314 " }\n" 1315 "}"); 1316 verifyFormat("class C {\n" 1317 " private x(@A x: string) {}\n" 1318 "}"); 1319 verifyFormat("class X {}\n" 1320 "class Y {}"); 1321 verifyFormat("class X {\n" 1322 " @property() private isReply = false;\n" 1323 "}\n"); 1324 } 1325 1326 TEST_F(FormatTestJS, TypeAliases) { 1327 verifyFormat("type X = number;\n" 1328 "class C {}"); 1329 verifyFormat("type X<Y> = Z<Y>;"); 1330 verifyFormat("type X = {\n" 1331 " y: number\n" 1332 "};\n" 1333 "class C {}"); 1334 } 1335 1336 TEST_F(FormatTestJS, TypeInterfaceLineWrapping) { 1337 const FormatStyle &Style = getGoogleJSStyleWithColumns(20); 1338 verifyFormat("type LongTypeIsReallyUnreasonablyLong =\n" 1339 " string;\n", 1340 "type LongTypeIsReallyUnreasonablyLong = string;\n", 1341 Style); 1342 verifyFormat( 1343 "interface AbstractStrategyFactoryProvider {\n" 1344 " a: number\n" 1345 "}\n", 1346 "interface AbstractStrategyFactoryProvider { a: number }\n", 1347 Style); 1348 } 1349 1350 TEST_F(FormatTestJS, Modules) { 1351 verifyFormat("import SomeThing from 'some/module.js';"); 1352 verifyFormat("import {X, Y} from 'some/module.js';"); 1353 verifyFormat("import a, {X, Y} from 'some/module.js';"); 1354 verifyFormat("import {X, Y,} from 'some/module.js';"); 1355 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); 1356 // Ensure Automatic Semicolon Insertion does not break on "as\n". 1357 verifyFormat("import {X as myX} from 'm';", "import {X as\n" 1358 " myX} from 'm';"); 1359 verifyFormat("import * as lib from 'some/module.js';"); 1360 verifyFormat("var x = {import: 1};\nx.import = 2;"); 1361 1362 verifyFormat("export function fn() {\n" 1363 " return 'fn';\n" 1364 "}"); 1365 verifyFormat("export function A() {}\n" 1366 "export default function B() {}\n" 1367 "export function C() {}"); 1368 verifyFormat("export default () => {\n" 1369 " let x = 1;\n" 1370 " return x;\n" 1371 "}"); 1372 verifyFormat("export const x = 12;"); 1373 verifyFormat("export default class X {}"); 1374 verifyFormat("export {X, Y} from 'some/module.js';"); 1375 verifyFormat("export {X, Y,} from 'some/module.js';"); 1376 verifyFormat("export {SomeVeryLongExport as X, " 1377 "SomeOtherVeryLongExport as Y} from 'some/module.js';"); 1378 // export without 'from' is wrapped. 1379 verifyFormat("export let someRatherLongVariableName =\n" 1380 " someSurprisinglyLongVariable + someOtherRatherLongVar;"); 1381 // ... but not if from is just an identifier. 1382 verifyFormat("export {\n" 1383 " from as from,\n" 1384 " someSurprisinglyLongVariable as\n" 1385 " from\n" 1386 "};", 1387 getGoogleJSStyleWithColumns(20)); 1388 verifyFormat("export class C {\n" 1389 " x: number;\n" 1390 " y: string;\n" 1391 "}"); 1392 verifyFormat("export class X { y: number; }"); 1393 verifyFormat("export abstract class X { y: number; }"); 1394 verifyFormat("export default class X { y: number }"); 1395 verifyFormat("export default function() {\n return 1;\n}"); 1396 verifyFormat("export var x = 12;"); 1397 verifyFormat("class C {}\n" 1398 "export function f() {}\n" 1399 "var v;"); 1400 verifyFormat("export var x: number = 12;"); 1401 verifyFormat("export const y = {\n" 1402 " a: 1,\n" 1403 " b: 2\n" 1404 "};"); 1405 verifyFormat("export enum Foo {\n" 1406 " BAR,\n" 1407 " // adsdasd\n" 1408 " BAZ\n" 1409 "}"); 1410 verifyFormat("export default [\n" 1411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1412 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 1413 "];"); 1414 verifyFormat("export default [];"); 1415 verifyFormat("export default () => {};"); 1416 verifyFormat("export interface Foo { foo: number; }\n" 1417 "export class Bar {\n" 1418 " blah(): string {\n" 1419 " return this.blah;\n" 1420 " };\n" 1421 "}"); 1422 } 1423 1424 TEST_F(FormatTestJS, ImportWrapping) { 1425 verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying," 1426 " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying" 1427 "} from 'some/module.js';"); 1428 FormatStyle Style = getGoogleJSStyleWithColumns(80); 1429 Style.JavaScriptWrapImports = true; 1430 verifyFormat("import {\n" 1431 " VeryLongImportsAreAnnoying,\n" 1432 " VeryLongImportsAreAnnoying,\n" 1433 " VeryLongImportsAreAnnoying,\n" 1434 "} from 'some/module.js';", 1435 Style); 1436 verifyFormat("import {\n" 1437 " A,\n" 1438 " A,\n" 1439 "} from 'some/module.js';", 1440 Style); 1441 verifyFormat("export {\n" 1442 " A,\n" 1443 " A,\n" 1444 "} from 'some/module.js';", 1445 Style); 1446 } 1447 1448 TEST_F(FormatTestJS, TemplateStrings) { 1449 // Keeps any whitespace/indentation within the template string. 1450 verifyFormat("var x = `hello\n" 1451 " ${name}\n" 1452 " !`;", 1453 "var x = `hello\n" 1454 " ${ name }\n" 1455 " !`;"); 1456 1457 verifyFormat("var x =\n" 1458 " `hello ${world}` >= some();", 1459 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. 1460 verifyFormat("var x = `hello ${world}` >= some();", 1461 getGoogleJSStyleWithColumns(35)); // Barely fits. 1462 verifyFormat("var x = `hellö ${wörld}` >= söme();", 1463 getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8. 1464 verifyFormat("var x = `hello\n" 1465 " ${world}` >=\n" 1466 " some();", 1467 "var x =\n" 1468 " `hello\n" 1469 " ${world}` >= some();", 1470 getGoogleJSStyleWithColumns(21)); // Barely doesn't fit. 1471 verifyFormat("var x = `hello\n" 1472 " ${world}` >= some();", 1473 "var x =\n" 1474 " `hello\n" 1475 " ${world}` >= some();", 1476 getGoogleJSStyleWithColumns(22)); // Barely fits. 1477 1478 verifyFormat("var x =\n" 1479 " `h`;", 1480 getGoogleJSStyleWithColumns(11)); 1481 verifyFormat("var x =\n `multi\n line`;", "var x = `multi\n line`;", 1482 getGoogleJSStyleWithColumns(13)); 1483 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1484 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);"); 1485 // Repro for an obscure width-miscounting issue with template strings. 1486 verifyFormat( 1487 "someLongVariable =\n" 1488 " " 1489 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;", 1490 "someLongVariable = " 1491 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;"); 1492 1493 // Make sure template strings get a proper ColumnWidth assigned, even if they 1494 // are first token in line. 1495 verifyFormat( 1496 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1497 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); 1498 1499 // Two template strings. 1500 verifyFormat("var x = `hello` == `hello`;"); 1501 1502 // Comments in template strings. 1503 verifyFormat("var x = `//a`;\n" 1504 "var y;", 1505 "var x =\n `//a`;\n" 1506 "var y ;"); 1507 verifyFormat("var x = `/*a`;\n" 1508 "var y;", 1509 "var x =\n `/*a`;\n" 1510 "var y;"); 1511 // Unterminated string literals in a template string. 1512 verifyFormat("var x = `'`; // comment with matching quote '\n" 1513 "var y;"); 1514 verifyFormat("var x = `\"`; // comment with matching quote \"\n" 1515 "var y;"); 1516 verifyFormat("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa);", 1517 "it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa) ;", 1518 getGoogleJSStyleWithColumns(40)); 1519 // Backticks in a comment - not a template string. 1520 verifyFormat("var x = 1 // `/*a`;\n" 1521 " ;", 1522 "var x =\n 1 // `/*a`;\n" 1523 " ;"); 1524 verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */"); 1525 // Comment spans multiple template strings. 1526 verifyFormat("var x = `/*a`;\n" 1527 "var y = ` */ `;", 1528 "var x =\n `/*a`;\n" 1529 "var y =\n ` */ `;"); 1530 // Escaped backtick. 1531 verifyFormat("var x = ` \\` a`;\n" 1532 "var y;", 1533 "var x = ` \\` a`;\n" 1534 "var y;"); 1535 // Escaped dollar. 1536 verifyFormat("var x = ` \\${foo}`;\n"); 1537 1538 // The token stream can contain two string_literals in sequence, but that 1539 // doesn't mean that they are implicitly concatenated in JavaScript. 1540 verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;"); 1541 1542 // Ensure that scopes are appropriately set around evaluated expressions in 1543 // template strings. 1544 verifyFormat("var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa\n" 1545 " aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;", 1546 "var f = `aaaaaaaaaaaaa:${aaaaaaa. aaaaa} aaaaaaaa\n" 1547 " aaaaaaaaaaaaa:${ aaaaaaa. aaaaa} aaaaaaaa`;"); 1548 verifyFormat("var x = someFunction(`${})`) //\n" 1549 " .oooooooooooooooooon();"); 1550 verifyFormat("var x = someFunction(`${aaaa}${\n" 1551 " aaaaa( //\n" 1552 " aaaaa)\n" 1553 " })`);"); 1554 } 1555 1556 TEST_F(FormatTestJS, TemplateStringMultiLineExpression) { 1557 verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${\n" 1558 " aaaaa + //\n" 1559 " bbbb\n" 1560 " }`;", 1561 "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa + //\n" 1562 " bbbb}`;"); 1563 verifyFormat("var f = `\n" 1564 " aaaaaaaaaaaaaaaaaa: ${\n" 1565 " aaaaa + //\n" 1566 " bbbb\n" 1567 " }`;", 1568 "var f = `\n" 1569 " aaaaaaaaaaaaaaaaaa: ${ aaaaa + //\n" 1570 " bbbb }`;"); 1571 verifyFormat("var f = `\n" 1572 " aaaaaaaaaaaaaaaaaa: ${\n" 1573 " someFunction(\n" 1574 " aaaaa + //\n" 1575 " bbbb)\n" 1576 " }`;", 1577 "var f = `\n" 1578 " aaaaaaaaaaaaaaaaaa: ${someFunction (\n" 1579 " aaaaa + //\n" 1580 " bbbb)}`;"); 1581 1582 // It might be preferable to wrap before "someFunction". 1583 verifyFormat("var f = `\n" 1584 " aaaaaaaaaaaaaaaaaa: ${someFunction({\n" 1585 " aaaa: aaaaa,\n" 1586 " bbbb: bbbbb,\n" 1587 " })}`;", 1588 "var f = `\n" 1589 " aaaaaaaaaaaaaaaaaa: ${someFunction ({\n" 1590 " aaaa: aaaaa,\n" 1591 " bbbb: bbbbb,\n" 1592 " })}`;"); 1593 } 1594 1595 TEST_F(FormatTestJS, TemplateStringASI) { 1596 verifyFormat("var x = `hello${world}`;", "var x = `hello${\n" 1597 " world\n" 1598 "}`;"); 1599 } 1600 1601 TEST_F(FormatTestJS, NestedTemplateStrings) { 1602 verifyFormat( 1603 "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;"); 1604 verifyFormat("var x = `he${({text: 'll'}.text)}o`;"); 1605 1606 // Crashed at some point. 1607 verifyFormat("}"); 1608 } 1609 1610 TEST_F(FormatTestJS, TaggedTemplateStrings) { 1611 verifyFormat("var x = html`<ul>`;"); 1612 } 1613 1614 TEST_F(FormatTestJS, CastSyntax) { 1615 verifyFormat("var x = <type>foo;"); 1616 verifyFormat("var x = foo as type;"); 1617 verifyFormat("let x = (a + b) as\n" 1618 " LongTypeIsLong;", 1619 getGoogleJSStyleWithColumns(20)); 1620 verifyFormat("foo = <Bar[]>[\n" 1621 " 1, //\n" 1622 " 2\n" 1623 "];"); 1624 verifyFormat("var x = [{x: 1} as type];"); 1625 verifyFormat("x = x as [a, b];"); 1626 verifyFormat("x = x as {a: string};"); 1627 verifyFormat("x = x as (string);"); 1628 verifyFormat("x = x! as (string);"); 1629 verifyFormat("var x = something.someFunction() as\n" 1630 " something;", 1631 getGoogleJSStyleWithColumns(40)); 1632 } 1633 1634 TEST_F(FormatTestJS, TypeArguments) { 1635 verifyFormat("class X<Y> {}"); 1636 verifyFormat("new X<Y>();"); 1637 verifyFormat("foo<Y>(a);"); 1638 verifyFormat("var x: X<Y>[];"); 1639 verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); 1640 verifyFormat("function f(a: List<any> = null) {}"); 1641 verifyFormat("function f(): List<any> {}"); 1642 verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n" 1643 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}"); 1644 verifyFormat("function aaaaaaaaaa(\n" 1645 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n" 1646 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n" 1647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}"); 1648 } 1649 1650 TEST_F(FormatTestJS, UserDefinedTypeGuards) { 1651 verifyFormat( 1652 "function foo(check: Object):\n" 1653 " check is {foo: string, bar: string, baz: string, foobar: string} {\n" 1654 " return 'bar' in check;\n" 1655 "}\n"); 1656 } 1657 1658 TEST_F(FormatTestJS, OptionalTypes) { 1659 verifyFormat("function x(a?: b, c?, d?) {}"); 1660 verifyFormat("class X {\n" 1661 " y?: z;\n" 1662 " z?;\n" 1663 "}"); 1664 verifyFormat("interface X {\n" 1665 " y?(): z;\n" 1666 "}"); 1667 verifyFormat("constructor({aa}: {\n" 1668 " aa?: string,\n" 1669 " aaaaaaaa?: string,\n" 1670 " aaaaaaaaaaaaaaa?: boolean,\n" 1671 " aaaaaa?: List<string>\n" 1672 "}) {}"); 1673 } 1674 1675 TEST_F(FormatTestJS, IndexSignature) { 1676 verifyFormat("var x: {[k: string]: v};"); 1677 } 1678 1679 TEST_F(FormatTestJS, WrapAfterParen) { 1680 verifyFormat("xxxxxxxxxxx(\n" 1681 " aaa, aaa);", 1682 getGoogleJSStyleWithColumns(20)); 1683 verifyFormat("xxxxxxxxxxx(\n" 1684 " aaa, aaa, aaa,\n" 1685 " aaa, aaa, aaa);", 1686 getGoogleJSStyleWithColumns(20)); 1687 verifyFormat("xxxxxxxxxxx(\n" 1688 " aaaaaaaaaaaaaaaaaaaaaaaa,\n" 1689 " function(x) {\n" 1690 " y(); //\n" 1691 " });", 1692 getGoogleJSStyleWithColumns(40)); 1693 verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 1694 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 1695 } 1696 1697 TEST_F(FormatTestJS, JSDocAnnotations) { 1698 verifyFormat("/**\n" 1699 " * @export {this.is.a.long.path.to.a.Type}\n" 1700 " */", 1701 "/**\n" 1702 " * @export {this.is.a.long.path.to.a.Type}\n" 1703 " */", 1704 getGoogleJSStyleWithColumns(20)); 1705 verifyFormat("/**\n" 1706 " * @mods {this.is.a.long.path.to.a.Type}\n" 1707 " */", 1708 "/**\n" 1709 " * @mods {this.is.a.long.path.to.a.Type}\n" 1710 " */", 1711 getGoogleJSStyleWithColumns(20)); 1712 verifyFormat("/**\n" 1713 " * @param {this.is.a.long.path.to.a.Type}\n" 1714 " */", 1715 "/**\n" 1716 " * @param {this.is.a.long.path.to.a.Type}\n" 1717 " */", 1718 getGoogleJSStyleWithColumns(20)); 1719 verifyFormat("/**\n" 1720 " * @see http://very/very/long/url/is/long\n" 1721 " */", 1722 "/**\n" 1723 " * @see http://very/very/long/url/is/long\n" 1724 " */", 1725 getGoogleJSStyleWithColumns(20)); 1726 verifyFormat( 1727 "/**\n" 1728 " * @param This is a\n" 1729 " * long comment but\n" 1730 " * no type\n" 1731 " */", 1732 "/**\n" 1733 " * @param This is a long comment but no type\n" 1734 " */", 1735 getGoogleJSStyleWithColumns(20)); 1736 // Don't break @param line, but reindent it and reflow unrelated lines. 1737 verifyFormat("{\n" 1738 " /**\n" 1739 " * long long long\n" 1740 " * long\n" 1741 " * @param {this.is.a.long.path.to.a.Type} a\n" 1742 " * long long long\n" 1743 " * long long\n" 1744 " */\n" 1745 " function f(a) {}\n" 1746 "}", 1747 "{\n" 1748 "/**\n" 1749 " * long long long long\n" 1750 " * @param {this.is.a.long.path.to.a.Type} a\n" 1751 " * long long long long\n" 1752 " * long\n" 1753 " */\n" 1754 " function f(a) {}\n" 1755 "}", 1756 getGoogleJSStyleWithColumns(20)); 1757 } 1758 1759 TEST_F(FormatTestJS, RequoteStringsSingle) { 1760 verifyFormat("var x = 'foo';", "var x = \"foo\";"); 1761 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";"); 1762 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";"); 1763 verifyFormat( 1764 "var x =\n" 1765 " 'foo\\'';", 1766 // Code below is 15 chars wide, doesn't fit into the line with the 1767 // \ escape added. 1768 "var x = \"foo'\";", getGoogleJSStyleWithColumns(15)); 1769 // Removes no-longer needed \ escape from ". 1770 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";"); 1771 // Code below fits into 15 chars *after* removing the \ escape. 1772 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";", 1773 getGoogleJSStyleWithColumns(15)); 1774 verifyFormat("// clang-format off\n" 1775 "let x = \"double\";\n" 1776 "// clang-format on\n" 1777 "let x = 'single';\n", 1778 "// clang-format off\n" 1779 "let x = \"double\";\n" 1780 "// clang-format on\n" 1781 "let x = \"single\";\n"); 1782 } 1783 1784 TEST_F(FormatTestJS, RequoteAndIndent) { 1785 verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n" 1786 " 'double quoted string that needs wrapping');", 1787 "let x = someVeryLongFunctionThatGoesOnAndOn(" 1788 "\"double quoted string that needs wrapping\");"); 1789 1790 verifyFormat("let x =\n" 1791 " 'foo\\'oo';\n" 1792 "let x =\n" 1793 " 'foo\\'oo';", 1794 "let x=\"foo'oo\";\n" 1795 "let x=\"foo'oo\";", 1796 getGoogleJSStyleWithColumns(15)); 1797 } 1798 1799 TEST_F(FormatTestJS, RequoteStringsDouble) { 1800 FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1801 DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double; 1802 verifyFormat("var x = \"foo\";", DoubleQuotes); 1803 verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes); 1804 verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes); 1805 } 1806 1807 TEST_F(FormatTestJS, RequoteStringsLeave) { 1808 FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1809 LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave; 1810 verifyFormat("var x = \"foo\";", LeaveQuotes); 1811 verifyFormat("var x = 'foo';", LeaveQuotes); 1812 } 1813 1814 TEST_F(FormatTestJS, SupportShebangLines) { 1815 verifyFormat("#!/usr/bin/env node\n" 1816 "var x = hello();", 1817 "#!/usr/bin/env node\n" 1818 "var x = hello();"); 1819 } 1820 1821 TEST_F(FormatTestJS, NonNullAssertionOperator) { 1822 verifyFormat("let x = foo!.bar();\n"); 1823 verifyFormat("let x = foo ? bar! : baz;\n"); 1824 verifyFormat("let x = !foo;\n"); 1825 verifyFormat("let x = foo[0]!;\n"); 1826 verifyFormat("let x = (foo)!;\n"); 1827 verifyFormat("let x = foo! - 1;\n"); 1828 verifyFormat("let x = {foo: 1}!;\n"); 1829 verifyFormat( 1830 "let x = hello.foo()!\n" 1831 " .foo()!\n" 1832 " .foo()!\n" 1833 " .foo()!;\n", 1834 getGoogleJSStyleWithColumns(20)); 1835 verifyFormat("let x = namespace!;\n"); 1836 verifyFormat("return !!x;\n"); 1837 } 1838 1839 TEST_F(FormatTestJS, Conditional) { 1840 verifyFormat("y = x ? 1 : 2;"); 1841 verifyFormat("x ? 1 : 2;"); 1842 verifyFormat("class Foo {\n" 1843 " field = true ? 1 : 2;\n" 1844 " method(a = true ? 1 : 2) {}\n" 1845 "}"); 1846 } 1847 1848 TEST_F(FormatTestJS, ImportComments) { 1849 verifyFormat("import {x} from 'x'; // from some location", 1850 getGoogleJSStyleWithColumns(25)); 1851 verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10)); 1852 verifyFormat("/// <reference path=\"some/location\" />", getGoogleJSStyleWithColumns(10)); 1853 } 1854 1855 TEST_F(FormatTestJS, Exponentiation) { 1856 verifyFormat("squared = x ** 2;"); 1857 verifyFormat("squared **= 2;"); 1858 } 1859 1860 } // end namespace tooling 1861 } // end namespace clang 1862