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