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 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); 53 } 54 }; 55 56 TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) { 57 verifyFormat("a == = b;"); 58 verifyFormat("a != = b;"); 59 60 verifyFormat("a === b;"); 61 verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10)); 62 verifyFormat("a !== b;"); 63 verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10)); 64 verifyFormat("if (a + b + c +\n" 65 " d !==\n" 66 " e + f + g)\n" 67 " q();", 68 getGoogleJSStyleWithColumns(20)); 69 70 verifyFormat("a >> >= b;"); 71 72 verifyFormat("a >>> b;"); 73 verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10)); 74 verifyFormat("a >>>= b;"); 75 verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10)); 76 verifyFormat("if (a + b + c +\n" 77 " d >>>\n" 78 " e + f + g)\n" 79 " q();", 80 getGoogleJSStyleWithColumns(20)); 81 verifyFormat("var x = aaaaaaaaaa ?\n" 82 " bbbbbb :\n" 83 " ccc;", 84 getGoogleJSStyleWithColumns(20)); 85 86 verifyFormat("var b = a.map((x) => x + 1);"); 87 verifyFormat("return ('aaa') in bbbb;"); 88 89 // ES6 spread operator. 90 verifyFormat("someFunction(...a);"); 91 verifyFormat("var x = [1, ...a, 2];"); 92 } 93 94 TEST_F(FormatTestJS, UnderstandsAmpAmp) { 95 verifyFormat("e && e.SomeFunction();"); 96 } 97 98 TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) { 99 verifyFormat("not.and.or.not_eq = 1;"); 100 } 101 102 TEST_F(FormatTestJS, ES6DestructuringAssignment) { 103 verifyFormat("var [a, b, c] = [1, 2, 3];"); 104 verifyFormat("var {a, b} = {a: 1, b: 2};"); 105 } 106 107 TEST_F(FormatTestJS, ContainerLiterals) { 108 verifyFormat("var x = {y: function(a) { return a; }};"); 109 verifyFormat("return {\n" 110 " link: function() {\n" 111 " f(); //\n" 112 " }\n" 113 "};"); 114 verifyFormat("return {\n" 115 " a: a,\n" 116 " link: function() {\n" 117 " f(); //\n" 118 " }\n" 119 "};"); 120 verifyFormat("return {\n" 121 " a: a,\n" 122 " link: function() {\n" 123 " f(); //\n" 124 " },\n" 125 " link: function() {\n" 126 " f(); //\n" 127 " }\n" 128 "};"); 129 verifyFormat("var stuff = {\n" 130 " // comment for update\n" 131 " update: false,\n" 132 " // comment for modules\n" 133 " modules: false,\n" 134 " // comment for tasks\n" 135 " tasks: false\n" 136 "};"); 137 verifyFormat("return {\n" 138 " 'finish':\n" 139 " //\n" 140 " a\n" 141 "};"); 142 verifyFormat("var obj = {\n" 143 " fooooooooo: function(x) {\n" 144 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 145 " }\n" 146 "};"); 147 // Simple object literal, as opposed to enum style below. 148 verifyFormat("var obj = {a: 123};"); 149 // Enum style top level assignment. 150 verifyFormat("X = {\n a: 123\n};"); 151 verifyFormat("X.Y = {\n a: 123\n};"); 152 // But only on the top level, otherwise its a plain object literal assignment. 153 verifyFormat("function x() {\n" 154 " y = {z: 1};\n" 155 "}"); 156 verifyFormat("x = foo && {a: 123};"); 157 158 // Arrow functions in object literals. 159 verifyFormat("var x = {y: (a) => { return a; }};"); 160 verifyFormat("var x = {y: (a) => a};"); 161 162 // Computed keys. 163 verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};"); 164 verifyFormat("var x = {\n" 165 " [a]: 1,\n" 166 " b: 2,\n" 167 " [c]: 3,\n" 168 "};"); 169 } 170 171 TEST_F(FormatTestJS, MethodsInObjectLiterals) { 172 verifyFormat("var o = {\n" 173 " value: 'test',\n" 174 " get value() { // getter\n" 175 " return this.value;\n" 176 " }\n" 177 "};"); 178 verifyFormat("var o = {\n" 179 " value: 'test',\n" 180 " set value(val) { // setter\n" 181 " this.value = val;\n" 182 " }\n" 183 "};"); 184 verifyFormat("var o = {\n" 185 " value: 'test',\n" 186 " someMethod(val) { // method\n" 187 " doSomething(this.value + val);\n" 188 " }\n" 189 "};"); 190 verifyFormat("var o = {\n" 191 " someMethod(val) { // method\n" 192 " doSomething(this.value + val);\n" 193 " },\n" 194 " someOtherMethod(val) { // method\n" 195 " doSomething(this.value + val);\n" 196 " }\n" 197 "};"); 198 } 199 200 TEST_F(FormatTestJS, SpacesInContainerLiterals) { 201 verifyFormat("var arr = [1, 2, 3];"); 202 verifyFormat("f({a: 1, b: 2, c: 3});"); 203 204 verifyFormat("var object_literal_with_long_name = {\n" 205 " a: 'aaaaaaaaaaaaaaaaaa',\n" 206 " b: 'bbbbbbbbbbbbbbbbbb'\n" 207 "};"); 208 209 verifyFormat("f({a: 1, b: 2, c: 3});", 210 getChromiumStyle(FormatStyle::LK_JavaScript)); 211 verifyFormat("f({'a': [{}]});"); 212 } 213 214 TEST_F(FormatTestJS, SingleQuoteStrings) { 215 verifyFormat("this.function('', true);"); 216 } 217 218 TEST_F(FormatTestJS, GoogScopes) { 219 verifyFormat("goog.scope(function() {\n" 220 "var x = a.b;\n" 221 "var y = c.d;\n" 222 "}); // goog.scope"); 223 verifyFormat("goog.scope(function() {\n" 224 "// test\n" 225 "var x = 0;\n" 226 "// test\n" 227 "});"); 228 } 229 230 TEST_F(FormatTestJS, GoogModules) { 231 verifyFormat("goog.module('this.is.really.absurdly.long');", 232 getGoogleJSStyleWithColumns(40)); 233 verifyFormat("goog.require('this.is.really.absurdly.long');", 234 getGoogleJSStyleWithColumns(40)); 235 verifyFormat("goog.provide('this.is.really.absurdly.long');", 236 getGoogleJSStyleWithColumns(40)); 237 verifyFormat("var long = goog.require('this.is.really.absurdly.long');", 238 getGoogleJSStyleWithColumns(40)); 239 240 // These should be wrapped normally. 241 verifyFormat( 242 "var MyLongClassName =\n" 243 " goog.module.get('my.long.module.name.followedBy.MyLongClassName');"); 244 } 245 246 TEST_F(FormatTestJS, FormatsFreestandingFunctions) { 247 verifyFormat("function outer1(a, b) {\n" 248 " function inner1(a, b) { return a; }\n" 249 " inner1(a, b);\n" 250 "}\n" 251 "function outer2(a, b) {\n" 252 " function inner2(a, b) { return a; }\n" 253 " inner2(a, b);\n" 254 "}"); 255 verifyFormat("function f() {}"); 256 } 257 258 TEST_F(FormatTestJS, ArrayLiterals) { 259 verifyFormat("var aaaaa: List<SomeThing> =\n" 260 " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];"); 261 verifyFormat("return [\n" 262 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 263 " bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 264 " ccccccccccccccccccccccccccc\n" 265 "];"); 266 verifyFormat("var someVariable = SomeFuntion([\n" 267 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 268 " bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 269 " ccccccccccccccccccccccccccc\n" 270 "]);"); 271 verifyFormat("var someVariable = SomeFuntion([\n" 272 " [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n" 273 "]);", 274 getGoogleJSStyleWithColumns(51)); 275 verifyFormat("var someVariable = SomeFuntion(aaaa, [\n" 276 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 277 " bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 278 " ccccccccccccccccccccccccccc\n" 279 "]);"); 280 verifyFormat("var someVariable = SomeFuntion(aaaa,\n" 281 " [\n" 282 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 283 " bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 284 " ccccccccccccccccccccccccccc\n" 285 " ],\n" 286 " aaaa);"); 287 288 verifyFormat("someFunction([], {a: a});"); 289 } 290 291 TEST_F(FormatTestJS, FunctionLiterals) { 292 verifyFormat("doFoo(function() {});"); 293 verifyFormat("doFoo(function() { return 1; });"); 294 verifyFormat("var func = function() {\n" 295 " return 1;\n" 296 "};"); 297 verifyFormat("var func = //\n" 298 " function() {\n" 299 " return 1;\n" 300 "};"); 301 verifyFormat("return {\n" 302 " body: {\n" 303 " setAttribute: function(key, val) { this[key] = val; },\n" 304 " getAttribute: function(key) { return this[key]; },\n" 305 " style: {direction: ''}\n" 306 " }\n" 307 "};"); 308 EXPECT_EQ("abc = xyz ?\n" 309 " function() {\n" 310 " return 1;\n" 311 " } :\n" 312 " function() {\n" 313 " return -1;\n" 314 " };", 315 format("abc=xyz?function(){return 1;}:function(){return -1;};")); 316 317 verifyFormat("var closure = goog.bind(\n" 318 " function() { // comment\n" 319 " foo();\n" 320 " bar();\n" 321 " },\n" 322 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n" 323 " arg3IsReallyLongAndNeeedsLineBreaks);"); 324 verifyFormat("var closure = goog.bind(function() { // comment\n" 325 " foo();\n" 326 " bar();\n" 327 "}, this);"); 328 verifyFormat("return {\n" 329 " a: 'E',\n" 330 " b: function() {\n" 331 " return function() {\n" 332 " f(); //\n" 333 " };\n" 334 " }\n" 335 "};"); 336 verifyFormat("{\n" 337 " var someVariable = function(x) {\n" 338 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 339 " };\n" 340 "}"); 341 verifyFormat("someLooooooooongFunction(\n" 342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 344 " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 345 " // code\n" 346 " });"); 347 348 verifyFormat("f({a: function() { return 1; }});", 349 getGoogleJSStyleWithColumns(33)); 350 verifyFormat("f({\n" 351 " a: function() { return 1; }\n" 352 "});", 353 getGoogleJSStyleWithColumns(32)); 354 355 verifyFormat("return {\n" 356 " a: function SomeFunction() {\n" 357 " // ...\n" 358 " return 1;\n" 359 " }\n" 360 "};"); 361 verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 362 " .then(goog.bind(function(aaaaaaaaaaa) {\n" 363 " someFunction();\n" 364 " someFunction();\n" 365 " }, this), aaaaaaaaaaaaaaaaa);"); 366 367 // FIXME: This is not ideal yet. 368 verifyFormat("someFunction(goog.bind(\n" 369 " function() {\n" 370 " doSomething();\n" 371 " doSomething();\n" 372 " },\n" 373 " this),\n" 374 " goog.bind(function() {\n" 375 " doSomething();\n" 376 " doSomething();\n" 377 " }, this));"); 378 379 // FIXME: This is bad, we should be wrapping before "function() {". 380 verifyFormat("someFunction(function() {\n" 381 " doSomething(); // break\n" 382 "})\n" 383 " .doSomethingElse(\n" 384 " // break\n" 385 " );"); 386 } 387 388 TEST_F(FormatTestJS, InliningFunctionLiterals) { 389 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 390 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 391 verifyFormat("var func = function() {\n" 392 " return 1;\n" 393 "};", 394 Style); 395 verifyFormat("var func = doSomething(function() { return 1; });", Style); 396 verifyFormat("var outer = function() {\n" 397 " var inner = function() { return 1; }\n" 398 "};", 399 Style); 400 verifyFormat("function outer1(a, b) {\n" 401 " function inner1(a, b) { return a; }\n" 402 "}", 403 Style); 404 405 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 406 verifyFormat("var func = function() { return 1; };", Style); 407 verifyFormat("var func = doSomething(function() { return 1; });", Style); 408 verifyFormat( 409 "var outer = function() { var inner = function() { return 1; } };", 410 Style); 411 verifyFormat("function outer1(a, b) {\n" 412 " function inner1(a, b) { return a; }\n" 413 "}", 414 Style); 415 416 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 417 verifyFormat("var func = function() {\n" 418 " return 1;\n" 419 "};", 420 Style); 421 verifyFormat("var func = doSomething(function() {\n" 422 " return 1;\n" 423 "});", 424 Style); 425 verifyFormat("var outer = function() {\n" 426 " var inner = function() {\n" 427 " return 1;\n" 428 " }\n" 429 "};", 430 Style); 431 verifyFormat("function outer1(a, b) {\n" 432 " function inner1(a, b) {\n" 433 " return a;\n" 434 " }\n" 435 "}", 436 Style); 437 } 438 439 TEST_F(FormatTestJS, MultipleFunctionLiterals) { 440 verifyFormat("promise.then(\n" 441 " function success() {\n" 442 " doFoo();\n" 443 " doBar();\n" 444 " },\n" 445 " function error() {\n" 446 " doFoo();\n" 447 " doBaz();\n" 448 " },\n" 449 " []);\n"); 450 verifyFormat("promise.then(\n" 451 " function success() {\n" 452 " doFoo();\n" 453 " doBar();\n" 454 " },\n" 455 " [],\n" 456 " function error() {\n" 457 " doFoo();\n" 458 " doBaz();\n" 459 " });\n"); 460 // FIXME: Here, we should probably break right after the "(" for consistency. 461 verifyFormat("promise.then([],\n" 462 " function success() {\n" 463 " doFoo();\n" 464 " doBar();\n" 465 " },\n" 466 " function error() {\n" 467 " doFoo();\n" 468 " doBaz();\n" 469 " });\n"); 470 471 verifyFormat("getSomeLongPromise()\n" 472 " .then(function(value) { body(); })\n" 473 " .thenCatch(function(error) {\n" 474 " body();\n" 475 " body();\n" 476 " });"); 477 verifyFormat("getSomeLongPromise()\n" 478 " .then(function(value) {\n" 479 " body();\n" 480 " body();\n" 481 " })\n" 482 " .thenCatch(function(error) {\n" 483 " body();\n" 484 " body();\n" 485 " });"); 486 487 verifyFormat("getSomeLongPromise()\n" 488 " .then(function(value) { body(); })\n" 489 " .thenCatch(function(error) { body(); });"); 490 } 491 492 TEST_F(FormatTestJS, ArrowFunctions) { 493 verifyFormat("var x = (a) => {\n" 494 " return a;\n" 495 "};"); 496 verifyFormat("var x = (a) => {\n" 497 " function y() { return 42; }\n" 498 " return a;\n" 499 "};"); 500 verifyFormat("var x = (a: type): {some: type} => {\n" 501 " return a;\n" 502 "};"); 503 verifyFormat("var x = (a) => a;"); 504 verifyFormat("return () => [];"); 505 verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n" 506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 507 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 508 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n" 509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 510 "};"); 511 verifyFormat( 512 "var a = a.aaaaaaa((a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n" 513 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 514 verifyFormat( 515 "var a = a.aaaaaaa((a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n" 516 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n" 517 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 518 519 // FIXME: This is bad, we should be wrapping before "() => {". 520 verifyFormat("someFunction(() => {\n" 521 " doSomething(); // break\n" 522 "})\n" 523 " .doSomethingElse(\n" 524 " // break\n" 525 " );"); 526 } 527 528 TEST_F(FormatTestJS, ReturnStatements) { 529 verifyFormat("function() {\n" 530 " return [hello, world];\n" 531 "}"); 532 } 533 534 TEST_F(FormatTestJS, AutomaticSemicolonInsertion) { 535 // The following statements must not wrap, as otherwise the program meaning 536 // would change due to automatic semicolon insertion. 537 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1. 538 verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10)); 539 verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10)); 540 verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10)); 541 verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10)); 542 verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10)); 543 verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10)); 544 } 545 546 TEST_F(FormatTestJS, ClosureStyleCasts) { 547 verifyFormat("var x = /** @type {foo} */ (bar);"); 548 } 549 550 TEST_F(FormatTestJS, TryCatch) { 551 verifyFormat("try {\n" 552 " f();\n" 553 "} catch (e) {\n" 554 " g();\n" 555 "} finally {\n" 556 " h();\n" 557 "}"); 558 559 // But, of course, "catch" is a perfectly fine function name in JavaScript. 560 verifyFormat("someObject.catch();"); 561 verifyFormat("someObject.new();"); 562 verifyFormat("someObject.delete();"); 563 } 564 565 TEST_F(FormatTestJS, StringLiteralConcatenation) { 566 verifyFormat("var literal = 'hello ' +\n" 567 " 'world';"); 568 } 569 570 TEST_F(FormatTestJS, RegexLiteralClassification) { 571 // Regex literals. 572 verifyFormat("var regex = /abc/;"); 573 verifyFormat("f(/abc/);"); 574 verifyFormat("f(abc, /abc/);"); 575 verifyFormat("some_map[/abc/];"); 576 verifyFormat("var x = a ? /abc/ : /abc/;"); 577 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 578 verifyFormat("var x = !/abc/.test(y);"); 579 verifyFormat("var x = a && /abc/.test(y);"); 580 verifyFormat("var x = a || /abc/.test(y);"); 581 verifyFormat("var x = a + /abc/.search(y);"); 582 verifyFormat("/abc/.search(y);"); 583 verifyFormat("var regexs = {/abc/, /abc/};"); 584 verifyFormat("return /abc/;"); 585 586 // Not regex literals. 587 verifyFormat("var a = a / 2 + b / 3;"); 588 } 589 590 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 591 verifyFormat("var regex = /=/;"); 592 verifyFormat("var regex = /a*/;"); 593 verifyFormat("var regex = /a+/;"); 594 verifyFormat("var regex = /a?/;"); 595 verifyFormat("var regex = /.a./;"); 596 verifyFormat("var regex = /a\\*/;"); 597 verifyFormat("var regex = /^a$/;"); 598 verifyFormat("var regex = /\\/a/;"); 599 verifyFormat("var regex = /(?:x)/;"); 600 verifyFormat("var regex = /x(?=y)/;"); 601 verifyFormat("var regex = /x(?!y)/;"); 602 verifyFormat("var regex = /x|y/;"); 603 verifyFormat("var regex = /a{2}/;"); 604 verifyFormat("var regex = /a{1,3}/;"); 605 verifyFormat("var regex = /[abc]/;"); 606 verifyFormat("var regex = /[^abc]/;"); 607 verifyFormat("var regex = /[\\b]/;"); 608 verifyFormat("var regex = /\\b/;"); 609 verifyFormat("var regex = /\\B/;"); 610 verifyFormat("var regex = /\\d/;"); 611 verifyFormat("var regex = /\\D/;"); 612 verifyFormat("var regex = /\\f/;"); 613 verifyFormat("var regex = /\\n/;"); 614 verifyFormat("var regex = /\\r/;"); 615 verifyFormat("var regex = /\\s/;"); 616 verifyFormat("var regex = /\\S/;"); 617 verifyFormat("var regex = /\\t/;"); 618 verifyFormat("var regex = /\\v/;"); 619 verifyFormat("var regex = /\\w/;"); 620 verifyFormat("var regex = /\\W/;"); 621 verifyFormat("var regex = /a(a)\\1/;"); 622 verifyFormat("var regex = /\\0/;"); 623 verifyFormat("var regex = /\\\\/g;"); 624 verifyFormat("var regex = /\\a\\\\/g;"); 625 verifyFormat("var regex = /\a\\//g;"); 626 verifyFormat("var regex = /a\\//;\n" 627 "var x = 0;"); 628 EXPECT_EQ("var regex = /'/g;", format("var regex = /'/g ;")); 629 EXPECT_EQ("var regex = /'/g; //'", format("var regex = /'/g ; //'")); 630 EXPECT_EQ("var regex = /\\/*/;\n" 631 "var x = 0;", 632 format("var regex = /\\/*/;\n" 633 "var x=0;")); 634 EXPECT_EQ("var x = /a\\//;", format("var x = /a\\// \n;")); 635 verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16)); 636 verifyFormat("var regex =\n" 637 " /\"/;", 638 getGoogleJSStyleWithColumns(15)); 639 } 640 641 TEST_F(FormatTestJS, RegexLiteralModifiers) { 642 verifyFormat("var regex = /abc/g;"); 643 verifyFormat("var regex = /abc/i;"); 644 verifyFormat("var regex = /abc/m;"); 645 verifyFormat("var regex = /abc/y;"); 646 } 647 648 TEST_F(FormatTestJS, RegexLiteralLength) { 649 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 650 getGoogleJSStyleWithColumns(60)); 651 verifyFormat("var regex =\n" 652 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 653 getGoogleJSStyleWithColumns(60)); 654 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 655 getGoogleJSStyleWithColumns(50)); 656 } 657 658 TEST_F(FormatTestJS, RegexLiteralExamples) { 659 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 660 } 661 662 TEST_F(FormatTestJS, TypeAnnotations) { 663 verifyFormat("var x: string;"); 664 verifyFormat("function x(): string {\n return 'x';\n}"); 665 verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}"); 666 verifyFormat("function x(y: string): string {\n return 'x';\n}"); 667 verifyFormat("for (var y: string in x) {\n x();\n}"); 668 verifyFormat("((a: string, b: number): string => a + b);"); 669 verifyFormat("var x: (y: number) => string;"); 670 verifyFormat("var x: P<string, (a: number) => string>;"); 671 verifyFormat("var x = {y: function(): z { return 1; }};"); 672 verifyFormat("var x = {y: function(): {a: number} { return 1; }};"); 673 } 674 675 TEST_F(FormatTestJS, ClassDeclarations) { 676 verifyFormat("class C {\n x: string = 12;\n}"); 677 verifyFormat("class C {\n x(): string => 12;\n}"); 678 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); 679 verifyFormat("class C {\n private x: string = 12;\n}"); 680 verifyFormat("class C {\n private static x: string = 12;\n}"); 681 verifyFormat("class C {\n static x(): string { return 'asd'; }\n}"); 682 verifyFormat("class C extends P implements I {}"); 683 verifyFormat("class C extends p.P implements i.I {}"); 684 685 // ':' is not a type declaration here. 686 verifyFormat("class X {\n" 687 " subs = {\n" 688 " 'b': {\n" 689 " 'c': 1,\n" 690 " },\n" 691 " };\n" 692 "}"); 693 } 694 695 TEST_F(FormatTestJS, InterfaceDeclarations) { 696 verifyFormat("interface I {\n" 697 " x: string;\n" 698 "}\n" 699 "var y;"); 700 } 701 702 TEST_F(FormatTestJS, EnumDeclarations) { 703 verifyFormat("enum Foo {\n" 704 " A = 1,\n" 705 " B\n" 706 "}"); 707 verifyFormat("export /* somecomment*/ enum Foo {\n" 708 " A = 1,\n" 709 " B\n" 710 "}"); 711 verifyFormat("enum Foo {\n" 712 " A = 1, // comment\n" 713 " B\n" 714 "}\n" 715 "var x = 1;"); 716 } 717 718 TEST_F(FormatTestJS, MetadataAnnotations) { 719 verifyFormat("@A\nclass C {\n}"); 720 verifyFormat("@A({arg: 'value'})\nclass C {\n}"); 721 verifyFormat("@A\n@B\nclass C {\n}"); 722 verifyFormat("class C {\n @A x: string;\n}"); 723 verifyFormat("class C {\n" 724 " @A\n" 725 " private x(): string {\n" 726 " return 'y';\n" 727 " }\n" 728 "}"); 729 verifyFormat("class X {}\n" 730 "class Y {}"); 731 } 732 733 TEST_F(FormatTestJS, Modules) { 734 verifyFormat("import SomeThing from 'some/module.js';"); 735 verifyFormat("import {X, Y} from 'some/module.js';"); 736 verifyFormat("import {\n" 737 " VeryLongImportsAreAnnoying,\n" 738 " VeryLongImportsAreAnnoying,\n" 739 " VeryLongImportsAreAnnoying,\n" 740 " VeryLongImportsAreAnnoying\n" 741 "} from 'some/module.js';"); 742 verifyFormat("import {\n" 743 " X,\n" 744 " Y,\n" 745 "} from 'some/module.js';"); 746 verifyFormat("import {\n" 747 " X,\n" 748 " Y,\n" 749 "} from 'some/long/module.js';", 750 getGoogleJSStyleWithColumns(20)); 751 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); 752 verifyFormat("import * as lib from 'some/module.js';"); 753 verifyFormat("var x = {import: 1};\nx.import = 2;"); 754 755 verifyFormat("export function fn() {\n" 756 " return 'fn';\n" 757 "}"); 758 verifyFormat("export function A() {}\n" 759 "export default function B() {}\n" 760 "export function C() {}"); 761 verifyFormat("export const x = 12;"); 762 verifyFormat("export default class X {}"); 763 verifyFormat("export {X, Y} from 'some/module.js';"); 764 verifyFormat("export {\n" 765 " X,\n" 766 " Y,\n" 767 "} from 'some/module.js';"); 768 verifyFormat("export class C {\n" 769 " x: number;\n" 770 " y: string;\n" 771 "}"); 772 verifyFormat("export class X { y: number; }"); 773 verifyFormat("export default class X { y: number }"); 774 verifyFormat("export default function() {\n return 1;\n}"); 775 verifyFormat("export var x = 12;"); 776 verifyFormat("class C {}\n" 777 "export function f() {}\n" 778 "var v;"); 779 verifyFormat("export var x: number = 12;"); 780 verifyFormat("export const y = {\n" 781 " a: 1,\n" 782 " b: 2\n" 783 "};"); 784 verifyFormat("export enum Foo {\n" 785 " BAR,\n" 786 " // adsdasd\n" 787 " BAZ\n" 788 "}"); 789 } 790 791 TEST_F(FormatTestJS, TemplateStrings) { 792 // Keeps any whitespace/indentation within the template string. 793 EXPECT_EQ("var x = `hello\n" 794 " ${ name }\n" 795 " !`;", 796 format("var x = `hello\n" 797 " ${ name }\n" 798 " !`;")); 799 800 verifyFormat("var x =\n" 801 " `hello ${world}` >= some();", 802 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. 803 verifyFormat("var x = `hello ${world}` >= some();", 804 getGoogleJSStyleWithColumns(35)); // Barely fits. 805 EXPECT_EQ("var x = `hello\n" 806 " ${world}` >=\n" 807 " some();", 808 format("var x =\n" 809 " `hello\n" 810 " ${world}` >= some();", 811 getGoogleJSStyleWithColumns(21))); // Barely doesn't fit. 812 EXPECT_EQ("var x = `hello\n" 813 " ${world}` >= some();", 814 format("var x =\n" 815 " `hello\n" 816 " ${world}` >= some();", 817 getGoogleJSStyleWithColumns(22))); // Barely fits. 818 819 verifyFormat("var x =\n" 820 " `h`;", 821 getGoogleJSStyleWithColumns(11)); 822 EXPECT_EQ( 823 "var x =\n `multi\n line`;", 824 format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(13))); 825 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 826 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);"); 827 828 // Make sure template strings get a proper ColumnWidth assigned, even if they 829 // are first token in line. 830 verifyFormat( 831 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 832 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); 833 834 // Two template strings. 835 verifyFormat("var x = `hello` == `hello`;"); 836 837 // Comments in template strings. 838 EXPECT_EQ("var x = `//a`;\n" 839 "var y;", 840 format("var x =\n `//a`;\n" 841 "var y ;")); 842 EXPECT_EQ("var x = `/*a`;\n" 843 "var y;", 844 format("var x =\n `/*a`;\n" 845 "var y;")); 846 // Unterminated string literals in a template string. 847 verifyFormat("var x = `'`; // comment with matching quote '\n" 848 "var y;"); 849 verifyFormat("var x = `\"`; // comment with matching quote \"\n" 850 "var y;"); 851 // Backticks in a comment - not a template string. 852 EXPECT_EQ("var x = 1 // `/*a`;\n" 853 " ;", 854 format("var x =\n 1 // `/*a`;\n" 855 " ;")); 856 EXPECT_EQ("/* ` */ var x = 1; /* ` */", 857 format("/* ` */ var x\n= 1; /* ` */")); 858 // Comment spans multiple template strings. 859 EXPECT_EQ("var x = `/*a`;\n" 860 "var y = ` */ `;", 861 format("var x =\n `/*a`;\n" 862 "var y =\n ` */ `;")); 863 // Escaped backtick. 864 EXPECT_EQ("var x = ` \\` a`;\n" 865 "var y;", 866 format("var x = ` \\` a`;\n" 867 "var y;")); 868 } 869 870 TEST_F(FormatTestJS, CastSyntax) { verifyFormat("var x = <type>foo;"); } 871 872 TEST_F(FormatTestJS, TypeArguments) { 873 verifyFormat("class X<Y> {}"); 874 verifyFormat("new X<Y>();"); 875 verifyFormat("foo<Y>(a);"); 876 verifyFormat("var x: X<Y>[];"); 877 verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); 878 verifyFormat("function f(a: List<any> = null) {}"); 879 verifyFormat("function f(): List<any> {}"); 880 } 881 882 TEST_F(FormatTestJS, OptionalTypes) { 883 verifyFormat("function x(a?: b, c?, d?) {}"); 884 verifyFormat("class X {\n" 885 " y?: z;\n" 886 " z?;\n" 887 "}"); 888 verifyFormat("interface X {\n" 889 " y?(): z;\n" 890 "}"); 891 verifyFormat("x ? 1 : 2;"); 892 verifyFormat("constructor({aa}: {\n" 893 " aa?: string,\n" 894 " aaaaaaaa?: string,\n" 895 " aaaaaaaaaaaaaaa?: boolean,\n" 896 " aaaaaa?: List<string>\n" 897 "}) {}"); 898 } 899 900 TEST_F(FormatTestJS, IndexSignature) { 901 verifyFormat("var x: {[k: string]: v};"); 902 } 903 904 } // end namespace tooling 905 } // end namespace clang 906