1*0a6a1f1dSLionel Sambuc //===- unittest/Format/FormatTestJS.cpp - Formatting unit tests for JS ----===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc // The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc
10*0a6a1f1dSLionel Sambuc #include "FormatTestUtils.h"
11*0a6a1f1dSLionel Sambuc #include "clang/Format/Format.h"
12*0a6a1f1dSLionel Sambuc #include "llvm/Support/Debug.h"
13*0a6a1f1dSLionel Sambuc #include "gtest/gtest.h"
14*0a6a1f1dSLionel Sambuc
15*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "format-test"
16*0a6a1f1dSLionel Sambuc
17*0a6a1f1dSLionel Sambuc namespace clang {
18*0a6a1f1dSLionel Sambuc namespace format {
19*0a6a1f1dSLionel Sambuc
20*0a6a1f1dSLionel Sambuc class FormatTestJS : public ::testing::Test {
21*0a6a1f1dSLionel Sambuc protected:
format(llvm::StringRef Code,unsigned Offset,unsigned Length,const FormatStyle & Style)22*0a6a1f1dSLionel Sambuc static std::string format(llvm::StringRef Code, unsigned Offset,
23*0a6a1f1dSLionel Sambuc unsigned Length, const FormatStyle &Style) {
24*0a6a1f1dSLionel Sambuc DEBUG(llvm::errs() << "---\n");
25*0a6a1f1dSLionel Sambuc DEBUG(llvm::errs() << Code << "\n\n");
26*0a6a1f1dSLionel Sambuc std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27*0a6a1f1dSLionel Sambuc tooling::Replacements Replaces = reformat(Style, Code, Ranges);
28*0a6a1f1dSLionel Sambuc std::string Result = applyAllReplacements(Code, Replaces);
29*0a6a1f1dSLionel Sambuc EXPECT_NE("", Result);
30*0a6a1f1dSLionel Sambuc DEBUG(llvm::errs() << "\n" << Result << "\n\n");
31*0a6a1f1dSLionel Sambuc return Result;
32*0a6a1f1dSLionel Sambuc }
33*0a6a1f1dSLionel Sambuc
format(llvm::StringRef Code,const FormatStyle & Style=getGoogleStyle (FormatStyle::LK_JavaScript))34*0a6a1f1dSLionel Sambuc static std::string format(
35*0a6a1f1dSLionel Sambuc llvm::StringRef Code,
36*0a6a1f1dSLionel Sambuc const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
37*0a6a1f1dSLionel Sambuc return format(Code, 0, Code.size(), Style);
38*0a6a1f1dSLionel Sambuc }
39*0a6a1f1dSLionel Sambuc
getGoogleJSStyleWithColumns(unsigned ColumnLimit)40*0a6a1f1dSLionel Sambuc static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
41*0a6a1f1dSLionel Sambuc FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
42*0a6a1f1dSLionel Sambuc Style.ColumnLimit = ColumnLimit;
43*0a6a1f1dSLionel Sambuc return Style;
44*0a6a1f1dSLionel Sambuc }
45*0a6a1f1dSLionel Sambuc
verifyFormat(llvm::StringRef Code,const FormatStyle & Style=getGoogleStyle (FormatStyle::LK_JavaScript))46*0a6a1f1dSLionel Sambuc static void verifyFormat(
47*0a6a1f1dSLionel Sambuc llvm::StringRef Code,
48*0a6a1f1dSLionel Sambuc const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
49*0a6a1f1dSLionel Sambuc EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
50*0a6a1f1dSLionel Sambuc }
51*0a6a1f1dSLionel Sambuc };
52*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,UnderstandsJavaScriptOperators)53*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
54*0a6a1f1dSLionel Sambuc verifyFormat("a == = b;");
55*0a6a1f1dSLionel Sambuc verifyFormat("a != = b;");
56*0a6a1f1dSLionel Sambuc
57*0a6a1f1dSLionel Sambuc verifyFormat("a === b;");
58*0a6a1f1dSLionel Sambuc verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10));
59*0a6a1f1dSLionel Sambuc verifyFormat("a !== b;");
60*0a6a1f1dSLionel Sambuc verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10));
61*0a6a1f1dSLionel Sambuc verifyFormat("if (a + b + c +\n"
62*0a6a1f1dSLionel Sambuc " d !==\n"
63*0a6a1f1dSLionel Sambuc " e + f + g)\n"
64*0a6a1f1dSLionel Sambuc " q();",
65*0a6a1f1dSLionel Sambuc getGoogleJSStyleWithColumns(20));
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc verifyFormat("a >> >= b;");
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambuc verifyFormat("a >>> b;");
70*0a6a1f1dSLionel Sambuc verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10));
71*0a6a1f1dSLionel Sambuc verifyFormat("a >>>= b;");
72*0a6a1f1dSLionel Sambuc verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10));
73*0a6a1f1dSLionel Sambuc verifyFormat("if (a + b + c +\n"
74*0a6a1f1dSLionel Sambuc " d >>>\n"
75*0a6a1f1dSLionel Sambuc " e + f + g)\n"
76*0a6a1f1dSLionel Sambuc " q();",
77*0a6a1f1dSLionel Sambuc getGoogleJSStyleWithColumns(20));
78*0a6a1f1dSLionel Sambuc verifyFormat("var x = aaaaaaaaaa ?\n"
79*0a6a1f1dSLionel Sambuc " bbbbbb :\n"
80*0a6a1f1dSLionel Sambuc " ccc;",
81*0a6a1f1dSLionel Sambuc getGoogleJSStyleWithColumns(20));
82*0a6a1f1dSLionel Sambuc
83*0a6a1f1dSLionel Sambuc verifyFormat("var b = a.map((x) => x + 1);");
84*0a6a1f1dSLionel Sambuc verifyFormat("return ('aaa') in bbbb;");
85*0a6a1f1dSLionel Sambuc }
86*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,UnderstandsAmpAmp)87*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, UnderstandsAmpAmp) {
88*0a6a1f1dSLionel Sambuc verifyFormat("e && e.SomeFunction();");
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,LiteralOperatorsCanBeKeywords)91*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
92*0a6a1f1dSLionel Sambuc verifyFormat("not.and.or.not_eq = 1;");
93*0a6a1f1dSLionel Sambuc }
94*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,ES6DestructuringAssignment)95*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, ES6DestructuringAssignment) {
96*0a6a1f1dSLionel Sambuc verifyFormat("var [a, b, c] = [1, 2, 3];");
97*0a6a1f1dSLionel Sambuc verifyFormat("var {a, b} = {\n"
98*0a6a1f1dSLionel Sambuc " a: 1,\n"
99*0a6a1f1dSLionel Sambuc " b: 2\n"
100*0a6a1f1dSLionel Sambuc "};");
101*0a6a1f1dSLionel Sambuc }
102*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,ContainerLiterals)103*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, ContainerLiterals) {
104*0a6a1f1dSLionel Sambuc verifyFormat("return {\n"
105*0a6a1f1dSLionel Sambuc " link: function() {\n"
106*0a6a1f1dSLionel Sambuc " f(); //\n"
107*0a6a1f1dSLionel Sambuc " }\n"
108*0a6a1f1dSLionel Sambuc "};");
109*0a6a1f1dSLionel Sambuc verifyFormat("return {\n"
110*0a6a1f1dSLionel Sambuc " a: a,\n"
111*0a6a1f1dSLionel Sambuc " link: function() {\n"
112*0a6a1f1dSLionel Sambuc " f(); //\n"
113*0a6a1f1dSLionel Sambuc " }\n"
114*0a6a1f1dSLionel Sambuc "};");
115*0a6a1f1dSLionel Sambuc verifyFormat("return {\n"
116*0a6a1f1dSLionel Sambuc " a: a,\n"
117*0a6a1f1dSLionel Sambuc " link: function() {\n"
118*0a6a1f1dSLionel Sambuc " f(); //\n"
119*0a6a1f1dSLionel Sambuc " },\n"
120*0a6a1f1dSLionel Sambuc " link: function() {\n"
121*0a6a1f1dSLionel Sambuc " f(); //\n"
122*0a6a1f1dSLionel Sambuc " }\n"
123*0a6a1f1dSLionel Sambuc "};");
124*0a6a1f1dSLionel Sambuc verifyFormat("var stuff = {\n"
125*0a6a1f1dSLionel Sambuc " // comment for update\n"
126*0a6a1f1dSLionel Sambuc " update: false,\n"
127*0a6a1f1dSLionel Sambuc " // comment for modules\n"
128*0a6a1f1dSLionel Sambuc " modules: false,\n"
129*0a6a1f1dSLionel Sambuc " // comment for tasks\n"
130*0a6a1f1dSLionel Sambuc " tasks: false\n"
131*0a6a1f1dSLionel Sambuc "};");
132*0a6a1f1dSLionel Sambuc verifyFormat("return {\n"
133*0a6a1f1dSLionel Sambuc " 'finish':\n"
134*0a6a1f1dSLionel Sambuc " //\n"
135*0a6a1f1dSLionel Sambuc " a\n"
136*0a6a1f1dSLionel Sambuc "};");
137*0a6a1f1dSLionel Sambuc verifyFormat("var obj = {\n"
138*0a6a1f1dSLionel Sambuc " fooooooooo: function(x) {\n"
139*0a6a1f1dSLionel Sambuc " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
140*0a6a1f1dSLionel Sambuc " }\n"
141*0a6a1f1dSLionel Sambuc "};");
142*0a6a1f1dSLionel Sambuc }
143*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,SpacesInContainerLiterals)144*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, SpacesInContainerLiterals) {
145*0a6a1f1dSLionel Sambuc verifyFormat("var arr = [1, 2, 3];");
146*0a6a1f1dSLionel Sambuc verifyFormat("f({a: 1, b: 2, c: 3});");
147*0a6a1f1dSLionel Sambuc
148*0a6a1f1dSLionel Sambuc verifyFormat("var object_literal_with_long_name = {\n"
149*0a6a1f1dSLionel Sambuc " a: 'aaaaaaaaaaaaaaaaaa',\n"
150*0a6a1f1dSLionel Sambuc " b: 'bbbbbbbbbbbbbbbbbb'\n"
151*0a6a1f1dSLionel Sambuc "};");
152*0a6a1f1dSLionel Sambuc
153*0a6a1f1dSLionel Sambuc verifyFormat("f({a: 1, b: 2, c: 3});",
154*0a6a1f1dSLionel Sambuc getChromiumStyle(FormatStyle::LK_JavaScript));
155*0a6a1f1dSLionel Sambuc verifyFormat("f({'a': [{}]});");
156*0a6a1f1dSLionel Sambuc }
157*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,SingleQuoteStrings)158*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, SingleQuoteStrings) {
159*0a6a1f1dSLionel Sambuc verifyFormat("this.function('', true);");
160*0a6a1f1dSLionel Sambuc }
161*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,GoogScopes)162*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, GoogScopes) {
163*0a6a1f1dSLionel Sambuc verifyFormat("goog.scope(function() {\n"
164*0a6a1f1dSLionel Sambuc "var x = a.b;\n"
165*0a6a1f1dSLionel Sambuc "var y = c.d;\n"
166*0a6a1f1dSLionel Sambuc "}); // goog.scope");
167*0a6a1f1dSLionel Sambuc }
168*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,GoogModules)169*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, GoogModules) {
170*0a6a1f1dSLionel Sambuc verifyFormat("goog.module('this.is.really.absurdly.long');",
171*0a6a1f1dSLionel Sambuc getGoogleJSStyleWithColumns(40));
172*0a6a1f1dSLionel Sambuc verifyFormat("goog.require('this.is.really.absurdly.long');",
173*0a6a1f1dSLionel Sambuc getGoogleJSStyleWithColumns(40));
174*0a6a1f1dSLionel Sambuc verifyFormat("goog.provide('this.is.really.absurdly.long');",
175*0a6a1f1dSLionel Sambuc getGoogleJSStyleWithColumns(40));
176*0a6a1f1dSLionel Sambuc verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
177*0a6a1f1dSLionel Sambuc getGoogleJSStyleWithColumns(40));
178*0a6a1f1dSLionel Sambuc
179*0a6a1f1dSLionel Sambuc // These should be wrapped normally.
180*0a6a1f1dSLionel Sambuc verifyFormat(
181*0a6a1f1dSLionel Sambuc "var MyLongClassName =\n"
182*0a6a1f1dSLionel Sambuc " goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
183*0a6a1f1dSLionel Sambuc }
184*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,FormatsFreestandingFunctions)185*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
186*0a6a1f1dSLionel Sambuc verifyFormat("function outer1(a, b) {\n"
187*0a6a1f1dSLionel Sambuc " function inner1(a, b) { return a; }\n"
188*0a6a1f1dSLionel Sambuc " inner1(a, b);\n"
189*0a6a1f1dSLionel Sambuc "}\n"
190*0a6a1f1dSLionel Sambuc "function outer2(a, b) {\n"
191*0a6a1f1dSLionel Sambuc " function inner2(a, b) { return a; }\n"
192*0a6a1f1dSLionel Sambuc " inner2(a, b);\n"
193*0a6a1f1dSLionel Sambuc "}");
194*0a6a1f1dSLionel Sambuc }
195*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,FunctionLiterals)196*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, FunctionLiterals) {
197*0a6a1f1dSLionel Sambuc verifyFormat("doFoo(function() {});");
198*0a6a1f1dSLionel Sambuc verifyFormat("doFoo(function() { return 1; });");
199*0a6a1f1dSLionel Sambuc verifyFormat("var func = function() {\n"
200*0a6a1f1dSLionel Sambuc " return 1;\n"
201*0a6a1f1dSLionel Sambuc "};");
202*0a6a1f1dSLionel Sambuc verifyFormat("return {\n"
203*0a6a1f1dSLionel Sambuc " body: {\n"
204*0a6a1f1dSLionel Sambuc " setAttribute: function(key, val) { this[key] = val; },\n"
205*0a6a1f1dSLionel Sambuc " getAttribute: function(key) { return this[key]; },\n"
206*0a6a1f1dSLionel Sambuc " style: {direction: ''}\n"
207*0a6a1f1dSLionel Sambuc " }\n"
208*0a6a1f1dSLionel Sambuc "};");
209*0a6a1f1dSLionel Sambuc EXPECT_EQ("abc = xyz ?\n"
210*0a6a1f1dSLionel Sambuc " function() {\n"
211*0a6a1f1dSLionel Sambuc " return 1;\n"
212*0a6a1f1dSLionel Sambuc " } :\n"
213*0a6a1f1dSLionel Sambuc " function() {\n"
214*0a6a1f1dSLionel Sambuc " return -1;\n"
215*0a6a1f1dSLionel Sambuc " };",
216*0a6a1f1dSLionel Sambuc format("abc=xyz?function(){return 1;}:function(){return -1;};"));
217*0a6a1f1dSLionel Sambuc
218*0a6a1f1dSLionel Sambuc verifyFormat("var closure = goog.bind(\n"
219*0a6a1f1dSLionel Sambuc " function() { // comment\n"
220*0a6a1f1dSLionel Sambuc " foo();\n"
221*0a6a1f1dSLionel Sambuc " bar();\n"
222*0a6a1f1dSLionel Sambuc " },\n"
223*0a6a1f1dSLionel Sambuc " this, arg1IsReallyLongAndNeeedsLineBreaks,\n"
224*0a6a1f1dSLionel Sambuc " arg3IsReallyLongAndNeeedsLineBreaks);");
225*0a6a1f1dSLionel Sambuc verifyFormat("var closure = goog.bind(function() { // comment\n"
226*0a6a1f1dSLionel Sambuc " foo();\n"
227*0a6a1f1dSLionel Sambuc " bar();\n"
228*0a6a1f1dSLionel Sambuc "}, this);");
229*0a6a1f1dSLionel Sambuc verifyFormat("return {\n"
230*0a6a1f1dSLionel Sambuc " a: 'E',\n"
231*0a6a1f1dSLionel Sambuc " b: function() {\n"
232*0a6a1f1dSLionel Sambuc " return function() {\n"
233*0a6a1f1dSLionel Sambuc " f(); //\n"
234*0a6a1f1dSLionel Sambuc " };\n"
235*0a6a1f1dSLionel Sambuc " }\n"
236*0a6a1f1dSLionel Sambuc "};");
237*0a6a1f1dSLionel Sambuc verifyFormat("{\n"
238*0a6a1f1dSLionel Sambuc " var someVariable = function(x) {\n"
239*0a6a1f1dSLionel Sambuc " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
240*0a6a1f1dSLionel Sambuc " };\n"
241*0a6a1f1dSLionel Sambuc "}");
242*0a6a1f1dSLionel Sambuc
243*0a6a1f1dSLionel Sambuc verifyFormat("f({a: function() { return 1; }});",
244*0a6a1f1dSLionel Sambuc getGoogleJSStyleWithColumns(33));
245*0a6a1f1dSLionel Sambuc verifyFormat("f({\n"
246*0a6a1f1dSLionel Sambuc " a: function() { return 1; }\n"
247*0a6a1f1dSLionel Sambuc "});",
248*0a6a1f1dSLionel Sambuc getGoogleJSStyleWithColumns(32));
249*0a6a1f1dSLionel Sambuc
250*0a6a1f1dSLionel Sambuc verifyFormat("return {\n"
251*0a6a1f1dSLionel Sambuc " a: function SomeFunction() {\n"
252*0a6a1f1dSLionel Sambuc " // ...\n"
253*0a6a1f1dSLionel Sambuc " return 1;\n"
254*0a6a1f1dSLionel Sambuc " }\n"
255*0a6a1f1dSLionel Sambuc "};");
256*0a6a1f1dSLionel Sambuc verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
257*0a6a1f1dSLionel Sambuc " .then(goog.bind(function(aaaaaaaaaaa) {\n"
258*0a6a1f1dSLionel Sambuc " someFunction();\n"
259*0a6a1f1dSLionel Sambuc " someFunction();\n"
260*0a6a1f1dSLionel Sambuc " }, this), aaaaaaaaaaaaaaaaa);");
261*0a6a1f1dSLionel Sambuc
262*0a6a1f1dSLionel Sambuc // FIXME: This is not ideal yet.
263*0a6a1f1dSLionel Sambuc verifyFormat("someFunction(goog.bind(\n"
264*0a6a1f1dSLionel Sambuc " function() {\n"
265*0a6a1f1dSLionel Sambuc " doSomething();\n"
266*0a6a1f1dSLionel Sambuc " doSomething();\n"
267*0a6a1f1dSLionel Sambuc " },\n"
268*0a6a1f1dSLionel Sambuc " this),\n"
269*0a6a1f1dSLionel Sambuc " goog.bind(function() {\n"
270*0a6a1f1dSLionel Sambuc " doSomething();\n"
271*0a6a1f1dSLionel Sambuc " doSomething();\n"
272*0a6a1f1dSLionel Sambuc " }, this));");
273*0a6a1f1dSLionel Sambuc }
274*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,InliningFunctionLiterals)275*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, InliningFunctionLiterals) {
276*0a6a1f1dSLionel Sambuc FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
277*0a6a1f1dSLionel Sambuc Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
278*0a6a1f1dSLionel Sambuc verifyFormat("var func = function() {\n"
279*0a6a1f1dSLionel Sambuc " return 1;\n"
280*0a6a1f1dSLionel Sambuc "};",
281*0a6a1f1dSLionel Sambuc Style);
282*0a6a1f1dSLionel Sambuc verifyFormat("var func = doSomething(function() { return 1; });", Style);
283*0a6a1f1dSLionel Sambuc verifyFormat("var outer = function() {\n"
284*0a6a1f1dSLionel Sambuc " var inner = function() { return 1; }\n"
285*0a6a1f1dSLionel Sambuc "};",
286*0a6a1f1dSLionel Sambuc Style);
287*0a6a1f1dSLionel Sambuc verifyFormat("function outer1(a, b) {\n"
288*0a6a1f1dSLionel Sambuc " function inner1(a, b) { return a; }\n"
289*0a6a1f1dSLionel Sambuc "}",
290*0a6a1f1dSLionel Sambuc Style);
291*0a6a1f1dSLionel Sambuc
292*0a6a1f1dSLionel Sambuc Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
293*0a6a1f1dSLionel Sambuc verifyFormat("var func = function() { return 1; };", Style);
294*0a6a1f1dSLionel Sambuc verifyFormat("var func = doSomething(function() { return 1; });", Style);
295*0a6a1f1dSLionel Sambuc verifyFormat(
296*0a6a1f1dSLionel Sambuc "var outer = function() { var inner = function() { return 1; } };",
297*0a6a1f1dSLionel Sambuc Style);
298*0a6a1f1dSLionel Sambuc verifyFormat("function outer1(a, b) {\n"
299*0a6a1f1dSLionel Sambuc " function inner1(a, b) { return a; }\n"
300*0a6a1f1dSLionel Sambuc "}",
301*0a6a1f1dSLionel Sambuc Style);
302*0a6a1f1dSLionel Sambuc
303*0a6a1f1dSLionel Sambuc Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
304*0a6a1f1dSLionel Sambuc verifyFormat("var func = function() {\n"
305*0a6a1f1dSLionel Sambuc " return 1;\n"
306*0a6a1f1dSLionel Sambuc "};",
307*0a6a1f1dSLionel Sambuc Style);
308*0a6a1f1dSLionel Sambuc verifyFormat("var func = doSomething(function() {\n"
309*0a6a1f1dSLionel Sambuc " return 1;\n"
310*0a6a1f1dSLionel Sambuc "});",
311*0a6a1f1dSLionel Sambuc Style);
312*0a6a1f1dSLionel Sambuc verifyFormat("var outer = function() {\n"
313*0a6a1f1dSLionel Sambuc " var inner = function() {\n"
314*0a6a1f1dSLionel Sambuc " return 1;\n"
315*0a6a1f1dSLionel Sambuc " }\n"
316*0a6a1f1dSLionel Sambuc "};",
317*0a6a1f1dSLionel Sambuc Style);
318*0a6a1f1dSLionel Sambuc verifyFormat("function outer1(a, b) {\n"
319*0a6a1f1dSLionel Sambuc " function inner1(a, b) {\n"
320*0a6a1f1dSLionel Sambuc " return a;\n"
321*0a6a1f1dSLionel Sambuc " }\n"
322*0a6a1f1dSLionel Sambuc "}",
323*0a6a1f1dSLionel Sambuc Style);
324*0a6a1f1dSLionel Sambuc }
325*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,MultipleFunctionLiterals)326*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, MultipleFunctionLiterals) {
327*0a6a1f1dSLionel Sambuc verifyFormat("promise.then(\n"
328*0a6a1f1dSLionel Sambuc " function success() {\n"
329*0a6a1f1dSLionel Sambuc " doFoo();\n"
330*0a6a1f1dSLionel Sambuc " doBar();\n"
331*0a6a1f1dSLionel Sambuc " },\n"
332*0a6a1f1dSLionel Sambuc " function error() {\n"
333*0a6a1f1dSLionel Sambuc " doFoo();\n"
334*0a6a1f1dSLionel Sambuc " doBaz();\n"
335*0a6a1f1dSLionel Sambuc " },\n"
336*0a6a1f1dSLionel Sambuc " []);\n");
337*0a6a1f1dSLionel Sambuc verifyFormat("promise.then(\n"
338*0a6a1f1dSLionel Sambuc " function success() {\n"
339*0a6a1f1dSLionel Sambuc " doFoo();\n"
340*0a6a1f1dSLionel Sambuc " doBar();\n"
341*0a6a1f1dSLionel Sambuc " },\n"
342*0a6a1f1dSLionel Sambuc " [],\n"
343*0a6a1f1dSLionel Sambuc " function error() {\n"
344*0a6a1f1dSLionel Sambuc " doFoo();\n"
345*0a6a1f1dSLionel Sambuc " doBaz();\n"
346*0a6a1f1dSLionel Sambuc " });\n");
347*0a6a1f1dSLionel Sambuc // FIXME: Here, we should probably break right after the "(" for consistency.
348*0a6a1f1dSLionel Sambuc verifyFormat("promise.then([],\n"
349*0a6a1f1dSLionel Sambuc " function success() {\n"
350*0a6a1f1dSLionel Sambuc " doFoo();\n"
351*0a6a1f1dSLionel Sambuc " doBar();\n"
352*0a6a1f1dSLionel Sambuc " },\n"
353*0a6a1f1dSLionel Sambuc " function error() {\n"
354*0a6a1f1dSLionel Sambuc " doFoo();\n"
355*0a6a1f1dSLionel Sambuc " doBaz();\n"
356*0a6a1f1dSLionel Sambuc " });\n");
357*0a6a1f1dSLionel Sambuc
358*0a6a1f1dSLionel Sambuc verifyFormat("getSomeLongPromise()\n"
359*0a6a1f1dSLionel Sambuc " .then(function(value) { body(); })\n"
360*0a6a1f1dSLionel Sambuc " .thenCatch(function(error) {\n"
361*0a6a1f1dSLionel Sambuc " body();\n"
362*0a6a1f1dSLionel Sambuc " body();\n"
363*0a6a1f1dSLionel Sambuc " });");
364*0a6a1f1dSLionel Sambuc verifyFormat("getSomeLongPromise()\n"
365*0a6a1f1dSLionel Sambuc " .then(function(value) {\n"
366*0a6a1f1dSLionel Sambuc " body();\n"
367*0a6a1f1dSLionel Sambuc " body();\n"
368*0a6a1f1dSLionel Sambuc " })\n"
369*0a6a1f1dSLionel Sambuc " .thenCatch(function(error) {\n"
370*0a6a1f1dSLionel Sambuc " body();\n"
371*0a6a1f1dSLionel Sambuc " body();\n"
372*0a6a1f1dSLionel Sambuc " });");
373*0a6a1f1dSLionel Sambuc
374*0a6a1f1dSLionel Sambuc // FIXME: This is bad, but it used to be formatted correctly by accident.
375*0a6a1f1dSLionel Sambuc verifyFormat("getSomeLongPromise().then(function(value) {\n"
376*0a6a1f1dSLionel Sambuc " body();\n"
377*0a6a1f1dSLionel Sambuc "}).thenCatch(function(error) { body(); });");
378*0a6a1f1dSLionel Sambuc }
379*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,ReturnStatements)380*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, ReturnStatements) {
381*0a6a1f1dSLionel Sambuc verifyFormat("function() {\n"
382*0a6a1f1dSLionel Sambuc " return [hello, world];\n"
383*0a6a1f1dSLionel Sambuc "}");
384*0a6a1f1dSLionel Sambuc }
385*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,ClosureStyleComments)386*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, ClosureStyleComments) {
387*0a6a1f1dSLionel Sambuc verifyFormat("var x = /** @type {foo} */ (bar);");
388*0a6a1f1dSLionel Sambuc }
389*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,TryCatch)390*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, TryCatch) {
391*0a6a1f1dSLionel Sambuc verifyFormat("try {\n"
392*0a6a1f1dSLionel Sambuc " f();\n"
393*0a6a1f1dSLionel Sambuc "} catch (e) {\n"
394*0a6a1f1dSLionel Sambuc " g();\n"
395*0a6a1f1dSLionel Sambuc "} finally {\n"
396*0a6a1f1dSLionel Sambuc " h();\n"
397*0a6a1f1dSLionel Sambuc "}");
398*0a6a1f1dSLionel Sambuc
399*0a6a1f1dSLionel Sambuc // But, of course, "catch" is a perfectly fine function name in JavaScript.
400*0a6a1f1dSLionel Sambuc verifyFormat("someObject.catch();");
401*0a6a1f1dSLionel Sambuc verifyFormat("someObject.new();");
402*0a6a1f1dSLionel Sambuc verifyFormat("someObject.delete();");
403*0a6a1f1dSLionel Sambuc }
404*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,StringLiteralConcatenation)405*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, StringLiteralConcatenation) {
406*0a6a1f1dSLionel Sambuc verifyFormat("var literal = 'hello ' +\n"
407*0a6a1f1dSLionel Sambuc " 'world';");
408*0a6a1f1dSLionel Sambuc }
409*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,RegexLiteralClassification)410*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, RegexLiteralClassification) {
411*0a6a1f1dSLionel Sambuc // Regex literals.
412*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /abc/;");
413*0a6a1f1dSLionel Sambuc verifyFormat("f(/abc/);");
414*0a6a1f1dSLionel Sambuc verifyFormat("f(abc, /abc/);");
415*0a6a1f1dSLionel Sambuc verifyFormat("some_map[/abc/];");
416*0a6a1f1dSLionel Sambuc verifyFormat("var x = a ? /abc/ : /abc/;");
417*0a6a1f1dSLionel Sambuc verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
418*0a6a1f1dSLionel Sambuc verifyFormat("var x = !/abc/.test(y);");
419*0a6a1f1dSLionel Sambuc verifyFormat("var x = a && /abc/.test(y);");
420*0a6a1f1dSLionel Sambuc verifyFormat("var x = a || /abc/.test(y);");
421*0a6a1f1dSLionel Sambuc verifyFormat("var x = a + /abc/.search(y);");
422*0a6a1f1dSLionel Sambuc verifyFormat("var regexs = {/abc/, /abc/};");
423*0a6a1f1dSLionel Sambuc verifyFormat("return /abc/;");
424*0a6a1f1dSLionel Sambuc
425*0a6a1f1dSLionel Sambuc // Not regex literals.
426*0a6a1f1dSLionel Sambuc verifyFormat("var a = a / 2 + b / 3;");
427*0a6a1f1dSLionel Sambuc }
428*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,RegexLiteralSpecialCharacters)429*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
430*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /a*/;");
431*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /a+/;");
432*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /a?/;");
433*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /.a./;");
434*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /a\\*/;");
435*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /^a$/;");
436*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\/a/;");
437*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /(?:x)/;");
438*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /x(?=y)/;");
439*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /x(?!y)/;");
440*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /x|y/;");
441*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /a{2}/;");
442*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /a{1,3}/;");
443*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /[abc]/;");
444*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /[^abc]/;");
445*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /[\\b]/;");
446*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\b/;");
447*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\B/;");
448*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\d/;");
449*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\D/;");
450*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\f/;");
451*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\n/;");
452*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\r/;");
453*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\s/;");
454*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\S/;");
455*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\t/;");
456*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\v/;");
457*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\w/;");
458*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\W/;");
459*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /a(a)\\1/;");
460*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\0/;");
461*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\\\/g;");
462*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\a\\\\/g;");
463*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\a\\//g;");
464*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /a\\//;\n"
465*0a6a1f1dSLionel Sambuc "var x = 0;");
466*0a6a1f1dSLionel Sambuc EXPECT_EQ("var regex = /\\/*/;\n"
467*0a6a1f1dSLionel Sambuc "var x = 0;",
468*0a6a1f1dSLionel Sambuc format("var regex = /\\/*/;\n"
469*0a6a1f1dSLionel Sambuc "var x=0;"));
470*0a6a1f1dSLionel Sambuc }
471*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,RegexLiteralModifiers)472*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, RegexLiteralModifiers) {
473*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /abc/g;");
474*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /abc/i;");
475*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /abc/m;");
476*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /abc/y;");
477*0a6a1f1dSLionel Sambuc }
478*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,RegexLiteralLength)479*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, RegexLiteralLength) {
480*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
481*0a6a1f1dSLionel Sambuc getGoogleJSStyleWithColumns(60));
482*0a6a1f1dSLionel Sambuc verifyFormat("var regex =\n"
483*0a6a1f1dSLionel Sambuc " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
484*0a6a1f1dSLionel Sambuc getGoogleJSStyleWithColumns(60));
485*0a6a1f1dSLionel Sambuc verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
486*0a6a1f1dSLionel Sambuc getGoogleJSStyleWithColumns(50));
487*0a6a1f1dSLionel Sambuc }
488*0a6a1f1dSLionel Sambuc
TEST_F(FormatTestJS,RegexLiteralExamples)489*0a6a1f1dSLionel Sambuc TEST_F(FormatTestJS, RegexLiteralExamples) {
490*0a6a1f1dSLionel Sambuc verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
491*0a6a1f1dSLionel Sambuc }
492*0a6a1f1dSLionel Sambuc
493*0a6a1f1dSLionel Sambuc } // end namespace tooling
494*0a6a1f1dSLionel Sambuc } // end namespace clang
495