FormatTestJS.cpp 69 KB

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