FormatTestJS.cpp 76 KB

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