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