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 } 85 86 TEST_F(FormatTestJS, UnderstandsAmpAmp) { 87 verifyFormat("e && e.SomeFunction();"); 88 } 89 90 TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) { 91 verifyFormat("not.and.or.not_eq = 1;"); 92 } 93 94 TEST_F(FormatTestJS, ES6DestructuringAssignment) { 95 verifyFormat("var [a, b, c] = [1, 2, 3];"); 96 verifyFormat("var {a, b} = {a: 1, b: 2};"); 97 } 98 99 TEST_F(FormatTestJS, ContainerLiterals) { 100 verifyFormat("return {\n" 101 " link: function() {\n" 102 " f(); //\n" 103 " }\n" 104 "};"); 105 verifyFormat("return {\n" 106 " a: a,\n" 107 " link: function() {\n" 108 " f(); //\n" 109 " }\n" 110 "};"); 111 verifyFormat("return {\n" 112 " a: a,\n" 113 " link:\n" 114 " function() {\n" 115 " f(); //\n" 116 " },\n" 117 " link:\n" 118 " function() {\n" 119 " f(); //\n" 120 " }\n" 121 "};"); 122 verifyFormat("var stuff = {\n" 123 " // comment for update\n" 124 " update: false,\n" 125 " // comment for modules\n" 126 " modules: false,\n" 127 " // comment for tasks\n" 128 " tasks: false\n" 129 "};"); 130 verifyFormat("return {\n" 131 " 'finish':\n" 132 " //\n" 133 " a\n" 134 "};"); 135 } 136 137 TEST_F(FormatTestJS, SpacesInContainerLiterals) { 138 verifyFormat("var arr = [1, 2, 3];"); 139 verifyFormat("var obj = {a: 1, b: 2, c: 3};"); 140 141 verifyFormat("var object_literal_with_long_name = {\n" 142 " a: 'aaaaaaaaaaaaaaaaaa',\n" 143 " b: 'bbbbbbbbbbbbbbbbbb'\n" 144 "};"); 145 146 verifyFormat("var obj = {a: 1, b: 2, c: 3};", 147 getChromiumStyle(FormatStyle::LK_JavaScript)); 148 verifyFormat("someVariable = {'a': [{}]};"); 149 } 150 151 TEST_F(FormatTestJS, SingleQuoteStrings) { 152 verifyFormat("this.function('', true);"); 153 } 154 155 TEST_F(FormatTestJS, GoogScopes) { 156 verifyFormat("goog.scope(function() {\n" 157 "var x = a.b;\n" 158 "var y = c.d;\n" 159 "}); // goog.scope"); 160 } 161 162 TEST_F(FormatTestJS, FormatsFreestandingFunctions) { 163 verifyFormat("function outer1(a, b) {\n" 164 " function inner1(a, b) { return a; }\n" 165 " inner1(a, b);\n" 166 "}\n" 167 "function outer2(a, b) {\n" 168 " function inner2(a, b) { return a; }\n" 169 " inner2(a, b);\n" 170 "}"); 171 } 172 173 TEST_F(FormatTestJS, FunctionLiterals) { 174 verifyFormat("doFoo(function() {});"); 175 verifyFormat("doFoo(function() { return 1; });"); 176 verifyFormat("var func = function() { return 1; };"); 177 verifyFormat("return {\n" 178 " body: {\n" 179 " setAttribute: function(key, val) { this[key] = val; },\n" 180 " getAttribute: function(key) { return this[key]; },\n" 181 " style: {direction: ''}\n" 182 " }\n" 183 "};"); 184 EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };", 185 format("abc=xyz?function(){return 1;}:function(){return -1;};")); 186 187 verifyFormat("var closure = goog.bind(\n" 188 " function() { // comment\n" 189 " foo();\n" 190 " bar();\n" 191 " },\n" 192 " this, arg1IsReallyLongAndNeeedsLineBreaks,\n" 193 " arg3IsReallyLongAndNeeedsLineBreaks);"); 194 verifyFormat("var closure = goog.bind(function() { // comment\n" 195 " foo();\n" 196 " bar();\n" 197 "}, this);"); 198 verifyFormat("return {\n" 199 " a: 'E',\n" 200 " b: function() {\n" 201 " return function() {\n" 202 " f(); //\n" 203 " };\n" 204 " }\n" 205 "};"); 206 207 verifyFormat("var x = {a: function() { return 1; }};", 208 getGoogleJSStyleWithColumns(38)); 209 verifyFormat("var x = {\n" 210 " a: function() { return 1; }\n" 211 "};", 212 getGoogleJSStyleWithColumns(37)); 213 214 verifyFormat("return {\n" 215 " a: function SomeFunction() {\n" 216 " // ...\n" 217 " return 1;\n" 218 " }\n" 219 "};"); 220 } 221 222 TEST_F(FormatTestJS, MultipleFunctionLiterals) { 223 verifyFormat("promise.then(\n" 224 " function success() {\n" 225 " doFoo();\n" 226 " doBar();\n" 227 " },\n" 228 " function error() {\n" 229 " doFoo();\n" 230 " doBaz();\n" 231 " },\n" 232 " []);\n"); 233 verifyFormat("promise.then(\n" 234 " function success() {\n" 235 " doFoo();\n" 236 " doBar();\n" 237 " },\n" 238 " [],\n" 239 " function error() {\n" 240 " doFoo();\n" 241 " doBaz();\n" 242 " });\n"); 243 // FIXME: Here, we should probably break right after the "(" for consistency. 244 verifyFormat("promise.then([],\n" 245 " function success() {\n" 246 " doFoo();\n" 247 " doBar();\n" 248 " },\n" 249 " function error() {\n" 250 " doFoo();\n" 251 " doBaz();\n" 252 " });\n"); 253 } 254 255 TEST_F(FormatTestJS, ReturnStatements) { 256 verifyFormat("function() { return [hello, world]; }"); 257 } 258 259 TEST_F(FormatTestJS, ClosureStyleComments) { 260 verifyFormat("var x = /** @type {foo} */ (bar);"); 261 } 262 263 TEST_F(FormatTestJS, TryCatch) { 264 verifyFormat("try {\n" 265 " f();\n" 266 "} catch (e) {\n" 267 " g();\n" 268 "} finally {\n" 269 " h();\n" 270 "}"); 271 272 // But, of course, "catch" is a perfectly fine function name in JavaScript. 273 verifyFormat("someObject.catch();"); 274 } 275 276 TEST_F(FormatTestJS, StringLiteralConcatenation) { 277 verifyFormat("var literal = 'hello ' +\n" 278 " 'world';"); 279 } 280 281 TEST_F(FormatTestJS, RegexLiteralClassification) { 282 // Regex literals. 283 verifyFormat("var regex = /abc/;"); 284 verifyFormat("f(/abc/);"); 285 verifyFormat("f(abc, /abc/);"); 286 verifyFormat("some_map[/abc/];"); 287 verifyFormat("var x = a ? /abc/ : /abc/;"); 288 verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); 289 verifyFormat("var x = !/abc/.test(y);"); 290 verifyFormat("var x = a && /abc/.test(y);"); 291 verifyFormat("var x = a || /abc/.test(y);"); 292 verifyFormat("var x = a + /abc/.search(y);"); 293 verifyFormat("var regexs = {/abc/, /abc/};"); 294 verifyFormat("return /abc/;"); 295 296 // Not regex literals. 297 verifyFormat("var a = a / 2 + b / 3;"); 298 } 299 300 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { 301 verifyFormat("var regex = /a*/;"); 302 verifyFormat("var regex = /a+/;"); 303 verifyFormat("var regex = /a?/;"); 304 verifyFormat("var regex = /.a./;"); 305 verifyFormat("var regex = /a\\*/;"); 306 verifyFormat("var regex = /^a$/;"); 307 verifyFormat("var regex = /\\/a/;"); 308 verifyFormat("var regex = /(?:x)/;"); 309 verifyFormat("var regex = /x(?=y)/;"); 310 verifyFormat("var regex = /x(?!y)/;"); 311 verifyFormat("var regex = /x|y/;"); 312 verifyFormat("var regex = /a{2}/;"); 313 verifyFormat("var regex = /a{1,3}/;"); 314 verifyFormat("var regex = /[abc]/;"); 315 verifyFormat("var regex = /[^abc]/;"); 316 verifyFormat("var regex = /[\\b]/;"); 317 verifyFormat("var regex = /\\b/;"); 318 verifyFormat("var regex = /\\B/;"); 319 verifyFormat("var regex = /\\d/;"); 320 verifyFormat("var regex = /\\D/;"); 321 verifyFormat("var regex = /\\f/;"); 322 verifyFormat("var regex = /\\n/;"); 323 verifyFormat("var regex = /\\r/;"); 324 verifyFormat("var regex = /\\s/;"); 325 verifyFormat("var regex = /\\S/;"); 326 verifyFormat("var regex = /\\t/;"); 327 verifyFormat("var regex = /\\v/;"); 328 verifyFormat("var regex = /\\w/;"); 329 verifyFormat("var regex = /\\W/;"); 330 verifyFormat("var regex = /a(a)\\1/;"); 331 verifyFormat("var regex = /\\0/;"); 332 verifyFormat("var regex = /\\\\/g;"); 333 verifyFormat("var regex = /\\a\\\\/g;"); 334 verifyFormat("var regex = /\a\\//g;"); 335 } 336 337 TEST_F(FormatTestJS, RegexLiteralModifiers) { 338 verifyFormat("var regex = /abc/g;"); 339 verifyFormat("var regex = /abc/i;"); 340 verifyFormat("var regex = /abc/m;"); 341 verifyFormat("var regex = /abc/y;"); 342 } 343 344 TEST_F(FormatTestJS, RegexLiteralLength) { 345 verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 346 getGoogleJSStyleWithColumns(60)); 347 verifyFormat("var regex =\n" 348 " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", 349 getGoogleJSStyleWithColumns(60)); 350 } 351 352 TEST_F(FormatTestJS, RegexLiteralExamples) { 353 verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); 354 } 355 356 } // end namespace tooling 357 } // end namespace clang 358