Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10 | #include "FormatTestUtils.h" |
| 11 | #include "clang/Format/Format.h" |
| 12 | #include "llvm/Support/Debug.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 15 | #define DEBUG_TYPE "format-test" |
| 16 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 34 | static std::string format( |
| 35 | llvm::StringRef Code, |
| 36 | const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 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)); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 82 | |
| 83 | verifyFormat("var b = a.map((x) => x + 1);"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 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;"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | TEST_F(FormatTestJS, ES6DestructuringAssignment) { |
| 96 | verifyFormat("var [a, b, c] = [1, 2, 3];"); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 97 | verifyFormat("var {a, b} = {a: 1, b: 2};"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 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" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 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" |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 133 | "};"); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 134 | verifyFormat("var obj = {\n" |
| 135 | " fooooooooo: function(x) {\n" |
| 136 | " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" |
| 137 | " }\n" |
| 138 | "};"); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 139 | // Simple object literal, as opposed to enum style below. |
| 140 | verifyFormat("var obj = {a: 123};"); |
| 141 | // Enum style top level assignment. |
| 142 | verifyFormat("X = {\n a: 123\n};"); |
| 143 | verifyFormat("X.Y = {\n a: 123\n};"); |
| 144 | verifyFormat("x = foo && {a: 123};"); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 147 | TEST_F(FormatTestJS, SpacesInContainerLiterals) { |
| 148 | verifyFormat("var arr = [1, 2, 3];"); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 149 | verifyFormat("f({a: 1, b: 2, c: 3});"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 150 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 151 | verifyFormat("var object_literal_with_long_name = {\n" |
| 152 | " a: 'aaaaaaaaaaaaaaaaaa',\n" |
| 153 | " b: 'bbbbbbbbbbbbbbbbbb'\n" |
| 154 | "};"); |
| 155 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 156 | verifyFormat("f({a: 1, b: 2, c: 3});", |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 157 | getChromiumStyle(FormatStyle::LK_JavaScript)); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 158 | verifyFormat("f({'a': [{}]});"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | TEST_F(FormatTestJS, SingleQuoteStrings) { |
| 162 | verifyFormat("this.function('', true);"); |
| 163 | } |
| 164 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 165 | TEST_F(FormatTestJS, GoogScopes) { |
| 166 | verifyFormat("goog.scope(function() {\n" |
| 167 | "var x = a.b;\n" |
| 168 | "var y = c.d;\n" |
| 169 | "}); // goog.scope"); |
| 170 | } |
| 171 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 172 | TEST_F(FormatTestJS, GoogModules) { |
| 173 | verifyFormat("goog.module('this.is.really.absurdly.long');", |
| 174 | getGoogleJSStyleWithColumns(40)); |
| 175 | verifyFormat("goog.require('this.is.really.absurdly.long');", |
| 176 | getGoogleJSStyleWithColumns(40)); |
| 177 | verifyFormat("goog.provide('this.is.really.absurdly.long');", |
| 178 | getGoogleJSStyleWithColumns(40)); |
| 179 | verifyFormat("var long = goog.require('this.is.really.absurdly.long');", |
| 180 | getGoogleJSStyleWithColumns(40)); |
| 181 | |
| 182 | // These should be wrapped normally. |
| 183 | verifyFormat( |
| 184 | "var MyLongClassName =\n" |
| 185 | " goog.module.get('my.long.module.name.followedBy.MyLongClassName');"); |
| 186 | } |
| 187 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 188 | TEST_F(FormatTestJS, FormatsFreestandingFunctions) { |
| 189 | verifyFormat("function outer1(a, b) {\n" |
| 190 | " function inner1(a, b) { return a; }\n" |
| 191 | " inner1(a, b);\n" |
| 192 | "}\n" |
| 193 | "function outer2(a, b) {\n" |
| 194 | " function inner2(a, b) { return a; }\n" |
| 195 | " inner2(a, b);\n" |
| 196 | "}"); |
| 197 | } |
| 198 | |
| 199 | TEST_F(FormatTestJS, FunctionLiterals) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 200 | verifyFormat("doFoo(function() {});"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 201 | verifyFormat("doFoo(function() { return 1; });"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 202 | verifyFormat("var func = function() {\n" |
| 203 | " return 1;\n" |
| 204 | "};"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 205 | verifyFormat("return {\n" |
| 206 | " body: {\n" |
| 207 | " setAttribute: function(key, val) { this[key] = val; },\n" |
| 208 | " getAttribute: function(key) { return this[key]; },\n" |
| 209 | " style: {direction: ''}\n" |
| 210 | " }\n" |
| 211 | "};"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 212 | EXPECT_EQ("abc = xyz ?\n" |
| 213 | " function() {\n" |
| 214 | " return 1;\n" |
| 215 | " } :\n" |
| 216 | " function() {\n" |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 217 | " return -1;\n" |
| 218 | " };", |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 219 | format("abc=xyz?function(){return 1;}:function(){return -1;};")); |
| 220 | |
| 221 | verifyFormat("var closure = goog.bind(\n" |
| 222 | " function() { // comment\n" |
| 223 | " foo();\n" |
| 224 | " bar();\n" |
| 225 | " },\n" |
| 226 | " this, arg1IsReallyLongAndNeeedsLineBreaks,\n" |
| 227 | " arg3IsReallyLongAndNeeedsLineBreaks);"); |
| 228 | verifyFormat("var closure = goog.bind(function() { // comment\n" |
| 229 | " foo();\n" |
| 230 | " bar();\n" |
| 231 | "}, this);"); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 232 | verifyFormat("return {\n" |
| 233 | " a: 'E',\n" |
| 234 | " b: function() {\n" |
| 235 | " return function() {\n" |
| 236 | " f(); //\n" |
| 237 | " };\n" |
| 238 | " }\n" |
| 239 | "};"); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 240 | verifyFormat("{\n" |
| 241 | " var someVariable = function(x) {\n" |
| 242 | " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" |
| 243 | " };\n" |
| 244 | "}"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 245 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 246 | verifyFormat("f({a: function() { return 1; }});", |
| 247 | getGoogleJSStyleWithColumns(33)); |
| 248 | verifyFormat("f({\n" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 249 | " a: function() { return 1; }\n" |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 250 | "});", |
| 251 | getGoogleJSStyleWithColumns(32)); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 252 | |
| 253 | verifyFormat("return {\n" |
| 254 | " a: function SomeFunction() {\n" |
| 255 | " // ...\n" |
| 256 | " return 1;\n" |
| 257 | " }\n" |
| 258 | "};"); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 259 | verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 260 | " .then(goog.bind(function(aaaaaaaaaaa) {\n" |
| 261 | " someFunction();\n" |
| 262 | " someFunction();\n" |
| 263 | " }, this), aaaaaaaaaaaaaaaaa);"); |
| 264 | |
| 265 | // FIXME: This is not ideal yet. |
| 266 | verifyFormat("someFunction(goog.bind(\n" |
| 267 | " function() {\n" |
| 268 | " doSomething();\n" |
| 269 | " doSomething();\n" |
| 270 | " },\n" |
| 271 | " this),\n" |
| 272 | " goog.bind(function() {\n" |
| 273 | " doSomething();\n" |
| 274 | " doSomething();\n" |
| 275 | " }, this));"); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 278 | TEST_F(FormatTestJS, InliningFunctionLiterals) { |
| 279 | FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript); |
| 280 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; |
| 281 | verifyFormat("var func = function() {\n" |
| 282 | " return 1;\n" |
| 283 | "};", |
| 284 | Style); |
| 285 | verifyFormat("var func = doSomething(function() { return 1; });", Style); |
| 286 | verifyFormat("var outer = function() {\n" |
| 287 | " var inner = function() { return 1; }\n" |
| 288 | "};", |
| 289 | Style); |
| 290 | verifyFormat("function outer1(a, b) {\n" |
| 291 | " function inner1(a, b) { return a; }\n" |
| 292 | "}", |
| 293 | Style); |
| 294 | |
| 295 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; |
| 296 | verifyFormat("var func = function() { return 1; };", Style); |
| 297 | verifyFormat("var func = doSomething(function() { return 1; });", Style); |
| 298 | verifyFormat( |
| 299 | "var outer = function() { var inner = function() { return 1; } };", |
| 300 | Style); |
| 301 | verifyFormat("function outer1(a, b) {\n" |
| 302 | " function inner1(a, b) { return a; }\n" |
| 303 | "}", |
| 304 | Style); |
| 305 | |
| 306 | Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; |
| 307 | verifyFormat("var func = function() {\n" |
| 308 | " return 1;\n" |
| 309 | "};", |
| 310 | Style); |
| 311 | verifyFormat("var func = doSomething(function() {\n" |
| 312 | " return 1;\n" |
| 313 | "});", |
| 314 | Style); |
| 315 | verifyFormat("var outer = function() {\n" |
| 316 | " var inner = function() {\n" |
| 317 | " return 1;\n" |
| 318 | " }\n" |
| 319 | "};", |
| 320 | Style); |
| 321 | verifyFormat("function outer1(a, b) {\n" |
| 322 | " function inner1(a, b) {\n" |
| 323 | " return a;\n" |
| 324 | " }\n" |
| 325 | "}", |
| 326 | Style); |
| 327 | } |
| 328 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 329 | TEST_F(FormatTestJS, MultipleFunctionLiterals) { |
| 330 | verifyFormat("promise.then(\n" |
| 331 | " function success() {\n" |
| 332 | " doFoo();\n" |
| 333 | " doBar();\n" |
| 334 | " },\n" |
| 335 | " function error() {\n" |
| 336 | " doFoo();\n" |
| 337 | " doBaz();\n" |
| 338 | " },\n" |
| 339 | " []);\n"); |
| 340 | verifyFormat("promise.then(\n" |
| 341 | " function success() {\n" |
| 342 | " doFoo();\n" |
| 343 | " doBar();\n" |
| 344 | " },\n" |
| 345 | " [],\n" |
| 346 | " function error() {\n" |
| 347 | " doFoo();\n" |
| 348 | " doBaz();\n" |
| 349 | " });\n"); |
| 350 | // FIXME: Here, we should probably break right after the "(" for consistency. |
| 351 | verifyFormat("promise.then([],\n" |
| 352 | " function success() {\n" |
| 353 | " doFoo();\n" |
| 354 | " doBar();\n" |
| 355 | " },\n" |
| 356 | " function error() {\n" |
| 357 | " doFoo();\n" |
| 358 | " doBaz();\n" |
| 359 | " });\n"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 360 | |
| 361 | verifyFormat("getSomeLongPromise()\n" |
| 362 | " .then(function(value) { body(); })\n" |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 363 | " .thenCatch(function(error) {\n" |
| 364 | " body();\n" |
| 365 | " body();\n" |
| 366 | " });"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 367 | verifyFormat("getSomeLongPromise()\n" |
| 368 | " .then(function(value) {\n" |
| 369 | " body();\n" |
| 370 | " body();\n" |
| 371 | " })\n" |
| 372 | " .thenCatch(function(error) {\n" |
| 373 | " body();\n" |
| 374 | " body();\n" |
| 375 | " });"); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 376 | |
| 377 | // FIXME: This is bad, but it used to be formatted correctly by accident. |
| 378 | verifyFormat("getSomeLongPromise().then(function(value) {\n" |
| 379 | " body();\n" |
| 380 | "}).thenCatch(function(error) { body(); });"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | TEST_F(FormatTestJS, ReturnStatements) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 384 | verifyFormat("function() {\n" |
| 385 | " return [hello, world];\n" |
| 386 | "}"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | TEST_F(FormatTestJS, ClosureStyleComments) { |
| 390 | verifyFormat("var x = /** @type {foo} */ (bar);"); |
| 391 | } |
| 392 | |
| 393 | TEST_F(FormatTestJS, TryCatch) { |
| 394 | verifyFormat("try {\n" |
| 395 | " f();\n" |
| 396 | "} catch (e) {\n" |
| 397 | " g();\n" |
| 398 | "} finally {\n" |
| 399 | " h();\n" |
| 400 | "}"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 401 | |
| 402 | // But, of course, "catch" is a perfectly fine function name in JavaScript. |
| 403 | verifyFormat("someObject.catch();"); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 404 | verifyFormat("someObject.new();"); |
| 405 | verifyFormat("someObject.delete();"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | TEST_F(FormatTestJS, StringLiteralConcatenation) { |
| 409 | verifyFormat("var literal = 'hello ' +\n" |
| 410 | " 'world';"); |
| 411 | } |
| 412 | |
| 413 | TEST_F(FormatTestJS, RegexLiteralClassification) { |
| 414 | // Regex literals. |
| 415 | verifyFormat("var regex = /abc/;"); |
| 416 | verifyFormat("f(/abc/);"); |
| 417 | verifyFormat("f(abc, /abc/);"); |
| 418 | verifyFormat("some_map[/abc/];"); |
| 419 | verifyFormat("var x = a ? /abc/ : /abc/;"); |
| 420 | verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}"); |
| 421 | verifyFormat("var x = !/abc/.test(y);"); |
| 422 | verifyFormat("var x = a && /abc/.test(y);"); |
| 423 | verifyFormat("var x = a || /abc/.test(y);"); |
| 424 | verifyFormat("var x = a + /abc/.search(y);"); |
| 425 | verifyFormat("var regexs = {/abc/, /abc/};"); |
| 426 | verifyFormat("return /abc/;"); |
| 427 | |
| 428 | // Not regex literals. |
| 429 | verifyFormat("var a = a / 2 + b / 3;"); |
| 430 | } |
| 431 | |
| 432 | TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { |
| 433 | verifyFormat("var regex = /a*/;"); |
| 434 | verifyFormat("var regex = /a+/;"); |
| 435 | verifyFormat("var regex = /a?/;"); |
| 436 | verifyFormat("var regex = /.a./;"); |
| 437 | verifyFormat("var regex = /a\\*/;"); |
| 438 | verifyFormat("var regex = /^a$/;"); |
| 439 | verifyFormat("var regex = /\\/a/;"); |
| 440 | verifyFormat("var regex = /(?:x)/;"); |
| 441 | verifyFormat("var regex = /x(?=y)/;"); |
| 442 | verifyFormat("var regex = /x(?!y)/;"); |
| 443 | verifyFormat("var regex = /x|y/;"); |
| 444 | verifyFormat("var regex = /a{2}/;"); |
| 445 | verifyFormat("var regex = /a{1,3}/;"); |
| 446 | verifyFormat("var regex = /[abc]/;"); |
| 447 | verifyFormat("var regex = /[^abc]/;"); |
| 448 | verifyFormat("var regex = /[\\b]/;"); |
| 449 | verifyFormat("var regex = /\\b/;"); |
| 450 | verifyFormat("var regex = /\\B/;"); |
| 451 | verifyFormat("var regex = /\\d/;"); |
| 452 | verifyFormat("var regex = /\\D/;"); |
| 453 | verifyFormat("var regex = /\\f/;"); |
| 454 | verifyFormat("var regex = /\\n/;"); |
| 455 | verifyFormat("var regex = /\\r/;"); |
| 456 | verifyFormat("var regex = /\\s/;"); |
| 457 | verifyFormat("var regex = /\\S/;"); |
| 458 | verifyFormat("var regex = /\\t/;"); |
| 459 | verifyFormat("var regex = /\\v/;"); |
| 460 | verifyFormat("var regex = /\\w/;"); |
| 461 | verifyFormat("var regex = /\\W/;"); |
| 462 | verifyFormat("var regex = /a(a)\\1/;"); |
| 463 | verifyFormat("var regex = /\\0/;"); |
| 464 | verifyFormat("var regex = /\\\\/g;"); |
| 465 | verifyFormat("var regex = /\\a\\\\/g;"); |
| 466 | verifyFormat("var regex = /\a\\//g;"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 467 | verifyFormat("var regex = /a\\//;\n" |
| 468 | "var x = 0;"); |
| 469 | EXPECT_EQ("var regex = /\\/*/;\n" |
| 470 | "var x = 0;", |
| 471 | format("var regex = /\\/*/;\n" |
| 472 | "var x=0;")); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | TEST_F(FormatTestJS, RegexLiteralModifiers) { |
| 476 | verifyFormat("var regex = /abc/g;"); |
| 477 | verifyFormat("var regex = /abc/i;"); |
| 478 | verifyFormat("var regex = /abc/m;"); |
| 479 | verifyFormat("var regex = /abc/y;"); |
| 480 | } |
| 481 | |
| 482 | TEST_F(FormatTestJS, RegexLiteralLength) { |
| 483 | verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", |
| 484 | getGoogleJSStyleWithColumns(60)); |
| 485 | verifyFormat("var regex =\n" |
| 486 | " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", |
| 487 | getGoogleJSStyleWithColumns(60)); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 488 | verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", |
| 489 | getGoogleJSStyleWithColumns(50)); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | TEST_F(FormatTestJS, RegexLiteralExamples) { |
| 493 | verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);"); |
| 494 | } |
| 495 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 496 | TEST_F(FormatTestJS, TypeAnnotations) { |
| 497 | verifyFormat("var x: string;"); |
| 498 | verifyFormat("function x(): string {\n return 'x';\n}"); |
| 499 | verifyFormat("function x(y: string): string {\n return 'x';\n}"); |
| 500 | verifyFormat("for (var y: string in x) {\n x();\n}"); |
| 501 | verifyFormat("((a: string, b: number): string => a + b);"); |
| 502 | verifyFormat("var x: (y: number) => string;"); |
| 503 | verifyFormat("var x: P<string, (a: number) => string>;"); |
| 504 | } |
| 505 | |
| 506 | TEST_F(FormatTestJS, ClassDeclarations) { |
| 507 | verifyFormat("class C {\n x: string = 12;\n}"); |
| 508 | verifyFormat("class C {\n x(): string => 12;\n}"); |
| 509 | verifyFormat("class C {\n ['x' + 2]: string = 12;\n}"); |
| 510 | verifyFormat("class C {\n private x: string = 12;\n}"); |
| 511 | verifyFormat("class C {\n private static x: string = 12;\n}"); |
| 512 | verifyFormat("class C {\n static x(): string { return 'asd'; }\n}"); |
| 513 | verifyFormat("class C extends P implements I {}"); |
| 514 | } |
| 515 | |
| 516 | TEST_F(FormatTestJS, MetadataAnnotations) { |
| 517 | verifyFormat("@A\nclass C {\n}"); |
| 518 | verifyFormat("@A({arg: 'value'})\nclass C {\n}"); |
| 519 | verifyFormat("@A\n@B\nclass C {\n}"); |
| 520 | verifyFormat("class C {\n @A x: string;\n}"); |
| 521 | verifyFormat("class C {\n" |
| 522 | " @A\n" |
| 523 | " private x(): string {\n" |
| 524 | " return 'y';\n" |
| 525 | " }\n" |
| 526 | "}"); |
| 527 | verifyFormat("class X {}\n" |
| 528 | "class Y {}"); |
| 529 | } |
| 530 | |
| 531 | TEST_F(FormatTestJS, Modules) { |
| 532 | verifyFormat("import SomeThing from 'some/module.js';"); |
| 533 | verifyFormat("import {X, Y} from 'some/module.js';"); |
| 534 | verifyFormat("import {\n" |
| 535 | " VeryLongImportsAreAnnoying,\n" |
| 536 | " VeryLongImportsAreAnnoying,\n" |
| 537 | " VeryLongImportsAreAnnoying,\n" |
| 538 | " VeryLongImportsAreAnnoying\n" |
| 539 | "} from 'some/module.js';"); |
| 540 | verifyFormat("import {\n" |
| 541 | " X,\n" |
| 542 | " Y,\n" |
| 543 | "} from 'some/module.js';"); |
| 544 | verifyFormat("import {\n" |
| 545 | " X,\n" |
| 546 | " Y,\n" |
| 547 | "} from 'some/long/module.js';", |
| 548 | getGoogleJSStyleWithColumns(20)); |
| 549 | verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';"); |
| 550 | verifyFormat("import * as lib from 'some/module.js';"); |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 551 | verifyFormat("var x = {import: 1};\nx.import = 2;"); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 552 | |
| 553 | verifyFormat("export function fn() {\n" |
| 554 | " return 'fn';\n" |
| 555 | "}"); |
| 556 | verifyFormat("export const x = 12;"); |
| 557 | verifyFormat("export default class X {}"); |
| 558 | verifyFormat("export {X, Y} from 'some/module.js';"); |
| 559 | verifyFormat("export {\n" |
| 560 | " X,\n" |
| 561 | " Y,\n" |
| 562 | "} from 'some/module.js';"); |
| 563 | verifyFormat("export class C {\n" |
| 564 | " x: number;\n" |
| 565 | " y: string;\n" |
| 566 | "}"); |
| 567 | verifyFormat("export class X { y: number; }"); |
| 568 | verifyFormat("export default class X { y: number }"); |
| 569 | verifyFormat("export default function() {\n return 1;\n}"); |
| 570 | verifyFormat("export var x = 12;"); |
| 571 | verifyFormat("export var x: number = 12;"); |
| 572 | verifyFormat("export const y = {\n" |
| 573 | " a: 1,\n" |
| 574 | " b: 2\n" |
| 575 | "};"); |
| 576 | } |
| 577 | |
| 578 | TEST_F(FormatTestJS, TemplateStrings) { |
| 579 | // Keeps any whitespace/indentation within the template string. |
| 580 | EXPECT_EQ("var x = `hello\n" |
| 581 | " ${ name }\n" |
| 582 | " !`;", |
| 583 | format("var x = `hello\n" |
| 584 | " ${ name }\n" |
| 585 | " !`;")); |
| 586 | |
| 587 | // FIXME: +1 / -1 offsets are to work around clang-format miscalculating |
| 588 | // widths for unknown tokens that are not whitespace (e.g. '`'). Remove when |
| 589 | // the code is corrected. |
| 590 | |
| 591 | verifyFormat("var x =\n" |
| 592 | " `hello ${world}` >= some();", |
| 593 | getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. |
| 594 | verifyFormat("var x = `hello ${world}` >= some();", |
| 595 | getGoogleJSStyleWithColumns(35 + 1)); // Barely fits. |
| 596 | EXPECT_EQ("var x = `hello\n" |
| 597 | " ${world}` >=\n" |
| 598 | " some();", |
| 599 | format("var x =\n" |
| 600 | " `hello\n" |
| 601 | " ${world}` >= some();", |
| 602 | getGoogleJSStyleWithColumns(21))); // Barely doesn't fit. |
| 603 | EXPECT_EQ("var x = `hello\n" |
| 604 | " ${world}` >= some();", |
| 605 | format("var x =\n" |
| 606 | " `hello\n" |
| 607 | " ${world}` >= some();", |
| 608 | getGoogleJSStyleWithColumns(22))); // Barely fits. |
| 609 | |
| 610 | verifyFormat("var x =\n `h`;", getGoogleJSStyleWithColumns(13 - 1)); |
| 611 | EXPECT_EQ( |
| 612 | "var x =\n `multi\n line`;", |
| 613 | format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(14 - 1))); |
| 614 | |
| 615 | // Two template strings. |
| 616 | verifyFormat("var x = `hello` == `hello`;"); |
| 617 | } |
| 618 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 619 | TEST_F(FormatTestJS, CastSyntax) { |
| 620 | verifyFormat("var x = <type>foo;"); |
| 621 | } |
| 622 | |
| 623 | TEST_F(FormatTestJS, TypeArguments) { |
| 624 | verifyFormat("class X<Y> {}"); |
| 625 | verifyFormat("new X<Y>();"); |
| 626 | verifyFormat("foo<Y>(a);"); |
| 627 | verifyFormat("var x: X<Y>[];"); |
| 628 | verifyFormat("class C extends D<E> implements F<G>, H<I> {}"); |
| 629 | } |
| 630 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 631 | } // end namespace tooling |
| 632 | } // end namespace clang |