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