FormatTestJS.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  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. #include "FormatTestUtils.h"
  10. #include "clang/Format/Format.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "gtest/gtest.h"
  13. #define DEBUG_TYPE "format-test"
  14. namespace clang {
  15. namespace format {
  16. class FormatTestJS : public ::testing::Test {
  17. protected:
  18. static std::string format(llvm::StringRef Code, unsigned Offset,
  19. unsigned Length, const FormatStyle &Style) {
  20. DEBUG(llvm::errs() << "---\n");
  21. DEBUG(llvm::errs() << Code << "\n\n");
  22. std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
  23. FormattingAttemptStatus Status;
  24. tooling::Replacements Replaces =
  25. reformat(Style, Code, Ranges, "<stdin>", &Status);
  26. EXPECT_TRUE(Status.FormatComplete);
  27. auto Result = applyAllReplacements(Code, Replaces);
  28. EXPECT_TRUE(static_cast<bool>(Result));
  29. DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
  30. return *Result;
  31. }
  32. static std::string format(
  33. llvm::StringRef Code,
  34. const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
  35. return format(Code, 0, Code.size(), Style);
  36. }
  37. static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
  38. FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
  39. Style.ColumnLimit = ColumnLimit;
  40. return Style;
  41. }
  42. static void verifyFormat(
  43. llvm::StringRef Code,
  44. const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
  45. std::string Result = format(test::messUp(Code), Style);
  46. EXPECT_EQ(Code.str(), Result) << "Formatted:\n" << Result;
  47. }
  48. static void verifyFormat(
  49. llvm::StringRef Expected,
  50. llvm::StringRef Code,
  51. const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
  52. std::string Result = format(Code, Style);
  53. EXPECT_EQ(Expected.str(), Result) << "Formatted:\n" << Result;
  54. }
  55. };
  56. TEST_F(FormatTestJS, BlockComments) {
  57. verifyFormat("/* aaaaaaaaaaaaa */ aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  58. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  59. // Breaks after a single line block comment.
  60. EXPECT_EQ("aaaaa = bbbb.ccccccccccccccc(\n"
  61. " /** @type_{!cccc.rrrrrrr.MMMMMMMMMMMM.LLLLLLLLLLL.lala} */\n"
  62. " mediaMessage);",
  63. format("aaaaa = bbbb.ccccccccccccccc(\n"
  64. " /** "
  65. "@type_{!cccc.rrrrrrr.MMMMMMMMMMMM.LLLLLLLLLLL.lala} */ "
  66. "mediaMessage);",
  67. getGoogleJSStyleWithColumns(70)));
  68. // Breaks after a multiline block comment.
  69. EXPECT_EQ(
  70. "aaaaa = bbbb.ccccccccccccccc(\n"
  71. " /**\n"
  72. " * @type_{!cccc.rrrrrrr.MMMMMMMMMMMM.LLLLLLLLLLL.lala}\n"
  73. " */\n"
  74. " mediaMessage);",
  75. format("aaaaa = bbbb.ccccccccccccccc(\n"
  76. " /**\n"
  77. " * @type_{!cccc.rrrrrrr.MMMMMMMMMMMM.LLLLLLLLLLL.lala}\n"
  78. " */ mediaMessage);",
  79. getGoogleJSStyleWithColumns(70)));
  80. }
  81. TEST_F(FormatTestJS, JSDocComments) {
  82. // Break the first line of a multiline jsdoc comment.
  83. EXPECT_EQ("/**\n"
  84. " * jsdoc line 1\n"
  85. " * jsdoc line 2\n"
  86. " */",
  87. format("/** jsdoc line 1\n"
  88. " * jsdoc line 2\n"
  89. " */",
  90. getGoogleJSStyleWithColumns(20)));
  91. // Both break after '/**' and break the line itself.
  92. EXPECT_EQ("/**\n"
  93. " * jsdoc line long\n"
  94. " * long jsdoc line 2\n"
  95. " */",
  96. format("/** jsdoc line long long\n"
  97. " * jsdoc line 2\n"
  98. " */",
  99. getGoogleJSStyleWithColumns(20)));
  100. // Break a short first line if the ending '*/' is on a newline.
  101. EXPECT_EQ("/**\n"
  102. " * jsdoc line 1\n"
  103. " */",
  104. format("/** jsdoc line 1\n"
  105. " */", getGoogleJSStyleWithColumns(20)));
  106. // Don't break the first line of a short single line jsdoc comment.
  107. EXPECT_EQ("/** jsdoc line 1 */",
  108. format("/** jsdoc line 1 */", getGoogleJSStyleWithColumns(20)));
  109. // Don't break the first line of a single line jsdoc comment if it just fits
  110. // the column limit.
  111. EXPECT_EQ("/** jsdoc line 12 */",
  112. format("/** jsdoc line 12 */", getGoogleJSStyleWithColumns(20)));
  113. // Don't break after '/**' and before '*/' if there is no space between
  114. // '/**' and the content.
  115. EXPECT_EQ(
  116. "/*** nonjsdoc long\n"
  117. " * line */",
  118. format("/*** nonjsdoc long line */", getGoogleJSStyleWithColumns(20)));
  119. EXPECT_EQ(
  120. "/**strange long long\n"
  121. " * line */",
  122. format("/**strange long long line */", getGoogleJSStyleWithColumns(20)));
  123. // Break the first line of a single line jsdoc comment if it just exceeds the
  124. // column limit.
  125. EXPECT_EQ("/**\n"
  126. " * jsdoc line 123\n"
  127. " */",
  128. format("/** jsdoc line 123 */", getGoogleJSStyleWithColumns(20)));
  129. // Break also if the leading indent of the first line is more than 1 column.
  130. EXPECT_EQ("/**\n"
  131. " * jsdoc line 123\n"
  132. " */",
  133. format("/** jsdoc line 123 */", getGoogleJSStyleWithColumns(20)));
  134. // Break also if the leading indent of the first line is more than 1 column.
  135. EXPECT_EQ("/**\n"
  136. " * jsdoc line 123\n"
  137. " */",
  138. format("/** jsdoc line 123 */", getGoogleJSStyleWithColumns(20)));
  139. // Break after the content of the last line.
  140. EXPECT_EQ("/**\n"
  141. " * line 1\n"
  142. " * line 2\n"
  143. " */",
  144. format("/**\n"
  145. " * line 1\n"
  146. " * line 2 */",
  147. getGoogleJSStyleWithColumns(20)));
  148. // Break both the content and after the content of the last line.
  149. EXPECT_EQ("/**\n"
  150. " * line 1\n"
  151. " * line long long\n"
  152. " * long\n"
  153. " */",
  154. format("/**\n"
  155. " * line 1\n"
  156. " * line long long long */",
  157. getGoogleJSStyleWithColumns(20)));
  158. // The comment block gets indented.
  159. EXPECT_EQ("function f() {\n"
  160. " /**\n"
  161. " * comment about\n"
  162. " * x\n"
  163. " */\n"
  164. " var x = 1;\n"
  165. "}",
  166. format("function f() {\n"
  167. "/** comment about x */\n"
  168. "var x = 1;\n"
  169. "}",
  170. getGoogleJSStyleWithColumns(20)));
  171. // Don't break the first line of a single line short jsdoc comment pragma.
  172. EXPECT_EQ("/** @returns j */",
  173. format("/** @returns j */",
  174. getGoogleJSStyleWithColumns(20)));
  175. // Break a single line long jsdoc comment pragma.
  176. EXPECT_EQ("/**\n"
  177. " * @returns {string} jsdoc line 12\n"
  178. " */",
  179. format("/** @returns {string} jsdoc line 12 */",
  180. getGoogleJSStyleWithColumns(20)));
  181. EXPECT_EQ("/**\n"
  182. " * @returns {string} jsdoc line 12\n"
  183. " */",
  184. format("/** @returns {string} jsdoc line 12 */",
  185. getGoogleJSStyleWithColumns(20)));
  186. EXPECT_EQ("/**\n"
  187. " * @returns {string} jsdoc line 12\n"
  188. " */",
  189. format("/** @returns {string} jsdoc line 12*/",
  190. getGoogleJSStyleWithColumns(20)));
  191. // Fix a multiline jsdoc comment ending in a comment pragma.
  192. EXPECT_EQ("/**\n"
  193. " * line 1\n"
  194. " * line 2\n"
  195. " * @returns {string} jsdoc line 12\n"
  196. " */",
  197. format("/** line 1\n"
  198. " * line 2\n"
  199. " * @returns {string} jsdoc line 12 */",
  200. getGoogleJSStyleWithColumns(20)));
  201. EXPECT_EQ("/**\n"
  202. " * line 1\n"
  203. " * line 2\n"
  204. " *\n"
  205. " * @returns j\n"
  206. " */",
  207. format("/** line 1\n"
  208. " * line 2\n"
  209. " *\n"
  210. " * @returns j */",
  211. getGoogleJSStyleWithColumns(20)));
  212. }
  213. TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) {
  214. verifyFormat("a == = b;");
  215. verifyFormat("a != = b;");
  216. verifyFormat("a === b;");
  217. verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10));
  218. verifyFormat("a !== b;");
  219. verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10));
  220. verifyFormat("if (a + b + c +\n"
  221. " d !==\n"
  222. " e + f + g)\n"
  223. " q();",
  224. getGoogleJSStyleWithColumns(20));
  225. verifyFormat("a >> >= b;");
  226. verifyFormat("a >>> b;");
  227. verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10));
  228. verifyFormat("a >>>= b;");
  229. verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10));
  230. verifyFormat("if (a + b + c +\n"
  231. " d >>>\n"
  232. " e + f + g)\n"
  233. " q();",
  234. getGoogleJSStyleWithColumns(20));
  235. verifyFormat("var x = aaaaaaaaaa ?\n"
  236. " bbbbbb :\n"
  237. " ccc;",
  238. getGoogleJSStyleWithColumns(20));
  239. verifyFormat("var b = a.map((x) => x + 1);");
  240. verifyFormat("return ('aaa') in bbbb;");
  241. verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n"
  242. " aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
  243. FormatStyle Style = getGoogleJSStyleWithColumns(80);
  244. Style.AlignOperands = true;
  245. verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n"
  246. " aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  247. Style);
  248. Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
  249. verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa()\n"
  250. " in aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
  251. Style);
  252. // ES6 spread operator.
  253. verifyFormat("someFunction(...a);");
  254. verifyFormat("var x = [1, ...a, 2];");
  255. }
  256. TEST_F(FormatTestJS, UnderstandsAmpAmp) {
  257. verifyFormat("e && e.SomeFunction();");
  258. }
  259. TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
  260. verifyFormat("not.and.or.not_eq = 1;");
  261. }
  262. TEST_F(FormatTestJS, ReservedWords) {
  263. // JavaScript reserved words (aka keywords) are only illegal when used as
  264. // Identifiers, but are legal as IdentifierNames.
  265. verifyFormat("x.class.struct = 1;");
  266. verifyFormat("x.case = 1;");
  267. verifyFormat("x.interface = 1;");
  268. verifyFormat("x.for = 1;");
  269. verifyFormat("x.of();");
  270. verifyFormat("of(null);");
  271. verifyFormat("return of(null);");
  272. verifyFormat("import {of} from 'x';");
  273. verifyFormat("x.in();");
  274. verifyFormat("x.let();");
  275. verifyFormat("x.var();");
  276. verifyFormat("x.for();");
  277. verifyFormat("x.as();");
  278. verifyFormat("x.instanceof();");
  279. verifyFormat("x.switch();");
  280. verifyFormat("x.case();");
  281. verifyFormat("x.delete();");
  282. verifyFormat("x.throw();");
  283. verifyFormat("x.throws();");
  284. verifyFormat("x.if();");
  285. verifyFormat("x = {\n"
  286. " a: 12,\n"
  287. " interface: 1,\n"
  288. " switch: 1,\n"
  289. "};");
  290. verifyFormat("var struct = 2;");
  291. verifyFormat("var union = 2;");
  292. verifyFormat("var interface = 2;");
  293. verifyFormat("interface = 2;");
  294. verifyFormat("x = interface instanceof y;");
  295. verifyFormat("interface Test {\n"
  296. " x: string;\n"
  297. " switch: string;\n"
  298. " case: string;\n"
  299. " default: string;\n"
  300. "}\n");
  301. verifyFormat("const Axis = {\n"
  302. " for: 'for',\n"
  303. " x: 'x'\n"
  304. "};",
  305. "const Axis = {for: 'for', x: 'x'};");
  306. }
  307. TEST_F(FormatTestJS, ReservedWordsMethods) {
  308. verifyFormat(
  309. "class X {\n"
  310. " delete() {\n"
  311. " x();\n"
  312. " }\n"
  313. " interface() {\n"
  314. " x();\n"
  315. " }\n"
  316. " let() {\n"
  317. " x();\n"
  318. " }\n"
  319. "}\n");
  320. }
  321. TEST_F(FormatTestJS, ReservedWordsParenthesized) {
  322. // All of these are statements using the keyword, not function calls.
  323. verifyFormat("throw (x + y);\n"
  324. "await (await x).y;\n"
  325. "typeof (x) === 'string';\n"
  326. "void (0);\n"
  327. "delete (x.y);\n"
  328. "return (x);\n");
  329. }
  330. TEST_F(FormatTestJS, CppKeywords) {
  331. // Make sure we don't mess stuff up because of C++ keywords.
  332. verifyFormat("return operator && (aa);");
  333. // .. or QT ones.
  334. verifyFormat("slots: Slot[];");
  335. }
  336. TEST_F(FormatTestJS, ES6DestructuringAssignment) {
  337. verifyFormat("var [a, b, c] = [1, 2, 3];");
  338. verifyFormat("const [a, b, c] = [1, 2, 3];");
  339. verifyFormat("let [a, b, c] = [1, 2, 3];");
  340. verifyFormat("var {a, b} = {a: 1, b: 2};");
  341. verifyFormat("let {a, b} = {a: 1, b: 2};");
  342. }
  343. TEST_F(FormatTestJS, ContainerLiterals) {
  344. verifyFormat("var x = {\n"
  345. " y: function(a) {\n"
  346. " return a;\n"
  347. " }\n"
  348. "};");
  349. verifyFormat("return {\n"
  350. " link: function() {\n"
  351. " f(); //\n"
  352. " }\n"
  353. "};");
  354. verifyFormat("return {\n"
  355. " a: a,\n"
  356. " link: function() {\n"
  357. " f(); //\n"
  358. " }\n"
  359. "};");
  360. verifyFormat("return {\n"
  361. " a: a,\n"
  362. " link: function() {\n"
  363. " f(); //\n"
  364. " },\n"
  365. " link: function() {\n"
  366. " f(); //\n"
  367. " }\n"
  368. "};");
  369. verifyFormat("var stuff = {\n"
  370. " // comment for update\n"
  371. " update: false,\n"
  372. " // comment for modules\n"
  373. " modules: false,\n"
  374. " // comment for tasks\n"
  375. " tasks: false\n"
  376. "};");
  377. verifyFormat("return {\n"
  378. " 'finish':\n"
  379. " //\n"
  380. " a\n"
  381. "};");
  382. verifyFormat("var obj = {\n"
  383. " fooooooooo: function(x) {\n"
  384. " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
  385. " }\n"
  386. "};");
  387. // Simple object literal, as opposed to enum style below.
  388. verifyFormat("var obj = {a: 123};");
  389. // Enum style top level assignment.
  390. verifyFormat("X = {\n a: 123\n};");
  391. verifyFormat("X.Y = {\n a: 123\n};");
  392. // But only on the top level, otherwise its a plain object literal assignment.
  393. verifyFormat("function x() {\n"
  394. " y = {z: 1};\n"
  395. "}");
  396. verifyFormat("x = foo && {a: 123};");
  397. // Arrow functions in object literals.
  398. verifyFormat("var x = {\n"
  399. " y: (a) => {\n"
  400. " return a;\n"
  401. " }\n"
  402. "};");
  403. verifyFormat("var x = {y: (a) => a};");
  404. // Methods in object literals.
  405. verifyFormat("var x = {\n"
  406. " y(a: string): number {\n"
  407. " return a;\n"
  408. " }\n"
  409. "};");
  410. verifyFormat("var x = {\n"
  411. " y(a: string) {\n"
  412. " return a;\n"
  413. " }\n"
  414. "};");
  415. // Computed keys.
  416. verifyFormat("var x = {[a]: 1, b: 2, [c]: 3};");
  417. verifyFormat("var x = {\n"
  418. " [a]: 1,\n"
  419. " b: 2,\n"
  420. " [c]: 3,\n"
  421. "};");
  422. // Object literals can leave out labels.
  423. verifyFormat("f({a}, () => {\n"
  424. " g(); //\n"
  425. "});");
  426. // Keys can be quoted.
  427. verifyFormat("var x = {\n"
  428. " a: a,\n"
  429. " b: b,\n"
  430. " 'c': c,\n"
  431. "};");
  432. // Dict literals can skip the label names.
  433. verifyFormat("var x = {\n"
  434. " aaa,\n"
  435. " aaa,\n"
  436. " aaa,\n"
  437. "};");
  438. verifyFormat("return {\n"
  439. " a,\n"
  440. " b: 'b',\n"
  441. " c,\n"
  442. "};");
  443. }
  444. TEST_F(FormatTestJS, MethodsInObjectLiterals) {
  445. verifyFormat("var o = {\n"
  446. " value: 'test',\n"
  447. " get value() { // getter\n"
  448. " return this.value;\n"
  449. " }\n"
  450. "};");
  451. verifyFormat("var o = {\n"
  452. " value: 'test',\n"
  453. " set value(val) { // setter\n"
  454. " this.value = val;\n"
  455. " }\n"
  456. "};");
  457. verifyFormat("var o = {\n"
  458. " value: 'test',\n"
  459. " someMethod(val) { // method\n"
  460. " doSomething(this.value + val);\n"
  461. " }\n"
  462. "};");
  463. verifyFormat("var o = {\n"
  464. " someMethod(val) { // method\n"
  465. " doSomething(this.value + val);\n"
  466. " },\n"
  467. " someOtherMethod(val) { // method\n"
  468. " doSomething(this.value + val);\n"
  469. " }\n"
  470. "};");
  471. }
  472. TEST_F(FormatTestJS, GettersSettersVisibilityKeywords) {
  473. // Don't break after "protected"
  474. verifyFormat("class X {\n"
  475. " protected get getter():\n"
  476. " number {\n"
  477. " return 1;\n"
  478. " }\n"
  479. "}",
  480. getGoogleJSStyleWithColumns(12));
  481. // Don't break after "get"
  482. verifyFormat("class X {\n"
  483. " protected get someReallyLongGetterName():\n"
  484. " number {\n"
  485. " return 1;\n"
  486. " }\n"
  487. "}",
  488. getGoogleJSStyleWithColumns(40));
  489. }
  490. TEST_F(FormatTestJS, SpacesInContainerLiterals) {
  491. verifyFormat("var arr = [1, 2, 3];");
  492. verifyFormat("f({a: 1, b: 2, c: 3});");
  493. verifyFormat("var object_literal_with_long_name = {\n"
  494. " a: 'aaaaaaaaaaaaaaaaaa',\n"
  495. " b: 'bbbbbbbbbbbbbbbbbb'\n"
  496. "};");
  497. verifyFormat("f({a: 1, b: 2, c: 3});",
  498. getChromiumStyle(FormatStyle::LK_JavaScript));
  499. verifyFormat("f({'a': [{}]});");
  500. }
  501. TEST_F(FormatTestJS, SingleQuotedStrings) {
  502. verifyFormat("this.function('', true);");
  503. }
  504. TEST_F(FormatTestJS, GoogScopes) {
  505. verifyFormat("goog.scope(function() {\n"
  506. "var x = a.b;\n"
  507. "var y = c.d;\n"
  508. "}); // goog.scope");
  509. verifyFormat("goog.scope(function() {\n"
  510. "// test\n"
  511. "var x = 0;\n"
  512. "// test\n"
  513. "});");
  514. }
  515. TEST_F(FormatTestJS, IIFEs) {
  516. // Internal calling parens; no semi.
  517. verifyFormat("(function() {\n"
  518. "var a = 1;\n"
  519. "}())");
  520. // External calling parens; no semi.
  521. verifyFormat("(function() {\n"
  522. "var b = 2;\n"
  523. "})()");
  524. // Internal calling parens; with semi.
  525. verifyFormat("(function() {\n"
  526. "var c = 3;\n"
  527. "}());");
  528. // External calling parens; with semi.
  529. verifyFormat("(function() {\n"
  530. "var d = 4;\n"
  531. "})();");
  532. }
  533. TEST_F(FormatTestJS, GoogModules) {
  534. verifyFormat("goog.module('this.is.really.absurdly.long');",
  535. getGoogleJSStyleWithColumns(40));
  536. verifyFormat("goog.require('this.is.really.absurdly.long');",
  537. getGoogleJSStyleWithColumns(40));
  538. verifyFormat("goog.provide('this.is.really.absurdly.long');",
  539. getGoogleJSStyleWithColumns(40));
  540. verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
  541. getGoogleJSStyleWithColumns(40));
  542. verifyFormat("goog.forwardDeclare('this.is.really.absurdly.long');",
  543. getGoogleJSStyleWithColumns(40));
  544. // These should be wrapped normally.
  545. verifyFormat(
  546. "var MyLongClassName =\n"
  547. " goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
  548. verifyFormat("function a() {\n"
  549. " goog.setTestOnly();\n"
  550. "}\n",
  551. "function a() {\n"
  552. "goog.setTestOnly();\n"
  553. "}\n");
  554. }
  555. TEST_F(FormatTestJS, FormatsNamespaces) {
  556. verifyFormat("namespace Foo {\n"
  557. " export let x = 1;\n"
  558. "}\n");
  559. verifyFormat("declare namespace Foo {\n"
  560. " export let x: number;\n"
  561. "}\n");
  562. }
  563. TEST_F(FormatTestJS, NamespacesMayNotWrap) {
  564. verifyFormat("declare namespace foobarbaz {\n"
  565. "}\n", getGoogleJSStyleWithColumns(18));
  566. verifyFormat("declare module foobarbaz {\n"
  567. "}\n", getGoogleJSStyleWithColumns(15));
  568. verifyFormat("namespace foobarbaz {\n"
  569. "}\n", getGoogleJSStyleWithColumns(10));
  570. verifyFormat("module foobarbaz {\n"
  571. "}\n", getGoogleJSStyleWithColumns(7));
  572. }
  573. TEST_F(FormatTestJS, AmbientDeclarations) {
  574. FormatStyle NineCols = getGoogleJSStyleWithColumns(9);
  575. verifyFormat(
  576. "declare class\n"
  577. " X {}",
  578. NineCols);
  579. verifyFormat(
  580. "declare function\n"
  581. "x();", // TODO(martinprobst): should ideally be indented.
  582. NineCols);
  583. verifyFormat("declare function foo();\n"
  584. "let x = 1;\n");
  585. verifyFormat("declare function foo(): string;\n"
  586. "let x = 1;\n");
  587. verifyFormat("declare function foo(): {x: number};\n"
  588. "let x = 1;\n");
  589. verifyFormat("declare class X {}\n"
  590. "let x = 1;\n");
  591. verifyFormat("declare interface Y {}\n"
  592. "let x = 1;\n");
  593. verifyFormat(
  594. "declare enum X {\n"
  595. "}",
  596. NineCols);
  597. verifyFormat(
  598. "declare let\n"
  599. " x: number;",
  600. NineCols);
  601. }
  602. TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
  603. verifyFormat("function outer1(a, b) {\n"
  604. " function inner1(a, b) {\n"
  605. " return a;\n"
  606. " }\n"
  607. " inner1(a, b);\n"
  608. "}\n"
  609. "function outer2(a, b) {\n"
  610. " function inner2(a, b) {\n"
  611. " return a;\n"
  612. " }\n"
  613. " inner2(a, b);\n"
  614. "}");
  615. verifyFormat("function f() {}");
  616. verifyFormat("function aFunction() {}\n"
  617. "(function f() {\n"
  618. " var x = 1;\n"
  619. "}());\n");
  620. verifyFormat("function aFunction() {}\n"
  621. "{\n"
  622. " let x = 1;\n"
  623. " console.log(x);\n"
  624. "}\n");
  625. }
  626. TEST_F(FormatTestJS, GeneratorFunctions) {
  627. verifyFormat("function* f() {\n"
  628. " let x = 1;\n"
  629. " yield x;\n"
  630. " yield* something();\n"
  631. " yield [1, 2];\n"
  632. " yield {a: 1};\n"
  633. "}");
  634. verifyFormat("function*\n"
  635. " f() {\n"
  636. "}",
  637. getGoogleJSStyleWithColumns(8));
  638. verifyFormat("export function* f() {\n"
  639. " yield 1;\n"
  640. "}\n");
  641. verifyFormat("class X {\n"
  642. " * generatorMethod() {\n"
  643. " yield x;\n"
  644. " }\n"
  645. "}");
  646. verifyFormat("var x = {\n"
  647. " a: function*() {\n"
  648. " //\n"
  649. " }\n"
  650. "}\n");
  651. }
  652. TEST_F(FormatTestJS, AsyncFunctions) {
  653. verifyFormat("async function f() {\n"
  654. " let x = 1;\n"
  655. " return fetch(x);\n"
  656. "}");
  657. verifyFormat("async function f() {\n"
  658. " return 1;\n"
  659. "}\n"
  660. "\n"
  661. "function a() {\n"
  662. " return 1;\n"
  663. "}\n",
  664. " async function f() {\n"
  665. " return 1;\n"
  666. "}\n"
  667. "\n"
  668. " function a() {\n"
  669. " return 1;\n"
  670. "} \n");
  671. verifyFormat("async function* f() {\n"
  672. " yield fetch(x);\n"
  673. "}");
  674. verifyFormat("export async function f() {\n"
  675. " return fetch(x);\n"
  676. "}");
  677. verifyFormat("let x = async () => f();");
  678. verifyFormat("let x = async function() {\n"
  679. " f();\n"
  680. "};");
  681. verifyFormat("let x = async();");
  682. verifyFormat("class X {\n"
  683. " async asyncMethod() {\n"
  684. " return fetch(1);\n"
  685. " }\n"
  686. "}");
  687. verifyFormat("function initialize() {\n"
  688. " // Comment.\n"
  689. " return async.then();\n"
  690. "}\n");
  691. verifyFormat("for await (const x of y) {\n"
  692. " console.log(x);\n"
  693. "}\n");
  694. verifyFormat("function asyncLoop() {\n"
  695. " for await (const x of y) {\n"
  696. " console.log(x);\n"
  697. " }\n"
  698. "}\n");
  699. }
  700. TEST_F(FormatTestJS, FunctionParametersTrailingComma) {
  701. verifyFormat("function trailingComma(\n"
  702. " p1,\n"
  703. " p2,\n"
  704. " p3,\n"
  705. ") {\n"
  706. " a; //\n"
  707. "}\n",
  708. "function trailingComma(p1, p2, p3,) {\n"
  709. " a; //\n"
  710. "}\n");
  711. verifyFormat("trailingComma(\n"
  712. " p1,\n"
  713. " p2,\n"
  714. " p3,\n"
  715. ");\n",
  716. "trailingComma(p1, p2, p3,);\n");
  717. verifyFormat("trailingComma(\n"
  718. " p1 // hello\n"
  719. ");\n",
  720. "trailingComma(p1 // hello\n"
  721. ");\n");
  722. }
  723. TEST_F(FormatTestJS, ArrayLiterals) {
  724. verifyFormat("var aaaaa: List<SomeThing> =\n"
  725. " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];");
  726. verifyFormat("return [\n"
  727. " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  728. " ccccccccccccccccccccccccccc\n"
  729. "];");
  730. verifyFormat("return [\n"
  731. " aaaa().bbbbbbbb('A'),\n"
  732. " aaaa().bbbbbbbb('B'),\n"
  733. " aaaa().bbbbbbbb('C'),\n"
  734. "];");
  735. verifyFormat("var someVariable = SomeFunction([\n"
  736. " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  737. " ccccccccccccccccccccccccccc\n"
  738. "]);");
  739. verifyFormat("var someVariable = SomeFunction([\n"
  740. " [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n"
  741. "]);",
  742. getGoogleJSStyleWithColumns(51));
  743. verifyFormat("var someVariable = SomeFunction(aaaa, [\n"
  744. " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  745. " ccccccccccccccccccccccccccc\n"
  746. "]);");
  747. verifyFormat("var someVariable = SomeFunction(\n"
  748. " aaaa,\n"
  749. " [\n"
  750. " aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  751. " cccccccccccccccccccccccccc\n"
  752. " ],\n"
  753. " aaaa);");
  754. verifyFormat("var aaaa = aaaaa || // wrap\n"
  755. " [];");
  756. verifyFormat("someFunction([], {a: a});");
  757. verifyFormat("var string = [\n"
  758. " 'aaaaaa',\n"
  759. " 'bbbbbb',\n"
  760. "].join('+');");
  761. }
  762. TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) {
  763. verifyFormat("var array = [\n"
  764. " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
  765. " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
  766. "];");
  767. verifyFormat("var array = someFunction([\n"
  768. " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
  769. " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
  770. "]);");
  771. }
  772. TEST_F(FormatTestJS, FunctionLiterals) {
  773. FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
  774. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  775. verifyFormat("doFoo(function() {});");
  776. verifyFormat("doFoo(function() { return 1; });", Style);
  777. verifyFormat("var func = function() {\n"
  778. " return 1;\n"
  779. "};");
  780. verifyFormat("var func = //\n"
  781. " function() {\n"
  782. " return 1;\n"
  783. "};");
  784. verifyFormat("return {\n"
  785. " body: {\n"
  786. " setAttribute: function(key, val) { this[key] = val; },\n"
  787. " getAttribute: function(key) { return this[key]; },\n"
  788. " style: {direction: ''}\n"
  789. " }\n"
  790. "};",
  791. Style);
  792. verifyFormat("abc = xyz ? function() {\n"
  793. " return 1;\n"
  794. "} : function() {\n"
  795. " return -1;\n"
  796. "};");
  797. verifyFormat("var closure = goog.bind(\n"
  798. " function() { // comment\n"
  799. " foo();\n"
  800. " bar();\n"
  801. " },\n"
  802. " this, arg1IsReallyLongAndNeedsLineBreaks,\n"
  803. " arg3IsReallyLongAndNeedsLineBreaks);");
  804. verifyFormat("var closure = goog.bind(function() { // comment\n"
  805. " foo();\n"
  806. " bar();\n"
  807. "}, this);");
  808. verifyFormat("return {\n"
  809. " a: 'E',\n"
  810. " b: function() {\n"
  811. " return function() {\n"
  812. " f(); //\n"
  813. " };\n"
  814. " }\n"
  815. "};");
  816. verifyFormat("{\n"
  817. " var someVariable = function(x) {\n"
  818. " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
  819. " };\n"
  820. "}");
  821. verifyFormat("someLooooooooongFunction(\n"
  822. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  823. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  824. " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  825. " // code\n"
  826. " });");
  827. verifyFormat("return {\n"
  828. " a: function SomeFunction() {\n"
  829. " // ...\n"
  830. " return 1;\n"
  831. " }\n"
  832. "};");
  833. verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  834. " .then(goog.bind(function(aaaaaaaaaaa) {\n"
  835. " someFunction();\n"
  836. " someFunction();\n"
  837. " }, this), aaaaaaaaaaaaaaaaa);");
  838. verifyFormat("someFunction(goog.bind(function() {\n"
  839. " doSomething();\n"
  840. " doSomething();\n"
  841. "}, this), goog.bind(function() {\n"
  842. " doSomething();\n"
  843. " doSomething();\n"
  844. "}, this));");
  845. verifyFormat("SomeFunction(function() {\n"
  846. " foo();\n"
  847. " bar();\n"
  848. "}.bind(this));");
  849. verifyFormat("SomeFunction((function() {\n"
  850. " foo();\n"
  851. " bar();\n"
  852. " }).bind(this));");
  853. // FIXME: This is bad, we should be wrapping before "function() {".
  854. verifyFormat("someFunction(function() {\n"
  855. " doSomething(); // break\n"
  856. "})\n"
  857. " .doSomethingElse(\n"
  858. " // break\n"
  859. " );");
  860. Style.ColumnLimit = 33;
  861. verifyFormat("f({a: function() { return 1; }});", Style);
  862. Style.ColumnLimit = 32;
  863. verifyFormat("f({\n"
  864. " a: function() { return 1; }\n"
  865. "});",
  866. Style);
  867. }
  868. TEST_F(FormatTestJS, DontWrapEmptyLiterals) {
  869. verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n"
  870. " .and.returnValue(Observable.of([]));");
  871. verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n"
  872. " .and.returnValue(Observable.of({}));");
  873. verifyFormat("(aaaaaaaaaaaaaaaaaaaaa.getData as jasmine.Spy)\n"
  874. " .and.returnValue(Observable.of(()));");
  875. }
  876. TEST_F(FormatTestJS, InliningFunctionLiterals) {
  877. FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
  878. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  879. verifyFormat("var func = function() {\n"
  880. " return 1;\n"
  881. "};",
  882. Style);
  883. verifyFormat("var func = doSomething(function() { return 1; });", Style);
  884. verifyFormat("var outer = function() {\n"
  885. " var inner = function() { return 1; }\n"
  886. "};",
  887. Style);
  888. verifyFormat("function outer1(a, b) {\n"
  889. " function inner1(a, b) { return a; }\n"
  890. "}",
  891. Style);
  892. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  893. verifyFormat("var func = function() { return 1; };", Style);
  894. verifyFormat("var func = doSomething(function() { return 1; });", Style);
  895. verifyFormat(
  896. "var outer = function() { var inner = function() { return 1; } };",
  897. Style);
  898. verifyFormat("function outer1(a, b) {\n"
  899. " function inner1(a, b) { return a; }\n"
  900. "}",
  901. Style);
  902. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  903. verifyFormat("var func = function() {\n"
  904. " return 1;\n"
  905. "};",
  906. Style);
  907. verifyFormat("var func = doSomething(function() {\n"
  908. " return 1;\n"
  909. "});",
  910. Style);
  911. verifyFormat("var outer = function() {\n"
  912. " var inner = function() {\n"
  913. " return 1;\n"
  914. " }\n"
  915. "};",
  916. Style);
  917. verifyFormat("function outer1(a, b) {\n"
  918. " function inner1(a, b) {\n"
  919. " return a;\n"
  920. " }\n"
  921. "}",
  922. Style);
  923. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
  924. verifyFormat("var func = function() {\n"
  925. " return 1;\n"
  926. "};",
  927. Style);
  928. }
  929. TEST_F(FormatTestJS, MultipleFunctionLiterals) {
  930. FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
  931. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  932. verifyFormat("promise.then(\n"
  933. " function success() {\n"
  934. " doFoo();\n"
  935. " doBar();\n"
  936. " },\n"
  937. " function error() {\n"
  938. " doFoo();\n"
  939. " doBaz();\n"
  940. " },\n"
  941. " []);\n");
  942. verifyFormat("promise.then(\n"
  943. " function success() {\n"
  944. " doFoo();\n"
  945. " doBar();\n"
  946. " },\n"
  947. " [],\n"
  948. " function error() {\n"
  949. " doFoo();\n"
  950. " doBaz();\n"
  951. " });\n");
  952. verifyFormat("promise.then(\n"
  953. " [],\n"
  954. " function success() {\n"
  955. " doFoo();\n"
  956. " doBar();\n"
  957. " },\n"
  958. " function error() {\n"
  959. " doFoo();\n"
  960. " doBaz();\n"
  961. " });\n");
  962. verifyFormat("getSomeLongPromise()\n"
  963. " .then(function(value) { body(); })\n"
  964. " .thenCatch(function(error) {\n"
  965. " body();\n"
  966. " body();\n"
  967. " });",
  968. Style);
  969. verifyFormat("getSomeLongPromise()\n"
  970. " .then(function(value) {\n"
  971. " body();\n"
  972. " body();\n"
  973. " })\n"
  974. " .thenCatch(function(error) {\n"
  975. " body();\n"
  976. " body();\n"
  977. " });");
  978. verifyFormat("getSomeLongPromise()\n"
  979. " .then(function(value) { body(); })\n"
  980. " .thenCatch(function(error) { body(); });",
  981. Style);
  982. verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n"
  983. " .aaaaaaa(function() {\n"
  984. " //\n"
  985. " })\n"
  986. " .bbbbbb();");
  987. }
  988. TEST_F(FormatTestJS, ArrowFunctions) {
  989. verifyFormat("var x = (a) => {\n"
  990. " return a;\n"
  991. "};");
  992. verifyFormat("var x = (a) => {\n"
  993. " function y() {\n"
  994. " return 42;\n"
  995. " }\n"
  996. " return a;\n"
  997. "};");
  998. verifyFormat("var x = (a: type): {some: type} => {\n"
  999. " return a;\n"
  1000. "};");
  1001. verifyFormat("var x = (a) => a;");
  1002. verifyFormat("return () => [];");
  1003. verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n"
  1004. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
  1005. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1006. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n"
  1007. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1008. "};");
  1009. verifyFormat("var a = a.aaaaaaa(\n"
  1010. " (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n"
  1011. " aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
  1012. verifyFormat("var a = a.aaaaaaa(\n"
  1013. " (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n"
  1014. " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n"
  1015. " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
  1016. // FIXME: This is bad, we should be wrapping before "() => {".
  1017. verifyFormat("someFunction(() => {\n"
  1018. " doSomething(); // break\n"
  1019. "})\n"
  1020. " .doSomethingElse(\n"
  1021. " // break\n"
  1022. " );");
  1023. verifyFormat("const f = (x: string|null): string|null => {\n"
  1024. " return x;\n"
  1025. "}\n");
  1026. }
  1027. TEST_F(FormatTestJS, ReturnStatements) {
  1028. verifyFormat("function() {\n"
  1029. " return [hello, world];\n"
  1030. "}");
  1031. }
  1032. TEST_F(FormatTestJS, ForLoops) {
  1033. verifyFormat("for (var i in [2, 3]) {\n"
  1034. "}");
  1035. verifyFormat("for (var i of [2, 3]) {\n"
  1036. "}");
  1037. verifyFormat("for (let {a, b} of x) {\n"
  1038. "}");
  1039. verifyFormat("for (let {a, b} of [x]) {\n"
  1040. "}");
  1041. verifyFormat("for (let [a, b] of [x]) {\n"
  1042. "}");
  1043. verifyFormat("for (let {a, b} in x) {\n"
  1044. "}");
  1045. }
  1046. TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) {
  1047. // The following statements must not wrap, as otherwise the program meaning
  1048. // would change due to automatic semicolon insertion.
  1049. // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1.
  1050. verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10));
  1051. verifyFormat("yield aaaaa;", getGoogleJSStyleWithColumns(10));
  1052. verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
  1053. verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10));
  1054. verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
  1055. verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10));
  1056. verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10));
  1057. verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10));
  1058. verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10));
  1059. verifyFormat("return [\n"
  1060. " aaa\n"
  1061. "];",
  1062. getGoogleJSStyleWithColumns(12));
  1063. verifyFormat("class X {\n"
  1064. " readonly ratherLongField =\n"
  1065. " 1;\n"
  1066. "}",
  1067. "class X {\n"
  1068. " readonly ratherLongField = 1;\n"
  1069. "}",
  1070. getGoogleJSStyleWithColumns(20));
  1071. verifyFormat("const x = (5 + 9)\n"
  1072. "const y = 3\n",
  1073. "const x = ( 5 + 9)\n"
  1074. "const y = 3\n");
  1075. // Ideally the foo() bit should be indented relative to the async function().
  1076. verifyFormat("async function\n"
  1077. "foo() {}",
  1078. getGoogleJSStyleWithColumns(10));
  1079. verifyFormat("await theReckoning;", getGoogleJSStyleWithColumns(10));
  1080. verifyFormat("some['a']['b']", getGoogleJSStyleWithColumns(10));
  1081. verifyFormat("x = (a['a']\n"
  1082. " ['b']);", getGoogleJSStyleWithColumns(10));
  1083. }
  1084. TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {
  1085. verifyFormat("a\n"
  1086. "b;",
  1087. " a \n"
  1088. " b ;");
  1089. verifyFormat("a()\n"
  1090. "b;",
  1091. " a ()\n"
  1092. " b ;");
  1093. verifyFormat("a[b]\n"
  1094. "c;",
  1095. "a [b]\n"
  1096. "c ;");
  1097. verifyFormat("1\n"
  1098. "a;",
  1099. "1 \n"
  1100. "a ;");
  1101. verifyFormat("a\n"
  1102. "1;",
  1103. "a \n"
  1104. "1 ;");
  1105. verifyFormat("a\n"
  1106. "'x';",
  1107. "a \n"
  1108. " 'x';");
  1109. verifyFormat("a++\n"
  1110. "b;",
  1111. "a ++\n"
  1112. "b ;");
  1113. verifyFormat("a\n"
  1114. "!b && c;",
  1115. "a \n"
  1116. " ! b && c;");
  1117. verifyFormat("a\n"
  1118. "if (1) f();",
  1119. " a\n"
  1120. " if (1) f();");
  1121. verifyFormat("a\n"
  1122. "class X {}",
  1123. " a\n"
  1124. " class X {}");
  1125. verifyFormat("var a", "var\n"
  1126. "a");
  1127. verifyFormat("x instanceof String", "x\n"
  1128. "instanceof\n"
  1129. "String");
  1130. verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n"
  1131. " bar) {}");
  1132. verifyFormat("function f(@Foo(Param) bar) {}", "function f(@Foo(Param)\n"
  1133. " bar) {}");
  1134. verifyFormat("a = true\n"
  1135. "return 1",
  1136. "a = true\n"
  1137. " return 1");
  1138. verifyFormat("a = 's'\n"
  1139. "return 1",
  1140. "a = 's'\n"
  1141. " return 1");
  1142. verifyFormat("a = null\n"
  1143. "return 1",
  1144. "a = null\n"
  1145. " return 1");
  1146. // Below "class Y {}" should ideally be on its own line.
  1147. verifyFormat(
  1148. "x = {\n"
  1149. " a: 1\n"
  1150. "} class Y {}",
  1151. " x = {a : 1}\n"
  1152. " class Y { }");
  1153. verifyFormat(
  1154. "if (x) {\n"
  1155. "}\n"
  1156. "return 1",
  1157. "if (x) {}\n"
  1158. " return 1");
  1159. verifyFormat(
  1160. "if (x) {\n"
  1161. "}\n"
  1162. "class X {}",
  1163. "if (x) {}\n"
  1164. " class X {}");
  1165. }
  1166. TEST_F(FormatTestJS, ImportExportASI) {
  1167. verifyFormat(
  1168. "import {x} from 'y'\n"
  1169. "export function z() {}",
  1170. "import {x} from 'y'\n"
  1171. " export function z() {}");
  1172. // Below "class Y {}" should ideally be on its own line.
  1173. verifyFormat(
  1174. "export {x} class Y {}",
  1175. " export {x}\n"
  1176. " class Y {\n}");
  1177. verifyFormat(
  1178. "if (x) {\n"
  1179. "}\n"
  1180. "export class Y {}",
  1181. "if ( x ) { }\n"
  1182. " export class Y {}");
  1183. }
  1184. TEST_F(FormatTestJS, ClosureStyleCasts) {
  1185. verifyFormat("var x = /** @type {foo} */ (bar);");
  1186. }
  1187. TEST_F(FormatTestJS, TryCatch) {
  1188. verifyFormat("try {\n"
  1189. " f();\n"
  1190. "} catch (e) {\n"
  1191. " g();\n"
  1192. "} finally {\n"
  1193. " h();\n"
  1194. "}");
  1195. // But, of course, "catch" is a perfectly fine function name in JavaScript.
  1196. verifyFormat("someObject.catch();");
  1197. verifyFormat("someObject.new();");
  1198. }
  1199. TEST_F(FormatTestJS, StringLiteralConcatenation) {
  1200. verifyFormat("var literal = 'hello ' +\n"
  1201. " 'world';");
  1202. }
  1203. TEST_F(FormatTestJS, RegexLiteralClassification) {
  1204. // Regex literals.
  1205. verifyFormat("var regex = /abc/;");
  1206. verifyFormat("f(/abc/);");
  1207. verifyFormat("f(abc, /abc/);");
  1208. verifyFormat("some_map[/abc/];");
  1209. verifyFormat("var x = a ? /abc/ : /abc/;");
  1210. verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
  1211. verifyFormat("var x = !/abc/.test(y);");
  1212. verifyFormat("var x = foo()! / 10;");
  1213. verifyFormat("var x = a && /abc/.test(y);");
  1214. verifyFormat("var x = a || /abc/.test(y);");
  1215. verifyFormat("var x = a + /abc/.search(y);");
  1216. verifyFormat("/abc/.search(y);");
  1217. verifyFormat("var regexs = {/abc/, /abc/};");
  1218. verifyFormat("return /abc/;");
  1219. // Not regex literals.
  1220. verifyFormat("var a = a / 2 + b / 3;");
  1221. verifyFormat("var a = a++ / 2;");
  1222. // Prefix unary can operate on regex literals, not that it makes sense.
  1223. verifyFormat("var a = ++/a/;");
  1224. // This is a known issue, regular expressions are incorrectly detected if
  1225. // directly following a closing parenthesis.
  1226. verifyFormat("if (foo) / bar /.exec(baz);");
  1227. }
  1228. TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
  1229. verifyFormat("var regex = /=/;");
  1230. verifyFormat("var regex = /a*/;");
  1231. verifyFormat("var regex = /a+/;");
  1232. verifyFormat("var regex = /a?/;");
  1233. verifyFormat("var regex = /.a./;");
  1234. verifyFormat("var regex = /a\\*/;");
  1235. verifyFormat("var regex = /^a$/;");
  1236. verifyFormat("var regex = /\\/a/;");
  1237. verifyFormat("var regex = /(?:x)/;");
  1238. verifyFormat("var regex = /x(?=y)/;");
  1239. verifyFormat("var regex = /x(?!y)/;");
  1240. verifyFormat("var regex = /x|y/;");
  1241. verifyFormat("var regex = /a{2}/;");
  1242. verifyFormat("var regex = /a{1,3}/;");
  1243. verifyFormat("var regex = /[abc]/;");
  1244. verifyFormat("var regex = /[^abc]/;");
  1245. verifyFormat("var regex = /[\\b]/;");
  1246. verifyFormat("var regex = /[/]/;");
  1247. verifyFormat("var regex = /[\\/]/;");
  1248. verifyFormat("var regex = /\\[/;");
  1249. verifyFormat("var regex = /\\\\[/]/;");
  1250. verifyFormat("var regex = /}[\"]/;");
  1251. verifyFormat("var regex = /}[/\"]/;");
  1252. verifyFormat("var regex = /}[\"/]/;");
  1253. verifyFormat("var regex = /\\b/;");
  1254. verifyFormat("var regex = /\\B/;");
  1255. verifyFormat("var regex = /\\d/;");
  1256. verifyFormat("var regex = /\\D/;");
  1257. verifyFormat("var regex = /\\f/;");
  1258. verifyFormat("var regex = /\\n/;");
  1259. verifyFormat("var regex = /\\r/;");
  1260. verifyFormat("var regex = /\\s/;");
  1261. verifyFormat("var regex = /\\S/;");
  1262. verifyFormat("var regex = /\\t/;");
  1263. verifyFormat("var regex = /\\v/;");
  1264. verifyFormat("var regex = /\\w/;");
  1265. verifyFormat("var regex = /\\W/;");
  1266. verifyFormat("var regex = /a(a)\\1/;");
  1267. verifyFormat("var regex = /\\0/;");
  1268. verifyFormat("var regex = /\\\\/g;");
  1269. verifyFormat("var regex = /\\a\\\\/g;");
  1270. verifyFormat("var regex = /\a\\//g;");
  1271. verifyFormat("var regex = /a\\//;\n"
  1272. "var x = 0;");
  1273. verifyFormat("var regex = /'/g;", "var regex = /'/g ;");
  1274. verifyFormat("var regex = /'/g; //'", "var regex = /'/g ; //'");
  1275. verifyFormat("var regex = /\\/*/;\n"
  1276. "var x = 0;",
  1277. "var regex = /\\/*/;\n"
  1278. "var x=0;");
  1279. verifyFormat("var x = /a\\//;", "var x = /a\\// \n;");
  1280. verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16));
  1281. verifyFormat("var regex =\n"
  1282. " /\"/;",
  1283. getGoogleJSStyleWithColumns(15));
  1284. verifyFormat("var regex = //\n"
  1285. " /a/;");
  1286. verifyFormat("var regexs = [\n"
  1287. " /d/, //\n"
  1288. " /aa/, //\n"
  1289. "];");
  1290. }
  1291. TEST_F(FormatTestJS, RegexLiteralModifiers) {
  1292. verifyFormat("var regex = /abc/g;");
  1293. verifyFormat("var regex = /abc/i;");
  1294. verifyFormat("var regex = /abc/m;");
  1295. verifyFormat("var regex = /abc/y;");
  1296. }
  1297. TEST_F(FormatTestJS, RegexLiteralLength) {
  1298. verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
  1299. getGoogleJSStyleWithColumns(60));
  1300. verifyFormat("var regex =\n"
  1301. " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
  1302. getGoogleJSStyleWithColumns(60));
  1303. verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
  1304. getGoogleJSStyleWithColumns(50));
  1305. }
  1306. TEST_F(FormatTestJS, RegexLiteralExamples) {
  1307. verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
  1308. }
  1309. TEST_F(FormatTestJS, IgnoresMpegTS) {
  1310. std::string MpegTS(200, ' ');
  1311. MpegTS.replace(0, strlen("nearlyLooks + like + ts + code; "),
  1312. "nearlyLooks + like + ts + code; ");
  1313. MpegTS[0] = 0x47;
  1314. MpegTS[188] = 0x47;
  1315. verifyFormat(MpegTS, MpegTS);
  1316. }
  1317. TEST_F(FormatTestJS, TypeAnnotations) {
  1318. verifyFormat("var x: string;");
  1319. verifyFormat("var x: {a: string; b: number;} = {};");
  1320. verifyFormat("function x(): string {\n return 'x';\n}");
  1321. verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}");
  1322. verifyFormat("function x(y: string): string {\n return 'x';\n}");
  1323. verifyFormat("for (var y: string in x) {\n x();\n}");
  1324. verifyFormat("for (var y: string of x) {\n x();\n}");
  1325. verifyFormat("function x(y: {a?: number;} = {}): number {\n"
  1326. " return 12;\n"
  1327. "}");
  1328. verifyFormat("const x: Array<{a: number; b: string;}> = [];");
  1329. verifyFormat("((a: string, b: number): string => a + b);");
  1330. verifyFormat("var x: (y: number) => string;");
  1331. verifyFormat("var x: P<string, (a: number) => string>;");
  1332. verifyFormat("var x = {\n"
  1333. " y: function(): z {\n"
  1334. " return 1;\n"
  1335. " }\n"
  1336. "};");
  1337. verifyFormat("var x = {\n"
  1338. " y: function(): {a: number} {\n"
  1339. " return 1;\n"
  1340. " }\n"
  1341. "};");
  1342. verifyFormat("function someFunc(args: string[]):\n"
  1343. " {longReturnValue: string[]} {}",
  1344. getGoogleJSStyleWithColumns(60));
  1345. verifyFormat(
  1346. "var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])\n"
  1347. " .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  1348. verifyFormat("const xIsALongIdent:\n"" YJustBarelyFitsLinex[];",
  1349. getGoogleJSStyleWithColumns(20));
  1350. }
  1351. TEST_F(FormatTestJS, UnionIntersectionTypes) {
  1352. verifyFormat("let x: A|B = A | B;");
  1353. verifyFormat("let x: A&B|C = A & B;");
  1354. verifyFormat("let x: Foo<A|B> = new Foo<A|B>();");
  1355. verifyFormat("function(x: A|B): C&D {}");
  1356. verifyFormat("function(x: A|B = A | B): C&D {}");
  1357. verifyFormat("function x(path: number|string) {}");
  1358. verifyFormat("function x(): string|number {}");
  1359. verifyFormat("type Foo = Bar|Baz;");
  1360. verifyFormat("type Foo = Bar<X>|Baz;");
  1361. verifyFormat("type Foo = (Bar<X>|Baz);");
  1362. verifyFormat("let x: Bar|Baz;");
  1363. verifyFormat("let x: Bar<X>|Baz;");
  1364. verifyFormat("let x: (Foo|Bar)[];");
  1365. verifyFormat("type X = {\n"
  1366. " a: Foo|Bar;\n"
  1367. "};");
  1368. verifyFormat("export type X = {\n"
  1369. " a: Foo|Bar;\n"
  1370. "};");
  1371. }
  1372. TEST_F(FormatTestJS, UnionIntersectionTypesInObjectType) {
  1373. verifyFormat("let x: {x: number|null} = {x: number | null};");
  1374. verifyFormat("let nested: {x: {y: number|null}};");
  1375. verifyFormat("let mixed: {x: [number|null, {w: number}]};");
  1376. verifyFormat("class X {\n"
  1377. " contructor(x: {\n"
  1378. " a: a|null,\n"
  1379. " b: b|null,\n"
  1380. " }) {}\n"
  1381. "}");
  1382. }
  1383. TEST_F(FormatTestJS, ClassDeclarations) {
  1384. verifyFormat("class C {\n x: string = 12;\n}");
  1385. verifyFormat("class C {\n x(): string => 12;\n}");
  1386. verifyFormat("class C {\n ['x' + 2]: string = 12;\n}");
  1387. verifyFormat("class C {\n"
  1388. " foo() {}\n"
  1389. " [bar]() {}\n"
  1390. "}\n");
  1391. verifyFormat("class C {\n private x: string = 12;\n}");
  1392. verifyFormat("class C {\n private static x: string = 12;\n}");
  1393. verifyFormat("class C {\n static x(): string {\n return 'asd';\n }\n}");
  1394. verifyFormat("class C extends P implements I {}");
  1395. verifyFormat("class C extends p.P implements i.I {}");
  1396. verifyFormat(
  1397. "x(class {\n"
  1398. " a(): A {}\n"
  1399. "});");
  1400. verifyFormat("class Test {\n"
  1401. " aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n"
  1402. " aaaaaaaaaaaaaaaaaaaaaa {}\n"
  1403. "}");
  1404. verifyFormat("foo = class Name {\n"
  1405. " constructor() {}\n"
  1406. "};");
  1407. verifyFormat("foo = class {\n"
  1408. " constructor() {}\n"
  1409. "};");
  1410. verifyFormat("class C {\n"
  1411. " x: {y: Z;} = {};\n"
  1412. " private y: {y: Z;} = {};\n"
  1413. "}");
  1414. // ':' is not a type declaration here.
  1415. verifyFormat("class X {\n"
  1416. " subs = {\n"
  1417. " 'b': {\n"
  1418. " 'c': 1,\n"
  1419. " },\n"
  1420. " };\n"
  1421. "}");
  1422. verifyFormat("@Component({\n"
  1423. " moduleId: module.id,\n"
  1424. "})\n"
  1425. "class SessionListComponent implements OnDestroy, OnInit {\n"
  1426. "}");
  1427. }
  1428. TEST_F(FormatTestJS, InterfaceDeclarations) {
  1429. verifyFormat("interface I {\n"
  1430. " x: string;\n"
  1431. " enum: string[];\n"
  1432. " enum?: string[];\n"
  1433. "}\n"
  1434. "var y;");
  1435. // Ensure that state is reset after parsing the interface.
  1436. verifyFormat("interface a {}\n"
  1437. "export function b() {}\n"
  1438. "var x;");
  1439. // Arrays of object type literals.
  1440. verifyFormat("interface I {\n"
  1441. " o: {}[];\n"
  1442. "}");
  1443. }
  1444. TEST_F(FormatTestJS, ObjectTypesInExtendsImplements) {
  1445. verifyFormat("class C extends {} {}");
  1446. verifyFormat("class C implements {bar: number} {}");
  1447. // Somewhat odd, but probably closest to reasonable formatting?
  1448. verifyFormat("class C implements {\n"
  1449. " bar: number,\n"
  1450. " baz: string,\n"
  1451. "} {}");
  1452. verifyFormat("class C<P extends {}> {}");
  1453. }
  1454. TEST_F(FormatTestJS, EnumDeclarations) {
  1455. verifyFormat("enum Foo {\n"
  1456. " A = 1,\n"
  1457. " B\n"
  1458. "}");
  1459. verifyFormat("export /* somecomment*/ enum Foo {\n"
  1460. " A = 1,\n"
  1461. " B\n"
  1462. "}");
  1463. verifyFormat("enum Foo {\n"
  1464. " A = 1, // comment\n"
  1465. " B\n"
  1466. "}\n"
  1467. "var x = 1;");
  1468. verifyFormat("const enum Foo {\n"
  1469. " A = 1,\n"
  1470. " B\n"
  1471. "}");
  1472. verifyFormat("export const enum Foo {\n"
  1473. " A = 1,\n"
  1474. " B\n"
  1475. "}");
  1476. }
  1477. TEST_F(FormatTestJS, Decorators) {
  1478. verifyFormat("@A\nclass C {\n}");
  1479. verifyFormat("@A({arg: 'value'})\nclass C {\n}");
  1480. verifyFormat("@A\n@B\nclass C {\n}");
  1481. verifyFormat("class C {\n @A x: string;\n}");
  1482. verifyFormat("class C {\n"
  1483. " @A\n"
  1484. " private x(): string {\n"
  1485. " return 'y';\n"
  1486. " }\n"
  1487. "}");
  1488. verifyFormat("class C {\n"
  1489. " private x(@A x: string) {}\n"
  1490. "}");
  1491. verifyFormat("class X {}\n"
  1492. "class Y {}");
  1493. verifyFormat("class X {\n"
  1494. " @property() private isReply = false;\n"
  1495. "}\n");
  1496. }
  1497. TEST_F(FormatTestJS, TypeAliases) {
  1498. verifyFormat("type X = number;\n"
  1499. "class C {}");
  1500. verifyFormat("type X<Y> = Z<Y>;");
  1501. verifyFormat("type X = {\n"
  1502. " y: number\n"
  1503. "};\n"
  1504. "class C {}");
  1505. verifyFormat("export type X = {\n"
  1506. " a: string,\n"
  1507. " b?: string,\n"
  1508. "};\n");
  1509. }
  1510. TEST_F(FormatTestJS, TypeInterfaceLineWrapping) {
  1511. const FormatStyle &Style = getGoogleJSStyleWithColumns(20);
  1512. verifyFormat("type LongTypeIsReallyUnreasonablyLong =\n"
  1513. " string;\n",
  1514. "type LongTypeIsReallyUnreasonablyLong = string;\n",
  1515. Style);
  1516. verifyFormat(
  1517. "interface AbstractStrategyFactoryProvider {\n"
  1518. " a: number\n"
  1519. "}\n",
  1520. "interface AbstractStrategyFactoryProvider { a: number }\n",
  1521. Style);
  1522. }
  1523. TEST_F(FormatTestJS, RemoveEmptyLinesInArrowFunctions) {
  1524. verifyFormat("x = () => {\n"
  1525. " foo();\n"
  1526. "};\n",
  1527. "x = () => {\n"
  1528. "\n"
  1529. " foo();\n"
  1530. "\n"
  1531. "};\n");
  1532. }
  1533. TEST_F(FormatTestJS, Modules) {
  1534. verifyFormat("import SomeThing from 'some/module.js';");
  1535. verifyFormat("import {X, Y} from 'some/module.js';");
  1536. verifyFormat("import a, {X, Y} from 'some/module.js';");
  1537. verifyFormat("import {X, Y,} from 'some/module.js';");
  1538. verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
  1539. // Ensure Automatic Semicolon Insertion does not break on "as\n".
  1540. verifyFormat("import {X as myX} from 'm';", "import {X as\n"
  1541. " myX} from 'm';");
  1542. verifyFormat("import * as lib from 'some/module.js';");
  1543. verifyFormat("var x = {import: 1};\nx.import = 2;");
  1544. verifyFormat("export function fn() {\n"
  1545. " return 'fn';\n"
  1546. "}");
  1547. verifyFormat("export function A() {}\n"
  1548. "export default function B() {}\n"
  1549. "export function C() {}");
  1550. verifyFormat("export default () => {\n"
  1551. " let x = 1;\n"
  1552. " return x;\n"
  1553. "}");
  1554. verifyFormat("export const x = 12;");
  1555. verifyFormat("export default class X {}");
  1556. verifyFormat("export {X, Y} from 'some/module.js';");
  1557. verifyFormat("export {X, Y,} from 'some/module.js';");
  1558. verifyFormat("export {SomeVeryLongExport as X, "
  1559. "SomeOtherVeryLongExport as Y} from 'some/module.js';");
  1560. // export without 'from' is wrapped.
  1561. verifyFormat("export let someRatherLongVariableName =\n"
  1562. " someSurprisinglyLongVariable + someOtherRatherLongVar;");
  1563. // ... but not if from is just an identifier.
  1564. verifyFormat("export {\n"
  1565. " from as from,\n"
  1566. " someSurprisinglyLongVariable as\n"
  1567. " from\n"
  1568. "};",
  1569. getGoogleJSStyleWithColumns(20));
  1570. verifyFormat("export class C {\n"
  1571. " x: number;\n"
  1572. " y: string;\n"
  1573. "}");
  1574. verifyFormat("export class X {\n"
  1575. " y: number;\n"
  1576. "}");
  1577. verifyFormat("export abstract class X {\n"
  1578. " y: number;\n"
  1579. "}");
  1580. verifyFormat("export default class X {\n"
  1581. " y: number\n"
  1582. "}");
  1583. verifyFormat("export default function() {\n return 1;\n}");
  1584. verifyFormat("export var x = 12;");
  1585. verifyFormat("class C {}\n"
  1586. "export function f() {}\n"
  1587. "var v;");
  1588. verifyFormat("export var x: number = 12;");
  1589. verifyFormat("export const y = {\n"
  1590. " a: 1,\n"
  1591. " b: 2\n"
  1592. "};");
  1593. verifyFormat("export enum Foo {\n"
  1594. " BAR,\n"
  1595. " // adsdasd\n"
  1596. " BAZ\n"
  1597. "}");
  1598. verifyFormat("export default [\n"
  1599. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1600. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  1601. "];");
  1602. verifyFormat("export default [];");
  1603. verifyFormat("export default () => {};");
  1604. verifyFormat("export interface Foo {\n"
  1605. " foo: number;\n"
  1606. "}\n"
  1607. "export class Bar {\n"
  1608. " blah(): string {\n"
  1609. " return this.blah;\n"
  1610. " };\n"
  1611. "}");
  1612. }
  1613. TEST_F(FormatTestJS, ImportWrapping) {
  1614. verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,"
  1615. " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying"
  1616. "} from 'some/module.js';");
  1617. FormatStyle Style = getGoogleJSStyleWithColumns(80);
  1618. Style.JavaScriptWrapImports = true;
  1619. verifyFormat("import {\n"
  1620. " VeryLongImportsAreAnnoying,\n"
  1621. " VeryLongImportsAreAnnoying,\n"
  1622. " VeryLongImportsAreAnnoying,\n"
  1623. "} from 'some/module.js';",
  1624. Style);
  1625. verifyFormat("import {\n"
  1626. " A,\n"
  1627. " A,\n"
  1628. "} from 'some/module.js';",
  1629. Style);
  1630. verifyFormat("export {\n"
  1631. " A,\n"
  1632. " A,\n"
  1633. "} from 'some/module.js';",
  1634. Style);
  1635. Style.ColumnLimit = 40;
  1636. // Using this version of verifyFormat because test::messUp hides the issue.
  1637. verifyFormat("import {\n"
  1638. " A,\n"
  1639. "} from\n"
  1640. " 'some/path/longer/than/column/limit/module.js';",
  1641. " import { \n"
  1642. " A, \n"
  1643. " } from\n"
  1644. " 'some/path/longer/than/column/limit/module.js' ; ",
  1645. Style);
  1646. }
  1647. TEST_F(FormatTestJS, TemplateStrings) {
  1648. // Keeps any whitespace/indentation within the template string.
  1649. verifyFormat("var x = `hello\n"
  1650. " ${name}\n"
  1651. " !`;",
  1652. "var x = `hello\n"
  1653. " ${ name }\n"
  1654. " !`;");
  1655. verifyFormat("var x =\n"
  1656. " `hello ${world}` >= some();",
  1657. getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
  1658. verifyFormat("var x = `hello ${world}` >= some();",
  1659. getGoogleJSStyleWithColumns(35)); // Barely fits.
  1660. verifyFormat("var x = `hellö ${wörld}` >= söme();",
  1661. getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8.
  1662. verifyFormat("var x = `hello\n"
  1663. " ${world}` >=\n"
  1664. " some();",
  1665. "var x =\n"
  1666. " `hello\n"
  1667. " ${world}` >= some();",
  1668. getGoogleJSStyleWithColumns(21)); // Barely doesn't fit.
  1669. verifyFormat("var x = `hello\n"
  1670. " ${world}` >= some();",
  1671. "var x =\n"
  1672. " `hello\n"
  1673. " ${world}` >= some();",
  1674. getGoogleJSStyleWithColumns(22)); // Barely fits.
  1675. verifyFormat("var x =\n"
  1676. " `h`;",
  1677. getGoogleJSStyleWithColumns(11));
  1678. verifyFormat("var x =\n `multi\n line`;", "var x = `multi\n line`;",
  1679. getGoogleJSStyleWithColumns(13));
  1680. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1681. " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);");
  1682. // Repro for an obscure width-miscounting issue with template strings.
  1683. verifyFormat(
  1684. "someLongVariable =\n"
  1685. " "
  1686. "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;",
  1687. "someLongVariable = "
  1688. "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;");
  1689. // Make sure template strings get a proper ColumnWidth assigned, even if they
  1690. // are first token in line.
  1691. verifyFormat(
  1692. "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  1693. " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;");
  1694. // Two template strings.
  1695. verifyFormat("var x = `hello` == `hello`;");
  1696. // Comments in template strings.
  1697. verifyFormat("var x = `//a`;\n"
  1698. "var y;",
  1699. "var x =\n `//a`;\n"
  1700. "var y ;");
  1701. verifyFormat("var x = `/*a`;\n"
  1702. "var y;",
  1703. "var x =\n `/*a`;\n"
  1704. "var y;");
  1705. // Unterminated string literals in a template string.
  1706. verifyFormat("var x = `'`; // comment with matching quote '\n"
  1707. "var y;");
  1708. verifyFormat("var x = `\"`; // comment with matching quote \"\n"
  1709. "var y;");
  1710. verifyFormat("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa);",
  1711. "it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa) ;",
  1712. getGoogleJSStyleWithColumns(40));
  1713. // Backticks in a comment - not a template string.
  1714. verifyFormat("var x = 1 // `/*a`;\n"
  1715. " ;",
  1716. "var x =\n 1 // `/*a`;\n"
  1717. " ;");
  1718. verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */");
  1719. // Comment spans multiple template strings.
  1720. verifyFormat("var x = `/*a`;\n"
  1721. "var y = ` */ `;",
  1722. "var x =\n `/*a`;\n"
  1723. "var y =\n ` */ `;");
  1724. // Escaped backtick.
  1725. verifyFormat("var x = ` \\` a`;\n"
  1726. "var y;",
  1727. "var x = ` \\` a`;\n"
  1728. "var y;");
  1729. // Escaped dollar.
  1730. verifyFormat("var x = ` \\${foo}`;\n");
  1731. // The token stream can contain two string_literals in sequence, but that
  1732. // doesn't mean that they are implicitly concatenated in JavaScript.
  1733. verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;");
  1734. // Ensure that scopes are appropriately set around evaluated expressions in
  1735. // template strings.
  1736. verifyFormat("var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa\n"
  1737. " aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;",
  1738. "var f = `aaaaaaaaaaaaa:${aaaaaaa. aaaaa} aaaaaaaa\n"
  1739. " aaaaaaaaaaaaa:${ aaaaaaa. aaaaa} aaaaaaaa`;");
  1740. verifyFormat("var x = someFunction(`${})`) //\n"
  1741. " .oooooooooooooooooon();");
  1742. verifyFormat("var x = someFunction(`${aaaa}${\n"
  1743. " aaaaa( //\n"
  1744. " aaaaa)})`);");
  1745. }
  1746. TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
  1747. verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${\n"
  1748. " aaaaa + //\n"
  1749. " bbbb}`;",
  1750. "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa + //\n"
  1751. " bbbb}`;");
  1752. verifyFormat("var f = `\n"
  1753. " aaaaaaaaaaaaaaaaaa: ${\n"
  1754. " aaaaa + //\n"
  1755. " bbbb}`;",
  1756. "var f = `\n"
  1757. " aaaaaaaaaaaaaaaaaa: ${ aaaaa + //\n"
  1758. " bbbb }`;");
  1759. verifyFormat("var f = `\n"
  1760. " aaaaaaaaaaaaaaaaaa: ${\n"
  1761. " someFunction(\n"
  1762. " aaaaa + //\n"
  1763. " bbbb)}`;",
  1764. "var f = `\n"
  1765. " aaaaaaaaaaaaaaaaaa: ${someFunction (\n"
  1766. " aaaaa + //\n"
  1767. " bbbb)}`;");
  1768. // It might be preferable to wrap before "someFunction".
  1769. verifyFormat("var f = `\n"
  1770. " aaaaaaaaaaaaaaaaaa: ${someFunction({\n"
  1771. " aaaa: aaaaa,\n"
  1772. " bbbb: bbbbb,\n"
  1773. "})}`;",
  1774. "var f = `\n"
  1775. " aaaaaaaaaaaaaaaaaa: ${someFunction ({\n"
  1776. " aaaa: aaaaa,\n"
  1777. " bbbb: bbbbb,\n"
  1778. " })}`;");
  1779. }
  1780. TEST_F(FormatTestJS, TemplateStringASI) {
  1781. verifyFormat("var x = `hello${world}`;", "var x = `hello${\n"
  1782. " world\n"
  1783. "}`;");
  1784. }
  1785. TEST_F(FormatTestJS, NestedTemplateStrings) {
  1786. verifyFormat(
  1787. "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;");
  1788. verifyFormat("var x = `he${({text: 'll'}.text)}o`;");
  1789. // Crashed at some point.
  1790. verifyFormat("}");
  1791. }
  1792. TEST_F(FormatTestJS, TaggedTemplateStrings) {
  1793. verifyFormat("var x = html`<ul>`;");
  1794. verifyFormat("yield `hello`;");
  1795. }
  1796. TEST_F(FormatTestJS, CastSyntax) {
  1797. verifyFormat("var x = <type>foo;");
  1798. verifyFormat("var x = foo as type;");
  1799. verifyFormat("let x = (a + b) as\n"
  1800. " LongTypeIsLong;",
  1801. getGoogleJSStyleWithColumns(20));
  1802. verifyFormat("foo = <Bar[]>[\n"
  1803. " 1, //\n"
  1804. " 2\n"
  1805. "];");
  1806. verifyFormat("var x = [{x: 1} as type];");
  1807. verifyFormat("x = x as [a, b];");
  1808. verifyFormat("x = x as {a: string};");
  1809. verifyFormat("x = x as (string);");
  1810. verifyFormat("x = x! as (string);");
  1811. verifyFormat("x = y! in z;");
  1812. verifyFormat("var x = something.someFunction() as\n"
  1813. " something;",
  1814. getGoogleJSStyleWithColumns(40));
  1815. }
  1816. TEST_F(FormatTestJS, TypeArguments) {
  1817. verifyFormat("class X<Y> {}");
  1818. verifyFormat("new X<Y>();");
  1819. verifyFormat("foo<Y>(a);");
  1820. verifyFormat("var x: X<Y>[];");
  1821. verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
  1822. verifyFormat("function f(a: List<any> = null) {}");
  1823. verifyFormat("function f(): List<any> {}");
  1824. verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n"
  1825. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}");
  1826. verifyFormat("function aaaaaaaaaa(\n"
  1827. " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n"
  1828. " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n"
  1829. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}");
  1830. }
  1831. TEST_F(FormatTestJS, UserDefinedTypeGuards) {
  1832. verifyFormat(
  1833. "function foo(check: Object):\n"
  1834. " check is {foo: string, bar: string, baz: string, foobar: string} {\n"
  1835. " return 'bar' in check;\n"
  1836. "}\n");
  1837. }
  1838. TEST_F(FormatTestJS, OptionalTypes) {
  1839. verifyFormat("function x(a?: b, c?, d?) {}");
  1840. verifyFormat("class X {\n"
  1841. " y?: z;\n"
  1842. " z?;\n"
  1843. "}");
  1844. verifyFormat("interface X {\n"
  1845. " y?(): z;\n"
  1846. "}");
  1847. verifyFormat("constructor({aa}: {\n"
  1848. " aa?: string,\n"
  1849. " aaaaaaaa?: string,\n"
  1850. " aaaaaaaaaaaaaaa?: boolean,\n"
  1851. " aaaaaa?: List<string>\n"
  1852. "}) {}");
  1853. }
  1854. TEST_F(FormatTestJS, IndexSignature) {
  1855. verifyFormat("var x: {[k: string]: v};");
  1856. }
  1857. TEST_F(FormatTestJS, WrapAfterParen) {
  1858. verifyFormat("xxxxxxxxxxx(\n"
  1859. " aaa, aaa);",
  1860. getGoogleJSStyleWithColumns(20));
  1861. verifyFormat("xxxxxxxxxxx(\n"
  1862. " aaa, aaa, aaa,\n"
  1863. " aaa, aaa, aaa);",
  1864. getGoogleJSStyleWithColumns(20));
  1865. verifyFormat("xxxxxxxxxxx(\n"
  1866. " aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1867. " function(x) {\n"
  1868. " y(); //\n"
  1869. " });",
  1870. getGoogleJSStyleWithColumns(40));
  1871. verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  1872. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  1873. }
  1874. TEST_F(FormatTestJS, JSDocAnnotations) {
  1875. verifyFormat("/**\n"
  1876. " * @export {this.is.a.long.path.to.a.Type}\n"
  1877. " */",
  1878. "/**\n"
  1879. " * @export {this.is.a.long.path.to.a.Type}\n"
  1880. " */",
  1881. getGoogleJSStyleWithColumns(20));
  1882. verifyFormat("/**\n"
  1883. " * @mods {this.is.a.long.path.to.a.Type}\n"
  1884. " */",
  1885. "/**\n"
  1886. " * @mods {this.is.a.long.path.to.a.Type}\n"
  1887. " */",
  1888. getGoogleJSStyleWithColumns(20));
  1889. verifyFormat("/**\n"
  1890. " * @param {this.is.a.long.path.to.a.Type}\n"
  1891. " */",
  1892. "/**\n"
  1893. " * @param {this.is.a.long.path.to.a.Type}\n"
  1894. " */",
  1895. getGoogleJSStyleWithColumns(20));
  1896. verifyFormat("/**\n"
  1897. " * @see http://very/very/long/url/is/long\n"
  1898. " */",
  1899. "/**\n"
  1900. " * @see http://very/very/long/url/is/long\n"
  1901. " */",
  1902. getGoogleJSStyleWithColumns(20));
  1903. verifyFormat(
  1904. "/**\n"
  1905. " * @param This is a\n"
  1906. " * long comment but\n"
  1907. " * no type\n"
  1908. " */",
  1909. "/**\n"
  1910. " * @param This is a long comment but no type\n"
  1911. " */",
  1912. getGoogleJSStyleWithColumns(20));
  1913. // Don't break @param line, but reindent it and reflow unrelated lines.
  1914. verifyFormat("{\n"
  1915. " /**\n"
  1916. " * long long long\n"
  1917. " * long\n"
  1918. " * @param {this.is.a.long.path.to.a.Type} a\n"
  1919. " * long long long\n"
  1920. " * long long\n"
  1921. " */\n"
  1922. " function f(a) {}\n"
  1923. "}",
  1924. "{\n"
  1925. "/**\n"
  1926. " * long long long long\n"
  1927. " * @param {this.is.a.long.path.to.a.Type} a\n"
  1928. " * long long long long\n"
  1929. " * long\n"
  1930. " */\n"
  1931. " function f(a) {}\n"
  1932. "}",
  1933. getGoogleJSStyleWithColumns(20));
  1934. }
  1935. TEST_F(FormatTestJS, RequoteStringsSingle) {
  1936. verifyFormat("var x = 'foo';", "var x = \"foo\";");
  1937. verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";");
  1938. verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";");
  1939. verifyFormat(
  1940. "var x =\n"
  1941. " 'foo\\'';",
  1942. // Code below is 15 chars wide, doesn't fit into the line with the
  1943. // \ escape added.
  1944. "var x = \"foo'\";", getGoogleJSStyleWithColumns(15));
  1945. // Removes no-longer needed \ escape from ".
  1946. verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";");
  1947. // Code below fits into 15 chars *after* removing the \ escape.
  1948. verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";",
  1949. getGoogleJSStyleWithColumns(15));
  1950. verifyFormat("// clang-format off\n"
  1951. "let x = \"double\";\n"
  1952. "// clang-format on\n"
  1953. "let x = 'single';\n",
  1954. "// clang-format off\n"
  1955. "let x = \"double\";\n"
  1956. "// clang-format on\n"
  1957. "let x = \"single\";\n");
  1958. }
  1959. TEST_F(FormatTestJS, RequoteAndIndent) {
  1960. verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n"
  1961. " 'double quoted string that needs wrapping');",
  1962. "let x = someVeryLongFunctionThatGoesOnAndOn("
  1963. "\"double quoted string that needs wrapping\");");
  1964. verifyFormat("let x =\n"
  1965. " 'foo\\'oo';\n"
  1966. "let x =\n"
  1967. " 'foo\\'oo';",
  1968. "let x=\"foo'oo\";\n"
  1969. "let x=\"foo'oo\";",
  1970. getGoogleJSStyleWithColumns(15));
  1971. }
  1972. TEST_F(FormatTestJS, RequoteStringsDouble) {
  1973. FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
  1974. DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double;
  1975. verifyFormat("var x = \"foo\";", DoubleQuotes);
  1976. verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes);
  1977. verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes);
  1978. }
  1979. TEST_F(FormatTestJS, RequoteStringsLeave) {
  1980. FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
  1981. LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave;
  1982. verifyFormat("var x = \"foo\";", LeaveQuotes);
  1983. verifyFormat("var x = 'foo';", LeaveQuotes);
  1984. }
  1985. TEST_F(FormatTestJS, SupportShebangLines) {
  1986. verifyFormat("#!/usr/bin/env node\n"
  1987. "var x = hello();",
  1988. "#!/usr/bin/env node\n"
  1989. "var x = hello();");
  1990. }
  1991. TEST_F(FormatTestJS, NonNullAssertionOperator) {
  1992. verifyFormat("let x = foo!.bar();\n");
  1993. verifyFormat("let x = foo ? bar! : baz;\n");
  1994. verifyFormat("let x = !foo;\n");
  1995. verifyFormat("let x = foo[0]!;\n");
  1996. verifyFormat("let x = (foo)!;\n");
  1997. verifyFormat("let x = x(foo!);\n");
  1998. verifyFormat(
  1999. "a.aaaaaa(a.a!).then(\n"
  2000. " x => x(x));\n",
  2001. getGoogleJSStyleWithColumns(20));
  2002. verifyFormat("let x = foo! - 1;\n");
  2003. verifyFormat("let x = {foo: 1}!;\n");
  2004. verifyFormat(
  2005. "let x = hello.foo()!\n"
  2006. " .foo()!\n"
  2007. " .foo()!\n"
  2008. " .foo()!;\n",
  2009. getGoogleJSStyleWithColumns(20));
  2010. verifyFormat("let x = namespace!;\n");
  2011. verifyFormat("return !!x;\n");
  2012. }
  2013. TEST_F(FormatTestJS, Conditional) {
  2014. verifyFormat("y = x ? 1 : 2;");
  2015. verifyFormat("x ? 1 : 2;");
  2016. verifyFormat("class Foo {\n"
  2017. " field = true ? 1 : 2;\n"
  2018. " method(a = true ? 1 : 2) {}\n"
  2019. "}");
  2020. }
  2021. TEST_F(FormatTestJS, ImportComments) {
  2022. verifyFormat("import {x} from 'x'; // from some location",
  2023. getGoogleJSStyleWithColumns(25));
  2024. verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10));
  2025. verifyFormat("/// <reference path=\"some/location\" />", getGoogleJSStyleWithColumns(10));
  2026. }
  2027. TEST_F(FormatTestJS, Exponentiation) {
  2028. verifyFormat("squared = x ** 2;");
  2029. verifyFormat("squared **= 2;");
  2030. }
  2031. TEST_F(FormatTestJS, NestedLiterals) {
  2032. FormatStyle FourSpaces = getGoogleJSStyleWithColumns(15);
  2033. FourSpaces.IndentWidth = 4;
  2034. verifyFormat("var l = [\n"
  2035. " [\n"
  2036. " 1,\n"
  2037. " ],\n"
  2038. "];", FourSpaces);
  2039. verifyFormat("var l = [\n"
  2040. " {\n"
  2041. " 1: 1,\n"
  2042. " },\n"
  2043. "];", FourSpaces);
  2044. verifyFormat("someFunction(\n"
  2045. " p1,\n"
  2046. " [\n"
  2047. " 1,\n"
  2048. " ],\n"
  2049. ");", FourSpaces);
  2050. verifyFormat("someFunction(\n"
  2051. " p1,\n"
  2052. " {\n"
  2053. " 1: 1,\n"
  2054. " },\n"
  2055. ");", FourSpaces);
  2056. verifyFormat("var o = {\n"
  2057. " 1: 1,\n"
  2058. " 2: {\n"
  2059. " 3: 3,\n"
  2060. " },\n"
  2061. "};", FourSpaces);
  2062. verifyFormat("var o = {\n"
  2063. " 1: 1,\n"
  2064. " 2: [\n"
  2065. " 3,\n"
  2066. " ],\n"
  2067. "};", FourSpaces);
  2068. }
  2069. TEST_F(FormatTestJS, BackslashesInComments) {
  2070. verifyFormat("// hello \\\n"
  2071. "if (x) foo();\n",
  2072. "// hello \\\n"
  2073. " if ( x) \n"
  2074. " foo();\n");
  2075. verifyFormat("/* ignore \\\n"
  2076. " */\n"
  2077. "if (x) foo();\n",
  2078. "/* ignore \\\n"
  2079. " */\n"
  2080. " if ( x) foo();\n");
  2081. verifyFormat("// st \\ art\\\n"
  2082. "// comment"
  2083. "// continue \\\n"
  2084. "formatMe();\n",
  2085. "// st \\ art\\\n"
  2086. "// comment"
  2087. "// continue \\\n"
  2088. "formatMe( );\n");
  2089. }
  2090. } // end namespace tooling
  2091. } // end namespace clang