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