FormatTestJS.cpp 75 KB

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