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