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