FormatTestJS.cpp 77 KB

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