FormatTestJS.cpp 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  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. // Ideally the foo() bit should be indented relative to the async function().
  1075. verifyFormat("async function\n"
  1076. "foo() {}",
  1077. getGoogleJSStyleWithColumns(10));
  1078. verifyFormat("await theReckoning;", getGoogleJSStyleWithColumns(10));
  1079. verifyFormat("some['a']['b']", getGoogleJSStyleWithColumns(10));
  1080. verifyFormat("x = (a['a']\n"
  1081. " ['b']);", getGoogleJSStyleWithColumns(10));
  1082. }
  1083. TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {
  1084. verifyFormat("a\n"
  1085. "b;",
  1086. " a \n"
  1087. " b ;");
  1088. verifyFormat("a()\n"
  1089. "b;",
  1090. " a ()\n"
  1091. " b ;");
  1092. verifyFormat("a[b]\n"
  1093. "c;",
  1094. "a [b]\n"
  1095. "c ;");
  1096. verifyFormat("1\n"
  1097. "a;",
  1098. "1 \n"
  1099. "a ;");
  1100. verifyFormat("a\n"
  1101. "1;",
  1102. "a \n"
  1103. "1 ;");
  1104. verifyFormat("a\n"
  1105. "'x';",
  1106. "a \n"
  1107. " 'x';");
  1108. verifyFormat("a++\n"
  1109. "b;",
  1110. "a ++\n"
  1111. "b ;");
  1112. verifyFormat("a\n"
  1113. "!b && c;",
  1114. "a \n"
  1115. " ! b && c;");
  1116. verifyFormat("a\n"
  1117. "if (1) f();",
  1118. " a\n"
  1119. " if (1) f();");
  1120. verifyFormat("a\n"
  1121. "class X {}",
  1122. " a\n"
  1123. " class X {}");
  1124. verifyFormat("var a", "var\n"
  1125. "a");
  1126. verifyFormat("x instanceof String", "x\n"
  1127. "instanceof\n"
  1128. "String");
  1129. verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n"
  1130. " bar) {}");
  1131. verifyFormat("function f(@Foo(Param) bar) {}", "function f(@Foo(Param)\n"
  1132. " bar) {}");
  1133. verifyFormat("a = true\n"
  1134. "return 1",
  1135. "a = true\n"
  1136. " return 1");
  1137. verifyFormat("a = 's'\n"
  1138. "return 1",
  1139. "a = 's'\n"
  1140. " return 1");
  1141. verifyFormat("a = null\n"
  1142. "return 1",
  1143. "a = null\n"
  1144. " return 1");
  1145. // Below "class Y {}" should ideally be on its own line.
  1146. verifyFormat(
  1147. "x = {\n"
  1148. " a: 1\n"
  1149. "} class Y {}",
  1150. " x = {a : 1}\n"
  1151. " class Y { }");
  1152. verifyFormat(
  1153. "if (x) {\n"
  1154. "}\n"
  1155. "return 1",
  1156. "if (x) {}\n"
  1157. " return 1");
  1158. verifyFormat(
  1159. "if (x) {\n"
  1160. "}\n"
  1161. "class X {}",
  1162. "if (x) {}\n"
  1163. " class X {}");
  1164. }
  1165. TEST_F(FormatTestJS, ImportExportASI) {
  1166. verifyFormat(
  1167. "import {x} from 'y'\n"
  1168. "export function z() {}",
  1169. "import {x} from 'y'\n"
  1170. " export function z() {}");
  1171. // Below "class Y {}" should ideally be on its own line.
  1172. verifyFormat(
  1173. "export {x} class Y {}",
  1174. " export {x}\n"
  1175. " class Y {\n}");
  1176. verifyFormat(
  1177. "if (x) {\n"
  1178. "}\n"
  1179. "export class Y {}",
  1180. "if ( x ) { }\n"
  1181. " export class Y {}");
  1182. }
  1183. TEST_F(FormatTestJS, ClosureStyleCasts) {
  1184. verifyFormat("var x = /** @type {foo} */ (bar);");
  1185. }
  1186. TEST_F(FormatTestJS, TryCatch) {
  1187. verifyFormat("try {\n"
  1188. " f();\n"
  1189. "} catch (e) {\n"
  1190. " g();\n"
  1191. "} finally {\n"
  1192. " h();\n"
  1193. "}");
  1194. // But, of course, "catch" is a perfectly fine function name in JavaScript.
  1195. verifyFormat("someObject.catch();");
  1196. verifyFormat("someObject.new();");
  1197. }
  1198. TEST_F(FormatTestJS, StringLiteralConcatenation) {
  1199. verifyFormat("var literal = 'hello ' +\n"
  1200. " 'world';");
  1201. }
  1202. TEST_F(FormatTestJS, RegexLiteralClassification) {
  1203. // Regex literals.
  1204. verifyFormat("var regex = /abc/;");
  1205. verifyFormat("f(/abc/);");
  1206. verifyFormat("f(abc, /abc/);");
  1207. verifyFormat("some_map[/abc/];");
  1208. verifyFormat("var x = a ? /abc/ : /abc/;");
  1209. verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
  1210. verifyFormat("var x = !/abc/.test(y);");
  1211. verifyFormat("var x = foo()! / 10;");
  1212. verifyFormat("var x = a && /abc/.test(y);");
  1213. verifyFormat("var x = a || /abc/.test(y);");
  1214. verifyFormat("var x = a + /abc/.search(y);");
  1215. verifyFormat("/abc/.search(y);");
  1216. verifyFormat("var regexs = {/abc/, /abc/};");
  1217. verifyFormat("return /abc/;");
  1218. // Not regex literals.
  1219. verifyFormat("var a = a / 2 + b / 3;");
  1220. verifyFormat("var a = a++ / 2;");
  1221. // Prefix unary can operate on regex literals, not that it makes sense.
  1222. verifyFormat("var a = ++/a/;");
  1223. // This is a known issue, regular expressions are incorrectly detected if
  1224. // directly following a closing parenthesis.
  1225. verifyFormat("if (foo) / bar /.exec(baz);");
  1226. }
  1227. TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
  1228. verifyFormat("var regex = /=/;");
  1229. verifyFormat("var regex = /a*/;");
  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 = /(?:x)/;");
  1237. verifyFormat("var regex = /x(?=y)/;");
  1238. verifyFormat("var regex = /x(?!y)/;");
  1239. verifyFormat("var regex = /x|y/;");
  1240. verifyFormat("var regex = /a{2}/;");
  1241. verifyFormat("var regex = /a{1,3}/;");
  1242. verifyFormat("var regex = /[abc]/;");
  1243. verifyFormat("var regex = /[^abc]/;");
  1244. verifyFormat("var regex = /[\\b]/;");
  1245. verifyFormat("var regex = /[/]/;");
  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 = /\\b/;");
  1253. verifyFormat("var regex = /\\B/;");
  1254. verifyFormat("var regex = /\\d/;");
  1255. verifyFormat("var regex = /\\D/;");
  1256. verifyFormat("var regex = /\\f/;");
  1257. verifyFormat("var regex = /\\n/;");
  1258. verifyFormat("var regex = /\\r/;");
  1259. verifyFormat("var regex = /\\s/;");
  1260. verifyFormat("var regex = /\\S/;");
  1261. verifyFormat("var regex = /\\t/;");
  1262. verifyFormat("var regex = /\\v/;");
  1263. verifyFormat("var regex = /\\w/;");
  1264. verifyFormat("var regex = /\\W/;");
  1265. verifyFormat("var regex = /a(a)\\1/;");
  1266. verifyFormat("var regex = /\\0/;");
  1267. verifyFormat("var regex = /\\\\/g;");
  1268. verifyFormat("var regex = /\\a\\\\/g;");
  1269. verifyFormat("var regex = /\a\\//g;");
  1270. verifyFormat("var regex = /a\\//;\n"
  1271. "var x = 0;");
  1272. verifyFormat("var regex = /'/g;", "var regex = /'/g ;");
  1273. verifyFormat("var regex = /'/g; //'", "var regex = /'/g ; //'");
  1274. verifyFormat("var regex = /\\/*/;\n"
  1275. "var x = 0;",
  1276. "var regex = /\\/*/;\n"
  1277. "var x=0;");
  1278. verifyFormat("var x = /a\\//;", "var x = /a\\// \n;");
  1279. verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16));
  1280. verifyFormat("var regex =\n"
  1281. " /\"/;",
  1282. getGoogleJSStyleWithColumns(15));
  1283. verifyFormat("var regex = //\n"
  1284. " /a/;");
  1285. verifyFormat("var regexs = [\n"
  1286. " /d/, //\n"
  1287. " /aa/, //\n"
  1288. "];");
  1289. }
  1290. TEST_F(FormatTestJS, RegexLiteralModifiers) {
  1291. verifyFormat("var regex = /abc/g;");
  1292. verifyFormat("var regex = /abc/i;");
  1293. verifyFormat("var regex = /abc/m;");
  1294. verifyFormat("var regex = /abc/y;");
  1295. }
  1296. TEST_F(FormatTestJS, RegexLiteralLength) {
  1297. verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
  1298. getGoogleJSStyleWithColumns(60));
  1299. verifyFormat("var regex =\n"
  1300. " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
  1301. getGoogleJSStyleWithColumns(60));
  1302. verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
  1303. getGoogleJSStyleWithColumns(50));
  1304. }
  1305. TEST_F(FormatTestJS, RegexLiteralExamples) {
  1306. verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
  1307. }
  1308. TEST_F(FormatTestJS, IgnoresMpegTS) {
  1309. std::string MpegTS(200, ' ');
  1310. MpegTS.replace(0, strlen("nearlyLooks + like + ts + code; "),
  1311. "nearlyLooks + like + ts + code; ");
  1312. MpegTS[0] = 0x47;
  1313. MpegTS[188] = 0x47;
  1314. verifyFormat(MpegTS, MpegTS);
  1315. }
  1316. TEST_F(FormatTestJS, TypeAnnotations) {
  1317. verifyFormat("var x: string;");
  1318. verifyFormat("var x: {a: string; b: number;} = {};");
  1319. verifyFormat("function x(): string {\n return 'x';\n}");
  1320. verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}");
  1321. verifyFormat("function x(y: string): string {\n return 'x';\n}");
  1322. verifyFormat("for (var y: string in x) {\n x();\n}");
  1323. verifyFormat("for (var y: string of x) {\n x();\n}");
  1324. verifyFormat("function x(y: {a?: number;} = {}): number {\n"
  1325. " return 12;\n"
  1326. "}");
  1327. verifyFormat("const x: Array<{a: number; b: string;}> = [];");
  1328. verifyFormat("((a: string, b: number): string => a + b);");
  1329. verifyFormat("var x: (y: number) => string;");
  1330. verifyFormat("var x: P<string, (a: number) => string>;");
  1331. verifyFormat("var x = {\n"
  1332. " y: function(): z {\n"
  1333. " return 1;\n"
  1334. " }\n"
  1335. "};");
  1336. verifyFormat("var x = {\n"
  1337. " y: function(): {a: number} {\n"
  1338. " return 1;\n"
  1339. " }\n"
  1340. "};");
  1341. verifyFormat("function someFunc(args: string[]):\n"
  1342. " {longReturnValue: string[]} {}",
  1343. getGoogleJSStyleWithColumns(60));
  1344. verifyFormat(
  1345. "var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])\n"
  1346. " .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  1347. verifyFormat("const xIsALongIdent:\n"" YJustBarelyFitsLinex[];",
  1348. getGoogleJSStyleWithColumns(20));
  1349. }
  1350. TEST_F(FormatTestJS, UnionIntersectionTypes) {
  1351. verifyFormat("let x: A|B = A | B;");
  1352. verifyFormat("let x: A&B|C = A & B;");
  1353. verifyFormat("let x: Foo<A|B> = new Foo<A|B>();");
  1354. verifyFormat("function(x: A|B): C&D {}");
  1355. verifyFormat("function(x: A|B = A | B): C&D {}");
  1356. verifyFormat("function x(path: number|string) {}");
  1357. verifyFormat("function x(): string|number {}");
  1358. verifyFormat("type Foo = Bar|Baz;");
  1359. verifyFormat("type Foo = Bar<X>|Baz;");
  1360. verifyFormat("type Foo = (Bar<X>|Baz);");
  1361. verifyFormat("let x: Bar|Baz;");
  1362. verifyFormat("let x: Bar<X>|Baz;");
  1363. verifyFormat("let x: (Foo|Bar)[];");
  1364. verifyFormat("type X = {\n"
  1365. " a: Foo|Bar;\n"
  1366. "};");
  1367. verifyFormat("export type X = {\n"
  1368. " a: Foo|Bar;\n"
  1369. "};");
  1370. }
  1371. TEST_F(FormatTestJS, UnionIntersectionTypesInObjectType) {
  1372. verifyFormat("let x: {x: number|null} = {x: number | null};");
  1373. verifyFormat("let nested: {x: {y: number|null}};");
  1374. verifyFormat("let mixed: {x: [number|null, {w: number}]};");
  1375. verifyFormat("class X {\n"
  1376. " contructor(x: {\n"
  1377. " a: a|null,\n"
  1378. " b: b|null,\n"
  1379. " }) {}\n"
  1380. "}");
  1381. }
  1382. TEST_F(FormatTestJS, ClassDeclarations) {
  1383. verifyFormat("class C {\n x: string = 12;\n}");
  1384. verifyFormat("class C {\n x(): string => 12;\n}");
  1385. verifyFormat("class C {\n ['x' + 2]: string = 12;\n}");
  1386. verifyFormat("class C {\n"
  1387. " foo() {}\n"
  1388. " [bar]() {}\n"
  1389. "}\n");
  1390. verifyFormat("class C {\n private x: string = 12;\n}");
  1391. verifyFormat("class C {\n private static x: string = 12;\n}");
  1392. verifyFormat("class C {\n static x(): string {\n return 'asd';\n }\n}");
  1393. verifyFormat("class C extends P implements I {}");
  1394. verifyFormat("class C extends p.P implements i.I {}");
  1395. verifyFormat(
  1396. "x(class {\n"
  1397. " a(): A {}\n"
  1398. "});");
  1399. verifyFormat("class Test {\n"
  1400. " aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n"
  1401. " aaaaaaaaaaaaaaaaaaaaaa {}\n"
  1402. "}");
  1403. verifyFormat("foo = class Name {\n"
  1404. " constructor() {}\n"
  1405. "};");
  1406. verifyFormat("foo = class {\n"
  1407. " constructor() {}\n"
  1408. "};");
  1409. verifyFormat("class C {\n"
  1410. " x: {y: Z;} = {};\n"
  1411. " private y: {y: Z;} = {};\n"
  1412. "}");
  1413. // ':' is not a type declaration here.
  1414. verifyFormat("class X {\n"
  1415. " subs = {\n"
  1416. " 'b': {\n"
  1417. " 'c': 1,\n"
  1418. " },\n"
  1419. " };\n"
  1420. "}");
  1421. verifyFormat("@Component({\n"
  1422. " moduleId: module.id,\n"
  1423. "})\n"
  1424. "class SessionListComponent implements OnDestroy, OnInit {\n"
  1425. "}");
  1426. }
  1427. TEST_F(FormatTestJS, InterfaceDeclarations) {
  1428. verifyFormat("interface I {\n"
  1429. " x: string;\n"
  1430. " enum: string[];\n"
  1431. " enum?: string[];\n"
  1432. "}\n"
  1433. "var y;");
  1434. // Ensure that state is reset after parsing the interface.
  1435. verifyFormat("interface a {}\n"
  1436. "export function b() {}\n"
  1437. "var x;");
  1438. // Arrays of object type literals.
  1439. verifyFormat("interface I {\n"
  1440. " o: {}[];\n"
  1441. "}");
  1442. }
  1443. TEST_F(FormatTestJS, ObjectTypesInExtendsImplements) {
  1444. verifyFormat("class C extends {} {}");
  1445. verifyFormat("class C implements {bar: number} {}");
  1446. // Somewhat odd, but probably closest to reasonable formatting?
  1447. verifyFormat("class C implements {\n"
  1448. " bar: number,\n"
  1449. " baz: string,\n"
  1450. "} {}");
  1451. verifyFormat("class C<P extends {}> {}");
  1452. }
  1453. TEST_F(FormatTestJS, EnumDeclarations) {
  1454. verifyFormat("enum Foo {\n"
  1455. " A = 1,\n"
  1456. " B\n"
  1457. "}");
  1458. verifyFormat("export /* somecomment*/ enum Foo {\n"
  1459. " A = 1,\n"
  1460. " B\n"
  1461. "}");
  1462. verifyFormat("enum Foo {\n"
  1463. " A = 1, // comment\n"
  1464. " B\n"
  1465. "}\n"
  1466. "var x = 1;");
  1467. verifyFormat("const enum Foo {\n"
  1468. " A = 1,\n"
  1469. " B\n"
  1470. "}");
  1471. verifyFormat("export const enum Foo {\n"
  1472. " A = 1,\n"
  1473. " B\n"
  1474. "}");
  1475. }
  1476. TEST_F(FormatTestJS, Decorators) {
  1477. verifyFormat("@A\nclass C {\n}");
  1478. verifyFormat("@A({arg: 'value'})\nclass C {\n}");
  1479. verifyFormat("@A\n@B\nclass C {\n}");
  1480. verifyFormat("class C {\n @A x: string;\n}");
  1481. verifyFormat("class C {\n"
  1482. " @A\n"
  1483. " private x(): string {\n"
  1484. " return 'y';\n"
  1485. " }\n"
  1486. "}");
  1487. verifyFormat("class C {\n"
  1488. " private x(@A x: string) {}\n"
  1489. "}");
  1490. verifyFormat("class X {}\n"
  1491. "class Y {}");
  1492. verifyFormat("class X {\n"
  1493. " @property() private isReply = false;\n"
  1494. "}\n");
  1495. }
  1496. TEST_F(FormatTestJS, TypeAliases) {
  1497. verifyFormat("type X = number;\n"
  1498. "class C {}");
  1499. verifyFormat("type X<Y> = Z<Y>;");
  1500. verifyFormat("type X = {\n"
  1501. " y: number\n"
  1502. "};\n"
  1503. "class C {}");
  1504. verifyFormat("export type X = {\n"
  1505. " a: string,\n"
  1506. " b?: string,\n"
  1507. "};\n");
  1508. }
  1509. TEST_F(FormatTestJS, TypeInterfaceLineWrapping) {
  1510. const FormatStyle &Style = getGoogleJSStyleWithColumns(20);
  1511. verifyFormat("type LongTypeIsReallyUnreasonablyLong =\n"
  1512. " string;\n",
  1513. "type LongTypeIsReallyUnreasonablyLong = string;\n",
  1514. Style);
  1515. verifyFormat(
  1516. "interface AbstractStrategyFactoryProvider {\n"
  1517. " a: number\n"
  1518. "}\n",
  1519. "interface AbstractStrategyFactoryProvider { a: number }\n",
  1520. Style);
  1521. }
  1522. TEST_F(FormatTestJS, RemoveEmptyLinesInArrowFunctions) {
  1523. verifyFormat("x = () => {\n"
  1524. " foo();\n"
  1525. "};\n",
  1526. "x = () => {\n"
  1527. "\n"
  1528. " foo();\n"
  1529. "\n"
  1530. "};\n");
  1531. }
  1532. TEST_F(FormatTestJS, Modules) {
  1533. verifyFormat("import SomeThing from 'some/module.js';");
  1534. verifyFormat("import {X, Y} from 'some/module.js';");
  1535. verifyFormat("import a, {X, Y} from 'some/module.js';");
  1536. verifyFormat("import {X, Y,} from 'some/module.js';");
  1537. verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
  1538. // Ensure Automatic Semicolon Insertion does not break on "as\n".
  1539. verifyFormat("import {X as myX} from 'm';", "import {X as\n"
  1540. " myX} from 'm';");
  1541. verifyFormat("import * as lib from 'some/module.js';");
  1542. verifyFormat("var x = {import: 1};\nx.import = 2;");
  1543. verifyFormat("export function fn() {\n"
  1544. " return 'fn';\n"
  1545. "}");
  1546. verifyFormat("export function A() {}\n"
  1547. "export default function B() {}\n"
  1548. "export function C() {}");
  1549. verifyFormat("export default () => {\n"
  1550. " let x = 1;\n"
  1551. " return x;\n"
  1552. "}");
  1553. verifyFormat("export const x = 12;");
  1554. verifyFormat("export default class X {}");
  1555. verifyFormat("export {X, Y} from 'some/module.js';");
  1556. verifyFormat("export {X, Y,} from 'some/module.js';");
  1557. verifyFormat("export {SomeVeryLongExport as X, "
  1558. "SomeOtherVeryLongExport as Y} from 'some/module.js';");
  1559. // export without 'from' is wrapped.
  1560. verifyFormat("export let someRatherLongVariableName =\n"
  1561. " someSurprisinglyLongVariable + someOtherRatherLongVar;");
  1562. // ... but not if from is just an identifier.
  1563. verifyFormat("export {\n"
  1564. " from as from,\n"
  1565. " someSurprisinglyLongVariable as\n"
  1566. " from\n"
  1567. "};",
  1568. getGoogleJSStyleWithColumns(20));
  1569. verifyFormat("export class C {\n"
  1570. " x: number;\n"
  1571. " y: string;\n"
  1572. "}");
  1573. verifyFormat("export class X {\n"
  1574. " y: number;\n"
  1575. "}");
  1576. verifyFormat("export abstract class X {\n"
  1577. " y: number;\n"
  1578. "}");
  1579. verifyFormat("export default class X {\n"
  1580. " y: number\n"
  1581. "}");
  1582. verifyFormat("export default function() {\n return 1;\n}");
  1583. verifyFormat("export var x = 12;");
  1584. verifyFormat("class C {}\n"
  1585. "export function f() {}\n"
  1586. "var v;");
  1587. verifyFormat("export var x: number = 12;");
  1588. verifyFormat("export const y = {\n"
  1589. " a: 1,\n"
  1590. " b: 2\n"
  1591. "};");
  1592. verifyFormat("export enum Foo {\n"
  1593. " BAR,\n"
  1594. " // adsdasd\n"
  1595. " BAZ\n"
  1596. "}");
  1597. verifyFormat("export default [\n"
  1598. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1599. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  1600. "];");
  1601. verifyFormat("export default [];");
  1602. verifyFormat("export default () => {};");
  1603. verifyFormat("export interface Foo {\n"
  1604. " foo: number;\n"
  1605. "}\n"
  1606. "export class Bar {\n"
  1607. " blah(): string {\n"
  1608. " return this.blah;\n"
  1609. " };\n"
  1610. "}");
  1611. }
  1612. TEST_F(FormatTestJS, ImportWrapping) {
  1613. verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,"
  1614. " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying"
  1615. "} from 'some/module.js';");
  1616. FormatStyle Style = getGoogleJSStyleWithColumns(80);
  1617. Style.JavaScriptWrapImports = true;
  1618. verifyFormat("import {\n"
  1619. " VeryLongImportsAreAnnoying,\n"
  1620. " VeryLongImportsAreAnnoying,\n"
  1621. " VeryLongImportsAreAnnoying,\n"
  1622. "} from 'some/module.js';",
  1623. Style);
  1624. verifyFormat("import {\n"
  1625. " A,\n"
  1626. " A,\n"
  1627. "} from 'some/module.js';",
  1628. Style);
  1629. verifyFormat("export {\n"
  1630. " A,\n"
  1631. " A,\n"
  1632. "} from 'some/module.js';",
  1633. Style);
  1634. Style.ColumnLimit = 40;
  1635. // Using this version of verifyFormat because test::messUp hides the issue.
  1636. verifyFormat("import {\n"
  1637. " A,\n"
  1638. "} from\n"
  1639. " 'some/path/longer/than/column/limit/module.js';",
  1640. " import { \n"
  1641. " A, \n"
  1642. " } from\n"
  1643. " 'some/path/longer/than/column/limit/module.js' ; ",
  1644. Style);
  1645. }
  1646. TEST_F(FormatTestJS, TemplateStrings) {
  1647. // Keeps any whitespace/indentation within the template string.
  1648. verifyFormat("var x = `hello\n"
  1649. " ${name}\n"
  1650. " !`;",
  1651. "var x = `hello\n"
  1652. " ${ name }\n"
  1653. " !`;");
  1654. verifyFormat("var x =\n"
  1655. " `hello ${world}` >= some();",
  1656. getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
  1657. verifyFormat("var x = `hello ${world}` >= some();",
  1658. getGoogleJSStyleWithColumns(35)); // Barely fits.
  1659. verifyFormat("var x = `hellö ${wörld}` >= söme();",
  1660. getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8.
  1661. verifyFormat("var x = `hello\n"
  1662. " ${world}` >=\n"
  1663. " some();",
  1664. "var x =\n"
  1665. " `hello\n"
  1666. " ${world}` >= some();",
  1667. getGoogleJSStyleWithColumns(21)); // Barely doesn't fit.
  1668. verifyFormat("var x = `hello\n"
  1669. " ${world}` >= some();",
  1670. "var x =\n"
  1671. " `hello\n"
  1672. " ${world}` >= some();",
  1673. getGoogleJSStyleWithColumns(22)); // Barely fits.
  1674. verifyFormat("var x =\n"
  1675. " `h`;",
  1676. getGoogleJSStyleWithColumns(11));
  1677. verifyFormat("var x =\n `multi\n line`;", "var x = `multi\n line`;",
  1678. getGoogleJSStyleWithColumns(13));
  1679. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1680. " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);");
  1681. // Repro for an obscure width-miscounting issue with template strings.
  1682. verifyFormat(
  1683. "someLongVariable =\n"
  1684. " "
  1685. "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;",
  1686. "someLongVariable = "
  1687. "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;");
  1688. // Make sure template strings get a proper ColumnWidth assigned, even if they
  1689. // are first token in line.
  1690. verifyFormat(
  1691. "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  1692. " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;");
  1693. // Two template strings.
  1694. verifyFormat("var x = `hello` == `hello`;");
  1695. // Comments in template strings.
  1696. verifyFormat("var x = `//a`;\n"
  1697. "var y;",
  1698. "var x =\n `//a`;\n"
  1699. "var y ;");
  1700. verifyFormat("var x = `/*a`;\n"
  1701. "var y;",
  1702. "var x =\n `/*a`;\n"
  1703. "var y;");
  1704. // Unterminated string literals in a template string.
  1705. verifyFormat("var x = `'`; // comment with matching quote '\n"
  1706. "var y;");
  1707. verifyFormat("var x = `\"`; // comment with matching quote \"\n"
  1708. "var y;");
  1709. verifyFormat("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa);",
  1710. "it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa) ;",
  1711. getGoogleJSStyleWithColumns(40));
  1712. // Backticks in a comment - not a template string.
  1713. verifyFormat("var x = 1 // `/*a`;\n"
  1714. " ;",
  1715. "var x =\n 1 // `/*a`;\n"
  1716. " ;");
  1717. verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */");
  1718. // Comment spans multiple template strings.
  1719. verifyFormat("var x = `/*a`;\n"
  1720. "var y = ` */ `;",
  1721. "var x =\n `/*a`;\n"
  1722. "var y =\n ` */ `;");
  1723. // Escaped backtick.
  1724. verifyFormat("var x = ` \\` a`;\n"
  1725. "var y;",
  1726. "var x = ` \\` a`;\n"
  1727. "var y;");
  1728. // Escaped dollar.
  1729. verifyFormat("var x = ` \\${foo}`;\n");
  1730. // The token stream can contain two string_literals in sequence, but that
  1731. // doesn't mean that they are implicitly concatenated in JavaScript.
  1732. verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;");
  1733. // Ensure that scopes are appropriately set around evaluated expressions in
  1734. // template strings.
  1735. verifyFormat("var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa\n"
  1736. " aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;",
  1737. "var f = `aaaaaaaaaaaaa:${aaaaaaa. aaaaa} aaaaaaaa\n"
  1738. " aaaaaaaaaaaaa:${ aaaaaaa. aaaaa} aaaaaaaa`;");
  1739. verifyFormat("var x = someFunction(`${})`) //\n"
  1740. " .oooooooooooooooooon();");
  1741. verifyFormat("var x = someFunction(`${aaaa}${\n"
  1742. " aaaaa( //\n"
  1743. " aaaaa)})`);");
  1744. }
  1745. TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
  1746. verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${\n"
  1747. " aaaaa + //\n"
  1748. " bbbb}`;",
  1749. "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa + //\n"
  1750. " bbbb}`;");
  1751. verifyFormat("var f = `\n"
  1752. " aaaaaaaaaaaaaaaaaa: ${\n"
  1753. " aaaaa + //\n"
  1754. " bbbb}`;",
  1755. "var f = `\n"
  1756. " aaaaaaaaaaaaaaaaaa: ${ aaaaa + //\n"
  1757. " bbbb }`;");
  1758. verifyFormat("var f = `\n"
  1759. " aaaaaaaaaaaaaaaaaa: ${\n"
  1760. " someFunction(\n"
  1761. " aaaaa + //\n"
  1762. " bbbb)}`;",
  1763. "var f = `\n"
  1764. " aaaaaaaaaaaaaaaaaa: ${someFunction (\n"
  1765. " aaaaa + //\n"
  1766. " bbbb)}`;");
  1767. // It might be preferable to wrap before "someFunction".
  1768. verifyFormat("var f = `\n"
  1769. " aaaaaaaaaaaaaaaaaa: ${someFunction({\n"
  1770. " aaaa: aaaaa,\n"
  1771. " bbbb: bbbbb,\n"
  1772. "})}`;",
  1773. "var f = `\n"
  1774. " aaaaaaaaaaaaaaaaaa: ${someFunction ({\n"
  1775. " aaaa: aaaaa,\n"
  1776. " bbbb: bbbbb,\n"
  1777. " })}`;");
  1778. }
  1779. TEST_F(FormatTestJS, TemplateStringASI) {
  1780. verifyFormat("var x = `hello${world}`;", "var x = `hello${\n"
  1781. " world\n"
  1782. "}`;");
  1783. }
  1784. TEST_F(FormatTestJS, NestedTemplateStrings) {
  1785. verifyFormat(
  1786. "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;");
  1787. verifyFormat("var x = `he${({text: 'll'}.text)}o`;");
  1788. // Crashed at some point.
  1789. verifyFormat("}");
  1790. }
  1791. TEST_F(FormatTestJS, TaggedTemplateStrings) {
  1792. verifyFormat("var x = html`<ul>`;");
  1793. verifyFormat("yield `hello`;");
  1794. }
  1795. TEST_F(FormatTestJS, CastSyntax) {
  1796. verifyFormat("var x = <type>foo;");
  1797. verifyFormat("var x = foo as type;");
  1798. verifyFormat("let x = (a + b) as\n"
  1799. " LongTypeIsLong;",
  1800. getGoogleJSStyleWithColumns(20));
  1801. verifyFormat("foo = <Bar[]>[\n"
  1802. " 1, //\n"
  1803. " 2\n"
  1804. "];");
  1805. verifyFormat("var x = [{x: 1} as type];");
  1806. verifyFormat("x = x as [a, b];");
  1807. verifyFormat("x = x as {a: string};");
  1808. verifyFormat("x = x as (string);");
  1809. verifyFormat("x = x! as (string);");
  1810. verifyFormat("x = y! in z;");
  1811. verifyFormat("var x = something.someFunction() as\n"
  1812. " something;",
  1813. getGoogleJSStyleWithColumns(40));
  1814. }
  1815. TEST_F(FormatTestJS, TypeArguments) {
  1816. verifyFormat("class X<Y> {}");
  1817. verifyFormat("new X<Y>();");
  1818. verifyFormat("foo<Y>(a);");
  1819. verifyFormat("var x: X<Y>[];");
  1820. verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
  1821. verifyFormat("function f(a: List<any> = null) {}");
  1822. verifyFormat("function f(): List<any> {}");
  1823. verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n"
  1824. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}");
  1825. verifyFormat("function aaaaaaaaaa(\n"
  1826. " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n"
  1827. " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n"
  1828. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}");
  1829. }
  1830. TEST_F(FormatTestJS, UserDefinedTypeGuards) {
  1831. verifyFormat(
  1832. "function foo(check: Object):\n"
  1833. " check is {foo: string, bar: string, baz: string, foobar: string} {\n"
  1834. " return 'bar' in check;\n"
  1835. "}\n");
  1836. }
  1837. TEST_F(FormatTestJS, OptionalTypes) {
  1838. verifyFormat("function x(a?: b, c?, d?) {}");
  1839. verifyFormat("class X {\n"
  1840. " y?: z;\n"
  1841. " z?;\n"
  1842. "}");
  1843. verifyFormat("interface X {\n"
  1844. " y?(): z;\n"
  1845. "}");
  1846. verifyFormat("constructor({aa}: {\n"
  1847. " aa?: string,\n"
  1848. " aaaaaaaa?: string,\n"
  1849. " aaaaaaaaaaaaaaa?: boolean,\n"
  1850. " aaaaaa?: List<string>\n"
  1851. "}) {}");
  1852. }
  1853. TEST_F(FormatTestJS, IndexSignature) {
  1854. verifyFormat("var x: {[k: string]: v};");
  1855. }
  1856. TEST_F(FormatTestJS, WrapAfterParen) {
  1857. verifyFormat("xxxxxxxxxxx(\n"
  1858. " aaa, aaa);",
  1859. getGoogleJSStyleWithColumns(20));
  1860. verifyFormat("xxxxxxxxxxx(\n"
  1861. " aaa, aaa, aaa,\n"
  1862. " aaa, aaa, aaa);",
  1863. getGoogleJSStyleWithColumns(20));
  1864. verifyFormat("xxxxxxxxxxx(\n"
  1865. " aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1866. " function(x) {\n"
  1867. " y(); //\n"
  1868. " });",
  1869. getGoogleJSStyleWithColumns(40));
  1870. verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  1871. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  1872. }
  1873. TEST_F(FormatTestJS, JSDocAnnotations) {
  1874. verifyFormat("/**\n"
  1875. " * @export {this.is.a.long.path.to.a.Type}\n"
  1876. " */",
  1877. "/**\n"
  1878. " * @export {this.is.a.long.path.to.a.Type}\n"
  1879. " */",
  1880. getGoogleJSStyleWithColumns(20));
  1881. verifyFormat("/**\n"
  1882. " * @mods {this.is.a.long.path.to.a.Type}\n"
  1883. " */",
  1884. "/**\n"
  1885. " * @mods {this.is.a.long.path.to.a.Type}\n"
  1886. " */",
  1887. getGoogleJSStyleWithColumns(20));
  1888. verifyFormat("/**\n"
  1889. " * @param {this.is.a.long.path.to.a.Type}\n"
  1890. " */",
  1891. "/**\n"
  1892. " * @param {this.is.a.long.path.to.a.Type}\n"
  1893. " */",
  1894. getGoogleJSStyleWithColumns(20));
  1895. verifyFormat("/**\n"
  1896. " * @see http://very/very/long/url/is/long\n"
  1897. " */",
  1898. "/**\n"
  1899. " * @see http://very/very/long/url/is/long\n"
  1900. " */",
  1901. getGoogleJSStyleWithColumns(20));
  1902. verifyFormat(
  1903. "/**\n"
  1904. " * @param This is a\n"
  1905. " * long comment but\n"
  1906. " * no type\n"
  1907. " */",
  1908. "/**\n"
  1909. " * @param This is a long comment but no type\n"
  1910. " */",
  1911. getGoogleJSStyleWithColumns(20));
  1912. // Don't break @param line, but reindent it and reflow unrelated lines.
  1913. verifyFormat("{\n"
  1914. " /**\n"
  1915. " * long long long\n"
  1916. " * long\n"
  1917. " * @param {this.is.a.long.path.to.a.Type} a\n"
  1918. " * long long long\n"
  1919. " * long long\n"
  1920. " */\n"
  1921. " function f(a) {}\n"
  1922. "}",
  1923. "{\n"
  1924. "/**\n"
  1925. " * long long long long\n"
  1926. " * @param {this.is.a.long.path.to.a.Type} a\n"
  1927. " * long long long long\n"
  1928. " * long\n"
  1929. " */\n"
  1930. " function f(a) {}\n"
  1931. "}",
  1932. getGoogleJSStyleWithColumns(20));
  1933. }
  1934. TEST_F(FormatTestJS, RequoteStringsSingle) {
  1935. verifyFormat("var x = 'foo';", "var x = \"foo\";");
  1936. verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";");
  1937. verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";");
  1938. verifyFormat(
  1939. "var x =\n"
  1940. " 'foo\\'';",
  1941. // Code below is 15 chars wide, doesn't fit into the line with the
  1942. // \ escape added.
  1943. "var x = \"foo'\";", getGoogleJSStyleWithColumns(15));
  1944. // Removes no-longer needed \ escape from ".
  1945. verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";");
  1946. // Code below fits into 15 chars *after* removing the \ escape.
  1947. verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";",
  1948. getGoogleJSStyleWithColumns(15));
  1949. verifyFormat("// clang-format off\n"
  1950. "let x = \"double\";\n"
  1951. "// clang-format on\n"
  1952. "let x = 'single';\n",
  1953. "// clang-format off\n"
  1954. "let x = \"double\";\n"
  1955. "// clang-format on\n"
  1956. "let x = \"single\";\n");
  1957. }
  1958. TEST_F(FormatTestJS, RequoteAndIndent) {
  1959. verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n"
  1960. " 'double quoted string that needs wrapping');",
  1961. "let x = someVeryLongFunctionThatGoesOnAndOn("
  1962. "\"double quoted string that needs wrapping\");");
  1963. verifyFormat("let x =\n"
  1964. " 'foo\\'oo';\n"
  1965. "let x =\n"
  1966. " 'foo\\'oo';",
  1967. "let x=\"foo'oo\";\n"
  1968. "let x=\"foo'oo\";",
  1969. getGoogleJSStyleWithColumns(15));
  1970. }
  1971. TEST_F(FormatTestJS, RequoteStringsDouble) {
  1972. FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
  1973. DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double;
  1974. verifyFormat("var x = \"foo\";", DoubleQuotes);
  1975. verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes);
  1976. verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes);
  1977. }
  1978. TEST_F(FormatTestJS, RequoteStringsLeave) {
  1979. FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
  1980. LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave;
  1981. verifyFormat("var x = \"foo\";", LeaveQuotes);
  1982. verifyFormat("var x = 'foo';", LeaveQuotes);
  1983. }
  1984. TEST_F(FormatTestJS, SupportShebangLines) {
  1985. verifyFormat("#!/usr/bin/env node\n"
  1986. "var x = hello();",
  1987. "#!/usr/bin/env node\n"
  1988. "var x = hello();");
  1989. }
  1990. TEST_F(FormatTestJS, NonNullAssertionOperator) {
  1991. verifyFormat("let x = foo!.bar();\n");
  1992. verifyFormat("let x = foo ? bar! : baz;\n");
  1993. verifyFormat("let x = !foo;\n");
  1994. verifyFormat("let x = foo[0]!;\n");
  1995. verifyFormat("let x = (foo)!;\n");
  1996. verifyFormat("let x = x(foo!);\n");
  1997. verifyFormat(
  1998. "a.aaaaaa(a.a!).then(\n"
  1999. " x => x(x));\n",
  2000. getGoogleJSStyleWithColumns(20));
  2001. verifyFormat("let x = foo! - 1;\n");
  2002. verifyFormat("let x = {foo: 1}!;\n");
  2003. verifyFormat(
  2004. "let x = hello.foo()!\n"
  2005. " .foo()!\n"
  2006. " .foo()!\n"
  2007. " .foo()!;\n",
  2008. getGoogleJSStyleWithColumns(20));
  2009. verifyFormat("let x = namespace!;\n");
  2010. verifyFormat("return !!x;\n");
  2011. }
  2012. TEST_F(FormatTestJS, Conditional) {
  2013. verifyFormat("y = x ? 1 : 2;");
  2014. verifyFormat("x ? 1 : 2;");
  2015. verifyFormat("class Foo {\n"
  2016. " field = true ? 1 : 2;\n"
  2017. " method(a = true ? 1 : 2) {}\n"
  2018. "}");
  2019. }
  2020. TEST_F(FormatTestJS, ImportComments) {
  2021. verifyFormat("import {x} from 'x'; // from some location",
  2022. getGoogleJSStyleWithColumns(25));
  2023. verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10));
  2024. verifyFormat("/// <reference path=\"some/location\" />", getGoogleJSStyleWithColumns(10));
  2025. }
  2026. TEST_F(FormatTestJS, Exponentiation) {
  2027. verifyFormat("squared = x ** 2;");
  2028. verifyFormat("squared **= 2;");
  2029. }
  2030. TEST_F(FormatTestJS, NestedLiterals) {
  2031. FormatStyle FourSpaces = getGoogleJSStyleWithColumns(15);
  2032. FourSpaces.IndentWidth = 4;
  2033. verifyFormat("var l = [\n"
  2034. " [\n"
  2035. " 1,\n"
  2036. " ],\n"
  2037. "];", FourSpaces);
  2038. verifyFormat("var l = [\n"
  2039. " {\n"
  2040. " 1: 1,\n"
  2041. " },\n"
  2042. "];", FourSpaces);
  2043. verifyFormat("someFunction(\n"
  2044. " p1,\n"
  2045. " [\n"
  2046. " 1,\n"
  2047. " ],\n"
  2048. ");", FourSpaces);
  2049. verifyFormat("someFunction(\n"
  2050. " p1,\n"
  2051. " {\n"
  2052. " 1: 1,\n"
  2053. " },\n"
  2054. ");", FourSpaces);
  2055. verifyFormat("var o = {\n"
  2056. " 1: 1,\n"
  2057. " 2: {\n"
  2058. " 3: 3,\n"
  2059. " },\n"
  2060. "};", FourSpaces);
  2061. verifyFormat("var o = {\n"
  2062. " 1: 1,\n"
  2063. " 2: [\n"
  2064. " 3,\n"
  2065. " ],\n"
  2066. "};", FourSpaces);
  2067. }
  2068. TEST_F(FormatTestJS, BackslashesInComments) {
  2069. verifyFormat("// hello \\\n"
  2070. "if (x) foo();\n",
  2071. "// hello \\\n"
  2072. " if ( x) \n"
  2073. " foo();\n");
  2074. verifyFormat("/* ignore \\\n"
  2075. " */\n"
  2076. "if (x) foo();\n",
  2077. "/* ignore \\\n"
  2078. " */\n"
  2079. " if ( x) foo();\n");
  2080. verifyFormat("// st \\ art\\\n"
  2081. "// comment"
  2082. "// continue \\\n"
  2083. "formatMe();\n",
  2084. "// st \\ art\\\n"
  2085. "// comment"
  2086. "// continue \\\n"
  2087. "formatMe( );\n");
  2088. }
  2089. } // end namespace tooling
  2090. } // end namespace clang