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