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 FormattingAttemptStatus Status; 28 tooling::Replacements Replaces = 29 reformat(Style, Code, Ranges, "<stdin>", &Status); 30 EXPECT_TRUE(Status.FormatComplete); 31 auto Result = applyAllReplacements(Code, Replaces); 32 EXPECT_TRUE(static_cast<bool>(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("of(null);"); 136 verifyFormat("import {of} from 'x';"); 137 verifyFormat("x.in() = 1;"); 138 verifyFormat("x.let() = 1;"); 139 verifyFormat("x.var() = 1;"); 140 verifyFormat("x.for() = 1;"); 141 verifyFormat("x.as() = 1;"); 142 verifyFormat("x = {\n" 143 " a: 12,\n" 144 " interface: 1,\n" 145 " switch: 1,\n" 146 "};"); 147 verifyFormat("var struct = 2;"); 148 verifyFormat("var union = 2;"); 149 verifyFormat("var interface = 2;"); 150 verifyFormat("interface = 2;"); 151 verifyFormat("x = interface instanceof y;"); 152 } 153 154 TEST_F(FormatTestJS, ReservedWordsMethods) { 155 verifyFormat( 156 "class X {\n" 157 " delete() {\n" 158 " x();\n" 159 " }\n" 160 " interface() {\n" 161 " x();\n" 162 " }\n" 163 " let() {\n" 164 " x();\n" 165 " }\n" 166 "}\n"); 167 } 168 169 TEST_F(FormatTestJS, CppKeywords) { 170 // Make sure we don't mess stuff up because of C++ keywords. 171 verifyFormat("return operator && (aa);"); 172 // .. or QT ones. 173 verifyFormat("slots: Slot[];"); 174 } 175 176 TEST_F(FormatTestJS, ES6DestructuringAssignment) { 177 verifyFormat("var [a, b, c] = [1, 2, 3];"); 178 verifyFormat("const [a, b, c] = [1, 2, 3];"); 179 verifyFormat("let [a, b, c] = [1, 2, 3];"); 180 verifyFormat("var {a, b} = {a: 1, b: 2};"); 181 verifyFormat("let {a, b} = {a: 1, b: 2};"); 182 } 183 184 TEST_F(FormatTestJS, ContainerLiterals) { 185 verifyFormat("var x = {\n" 186 " y: function(a) {\n" 187 " return a;\n" 188 " }\n" 189 "};"); 190 verifyFormat("return {\n" 191 " link: function() {\n" 192 " f(); //\n" 193 " }\n" 194 "};"); 195 verifyFormat("return {\n" 196 " a: a,\n" 197 " link: function() {\n" 198 " f(); //\n" 199 " }\n" 200 "};"); 201 verifyFormat("return {\n" 202 " a: a,\n" 203 " link: function() {\n" 204 " f(); //\n" 205 " },\n" 206 " link: function() {\n" 207 " f(); //\n" 208 " }\n" 209 "};"); 210 verifyFormat("var stuff = {\n" 211 " // comment for update\n" 212 " update: false,\n" 213 " // comment for modules\n" 214 " modules: false,\n" 215 " // comment for tasks\n" 216 " tasks: false\n" 217 "};"); 218 verifyFormat("return {\n" 219 " 'finish':\n" 220 " //\n" 221 " a\n" 222 "};"); 223 verifyFormat("var obj = {\n" 224 " fooooooooo: function(x) {\n" 225 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 226 " }\n" 227 "};"); 228 // Simple object literal, as opposed to enum style below. 229 verifyFormat("var obj = {a: 123};"); 230 // Enum style top level assignment. 231 verifyFormat("X = {\n a: 123\n};"); 232 verifyFormat("X.Y = {\n a: 123\n};"); 233 // But only on the top level, otherwise its a plain object literal assignment. 234 verifyFormat("function x() {\n" 235 " y = {z: 1};\n" 236 "}"); 237 verifyFormat("x = foo && {a: 123};"); 238 239 // Arrow functions in object literals. 240 verifyFormat("var x = {\n" 241 " y: (a) => {\n" 242 " return a;\n" 243 " }\n" 244 "};"); 245 verifyFormat("var x = {y: (a) => a};"); 246 247 // Methods in object literals. 248 verifyFormat("var x = {\n" 249 " y(a: string): number {\n" 250 " return a;\n" 251 " }\n" 252 "};"); 253 verifyFormat("var x = {\n" 254 " y(a: string) {\n" 255 " return a;\n" 256 " }\n" 257 "};"); 258 259 // Computed keys. 260 verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};"); 261 verifyFormat("var x = {\n" 262 " [a]: 1,\n" 263 " b: 2,\n" 264 " [c]: 3,\n" 265 "};"); 266 267 // Object literals can leave out labels. 268 verifyFormat("f({a}, () => {\n" 269 " g(); //\n" 270 "});"); 271 272 // Keys can be quoted. 273 verifyFormat("var x = {\n" 274 " a: a,\n" 275 " b: b,\n" 276 " 'c': c,\n" 277 "};"); 278 279 // Dict literals can skip the label names. 280 verifyFormat("var x = {\n" 281 " aaa,\n" 282 " aaa,\n" 283 " aaa,\n" 284 "};"); 285 verifyFormat("return {\n" 286 " a,\n" 287 " b: 'b',\n" 288 " c,\n" 289 "};"); 290 } 291 292 TEST_F(FormatTestJS, MethodsInObjectLiterals) { 293 verifyFormat("var o = {\n" 294 " value: 'test',\n" 295 " get value() { // getter\n" 296 " return this.value;\n" 297 " }\n" 298 "};"); 299 verifyFormat("var o = {\n" 300 " value: 'test',\n" 301 " set value(val) { // setter\n" 302 " this.value = val;\n" 303 " }\n" 304 "};"); 305 verifyFormat("var o = {\n" 306 " value: 'test',\n" 307 " someMethod(val) { // method\n" 308 " doSomething(this.value + val);\n" 309 " }\n" 310 "};"); 311 verifyFormat("var o = {\n" 312 " someMethod(val) { // method\n" 313 " doSomething(this.value + val);\n" 314 " },\n" 315 " someOtherMethod(val) { // method\n" 316 " doSomething(this.value + val);\n" 317 " }\n" 318 "};"); 319 } 320 321 TEST_F(FormatTestJS, GettersSettersVisibilityKeywords) { 322 // Don't break after "protected" 323 verifyFormat("class X {\n" 324 " protected get getter():\n" 325 " number {\n" 326 " return 1;\n" 327 " }\n" 328 "}", 329 getGoogleJSStyleWithColumns(12)); 330 // Don't break after "get" 331 verifyFormat("class X {\n" 332 " protected get someReallyLongGetterName():\n" 333 " number {\n" 334 " return 1;\n" 335 " }\n" 336 "}", 337 getGoogleJSStyleWithColumns(40)); 338 } 339 340 TEST_F(FormatTestJS, SpacesInContainerLiterals) { 341 verifyFormat("var arr = [1, 2, 3];"); 342 verifyFormat("f({a: 1, b: 2, c: 3});"); 343 344 verifyFormat("var object_literal_with_long_name = {\n" 345 " a: 'aaaaaaaaaaaaaaaaaa',\n" 346 " b: 'bbbbbbbbbbbbbbbbbb'\n" 347 "};"); 348 349 verifyFormat("f({a: 1, b: 2, c: 3});", 350 getChromiumStyle(FormatStyle::LK_JavaScript)); 351 verifyFormat("f({'a': [{}]});"); 352 } 353 354 TEST_F(FormatTestJS, SingleQuotedStrings) { 355 verifyFormat("this.function('', true);"); 356 } 357 358 TEST_F(FormatTestJS, GoogScopes) { 359 verifyFormat("goog.scope(function() {\n" 360 "var x = a.b;\n" 361 "var y = c.d;\n" 362 "}); // goog.scope"); 363 verifyFormat("goog.scope(function() {\n" 364 "// test\n" 365 "var x = 0;\n" 366 "// test\n" 367 "});"); 368 } 369 370 TEST_F(FormatTestJS, GoogModules) { 371 verifyFormat("goog.module('this.is.really.absurdly.long');", 372 getGoogleJSStyleWithColumns(40)); 373 verifyFormat("goog.require('this.is.really.absurdly.long');", 374 getGoogleJSStyleWithColumns(40)); 375 verifyFormat("goog.provide('this.is.really.absurdly.long');", 376 getGoogleJSStyleWithColumns(40)); 377 verifyFormat("var long = goog.require('this.is.really.absurdly.long');", 378 getGoogleJSStyleWithColumns(40)); 379 verifyFormat("goog.setTestOnly('this.is.really.absurdly.long');", 380 getGoogleJSStyleWithColumns(40)); 381 verifyFormat("goog.forwardDeclare('this.is.really.absurdly.long');", 382 getGoogleJSStyleWithColumns(40)); 383 384 // These should be wrapped normally. 385 verifyFormat( 386 "var MyLongClassName =\n" 387 " goog.module.get('my.long.module.name.followedBy.MyLongClassName');"); 388 } 389 390 TEST_F(FormatTestJS, FormatsNamespaces) { 391 verifyFormat("namespace Foo {\n" 392 " export let x = 1;\n" 393 "}\n"); 394 verifyFormat("declare namespace Foo {\n" 395 " export let x: number;\n" 396 "}\n"); 397 } 398 399 TEST_F(FormatTestJS, NamespacesMayNotWrap) { 400 verifyFormat("declare namespace foobarbaz {\n" 401 "}\n", getGoogleJSStyleWithColumns(18)); 402 verifyFormat("declare module foobarbaz {\n" 403 "}\n", getGoogleJSStyleWithColumns(15)); 404 verifyFormat("namespace foobarbaz {\n" 405 "}\n", getGoogleJSStyleWithColumns(10)); 406 verifyFormat("module foobarbaz {\n" 407 "}\n", getGoogleJSStyleWithColumns(7)); 408 } 409 410 TEST_F(FormatTestJS, AmbientDeclarations) { 411 FormatStyle NineCols = getGoogleJSStyleWithColumns(9); 412 verifyFormat( 413 "declare class\n" 414 " X {}", 415 NineCols); 416 verifyFormat( 417 "declare function\n" 418 "x();", // TODO(martinprobst): should ideally be indented. 419 NineCols); 420 verifyFormat("declare function foo();\n" 421 "let x = 1;\n"); 422 verifyFormat("declare function foo(): string;\n" 423 "let x = 1;\n"); 424 verifyFormat("declare function foo(): {x: number};\n" 425 "let x = 1;\n"); 426 verifyFormat("declare class X {}\n" 427 "let x = 1;\n"); 428 verifyFormat("declare interface Y {}\n" 429 "let x = 1;\n"); 430 verifyFormat( 431 "declare enum X {\n" 432 "}", 433 NineCols); 434 verifyFormat( 435 "declare let\n" 436 " x: number;", 437 NineCols); 438 } 439 440 TEST_F(FormatTestJS, FormatsFreestandingFunctions) { 441 verifyFormat("function outer1(a, b) {\n" 442 " function inner1(a, b) {\n" 443 " return a;\n" 444 " }\n" 445 " inner1(a, b);\n" 446 "}\n" 447 "function outer2(a, b) {\n" 448 " function inner2(a, b) {\n" 449 " return a;\n" 450 " }\n" 451 " inner2(a, b);\n" 452 "}"); 453 verifyFormat("function f() {}"); 454 } 455 456 TEST_F(FormatTestJS, GeneratorFunctions) { 457 verifyFormat("function* f() {\n" 458 " let x = 1;\n" 459 " yield x;\n" 460 " yield* something();\n" 461 " yield [1, 2];\n" 462 " yield {a: 1};\n" 463 "}"); 464 verifyFormat("function*\n" 465 " f() {\n" 466 "}", 467 getGoogleJSStyleWithColumns(8)); 468 verifyFormat("export function* f() {\n" 469 " yield 1;\n" 470 "}\n"); 471 verifyFormat("class X {\n" 472 " * generatorMethod() {\n" 473 " yield x;\n" 474 " }\n" 475 "}"); 476 verifyFormat("var x = {\n" 477 " a: function*() {\n" 478 " //\n" 479 " }\n" 480 "}\n"); 481 } 482 483 TEST_F(FormatTestJS, AsyncFunctions) { 484 verifyFormat("async function f() {\n" 485 " let x = 1;\n" 486 " return fetch(x);\n" 487 "}"); 488 verifyFormat("async function f() {\n" 489 " return 1;\n" 490 "}\n" 491 "\n" 492 "function a() {\n" 493 " return 1;\n" 494 "}\n", 495 " async function f() {\n" 496 " return 1;\n" 497 "}\n" 498 "\n" 499 " function a() {\n" 500 " return 1;\n" 501 "} \n"); 502 verifyFormat("async function* f() {\n" 503 " yield fetch(x);\n" 504 "}"); 505 verifyFormat("export async function f() {\n" 506 " return fetch(x);\n" 507 "}"); 508 verifyFormat("let x = async () => f();"); 509 verifyFormat("let x = async function() {\n" 510 " f();\n" 511 "};"); 512 verifyFormat("let x = async();"); 513 verifyFormat("class X {\n" 514 " async asyncMethod() {\n" 515 " return fetch(1);\n" 516 " }\n" 517 "}"); 518 verifyFormat("function initialize() {\n" 519 " // Comment.\n" 520 " return async.then();\n" 521 "}\n"); 522 } 523 524 TEST_F(FormatTestJS, ArrayLiterals) { 525 verifyFormat("var aaaaa: List<SomeThing> =\n" 526 " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];"); 527 verifyFormat("return [\n" 528 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 529 " ccccccccccccccccccccccccccc\n" 530 "];"); 531 verifyFormat("return [\n" 532 " aaaa().bbbbbbbb('A'),\n" 533 " aaaa().bbbbbbbb('B'),\n" 534 " aaaa().bbbbbbbb('C'),\n" 535 "];"); 536 verifyFormat("var someVariable = SomeFunction([\n" 537 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 538 " ccccccccccccccccccccccccccc\n" 539 "]);"); 540 verifyFormat("var someVariable = SomeFunction([\n" 541 " [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n" 542 "]);", 543 getGoogleJSStyleWithColumns(51)); 544 verifyFormat("var someVariable = SomeFunction(aaaa, [\n" 545 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 546 " ccccccccccccccccccccccccccc\n" 547 "]);"); 548 verifyFormat("var someVariable = SomeFunction(\n" 549 " aaaa,\n" 550 " [\n" 551 " aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 552 " cccccccccccccccccccccccccc\n" 553 " ],\n" 554 " aaaa);"); 555 verifyFormat("var aaaa = aaaaa || // wrap\n" 556 " [];"); 557 558 verifyFormat("someFunction([], {a: a});"); 559 560 verifyFormat("var string = [\n" 561 " 'aaaaaa',\n" 562 " 'bbbbbb',\n" 563 "].join('+');"); 564 } 565 566 TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) { 567 verifyFormat("var array = [\n" 568 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 569 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 570 "];"); 571 verifyFormat("var array = someFunction([\n" 572 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 573 " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n" 574 "]);"); 575 } 576 577 TEST_F(FormatTestJS, FunctionLiterals) { 578 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 579 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 580 verifyFormat("doFoo(function() {});"); 581 verifyFormat("doFoo(function() { return 1; });", Style); 582 verifyFormat("var func = function() {\n" 583 " return 1;\n" 584 "};"); 585 verifyFormat("var func = //\n" 586 " function() {\n" 587 " return 1;\n" 588 "};"); 589 verifyFormat("return {\n" 590 " body: {\n" 591 " setAttribute: function(key, val) { this[key] = val; },\n" 592 " getAttribute: function(key) { return this[key]; },\n" 593 " style: {direction: ''}\n" 594 " }\n" 595 "};", 596 Style); 597 verifyFormat("abc = xyz ? function() {\n" 598 " return 1;\n" 599 "} : function() {\n" 600 " return -1;\n" 601 "};"); 602 603 verifyFormat("var closure = goog.bind(\n" 604 " function() { // comment\n" 605 " foo();\n" 606 " bar();\n" 607 " },\n" 608 " this, arg1IsReallyLongAndNeedsLineBreaks,\n" 609 " arg3IsReallyLongAndNeedsLineBreaks);"); 610 verifyFormat("var closure = goog.bind(function() { // comment\n" 611 " foo();\n" 612 " bar();\n" 613 "}, this);"); 614 verifyFormat("return {\n" 615 " a: 'E',\n" 616 " b: function() {\n" 617 " return function() {\n" 618 " f(); //\n" 619 " };\n" 620 " }\n" 621 "};"); 622 verifyFormat("{\n" 623 " var someVariable = function(x) {\n" 624 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 625 " };\n" 626 "}"); 627 verifyFormat("someLooooooooongFunction(\n" 628 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 630 " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 631 " // code\n" 632 " });"); 633 634 verifyFormat("return {\n" 635 " a: function SomeFunction() {\n" 636 " // ...\n" 637 " return 1;\n" 638 " }\n" 639 "};"); 640 verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 641 " .then(goog.bind(function(aaaaaaaaaaa) {\n" 642 " someFunction();\n" 643 " someFunction();\n" 644 " }, this), aaaaaaaaaaaaaaaaa);"); 645 646 verifyFormat("someFunction(goog.bind(function() {\n" 647 " doSomething();\n" 648 " doSomething();\n" 649 "}, this), goog.bind(function() {\n" 650 " doSomething();\n" 651 " doSomething();\n" 652 "}, this));"); 653 654 verifyFormat("SomeFunction(function() {\n" 655 " foo();\n" 656 " bar();\n" 657 "}.bind(this));"); 658 659 // FIXME: This is bad, we should be wrapping before "function() {". 660 verifyFormat("someFunction(function() {\n" 661 " doSomething(); // break\n" 662 "})\n" 663 " .doSomethingElse(\n" 664 " // break\n" 665 " );"); 666 667 Style.ColumnLimit = 33; 668 verifyFormat("f({a: function() { return 1; }});", Style); 669 Style.ColumnLimit = 32; 670 verifyFormat("f({\n" 671 " a: function() { return 1; }\n" 672 "});", 673 Style); 674 675 } 676 677 TEST_F(FormatTestJS, InliningFunctionLiterals) { 678 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 679 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 680 verifyFormat("var func = function() {\n" 681 " return 1;\n" 682 "};", 683 Style); 684 verifyFormat("var func = doSomething(function() { return 1; });", Style); 685 verifyFormat("var outer = function() {\n" 686 " var inner = function() { return 1; }\n" 687 "};", 688 Style); 689 verifyFormat("function outer1(a, b) {\n" 690 " function inner1(a, b) { return a; }\n" 691 "}", 692 Style); 693 694 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 695 verifyFormat("var func = function() { return 1; };", Style); 696 verifyFormat("var func = doSomething(function() { return 1; });", Style); 697 verifyFormat( 698 "var outer = function() { var inner = function() { return 1; } };", 699 Style); 700 verifyFormat("function outer1(a, b) {\n" 701 " function inner1(a, b) { return a; }\n" 702 "}", 703 Style); 704 705 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 706 verifyFormat("var func = function() {\n" 707 " return 1;\n" 708 "};", 709 Style); 710 verifyFormat("var func = doSomething(function() {\n" 711 " return 1;\n" 712 "});", 713 Style); 714 verifyFormat("var outer = function() {\n" 715 " var inner = function() {\n" 716 " return 1;\n" 717 " }\n" 718 "};", 719 Style); 720 verifyFormat("function outer1(a, b) {\n" 721 " function inner1(a, b) {\n" 722 " return a;\n" 723 " }\n" 724 "}", 725 Style); 726 727 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 728 verifyFormat("var func = function() {\n" 729 " return 1;\n" 730 "};", 731 Style); 732 } 733 734 TEST_F(FormatTestJS, MultipleFunctionLiterals) { 735 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 736 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 737 verifyFormat("promise.then(\n" 738 " function success() {\n" 739 " doFoo();\n" 740 " doBar();\n" 741 " },\n" 742 " function error() {\n" 743 " doFoo();\n" 744 " doBaz();\n" 745 " },\n" 746 " []);\n"); 747 verifyFormat("promise.then(\n" 748 " function success() {\n" 749 " doFoo();\n" 750 " doBar();\n" 751 " },\n" 752 " [],\n" 753 " function error() {\n" 754 " doFoo();\n" 755 " doBaz();\n" 756 " });\n"); 757 verifyFormat("promise.then(\n" 758 " [],\n" 759 " function success() {\n" 760 " doFoo();\n" 761 " doBar();\n" 762 " },\n" 763 " function error() {\n" 764 " doFoo();\n" 765 " doBaz();\n" 766 " });\n"); 767 768 verifyFormat("getSomeLongPromise()\n" 769 " .then(function(value) { body(); })\n" 770 " .thenCatch(function(error) {\n" 771 " body();\n" 772 " body();\n" 773 " });", 774 Style); 775 verifyFormat("getSomeLongPromise()\n" 776 " .then(function(value) {\n" 777 " body();\n" 778 " body();\n" 779 " })\n" 780 " .thenCatch(function(error) {\n" 781 " body();\n" 782 " body();\n" 783 " });"); 784 785 verifyFormat("getSomeLongPromise()\n" 786 " .then(function(value) { body(); })\n" 787 " .thenCatch(function(error) { body(); });", 788 Style); 789 790 verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n" 791 " .aaaaaaa(function() {\n" 792 " //\n" 793 " })\n" 794 " .bbbbbb();"); 795 } 796 797 TEST_F(FormatTestJS, ArrowFunctions) { 798 verifyFormat("var x = (a) => {\n" 799 " return a;\n" 800 "};"); 801 verifyFormat("var x = (a) => {\n" 802 " function y() {\n" 803 " return 42;\n" 804 " }\n" 805 " return a;\n" 806 "};"); 807 verifyFormat("var x = (a: type): {some: type} => {\n" 808 " return a;\n" 809 "};"); 810 verifyFormat("var x = (a) => a;"); 811 verifyFormat("return () => [];"); 812 verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n" 813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 814 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n" 816 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 817 "};"); 818 verifyFormat("var a = a.aaaaaaa(\n" 819 " (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n" 820 " aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 821 verifyFormat("var a = a.aaaaaaa(\n" 822 " (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n" 823 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n" 824 " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));"); 825 826 // FIXME: This is bad, we should be wrapping before "() => {". 827 verifyFormat("someFunction(() => {\n" 828 " doSomething(); // break\n" 829 "})\n" 830 " .doSomethingElse(\n" 831 " // break\n" 832 " );"); 833 } 834 835 TEST_F(FormatTestJS, ReturnStatements) { 836 verifyFormat("function() {\n" 837 " return [hello, world];\n" 838 "}"); 839 } 840 841 TEST_F(FormatTestJS, ForLoops) { 842 verifyFormat("for (var i in [2, 3]) {\n" 843 "}"); 844 verifyFormat("for (var i of [2, 3]) {\n" 845 "}"); 846 verifyFormat("for (let {a, b} of x) {\n" 847 "}"); 848 verifyFormat("for (let {a, b} in x) {\n" 849 "}"); 850 } 851 852 TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) { 853 // The following statements must not wrap, as otherwise the program meaning 854 // would change due to automatic semicolon insertion. 855 // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1. 856 verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10)); 857 verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10)); 858 verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10)); 859 verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10)); 860 verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10)); 861 verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10)); 862 verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10)); 863 verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10)); 864 verifyFormat("return [\n" 865 " aaa\n" 866 "];", 867 getGoogleJSStyleWithColumns(12)); 868 } 869 870 TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) { 871 verifyFormat("a\n" 872 "b;", 873 " a \n" 874 " b ;"); 875 verifyFormat("a()\n" 876 "b;", 877 " a ()\n" 878 " b ;"); 879 verifyFormat("a[b]\n" 880 "c;", 881 "a [b]\n" 882 "c ;"); 883 verifyFormat("1\n" 884 "a;", 885 "1 \n" 886 "a ;"); 887 verifyFormat("a\n" 888 "1;", 889 "a \n" 890 "1 ;"); 891 verifyFormat("a\n" 892 "'x';", 893 "a \n" 894 " 'x';"); 895 verifyFormat("a++\n" 896 "b;", 897 "a ++\n" 898 "b ;"); 899 verifyFormat("a\n" 900 "!b && c;", 901 "a \n" 902 " ! b && c;"); 903 verifyFormat("a\n" 904 "if (1) f();", 905 " a\n" 906 " if (1) f();"); 907 verifyFormat("a\n" 908 "class X {}", 909 " a\n" 910 " class X {}"); 911 verifyFormat("var a", "var\n" 912 "a"); 913 verifyFormat("x instanceof String", "x\n" 914 "instanceof\n" 915 "String"); 916 verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n" 917 " bar) {}"); 918 verifyFormat("a = true\n" 919 "return 1", 920 "a = true\n" 921 " return 1"); 922 verifyFormat("a = 's'\n" 923 "return 1", 924 "a = 's'\n" 925 " return 1"); 926 verifyFormat("a = null\n" 927 "return 1", 928 "a = null\n" 929 " return 1"); 930 // Below "class Y {}" should ideally be on its own line. 931 verifyFormat( 932 "x = {\n" 933 " a: 1\n" 934 "} class Y {}", 935 " x = {a : 1}\n" 936 " class Y { }"); 937 verifyFormat( 938 "if (x) {\n" 939 "}\n" 940 "return 1", 941 "if (x) {}\n" 942 " return 1"); 943 verifyFormat( 944 "if (x) {\n" 945 "}\n" 946 "class X {}", 947 "if (x) {}\n" 948 " class X {}"); 949 } 950 951 TEST_F(FormatTestJS, ImportExportASI) { 952 verifyFormat( 953 "import {x} from 'y'\n" 954 "export function z() {}", 955 "import {x} from 'y'\n" 956 " export function z() {}"); 957 // Below "class Y {}" should ideally be on its own line. 958 verifyFormat( 959 "export {x} class Y {}", 960 " export {x}\n" 961 " class Y {\n}"); 962 verifyFormat( 963 "if (x) {\n" 964 "}\n" 965 "export class Y {}", 966 "if ( x ) { }\n" 967 " export class Y {}"); 968 } 969 970 TEST_F(FormatTestJS, ClosureStyleCasts) { 971 verifyFormat("var x = /** @type {foo} */ (bar);"); 972 } 973 974 TEST_F(FormatTestJS, TryCatch) { 975 verifyFormat("try {\n" 976 " f();\n" 977 "} catch (e) {\n" 978 " g();\n" 979 "} finally {\n" 980 " h();\n" 981 "}"); 982 983 // But, of course, "catch" is a perfectly fine function name in JavaScript. 984 verifyFormat("someObject.catch();"); 985 verifyFormat("someObject.new();"); 986 verifyFormat("someObject.delete();"); 987 } 988 989 TEST_F(FormatTestJS, StringLiteralConcatenation) { 990 verifyFormat("var literal = 'hello ' +\n" 991 " 'world';"); 992 } 993 994 TEST_F(FormatTestJS, RegexLiteralClassification) { 995 // Regex literals. 996 verifyFormat("var regex = /abc/;"); 997 verifyFormat("f(/abc/);"); 998 verifyFormat("f(abc, /abc/);"); 999 verifyFormat("some_map[/abc/];"); 1000 verifyFormat("var x = a ? /abc/ : /abc/;"); 1001 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 1002 verifyFormat("var x = !/abc/.test(y);"); 1003 verifyFormat("var x = foo()! / 10;"); 1004 verifyFormat("var x = a && /abc/.test(y);"); 1005 verifyFormat("var x = a || /abc/.test(y);"); 1006 verifyFormat("var x = a + /abc/.search(y);"); 1007 verifyFormat("/abc/.search(y);"); 1008 verifyFormat("var regexs = {/abc/, /abc/};"); 1009 verifyFormat("return /abc/;"); 1010 1011 // Not regex literals. 1012 verifyFormat("var a = a / 2 + b / 3;"); 1013 verifyFormat("var a = a++ / 2;"); 1014 // Prefix unary can operate on regex literals, not that it makes sense. 1015 verifyFormat("var a = ++/a/;"); 1016 1017 // This is a known issue, regular expressions are incorrectly detected if 1018 // directly following a closing parenthesis. 1019 verifyFormat("if (foo) / bar /.exec(baz);"); 1020 } 1021 1022 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 1023 verifyFormat("var regex = /=/;"); 1024 verifyFormat("var regex = /a*/;"); 1025 verifyFormat("var regex = /a+/;"); 1026 verifyFormat("var regex = /a?/;"); 1027 verifyFormat("var regex = /.a./;"); 1028 verifyFormat("var regex = /a\\*/;"); 1029 verifyFormat("var regex = /^a$/;"); 1030 verifyFormat("var regex = /\\/a/;"); 1031 verifyFormat("var regex = /(?:x)/;"); 1032 verifyFormat("var regex = /x(?=y)/;"); 1033 verifyFormat("var regex = /x(?!y)/;"); 1034 verifyFormat("var regex = /x|y/;"); 1035 verifyFormat("var regex = /a{2}/;"); 1036 verifyFormat("var regex = /a{1,3}/;"); 1037 1038 verifyFormat("var regex = /[abc]/;"); 1039 verifyFormat("var regex = /[^abc]/;"); 1040 verifyFormat("var regex = /[\\b]/;"); 1041 verifyFormat("var regex = /[/]/;"); 1042 verifyFormat("var regex = /[\\/]/;"); 1043 verifyFormat("var regex = /\\[/;"); 1044 verifyFormat("var regex = /\\\\[/]/;"); 1045 verifyFormat("var regex = /}[\"]/;"); 1046 verifyFormat("var regex = /}[/\"]/;"); 1047 verifyFormat("var regex = /}[\"/]/;"); 1048 1049 verifyFormat("var regex = /\\b/;"); 1050 verifyFormat("var regex = /\\B/;"); 1051 verifyFormat("var regex = /\\d/;"); 1052 verifyFormat("var regex = /\\D/;"); 1053 verifyFormat("var regex = /\\f/;"); 1054 verifyFormat("var regex = /\\n/;"); 1055 verifyFormat("var regex = /\\r/;"); 1056 verifyFormat("var regex = /\\s/;"); 1057 verifyFormat("var regex = /\\S/;"); 1058 verifyFormat("var regex = /\\t/;"); 1059 verifyFormat("var regex = /\\v/;"); 1060 verifyFormat("var regex = /\\w/;"); 1061 verifyFormat("var regex = /\\W/;"); 1062 verifyFormat("var regex = /a(a)\\1/;"); 1063 verifyFormat("var regex = /\\0/;"); 1064 verifyFormat("var regex = /\\\\/g;"); 1065 verifyFormat("var regex = /\\a\\\\/g;"); 1066 verifyFormat("var regex = /\a\\//g;"); 1067 verifyFormat("var regex = /a\\//;\n" 1068 "var x = 0;"); 1069 verifyFormat("var regex = /'/g;", "var regex = /'/g ;"); 1070 verifyFormat("var regex = /'/g; //'", "var regex = /'/g ; //'"); 1071 verifyFormat("var regex = /\\/*/;\n" 1072 "var x = 0;", 1073 "var regex = /\\/*/;\n" 1074 "var x=0;"); 1075 verifyFormat("var x = /a\\//;", "var x = /a\\// \n;"); 1076 verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16)); 1077 verifyFormat("var regex =\n" 1078 " /\"/;", 1079 getGoogleJSStyleWithColumns(15)); 1080 verifyFormat("var regex = //\n" 1081 " /a/;"); 1082 verifyFormat("var regexs = [\n" 1083 " /d/, //\n" 1084 " /aa/, //\n" 1085 "];"); 1086 } 1087 1088 TEST_F(FormatTestJS, RegexLiteralModifiers) { 1089 verifyFormat("var regex = /abc/g;"); 1090 verifyFormat("var regex = /abc/i;"); 1091 verifyFormat("var regex = /abc/m;"); 1092 verifyFormat("var regex = /abc/y;"); 1093 } 1094 1095 TEST_F(FormatTestJS, RegexLiteralLength) { 1096 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 1097 getGoogleJSStyleWithColumns(60)); 1098 verifyFormat("var regex =\n" 1099 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 1100 getGoogleJSStyleWithColumns(60)); 1101 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 1102 getGoogleJSStyleWithColumns(50)); 1103 } 1104 1105 TEST_F(FormatTestJS, RegexLiteralExamples) { 1106 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 1107 } 1108 1109 TEST_F(FormatTestJS, IgnoresMpegTS) { 1110 std::string MpegTS(200, ' '); 1111 MpegTS.replace(0, strlen("nearlyLooks + like + ts + code; "), 1112 "nearlyLooks + like + ts + code; "); 1113 MpegTS[0] = 0x47; 1114 MpegTS[188] = 0x47; 1115 verifyFormat(MpegTS, MpegTS); 1116 } 1117 1118 TEST_F(FormatTestJS, TypeAnnotations) { 1119 verifyFormat("var x: string;"); 1120 verifyFormat("var x: {a: string; b: number;} = {};"); 1121 verifyFormat("function x(): string {\n return 'x';\n}"); 1122 verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}"); 1123 verifyFormat("function x(y: string): string {\n return 'x';\n}"); 1124 verifyFormat("for (var y: string in x) {\n x();\n}"); 1125 verifyFormat("for (var y: string of x) {\n x();\n}"); 1126 verifyFormat("function x(y: {a?: number;} = {}): number {\n" 1127 " return 12;\n" 1128 "}"); 1129 verifyFormat("((a: string, b: number): string => a + b);"); 1130 verifyFormat("var x: (y: number) => string;"); 1131 verifyFormat("var x: P<string, (a: number) => string>;"); 1132 verifyFormat("var x = {\n" 1133 " y: function(): z {\n" 1134 " return 1;\n" 1135 " }\n" 1136 "};"); 1137 verifyFormat("var x = {\n" 1138 " y: function(): {a: number} {\n" 1139 " return 1;\n" 1140 " }\n" 1141 "};"); 1142 verifyFormat("function someFunc(args: string[]):\n" 1143 " {longReturnValue: string[]} {}", 1144 getGoogleJSStyleWithColumns(60)); 1145 verifyFormat( 1146 "var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])\n" 1147 " .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 1148 } 1149 1150 TEST_F(FormatTestJS, UnionIntersectionTypes) { 1151 verifyFormat("let x: A|B = A | B;"); 1152 verifyFormat("let x: A&B|C = A & B;"); 1153 verifyFormat("let x: Foo<A|B> = new Foo<A|B>();"); 1154 verifyFormat("function(x: A|B): C&D {}"); 1155 verifyFormat("function(x: A|B = A | B): C&D {}"); 1156 verifyFormat("function x(path: number|string) {}"); 1157 verifyFormat("function x(): string|number {}"); 1158 verifyFormat("type Foo = Bar|Baz;"); 1159 verifyFormat("type Foo = Bar<X>|Baz;"); 1160 verifyFormat("type Foo = (Bar<X>|Baz);"); 1161 verifyFormat("let x: Bar|Baz;"); 1162 verifyFormat("let x: Bar<X>|Baz;"); 1163 verifyFormat("let x: (Foo|Bar)[];"); 1164 } 1165 1166 TEST_F(FormatTestJS, ClassDeclarations) { 1167 verifyFormat("class C {\n x: string = 12;\n}"); 1168 verifyFormat("class C {\n x(): string => 12;\n}"); 1169 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); 1170 verifyFormat("class C {\n private x: string = 12;\n}"); 1171 verifyFormat("class C {\n private static x: string = 12;\n}"); 1172 verifyFormat("class C {\n static x(): string {\n return 'asd';\n }\n}"); 1173 verifyFormat("class C extends P implements I {}"); 1174 verifyFormat("class C extends p.P implements i.I {}"); 1175 verifyFormat( 1176 "x(class {\n" 1177 " a(): A {}\n" 1178 "});"); 1179 verifyFormat("class Test {\n" 1180 " aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n" 1181 " aaaaaaaaaaaaaaaaaaaaaa {}\n" 1182 "}"); 1183 verifyFormat("foo = class Name {\n" 1184 " constructor() {}\n" 1185 "};"); 1186 verifyFormat("foo = class {\n" 1187 " constructor() {}\n" 1188 "};"); 1189 verifyFormat("class C {\n" 1190 " x: {y: Z;} = {};\n" 1191 " private y: {y: Z;} = {};\n" 1192 "}"); 1193 1194 // ':' is not a type declaration here. 1195 verifyFormat("class X {\n" 1196 " subs = {\n" 1197 " 'b': {\n" 1198 " 'c': 1,\n" 1199 " },\n" 1200 " };\n" 1201 "}"); 1202 verifyFormat("@Component({\n" 1203 " moduleId: module.id,\n" 1204 "})\n" 1205 "class SessionListComponent implements OnDestroy, OnInit {\n" 1206 "}"); 1207 } 1208 1209 TEST_F(FormatTestJS, InterfaceDeclarations) { 1210 verifyFormat("interface I {\n" 1211 " x: string;\n" 1212 " enum: string[];\n" 1213 " enum?: string[];\n" 1214 "}\n" 1215 "var y;"); 1216 // Ensure that state is reset after parsing the interface. 1217 verifyFormat("interface a {}\n" 1218 "export function b() {}\n" 1219 "var x;"); 1220 1221 // Arrays of object type literals. 1222 verifyFormat("interface I {\n" 1223 " o: {}[];\n" 1224 "}"); 1225 } 1226 1227 TEST_F(FormatTestJS, EnumDeclarations) { 1228 verifyFormat("enum Foo {\n" 1229 " A = 1,\n" 1230 " B\n" 1231 "}"); 1232 verifyFormat("export /* somecomment*/ enum Foo {\n" 1233 " A = 1,\n" 1234 " B\n" 1235 "}"); 1236 verifyFormat("enum Foo {\n" 1237 " A = 1, // comment\n" 1238 " B\n" 1239 "}\n" 1240 "var x = 1;"); 1241 } 1242 1243 TEST_F(FormatTestJS, MetadataAnnotations) { 1244 verifyFormat("@A\nclass C {\n}"); 1245 verifyFormat("@A({arg: 'value'})\nclass C {\n}"); 1246 verifyFormat("@A\n@B\nclass C {\n}"); 1247 verifyFormat("class C {\n @A x: string;\n}"); 1248 verifyFormat("class C {\n" 1249 " @A\n" 1250 " private x(): string {\n" 1251 " return 'y';\n" 1252 " }\n" 1253 "}"); 1254 verifyFormat("class C {\n" 1255 " private x(@A x: string) {}\n" 1256 "}"); 1257 verifyFormat("class X {}\n" 1258 "class Y {}"); 1259 verifyFormat("class X {\n" 1260 " @property() private isReply = false;\n" 1261 "}\n"); 1262 } 1263 1264 TEST_F(FormatTestJS, TypeAliases) { 1265 verifyFormat("type X = number;\n" 1266 "class C {}"); 1267 verifyFormat("type X<Y> = Z<Y>;"); 1268 verifyFormat("type X = {\n" 1269 " y: number\n" 1270 "};\n" 1271 "class C {}"); 1272 } 1273 1274 TEST_F(FormatTestJS, TypeInterfaceLineWrapping) { 1275 const FormatStyle &Style = getGoogleJSStyleWithColumns(20); 1276 verifyFormat("type LongTypeIsReallyUnreasonablyLong =\n" 1277 " string;\n", 1278 "type LongTypeIsReallyUnreasonablyLong = string;\n", 1279 Style); 1280 verifyFormat( 1281 "interface AbstractStrategyFactoryProvider {\n" 1282 " a: number\n" 1283 "}\n", 1284 "interface AbstractStrategyFactoryProvider { a: number }\n", 1285 Style); 1286 } 1287 1288 TEST_F(FormatTestJS, Modules) { 1289 verifyFormat("import SomeThing from 'some/module.js';"); 1290 verifyFormat("import {X, Y} from 'some/module.js';"); 1291 verifyFormat("import a, {X, Y} from 'some/module.js';"); 1292 verifyFormat("import {X, Y,} from 'some/module.js';"); 1293 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); 1294 // Ensure Automatic Semicolon Insertion does not break on "as\n". 1295 verifyFormat("import {X as myX} from 'm';", "import {X as\n" 1296 " myX} from 'm';"); 1297 verifyFormat("import * as lib from 'some/module.js';"); 1298 verifyFormat("var x = {import: 1};\nx.import = 2;"); 1299 1300 verifyFormat("export function fn() {\n" 1301 " return 'fn';\n" 1302 "}"); 1303 verifyFormat("export function A() {}\n" 1304 "export default function B() {}\n" 1305 "export function C() {}"); 1306 verifyFormat("export default () => {\n" 1307 " let x = 1;\n" 1308 " return x;\n" 1309 "}"); 1310 verifyFormat("export const x = 12;"); 1311 verifyFormat("export default class X {}"); 1312 verifyFormat("export {X, Y} from 'some/module.js';"); 1313 verifyFormat("export {X, Y,} from 'some/module.js';"); 1314 verifyFormat("export {SomeVeryLongExport as X, " 1315 "SomeOtherVeryLongExport as Y} from 'some/module.js';"); 1316 // export without 'from' is wrapped. 1317 verifyFormat("export let someRatherLongVariableName =\n" 1318 " someSurprisinglyLongVariable + someOtherRatherLongVar;"); 1319 // ... but not if from is just an identifier. 1320 verifyFormat("export {\n" 1321 " from as from,\n" 1322 " someSurprisinglyLongVariable as\n" 1323 " from\n" 1324 "};", 1325 getGoogleJSStyleWithColumns(20)); 1326 verifyFormat("export class C {\n" 1327 " x: number;\n" 1328 " y: string;\n" 1329 "}"); 1330 verifyFormat("export class X { y: number; }"); 1331 verifyFormat("export abstract class X { y: number; }"); 1332 verifyFormat("export default class X { y: number }"); 1333 verifyFormat("export default function() {\n return 1;\n}"); 1334 verifyFormat("export var x = 12;"); 1335 verifyFormat("class C {}\n" 1336 "export function f() {}\n" 1337 "var v;"); 1338 verifyFormat("export var x: number = 12;"); 1339 verifyFormat("export const y = {\n" 1340 " a: 1,\n" 1341 " b: 2\n" 1342 "};"); 1343 verifyFormat("export enum Foo {\n" 1344 " BAR,\n" 1345 " // adsdasd\n" 1346 " BAZ\n" 1347 "}"); 1348 verifyFormat("export default [\n" 1349 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 1350 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 1351 "];"); 1352 verifyFormat("export default [];"); 1353 verifyFormat("export default () => {};"); 1354 verifyFormat("export interface Foo { foo: number; }\n" 1355 "export class Bar {\n" 1356 " blah(): string {\n" 1357 " return this.blah;\n" 1358 " };\n" 1359 "}"); 1360 } 1361 1362 TEST_F(FormatTestJS, ImportWrapping) { 1363 verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying," 1364 " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying" 1365 "} from 'some/module.js';"); 1366 FormatStyle Style = getGoogleJSStyleWithColumns(80); 1367 Style.JavaScriptWrapImports = true; 1368 verifyFormat("import {\n" 1369 " VeryLongImportsAreAnnoying,\n" 1370 " VeryLongImportsAreAnnoying,\n" 1371 " VeryLongImportsAreAnnoying,\n" 1372 "} from 'some/module.js';", 1373 Style); 1374 verifyFormat("import {\n" 1375 " A,\n" 1376 " A,\n" 1377 "} from 'some/module.js';", 1378 Style); 1379 verifyFormat("export {\n" 1380 " A,\n" 1381 " A,\n" 1382 "} from 'some/module.js';", 1383 Style); 1384 } 1385 1386 TEST_F(FormatTestJS, TemplateStrings) { 1387 // Keeps any whitespace/indentation within the template string. 1388 verifyFormat("var x = `hello\n" 1389 " ${name}\n" 1390 " !`;", 1391 "var x = `hello\n" 1392 " ${ name }\n" 1393 " !`;"); 1394 1395 verifyFormat("var x =\n" 1396 " `hello ${world}` >= some();", 1397 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. 1398 verifyFormat("var x = `hello ${world}` >= some();", 1399 getGoogleJSStyleWithColumns(35)); // Barely fits. 1400 verifyFormat("var x = `hellö ${wörld}` >= söme();", 1401 getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8. 1402 verifyFormat("var x = `hello\n" 1403 " ${world}` >=\n" 1404 " some();", 1405 "var x =\n" 1406 " `hello\n" 1407 " ${world}` >= some();", 1408 getGoogleJSStyleWithColumns(21)); // Barely doesn't fit. 1409 verifyFormat("var x = `hello\n" 1410 " ${world}` >= some();", 1411 "var x =\n" 1412 " `hello\n" 1413 " ${world}` >= some();", 1414 getGoogleJSStyleWithColumns(22)); // Barely fits. 1415 1416 verifyFormat("var x =\n" 1417 " `h`;", 1418 getGoogleJSStyleWithColumns(11)); 1419 verifyFormat("var x =\n `multi\n line`;", "var x = `multi\n line`;", 1420 getGoogleJSStyleWithColumns(13)); 1421 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1422 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);"); 1423 // Repro for an obscure width-miscounting issue with template strings. 1424 verifyFormat( 1425 "someLongVariable =\n" 1426 " " 1427 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;", 1428 "someLongVariable = " 1429 "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;"); 1430 1431 // Make sure template strings get a proper ColumnWidth assigned, even if they 1432 // are first token in line. 1433 verifyFormat( 1434 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1435 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); 1436 1437 // Two template strings. 1438 verifyFormat("var x = `hello` == `hello`;"); 1439 1440 // Comments in template strings. 1441 verifyFormat("var x = `//a`;\n" 1442 "var y;", 1443 "var x =\n `//a`;\n" 1444 "var y ;"); 1445 verifyFormat("var x = `/*a`;\n" 1446 "var y;", 1447 "var x =\n `/*a`;\n" 1448 "var y;"); 1449 // Unterminated string literals in a template string. 1450 verifyFormat("var x = `'`; // comment with matching quote '\n" 1451 "var y;"); 1452 verifyFormat("var x = `\"`; // comment with matching quote \"\n" 1453 "var y;"); 1454 verifyFormat("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa);", 1455 "it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa) ;", 1456 getGoogleJSStyleWithColumns(40)); 1457 // Backticks in a comment - not a template string. 1458 verifyFormat("var x = 1 // `/*a`;\n" 1459 " ;", 1460 "var x =\n 1 // `/*a`;\n" 1461 " ;"); 1462 verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */"); 1463 // Comment spans multiple template strings. 1464 verifyFormat("var x = `/*a`;\n" 1465 "var y = ` */ `;", 1466 "var x =\n `/*a`;\n" 1467 "var y =\n ` */ `;"); 1468 // Escaped backtick. 1469 verifyFormat("var x = ` \\` a`;\n" 1470 "var y;", 1471 "var x = ` \\` a`;\n" 1472 "var y;"); 1473 // Escaped dollar. 1474 verifyFormat("var x = ` \\${foo}`;\n"); 1475 1476 // The token stream can contain two string_literals in sequence, but that 1477 // doesn't mean that they are implicitly concatenated in JavaScript. 1478 verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;"); 1479 1480 // Ensure that scopes are appropriately set around evaluated expressions in 1481 // template strings. 1482 verifyFormat("var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa\n" 1483 " aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;", 1484 "var f = `aaaaaaaaaaaaa:${aaaaaaa. aaaaa} aaaaaaaa\n" 1485 " aaaaaaaaaaaaa:${ aaaaaaa. aaaaa} aaaaaaaa`;"); 1486 verifyFormat("var x = someFunction(`${})`) //\n" 1487 " .oooooooooooooooooon();"); 1488 verifyFormat("var x = someFunction(`${aaaa}${\n" 1489 " aaaaa( //\n" 1490 " aaaaa)\n" 1491 " })`);"); 1492 } 1493 1494 TEST_F(FormatTestJS, TemplateStringMultiLineExpression) { 1495 verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${\n" 1496 " aaaaa + //\n" 1497 " bbbb\n" 1498 " }`;", 1499 "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa + //\n" 1500 " bbbb}`;"); 1501 verifyFormat("var f = `\n" 1502 " aaaaaaaaaaaaaaaaaa: ${\n" 1503 " aaaaa + //\n" 1504 " bbbb\n" 1505 " }`;", 1506 "var f = `\n" 1507 " aaaaaaaaaaaaaaaaaa: ${ aaaaa + //\n" 1508 " bbbb }`;"); 1509 verifyFormat("var f = `\n" 1510 " aaaaaaaaaaaaaaaaaa: ${\n" 1511 " someFunction(\n" 1512 " aaaaa + //\n" 1513 " bbbb)\n" 1514 " }`;", 1515 "var f = `\n" 1516 " aaaaaaaaaaaaaaaaaa: ${someFunction (\n" 1517 " aaaaa + //\n" 1518 " bbbb)}`;"); 1519 1520 // It might be preferable to wrap before "someFunction". 1521 verifyFormat("var f = `\n" 1522 " aaaaaaaaaaaaaaaaaa: ${someFunction({\n" 1523 " aaaa: aaaaa,\n" 1524 " bbbb: bbbbb,\n" 1525 " })}`;", 1526 "var f = `\n" 1527 " aaaaaaaaaaaaaaaaaa: ${someFunction ({\n" 1528 " aaaa: aaaaa,\n" 1529 " bbbb: bbbbb,\n" 1530 " })}`;"); 1531 } 1532 1533 TEST_F(FormatTestJS, TemplateStringASI) { 1534 verifyFormat("var x = `hello${world}`;", "var x = `hello${\n" 1535 " world\n" 1536 "}`;"); 1537 } 1538 1539 TEST_F(FormatTestJS, NestedTemplateStrings) { 1540 verifyFormat( 1541 "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;"); 1542 verifyFormat("var x = `he${({text: 'll'}.text)}o`;"); 1543 1544 // Crashed at some point. 1545 verifyFormat("}"); 1546 } 1547 1548 TEST_F(FormatTestJS, TaggedTemplateStrings) { 1549 verifyFormat("var x = html`<ul>`;"); 1550 } 1551 1552 TEST_F(FormatTestJS, CastSyntax) { 1553 verifyFormat("var x = <type>foo;"); 1554 verifyFormat("var x = foo as type;"); 1555 verifyFormat("let x = (a + b) as\n" 1556 " LongTypeIsLong;", 1557 getGoogleJSStyleWithColumns(20)); 1558 verifyFormat("foo = <Bar[]>[\n" 1559 " 1, //\n" 1560 " 2\n" 1561 "];"); 1562 verifyFormat("var x = [{x: 1} as type];"); 1563 verifyFormat("x = x as [a, b];"); 1564 verifyFormat("x = x as {a: string};"); 1565 verifyFormat("x = x as (string);"); 1566 verifyFormat("x = x! as (string);"); 1567 verifyFormat("var x = something.someFunction() as\n" 1568 " something;", 1569 getGoogleJSStyleWithColumns(40)); 1570 } 1571 1572 TEST_F(FormatTestJS, TypeArguments) { 1573 verifyFormat("class X<Y> {}"); 1574 verifyFormat("new X<Y>();"); 1575 verifyFormat("foo<Y>(a);"); 1576 verifyFormat("var x: X<Y>[];"); 1577 verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); 1578 verifyFormat("function f(a: List<any> = null) {}"); 1579 verifyFormat("function f(): List<any> {}"); 1580 verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n" 1581 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}"); 1582 verifyFormat("function aaaaaaaaaa(\n" 1583 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n" 1584 " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n" 1585 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}"); 1586 } 1587 1588 TEST_F(FormatTestJS, UserDefinedTypeGuards) { 1589 verifyFormat( 1590 "function foo(check: Object):\n" 1591 " check is {foo: string, bar: string, baz: string, foobar: string} {\n" 1592 " return 'bar' in check;\n" 1593 "}\n"); 1594 } 1595 1596 TEST_F(FormatTestJS, OptionalTypes) { 1597 verifyFormat("function x(a?: b, c?, d?) {}"); 1598 verifyFormat("class X {\n" 1599 " y?: z;\n" 1600 " z?;\n" 1601 "}"); 1602 verifyFormat("interface X {\n" 1603 " y?(): z;\n" 1604 "}"); 1605 verifyFormat("constructor({aa}: {\n" 1606 " aa?: string,\n" 1607 " aaaaaaaa?: string,\n" 1608 " aaaaaaaaaaaaaaa?: boolean,\n" 1609 " aaaaaa?: List<string>\n" 1610 "}) {}"); 1611 } 1612 1613 TEST_F(FormatTestJS, IndexSignature) { 1614 verifyFormat("var x: {[k: string]: v};"); 1615 } 1616 1617 TEST_F(FormatTestJS, WrapAfterParen) { 1618 verifyFormat("xxxxxxxxxxx(\n" 1619 " aaa, aaa);", 1620 getGoogleJSStyleWithColumns(20)); 1621 verifyFormat("xxxxxxxxxxx(\n" 1622 " aaa, aaa, aaa,\n" 1623 " aaa, aaa, aaa);", 1624 getGoogleJSStyleWithColumns(20)); 1625 verifyFormat("xxxxxxxxxxx(\n" 1626 " aaaaaaaaaaaaaaaaaaaaaaaa,\n" 1627 " function(x) {\n" 1628 " y(); //\n" 1629 " });", 1630 getGoogleJSStyleWithColumns(40)); 1631 verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 1632 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 1633 } 1634 1635 TEST_F(FormatTestJS, JSDocAnnotations) { 1636 verifyFormat("/**\n" 1637 " * @export {this.is.a.long.path.to.a.Type}\n" 1638 " */", 1639 "/**\n" 1640 " * @export {this.is.a.long.path.to.a.Type}\n" 1641 " */", 1642 getGoogleJSStyleWithColumns(20)); 1643 verifyFormat("/**\n" 1644 " * @mods {this.is.a.long.path.to.a.Type}\n" 1645 " */", 1646 "/**\n" 1647 " * @mods {this.is.a.long.path.to.a.Type}\n" 1648 " */", 1649 getGoogleJSStyleWithColumns(20)); 1650 verifyFormat("/**\n" 1651 " * @param {this.is.a.long.path.to.a.Type}\n" 1652 " */", 1653 "/**\n" 1654 " * @param {this.is.a.long.path.to.a.Type}\n" 1655 " */", 1656 getGoogleJSStyleWithColumns(20)); 1657 verifyFormat("/**\n" 1658 " * @see http://very/very/long/url/is/long\n" 1659 " */", 1660 "/**\n" 1661 " * @see http://very/very/long/url/is/long\n" 1662 " */", 1663 getGoogleJSStyleWithColumns(20)); 1664 verifyFormat( 1665 "/**\n" 1666 " * @param This is a\n" 1667 " * long comment but\n" 1668 " * no type\n" 1669 " */", 1670 "/**\n" 1671 " * @param This is a long comment but no type\n" 1672 " */", 1673 getGoogleJSStyleWithColumns(20)); 1674 // Don't break @param line, but reindent it and reflow unrelated lines. 1675 verifyFormat("{\n" 1676 " /**\n" 1677 " * long long long\n" 1678 " * long\n" 1679 " * @param {this.is.a.long.path.to.a.Type} a\n" 1680 " * long long long\n" 1681 " * long long\n" 1682 " */\n" 1683 " function f(a) {}\n" 1684 "}", 1685 "{\n" 1686 "/**\n" 1687 " * long long long long\n" 1688 " * @param {this.is.a.long.path.to.a.Type} a\n" 1689 " * long long long long\n" 1690 " * long\n" 1691 " */\n" 1692 " function f(a) {}\n" 1693 "}", 1694 getGoogleJSStyleWithColumns(20)); 1695 } 1696 1697 TEST_F(FormatTestJS, RequoteStringsSingle) { 1698 verifyFormat("var x = 'foo';", "var x = \"foo\";"); 1699 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";"); 1700 verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";"); 1701 verifyFormat( 1702 "var x =\n" 1703 " 'foo\\'';", 1704 // Code below is 15 chars wide, doesn't fit into the line with the 1705 // \ escape added. 1706 "var x = \"foo'\";", getGoogleJSStyleWithColumns(15)); 1707 // Removes no-longer needed \ escape from ". 1708 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";"); 1709 // Code below fits into 15 chars *after* removing the \ escape. 1710 verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";", 1711 getGoogleJSStyleWithColumns(15)); 1712 verifyFormat("// clang-format off\n" 1713 "let x = \"double\";\n" 1714 "// clang-format on\n" 1715 "let x = 'single';\n", 1716 "// clang-format off\n" 1717 "let x = \"double\";\n" 1718 "// clang-format on\n" 1719 "let x = \"single\";\n"); 1720 } 1721 1722 TEST_F(FormatTestJS, RequoteAndIndent) { 1723 verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n" 1724 " 'double quoted string that needs wrapping');", 1725 "let x = someVeryLongFunctionThatGoesOnAndOn(" 1726 "\"double quoted string that needs wrapping\");"); 1727 1728 verifyFormat("let x =\n" 1729 " 'foo\\'oo';\n" 1730 "let x =\n" 1731 " 'foo\\'oo';", 1732 "let x=\"foo'oo\";\n" 1733 "let x=\"foo'oo\";", 1734 getGoogleJSStyleWithColumns(15)); 1735 } 1736 1737 TEST_F(FormatTestJS, RequoteStringsDouble) { 1738 FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1739 DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double; 1740 verifyFormat("var x = \"foo\";", DoubleQuotes); 1741 verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes); 1742 verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes); 1743 } 1744 1745 TEST_F(FormatTestJS, RequoteStringsLeave) { 1746 FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript); 1747 LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave; 1748 verifyFormat("var x = \"foo\";", LeaveQuotes); 1749 verifyFormat("var x = 'foo';", LeaveQuotes); 1750 } 1751 1752 TEST_F(FormatTestJS, SupportShebangLines) { 1753 verifyFormat("#!/usr/bin/env node\n" 1754 "var x = hello();", 1755 "#!/usr/bin/env node\n" 1756 "var x = hello();"); 1757 } 1758 1759 TEST_F(FormatTestJS, NonNullAssertionOperator) { 1760 verifyFormat("let x = foo!.bar();\n"); 1761 verifyFormat("let x = foo ? bar! : baz;\n"); 1762 verifyFormat("let x = !foo;\n"); 1763 verifyFormat("let x = foo[0]!;\n"); 1764 verifyFormat("let x = (foo)!;\n"); 1765 verifyFormat("let x = foo! - 1;\n"); 1766 verifyFormat("let x = {foo: 1}!;\n"); 1767 verifyFormat( 1768 "let x = hello.foo()!\n" 1769 " .foo()!\n" 1770 " .foo()!\n" 1771 " .foo()!;\n", 1772 getGoogleJSStyleWithColumns(20)); 1773 } 1774 1775 TEST_F(FormatTestJS, Conditional) { 1776 verifyFormat("y = x ? 1 : 2;"); 1777 verifyFormat("x ? 1 : 2;"); 1778 verifyFormat("class Foo {\n" 1779 " field = true ? 1 : 2;\n" 1780 " method(a = true ? 1 : 2) {}\n" 1781 "}"); 1782 } 1783 1784 TEST_F(FormatTestJS, ImportComments) { 1785 verifyFormat("import {x} from 'x'; // from some location", 1786 getGoogleJSStyleWithColumns(25)); 1787 verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10)); 1788 verifyFormat("/// <reference path=\"some/location\" />", getGoogleJSStyleWithColumns(10)); 1789 } 1790 1791 TEST_F(FormatTestJS, Exponentiation) { 1792 verifyFormat("squared = x ** 2;"); 1793 verifyFormat("squared **= 2;"); 1794 } 1795 1796 } // end namespace tooling 1797 } // end namespace clang 1798