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 } 140 141 TEST_F(FormatTestJS, SpacesInContainerLiterals) { 142 verifyFormat("var arr = [1, 2, 3];"); 143 verifyFormat("var obj = {a: 1, b: 2, c: 3};"); 144 145 verifyFormat("var object_literal_with_long_name = {\n" 146 " a: 'aaaaaaaaaaaaaaaaaa',\n" 147 " b: 'bbbbbbbbbbbbbbbbbb'\n" 148 "};"); 149 150 verifyFormat("var obj = {a: 1, b: 2, c: 3};", 151 getChromiumStyle(FormatStyle::LK_JavaScript)); 152 verifyFormat("someVariable = {'a': [{}]};"); 153 } 154 155 TEST_F(FormatTestJS, SingleQuoteStrings) { 156 verifyFormat("this.function('', true);"); 157 } 158 159 TEST_F(FormatTestJS, GoogScopes) { 160 verifyFormat("goog.scope(function() {\n" 161 "var x = a.b;\n" 162 "var y = c.d;\n" 163 "}); // goog.scope"); 164 } 165 166 TEST_F(FormatTestJS, GoogModules) { 167 verifyFormat("goog.module('this.is.really.absurdly.long');", 168 getGoogleJSStyleWithColumns(40)); 169 verifyFormat("goog.require('this.is.really.absurdly.long');", 170 getGoogleJSStyleWithColumns(40)); 171 verifyFormat("goog.provide('this.is.really.absurdly.long');", 172 getGoogleJSStyleWithColumns(40)); 173 verifyFormat("var long = goog.require('this.is.really.absurdly.long');", 174 getGoogleJSStyleWithColumns(40)); 175 176 // These should be wrapped normally. 177 verifyFormat( 178 "var MyLongClassName =\n" 179 " goog.module.get('my.long.module.name.followedBy.MyLongClassName');"); 180 } 181 182 TEST_F(FormatTestJS, FormatsFreestandingFunctions) { 183 verifyFormat("function outer1(a, b) {\n" 184 " function inner1(a, b) { return a; }\n" 185 " inner1(a, b);\n" 186 "}\n" 187 "function outer2(a, b) {\n" 188 " function inner2(a, b) { return a; }\n" 189 " inner2(a, b);\n" 190 "}"); 191 } 192 193 TEST_F(FormatTestJS, FunctionLiterals) { 194 verifyFormat("doFoo(function() {});"); 195 verifyFormat("doFoo(function() { return 1; });"); 196 verifyFormat("var func = function() {\n" 197 " return 1;\n" 198 "};"); 199 verifyFormat("return {\n" 200 " body: {\n" 201 " setAttribute: function(key, val) { this[key] = val; },\n" 202 " getAttribute: function(key) { return this[key]; },\n" 203 " style: {direction: ''}\n" 204 " }\n" 205 "};"); 206 // FIXME: The formatting here probably isn't ideal. 207 EXPECT_EQ("abc = xyz ?\n" 208 " function() {\n" 209 " return 1;\n" 210 " } :\n" 211 " function() {\n" 212 " return -1;\n" 213 "};", 214 format("abc=xyz?function(){return 1;}:function(){return -1;};")); 215 216 verifyFormat("var closure = goog.bind(\n" 217 " function() { // comment\n" 218 " foo();\n" 219 " bar();\n" 220 " },\n" 221 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n" 222 " arg3IsReallyLongAndNeeedsLineBreaks);"); 223 verifyFormat("var closure = goog.bind(function() { // comment\n" 224 " foo();\n" 225 " bar();\n" 226 "}, this);"); 227 verifyFormat("return {\n" 228 " a: 'E',\n" 229 " b: function() {\n" 230 " return function() {\n" 231 " f(); //\n" 232 " };\n" 233 " }\n" 234 "};"); 235 verifyFormat("{\n" 236 " var someVariable = function(x) {\n" 237 " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" 238 " };\n" 239 "}"); 240 241 verifyFormat("var x = {a: function() { return 1; }};", 242 getGoogleJSStyleWithColumns(38)); 243 verifyFormat("var x = {\n" 244 " a: function() { return 1; }\n" 245 "};", 246 getGoogleJSStyleWithColumns(37)); 247 248 verifyFormat("return {\n" 249 " a: function SomeFunction() {\n" 250 " // ...\n" 251 " return 1;\n" 252 " }\n" 253 "};"); 254 } 255 256 TEST_F(FormatTestJS, InliningFunctionLiterals) { 257 FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); 258 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 259 verifyFormat("var func = function() {\n" 260 " return 1;\n" 261 "};", 262 Style); 263 verifyFormat("var func = doSomething(function() { return 1; });", Style); 264 verifyFormat("var outer = function() {\n" 265 " var inner = function() { return 1; }\n" 266 "};", 267 Style); 268 verifyFormat("function outer1(a, b) {\n" 269 " function inner1(a, b) { return a; }\n" 270 "}", 271 Style); 272 273 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 274 verifyFormat("var func = function() { return 1; };", Style); 275 verifyFormat("var func = doSomething(function() { return 1; });", Style); 276 verifyFormat( 277 "var outer = function() { var inner = function() { return 1; } };", 278 Style); 279 verifyFormat("function outer1(a, b) {\n" 280 " function inner1(a, b) { return a; }\n" 281 "}", 282 Style); 283 284 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 285 verifyFormat("var func = function() {\n" 286 " return 1;\n" 287 "};", 288 Style); 289 verifyFormat("var func = doSomething(function() {\n" 290 " return 1;\n" 291 "});", 292 Style); 293 verifyFormat("var outer = function() {\n" 294 " var inner = function() {\n" 295 " return 1;\n" 296 " }\n" 297 "};", 298 Style); 299 verifyFormat("function outer1(a, b) {\n" 300 " function inner1(a, b) {\n" 301 " return a;\n" 302 " }\n" 303 "}", 304 Style); 305 } 306 307 TEST_F(FormatTestJS, MultipleFunctionLiterals) { 308 verifyFormat("promise.then(\n" 309 " function success() {\n" 310 " doFoo();\n" 311 " doBar();\n" 312 " },\n" 313 " function error() {\n" 314 " doFoo();\n" 315 " doBaz();\n" 316 " },\n" 317 " []);\n"); 318 verifyFormat("promise.then(\n" 319 " function success() {\n" 320 " doFoo();\n" 321 " doBar();\n" 322 " },\n" 323 " [],\n" 324 " function error() {\n" 325 " doFoo();\n" 326 " doBaz();\n" 327 " });\n"); 328 // FIXME: Here, we should probably break right after the "(" for consistency. 329 verifyFormat("promise.then([],\n" 330 " function success() {\n" 331 " doFoo();\n" 332 " doBar();\n" 333 " },\n" 334 " function error() {\n" 335 " doFoo();\n" 336 " doBaz();\n" 337 " });\n"); 338 339 verifyFormat("getSomeLongPromise()\n" 340 " .then(function(value) { body(); })\n" 341 " .thenCatch(function(error) { body(); });"); 342 verifyFormat("getSomeLongPromise()\n" 343 " .then(function(value) {\n" 344 " body();\n" 345 " body();\n" 346 " })\n" 347 " .thenCatch(function(error) {\n" 348 " body();\n" 349 " body();\n" 350 " });"); 351 } 352 353 TEST_F(FormatTestJS, ReturnStatements) { 354 verifyFormat("function() {\n" 355 " return [hello, world];\n" 356 "}"); 357 } 358 359 TEST_F(FormatTestJS, ClosureStyleComments) { 360 verifyFormat("var x = /** @type {foo} */ (bar);"); 361 } 362 363 TEST_F(FormatTestJS, TryCatch) { 364 verifyFormat("try {\n" 365 " f();\n" 366 "} catch (e) {\n" 367 " g();\n" 368 "} finally {\n" 369 " h();\n" 370 "}"); 371 372 // But, of course, "catch" is a perfectly fine function name in JavaScript. 373 verifyFormat("someObject.catch();"); 374 verifyFormat("someObject.new();"); 375 verifyFormat("someObject.delete();"); 376 } 377 378 TEST_F(FormatTestJS, StringLiteralConcatenation) { 379 verifyFormat("var literal = 'hello ' +\n" 380 " 'world';"); 381 } 382 383 TEST_F(FormatTestJS, RegexLiteralClassification) { 384 // Regex literals. 385 verifyFormat("var regex = /abc/;"); 386 verifyFormat("f(/abc/);"); 387 verifyFormat("f(abc, /abc/);"); 388 verifyFormat("some_map[/abc/];"); 389 verifyFormat("var x = a ? /abc/ : /abc/;"); 390 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 391 verifyFormat("var x = !/abc/.test(y);"); 392 verifyFormat("var x = a && /abc/.test(y);"); 393 verifyFormat("var x = a || /abc/.test(y);"); 394 verifyFormat("var x = a + /abc/.search(y);"); 395 verifyFormat("var regexs = {/abc/, /abc/};"); 396 verifyFormat("return /abc/;"); 397 398 // Not regex literals. 399 verifyFormat("var a = a / 2 + b / 3;"); 400 } 401 402 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 403 verifyFormat("var regex = /a*/;"); 404 verifyFormat("var regex = /a+/;"); 405 verifyFormat("var regex = /a?/;"); 406 verifyFormat("var regex = /.a./;"); 407 verifyFormat("var regex = /a\\*/;"); 408 verifyFormat("var regex = /^a$/;"); 409 verifyFormat("var regex = /\\/a/;"); 410 verifyFormat("var regex = /(?:x)/;"); 411 verifyFormat("var regex = /x(?=y)/;"); 412 verifyFormat("var regex = /x(?!y)/;"); 413 verifyFormat("var regex = /x|y/;"); 414 verifyFormat("var regex = /a{2}/;"); 415 verifyFormat("var regex = /a{1,3}/;"); 416 verifyFormat("var regex = /[abc]/;"); 417 verifyFormat("var regex = /[^abc]/;"); 418 verifyFormat("var regex = /[\\b]/;"); 419 verifyFormat("var regex = /\\b/;"); 420 verifyFormat("var regex = /\\B/;"); 421 verifyFormat("var regex = /\\d/;"); 422 verifyFormat("var regex = /\\D/;"); 423 verifyFormat("var regex = /\\f/;"); 424 verifyFormat("var regex = /\\n/;"); 425 verifyFormat("var regex = /\\r/;"); 426 verifyFormat("var regex = /\\s/;"); 427 verifyFormat("var regex = /\\S/;"); 428 verifyFormat("var regex = /\\t/;"); 429 verifyFormat("var regex = /\\v/;"); 430 verifyFormat("var regex = /\\w/;"); 431 verifyFormat("var regex = /\\W/;"); 432 verifyFormat("var regex = /a(a)\\1/;"); 433 verifyFormat("var regex = /\\0/;"); 434 verifyFormat("var regex = /\\\\/g;"); 435 verifyFormat("var regex = /\\a\\\\/g;"); 436 verifyFormat("var regex = /\a\\//g;"); 437 verifyFormat("var regex = /a\\//;\n" 438 "var x = 0;"); 439 EXPECT_EQ("var regex = /\\/*/;\n" 440 "var x = 0;", 441 format("var regex = /\\/*/;\n" 442 "var x=0;")); 443 } 444 445 TEST_F(FormatTestJS, RegexLiteralModifiers) { 446 verifyFormat("var regex = /abc/g;"); 447 verifyFormat("var regex = /abc/i;"); 448 verifyFormat("var regex = /abc/m;"); 449 verifyFormat("var regex = /abc/y;"); 450 } 451 452 TEST_F(FormatTestJS, RegexLiteralLength) { 453 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 454 getGoogleJSStyleWithColumns(60)); 455 verifyFormat("var regex =\n" 456 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 457 getGoogleJSStyleWithColumns(60)); 458 } 459 460 TEST_F(FormatTestJS, RegexLiteralExamples) { 461 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 462 } 463 464 } // end namespace tooling 465 } // end namespace clang 466