FormatTestJS.cpp 74 KB

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