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, FormatsFreestandingFunctions) { 162 verifyFormat("function outer1(a, b) {\n" 163 " function inner1(a, b) { return a; }\n" 164 " inner1(a, b);\n" 165 "}\n" 166 "function outer2(a, b) {\n" 167 " function inner2(a, b) { return a; }\n" 168 " inner2(a, b);\n" 169 "}"); 170 } 171 172 TEST_F(FormatTestJS, FunctionLiterals) { 173 verifyFormat("doFoo(function() {});"); 174 verifyFormat("doFoo(function() { return 1; });"); 175 verifyFormat("var func = function() { return 1; };"); 176 verifyFormat("return {\n" 177 " body: {\n" 178 " setAttribute: function(key, val) { this[key] = val; },\n" 179 " getAttribute: function(key) { return this[key]; },\n" 180 " style: {direction: ''}\n" 181 " }\n" 182 "};"); 183 EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };", 184 format("abc=xyz?function(){return 1;}:function(){return -1;};")); 185 186 verifyFormat("var closure = goog.bind(\n" 187 " function() { // comment\n" 188 " foo();\n" 189 " bar();\n" 190 " },\n" 191 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n" 192 " arg3IsReallyLongAndNeeedsLineBreaks);"); 193 verifyFormat("var closure = goog.bind(function() { // comment\n" 194 " foo();\n" 195 " bar();\n" 196 "}, this);"); 197 verifyFormat("return {\n" 198 " a: 'E',\n" 199 " b: function() {\n" 200 " return function() {\n" 201 " f(); //\n" 202 " };\n" 203 " }\n" 204 "};"); 205 206 verifyFormat("var x = {a: function() { return 1; }};", 207 getGoogleJSStyleWithColumns(38)); 208 verifyFormat("var x = {\n" 209 " a: function() { return 1; }\n" 210 "};", 211 getGoogleJSStyleWithColumns(37)); 212 213 verifyFormat("return {\n" 214 " a: function SomeFunction() {\n" 215 " // ...\n" 216 " return 1;\n" 217 " }\n" 218 "};"); 219 } 220 221 TEST_F(FormatTestJS, MultipleFunctionLiterals) { 222 verifyFormat("promise.then(\n" 223 " function success() {\n" 224 " doFoo();\n" 225 " doBar();\n" 226 " },\n" 227 " function error() {\n" 228 " doFoo();\n" 229 " doBaz();\n" 230 " },\n" 231 " []);\n"); 232 verifyFormat("promise.then(\n" 233 " function success() {\n" 234 " doFoo();\n" 235 " doBar();\n" 236 " },\n" 237 " [],\n" 238 " function error() {\n" 239 " doFoo();\n" 240 " doBaz();\n" 241 " });\n"); 242 // FIXME: Here, we should probably break right after the "(" for consistency. 243 verifyFormat("promise.then([],\n" 244 " function success() {\n" 245 " doFoo();\n" 246 " doBar();\n" 247 " },\n" 248 " function error() {\n" 249 " doFoo();\n" 250 " doBaz();\n" 251 " });\n"); 252 253 verifyFormat("getSomeLongPromise()\n" 254 " .then(function(value) { body(); })\n" 255 " .thenCatch(function(error) { body(); });"); 256 verifyFormat("getSomeLongPromise()\n" 257 " .then(function(value) {\n" 258 " body();\n" 259 " body();\n" 260 " })\n" 261 " .thenCatch(function(error) {\n" 262 " body();\n" 263 " body();\n" 264 " });"); 265 } 266 267 TEST_F(FormatTestJS, ReturnStatements) { 268 verifyFormat("function() { return [hello, world]; }"); 269 } 270 271 TEST_F(FormatTestJS, ClosureStyleComments) { 272 verifyFormat("var x = /** @type {foo} */ (bar);"); 273 } 274 275 TEST_F(FormatTestJS, TryCatch) { 276 verifyFormat("try {\n" 277 " f();\n" 278 "} catch (e) {\n" 279 " g();\n" 280 "} finally {\n" 281 " h();\n" 282 "}"); 283 284 // But, of course, "catch" is a perfectly fine function name in JavaScript. 285 verifyFormat("someObject.catch();"); 286 } 287 288 TEST_F(FormatTestJS, StringLiteralConcatenation) { 289 verifyFormat("var literal = 'hello ' +\n" 290 " 'world';"); 291 } 292 293 TEST_F(FormatTestJS, RegexLiteralClassification) { 294 // Regex literals. 295 verifyFormat("var regex = /abc/;"); 296 verifyFormat("f(/abc/);"); 297 verifyFormat("f(abc, /abc/);"); 298 verifyFormat("some_map[/abc/];"); 299 verifyFormat("var x = a ? /abc/ : /abc/;"); 300 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 301 verifyFormat("var x = !/abc/.test(y);"); 302 verifyFormat("var x = a && /abc/.test(y);"); 303 verifyFormat("var x = a || /abc/.test(y);"); 304 verifyFormat("var x = a + /abc/.search(y);"); 305 verifyFormat("var regexs = {/abc/, /abc/};"); 306 verifyFormat("return /abc/;"); 307 308 // Not regex literals. 309 verifyFormat("var a = a / 2 + b / 3;"); 310 } 311 312 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 313 verifyFormat("var regex = /a*/;"); 314 verifyFormat("var regex = /a+/;"); 315 verifyFormat("var regex = /a?/;"); 316 verifyFormat("var regex = /.a./;"); 317 verifyFormat("var regex = /a\\*/;"); 318 verifyFormat("var regex = /^a$/;"); 319 verifyFormat("var regex = /\\/a/;"); 320 verifyFormat("var regex = /(?:x)/;"); 321 verifyFormat("var regex = /x(?=y)/;"); 322 verifyFormat("var regex = /x(?!y)/;"); 323 verifyFormat("var regex = /x|y/;"); 324 verifyFormat("var regex = /a{2}/;"); 325 verifyFormat("var regex = /a{1,3}/;"); 326 verifyFormat("var regex = /[abc]/;"); 327 verifyFormat("var regex = /[^abc]/;"); 328 verifyFormat("var regex = /[\\b]/;"); 329 verifyFormat("var regex = /\\b/;"); 330 verifyFormat("var regex = /\\B/;"); 331 verifyFormat("var regex = /\\d/;"); 332 verifyFormat("var regex = /\\D/;"); 333 verifyFormat("var regex = /\\f/;"); 334 verifyFormat("var regex = /\\n/;"); 335 verifyFormat("var regex = /\\r/;"); 336 verifyFormat("var regex = /\\s/;"); 337 verifyFormat("var regex = /\\S/;"); 338 verifyFormat("var regex = /\\t/;"); 339 verifyFormat("var regex = /\\v/;"); 340 verifyFormat("var regex = /\\w/;"); 341 verifyFormat("var regex = /\\W/;"); 342 verifyFormat("var regex = /a(a)\\1/;"); 343 verifyFormat("var regex = /\\0/;"); 344 verifyFormat("var regex = /\\\\/g;"); 345 verifyFormat("var regex = /\\a\\\\/g;"); 346 verifyFormat("var regex = /\a\\//g;"); 347 verifyFormat("var regex = /a\\//;\n" 348 "var x = 0;"); 349 } 350 351 TEST_F(FormatTestJS, RegexLiteralModifiers) { 352 verifyFormat("var regex = /abc/g;"); 353 verifyFormat("var regex = /abc/i;"); 354 verifyFormat("var regex = /abc/m;"); 355 verifyFormat("var regex = /abc/y;"); 356 } 357 358 TEST_F(FormatTestJS, RegexLiteralLength) { 359 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 360 getGoogleJSStyleWithColumns(60)); 361 verifyFormat("var regex =\n" 362 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 363 getGoogleJSStyleWithColumns(60)); 364 } 365 366 TEST_F(FormatTestJS, RegexLiteralExamples) { 367 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 368 } 369 370 } // end namespace tooling 371 } // end namespace clang 372