FormatTestJS.cpp 77 KB

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