FormatTestJS.cpp 70 KB

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