FormatTestJS.cpp 65 KB

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