FormatTestJS.cpp 80 KB

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