FormatTestJS.cpp 79 KB

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