xref: /llvm-project/clang/unittests/Format/FormatTestJS.cpp (revision 0dd5291e698d03222365efec1d37da28ab91de22)
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(llvm::StringRef Code, const FormatStyle &Style) {
35     return format(Code, 0, Code.size(), Style);
36   }
37 
38   static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
39     FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
40     Style.ColumnLimit = ColumnLimit;
41     return Style;
42   }
43 
44   static void verifyFormat(
45       llvm::StringRef Code,
46       const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
47     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
48   }
49 };
50 
51 TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
52   verifyFormat("a == = b;");
53   verifyFormat("a != = b;");
54 
55   verifyFormat("a === b;");
56   verifyFormat("aaaaaaa ===\n    b;", getGoogleJSStyleWithColumns(10));
57   verifyFormat("a !== b;");
58   verifyFormat("aaaaaaa !==\n    b;", getGoogleJSStyleWithColumns(10));
59   verifyFormat("if (a + b + c +\n"
60                "        d !==\n"
61                "    e + f + g)\n"
62                "  q();",
63                getGoogleJSStyleWithColumns(20));
64 
65   verifyFormat("a >> >= b;");
66 
67   verifyFormat("a >>> b;");
68   verifyFormat("aaaaaaa >>>\n    b;", getGoogleJSStyleWithColumns(10));
69   verifyFormat("a >>>= b;");
70   verifyFormat("aaaaaaa >>>=\n    b;", getGoogleJSStyleWithColumns(10));
71   verifyFormat("if (a + b + c +\n"
72                "        d >>>\n"
73                "    e + f + g)\n"
74                "  q();",
75                getGoogleJSStyleWithColumns(20));
76   verifyFormat("var x = aaaaaaaaaa ?\n"
77                "            bbbbbb :\n"
78                "            ccc;",
79                getGoogleJSStyleWithColumns(20));
80 
81   verifyFormat("var b = a.map((x) => x + 1);");
82 }
83 
84 TEST_F(FormatTestJS, ES6DestructuringAssignment) {
85   verifyFormat("var [a, b, c] = [1, 2, 3];");
86   verifyFormat("var {a, b} = {a: 1, b: 2};");
87 }
88 
89 TEST_F(FormatTestJS, SpacesInContainerLiterals) {
90   verifyFormat("var arr = [1, 2, 3];");
91   verifyFormat("var obj = {a: 1, b: 2, c: 3};");
92 
93   verifyFormat("var obj = {a: 1, b: 2, c: 3};",
94                getChromiumStyle(FormatStyle::LK_JavaScript));
95   verifyFormat("someVariable = {'a': [{}]};");
96 }
97 
98 TEST_F(FormatTestJS, SingleQuoteStrings) {
99   verifyFormat("this.function('', true);");
100 }
101 
102 TEST_F(FormatTestJS, GoogScopes) {
103   verifyFormat("goog.scope(function() {\n"
104                "var x = a.b;\n"
105                "var y = c.d;\n"
106                "});  // goog.scope");
107 }
108 
109 TEST_F(FormatTestJS, Closures) {
110   verifyFormat("doFoo(function() { return 1; });");
111   verifyFormat("var func = function() { return 1; };");
112   verifyFormat("return {\n"
113                "  body: {\n"
114                "    setAttribute: function(key, val) { this[key] = val; },\n"
115                "    getAttribute: function(key) { return this[key]; },\n"
116                "    style: {direction: ''}\n"
117                "  }\n"
118                "};");
119 }
120 
121 TEST_F(FormatTestJS, ReturnStatements) {
122   verifyFormat("function() { return [hello, world]; }");
123 }
124 
125 TEST_F(FormatTestJS, ClosureStyleComments) {
126   verifyFormat("var x = /** @type {foo} */ (bar);");
127 }
128 
129 TEST_F(FormatTestJS, TryCatch) {
130   verifyFormat("try {\n"
131                "  f();\n"
132                "} catch (e) {\n"
133                "  g();\n"
134                "} finally {\n"
135                "  h();\n"
136                "}");
137 }
138 
139 TEST_F(FormatTestJS, RegexLiteralClassification) {
140   // Regex literals.
141   verifyFormat("var regex = /abc/;");
142   verifyFormat("f(/abc/);");
143   verifyFormat("f(abc, /abc/);");
144   verifyFormat("some_map[/abc/];");
145   verifyFormat("var x = a ? /abc/ : /abc/;");
146   verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
147   verifyFormat("var x = !/abc/.test(y);");
148   verifyFormat("var x = a && /abc/.test(y);");
149   verifyFormat("var x = a || /abc/.test(y);");
150   verifyFormat("var x = a + /abc/.search(y);");
151   verifyFormat("var regexs = {/abc/, /abc/};");
152   verifyFormat("return /abc/;");
153 
154   // Not regex literals.
155   verifyFormat("var a = a / 2 + b / 3;");
156 }
157 
158 TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
159   verifyFormat("var regex = /a*/;");
160   verifyFormat("var regex = /a+/;");
161   verifyFormat("var regex = /a?/;");
162   verifyFormat("var regex = /.a./;");
163   verifyFormat("var regex = /a\\*/;");
164   verifyFormat("var regex = /^a$/;");
165   verifyFormat("var regex = /\\/a/;");
166   verifyFormat("var regex = /(?:x)/;");
167   verifyFormat("var regex = /x(?=y)/;");
168   verifyFormat("var regex = /x(?!y)/;");
169   verifyFormat("var regex = /x|y/;");
170   verifyFormat("var regex = /a{2}/;");
171   verifyFormat("var regex = /a{1,3}/;");
172   verifyFormat("var regex = /[abc]/;");
173   verifyFormat("var regex = /[^abc]/;");
174   verifyFormat("var regex = /[\\b]/;");
175   verifyFormat("var regex = /\\b/;");
176   verifyFormat("var regex = /\\B/;");
177   verifyFormat("var regex = /\\d/;");
178   verifyFormat("var regex = /\\D/;");
179   verifyFormat("var regex = /\\f/;");
180   verifyFormat("var regex = /\\n/;");
181   verifyFormat("var regex = /\\r/;");
182   verifyFormat("var regex = /\\s/;");
183   verifyFormat("var regex = /\\S/;");
184   verifyFormat("var regex = /\\t/;");
185   verifyFormat("var regex = /\\v/;");
186   verifyFormat("var regex = /\\w/;");
187   verifyFormat("var regex = /\\W/;");
188   verifyFormat("var regex = /a(a)\\1/;");
189   verifyFormat("var regex = /\\0/;");
190   verifyFormat("var regex = /\\\\/g;");
191   verifyFormat("var regex = /\\a\\\\/g;");
192   verifyFormat("var regex = /\a\\//g;");
193 }
194 
195 TEST_F(FormatTestJS, RegexLiteralModifiers) {
196   verifyFormat("var regex = /abc/g;");
197   verifyFormat("var regex = /abc/i;");
198   verifyFormat("var regex = /abc/m;");
199   verifyFormat("var regex = /abc/y;");
200 }
201 
202 TEST_F(FormatTestJS, RegexLiteralLength) {
203   verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
204                getGoogleJSStyleWithColumns(60));
205   verifyFormat("var regex =\n"
206                "    /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
207                getGoogleJSStyleWithColumns(60));
208 }
209 
210 TEST_F(FormatTestJS, RegexLiteralExamples) {
211   verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
212 }
213 
214 } // end namespace tooling
215 } // end namespace clang
216