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