FormatTestJS.cpp 73 KB

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