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 bool IncompleteFormat = false; 28 tooling::Replacements Replaces = 29 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat); 30 EXPECT_FALSE(IncompleteFormat); 31 std::string Result = applyAllReplacements(Code, Replaces); 32 EXPECT_NE("", 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, UnderstandsJavaScriptOperators) { 66 verifyFormat("a == = b;"); 67 verifyFormat("a != = b;"); 68 69 verifyFormat("a === b;"); 70 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10)); 71 verifyFormat("a !== b;"); 72 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10)); 73 verifyFormat("if (a + b + c +\n" 74 " d !==\n" 75 " e + f + g)\n" 76 " q();", 77 getGoogleJSStyleWithColumns(20)); 78 79 verifyFormat("a >> >= b;"); 80 81 verifyFormat("a >>> b;"); 82 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10)); 83 verifyFormat("a >>>= b;"); 84 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10)); 85 verifyFormat("if (a + b + c +\n" 86 " d >>>\n" 87 " e + f + g)\n" 88 " q();", 89 getGoogleJSStyleWithColumns(20)); 90 verifyFormat("var x = aaaaaaaaaa ?\n" 91 " bbbbbb :\n" 92 " ccc;", 93 getGoogleJSStyleWithColumns(20)); 94 95 verifyFormat("var b = a.map((x) => x + 1);"); 96 verifyFormat("return ('aaa') in bbbb;"); 97 verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n" 98 " aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 99 FormatStyle Style = getGoogleJSStyleWithColumns(80); 100 Style.AlignOperands = true; 101 verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n" 102 " aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 103 Style); 104 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 105 verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa()\n" 106 " in aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 107 Style); 108 109 // ES6 spread operator. 110 verifyFormat("someFunction(...a);"); 111 verifyFormat("var x = [1, ...a, 2];"); 112 } 113 114 TEST_F(FormatTestJS, UnderstandsAmpAmp) { 115 verifyFormat("e && e.SomeFunction();"); 116 } 117 118 TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) { 119 verifyFormat("not.and.or.not_eq = 1;"); 120 } 121 122 TEST_F(FormatTestJS, ReservedWords) { 123 // JavaScript reserved words (aka keywords) are only illegal when used as 124 // Identifiers, but are legal as IdentifierNames. 125 verifyFormat("x.class.struct = 1;"); 126 verifyFormat("x.case = 1;"); 127 verifyFormat("x.interface = 1;"); 128 verifyFormat("x.for = 1;"); 129 verifyFormat("x.of() = 1;"); 130 verifyFormat("x.in() = 1;"); 131 verifyFormat("x.let() = 1;"); 132 verifyFormat("x.var() = 1;"); 133 verifyFormat("x = {\n" 134 " a: 12,\n" 135 " interface: 1,\n" 136 " switch: 1,\n" 137 "};"); 138 verifyFormat("var struct = 2;"); 139 verifyFormat("var union = 2;"); 140 verifyFormat("var interface = 2;"); 141 verifyFormat("interface = 2;"); 142 verifyFormat("x = interface instanceof y;"); 143 } 144 145 TEST_F(FormatTestJS, CppKeywords) { 146 // Make sure we don't mess stuff up because of C++ keywords. 147 verifyFormat("return operator && (aa);"); 148 } 149 150 TEST_F(FormatTestJS, ES6DestructuringAssignment) { 151 verifyFormat("var [a, b, c] = [1, 2, 3];"); 152 verifyFormat("const [a, b, c] = [1, 2, 3];"); 153 verifyFormat("let [a, b, c] = [1, 2, 3];"); 154 verifyFormat("var {a, b} = {a: 1, b: 2};"); 155 verifyFormat("let {a, b} = {a: 1, b: 2};"); 156 } 157 158 TEST_F(FormatTestJS, ContainerLiterals) { 159 verifyFormat("var x = {y: function(a) { return a; }};"); 160 verifyFormat("return {\n" 161 " link: function() {\n" 162 " f(); //\n" 163 " }\n" 164 "};"); 165 verifyFormat("return {\n" 166 " a: a,\n" 167 " link: function() {\n" 168 " f(); //\n" 169 " }\n" 170 "};"); 171 verifyFormat("return {\n" 172 " a: a,\n" 173 " link: function() {\n" 174 " f(); //\n" 175 " },\n" 176 " link: function() {\n" 177 " f(); //\n" 178 " }\n" 179 "};"); 180 verifyFormat("var stuff = {\n" 181 " // comment for update\n" 182 " update: false,\n" 183 " // comment for modules\n" 184 " modules: false,\n" 185 " // comment for tasks\n" 186 " tasks: false\n" 187 "};"); 188 verifyFormat("return {\n" 189 " 'finish':\n" 190 " //\n" 191 " a\n" 192 "};"); 193 verifyFormat("var obj = {\n" 194 " fooooooooo: function(x) {\n" 195 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 196 " }\n" 197 "};"); 198 // Simple object literal, as opposed to enum style below. 199 verifyFormat("var obj = {a: 123};"); 200 // Enum style top level assignment. 201 verifyFormat("X = {\n a: 123\n};"); 202 verifyFormat("X.Y = {\n a: 123\n};"); 203 // But only on the top level, otherwise its a plain object literal assignment. 204 verifyFormat("function x() {\n" 205 " y = {z: 1};\n" 206 "}"); 207 verifyFormat("x = foo && {a: 123};"); 208 209 // Arrow functions in object literals. 210 verifyFormat("var x = {y: (a) => { return a; }};"); 211 verifyFormat("var x = {y: (a) => a};"); 212 213 // Computed keys. 214 verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};"); 215 verifyFormat("var x = {\n" 216 " [a]: 1,\n" 217 " b: 2,\n" 218 " [c]: 3,\n" 219 "};"); 220 221 // Object literals can leave out labels. 222 verifyFormat("f({a}, () => {\n" 223 " g(); //\n" 224 "});"); 225 226 // Keys can be quoted. 227 verifyFormat("var x = {\n" 228 " a: a,\n" 229 " b: b,\n" 230 " 'c': c,\n" 231 "};"); 232 } 233 234 TEST_F(FormatTestJS, MethodsInObjectLiterals) { 235 verifyFormat("var o = {\n" 236 " value: 'test',\n" 237 " get value() { // getter\n" 238 " return this.value;\n" 239 " }\n" 240 "};"); 241 verifyFormat("var o = {\n" 242 " value: 'test',\n" 243 " set value(val) { // setter\n" 244 " this.value = val;\n" 245 " }\n" 246 "};"); 247 verifyFormat("var o = {\n" 248 " value: 'test',\n" 249 " someMethod(val) { // method\n" 250 " doSomething(this.value + val);\n" 251 " }\n" 252 "};"); 253 verifyFormat("var o = {\n" 254 " someMethod(val) { // method\n" 255 " doSomething(this.value + val);\n" 256 " },\n" 257 " someOtherMethod(val) { // method\n" 258 " doSomething(this.value + val);\n" 259 " }\n" 260 "};"); 261 } 262 263 TEST_F(FormatTestJS, SpacesInContainerLiterals) { 264 verifyFormat("var arr = [1, 2, 3];"); 265 verifyFormat("f({a: 1, b: 2, c: 3});"); 266 267 verifyFormat("var object_literal_with_long_name = {\n" 268 " a: 'aaaaaaaaaaaaaaaaaa',\n" 269 " b: 'bbbbbbbbbbbbbbbbbb'\n" 270 "};"); 271 272 verifyFormat("f({a: 1, b: 2, c: 3});", 273 getChromiumStyle(FormatStyle::LK_JavaScript)); 274 verifyFormat("f({'a': [{}]});"); 275 } 276 277 TEST_F(FormatTestJS, SingleQuotedStrings) { 278 verifyFormat("this.function('', true);"); 279 } 280 281 TEST_F(FormatTestJS, GoogScopes) { 282 verifyFormat("goog.scope(function() {\n" 283 "var x = a.b;\n" 284 "var y = c.d;\n" 285 "}); // goog.scope"); 286 verifyFormat("goog.scope(function() {\n" 287 "// test\n" 288 "var x = 0;\n" 289 "// test\n" 290 "});"); 291 } 292 293 TEST_F(FormatTestJS, GoogModules) { 294 verifyFormat("goog.module('this.is.really.absurdly.long');", 295 getGoogleJSStyleWithColumns(40)); 296 verifyFormat("goog.require('this.is.really.absurdly.long');", 297 getGoogleJSStyleWithColumns(40)); 298 verifyFormat("goog.provide('this.is.really.absurdly.long');", 299 getGoogleJSStyleWithColumns(40)); 300 verifyFormat("var long = goog.require('this.is.really.absurdly.long');", 301 getGoogleJSStyleWithColumns(40)); 302 verifyFormat("goog.setTestOnly('this.is.really.absurdly.long');", 303 getGoogleJSStyleWithColumns(40)); 304 verifyFormat("goog.forwardDeclare('this.is.really.absurdly.long');", 305 getGoogleJSStyleWithColumns(40)); 306 307 // These should be wrapped normally. 308 verifyFormat( 309 "var MyLongClassName =\n" 310 " goog.module.get('my.long.module.name.followedBy.MyLongClassName');"); 311 } 312 313 TEST_F(FormatTestJS, FormatsFreestandingFunctions) { 314 verifyFormat("function outer1(a, b) {\n" 315 " function inner1(a, b) { return a; }\n" 316 " inner1(a, b);\n" 317 "}\n" 318 "function outer2(a, b) {\n" 319 " function inner2(a, b) { return a; }\n" 320 " inner2(a, b);\n" 321 "}"); 322 verifyFormat("function f() {}"); 323 } 324 325 TEST_F(FormatTestJS, GeneratorFunctions) { 326 verifyFormat("function* f() {\n" 327 " let x = 1;\n" 328 " yield x;\n" 329 " yield* something();\n" 330 "}"); 331 verifyFormat("function*\n" 332 " f() {\n" 333 "}", 334 getGoogleJSStyleWithColumns(8)); 335 verifyFormat("export function* f() {\n" 336 " yield 1;\n" 337 "}\n"); 338 verifyFormat("class X {\n" 339 " * generatorMethod() { yield x; }\n" 340 "}"); 341 } 342 343 TEST_F(FormatTestJS, AsyncFunctions) { 344 verifyFormat("async function f() {\n" 345 " let x = 1;\n" 346 " return fetch(x);\n" 347 "}"); 348 verifyFormat("async function* f() {\n" 349 " yield fetch(x);\n" 350 "}"); 351 verifyFormat("export async function f() {\n" 352 " return fetch(x);\n" 353 "}"); 354 verifyFormat("class X {\n" 355 " async asyncMethod() { return fetch(1); }\n" 356 "}"); 357 } 358 359 TEST_F(FormatTestJS, ArrayLiterals) { 360 verifyFormat("var aaaaa: List<SomeThing> =\n" 361 " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];"); 362 verifyFormat("return [\n" 363 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 364 " ccccccccccccccccccccccccccc\n" 365 "];"); 366 verifyFormat("return [\n" 367 " aaaa().bbbbbbbb('A'),\n" 368 " aaaa().bbbbbbbb('B'),\n" 369 " aaaa().bbbbbbbb('C'),\n" 370 "];"); 371 verifyFormat("var someVariable = SomeFunction([\n" 372 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 373 " ccccccccccccccccccccccccccc\n" 374 "]);"); 375 verifyFormat("var someVariable = SomeFunction([\n" 376 " [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n" 377 "]);", 378 getGoogleJSStyleWithColumns(51)); 379 verifyFormat("var someVariable = SomeFunction(aaaa, [\n" 380 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 381 " ccccccccccccccccccccccccccc\n" 382 "]);"); 383 verifyFormat("var someVariable = SomeFunction(\n" 384 " aaaa,\n" 385 " [\n" 386 " aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 387 " cccccccccccccccccccccccccc\n" 388 " ],\n" 389 " aaaa);"); 390 verifyFormat("var aaaa = aaaaa || // wrap\n" 391 " [];"); 392 393 verifyFormat("someFunction([], {a: a});"); 394 } 395 396 TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) { 397 verifyFormat("var array = [\n" 398 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 399 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 400 "];"); 401 verifyFormat("var array = someFunction([\n" 402 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 403 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 404 "]);"); 405 } 406 407 TEST_F(FormatTestJS, FunctionLiterals) { 408 verifyFormat("doFoo(function() {});"); 409 verifyFormat("doFoo(function() { return 1; });"); 410 verifyFormat("var func = function() {\n" 411 " return 1;\n" 412 "};"); 413 verifyFormat("var func = //\n" 414 " function() {\n" 415 " return 1;\n" 416 "};"); 417 verifyFormat("return {\n" 418 " body: {\n" 419 " setAttribute: function(key, val) { this[key] = val; },\n" 420 " getAttribute: function(key) { return this[key]; },\n" 421 " style: {direction: ''}\n" 422 " }\n" 423 "};"); 424 verifyFormat("abc = xyz ? function() {\n" 425 " return 1;\n" 426 "} : function() {\n" 427 " return -1;\n" 428 "};"); 429 430 verifyFormat("var closure = goog.bind(\n" 431 " function() { // comment\n" 432 " foo();\n" 433 " bar();\n" 434 " },\n" 435 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n" 436 " arg3IsReallyLongAndNeeedsLineBreaks);"); 437 verifyFormat("var closure = goog.bind(function() { // comment\n" 438 " foo();\n" 439 " bar();\n" 440 "}, this);"); 441 verifyFormat("return {\n" 442 " a: 'E',\n" 443 " b: function() {\n" 444 " return function() {\n" 445 " f(); //\n" 446 " };\n" 447 " }\n" 448 "};"); 449 verifyFormat("{\n" 450 " var someVariable = function(x) {\n" 451 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 452 " };\n" 453 "}"); 454 verifyFormat("someLooooooooongFunction(\n" 455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 456 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 457 " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 458 " // code\n" 459 " });"); 460 461 verifyFormat("f({a: function() { return 1; }});", 462 getGoogleJSStyleWithColumns(33)); 463 verifyFormat("f({\n" 464 " a: function() { return 1; }\n" 465 "});", 466 getGoogleJSStyleWithColumns(32)); 467 468 verifyFormat("return {\n" 469 " a: function SomeFunction() {\n" 470 " // ...\n" 471 " return 1;\n" 472 " }\n" 473 "};"); 474 verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 475 " .then(goog.bind(function(aaaaaaaaaaa) {\n" 476 " someFunction();\n" 477 " someFunction();\n" 478 " }, this), aaaaaaaaaaaaaaaaa);"); 479 480 verifyFormat("someFunction(goog.bind(function() {\n" 481 " doSomething();\n" 482 " doSomething();\n" 483 "}, this), goog.bind(function() {\n" 484 " doSomething();\n" 485 " doSomething();\n" 486 "}, this));"); 487 488 // FIXME: This is bad, we should be wrapping before "function() {". 489 verifyFormat("someFunction(function() {\n" 490 " doSomething(); // break\n" 491 "})\n" 492 " .doSomethingElse(\n" 493 " // break\n" 494 " );"); 495 } 496 497 TEST_F(FormatTestJS, InliningFunctionLiterals) { 498 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 499 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 500 verifyFormat("var func = function() {\n" 501 " return 1;\n" 502 "};", 503 Style); 504 verifyFormat("var func = doSomething(function() { return 1; });", Style); 505 verifyFormat("var outer = function() {\n" 506 " var inner = function() { return 1; }\n" 507 "};", 508 Style); 509 verifyFormat("function outer1(a, b) {\n" 510 " function inner1(a, b) { return a; }\n" 511 "}", 512 Style); 513 514 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 515 verifyFormat("var func = function() { return 1; };", Style); 516 verifyFormat("var func = doSomething(function() { return 1; });", Style); 517 verifyFormat( 518 "var outer = function() { var inner = function() { return 1; } };", 519 Style); 520 verifyFormat("function outer1(a, b) {\n" 521 " function inner1(a, b) { return a; }\n" 522 "}", 523 Style); 524 525 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 526 verifyFormat("var func = function() {\n" 527 " return 1;\n" 528 "};", 529 Style); 530 verifyFormat("var func = doSomething(function() {\n" 531 " return 1;\n" 532 "});", 533 Style); 534 verifyFormat("var outer = function() {\n" 535 " var inner = function() {\n" 536 " return 1;\n" 537 " }\n" 538 "};", 539 Style); 540 verifyFormat("function outer1(a, b) {\n" 541 " function inner1(a, b) {\n" 542 " return a;\n" 543 " }\n" 544 "}", 545 Style); 546 547 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 548 verifyFormat("var func = function() {\n" 549 " return 1;\n" 550 "};", 551 Style); 552 } 553 554 TEST_F(FormatTestJS, MultipleFunctionLiterals) { 555 verifyFormat("promise.then(\n" 556 " function success() {\n" 557 " doFoo();\n" 558 " doBar();\n" 559 " },\n" 560 " function error() {\n" 561 " doFoo();\n" 562 " doBaz();\n" 563 " },\n" 564 " []);\n"); 565 verifyFormat("promise.then(\n" 566 " function success() {\n" 567 " doFoo();\n" 568 " doBar();\n" 569 " },\n" 570 " [],\n" 571 " function error() {\n" 572 " doFoo();\n" 573 " doBaz();\n" 574 " });\n"); 575 verifyFormat("promise.then(\n" 576 " [],\n" 577 " function success() {\n" 578 " doFoo();\n" 579 " doBar();\n" 580 " },\n" 581 " function error() {\n" 582 " doFoo();\n" 583 " doBaz();\n" 584 " });\n"); 585 586 verifyFormat("getSomeLongPromise()\n" 587 " .then(function(value) { body(); })\n" 588 " .thenCatch(function(error) {\n" 589 " body();\n" 590 " body();\n" 591 " });"); 592 verifyFormat("getSomeLongPromise()\n" 593 " .then(function(value) {\n" 594 " body();\n" 595 " body();\n" 596 " })\n" 597 " .thenCatch(function(error) {\n" 598 " body();\n" 599 " body();\n" 600 " });"); 601 602 verifyFormat("getSomeLongPromise()\n" 603 " .then(function(value) { body(); })\n" 604 " .thenCatch(function(error) { body(); });"); 605 606 verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n" 607 " .aaaaaaa(function() {\n" 608 " //\n" 609 " })\n" 610 " .bbbbbb();"); 611 } 612 613 TEST_F(FormatTestJS, ArrowFunctions) { 614 verifyFormat("var x = (a) => {\n" 615 " return a;\n" 616 "};"); 617 verifyFormat("var x = (a) => {\n" 618 " function y() { return 42; }\n" 619 " return a;\n" 620 "};"); 621 verifyFormat("var x = (a: type): {some: type} => {\n" 622 " return a;\n" 623 "};"); 624 verifyFormat("var x = (a) => a;"); 625 verifyFormat("return () => [];"); 626 verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n" 627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 628 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n" 630 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 631 "};"); 632 verifyFormat("var a = a.aaaaaaa(\n" 633 " (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n" 634 " aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 635 verifyFormat("var a = a.aaaaaaa(\n" 636 " (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n" 637 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n" 638 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 639 640 // FIXME: This is bad, we should be wrapping before "() => {". 641 verifyFormat("someFunction(() => {\n" 642 " doSomething(); // break\n" 643 "})\n" 644 " .doSomethingElse(\n" 645 " // break\n" 646 " );"); 647 } 648 649 TEST_F(FormatTestJS, ReturnStatements) { 650 verifyFormat("function() {\n" 651 " return [hello, world];\n" 652 "}"); 653 } 654 655 TEST_F(FormatTestJS, ForLoops) { 656 verifyFormat("for (var i in [2, 3]) {\n" 657 "}"); 658 verifyFormat("for (var i of [2, 3]) {\n" 659 "}"); 660 verifyFormat("for (let {a, b} of x) {\n" 661 "}"); 662 verifyFormat("for (let {a, b} in x) {\n" 663 "}"); 664 } 665 666 TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) { 667 // The following statements must not wrap, as otherwise the program meaning 668 // would change due to automatic semicolon insertion. 669 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1. 670 verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10)); 671 verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10)); 672 verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10)); 673 verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10)); 674 verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10)); 675 verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10)); 676 verifyFormat("return [\n" 677 " aaa\n" 678 "];", 679 getGoogleJSStyleWithColumns(12)); 680 } 681 682 TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) { 683 verifyFormat("a\n" 684 "b;", 685 " a \n" 686 " b ;"); 687 verifyFormat("a()\n" 688 "b;", 689 " a ()\n" 690 " b ;"); 691 verifyFormat("a[b]\n" 692 "c;", 693 "a [b]\n" 694 "c ;"); 695 verifyFormat("1\n" 696 "a;", 697 "1 \n" 698 "a ;"); 699 verifyFormat("a\n" 700 "1;", 701 "a \n" 702 "1 ;"); 703 verifyFormat("a\n" 704 "'x';", 705 "a \n" 706 " 'x';"); 707 verifyFormat("a++\n" 708 "b;", 709 "a ++\n" 710 "b ;"); 711 verifyFormat("a\n" 712 "!b && c;", 713 "a \n" 714 " ! b && c;"); 715 verifyFormat("a\n" 716 "if (1) f();", 717 " a\n" 718 " if (1) f();"); 719 verifyFormat("a\n" 720 "class X {}", 721 " a\n" 722 " class X {}"); 723 verifyFormat("var a", "var\n" 724 "a"); 725 verifyFormat("x instanceof String", "x\n" 726 "instanceof\n" 727 "String"); 728 verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n" 729 " bar) {}"); 730 } 731 732 TEST_F(FormatTestJS, ClosureStyleCasts) { 733 verifyFormat("var x = /** @type {foo} */ (bar);"); 734 } 735 736 TEST_F(FormatTestJS, TryCatch) { 737 verifyFormat("try {\n" 738 " f();\n" 739 "} catch (e) {\n" 740 " g();\n" 741 "} finally {\n" 742 " h();\n" 743 "}"); 744 745 // But, of course, "catch" is a perfectly fine function name in JavaScript. 746 verifyFormat("someObject.catch();"); 747 verifyFormat("someObject.new();"); 748 verifyFormat("someObject.delete();"); 749 } 750 751 TEST_F(FormatTestJS, StringLiteralConcatenation) { 752 verifyFormat("var literal = 'hello ' +\n" 753 " 'world';"); 754 } 755 756 TEST_F(FormatTestJS, RegexLiteralClassification) { 757 // Regex literals. 758 verifyFormat("var regex = /abc/;"); 759 verifyFormat("f(/abc/);"); 760 verifyFormat("f(abc, /abc/);"); 761 verifyFormat("some_map[/abc/];"); 762 verifyFormat("var x = a ? /abc/ : /abc/;"); 763 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 764 verifyFormat("var x = !/abc/.test(y);"); 765 verifyFormat("var x = a && /abc/.test(y);"); 766 verifyFormat("var x = a || /abc/.test(y);"); 767 verifyFormat("var x = a + /abc/.search(y);"); 768 verifyFormat("/abc/.search(y);"); 769 verifyFormat("var regexs = {/abc/, /abc/};"); 770 verifyFormat("return /abc/;"); 771 772 // Not regex literals. 773 verifyFormat("var a = a / 2 + b / 3;"); 774 verifyFormat("var a = a++ / 2;"); 775 // Prefix unary can operate on regex literals, not that it makes sense. 776 verifyFormat("var a = ++/a/;"); 777 778 // This is a known issue, regular expressions are incorrectly detected if 779 // directly following a closing parenthesis. 780 verifyFormat("if (foo) / bar /.exec(baz);"); 781 } 782 783 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 784 verifyFormat("var regex = /=/;"); 785 verifyFormat("var regex = /a*/;"); 786 verifyFormat("var regex = /a+/;"); 787 verifyFormat("var regex = /a?/;"); 788 verifyFormat("var regex = /.a./;"); 789 verifyFormat("var regex = /a\\*/;"); 790 verifyFormat("var regex = /^a$/;"); 791 verifyFormat("var regex = /\\/a/;"); 792 verifyFormat("var regex = /(?:x)/;"); 793 verifyFormat("var regex = /x(?=y)/;"); 794 verifyFormat("var regex = /x(?!y)/;"); 795 verifyFormat("var regex = /x|y/;"); 796 verifyFormat("var regex = /a{2}/;"); 797 verifyFormat("var regex = /a{1,3}/;"); 798 799 verifyFormat("var regex = /[abc]/;"); 800 verifyFormat("var regex = /[^abc]/;"); 801 verifyFormat("var regex = /[\\b]/;"); 802 verifyFormat("var regex = /[/]/;"); 803 verifyFormat("var regex = /[\\/]/;"); 804 verifyFormat("var regex = /\\[/;"); 805 verifyFormat("var regex = /\\\\[/]/;"); 806 verifyFormat("var regex = /}[\"]/;"); 807 verifyFormat("var regex = /}[/\"]/;"); 808 verifyFormat("var regex = /}[\"/]/;"); 809 810 verifyFormat("var regex = /\\b/;"); 811 verifyFormat("var regex = /\\B/;"); 812 verifyFormat("var regex = /\\d/;"); 813 verifyFormat("var regex = /\\D/;"); 814 verifyFormat("var regex = /\\f/;"); 815 verifyFormat("var regex = /\\n/;"); 816 verifyFormat("var regex = /\\r/;"); 817 verifyFormat("var regex = /\\s/;"); 818 verifyFormat("var regex = /\\S/;"); 819 verifyFormat("var regex = /\\t/;"); 820 verifyFormat("var regex = /\\v/;"); 821 verifyFormat("var regex = /\\w/;"); 822 verifyFormat("var regex = /\\W/;"); 823 verifyFormat("var regex = /a(a)\\1/;"); 824 verifyFormat("var regex = /\\0/;"); 825 verifyFormat("var regex = /\\\\/g;"); 826 verifyFormat("var regex = /\\a\\\\/g;"); 827 verifyFormat("var regex = /\a\\//g;"); 828 verifyFormat("var regex = /a\\//;\n" 829 "var x = 0;"); 830 verifyFormat("var regex = /'/g;", "var regex = /'/g ;"); 831 verifyFormat("var regex = /'/g; //'", "var regex = /'/g ; //'"); 832 verifyFormat("var regex = /\\/*/;\n" 833 "var x = 0;", 834 "var regex = /\\/*/;\n" 835 "var x=0;"); 836 verifyFormat("var x = /a\\//;", "var x = /a\\// \n;"); 837 verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16)); 838 verifyFormat("var regex =\n" 839 " /\"/;", 840 getGoogleJSStyleWithColumns(15)); 841 verifyFormat("var regex = //\n" 842 " /a/;"); 843 verifyFormat("var regexs = [\n" 844 " /d/, //\n" 845 " /aa/, //\n" 846 "];"); 847 } 848 849 TEST_F(FormatTestJS, RegexLiteralModifiers) { 850 verifyFormat("var regex = /abc/g;"); 851 verifyFormat("var regex = /abc/i;"); 852 verifyFormat("var regex = /abc/m;"); 853 verifyFormat("var regex = /abc/y;"); 854 } 855 856 TEST_F(FormatTestJS, RegexLiteralLength) { 857 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 858 getGoogleJSStyleWithColumns(60)); 859 verifyFormat("var regex =\n" 860 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 861 getGoogleJSStyleWithColumns(60)); 862 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 863 getGoogleJSStyleWithColumns(50)); 864 } 865 866 TEST_F(FormatTestJS, RegexLiteralExamples) { 867 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 868 } 869 870 TEST_F(FormatTestJS, TypeAnnotations) { 871 verifyFormat("var x: string;"); 872 verifyFormat("var x: {a: string; b: number;} = {};"); 873 verifyFormat("function x(): string {\n return 'x';\n}"); 874 verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}"); 875 verifyFormat("function x(y: string): string {\n return 'x';\n}"); 876 verifyFormat("for (var y: string in x) {\n x();\n}"); 877 verifyFormat("for (var y: string of x) {\n x();\n}"); 878 verifyFormat("function x(y: {a?: number;} = {}): number {\n" 879 " return 12;\n" 880 "}"); 881 verifyFormat("((a: string, b: number): string => a + b);"); 882 verifyFormat("var x: (y: number) => string;"); 883 verifyFormat("var x: P<string, (a: number) => string>;"); 884 verifyFormat("var x = {y: function(): z { return 1; }};"); 885 verifyFormat("var x = {y: function(): {a: number} { return 1; }};"); 886 verifyFormat("function someFunc(args: string[]):\n" 887 " {longReturnValue: string[]} {}", 888 getGoogleJSStyleWithColumns(60)); 889 } 890 891 TEST_F(FormatTestJS, UnionIntersectionTypes) { 892 verifyFormat("let x: A|B = A | B;"); 893 verifyFormat("let x: A&B|C = A & B;"); 894 verifyFormat("let x: Foo<A|B> = new Foo<A|B>();"); 895 verifyFormat("function(x: A|B): C&D {}"); 896 verifyFormat("function(x: A|B = A | B): C&D {}"); 897 } 898 899 TEST_F(FormatTestJS, ClassDeclarations) { 900 verifyFormat("class C {\n x: string = 12;\n}"); 901 verifyFormat("class C {\n x(): string => 12;\n}"); 902 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); 903 verifyFormat("class C {\n private x: string = 12;\n}"); 904 verifyFormat("class C {\n private static x: string = 12;\n}"); 905 verifyFormat("class C {\n static x(): string { return 'asd'; }\n}"); 906 verifyFormat("class C extends P implements I {}"); 907 verifyFormat("class C extends p.P implements i.I {}"); 908 verifyFormat("class Test {\n" 909 " aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n" 910 " aaaaaaaaaaaaaaaaaaaaaa {}\n" 911 "}"); 912 verifyFormat("foo = class Name {\n" 913 " constructor() {}\n" 914 "};"); 915 verifyFormat("foo = class {\n" 916 " constructor() {}\n" 917 "};"); 918 verifyFormat("class C {\n" 919 " x: {y: Z;} = {};\n" 920 " private y: {y: Z;} = {};\n" 921 "}"); 922 923 // ':' is not a type declaration here. 924 verifyFormat("class X {\n" 925 " subs = {\n" 926 " 'b': {\n" 927 " 'c': 1,\n" 928 " },\n" 929 " };\n" 930 "}"); 931 } 932 933 TEST_F(FormatTestJS, InterfaceDeclarations) { 934 verifyFormat("interface I {\n" 935 " x: string;\n" 936 " enum: string[];\n" 937 " enum?: string[];\n" 938 "}\n" 939 "var y;"); 940 // Ensure that state is reset after parsing the interface. 941 verifyFormat("interface a {}\n" 942 "export function b() {}\n" 943 "var x;"); 944 945 // Arrays of object type literals. 946 verifyFormat("interface I {\n" 947 " o: {}[];\n" 948 "}"); 949 } 950 951 TEST_F(FormatTestJS, EnumDeclarations) { 952 verifyFormat("enum Foo {\n" 953 " A = 1,\n" 954 " B\n" 955 "}"); 956 verifyFormat("export /* somecomment*/ enum Foo {\n" 957 " A = 1,\n" 958 " B\n" 959 "}"); 960 verifyFormat("enum Foo {\n" 961 " A = 1, // comment\n" 962 " B\n" 963 "}\n" 964 "var x = 1;"); 965 } 966 967 TEST_F(FormatTestJS, MetadataAnnotations) { 968 verifyFormat("@A\nclass C {\n}"); 969 verifyFormat("@A({arg: 'value'})\nclass C {\n}"); 970 verifyFormat("@A\n@B\nclass C {\n}"); 971 verifyFormat("class C {\n @A x: string;\n}"); 972 verifyFormat("class C {\n" 973 " @A\n" 974 " private x(): string {\n" 975 " return 'y';\n" 976 " }\n" 977 "}"); 978 verifyFormat("class C {\n" 979 " private x(@A x: string) {}\n" 980 "}"); 981 verifyFormat("class X {}\n" 982 "class Y {}"); 983 } 984 985 TEST_F(FormatTestJS, TypeAliases) { 986 verifyFormat("type X = number;\n" 987 "class C {}"); 988 verifyFormat("type X<Y> = Z<Y>;"); 989 verifyFormat("type X = {\n" 990 " y: number\n" 991 "};\n" 992 "class C {}"); 993 } 994 995 TEST_F(FormatTestJS, Modules) { 996 verifyFormat("import SomeThing from 'some/module.js';"); 997 verifyFormat("import {X, Y} from 'some/module.js';"); 998 verifyFormat("import a, {X, Y} from 'some/module.js';"); 999 verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying," 1000 " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying" 1001 "} from 'some/module.js';"); 1002 verifyFormat("import {X, Y,} from 'some/module.js';"); 1003 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); 1004 verifyFormat("import * as lib from 'some/module.js';"); 1005 verifyFormat("var x = {import: 1};\nx.import = 2;"); 1006 1007 verifyFormat("export function fn() {\n" 1008 " return 'fn';\n" 1009 "}"); 1010 verifyFormat("export function A() {}\n" 1011 "export default function B() {}\n" 1012 "export function C() {}"); 1013 verifyFormat("export default () => {\n" 1014 " let x = 1;\n" 1015 " return x;\n" 1016 "}"); 1017 verifyFormat("export const x = 12;"); 1018 verifyFormat("export default class X {}"); 1019 verifyFormat("export {X, Y} from 'some/module.js';"); 1020 verifyFormat("export {X, Y,} from 'some/module.js';"); 1021 verifyFormat("export {SomeVeryLongExport as X, " 1022 "SomeOtherVeryLongExport as Y} from 'some/module.js';"); 1023 // export without 'from' is wrapped. 1024 verifyFormat("export let someRatherLongVariableName =\n" 1025 " someSurprisinglyLongVariable + someOtherRatherLongVar;"); 1026 // ... but not if from is just an identifier. 1027 verifyFormat("export {\n" 1028 " from as from,\n" 1029 " someSurprisinglyLongVariable\n" 1030 " as from\n" 1031 "};", 1032 getGoogleJSStyleWithColumns(20)); 1033 verifyFormat("export class C {\n" 1034 " x: number;\n" 1035 " y: string;\n" 1036 "}"); 1037 verifyFormat("export class X { y: number; }"); 1038 verifyFormat("export abstract class X { y: number; }"); 1039 verifyFormat("export default class X { y: number }"); 1040 verifyFormat("export default function() {\n return 1;\n}"); 1041 verifyFormat("export var x = 12;"); 1042 verifyFormat("class C {}\n" 1043 "export function f() {}\n" 1044 "var v;"); 1045 verifyFormat("export var x: number = 12;"); 1046 verifyFormat("export const y = {\n" 1047 " a: 1,\n" 1048 " b: 2\n" 1049 "};"); 1050 verifyFormat("export enum Foo {\n" 1051 " BAR,\n" 1052 " // adsdasd\n" 1053 " BAZ\n" 1054 "}"); 1055 verifyFormat("export default [\n" 1056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1057 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 1058 "];"); 1059 verifyFormat("export default [];"); 1060 verifyFormat("export default () => {};"); 1061 verifyFormat("export interface Foo { foo: number; }\n" 1062 "export class Bar {\n" 1063 " blah(): string { return this.blah; };\n" 1064 "}"); 1065 } 1066 1067 TEST_F(FormatTestJS, TemplateStrings) { 1068 // Keeps any whitespace/indentation within the template string. 1069 verifyFormat("var x = `hello\n" 1070 " ${ name }\n" 1071 " !`;", 1072 "var x = `hello\n" 1073 " ${ name }\n" 1074 " !`;"); 1075 1076 verifyFormat("var x =\n" 1077 " `hello ${world}` >= some();", 1078 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. 1079 verifyFormat("var x = `hello ${world}` >= some();", 1080 getGoogleJSStyleWithColumns(35)); // Barely fits. 1081 verifyFormat("var x = `hellö ${wörld}` >= söme();", 1082 getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8. 1083 verifyFormat("var x = `hello\n" 1084 " ${world}` >=\n" 1085 " some();", 1086 "var x =\n" 1087 " `hello\n" 1088 " ${world}` >= some();", 1089 getGoogleJSStyleWithColumns(21)); // Barely doesn't fit. 1090 verifyFormat("var x = `hello\n" 1091 " ${world}` >= some();", 1092 "var x =\n" 1093 " `hello\n" 1094 " ${world}` >= some();", 1095 getGoogleJSStyleWithColumns(22)); // Barely fits. 1096 1097 verifyFormat("var x =\n" 1098 " `h`;", 1099 getGoogleJSStyleWithColumns(11)); 1100 verifyFormat("var x =\n `multi\n line`;", "var x = `multi\n line`;", 1101 getGoogleJSStyleWithColumns(13)); 1102 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1103 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);"); 1104 // Repro for an obscure width-miscounting issue with template strings. 1105 verifyFormat( 1106 "someLongVariable =\n" 1107 " " 1108 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;", 1109 "someLongVariable = " 1110 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;"); 1111 1112 // Make sure template strings get a proper ColumnWidth assigned, even if they 1113 // are first token in line. 1114 verifyFormat( 1115 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1116 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); 1117 1118 // Two template strings. 1119 verifyFormat("var x = `hello` == `hello`;"); 1120 1121 // Comments in template strings. 1122 verifyFormat("var x = `//a`;\n" 1123 "var y;", 1124 "var x =\n `//a`;\n" 1125 "var y ;"); 1126 verifyFormat("var x = `/*a`;\n" 1127 "var y;", 1128 "var x =\n `/*a`;\n" 1129 "var y;"); 1130 // Unterminated string literals in a template string. 1131 verifyFormat("var x = `'`; // comment with matching quote '\n" 1132 "var y;"); 1133 verifyFormat("var x = `\"`; // comment with matching quote \"\n" 1134 "var y;"); 1135 verifyFormat("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa);", 1136 "it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa) ;", 1137 getGoogleJSStyleWithColumns(40)); 1138 // Backticks in a comment - not a template string. 1139 verifyFormat("var x = 1 // `/*a`;\n" 1140 " ;", 1141 "var x =\n 1 // `/*a`;\n" 1142 " ;"); 1143 verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */"); 1144 // Comment spans multiple template strings. 1145 verifyFormat("var x = `/*a`;\n" 1146 "var y = ` */ `;", 1147 "var x =\n `/*a`;\n" 1148 "var y =\n ` */ `;"); 1149 // Escaped backtick. 1150 verifyFormat("var x = ` \\` a`;\n" 1151 "var y;", 1152 "var x = ` \\` a`;\n" 1153 "var y;"); 1154 } 1155 1156 TEST_F(FormatTestJS, CastSyntax) { 1157 verifyFormat("var x = <type>foo;"); 1158 verifyFormat("var x = foo as type;"); 1159 } 1160 1161 TEST_F(FormatTestJS, TypeArguments) { 1162 verifyFormat("class X<Y> {}"); 1163 verifyFormat("new X<Y>();"); 1164 verifyFormat("foo<Y>(a);"); 1165 verifyFormat("var x: X<Y>[];"); 1166 verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); 1167 verifyFormat("function f(a: List<any> = null) {}"); 1168 verifyFormat("function f(): List<any> {}"); 1169 verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n" 1170 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}"); 1171 verifyFormat("function aaaaaaaaaa(\n" 1172 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n" 1173 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n" 1174 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}"); 1175 } 1176 1177 TEST_F(FormatTestJS, UserDefinedTypeGuards) { 1178 verifyFormat( 1179 "function foo(check: Object):\n" 1180 " check is {foo: string, bar: string, baz: string, foobar: string} {\n" 1181 " return 'bar' in check;\n" 1182 "}\n"); 1183 } 1184 1185 TEST_F(FormatTestJS, OptionalTypes) { 1186 verifyFormat("function x(a?: b, c?, d?) {}"); 1187 verifyFormat("class X {\n" 1188 " y?: z;\n" 1189 " z?;\n" 1190 "}"); 1191 verifyFormat("interface X {\n" 1192 " y?(): z;\n" 1193 "}"); 1194 verifyFormat("x ? 1 : 2;"); 1195 verifyFormat("constructor({aa}: {\n" 1196 " aa?: string,\n" 1197 " aaaaaaaa?: string,\n" 1198 " aaaaaaaaaaaaaaa?: boolean,\n" 1199 " aaaaaa?: List<string>\n" 1200 "}) {}"); 1201 } 1202 1203 TEST_F(FormatTestJS, IndexSignature) { 1204 verifyFormat("var x: {[k: string]: v};"); 1205 } 1206 1207 TEST_F(FormatTestJS, WrapAfterParen) { 1208 verifyFormat("xxxxxxxxxxx(\n" 1209 " aaa, aaa);", 1210 getGoogleJSStyleWithColumns(20)); 1211 verifyFormat("xxxxxxxxxxx(\n" 1212 " aaa, aaa, aaa,\n" 1213 " aaa, aaa, aaa);", 1214 getGoogleJSStyleWithColumns(20)); 1215 verifyFormat("xxxxxxxxxxx(\n" 1216 " aaaaaaaaaaaaaaaaaaaaaaaa,\n" 1217 " function(x) {\n" 1218 " y(); //\n" 1219 " });", 1220 getGoogleJSStyleWithColumns(40)); 1221 verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 1222 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 1223 } 1224 1225 TEST_F(FormatTestJS, JSDocAnnotations) { 1226 verifyFormat("/**\n" 1227 " * @export {this.is.a.long.path.to.a.Type}\n" 1228 " */", 1229 "/**\n" 1230 " * @export {this.is.a.long.path.to.a.Type}\n" 1231 " */", 1232 getGoogleJSStyleWithColumns(20)); 1233 } 1234 1235 TEST_F(FormatTestJS, RequoteStringsSingle) { 1236 verifyFormat("var x = 'foo';", "var x = \"foo\";"); 1237 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";"); 1238 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";"); 1239 verifyFormat( 1240 "var x =\n" 1241 " 'foo\\'';", 1242 // Code below is 15 chars wide, doesn't fit into the line with the 1243 // \ escape added. 1244 "var x = \"foo'\";", getGoogleJSStyleWithColumns(15)); 1245 // Removes no-longer needed \ escape from ". 1246 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";"); 1247 // Code below fits into 15 chars *after* removing the \ escape. 1248 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";", 1249 getGoogleJSStyleWithColumns(15)); 1250 verifyFormat("// clang-format off\n" 1251 "let x = \"double\";\n" 1252 "// clang-format on\n" 1253 "let x = 'single';\n", 1254 "// clang-format off\n" 1255 "let x = \"double\";\n" 1256 "// clang-format on\n" 1257 "let x = \"single\";\n"); 1258 } 1259 1260 TEST_F(FormatTestJS, RequoteStringsDouble) { 1261 FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1262 DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double; 1263 verifyFormat("var x = \"foo\";", DoubleQuotes); 1264 verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes); 1265 verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes); 1266 } 1267 1268 TEST_F(FormatTestJS, RequoteStringsLeave) { 1269 FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1270 LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave; 1271 verifyFormat("var x = \"foo\";", LeaveQuotes); 1272 verifyFormat("var x = 'foo';", LeaveQuotes); 1273 } 1274 1275 } // end namespace tooling 1276 } // end namespace clang 1277