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