FormatTestJS.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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. // Known issue: this should wrap after {}, but calculateBraceTypes
  436. // misclassifies the first braces as a BK_BracedInit.
  437. verifyFormat("function aFunction(){} {\n"
  438. " let x = 1;\n"
  439. " console.log(x);\n"
  440. "}\n");
  441. }
  442. TEST_F(FormatTestJS, GeneratorFunctions) {
  443. verifyFormat("function* f() {\n"
  444. " let x = 1;\n"
  445. " yield x;\n"
  446. " yield* something();\n"
  447. " yield [1, 2];\n"
  448. " yield {a: 1};\n"
  449. "}");
  450. verifyFormat("function*\n"
  451. " f() {\n"
  452. "}",
  453. getGoogleJSStyleWithColumns(8));
  454. verifyFormat("export function* f() {\n"
  455. " yield 1;\n"
  456. "}\n");
  457. verifyFormat("class X {\n"
  458. " * generatorMethod() {\n"
  459. " yield x;\n"
  460. " }\n"
  461. "}");
  462. verifyFormat("var x = {\n"
  463. " a: function*() {\n"
  464. " //\n"
  465. " }\n"
  466. "}\n");
  467. }
  468. TEST_F(FormatTestJS, AsyncFunctions) {
  469. verifyFormat("async function f() {\n"
  470. " let x = 1;\n"
  471. " return fetch(x);\n"
  472. "}");
  473. verifyFormat("async function f() {\n"
  474. " return 1;\n"
  475. "}\n"
  476. "\n"
  477. "function a() {\n"
  478. " return 1;\n"
  479. "}\n",
  480. " async function f() {\n"
  481. " return 1;\n"
  482. "}\n"
  483. "\n"
  484. " function a() {\n"
  485. " return 1;\n"
  486. "} \n");
  487. verifyFormat("async function* f() {\n"
  488. " yield fetch(x);\n"
  489. "}");
  490. verifyFormat("export async function f() {\n"
  491. " return fetch(x);\n"
  492. "}");
  493. verifyFormat("let x = async () => f();");
  494. verifyFormat("let x = async function() {\n"
  495. " f();\n"
  496. "};");
  497. verifyFormat("let x = async();");
  498. verifyFormat("class X {\n"
  499. " async asyncMethod() {\n"
  500. " return fetch(1);\n"
  501. " }\n"
  502. "}");
  503. verifyFormat("function initialize() {\n"
  504. " // Comment.\n"
  505. " return async.then();\n"
  506. "}\n");
  507. verifyFormat("for await (const x of y) {\n"
  508. " console.log(x);\n"
  509. "}\n");
  510. verifyFormat("function asyncLoop() {\n"
  511. " for await (const x of y) {\n"
  512. " console.log(x);\n"
  513. " }\n"
  514. "}\n");
  515. }
  516. TEST_F(FormatTestJS, FunctionParametersTrailingComma) {
  517. verifyFormat("function trailingComma(\n"
  518. " p1,\n"
  519. " p2,\n"
  520. " p3,\n"
  521. ") {\n"
  522. " a; //\n"
  523. "}\n",
  524. "function trailingComma(p1, p2, p3,) {\n"
  525. " a; //\n"
  526. "}\n");
  527. verifyFormat("trailingComma(\n"
  528. " p1,\n"
  529. " p2,\n"
  530. " p3,\n"
  531. ");\n",
  532. "trailingComma(p1, p2, p3,);\n");
  533. verifyFormat("trailingComma(\n"
  534. " p1 // hello\n"
  535. ");\n",
  536. "trailingComma(p1 // hello\n"
  537. ");\n");
  538. }
  539. TEST_F(FormatTestJS, ArrayLiterals) {
  540. verifyFormat("var aaaaa: List<SomeThing> =\n"
  541. " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];");
  542. verifyFormat("return [\n"
  543. " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  544. " ccccccccccccccccccccccccccc\n"
  545. "];");
  546. verifyFormat("return [\n"
  547. " aaaa().bbbbbbbb('A'),\n"
  548. " aaaa().bbbbbbbb('B'),\n"
  549. " aaaa().bbbbbbbb('C'),\n"
  550. "];");
  551. verifyFormat("var someVariable = SomeFunction([\n"
  552. " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  553. " ccccccccccccccccccccccccccc\n"
  554. "]);");
  555. verifyFormat("var someVariable = SomeFunction([\n"
  556. " [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n"
  557. "]);",
  558. getGoogleJSStyleWithColumns(51));
  559. verifyFormat("var someVariable = SomeFunction(aaaa, [\n"
  560. " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  561. " ccccccccccccccccccccccccccc\n"
  562. "]);");
  563. verifyFormat("var someVariable = SomeFunction(\n"
  564. " aaaa,\n"
  565. " [\n"
  566. " aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
  567. " cccccccccccccccccccccccccc\n"
  568. " ],\n"
  569. " aaaa);");
  570. verifyFormat("var aaaa = aaaaa || // wrap\n"
  571. " [];");
  572. verifyFormat("someFunction([], {a: a});");
  573. verifyFormat("var string = [\n"
  574. " 'aaaaaa',\n"
  575. " 'bbbbbb',\n"
  576. "].join('+');");
  577. }
  578. TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) {
  579. verifyFormat("var array = [\n"
  580. " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
  581. " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
  582. "];");
  583. verifyFormat("var array = someFunction([\n"
  584. " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
  585. " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
  586. "]);");
  587. }
  588. TEST_F(FormatTestJS, FunctionLiterals) {
  589. FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
  590. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  591. verifyFormat("doFoo(function() {});");
  592. verifyFormat("doFoo(function() { return 1; });", Style);
  593. verifyFormat("var func = function() {\n"
  594. " return 1;\n"
  595. "};");
  596. verifyFormat("var func = //\n"
  597. " function() {\n"
  598. " return 1;\n"
  599. "};");
  600. verifyFormat("return {\n"
  601. " body: {\n"
  602. " setAttribute: function(key, val) { this[key] = val; },\n"
  603. " getAttribute: function(key) { return this[key]; },\n"
  604. " style: {direction: ''}\n"
  605. " }\n"
  606. "};",
  607. Style);
  608. verifyFormat("abc = xyz ? function() {\n"
  609. " return 1;\n"
  610. "} : function() {\n"
  611. " return -1;\n"
  612. "};");
  613. verifyFormat("var closure = goog.bind(\n"
  614. " function() { // comment\n"
  615. " foo();\n"
  616. " bar();\n"
  617. " },\n"
  618. " this, arg1IsReallyLongAndNeedsLineBreaks,\n"
  619. " arg3IsReallyLongAndNeedsLineBreaks);");
  620. verifyFormat("var closure = goog.bind(function() { // comment\n"
  621. " foo();\n"
  622. " bar();\n"
  623. "}, this);");
  624. verifyFormat("return {\n"
  625. " a: 'E',\n"
  626. " b: function() {\n"
  627. " return function() {\n"
  628. " f(); //\n"
  629. " };\n"
  630. " }\n"
  631. "};");
  632. verifyFormat("{\n"
  633. " var someVariable = function(x) {\n"
  634. " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
  635. " };\n"
  636. "}");
  637. verifyFormat("someLooooooooongFunction(\n"
  638. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  639. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  640. " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
  641. " // code\n"
  642. " });");
  643. verifyFormat("return {\n"
  644. " a: function SomeFunction() {\n"
  645. " // ...\n"
  646. " return 1;\n"
  647. " }\n"
  648. "};");
  649. verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  650. " .then(goog.bind(function(aaaaaaaaaaa) {\n"
  651. " someFunction();\n"
  652. " someFunction();\n"
  653. " }, this), aaaaaaaaaaaaaaaaa);");
  654. verifyFormat("someFunction(goog.bind(function() {\n"
  655. " doSomething();\n"
  656. " doSomething();\n"
  657. "}, this), goog.bind(function() {\n"
  658. " doSomething();\n"
  659. " doSomething();\n"
  660. "}, this));");
  661. verifyFormat("SomeFunction(function() {\n"
  662. " foo();\n"
  663. " bar();\n"
  664. "}.bind(this));");
  665. // FIXME: This is bad, we should be wrapping before "function() {".
  666. verifyFormat("someFunction(function() {\n"
  667. " doSomething(); // break\n"
  668. "})\n"
  669. " .doSomethingElse(\n"
  670. " // break\n"
  671. " );");
  672. Style.ColumnLimit = 33;
  673. verifyFormat("f({a: function() { return 1; }});", Style);
  674. Style.ColumnLimit = 32;
  675. verifyFormat("f({\n"
  676. " a: function() { return 1; }\n"
  677. "});",
  678. Style);
  679. }
  680. TEST_F(FormatTestJS, InliningFunctionLiterals) {
  681. FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
  682. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
  683. verifyFormat("var func = function() {\n"
  684. " return 1;\n"
  685. "};",
  686. Style);
  687. verifyFormat("var func = doSomething(function() { return 1; });", Style);
  688. verifyFormat("var outer = function() {\n"
  689. " var inner = function() { return 1; }\n"
  690. "};",
  691. Style);
  692. verifyFormat("function outer1(a, b) {\n"
  693. " function inner1(a, b) { return a; }\n"
  694. "}",
  695. Style);
  696. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  697. verifyFormat("var func = function() { return 1; };", Style);
  698. verifyFormat("var func = doSomething(function() { return 1; });", Style);
  699. verifyFormat(
  700. "var outer = function() { var inner = function() { return 1; } };",
  701. Style);
  702. verifyFormat("function outer1(a, b) {\n"
  703. " function inner1(a, b) { return a; }\n"
  704. "}",
  705. Style);
  706. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
  707. verifyFormat("var func = function() {\n"
  708. " return 1;\n"
  709. "};",
  710. Style);
  711. verifyFormat("var func = doSomething(function() {\n"
  712. " return 1;\n"
  713. "});",
  714. Style);
  715. verifyFormat("var outer = function() {\n"
  716. " var inner = function() {\n"
  717. " return 1;\n"
  718. " }\n"
  719. "};",
  720. Style);
  721. verifyFormat("function outer1(a, b) {\n"
  722. " function inner1(a, b) {\n"
  723. " return a;\n"
  724. " }\n"
  725. "}",
  726. Style);
  727. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
  728. verifyFormat("var func = function() {\n"
  729. " return 1;\n"
  730. "};",
  731. Style);
  732. }
  733. TEST_F(FormatTestJS, MultipleFunctionLiterals) {
  734. FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
  735. Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
  736. verifyFormat("promise.then(\n"
  737. " function success() {\n"
  738. " doFoo();\n"
  739. " doBar();\n"
  740. " },\n"
  741. " function error() {\n"
  742. " doFoo();\n"
  743. " doBaz();\n"
  744. " },\n"
  745. " []);\n");
  746. verifyFormat("promise.then(\n"
  747. " function success() {\n"
  748. " doFoo();\n"
  749. " doBar();\n"
  750. " },\n"
  751. " [],\n"
  752. " function error() {\n"
  753. " doFoo();\n"
  754. " doBaz();\n"
  755. " });\n");
  756. verifyFormat("promise.then(\n"
  757. " [],\n"
  758. " function success() {\n"
  759. " doFoo();\n"
  760. " doBar();\n"
  761. " },\n"
  762. " function error() {\n"
  763. " doFoo();\n"
  764. " doBaz();\n"
  765. " });\n");
  766. verifyFormat("getSomeLongPromise()\n"
  767. " .then(function(value) { body(); })\n"
  768. " .thenCatch(function(error) {\n"
  769. " body();\n"
  770. " body();\n"
  771. " });",
  772. Style);
  773. verifyFormat("getSomeLongPromise()\n"
  774. " .then(function(value) {\n"
  775. " body();\n"
  776. " body();\n"
  777. " })\n"
  778. " .thenCatch(function(error) {\n"
  779. " body();\n"
  780. " body();\n"
  781. " });");
  782. verifyFormat("getSomeLongPromise()\n"
  783. " .then(function(value) { body(); })\n"
  784. " .thenCatch(function(error) { body(); });",
  785. Style);
  786. verifyFormat("return [aaaaaaaaaaaaaaaaaaaaaa]\n"
  787. " .aaaaaaa(function() {\n"
  788. " //\n"
  789. " })\n"
  790. " .bbbbbb();");
  791. }
  792. TEST_F(FormatTestJS, ArrowFunctions) {
  793. verifyFormat("var x = (a) => {\n"
  794. " return a;\n"
  795. "};");
  796. verifyFormat("var x = (a) => {\n"
  797. " function y() {\n"
  798. " return 42;\n"
  799. " }\n"
  800. " return a;\n"
  801. "};");
  802. verifyFormat("var x = (a: type): {some: type} => {\n"
  803. " return a;\n"
  804. "};");
  805. verifyFormat("var x = (a) => a;");
  806. verifyFormat("return () => [];");
  807. verifyFormat("var aaaaaaaaaaaaaaaaaaaa = {\n"
  808. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
  809. " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  810. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) =>\n"
  811. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  812. "};");
  813. verifyFormat("var a = a.aaaaaaa(\n"
  814. " (a: a) => aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) &&\n"
  815. " aaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
  816. verifyFormat("var a = a.aaaaaaa(\n"
  817. " (a: a) => aaaaaaaaaaaaaaaaaaaaa(bbbbbbbbb) ?\n"
  818. " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb) :\n"
  819. " aaaaaaaaaaaaaaaaaaaaa(bbbbbbb));");
  820. // FIXME: This is bad, we should be wrapping before "() => {".
  821. verifyFormat("someFunction(() => {\n"
  822. " doSomething(); // break\n"
  823. "})\n"
  824. " .doSomethingElse(\n"
  825. " // break\n"
  826. " );");
  827. }
  828. TEST_F(FormatTestJS, ReturnStatements) {
  829. verifyFormat("function() {\n"
  830. " return [hello, world];\n"
  831. "}");
  832. }
  833. TEST_F(FormatTestJS, ForLoops) {
  834. verifyFormat("for (var i in [2, 3]) {\n"
  835. "}");
  836. verifyFormat("for (var i of [2, 3]) {\n"
  837. "}");
  838. verifyFormat("for (let {a, b} of x) {\n"
  839. "}");
  840. verifyFormat("for (let {a, b} in x) {\n"
  841. "}");
  842. }
  843. TEST_F(FormatTestJS, WrapRespectsAutomaticSemicolonInsertion) {
  844. // The following statements must not wrap, as otherwise the program meaning
  845. // would change due to automatic semicolon insertion.
  846. // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1.
  847. verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10));
  848. verifyFormat("return /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
  849. verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10));
  850. verifyFormat("continue /* hello! */ aaaaa;", getGoogleJSStyleWithColumns(10));
  851. verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10));
  852. verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10));
  853. verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10));
  854. verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10));
  855. verifyFormat("return [\n"
  856. " aaa\n"
  857. "];",
  858. getGoogleJSStyleWithColumns(12));
  859. }
  860. TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {
  861. verifyFormat("a\n"
  862. "b;",
  863. " a \n"
  864. " b ;");
  865. verifyFormat("a()\n"
  866. "b;",
  867. " a ()\n"
  868. " b ;");
  869. verifyFormat("a[b]\n"
  870. "c;",
  871. "a [b]\n"
  872. "c ;");
  873. verifyFormat("1\n"
  874. "a;",
  875. "1 \n"
  876. "a ;");
  877. verifyFormat("a\n"
  878. "1;",
  879. "a \n"
  880. "1 ;");
  881. verifyFormat("a\n"
  882. "'x';",
  883. "a \n"
  884. " 'x';");
  885. verifyFormat("a++\n"
  886. "b;",
  887. "a ++\n"
  888. "b ;");
  889. verifyFormat("a\n"
  890. "!b && c;",
  891. "a \n"
  892. " ! b && c;");
  893. verifyFormat("a\n"
  894. "if (1) f();",
  895. " a\n"
  896. " if (1) f();");
  897. verifyFormat("a\n"
  898. "class X {}",
  899. " a\n"
  900. " class X {}");
  901. verifyFormat("var a", "var\n"
  902. "a");
  903. verifyFormat("x instanceof String", "x\n"
  904. "instanceof\n"
  905. "String");
  906. verifyFormat("function f(@Foo bar) {}", "function f(@Foo\n"
  907. " bar) {}");
  908. verifyFormat("a = true\n"
  909. "return 1",
  910. "a = true\n"
  911. " return 1");
  912. verifyFormat("a = 's'\n"
  913. "return 1",
  914. "a = 's'\n"
  915. " return 1");
  916. verifyFormat("a = null\n"
  917. "return 1",
  918. "a = null\n"
  919. " return 1");
  920. // Below "class Y {}" should ideally be on its own line.
  921. verifyFormat(
  922. "x = {\n"
  923. " a: 1\n"
  924. "} class Y {}",
  925. " x = {a : 1}\n"
  926. " class Y { }");
  927. verifyFormat(
  928. "if (x) {\n"
  929. "}\n"
  930. "return 1",
  931. "if (x) {}\n"
  932. " return 1");
  933. verifyFormat(
  934. "if (x) {\n"
  935. "}\n"
  936. "class X {}",
  937. "if (x) {}\n"
  938. " class X {}");
  939. }
  940. TEST_F(FormatTestJS, ImportExportASI) {
  941. verifyFormat(
  942. "import {x} from 'y'\n"
  943. "export function z() {}",
  944. "import {x} from 'y'\n"
  945. " export function z() {}");
  946. // Below "class Y {}" should ideally be on its own line.
  947. verifyFormat(
  948. "export {x} class Y {}",
  949. " export {x}\n"
  950. " class Y {\n}");
  951. verifyFormat(
  952. "if (x) {\n"
  953. "}\n"
  954. "export class Y {}",
  955. "if ( x ) { }\n"
  956. " export class Y {}");
  957. }
  958. TEST_F(FormatTestJS, ClosureStyleCasts) {
  959. verifyFormat("var x = /** @type {foo} */ (bar);");
  960. }
  961. TEST_F(FormatTestJS, TryCatch) {
  962. verifyFormat("try {\n"
  963. " f();\n"
  964. "} catch (e) {\n"
  965. " g();\n"
  966. "} finally {\n"
  967. " h();\n"
  968. "}");
  969. // But, of course, "catch" is a perfectly fine function name in JavaScript.
  970. verifyFormat("someObject.catch();");
  971. verifyFormat("someObject.new();");
  972. verifyFormat("someObject.delete();");
  973. }
  974. TEST_F(FormatTestJS, StringLiteralConcatenation) {
  975. verifyFormat("var literal = 'hello ' +\n"
  976. " 'world';");
  977. }
  978. TEST_F(FormatTestJS, RegexLiteralClassification) {
  979. // Regex literals.
  980. verifyFormat("var regex = /abc/;");
  981. verifyFormat("f(/abc/);");
  982. verifyFormat("f(abc, /abc/);");
  983. verifyFormat("some_map[/abc/];");
  984. verifyFormat("var x = a ? /abc/ : /abc/;");
  985. verifyFormat("for (var i = 0; /abc/.test(s[i]); i++) {\n}");
  986. verifyFormat("var x = !/abc/.test(y);");
  987. verifyFormat("var x = foo()! / 10;");
  988. verifyFormat("var x = a && /abc/.test(y);");
  989. verifyFormat("var x = a || /abc/.test(y);");
  990. verifyFormat("var x = a + /abc/.search(y);");
  991. verifyFormat("/abc/.search(y);");
  992. verifyFormat("var regexs = {/abc/, /abc/};");
  993. verifyFormat("return /abc/;");
  994. // Not regex literals.
  995. verifyFormat("var a = a / 2 + b / 3;");
  996. verifyFormat("var a = a++ / 2;");
  997. // Prefix unary can operate on regex literals, not that it makes sense.
  998. verifyFormat("var a = ++/a/;");
  999. // This is a known issue, regular expressions are incorrectly detected if
  1000. // directly following a closing parenthesis.
  1001. verifyFormat("if (foo) / bar /.exec(baz);");
  1002. }
  1003. TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) {
  1004. verifyFormat("var regex = /=/;");
  1005. verifyFormat("var regex = /a*/;");
  1006. verifyFormat("var regex = /a+/;");
  1007. verifyFormat("var regex = /a?/;");
  1008. verifyFormat("var regex = /.a./;");
  1009. verifyFormat("var regex = /a\\*/;");
  1010. verifyFormat("var regex = /^a$/;");
  1011. verifyFormat("var regex = /\\/a/;");
  1012. verifyFormat("var regex = /(?:x)/;");
  1013. verifyFormat("var regex = /x(?=y)/;");
  1014. verifyFormat("var regex = /x(?!y)/;");
  1015. verifyFormat("var regex = /x|y/;");
  1016. verifyFormat("var regex = /a{2}/;");
  1017. verifyFormat("var regex = /a{1,3}/;");
  1018. verifyFormat("var regex = /[abc]/;");
  1019. verifyFormat("var regex = /[^abc]/;");
  1020. verifyFormat("var regex = /[\\b]/;");
  1021. verifyFormat("var regex = /[/]/;");
  1022. verifyFormat("var regex = /[\\/]/;");
  1023. verifyFormat("var regex = /\\[/;");
  1024. verifyFormat("var regex = /\\\\[/]/;");
  1025. verifyFormat("var regex = /}[\"]/;");
  1026. verifyFormat("var regex = /}[/\"]/;");
  1027. verifyFormat("var regex = /}[\"/]/;");
  1028. verifyFormat("var regex = /\\b/;");
  1029. verifyFormat("var regex = /\\B/;");
  1030. verifyFormat("var regex = /\\d/;");
  1031. verifyFormat("var regex = /\\D/;");
  1032. verifyFormat("var regex = /\\f/;");
  1033. verifyFormat("var regex = /\\n/;");
  1034. verifyFormat("var regex = /\\r/;");
  1035. verifyFormat("var regex = /\\s/;");
  1036. verifyFormat("var regex = /\\S/;");
  1037. verifyFormat("var regex = /\\t/;");
  1038. verifyFormat("var regex = /\\v/;");
  1039. verifyFormat("var regex = /\\w/;");
  1040. verifyFormat("var regex = /\\W/;");
  1041. verifyFormat("var regex = /a(a)\\1/;");
  1042. verifyFormat("var regex = /\\0/;");
  1043. verifyFormat("var regex = /\\\\/g;");
  1044. verifyFormat("var regex = /\\a\\\\/g;");
  1045. verifyFormat("var regex = /\a\\//g;");
  1046. verifyFormat("var regex = /a\\//;\n"
  1047. "var x = 0;");
  1048. verifyFormat("var regex = /'/g;", "var regex = /'/g ;");
  1049. verifyFormat("var regex = /'/g; //'", "var regex = /'/g ; //'");
  1050. verifyFormat("var regex = /\\/*/;\n"
  1051. "var x = 0;",
  1052. "var regex = /\\/*/;\n"
  1053. "var x=0;");
  1054. verifyFormat("var x = /a\\//;", "var x = /a\\// \n;");
  1055. verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16));
  1056. verifyFormat("var regex =\n"
  1057. " /\"/;",
  1058. getGoogleJSStyleWithColumns(15));
  1059. verifyFormat("var regex = //\n"
  1060. " /a/;");
  1061. verifyFormat("var regexs = [\n"
  1062. " /d/, //\n"
  1063. " /aa/, //\n"
  1064. "];");
  1065. }
  1066. TEST_F(FormatTestJS, RegexLiteralModifiers) {
  1067. verifyFormat("var regex = /abc/g;");
  1068. verifyFormat("var regex = /abc/i;");
  1069. verifyFormat("var regex = /abc/m;");
  1070. verifyFormat("var regex = /abc/y;");
  1071. }
  1072. TEST_F(FormatTestJS, RegexLiteralLength) {
  1073. verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
  1074. getGoogleJSStyleWithColumns(60));
  1075. verifyFormat("var regex =\n"
  1076. " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
  1077. getGoogleJSStyleWithColumns(60));
  1078. verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
  1079. getGoogleJSStyleWithColumns(50));
  1080. }
  1081. TEST_F(FormatTestJS, RegexLiteralExamples) {
  1082. verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
  1083. }
  1084. TEST_F(FormatTestJS, IgnoresMpegTS) {
  1085. std::string MpegTS(200, ' ');
  1086. MpegTS.replace(0, strlen("nearlyLooks + like + ts + code; "),
  1087. "nearlyLooks + like + ts + code; ");
  1088. MpegTS[0] = 0x47;
  1089. MpegTS[188] = 0x47;
  1090. verifyFormat(MpegTS, MpegTS);
  1091. }
  1092. TEST_F(FormatTestJS, TypeAnnotations) {
  1093. verifyFormat("var x: string;");
  1094. verifyFormat("var x: {a: string; b: number;} = {};");
  1095. verifyFormat("function x(): string {\n return 'x';\n}");
  1096. verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}");
  1097. verifyFormat("function x(y: string): string {\n return 'x';\n}");
  1098. verifyFormat("for (var y: string in x) {\n x();\n}");
  1099. verifyFormat("for (var y: string of x) {\n x();\n}");
  1100. verifyFormat("function x(y: {a?: number;} = {}): number {\n"
  1101. " return 12;\n"
  1102. "}");
  1103. verifyFormat("((a: string, b: number): string => a + b);");
  1104. verifyFormat("var x: (y: number) => string;");
  1105. verifyFormat("var x: P<string, (a: number) => string>;");
  1106. verifyFormat("var x = {\n"
  1107. " y: function(): z {\n"
  1108. " return 1;\n"
  1109. " }\n"
  1110. "};");
  1111. verifyFormat("var x = {\n"
  1112. " y: function(): {a: number} {\n"
  1113. " return 1;\n"
  1114. " }\n"
  1115. "};");
  1116. verifyFormat("function someFunc(args: string[]):\n"
  1117. " {longReturnValue: string[]} {}",
  1118. getGoogleJSStyleWithColumns(60));
  1119. verifyFormat(
  1120. "var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])\n"
  1121. " .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
  1122. }
  1123. TEST_F(FormatTestJS, UnionIntersectionTypes) {
  1124. verifyFormat("let x: A|B = A | B;");
  1125. verifyFormat("let x: A&B|C = A & B;");
  1126. verifyFormat("let x: Foo<A|B> = new Foo<A|B>();");
  1127. verifyFormat("function(x: A|B): C&D {}");
  1128. verifyFormat("function(x: A|B = A | B): C&D {}");
  1129. verifyFormat("function x(path: number|string) {}");
  1130. verifyFormat("function x(): string|number {}");
  1131. verifyFormat("type Foo = Bar|Baz;");
  1132. verifyFormat("type Foo = Bar<X>|Baz;");
  1133. verifyFormat("type Foo = (Bar<X>|Baz);");
  1134. verifyFormat("let x: Bar|Baz;");
  1135. verifyFormat("let x: Bar<X>|Baz;");
  1136. verifyFormat("let x: (Foo|Bar)[];");
  1137. }
  1138. TEST_F(FormatTestJS, ClassDeclarations) {
  1139. verifyFormat("class C {\n x: string = 12;\n}");
  1140. verifyFormat("class C {\n x(): string => 12;\n}");
  1141. verifyFormat("class C {\n ['x' + 2]: string = 12;\n}");
  1142. verifyFormat("class C {\n private x: string = 12;\n}");
  1143. verifyFormat("class C {\n private static x: string = 12;\n}");
  1144. verifyFormat("class C {\n static x(): string {\n return 'asd';\n }\n}");
  1145. verifyFormat("class C extends P implements I {}");
  1146. verifyFormat("class C extends p.P implements i.I {}");
  1147. verifyFormat(
  1148. "x(class {\n"
  1149. " a(): A {}\n"
  1150. "});");
  1151. verifyFormat("class Test {\n"
  1152. " aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaa):\n"
  1153. " aaaaaaaaaaaaaaaaaaaaaa {}\n"
  1154. "}");
  1155. verifyFormat("foo = class Name {\n"
  1156. " constructor() {}\n"
  1157. "};");
  1158. verifyFormat("foo = class {\n"
  1159. " constructor() {}\n"
  1160. "};");
  1161. verifyFormat("class C {\n"
  1162. " x: {y: Z;} = {};\n"
  1163. " private y: {y: Z;} = {};\n"
  1164. "}");
  1165. // ':' is not a type declaration here.
  1166. verifyFormat("class X {\n"
  1167. " subs = {\n"
  1168. " 'b': {\n"
  1169. " 'c': 1,\n"
  1170. " },\n"
  1171. " };\n"
  1172. "}");
  1173. verifyFormat("@Component({\n"
  1174. " moduleId: module.id,\n"
  1175. "})\n"
  1176. "class SessionListComponent implements OnDestroy, OnInit {\n"
  1177. "}");
  1178. }
  1179. TEST_F(FormatTestJS, InterfaceDeclarations) {
  1180. verifyFormat("interface I {\n"
  1181. " x: string;\n"
  1182. " enum: string[];\n"
  1183. " enum?: string[];\n"
  1184. "}\n"
  1185. "var y;");
  1186. // Ensure that state is reset after parsing the interface.
  1187. verifyFormat("interface a {}\n"
  1188. "export function b() {}\n"
  1189. "var x;");
  1190. // Arrays of object type literals.
  1191. verifyFormat("interface I {\n"
  1192. " o: {}[];\n"
  1193. "}");
  1194. }
  1195. TEST_F(FormatTestJS, EnumDeclarations) {
  1196. verifyFormat("enum Foo {\n"
  1197. " A = 1,\n"
  1198. " B\n"
  1199. "}");
  1200. verifyFormat("export /* somecomment*/ enum Foo {\n"
  1201. " A = 1,\n"
  1202. " B\n"
  1203. "}");
  1204. verifyFormat("enum Foo {\n"
  1205. " A = 1, // comment\n"
  1206. " B\n"
  1207. "}\n"
  1208. "var x = 1;");
  1209. }
  1210. TEST_F(FormatTestJS, MetadataAnnotations) {
  1211. verifyFormat("@A\nclass C {\n}");
  1212. verifyFormat("@A({arg: 'value'})\nclass C {\n}");
  1213. verifyFormat("@A\n@B\nclass C {\n}");
  1214. verifyFormat("class C {\n @A x: string;\n}");
  1215. verifyFormat("class C {\n"
  1216. " @A\n"
  1217. " private x(): string {\n"
  1218. " return 'y';\n"
  1219. " }\n"
  1220. "}");
  1221. verifyFormat("class C {\n"
  1222. " private x(@A x: string) {}\n"
  1223. "}");
  1224. verifyFormat("class X {}\n"
  1225. "class Y {}");
  1226. verifyFormat("class X {\n"
  1227. " @property() private isReply = false;\n"
  1228. "}\n");
  1229. }
  1230. TEST_F(FormatTestJS, TypeAliases) {
  1231. verifyFormat("type X = number;\n"
  1232. "class C {}");
  1233. verifyFormat("type X<Y> = Z<Y>;");
  1234. verifyFormat("type X = {\n"
  1235. " y: number\n"
  1236. "};\n"
  1237. "class C {}");
  1238. }
  1239. TEST_F(FormatTestJS, TypeInterfaceLineWrapping) {
  1240. const FormatStyle &Style = getGoogleJSStyleWithColumns(20);
  1241. verifyFormat("type LongTypeIsReallyUnreasonablyLong =\n"
  1242. " string;\n",
  1243. "type LongTypeIsReallyUnreasonablyLong = string;\n",
  1244. Style);
  1245. verifyFormat(
  1246. "interface AbstractStrategyFactoryProvider {\n"
  1247. " a: number\n"
  1248. "}\n",
  1249. "interface AbstractStrategyFactoryProvider { a: number }\n",
  1250. Style);
  1251. }
  1252. TEST_F(FormatTestJS, Modules) {
  1253. verifyFormat("import SomeThing from 'some/module.js';");
  1254. verifyFormat("import {X, Y} from 'some/module.js';");
  1255. verifyFormat("import a, {X, Y} from 'some/module.js';");
  1256. verifyFormat("import {X, Y,} from 'some/module.js';");
  1257. verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
  1258. // Ensure Automatic Semicolon Insertion does not break on "as\n".
  1259. verifyFormat("import {X as myX} from 'm';", "import {X as\n"
  1260. " myX} from 'm';");
  1261. verifyFormat("import * as lib from 'some/module.js';");
  1262. verifyFormat("var x = {import: 1};\nx.import = 2;");
  1263. verifyFormat("export function fn() {\n"
  1264. " return 'fn';\n"
  1265. "}");
  1266. verifyFormat("export function A() {}\n"
  1267. "export default function B() {}\n"
  1268. "export function C() {}");
  1269. verifyFormat("export default () => {\n"
  1270. " let x = 1;\n"
  1271. " return x;\n"
  1272. "}");
  1273. verifyFormat("export const x = 12;");
  1274. verifyFormat("export default class X {}");
  1275. verifyFormat("export {X, Y} from 'some/module.js';");
  1276. verifyFormat("export {X, Y,} from 'some/module.js';");
  1277. verifyFormat("export {SomeVeryLongExport as X, "
  1278. "SomeOtherVeryLongExport as Y} from 'some/module.js';");
  1279. // export without 'from' is wrapped.
  1280. verifyFormat("export let someRatherLongVariableName =\n"
  1281. " someSurprisinglyLongVariable + someOtherRatherLongVar;");
  1282. // ... but not if from is just an identifier.
  1283. verifyFormat("export {\n"
  1284. " from as from,\n"
  1285. " someSurprisinglyLongVariable as\n"
  1286. " from\n"
  1287. "};",
  1288. getGoogleJSStyleWithColumns(20));
  1289. verifyFormat("export class C {\n"
  1290. " x: number;\n"
  1291. " y: string;\n"
  1292. "}");
  1293. verifyFormat("export class X { y: number; }");
  1294. verifyFormat("export abstract class X { y: number; }");
  1295. verifyFormat("export default class X { y: number }");
  1296. verifyFormat("export default function() {\n return 1;\n}");
  1297. verifyFormat("export var x = 12;");
  1298. verifyFormat("class C {}\n"
  1299. "export function f() {}\n"
  1300. "var v;");
  1301. verifyFormat("export var x: number = 12;");
  1302. verifyFormat("export const y = {\n"
  1303. " a: 1,\n"
  1304. " b: 2\n"
  1305. "};");
  1306. verifyFormat("export enum Foo {\n"
  1307. " BAR,\n"
  1308. " // adsdasd\n"
  1309. " BAZ\n"
  1310. "}");
  1311. verifyFormat("export default [\n"
  1312. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1313. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
  1314. "];");
  1315. verifyFormat("export default [];");
  1316. verifyFormat("export default () => {};");
  1317. verifyFormat("export interface Foo { foo: number; }\n"
  1318. "export class Bar {\n"
  1319. " blah(): string {\n"
  1320. " return this.blah;\n"
  1321. " };\n"
  1322. "}");
  1323. }
  1324. TEST_F(FormatTestJS, ImportWrapping) {
  1325. verifyFormat("import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,"
  1326. " VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying"
  1327. "} from 'some/module.js';");
  1328. FormatStyle Style = getGoogleJSStyleWithColumns(80);
  1329. Style.JavaScriptWrapImports = true;
  1330. verifyFormat("import {\n"
  1331. " VeryLongImportsAreAnnoying,\n"
  1332. " VeryLongImportsAreAnnoying,\n"
  1333. " VeryLongImportsAreAnnoying,\n"
  1334. "} from 'some/module.js';",
  1335. Style);
  1336. verifyFormat("import {\n"
  1337. " A,\n"
  1338. " A,\n"
  1339. "} from 'some/module.js';",
  1340. Style);
  1341. verifyFormat("export {\n"
  1342. " A,\n"
  1343. " A,\n"
  1344. "} from 'some/module.js';",
  1345. Style);
  1346. }
  1347. TEST_F(FormatTestJS, TemplateStrings) {
  1348. // Keeps any whitespace/indentation within the template string.
  1349. verifyFormat("var x = `hello\n"
  1350. " ${name}\n"
  1351. " !`;",
  1352. "var x = `hello\n"
  1353. " ${ name }\n"
  1354. " !`;");
  1355. verifyFormat("var x =\n"
  1356. " `hello ${world}` >= some();",
  1357. getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
  1358. verifyFormat("var x = `hello ${world}` >= some();",
  1359. getGoogleJSStyleWithColumns(35)); // Barely fits.
  1360. verifyFormat("var x = `hellö ${wörld}` >= söme();",
  1361. getGoogleJSStyleWithColumns(35)); // Fits due to UTF-8.
  1362. verifyFormat("var x = `hello\n"
  1363. " ${world}` >=\n"
  1364. " some();",
  1365. "var x =\n"
  1366. " `hello\n"
  1367. " ${world}` >= some();",
  1368. getGoogleJSStyleWithColumns(21)); // Barely doesn't fit.
  1369. verifyFormat("var x = `hello\n"
  1370. " ${world}` >= some();",
  1371. "var x =\n"
  1372. " `hello\n"
  1373. " ${world}` >= some();",
  1374. getGoogleJSStyleWithColumns(22)); // Barely fits.
  1375. verifyFormat("var x =\n"
  1376. " `h`;",
  1377. getGoogleJSStyleWithColumns(11));
  1378. verifyFormat("var x =\n `multi\n line`;", "var x = `multi\n line`;",
  1379. getGoogleJSStyleWithColumns(13));
  1380. verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
  1381. " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);");
  1382. // Repro for an obscure width-miscounting issue with template strings.
  1383. verifyFormat(
  1384. "someLongVariable =\n"
  1385. " "
  1386. "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;",
  1387. "someLongVariable = "
  1388. "`${logPrefix[11]}/${logPrefix[12]}/${logPrefix[13]}${logPrefix[14]}`;");
  1389. // Make sure template strings get a proper ColumnWidth assigned, even if they
  1390. // are first token in line.
  1391. verifyFormat(
  1392. "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
  1393. " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;");
  1394. // Two template strings.
  1395. verifyFormat("var x = `hello` == `hello`;");
  1396. // Comments in template strings.
  1397. verifyFormat("var x = `//a`;\n"
  1398. "var y;",
  1399. "var x =\n `//a`;\n"
  1400. "var y ;");
  1401. verifyFormat("var x = `/*a`;\n"
  1402. "var y;",
  1403. "var x =\n `/*a`;\n"
  1404. "var y;");
  1405. // Unterminated string literals in a template string.
  1406. verifyFormat("var x = `'`; // comment with matching quote '\n"
  1407. "var y;");
  1408. verifyFormat("var x = `\"`; // comment with matching quote \"\n"
  1409. "var y;");
  1410. verifyFormat("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa);",
  1411. "it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa) ;",
  1412. getGoogleJSStyleWithColumns(40));
  1413. // Backticks in a comment - not a template string.
  1414. verifyFormat("var x = 1 // `/*a`;\n"
  1415. " ;",
  1416. "var x =\n 1 // `/*a`;\n"
  1417. " ;");
  1418. verifyFormat("/* ` */ var x = 1; /* ` */", "/* ` */ var x\n= 1; /* ` */");
  1419. // Comment spans multiple template strings.
  1420. verifyFormat("var x = `/*a`;\n"
  1421. "var y = ` */ `;",
  1422. "var x =\n `/*a`;\n"
  1423. "var y =\n ` */ `;");
  1424. // Escaped backtick.
  1425. verifyFormat("var x = ` \\` a`;\n"
  1426. "var y;",
  1427. "var x = ` \\` a`;\n"
  1428. "var y;");
  1429. // Escaped dollar.
  1430. verifyFormat("var x = ` \\${foo}`;\n");
  1431. // The token stream can contain two string_literals in sequence, but that
  1432. // doesn't mean that they are implicitly concatenated in JavaScript.
  1433. verifyFormat("var f = `aaaa ${a ? 'a' : 'b'}`;");
  1434. // Ensure that scopes are appropriately set around evaluated expressions in
  1435. // template strings.
  1436. verifyFormat("var f = `aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa\n"
  1437. " aaaaaaaaaaaaa:${aaaaaaa.aaaaa} aaaaaaaa`;",
  1438. "var f = `aaaaaaaaaaaaa:${aaaaaaa. aaaaa} aaaaaaaa\n"
  1439. " aaaaaaaaaaaaa:${ aaaaaaa. aaaaa} aaaaaaaa`;");
  1440. verifyFormat("var x = someFunction(`${})`) //\n"
  1441. " .oooooooooooooooooon();");
  1442. verifyFormat("var x = someFunction(`${aaaa}${\n"
  1443. " aaaaa( //\n"
  1444. " aaaaa)\n"
  1445. " })`);");
  1446. }
  1447. TEST_F(FormatTestJS, TemplateStringMultiLineExpression) {
  1448. verifyFormat("var f = `aaaaaaaaaaaaaaaaaa: ${\n"
  1449. " aaaaa + //\n"
  1450. " bbbb\n"
  1451. " }`;",
  1452. "var f = `aaaaaaaaaaaaaaaaaa: ${aaaaa + //\n"
  1453. " bbbb}`;");
  1454. verifyFormat("var f = `\n"
  1455. " aaaaaaaaaaaaaaaaaa: ${\n"
  1456. " aaaaa + //\n"
  1457. " bbbb\n"
  1458. " }`;",
  1459. "var f = `\n"
  1460. " aaaaaaaaaaaaaaaaaa: ${ aaaaa + //\n"
  1461. " bbbb }`;");
  1462. verifyFormat("var f = `\n"
  1463. " aaaaaaaaaaaaaaaaaa: ${\n"
  1464. " someFunction(\n"
  1465. " aaaaa + //\n"
  1466. " bbbb)\n"
  1467. " }`;",
  1468. "var f = `\n"
  1469. " aaaaaaaaaaaaaaaaaa: ${someFunction (\n"
  1470. " aaaaa + //\n"
  1471. " bbbb)}`;");
  1472. // It might be preferable to wrap before "someFunction".
  1473. verifyFormat("var f = `\n"
  1474. " aaaaaaaaaaaaaaaaaa: ${someFunction({\n"
  1475. " aaaa: aaaaa,\n"
  1476. " bbbb: bbbbb,\n"
  1477. " })}`;",
  1478. "var f = `\n"
  1479. " aaaaaaaaaaaaaaaaaa: ${someFunction ({\n"
  1480. " aaaa: aaaaa,\n"
  1481. " bbbb: bbbbb,\n"
  1482. " })}`;");
  1483. }
  1484. TEST_F(FormatTestJS, TemplateStringASI) {
  1485. verifyFormat("var x = `hello${world}`;", "var x = `hello${\n"
  1486. " world\n"
  1487. "}`;");
  1488. }
  1489. TEST_F(FormatTestJS, NestedTemplateStrings) {
  1490. verifyFormat(
  1491. "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;");
  1492. verifyFormat("var x = `he${({text: 'll'}.text)}o`;");
  1493. // Crashed at some point.
  1494. verifyFormat("}");
  1495. }
  1496. TEST_F(FormatTestJS, TaggedTemplateStrings) {
  1497. verifyFormat("var x = html`<ul>`;");
  1498. }
  1499. TEST_F(FormatTestJS, CastSyntax) {
  1500. verifyFormat("var x = <type>foo;");
  1501. verifyFormat("var x = foo as type;");
  1502. verifyFormat("let x = (a + b) as\n"
  1503. " LongTypeIsLong;",
  1504. getGoogleJSStyleWithColumns(20));
  1505. verifyFormat("foo = <Bar[]>[\n"
  1506. " 1, //\n"
  1507. " 2\n"
  1508. "];");
  1509. verifyFormat("var x = [{x: 1} as type];");
  1510. verifyFormat("x = x as [a, b];");
  1511. verifyFormat("x = x as {a: string};");
  1512. verifyFormat("x = x as (string);");
  1513. verifyFormat("x = x! as (string);");
  1514. verifyFormat("var x = something.someFunction() as\n"
  1515. " something;",
  1516. getGoogleJSStyleWithColumns(40));
  1517. }
  1518. TEST_F(FormatTestJS, TypeArguments) {
  1519. verifyFormat("class X<Y> {}");
  1520. verifyFormat("new X<Y>();");
  1521. verifyFormat("foo<Y>(a);");
  1522. verifyFormat("var x: X<Y>[];");
  1523. verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
  1524. verifyFormat("function f(a: List<any> = null) {}");
  1525. verifyFormat("function f(): List<any> {}");
  1526. verifyFormat("function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa():\n"
  1527. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}");
  1528. verifyFormat("function aaaaaaaaaa(\n"
  1529. " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa,\n"
  1530. " aaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaa):\n"
  1531. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}");
  1532. }
  1533. TEST_F(FormatTestJS, UserDefinedTypeGuards) {
  1534. verifyFormat(
  1535. "function foo(check: Object):\n"
  1536. " check is {foo: string, bar: string, baz: string, foobar: string} {\n"
  1537. " return 'bar' in check;\n"
  1538. "}\n");
  1539. }
  1540. TEST_F(FormatTestJS, OptionalTypes) {
  1541. verifyFormat("function x(a?: b, c?, d?) {}");
  1542. verifyFormat("class X {\n"
  1543. " y?: z;\n"
  1544. " z?;\n"
  1545. "}");
  1546. verifyFormat("interface X {\n"
  1547. " y?(): z;\n"
  1548. "}");
  1549. verifyFormat("constructor({aa}: {\n"
  1550. " aa?: string,\n"
  1551. " aaaaaaaa?: string,\n"
  1552. " aaaaaaaaaaaaaaa?: boolean,\n"
  1553. " aaaaaa?: List<string>\n"
  1554. "}) {}");
  1555. }
  1556. TEST_F(FormatTestJS, IndexSignature) {
  1557. verifyFormat("var x: {[k: string]: v};");
  1558. }
  1559. TEST_F(FormatTestJS, WrapAfterParen) {
  1560. verifyFormat("xxxxxxxxxxx(\n"
  1561. " aaa, aaa);",
  1562. getGoogleJSStyleWithColumns(20));
  1563. verifyFormat("xxxxxxxxxxx(\n"
  1564. " aaa, aaa, aaa,\n"
  1565. " aaa, aaa, aaa);",
  1566. getGoogleJSStyleWithColumns(20));
  1567. verifyFormat("xxxxxxxxxxx(\n"
  1568. " aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  1569. " function(x) {\n"
  1570. " y(); //\n"
  1571. " });",
  1572. getGoogleJSStyleWithColumns(40));
  1573. verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
  1574. " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
  1575. }
  1576. TEST_F(FormatTestJS, JSDocAnnotations) {
  1577. verifyFormat("/**\n"
  1578. " * @export {this.is.a.long.path.to.a.Type}\n"
  1579. " */",
  1580. "/**\n"
  1581. " * @export {this.is.a.long.path.to.a.Type}\n"
  1582. " */",
  1583. getGoogleJSStyleWithColumns(20));
  1584. verifyFormat("/**\n"
  1585. " * @mods {this.is.a.long.path.to.a.Type}\n"
  1586. " */",
  1587. "/**\n"
  1588. " * @mods {this.is.a.long.path.to.a.Type}\n"
  1589. " */",
  1590. getGoogleJSStyleWithColumns(20));
  1591. verifyFormat("/**\n"
  1592. " * @param {this.is.a.long.path.to.a.Type}\n"
  1593. " */",
  1594. "/**\n"
  1595. " * @param {this.is.a.long.path.to.a.Type}\n"
  1596. " */",
  1597. getGoogleJSStyleWithColumns(20));
  1598. verifyFormat("/**\n"
  1599. " * @see http://very/very/long/url/is/long\n"
  1600. " */",
  1601. "/**\n"
  1602. " * @see http://very/very/long/url/is/long\n"
  1603. " */",
  1604. getGoogleJSStyleWithColumns(20));
  1605. verifyFormat(
  1606. "/**\n"
  1607. " * @param This is a\n"
  1608. " * long comment but\n"
  1609. " * no type\n"
  1610. " */",
  1611. "/**\n"
  1612. " * @param This is a long comment but no type\n"
  1613. " */",
  1614. getGoogleJSStyleWithColumns(20));
  1615. // Don't break @param line, but reindent it and reflow unrelated lines.
  1616. verifyFormat("{\n"
  1617. " /**\n"
  1618. " * long long long\n"
  1619. " * long\n"
  1620. " * @param {this.is.a.long.path.to.a.Type} a\n"
  1621. " * long long long\n"
  1622. " * long long\n"
  1623. " */\n"
  1624. " function f(a) {}\n"
  1625. "}",
  1626. "{\n"
  1627. "/**\n"
  1628. " * long long long long\n"
  1629. " * @param {this.is.a.long.path.to.a.Type} a\n"
  1630. " * long long long long\n"
  1631. " * long\n"
  1632. " */\n"
  1633. " function f(a) {}\n"
  1634. "}",
  1635. getGoogleJSStyleWithColumns(20));
  1636. }
  1637. TEST_F(FormatTestJS, RequoteStringsSingle) {
  1638. verifyFormat("var x = 'foo';", "var x = \"foo\";");
  1639. verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo'o'\";");
  1640. verifyFormat("var x = 'fo\\'o\\'';", "var x = \"fo\\'o'\";");
  1641. verifyFormat(
  1642. "var x =\n"
  1643. " 'foo\\'';",
  1644. // Code below is 15 chars wide, doesn't fit into the line with the
  1645. // \ escape added.
  1646. "var x = \"foo'\";", getGoogleJSStyleWithColumns(15));
  1647. // Removes no-longer needed \ escape from ".
  1648. verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";");
  1649. // Code below fits into 15 chars *after* removing the \ escape.
  1650. verifyFormat("var x = 'fo\"o';", "var x = \"fo\\\"o\";",
  1651. getGoogleJSStyleWithColumns(15));
  1652. verifyFormat("// clang-format off\n"
  1653. "let x = \"double\";\n"
  1654. "// clang-format on\n"
  1655. "let x = 'single';\n",
  1656. "// clang-format off\n"
  1657. "let x = \"double\";\n"
  1658. "// clang-format on\n"
  1659. "let x = \"single\";\n");
  1660. }
  1661. TEST_F(FormatTestJS, RequoteAndIndent) {
  1662. verifyFormat("let x = someVeryLongFunctionThatGoesOnAndOn(\n"
  1663. " 'double quoted string that needs wrapping');",
  1664. "let x = someVeryLongFunctionThatGoesOnAndOn("
  1665. "\"double quoted string that needs wrapping\");");
  1666. verifyFormat("let x =\n"
  1667. " 'foo\\'oo';\n"
  1668. "let x =\n"
  1669. " 'foo\\'oo';",
  1670. "let x=\"foo'oo\";\n"
  1671. "let x=\"foo'oo\";",
  1672. getGoogleJSStyleWithColumns(15));
  1673. }
  1674. TEST_F(FormatTestJS, RequoteStringsDouble) {
  1675. FormatStyle DoubleQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
  1676. DoubleQuotes.JavaScriptQuotes = FormatStyle::JSQS_Double;
  1677. verifyFormat("var x = \"foo\";", DoubleQuotes);
  1678. verifyFormat("var x = \"foo\";", "var x = 'foo';", DoubleQuotes);
  1679. verifyFormat("var x = \"fo'o\";", "var x = 'fo\\'o';", DoubleQuotes);
  1680. }
  1681. TEST_F(FormatTestJS, RequoteStringsLeave) {
  1682. FormatStyle LeaveQuotes = getGoogleStyle(FormatStyle::LK_JavaScript);
  1683. LeaveQuotes.JavaScriptQuotes = FormatStyle::JSQS_Leave;
  1684. verifyFormat("var x = \"foo\";", LeaveQuotes);
  1685. verifyFormat("var x = 'foo';", LeaveQuotes);
  1686. }
  1687. TEST_F(FormatTestJS, SupportShebangLines) {
  1688. verifyFormat("#!/usr/bin/env node\n"
  1689. "var x = hello();",
  1690. "#!/usr/bin/env node\n"
  1691. "var x = hello();");
  1692. }
  1693. TEST_F(FormatTestJS, NonNullAssertionOperator) {
  1694. verifyFormat("let x = foo!.bar();\n");
  1695. verifyFormat("let x = foo ? bar! : baz;\n");
  1696. verifyFormat("let x = !foo;\n");
  1697. verifyFormat("let x = foo[0]!;\n");
  1698. verifyFormat("let x = (foo)!;\n");
  1699. verifyFormat("let x = x(foo!);\n");
  1700. verifyFormat(
  1701. "a.aaaaaa(a.a!).then(\n"
  1702. " x => x(x));\n",
  1703. getGoogleJSStyleWithColumns(20));
  1704. verifyFormat("let x = foo! - 1;\n");
  1705. verifyFormat("let x = {foo: 1}!;\n");
  1706. verifyFormat(
  1707. "let x = hello.foo()!\n"
  1708. " .foo()!\n"
  1709. " .foo()!\n"
  1710. " .foo()!;\n",
  1711. getGoogleJSStyleWithColumns(20));
  1712. verifyFormat("let x = namespace!;\n");
  1713. verifyFormat("return !!x;\n");
  1714. }
  1715. TEST_F(FormatTestJS, Conditional) {
  1716. verifyFormat("y = x ? 1 : 2;");
  1717. verifyFormat("x ? 1 : 2;");
  1718. verifyFormat("class Foo {\n"
  1719. " field = true ? 1 : 2;\n"
  1720. " method(a = true ? 1 : 2) {}\n"
  1721. "}");
  1722. }
  1723. TEST_F(FormatTestJS, ImportComments) {
  1724. verifyFormat("import {x} from 'x'; // from some location",
  1725. getGoogleJSStyleWithColumns(25));
  1726. verifyFormat("// taze: x from 'location'", getGoogleJSStyleWithColumns(10));
  1727. verifyFormat("/// <reference path=\"some/location\" />", getGoogleJSStyleWithColumns(10));
  1728. }
  1729. TEST_F(FormatTestJS, Exponentiation) {
  1730. verifyFormat("squared = x ** 2;");
  1731. verifyFormat("squared **= 2;");
  1732. }
  1733. } // end namespace tooling
  1734. } // end namespace clang