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 verifyFormat("someLooooooooongFunction(\n" 280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 282 " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 283 " // code\n" 284 " });"); 285 286 verifyFormat("f({a: function() { return 1; }});", 287 getGoogleJSStyleWithColumns(33)); 288 verifyFormat("f({\n" 289 " a: function() { return 1; }\n" 290 "});", 291 getGoogleJSStyleWithColumns(32)); 292 293 verifyFormat("return {\n" 294 " a: function SomeFunction() {\n" 295 " // ...\n" 296 " return 1;\n" 297 " }\n" 298 "};"); 299 verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 300 " .then(goog.bind(function(aaaaaaaaaaa) {\n" 301 " someFunction();\n" 302 " someFunction();\n" 303 " }, this), aaaaaaaaaaaaaaaaa);"); 304 305 // FIXME: This is not ideal yet. 306 verifyFormat("someFunction(goog.bind(\n" 307 " function() {\n" 308 " doSomething();\n" 309 " doSomething();\n" 310 " },\n" 311 " this),\n" 312 " goog.bind(function() {\n" 313 " doSomething();\n" 314 " doSomething();\n" 315 " }, this));"); 316 } 317 318 TEST_F(FormatTestJS, InliningFunctionLiterals) { 319 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 320 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 321 verifyFormat("var func = function() {\n" 322 " return 1;\n" 323 "};", 324 Style); 325 verifyFormat("var func = doSomething(function() { return 1; });", Style); 326 verifyFormat("var outer = function() {\n" 327 " var inner = function() { return 1; }\n" 328 "};", 329 Style); 330 verifyFormat("function outer1(a, b) {\n" 331 " function inner1(a, b) { return a; }\n" 332 "}", 333 Style); 334 335 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 336 verifyFormat("var func = function() { return 1; };", Style); 337 verifyFormat("var func = doSomething(function() { return 1; });", Style); 338 verifyFormat( 339 "var outer = function() { var inner = function() { return 1; } };", 340 Style); 341 verifyFormat("function outer1(a, b) {\n" 342 " function inner1(a, b) { return a; }\n" 343 "}", 344 Style); 345 346 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 347 verifyFormat("var func = function() {\n" 348 " return 1;\n" 349 "};", 350 Style); 351 verifyFormat("var func = doSomething(function() {\n" 352 " return 1;\n" 353 "});", 354 Style); 355 verifyFormat("var outer = function() {\n" 356 " var inner = function() {\n" 357 " return 1;\n" 358 " }\n" 359 "};", 360 Style); 361 verifyFormat("function outer1(a, b) {\n" 362 " function inner1(a, b) {\n" 363 " return a;\n" 364 " }\n" 365 "}", 366 Style); 367 } 368 369 TEST_F(FormatTestJS, MultipleFunctionLiterals) { 370 verifyFormat("promise.then(\n" 371 " function success() {\n" 372 " doFoo();\n" 373 " doBar();\n" 374 " },\n" 375 " function error() {\n" 376 " doFoo();\n" 377 " doBaz();\n" 378 " },\n" 379 " []);\n"); 380 verifyFormat("promise.then(\n" 381 " function success() {\n" 382 " doFoo();\n" 383 " doBar();\n" 384 " },\n" 385 " [],\n" 386 " function error() {\n" 387 " doFoo();\n" 388 " doBaz();\n" 389 " });\n"); 390 // FIXME: Here, we should probably break right after the "(" for consistency. 391 verifyFormat("promise.then([],\n" 392 " function success() {\n" 393 " doFoo();\n" 394 " doBar();\n" 395 " },\n" 396 " function error() {\n" 397 " doFoo();\n" 398 " doBaz();\n" 399 " });\n"); 400 401 verifyFormat("getSomeLongPromise()\n" 402 " .then(function(value) { body(); })\n" 403 " .thenCatch(function(error) {\n" 404 " body();\n" 405 " body();\n" 406 " });"); 407 verifyFormat("getSomeLongPromise()\n" 408 " .then(function(value) {\n" 409 " body();\n" 410 " body();\n" 411 " })\n" 412 " .thenCatch(function(error) {\n" 413 " body();\n" 414 " body();\n" 415 " });"); 416 417 verifyFormat("getSomeLongPromise()\n" 418 " .then(function(value) { body(); })\n" 419 " .thenCatch(function(error) { body(); });"); 420 } 421 422 TEST_F(FormatTestJS, ReturnStatements) { 423 verifyFormat("function() {\n" 424 " return [hello, world];\n" 425 "}"); 426 } 427 428 TEST_F(FormatTestJS, ClosureStyleComments) { 429 verifyFormat("var x = /** @type {foo} */ (bar);"); 430 } 431 432 TEST_F(FormatTestJS, TryCatch) { 433 verifyFormat("try {\n" 434 " f();\n" 435 "} catch (e) {\n" 436 " g();\n" 437 "} finally {\n" 438 " h();\n" 439 "}"); 440 441 // But, of course, "catch" is a perfectly fine function name in JavaScript. 442 verifyFormat("someObject.catch();"); 443 verifyFormat("someObject.new();"); 444 verifyFormat("someObject.delete();"); 445 } 446 447 TEST_F(FormatTestJS, StringLiteralConcatenation) { 448 verifyFormat("var literal = 'hello ' +\n" 449 " 'world';"); 450 } 451 452 TEST_F(FormatTestJS, RegexLiteralClassification) { 453 // Regex literals. 454 verifyFormat("var regex = /abc/;"); 455 verifyFormat("f(/abc/);"); 456 verifyFormat("f(abc, /abc/);"); 457 verifyFormat("some_map[/abc/];"); 458 verifyFormat("var x = a ? /abc/ : /abc/;"); 459 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 460 verifyFormat("var x = !/abc/.test(y);"); 461 verifyFormat("var x = a && /abc/.test(y);"); 462 verifyFormat("var x = a || /abc/.test(y);"); 463 verifyFormat("var x = a + /abc/.search(y);"); 464 verifyFormat("var regexs = {/abc/, /abc/};"); 465 verifyFormat("return /abc/;"); 466 467 // Not regex literals. 468 verifyFormat("var a = a / 2 + b / 3;"); 469 } 470 471 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 472 verifyFormat("var regex = /=/;"); 473 verifyFormat("var regex = /a*/;"); 474 verifyFormat("var regex = /a+/;"); 475 verifyFormat("var regex = /a?/;"); 476 verifyFormat("var regex = /.a./;"); 477 verifyFormat("var regex = /a\\*/;"); 478 verifyFormat("var regex = /^a$/;"); 479 verifyFormat("var regex = /\\/a/;"); 480 verifyFormat("var regex = /(?:x)/;"); 481 verifyFormat("var regex = /x(?=y)/;"); 482 verifyFormat("var regex = /x(?!y)/;"); 483 verifyFormat("var regex = /x|y/;"); 484 verifyFormat("var regex = /a{2}/;"); 485 verifyFormat("var regex = /a{1,3}/;"); 486 verifyFormat("var regex = /[abc]/;"); 487 verifyFormat("var regex = /[^abc]/;"); 488 verifyFormat("var regex = /[\\b]/;"); 489 verifyFormat("var regex = /\\b/;"); 490 verifyFormat("var regex = /\\B/;"); 491 verifyFormat("var regex = /\\d/;"); 492 verifyFormat("var regex = /\\D/;"); 493 verifyFormat("var regex = /\\f/;"); 494 verifyFormat("var regex = /\\n/;"); 495 verifyFormat("var regex = /\\r/;"); 496 verifyFormat("var regex = /\\s/;"); 497 verifyFormat("var regex = /\\S/;"); 498 verifyFormat("var regex = /\\t/;"); 499 verifyFormat("var regex = /\\v/;"); 500 verifyFormat("var regex = /\\w/;"); 501 verifyFormat("var regex = /\\W/;"); 502 verifyFormat("var regex = /a(a)\\1/;"); 503 verifyFormat("var regex = /\\0/;"); 504 verifyFormat("var regex = /\\\\/g;"); 505 verifyFormat("var regex = /\\a\\\\/g;"); 506 verifyFormat("var regex = /\a\\//g;"); 507 verifyFormat("var regex = /a\\//;\n" 508 "var x = 0;"); 509 EXPECT_EQ("var regex = /\\/*/;\n" 510 "var x = 0;", 511 format("var regex = /\\/*/;\n" 512 "var x=0;")); 513 } 514 515 TEST_F(FormatTestJS, RegexLiteralModifiers) { 516 verifyFormat("var regex = /abc/g;"); 517 verifyFormat("var regex = /abc/i;"); 518 verifyFormat("var regex = /abc/m;"); 519 verifyFormat("var regex = /abc/y;"); 520 } 521 522 TEST_F(FormatTestJS, RegexLiteralLength) { 523 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 524 getGoogleJSStyleWithColumns(60)); 525 verifyFormat("var regex =\n" 526 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 527 getGoogleJSStyleWithColumns(60)); 528 verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 529 getGoogleJSStyleWithColumns(50)); 530 } 531 532 TEST_F(FormatTestJS, RegexLiteralExamples) { 533 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 534 } 535 536 TEST_F(FormatTestJS, TypeAnnotations) { 537 verifyFormat("var x: string;"); 538 verifyFormat("function x(): string {\n return 'x';\n}"); 539 verifyFormat("function x(y: string): string {\n return 'x';\n}"); 540 verifyFormat("for (var y: string in x) {\n x();\n}"); 541 verifyFormat("((a: string, b: number): string => a + b);"); 542 verifyFormat("var x: (y: number) => string;"); 543 verifyFormat("var x: P<string, (a: number) => string>;"); 544 } 545 546 TEST_F(FormatTestJS, ClassDeclarations) { 547 verifyFormat("class C {\n x: string = 12;\n}"); 548 verifyFormat("class C {\n x(): string => 12;\n}"); 549 verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); 550 verifyFormat("class C {\n private x: string = 12;\n}"); 551 verifyFormat("class C {\n private static x: string = 12;\n}"); 552 verifyFormat("class C {\n static x(): string { return 'asd'; }\n}"); 553 verifyFormat("class C extends P implements I {}"); 554 verifyFormat("class C extends p.P implements i.I {}"); 555 } 556 557 TEST_F(FormatTestJS, InterfaceDeclarations) { 558 verifyFormat("interface I {\n" 559 " x: string;\n" 560 "}"); 561 } 562 563 TEST_F(FormatTestJS, MetadataAnnotations) { 564 verifyFormat("@A\nclass C {\n}"); 565 verifyFormat("@A({arg: 'value'})\nclass C {\n}"); 566 verifyFormat("@A\n@B\nclass C {\n}"); 567 verifyFormat("class C {\n @A x: string;\n}"); 568 verifyFormat("class C {\n" 569 " @A\n" 570 " private x(): string {\n" 571 " return 'y';\n" 572 " }\n" 573 "}"); 574 verifyFormat("class X {}\n" 575 "class Y {}"); 576 } 577 578 TEST_F(FormatTestJS, Modules) { 579 verifyFormat("import SomeThing from 'some/module.js';"); 580 verifyFormat("import {X, Y} from 'some/module.js';"); 581 verifyFormat("import {\n" 582 " VeryLongImportsAreAnnoying,\n" 583 " VeryLongImportsAreAnnoying,\n" 584 " VeryLongImportsAreAnnoying,\n" 585 " VeryLongImportsAreAnnoying\n" 586 "} from 'some/module.js';"); 587 verifyFormat("import {\n" 588 " X,\n" 589 " Y,\n" 590 "} from 'some/module.js';"); 591 verifyFormat("import {\n" 592 " X,\n" 593 " Y,\n" 594 "} from 'some/long/module.js';", 595 getGoogleJSStyleWithColumns(20)); 596 verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); 597 verifyFormat("import * as lib from 'some/module.js';"); 598 verifyFormat("var x = {import: 1};\nx.import = 2;"); 599 600 verifyFormat("export function fn() {\n" 601 " return 'fn';\n" 602 "}"); 603 verifyFormat("export const x = 12;"); 604 verifyFormat("export default class X {}"); 605 verifyFormat("export {X, Y} from 'some/module.js';"); 606 verifyFormat("export {\n" 607 " X,\n" 608 " Y,\n" 609 "} from 'some/module.js';"); 610 verifyFormat("export class C {\n" 611 " x: number;\n" 612 " y: string;\n" 613 "}"); 614 verifyFormat("export class X { y: number; }"); 615 verifyFormat("export default class X { y: number }"); 616 verifyFormat("export default function() {\n return 1;\n}"); 617 verifyFormat("export var x = 12;"); 618 verifyFormat("export var x: number = 12;"); 619 verifyFormat("export const y = {\n" 620 " a: 1,\n" 621 " b: 2\n" 622 "};"); 623 } 624 625 TEST_F(FormatTestJS, TemplateStrings) { 626 // Keeps any whitespace/indentation within the template string. 627 EXPECT_EQ("var x = `hello\n" 628 " ${ name }\n" 629 " !`;", 630 format("var x = `hello\n" 631 " ${ name }\n" 632 " !`;")); 633 634 // FIXME: +1 / -1 offsets are to work around clang-format miscalculating 635 // widths for unknown tokens that are not whitespace (e.g. '`'). Remove when 636 // the code is corrected. 637 638 verifyFormat("var x =\n" 639 " `hello ${world}` >= some();", 640 getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. 641 verifyFormat("var x = `hello ${world}` >= some();", 642 getGoogleJSStyleWithColumns(35 + 1)); // Barely fits. 643 EXPECT_EQ("var x = `hello\n" 644 " ${world}` >=\n" 645 " some();", 646 format("var x =\n" 647 " `hello\n" 648 " ${world}` >= some();", 649 getGoogleJSStyleWithColumns(21))); // Barely doesn't fit. 650 EXPECT_EQ("var x = `hello\n" 651 " ${world}` >= some();", 652 format("var x =\n" 653 " `hello\n" 654 " ${world}` >= some();", 655 getGoogleJSStyleWithColumns(22))); // Barely fits. 656 657 verifyFormat("var x =\n `h`;", getGoogleJSStyleWithColumns(13 - 1)); 658 EXPECT_EQ( 659 "var x =\n `multi\n line`;", 660 format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(14 - 1))); 661 662 // Make sure template strings get a proper ColumnWidth assigned, even if they 663 // are first token in line. 664 verifyFormat( 665 "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 666 " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); 667 668 // Two template strings. 669 verifyFormat("var x = `hello` == `hello`;"); 670 671 // Comments in template strings. 672 EXPECT_EQ("var x = `//a`;\n" 673 "var y;", 674 format("var x =\n `//a`;\n" 675 "var y ;")); 676 EXPECT_EQ("var x = `/*a`;\n" 677 "var y;", 678 format("var x =\n `/*a`;\n" 679 "var y;")); 680 // Backticks in a comment - not a template string. 681 EXPECT_EQ("var x = 1 // `/*a`;\n" 682 " ;", 683 format("var x =\n 1 // `/*a`;\n" 684 " ;")); 685 EXPECT_EQ("/* ` */ var x = 1; /* ` */", 686 format("/* ` */ var x\n= 1; /* ` */")); 687 // Comment spans multiple template strings. 688 EXPECT_EQ("var x = `/*a`;\n" 689 "var y = ` */ `;", 690 format("var x =\n `/*a`;\n" 691 "var y =\n ` */ `;")); 692 // Escaped backtick. 693 EXPECT_EQ("var x = ` \\` a`;\n" 694 "var y;", 695 format("var x = ` \\` a`;\n" 696 "var y;")); 697 } 698 699 TEST_F(FormatTestJS, CastSyntax) { 700 verifyFormat("var x = <type>foo;"); 701 } 702 703 TEST_F(FormatTestJS, TypeArguments) { 704 verifyFormat("class X<Y> {}"); 705 verifyFormat("new X<Y>();"); 706 verifyFormat("foo<Y>(a);"); 707 verifyFormat("var x: X<Y>[];"); 708 verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); 709 verifyFormat("function f(a: List<any> = null) {\n}"); 710 verifyFormat("function f(): List<any> {\n}"); 711 } 712 713 TEST_F(FormatTestJS, OptionalTypes) { 714 verifyFormat("function x(a?: b, c?, d?) {\n}"); 715 verifyFormat("class X {\n" 716 " y?: z;\n" 717 " z?;\n" 718 "}"); 719 verifyFormat("interface X {\n" 720 " y?(): z;\n" 721 "}"); 722 verifyFormat("x ? 1 : 2;"); 723 } 724 725 TEST_F(FormatTestJS, IndexSignature) { 726 verifyFormat("var x: {[k: string]: v};"); 727 } 728 729 } // end namespace tooling 730 } // end namespace clang 731