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