printer.js 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Printer = void 0;
  4. var tslib_1 = require("tslib");
  5. var assert_1 = tslib_1.__importDefault(require("assert"));
  6. var types = tslib_1.__importStar(require("ast-types"));
  7. var comments_1 = require("./comments");
  8. var fast_path_1 = tslib_1.__importDefault(require("./fast-path"));
  9. var lines_1 = require("./lines");
  10. var options_1 = require("./options");
  11. var patcher_1 = require("./patcher");
  12. var util = tslib_1.__importStar(require("./util"));
  13. var namedTypes = types.namedTypes;
  14. var isString = types.builtInTypes.string;
  15. var isObject = types.builtInTypes.object;
  16. var PrintResult = function PrintResult(code, sourceMap) {
  17. assert_1.default.ok(this instanceof PrintResult);
  18. isString.assert(code);
  19. this.code = code;
  20. if (sourceMap) {
  21. isObject.assert(sourceMap);
  22. this.map = sourceMap;
  23. }
  24. };
  25. var PRp = PrintResult.prototype;
  26. var warnedAboutToString = false;
  27. PRp.toString = function () {
  28. if (!warnedAboutToString) {
  29. console.warn("Deprecation warning: recast.print now returns an object with " +
  30. "a .code property. You appear to be treating the object as a " +
  31. "string, which might still work but is strongly discouraged.");
  32. warnedAboutToString = true;
  33. }
  34. return this.code;
  35. };
  36. var emptyPrintResult = new PrintResult("");
  37. var Printer = function Printer(config) {
  38. assert_1.default.ok(this instanceof Printer);
  39. var explicitTabWidth = config && config.tabWidth;
  40. config = (0, options_1.normalize)(config);
  41. // It's common for client code to pass the same options into both
  42. // recast.parse and recast.print, but the Printer doesn't need (and
  43. // can be confused by) config.sourceFileName, so we null it out.
  44. config.sourceFileName = null;
  45. // Non-destructively modifies options with overrides, and returns a
  46. // new print function that uses the modified options.
  47. function makePrintFunctionWith(options, overrides) {
  48. options = Object.assign({}, options, overrides);
  49. return function (path) { return print(path, options); };
  50. }
  51. function print(path, options) {
  52. assert_1.default.ok(path instanceof fast_path_1.default);
  53. options = options || {};
  54. if (options.includeComments) {
  55. return (0, comments_1.printComments)(path, makePrintFunctionWith(options, {
  56. includeComments: false,
  57. }));
  58. }
  59. var oldTabWidth = config.tabWidth;
  60. if (!explicitTabWidth) {
  61. var loc = path.getNode().loc;
  62. if (loc && loc.lines && loc.lines.guessTabWidth) {
  63. config.tabWidth = loc.lines.guessTabWidth();
  64. }
  65. }
  66. var reprinter = (0, patcher_1.getReprinter)(path);
  67. var lines = reprinter
  68. ? // Since the print function that we pass to the reprinter will
  69. // be used to print "new" nodes, it's tempting to think we
  70. // should pass printRootGenerically instead of print, to avoid
  71. // calling maybeReprint again, but that would be a mistake
  72. // because the new nodes might not be entirely new, but merely
  73. // moved from elsewhere in the AST. The print function is the
  74. // right choice because it gives us the opportunity to reprint
  75. // such nodes using their original source.
  76. reprinter(print)
  77. : genericPrint(path, config, options, makePrintFunctionWith(options, {
  78. includeComments: true,
  79. avoidRootParens: false,
  80. }));
  81. config.tabWidth = oldTabWidth;
  82. return lines;
  83. }
  84. this.print = function (ast) {
  85. if (!ast) {
  86. return emptyPrintResult;
  87. }
  88. var lines = print(fast_path_1.default.from(ast), {
  89. includeComments: true,
  90. avoidRootParens: false,
  91. });
  92. return new PrintResult(lines.toString(config), util.composeSourceMaps(config.inputSourceMap, lines.getSourceMap(config.sourceMapName, config.sourceRoot)));
  93. };
  94. this.printGenerically = function (ast) {
  95. if (!ast) {
  96. return emptyPrintResult;
  97. }
  98. // Print the entire AST generically.
  99. function printGenerically(path) {
  100. return (0, comments_1.printComments)(path, function (path) {
  101. return genericPrint(path, config, {
  102. includeComments: true,
  103. avoidRootParens: false,
  104. }, printGenerically);
  105. });
  106. }
  107. var path = fast_path_1.default.from(ast);
  108. var oldReuseWhitespace = config.reuseWhitespace;
  109. // Do not reuse whitespace (or anything else, for that matter)
  110. // when printing generically.
  111. config.reuseWhitespace = false;
  112. // TODO Allow printing of comments?
  113. var pr = new PrintResult(printGenerically(path).toString(config));
  114. config.reuseWhitespace = oldReuseWhitespace;
  115. return pr;
  116. };
  117. };
  118. exports.Printer = Printer;
  119. function genericPrint(path, config, options, printPath) {
  120. assert_1.default.ok(path instanceof fast_path_1.default);
  121. var node = path.getValue();
  122. var parts = [];
  123. var linesWithoutParens = genericPrintNoParens(path, config, printPath);
  124. if (!node || linesWithoutParens.isEmpty()) {
  125. return linesWithoutParens;
  126. }
  127. var shouldAddParens = false;
  128. var decoratorsLines = printDecorators(path, printPath);
  129. if (decoratorsLines.isEmpty()) {
  130. // Nodes with decorators can't have parentheses, so we can avoid
  131. // computing path.needsParens() except in this case.
  132. if (!options.avoidRootParens) {
  133. shouldAddParens = path.needsParens();
  134. }
  135. }
  136. else {
  137. parts.push(decoratorsLines);
  138. }
  139. if (shouldAddParens) {
  140. parts.unshift("(");
  141. }
  142. parts.push(linesWithoutParens);
  143. if (shouldAddParens) {
  144. parts.push(")");
  145. }
  146. return (0, lines_1.concat)(parts);
  147. }
  148. // Note that the `options` parameter of this function is what other
  149. // functions in this file call the `config` object (that is, the
  150. // configuration object originally passed into the Printer constructor).
  151. // Its properties are documented in lib/options.js.
  152. function genericPrintNoParens(path, options, print) {
  153. var n = path.getValue();
  154. if (!n) {
  155. return (0, lines_1.fromString)("");
  156. }
  157. if (typeof n === "string") {
  158. return (0, lines_1.fromString)(n, options);
  159. }
  160. namedTypes.Printable.assert(n);
  161. var parts = [];
  162. switch (n.type) {
  163. case "File":
  164. return path.call(print, "program");
  165. case "Program":
  166. // Babel 6
  167. if (n.directives) {
  168. path.each(function (childPath) {
  169. parts.push(print(childPath), ";\n");
  170. }, "directives");
  171. }
  172. if (n.interpreter) {
  173. parts.push(path.call(print, "interpreter"));
  174. }
  175. parts.push(path.call(function (bodyPath) { return printStatementSequence(bodyPath, options, print); }, "body"));
  176. return (0, lines_1.concat)(parts);
  177. case "Noop": // Babel extension.
  178. case "EmptyStatement":
  179. return (0, lines_1.fromString)("");
  180. case "ExpressionStatement":
  181. return (0, lines_1.concat)([path.call(print, "expression"), ";"]);
  182. case "ParenthesizedExpression": // Babel extension.
  183. return (0, lines_1.concat)(["(", path.call(print, "expression"), ")"]);
  184. case "BinaryExpression":
  185. case "LogicalExpression":
  186. case "AssignmentExpression":
  187. return (0, lines_1.fromString)(" ").join([
  188. path.call(print, "left"),
  189. n.operator,
  190. path.call(print, "right"),
  191. ]);
  192. case "AssignmentPattern":
  193. return (0, lines_1.concat)([
  194. path.call(print, "left"),
  195. " = ",
  196. path.call(print, "right"),
  197. ]);
  198. case "MemberExpression":
  199. case "OptionalMemberExpression": {
  200. parts.push(path.call(print, "object"));
  201. var property = path.call(print, "property");
  202. // Like n.optional, except with defaults applied, so optional
  203. // defaults to true for OptionalMemberExpression nodes.
  204. var optional = types.getFieldValue(n, "optional");
  205. if (n.computed) {
  206. parts.push(optional ? "?.[" : "[", property, "]");
  207. }
  208. else {
  209. parts.push(optional ? "?." : ".", property);
  210. }
  211. return (0, lines_1.concat)(parts);
  212. }
  213. case "ChainExpression":
  214. return path.call(print, "expression");
  215. case "MetaProperty":
  216. return (0, lines_1.concat)([
  217. path.call(print, "meta"),
  218. ".",
  219. path.call(print, "property"),
  220. ]);
  221. case "BindExpression":
  222. if (n.object) {
  223. parts.push(path.call(print, "object"));
  224. }
  225. parts.push("::", path.call(print, "callee"));
  226. return (0, lines_1.concat)(parts);
  227. case "Path":
  228. return (0, lines_1.fromString)(".").join(n.body);
  229. case "Identifier":
  230. return (0, lines_1.concat)([
  231. (0, lines_1.fromString)(n.name, options),
  232. n.optional ? "?" : "",
  233. path.call(print, "typeAnnotation"),
  234. ]);
  235. case "SpreadElement":
  236. case "SpreadElementPattern":
  237. case "RestProperty": // Babel 6 for ObjectPattern
  238. case "SpreadProperty":
  239. case "SpreadPropertyPattern":
  240. case "ObjectTypeSpreadProperty":
  241. case "RestElement":
  242. return (0, lines_1.concat)([
  243. "...",
  244. path.call(print, "argument"),
  245. path.call(print, "typeAnnotation"),
  246. ]);
  247. case "FunctionDeclaration":
  248. case "FunctionExpression":
  249. case "TSDeclareFunction":
  250. if (n.declare) {
  251. parts.push("declare ");
  252. }
  253. if (n.async) {
  254. parts.push("async ");
  255. }
  256. parts.push("function");
  257. if (n.generator)
  258. parts.push("*");
  259. if (n.id) {
  260. parts.push(" ", path.call(print, "id"), path.call(print, "typeParameters"));
  261. }
  262. else {
  263. if (n.typeParameters) {
  264. parts.push(path.call(print, "typeParameters"));
  265. }
  266. }
  267. parts.push("(", printFunctionParams(path, options, print), ")", path.call(print, "returnType"));
  268. if (n.body) {
  269. parts.push(" ", path.call(print, "body"));
  270. }
  271. return (0, lines_1.concat)(parts);
  272. case "ArrowFunctionExpression":
  273. if (n.async) {
  274. parts.push("async ");
  275. }
  276. if (n.typeParameters) {
  277. parts.push(path.call(print, "typeParameters"));
  278. }
  279. if (!options.arrowParensAlways &&
  280. n.params.length === 1 &&
  281. !n.rest &&
  282. n.params[0].type === "Identifier" &&
  283. !n.params[0].typeAnnotation &&
  284. !n.returnType) {
  285. parts.push(path.call(print, "params", 0));
  286. }
  287. else {
  288. parts.push("(", printFunctionParams(path, options, print), ")", path.call(print, "returnType"));
  289. }
  290. parts.push(" => ", path.call(print, "body"));
  291. return (0, lines_1.concat)(parts);
  292. case "MethodDefinition":
  293. return printMethod(path, options, print);
  294. case "YieldExpression":
  295. parts.push("yield");
  296. if (n.delegate)
  297. parts.push("*");
  298. if (n.argument)
  299. parts.push(" ", path.call(print, "argument"));
  300. return (0, lines_1.concat)(parts);
  301. case "AwaitExpression":
  302. parts.push("await");
  303. if (n.all)
  304. parts.push("*");
  305. if (n.argument)
  306. parts.push(" ", path.call(print, "argument"));
  307. return (0, lines_1.concat)(parts);
  308. case "ModuleExpression":
  309. return (0, lines_1.concat)([
  310. "module {\n",
  311. path.call(print, "body").indent(options.tabWidth),
  312. "\n}",
  313. ]);
  314. case "ModuleDeclaration":
  315. parts.push("module", path.call(print, "id"));
  316. if (n.source) {
  317. assert_1.default.ok(!n.body);
  318. parts.push("from", path.call(print, "source"));
  319. }
  320. else {
  321. parts.push(path.call(print, "body"));
  322. }
  323. return (0, lines_1.fromString)(" ").join(parts);
  324. case "ImportSpecifier":
  325. if (n.importKind && n.importKind !== "value") {
  326. parts.push(n.importKind + " ");
  327. }
  328. if (n.imported) {
  329. parts.push(path.call(print, "imported"));
  330. if (n.local && n.local.name !== n.imported.name) {
  331. parts.push(" as ", path.call(print, "local"));
  332. }
  333. }
  334. else if (n.id) {
  335. parts.push(path.call(print, "id"));
  336. if (n.name) {
  337. parts.push(" as ", path.call(print, "name"));
  338. }
  339. }
  340. return (0, lines_1.concat)(parts);
  341. case "ExportSpecifier":
  342. if (n.exportKind && n.exportKind !== "value") {
  343. parts.push(n.exportKind + " ");
  344. }
  345. if (n.local) {
  346. parts.push(path.call(print, "local"));
  347. if (n.exported && n.exported.name !== n.local.name) {
  348. parts.push(" as ", path.call(print, "exported"));
  349. }
  350. }
  351. else if (n.id) {
  352. parts.push(path.call(print, "id"));
  353. if (n.name) {
  354. parts.push(" as ", path.call(print, "name"));
  355. }
  356. }
  357. return (0, lines_1.concat)(parts);
  358. case "ExportBatchSpecifier":
  359. return (0, lines_1.fromString)("*");
  360. case "ImportNamespaceSpecifier":
  361. parts.push("* as ");
  362. if (n.local) {
  363. parts.push(path.call(print, "local"));
  364. }
  365. else if (n.id) {
  366. parts.push(path.call(print, "id"));
  367. }
  368. return (0, lines_1.concat)(parts);
  369. case "ImportDefaultSpecifier":
  370. if (n.local) {
  371. return path.call(print, "local");
  372. }
  373. return path.call(print, "id");
  374. case "TSExportAssignment":
  375. return (0, lines_1.concat)(["export = ", path.call(print, "expression")]);
  376. case "ExportDeclaration":
  377. case "ExportDefaultDeclaration":
  378. case "ExportNamedDeclaration":
  379. return printExportDeclaration(path, options, print);
  380. case "ExportAllDeclaration":
  381. parts.push("export *");
  382. if (n.exported) {
  383. parts.push(" as ", path.call(print, "exported"));
  384. }
  385. parts.push(" from ", path.call(print, "source"), ";");
  386. return (0, lines_1.concat)(parts);
  387. case "TSNamespaceExportDeclaration":
  388. parts.push("export as namespace ", path.call(print, "id"));
  389. return maybeAddSemicolon((0, lines_1.concat)(parts));
  390. case "ExportNamespaceSpecifier":
  391. return (0, lines_1.concat)(["* as ", path.call(print, "exported")]);
  392. case "ExportDefaultSpecifier":
  393. return path.call(print, "exported");
  394. case "Import":
  395. return (0, lines_1.fromString)("import", options);
  396. // Recast and ast-types currently support dynamic import(...) using
  397. // either this dedicated ImportExpression type or a CallExpression
  398. // whose callee has type Import.
  399. // https://github.com/benjamn/ast-types/pull/365#issuecomment-605214486
  400. case "ImportExpression":
  401. return (0, lines_1.concat)(["import(", path.call(print, "source"), ")"]);
  402. case "ImportDeclaration": {
  403. parts.push("import ");
  404. if (n.importKind && n.importKind !== "value") {
  405. parts.push(n.importKind + " ");
  406. }
  407. if (n.specifiers && n.specifiers.length > 0) {
  408. var unbracedSpecifiers_1 = [];
  409. var bracedSpecifiers_1 = [];
  410. path.each(function (specifierPath) {
  411. var spec = specifierPath.getValue();
  412. if (spec.type === "ImportSpecifier") {
  413. bracedSpecifiers_1.push(print(specifierPath));
  414. }
  415. else if (spec.type === "ImportDefaultSpecifier" ||
  416. spec.type === "ImportNamespaceSpecifier") {
  417. unbracedSpecifiers_1.push(print(specifierPath));
  418. }
  419. }, "specifiers");
  420. unbracedSpecifiers_1.forEach(function (lines, i) {
  421. if (i > 0) {
  422. parts.push(", ");
  423. }
  424. parts.push(lines);
  425. });
  426. if (bracedSpecifiers_1.length > 0) {
  427. var lines = (0, lines_1.fromString)(", ").join(bracedSpecifiers_1);
  428. if (lines.getLineLength(1) > options.wrapColumn) {
  429. lines = (0, lines_1.concat)([
  430. (0, lines_1.fromString)(",\n").join(bracedSpecifiers_1).indent(options.tabWidth),
  431. ",",
  432. ]);
  433. }
  434. if (unbracedSpecifiers_1.length > 0) {
  435. parts.push(", ");
  436. }
  437. if (lines.length > 1) {
  438. parts.push("{\n", lines, "\n}");
  439. }
  440. else if (options.objectCurlySpacing) {
  441. parts.push("{ ", lines, " }");
  442. }
  443. else {
  444. parts.push("{", lines, "}");
  445. }
  446. }
  447. parts.push(" from ");
  448. }
  449. parts.push(path.call(print, "source"), maybePrintImportAssertions(path, options, print), ";");
  450. return (0, lines_1.concat)(parts);
  451. }
  452. case "ImportAttribute":
  453. return (0, lines_1.concat)([path.call(print, "key"), ": ", path.call(print, "value")]);
  454. case "StaticBlock":
  455. parts.push("static ");
  456. // Intentionally fall through to BlockStatement below.
  457. case "BlockStatement": {
  458. var naked_1 = path.call(function (bodyPath) { return printStatementSequence(bodyPath, options, print); }, "body");
  459. if (naked_1.isEmpty()) {
  460. if (!n.directives || n.directives.length === 0) {
  461. parts.push("{}");
  462. return (0, lines_1.concat)(parts);
  463. }
  464. }
  465. parts.push("{\n");
  466. // Babel 6
  467. if (n.directives) {
  468. path.each(function (childPath) {
  469. parts.push(maybeAddSemicolon(print(childPath).indent(options.tabWidth)), n.directives.length > 1 || !naked_1.isEmpty() ? "\n" : "");
  470. }, "directives");
  471. }
  472. parts.push(naked_1.indent(options.tabWidth));
  473. parts.push("\n}");
  474. return (0, lines_1.concat)(parts);
  475. }
  476. case "ReturnStatement": {
  477. parts.push("return");
  478. if (n.argument) {
  479. var argLines = path.call(print, "argument");
  480. if (argLines.startsWithComment() ||
  481. (argLines.length > 1 &&
  482. namedTypes.JSXElement &&
  483. namedTypes.JSXElement.check(n.argument))) {
  484. parts.push(" (\n", argLines.indent(options.tabWidth), "\n)");
  485. }
  486. else {
  487. parts.push(" ", argLines);
  488. }
  489. }
  490. parts.push(";");
  491. return (0, lines_1.concat)(parts);
  492. }
  493. case "CallExpression":
  494. case "OptionalCallExpression":
  495. parts.push(path.call(print, "callee"));
  496. if (n.typeParameters) {
  497. parts.push(path.call(print, "typeParameters"));
  498. }
  499. if (n.typeArguments) {
  500. parts.push(path.call(print, "typeArguments"));
  501. }
  502. // Like n.optional, but defaults to true for OptionalCallExpression
  503. // nodes that are missing an n.optional property (unusual),
  504. // according to the OptionalCallExpression definition in ast-types.
  505. if (types.getFieldValue(n, "optional")) {
  506. parts.push("?.");
  507. }
  508. parts.push(printArgumentsList(path, options, print));
  509. return (0, lines_1.concat)(parts);
  510. case "RecordExpression":
  511. parts.push("#");
  512. // Intentionally fall through to printing the object literal...
  513. case "ObjectExpression":
  514. case "ObjectPattern":
  515. case "ObjectTypeAnnotation": {
  516. var isTypeAnnotation_1 = n.type === "ObjectTypeAnnotation";
  517. var separator_1 = options.flowObjectCommas
  518. ? ","
  519. : isTypeAnnotation_1
  520. ? ";"
  521. : ",";
  522. var fields = [];
  523. var allowBreak_1 = false;
  524. if (isTypeAnnotation_1) {
  525. fields.push("indexers", "callProperties");
  526. if (n.internalSlots != null) {
  527. fields.push("internalSlots");
  528. }
  529. }
  530. fields.push("properties");
  531. var len_1 = 0;
  532. fields.forEach(function (field) {
  533. len_1 += n[field].length;
  534. });
  535. var oneLine_1 = (isTypeAnnotation_1 && len_1 === 1) || len_1 === 0;
  536. var leftBrace = n.exact ? "{|" : "{";
  537. var rightBrace = n.exact ? "|}" : "}";
  538. parts.push(oneLine_1 ? leftBrace : leftBrace + "\n");
  539. var leftBraceIndex = parts.length - 1;
  540. var i_1 = 0;
  541. fields.forEach(function (field) {
  542. path.each(function (childPath) {
  543. var lines = print(childPath);
  544. if (!oneLine_1) {
  545. lines = lines.indent(options.tabWidth);
  546. }
  547. var multiLine = !isTypeAnnotation_1 && lines.length > 1;
  548. if (multiLine && allowBreak_1) {
  549. // Similar to the logic for BlockStatement.
  550. parts.push("\n");
  551. }
  552. parts.push(lines);
  553. if (i_1 < len_1 - 1) {
  554. // Add an extra line break if the previous object property
  555. // had a multi-line value.
  556. parts.push(separator_1 + (multiLine ? "\n\n" : "\n"));
  557. allowBreak_1 = !multiLine;
  558. }
  559. else if (len_1 !== 1 && isTypeAnnotation_1) {
  560. parts.push(separator_1);
  561. }
  562. else if (!oneLine_1 &&
  563. util.isTrailingCommaEnabled(options, "objects") &&
  564. childPath.getValue().type !== "RestElement") {
  565. parts.push(separator_1);
  566. }
  567. i_1++;
  568. }, field);
  569. });
  570. if (n.inexact) {
  571. var line = (0, lines_1.fromString)("...", options);
  572. if (oneLine_1) {
  573. if (len_1 > 0) {
  574. parts.push(separator_1, " ");
  575. }
  576. parts.push(line);
  577. }
  578. else {
  579. // No trailing separator after ... to maintain parity with prettier.
  580. parts.push("\n", line.indent(options.tabWidth));
  581. }
  582. }
  583. parts.push(oneLine_1 ? rightBrace : "\n" + rightBrace);
  584. if (i_1 !== 0 && oneLine_1 && options.objectCurlySpacing) {
  585. parts[leftBraceIndex] = leftBrace + " ";
  586. parts[parts.length - 1] = " " + rightBrace;
  587. }
  588. if (n.typeAnnotation) {
  589. parts.push(path.call(print, "typeAnnotation"));
  590. }
  591. return (0, lines_1.concat)(parts);
  592. }
  593. case "PropertyPattern":
  594. return (0, lines_1.concat)([
  595. path.call(print, "key"),
  596. ": ",
  597. path.call(print, "pattern"),
  598. ]);
  599. case "ObjectProperty": // Babel 6
  600. case "Property": {
  601. // Non-standard AST node type.
  602. if (n.method || n.kind === "get" || n.kind === "set") {
  603. return printMethod(path, options, print);
  604. }
  605. if (n.shorthand && n.value.type === "AssignmentPattern") {
  606. return path.call(print, "value");
  607. }
  608. var key = path.call(print, "key");
  609. if (n.computed) {
  610. parts.push("[", key, "]");
  611. }
  612. else {
  613. parts.push(key);
  614. }
  615. if (!n.shorthand || n.key.name !== n.value.name) {
  616. parts.push(": ", path.call(print, "value"));
  617. }
  618. return (0, lines_1.concat)(parts);
  619. }
  620. case "ClassMethod": // Babel 6
  621. case "ObjectMethod": // Babel 6
  622. case "ClassPrivateMethod":
  623. case "TSDeclareMethod":
  624. return printMethod(path, options, print);
  625. case "PrivateName":
  626. return (0, lines_1.concat)(["#", path.call(print, "id")]);
  627. case "Decorator":
  628. return (0, lines_1.concat)(["@", path.call(print, "expression")]);
  629. case "TupleExpression":
  630. parts.push("#");
  631. // Intentionally fall through to printing the tuple elements...
  632. case "ArrayExpression":
  633. case "ArrayPattern": {
  634. var elems = n.elements;
  635. var len_2 = elems.length;
  636. var printed_1 = path.map(print, "elements");
  637. var joined = (0, lines_1.fromString)(", ").join(printed_1);
  638. var oneLine_2 = joined.getLineLength(1) <= options.wrapColumn;
  639. if (oneLine_2) {
  640. if (options.arrayBracketSpacing) {
  641. parts.push("[ ");
  642. }
  643. else {
  644. parts.push("[");
  645. }
  646. }
  647. else {
  648. parts.push("[\n");
  649. }
  650. path.each(function (elemPath) {
  651. var i = elemPath.getName();
  652. var elem = elemPath.getValue();
  653. if (!elem) {
  654. // If the array expression ends with a hole, that hole
  655. // will be ignored by the interpreter, but if it ends with
  656. // two (or more) holes, we need to write out two (or more)
  657. // commas so that the resulting code is interpreted with
  658. // both (all) of the holes.
  659. parts.push(",");
  660. }
  661. else {
  662. var lines = printed_1[i];
  663. if (oneLine_2) {
  664. if (i > 0)
  665. parts.push(" ");
  666. }
  667. else {
  668. lines = lines.indent(options.tabWidth);
  669. }
  670. parts.push(lines);
  671. if (i < len_2 - 1 ||
  672. (!oneLine_2 && util.isTrailingCommaEnabled(options, "arrays")))
  673. parts.push(",");
  674. if (!oneLine_2)
  675. parts.push("\n");
  676. }
  677. }, "elements");
  678. if (oneLine_2 && options.arrayBracketSpacing) {
  679. parts.push(" ]");
  680. }
  681. else {
  682. parts.push("]");
  683. }
  684. if (n.typeAnnotation) {
  685. parts.push(path.call(print, "typeAnnotation"));
  686. }
  687. return (0, lines_1.concat)(parts);
  688. }
  689. case "SequenceExpression":
  690. return (0, lines_1.fromString)(", ").join(path.map(print, "expressions"));
  691. case "ThisExpression":
  692. return (0, lines_1.fromString)("this");
  693. case "Super":
  694. return (0, lines_1.fromString)("super");
  695. case "NullLiteral": // Babel 6 Literal split
  696. return (0, lines_1.fromString)("null");
  697. case "RegExpLiteral": // Babel 6 Literal split
  698. return (0, lines_1.fromString)(getPossibleRaw(n) || "/".concat(n.pattern, "/").concat(n.flags || ""), options);
  699. case "BigIntLiteral": // Babel 7 Literal split
  700. return (0, lines_1.fromString)(getPossibleRaw(n) || n.value + "n", options);
  701. case "NumericLiteral": // Babel 6 Literal Split
  702. return (0, lines_1.fromString)(getPossibleRaw(n) || n.value, options);
  703. case "DecimalLiteral":
  704. return (0, lines_1.fromString)(getPossibleRaw(n) || n.value + "m", options);
  705. case "StringLiteral":
  706. return (0, lines_1.fromString)(nodeStr(n.value, options));
  707. case "BooleanLiteral": // Babel 6 Literal split
  708. case "Literal":
  709. return (0, lines_1.fromString)(getPossibleRaw(n) ||
  710. (typeof n.value === "string" ? nodeStr(n.value, options) : n.value), options);
  711. case "Directive": // Babel 6
  712. return path.call(print, "value");
  713. case "DirectiveLiteral": // Babel 6
  714. return (0, lines_1.fromString)(getPossibleRaw(n) || nodeStr(n.value, options), options);
  715. case "InterpreterDirective":
  716. return (0, lines_1.fromString)("#!".concat(n.value, "\n"), options);
  717. case "ModuleSpecifier":
  718. if (n.local) {
  719. throw new Error("The ESTree ModuleSpecifier type should be abstract");
  720. }
  721. // The Esprima ModuleSpecifier type is just a string-valued
  722. // Literal identifying the imported-from module.
  723. return (0, lines_1.fromString)(nodeStr(n.value, options), options);
  724. case "UnaryExpression":
  725. parts.push(n.operator);
  726. if (/[a-z]$/.test(n.operator))
  727. parts.push(" ");
  728. parts.push(path.call(print, "argument"));
  729. return (0, lines_1.concat)(parts);
  730. case "UpdateExpression":
  731. parts.push(path.call(print, "argument"), n.operator);
  732. if (n.prefix)
  733. parts.reverse();
  734. return (0, lines_1.concat)(parts);
  735. case "ConditionalExpression":
  736. return (0, lines_1.concat)([
  737. path.call(print, "test"),
  738. " ? ",
  739. path.call(print, "consequent"),
  740. " : ",
  741. path.call(print, "alternate"),
  742. ]);
  743. case "NewExpression": {
  744. parts.push("new ", path.call(print, "callee"));
  745. if (n.typeParameters) {
  746. parts.push(path.call(print, "typeParameters"));
  747. }
  748. if (n.typeArguments) {
  749. parts.push(path.call(print, "typeArguments"));
  750. }
  751. var args = n.arguments;
  752. if (args) {
  753. parts.push(printArgumentsList(path, options, print));
  754. }
  755. return (0, lines_1.concat)(parts);
  756. }
  757. case "VariableDeclaration": {
  758. if (n.declare) {
  759. parts.push("declare ");
  760. }
  761. parts.push(n.kind, " ");
  762. var maxLen_1 = 0;
  763. var printed = path.map(function (childPath) {
  764. var lines = print(childPath);
  765. maxLen_1 = Math.max(lines.length, maxLen_1);
  766. return lines;
  767. }, "declarations");
  768. if (maxLen_1 === 1) {
  769. parts.push((0, lines_1.fromString)(", ").join(printed));
  770. }
  771. else if (printed.length > 1) {
  772. parts.push((0, lines_1.fromString)(",\n")
  773. .join(printed)
  774. .indentTail(n.kind.length + 1));
  775. }
  776. else {
  777. parts.push(printed[0]);
  778. }
  779. // We generally want to terminate all variable declarations with a
  780. // semicolon, except when they are children of for loops.
  781. var parentNode = path.getParentNode();
  782. if (!namedTypes.ForStatement.check(parentNode) &&
  783. !namedTypes.ForInStatement.check(parentNode) &&
  784. !(namedTypes.ForOfStatement &&
  785. namedTypes.ForOfStatement.check(parentNode)) &&
  786. !(namedTypes.ForAwaitStatement &&
  787. namedTypes.ForAwaitStatement.check(parentNode))) {
  788. parts.push(";");
  789. }
  790. return (0, lines_1.concat)(parts);
  791. }
  792. case "VariableDeclarator":
  793. return n.init
  794. ? (0, lines_1.fromString)(" = ").join([
  795. path.call(print, "id"),
  796. path.call(print, "init"),
  797. ])
  798. : path.call(print, "id");
  799. case "WithStatement":
  800. return (0, lines_1.concat)([
  801. "with (",
  802. path.call(print, "object"),
  803. ") ",
  804. path.call(print, "body"),
  805. ]);
  806. case "IfStatement": {
  807. var con = adjustClause(path.call(print, "consequent"), options);
  808. parts.push("if (", path.call(print, "test"), ")", con);
  809. if (n.alternate)
  810. parts.push(endsWithBrace(con) ? " else" : "\nelse", adjustClause(path.call(print, "alternate"), options));
  811. return (0, lines_1.concat)(parts);
  812. }
  813. case "ForStatement": {
  814. // TODO Get the for (;;) case right.
  815. var init = path.call(print, "init");
  816. var sep = init.length > 1 ? ";\n" : "; ";
  817. var forParen = "for (";
  818. var indented = (0, lines_1.fromString)(sep)
  819. .join([init, path.call(print, "test"), path.call(print, "update")])
  820. .indentTail(forParen.length);
  821. var head = (0, lines_1.concat)([forParen, indented, ")"]);
  822. var clause = adjustClause(path.call(print, "body"), options);
  823. parts.push(head);
  824. if (head.length > 1) {
  825. parts.push("\n");
  826. clause = clause.trimLeft();
  827. }
  828. parts.push(clause);
  829. return (0, lines_1.concat)(parts);
  830. }
  831. case "WhileStatement":
  832. return (0, lines_1.concat)([
  833. "while (",
  834. path.call(print, "test"),
  835. ")",
  836. adjustClause(path.call(print, "body"), options),
  837. ]);
  838. case "ForInStatement":
  839. // Note: esprima can't actually parse "for each (".
  840. return (0, lines_1.concat)([
  841. n.each ? "for each (" : "for (",
  842. path.call(print, "left"),
  843. " in ",
  844. path.call(print, "right"),
  845. ")",
  846. adjustClause(path.call(print, "body"), options),
  847. ]);
  848. case "ForOfStatement":
  849. case "ForAwaitStatement":
  850. parts.push("for ");
  851. if (n.await || n.type === "ForAwaitStatement") {
  852. parts.push("await ");
  853. }
  854. parts.push("(", path.call(print, "left"), " of ", path.call(print, "right"), ")", adjustClause(path.call(print, "body"), options));
  855. return (0, lines_1.concat)(parts);
  856. case "DoWhileStatement": {
  857. var doBody = (0, lines_1.concat)([
  858. "do",
  859. adjustClause(path.call(print, "body"), options),
  860. ]);
  861. parts.push(doBody);
  862. if (endsWithBrace(doBody))
  863. parts.push(" while");
  864. else
  865. parts.push("\nwhile");
  866. parts.push(" (", path.call(print, "test"), ");");
  867. return (0, lines_1.concat)(parts);
  868. }
  869. case "DoExpression": {
  870. var statements = path.call(function (bodyPath) { return printStatementSequence(bodyPath, options, print); }, "body");
  871. return (0, lines_1.concat)(["do {\n", statements.indent(options.tabWidth), "\n}"]);
  872. }
  873. case "BreakStatement":
  874. parts.push("break");
  875. if (n.label)
  876. parts.push(" ", path.call(print, "label"));
  877. parts.push(";");
  878. return (0, lines_1.concat)(parts);
  879. case "ContinueStatement":
  880. parts.push("continue");
  881. if (n.label)
  882. parts.push(" ", path.call(print, "label"));
  883. parts.push(";");
  884. return (0, lines_1.concat)(parts);
  885. case "LabeledStatement":
  886. return (0, lines_1.concat)([
  887. path.call(print, "label"),
  888. ":\n",
  889. path.call(print, "body"),
  890. ]);
  891. case "TryStatement":
  892. parts.push("try ", path.call(print, "block"));
  893. if (n.handler) {
  894. parts.push(" ", path.call(print, "handler"));
  895. }
  896. else if (n.handlers) {
  897. path.each(function (handlerPath) {
  898. parts.push(" ", print(handlerPath));
  899. }, "handlers");
  900. }
  901. if (n.finalizer) {
  902. parts.push(" finally ", path.call(print, "finalizer"));
  903. }
  904. return (0, lines_1.concat)(parts);
  905. case "CatchClause":
  906. parts.push("catch ");
  907. if (n.param) {
  908. parts.push("(", path.call(print, "param"));
  909. }
  910. if (n.guard) {
  911. // Note: esprima does not recognize conditional catch clauses.
  912. parts.push(" if ", path.call(print, "guard"));
  913. }
  914. if (n.param) {
  915. parts.push(") ");
  916. }
  917. parts.push(path.call(print, "body"));
  918. return (0, lines_1.concat)(parts);
  919. case "ThrowStatement":
  920. return (0, lines_1.concat)(["throw ", path.call(print, "argument"), ";"]);
  921. case "SwitchStatement":
  922. return (0, lines_1.concat)([
  923. "switch (",
  924. path.call(print, "discriminant"),
  925. ") {\n",
  926. (0, lines_1.fromString)("\n").join(path.map(print, "cases")),
  927. "\n}",
  928. ]);
  929. // Note: ignoring n.lexical because it has no printing consequences.
  930. case "SwitchCase":
  931. if (n.test)
  932. parts.push("case ", path.call(print, "test"), ":");
  933. else
  934. parts.push("default:");
  935. if (n.consequent.length > 0) {
  936. parts.push("\n", path
  937. .call(function (consequentPath) {
  938. return printStatementSequence(consequentPath, options, print);
  939. }, "consequent")
  940. .indent(options.tabWidth));
  941. }
  942. return (0, lines_1.concat)(parts);
  943. case "DebuggerStatement":
  944. return (0, lines_1.fromString)("debugger;");
  945. // JSX extensions below.
  946. case "JSXAttribute":
  947. parts.push(path.call(print, "name"));
  948. if (n.value)
  949. parts.push("=", path.call(print, "value"));
  950. return (0, lines_1.concat)(parts);
  951. case "JSXIdentifier":
  952. return (0, lines_1.fromString)(n.name, options);
  953. case "JSXNamespacedName":
  954. return (0, lines_1.fromString)(":").join([
  955. path.call(print, "namespace"),
  956. path.call(print, "name"),
  957. ]);
  958. case "JSXMemberExpression":
  959. return (0, lines_1.fromString)(".").join([
  960. path.call(print, "object"),
  961. path.call(print, "property"),
  962. ]);
  963. case "JSXSpreadAttribute":
  964. return (0, lines_1.concat)(["{...", path.call(print, "argument"), "}"]);
  965. case "JSXSpreadChild":
  966. return (0, lines_1.concat)(["{...", path.call(print, "expression"), "}"]);
  967. case "JSXExpressionContainer":
  968. return (0, lines_1.concat)(["{", path.call(print, "expression"), "}"]);
  969. case "JSXElement":
  970. case "JSXFragment": {
  971. var openingPropName = "opening" + (n.type === "JSXElement" ? "Element" : "Fragment");
  972. var closingPropName = "closing" + (n.type === "JSXElement" ? "Element" : "Fragment");
  973. var openingLines = path.call(print, openingPropName);
  974. if (n[openingPropName].selfClosing) {
  975. assert_1.default.ok(!n[closingPropName], "unexpected " +
  976. closingPropName +
  977. " element in self-closing " +
  978. n.type);
  979. return openingLines;
  980. }
  981. var childLines = (0, lines_1.concat)(path.map(function (childPath) {
  982. var child = childPath.getValue();
  983. if (namedTypes.Literal.check(child) &&
  984. typeof child.value === "string") {
  985. if (/\S/.test(child.value)) {
  986. return child.value.replace(/^\s+|\s+$/g, "");
  987. }
  988. else if (/\n/.test(child.value)) {
  989. return "\n";
  990. }
  991. }
  992. return print(childPath);
  993. }, "children")).indentTail(options.tabWidth);
  994. var closingLines = path.call(print, closingPropName);
  995. return (0, lines_1.concat)([openingLines, childLines, closingLines]);
  996. }
  997. case "JSXOpeningElement": {
  998. parts.push("<", path.call(print, "name"));
  999. var attrParts_1 = [];
  1000. path.each(function (attrPath) {
  1001. attrParts_1.push(" ", print(attrPath));
  1002. }, "attributes");
  1003. var attrLines = (0, lines_1.concat)(attrParts_1);
  1004. var needLineWrap = attrLines.length > 1 || attrLines.getLineLength(1) > options.wrapColumn;
  1005. if (needLineWrap) {
  1006. attrParts_1.forEach(function (part, i) {
  1007. if (part === " ") {
  1008. assert_1.default.strictEqual(i % 2, 0);
  1009. attrParts_1[i] = "\n";
  1010. }
  1011. });
  1012. attrLines = (0, lines_1.concat)(attrParts_1).indentTail(options.tabWidth);
  1013. }
  1014. parts.push(attrLines, n.selfClosing ? " />" : ">");
  1015. return (0, lines_1.concat)(parts);
  1016. }
  1017. case "JSXClosingElement":
  1018. return (0, lines_1.concat)(["</", path.call(print, "name"), ">"]);
  1019. case "JSXOpeningFragment":
  1020. return (0, lines_1.fromString)("<>");
  1021. case "JSXClosingFragment":
  1022. return (0, lines_1.fromString)("</>");
  1023. case "JSXText":
  1024. return (0, lines_1.fromString)(n.value, options);
  1025. case "JSXEmptyExpression":
  1026. return (0, lines_1.fromString)("");
  1027. case "TypeAnnotatedIdentifier":
  1028. return (0, lines_1.concat)([
  1029. path.call(print, "annotation"),
  1030. " ",
  1031. path.call(print, "identifier"),
  1032. ]);
  1033. case "ClassBody":
  1034. if (n.body.length === 0) {
  1035. return (0, lines_1.fromString)("{}");
  1036. }
  1037. return (0, lines_1.concat)([
  1038. "{\n",
  1039. path
  1040. .call(function (bodyPath) { return printStatementSequence(bodyPath, options, print); }, "body")
  1041. .indent(options.tabWidth),
  1042. "\n}",
  1043. ]);
  1044. case "ClassPropertyDefinition":
  1045. parts.push("static ", path.call(print, "definition"));
  1046. if (!namedTypes.MethodDefinition.check(n.definition))
  1047. parts.push(";");
  1048. return (0, lines_1.concat)(parts);
  1049. case "ClassProperty": {
  1050. if (n.declare) {
  1051. parts.push("declare ");
  1052. }
  1053. var access = n.accessibility || n.access;
  1054. if (typeof access === "string") {
  1055. parts.push(access, " ");
  1056. }
  1057. if (n.static) {
  1058. parts.push("static ");
  1059. }
  1060. if (n.abstract) {
  1061. parts.push("abstract ");
  1062. }
  1063. if (n.readonly) {
  1064. parts.push("readonly ");
  1065. }
  1066. var key = path.call(print, "key");
  1067. if (n.computed) {
  1068. key = (0, lines_1.concat)(["[", key, "]"]);
  1069. }
  1070. if (n.variance) {
  1071. key = (0, lines_1.concat)([printVariance(path, print), key]);
  1072. }
  1073. parts.push(key);
  1074. if (n.optional) {
  1075. parts.push("?");
  1076. }
  1077. if (n.definite) {
  1078. parts.push("!");
  1079. }
  1080. if (n.typeAnnotation) {
  1081. parts.push(path.call(print, "typeAnnotation"));
  1082. }
  1083. if (n.value) {
  1084. parts.push(" = ", path.call(print, "value"));
  1085. }
  1086. parts.push(";");
  1087. return (0, lines_1.concat)(parts);
  1088. }
  1089. case "ClassPrivateProperty":
  1090. if (n.static) {
  1091. parts.push("static ");
  1092. }
  1093. parts.push(path.call(print, "key"));
  1094. if (n.typeAnnotation) {
  1095. parts.push(path.call(print, "typeAnnotation"));
  1096. }
  1097. if (n.value) {
  1098. parts.push(" = ", path.call(print, "value"));
  1099. }
  1100. parts.push(";");
  1101. return (0, lines_1.concat)(parts);
  1102. case "ClassAccessorProperty": {
  1103. parts.push.apply(parts, tslib_1.__spreadArray(tslib_1.__spreadArray([], printClassMemberModifiers(n), false), ["accessor "], false));
  1104. if (n.computed) {
  1105. parts.push("[", path.call(print, "key"), "]");
  1106. }
  1107. else {
  1108. parts.push(path.call(print, "key"));
  1109. }
  1110. if (n.optional) {
  1111. parts.push("?");
  1112. }
  1113. if (n.definite) {
  1114. parts.push("!");
  1115. }
  1116. if (n.typeAnnotation) {
  1117. parts.push(path.call(print, "typeAnnotation"));
  1118. }
  1119. if (n.value) {
  1120. parts.push(" = ", path.call(print, "value"));
  1121. }
  1122. parts.push(";");
  1123. return (0, lines_1.concat)(parts);
  1124. }
  1125. case "ClassDeclaration":
  1126. case "ClassExpression":
  1127. case "DeclareClass":
  1128. if (n.declare) {
  1129. parts.push("declare ");
  1130. }
  1131. if (n.abstract) {
  1132. parts.push("abstract ");
  1133. }
  1134. parts.push("class");
  1135. if (n.id) {
  1136. parts.push(" ", path.call(print, "id"));
  1137. }
  1138. if (n.typeParameters) {
  1139. parts.push(path.call(print, "typeParameters"));
  1140. }
  1141. if (n.superClass) {
  1142. // ClassDeclaration and ClassExpression only
  1143. parts.push(" extends ", path.call(print, "superClass"), path.call(print, "superTypeParameters"));
  1144. }
  1145. if (n.extends && n.extends.length > 0) {
  1146. // DeclareClass only
  1147. parts.push(" extends ", (0, lines_1.fromString)(", ").join(path.map(print, "extends")));
  1148. }
  1149. if (n["implements"] && n["implements"].length > 0) {
  1150. parts.push(" implements ", (0, lines_1.fromString)(", ").join(path.map(print, "implements")));
  1151. }
  1152. parts.push(" ", path.call(print, "body"));
  1153. if (n.type === "DeclareClass") {
  1154. return printFlowDeclaration(path, parts);
  1155. }
  1156. else {
  1157. return (0, lines_1.concat)(parts);
  1158. }
  1159. case "TemplateElement":
  1160. return (0, lines_1.fromString)(n.value.raw, options).lockIndentTail();
  1161. case "TemplateLiteral": {
  1162. var expressions_1 = path.map(print, "expressions");
  1163. parts.push("`");
  1164. path.each(function (childPath) {
  1165. var i = childPath.getName();
  1166. parts.push(print(childPath));
  1167. if (i < expressions_1.length) {
  1168. parts.push("${", expressions_1[i], "}");
  1169. }
  1170. }, "quasis");
  1171. parts.push("`");
  1172. return (0, lines_1.concat)(parts).lockIndentTail();
  1173. }
  1174. case "TaggedTemplateExpression":
  1175. return (0, lines_1.concat)([path.call(print, "tag"), path.call(print, "quasi")]);
  1176. // These types are unprintable because they serve as abstract
  1177. // supertypes for other (printable) types.
  1178. case "Node":
  1179. case "Printable":
  1180. case "SourceLocation":
  1181. case "Position":
  1182. case "Statement":
  1183. case "Function":
  1184. case "Pattern":
  1185. case "Expression":
  1186. case "Declaration":
  1187. case "Specifier":
  1188. case "NamedSpecifier":
  1189. case "Comment": // Supertype of Block and Line
  1190. case "Flow": // Supertype of all Flow AST node types
  1191. case "FlowType": // Supertype of all Flow types
  1192. case "FlowPredicate": // Supertype of InferredPredicate and DeclaredPredicate
  1193. case "MemberTypeAnnotation": // Flow
  1194. case "Type": // Flow
  1195. case "TSHasOptionalTypeParameterInstantiation":
  1196. case "TSHasOptionalTypeParameters":
  1197. case "TSHasOptionalTypeAnnotation":
  1198. case "ChainElement": // Supertype of MemberExpression and CallExpression
  1199. throw new Error("unprintable type: " + JSON.stringify(n.type));
  1200. case "CommentBlock": // Babel block comment.
  1201. case "Block": // Esprima block comment.
  1202. return (0, lines_1.concat)(["/*", (0, lines_1.fromString)(n.value, options), "*/"]);
  1203. case "CommentLine": // Babel line comment.
  1204. case "Line": // Esprima line comment.
  1205. return (0, lines_1.concat)(["//", (0, lines_1.fromString)(n.value, options)]);
  1206. // Type Annotations for Facebook Flow, typically stripped out or
  1207. // transformed away before printing.
  1208. case "TypeAnnotation":
  1209. if (n.typeAnnotation) {
  1210. if (n.typeAnnotation.type !== "FunctionTypeAnnotation") {
  1211. parts.push(": ");
  1212. }
  1213. parts.push(path.call(print, "typeAnnotation"));
  1214. return (0, lines_1.concat)(parts);
  1215. }
  1216. return (0, lines_1.fromString)("");
  1217. case "ExistentialTypeParam":
  1218. case "ExistsTypeAnnotation":
  1219. return (0, lines_1.fromString)("*", options);
  1220. case "EmptyTypeAnnotation":
  1221. return (0, lines_1.fromString)("empty", options);
  1222. case "AnyTypeAnnotation":
  1223. return (0, lines_1.fromString)("any", options);
  1224. case "MixedTypeAnnotation":
  1225. return (0, lines_1.fromString)("mixed", options);
  1226. case "ArrayTypeAnnotation":
  1227. return (0, lines_1.concat)([path.call(print, "elementType"), "[]"]);
  1228. case "TupleTypeAnnotation": {
  1229. var printed_2 = path.map(print, "types");
  1230. var joined = (0, lines_1.fromString)(", ").join(printed_2);
  1231. var oneLine_3 = joined.getLineLength(1) <= options.wrapColumn;
  1232. if (oneLine_3) {
  1233. if (options.arrayBracketSpacing) {
  1234. parts.push("[ ");
  1235. }
  1236. else {
  1237. parts.push("[");
  1238. }
  1239. }
  1240. else {
  1241. parts.push("[\n");
  1242. }
  1243. path.each(function (elemPath) {
  1244. var i = elemPath.getName();
  1245. var elem = elemPath.getValue();
  1246. if (!elem) {
  1247. // If the array expression ends with a hole, that hole
  1248. // will be ignored by the interpreter, but if it ends with
  1249. // two (or more) holes, we need to write out two (or more)
  1250. // commas so that the resulting code is interpreted with
  1251. // both (all) of the holes.
  1252. parts.push(",");
  1253. }
  1254. else {
  1255. var lines = printed_2[i];
  1256. if (oneLine_3) {
  1257. if (i > 0)
  1258. parts.push(" ");
  1259. }
  1260. else {
  1261. lines = lines.indent(options.tabWidth);
  1262. }
  1263. parts.push(lines);
  1264. if (i < n.types.length - 1 ||
  1265. (!oneLine_3 && util.isTrailingCommaEnabled(options, "arrays")))
  1266. parts.push(",");
  1267. if (!oneLine_3)
  1268. parts.push("\n");
  1269. }
  1270. }, "types");
  1271. if (oneLine_3 && options.arrayBracketSpacing) {
  1272. parts.push(" ]");
  1273. }
  1274. else {
  1275. parts.push("]");
  1276. }
  1277. return (0, lines_1.concat)(parts);
  1278. }
  1279. case "BooleanTypeAnnotation":
  1280. return (0, lines_1.fromString)("boolean", options);
  1281. case "BooleanLiteralTypeAnnotation":
  1282. assert_1.default.strictEqual(typeof n.value, "boolean");
  1283. return (0, lines_1.fromString)("" + n.value, options);
  1284. case "InterfaceTypeAnnotation":
  1285. parts.push("interface");
  1286. if (n.extends && n.extends.length > 0) {
  1287. parts.push(" extends ", (0, lines_1.fromString)(", ").join(path.map(print, "extends")));
  1288. }
  1289. parts.push(" ", path.call(print, "body"));
  1290. return (0, lines_1.concat)(parts);
  1291. case "DeclareFunction":
  1292. return printFlowDeclaration(path, [
  1293. "function ",
  1294. path.call(print, "id"),
  1295. ";",
  1296. ]);
  1297. case "DeclareModule":
  1298. return printFlowDeclaration(path, [
  1299. "module ",
  1300. path.call(print, "id"),
  1301. " ",
  1302. path.call(print, "body"),
  1303. ]);
  1304. case "DeclareModuleExports":
  1305. return printFlowDeclaration(path, [
  1306. "module.exports",
  1307. path.call(print, "typeAnnotation"),
  1308. ]);
  1309. case "DeclareVariable":
  1310. return printFlowDeclaration(path, ["var ", path.call(print, "id"), ";"]);
  1311. case "DeclareExportDeclaration":
  1312. case "DeclareExportAllDeclaration":
  1313. return (0, lines_1.concat)(["declare ", printExportDeclaration(path, options, print)]);
  1314. case "EnumDeclaration":
  1315. return (0, lines_1.concat)([
  1316. "enum ",
  1317. path.call(print, "id"),
  1318. path.call(print, "body"),
  1319. ]);
  1320. case "EnumBooleanBody":
  1321. case "EnumNumberBody":
  1322. case "EnumStringBody":
  1323. case "EnumSymbolBody": {
  1324. if (n.type === "EnumSymbolBody" || n.explicitType) {
  1325. parts.push(" of ",
  1326. // EnumBooleanBody => boolean, etc.
  1327. n.type.slice(4, -4).toLowerCase());
  1328. }
  1329. parts.push(" {\n", (0, lines_1.fromString)("\n")
  1330. .join(path.map(print, "members"))
  1331. .indent(options.tabWidth), "\n}");
  1332. return (0, lines_1.concat)(parts);
  1333. }
  1334. case "EnumDefaultedMember":
  1335. return (0, lines_1.concat)([path.call(print, "id"), ","]);
  1336. case "EnumBooleanMember":
  1337. case "EnumNumberMember":
  1338. case "EnumStringMember":
  1339. return (0, lines_1.concat)([
  1340. path.call(print, "id"),
  1341. " = ",
  1342. path.call(print, "init"),
  1343. ",",
  1344. ]);
  1345. case "InferredPredicate":
  1346. return (0, lines_1.fromString)("%checks", options);
  1347. case "DeclaredPredicate":
  1348. return (0, lines_1.concat)(["%checks(", path.call(print, "value"), ")"]);
  1349. case "FunctionTypeAnnotation": {
  1350. // FunctionTypeAnnotation is ambiguous:
  1351. // declare function(a: B): void; OR
  1352. // const A: (a: B) => void;
  1353. var parent = path.getParentNode(0);
  1354. var isArrowFunctionTypeAnnotation = !(namedTypes.ObjectTypeCallProperty.check(parent) ||
  1355. (namedTypes.ObjectTypeInternalSlot.check(parent) && parent.method) ||
  1356. namedTypes.DeclareFunction.check(path.getParentNode(2)));
  1357. var needsColon = isArrowFunctionTypeAnnotation &&
  1358. !namedTypes.FunctionTypeParam.check(parent) &&
  1359. !namedTypes.TypeAlias.check(parent);
  1360. if (needsColon) {
  1361. parts.push(": ");
  1362. }
  1363. var hasTypeParameters = !!n.typeParameters;
  1364. var needsParens = hasTypeParameters || n.params.length !== 1 || n.params[0].name;
  1365. parts.push(hasTypeParameters ? path.call(print, "typeParameters") : "", needsParens ? "(" : "", printFunctionParams(path, options, print), needsParens ? ")" : "");
  1366. // The returnType is not wrapped in a TypeAnnotation, so the colon
  1367. // needs to be added separately.
  1368. if (n.returnType) {
  1369. parts.push(isArrowFunctionTypeAnnotation ? " => " : ": ", path.call(print, "returnType"));
  1370. }
  1371. return (0, lines_1.concat)(parts);
  1372. }
  1373. case "FunctionTypeParam": {
  1374. var name = path.call(print, "name");
  1375. parts.push(name);
  1376. if (n.optional) {
  1377. parts.push("?");
  1378. }
  1379. if (name.infos[0].line) {
  1380. parts.push(": ");
  1381. }
  1382. parts.push(path.call(print, "typeAnnotation"));
  1383. return (0, lines_1.concat)(parts);
  1384. }
  1385. case "GenericTypeAnnotation":
  1386. return (0, lines_1.concat)([
  1387. path.call(print, "id"),
  1388. path.call(print, "typeParameters"),
  1389. ]);
  1390. case "DeclareInterface":
  1391. parts.push("declare ");
  1392. // Fall through to InterfaceDeclaration...
  1393. case "InterfaceDeclaration":
  1394. case "TSInterfaceDeclaration":
  1395. if (n.declare) {
  1396. parts.push("declare ");
  1397. }
  1398. parts.push("interface ", path.call(print, "id"), path.call(print, "typeParameters"), " ");
  1399. if (n["extends"] && n["extends"].length > 0) {
  1400. parts.push("extends ", (0, lines_1.fromString)(", ").join(path.map(print, "extends")), " ");
  1401. }
  1402. if (n.body) {
  1403. parts.push(path.call(print, "body"));
  1404. }
  1405. return (0, lines_1.concat)(parts);
  1406. case "ClassImplements":
  1407. case "InterfaceExtends":
  1408. return (0, lines_1.concat)([
  1409. path.call(print, "id"),
  1410. path.call(print, "typeParameters"),
  1411. ]);
  1412. case "IntersectionTypeAnnotation":
  1413. return (0, lines_1.fromString)(" & ").join(path.map(print, "types"));
  1414. case "NullableTypeAnnotation":
  1415. return (0, lines_1.concat)(["?", path.call(print, "typeAnnotation")]);
  1416. case "NullLiteralTypeAnnotation":
  1417. return (0, lines_1.fromString)("null", options);
  1418. case "ThisTypeAnnotation":
  1419. return (0, lines_1.fromString)("this", options);
  1420. case "NumberTypeAnnotation":
  1421. return (0, lines_1.fromString)("number", options);
  1422. case "ObjectTypeCallProperty":
  1423. return path.call(print, "value");
  1424. case "ObjectTypeIndexer":
  1425. if (n.static) {
  1426. parts.push("static ");
  1427. }
  1428. parts.push(printVariance(path, print), "[");
  1429. if (n.id) {
  1430. parts.push(path.call(print, "id"), ": ");
  1431. }
  1432. parts.push(path.call(print, "key"), "]: ", path.call(print, "value"));
  1433. return (0, lines_1.concat)(parts);
  1434. case "ObjectTypeProperty":
  1435. return (0, lines_1.concat)([
  1436. printVariance(path, print),
  1437. path.call(print, "key"),
  1438. n.optional ? "?" : "",
  1439. ": ",
  1440. path.call(print, "value"),
  1441. ]);
  1442. case "ObjectTypeInternalSlot":
  1443. return (0, lines_1.concat)([
  1444. n.static ? "static " : "",
  1445. "[[",
  1446. path.call(print, "id"),
  1447. "]]",
  1448. n.optional ? "?" : "",
  1449. n.value.type !== "FunctionTypeAnnotation" ? ": " : "",
  1450. path.call(print, "value"),
  1451. ]);
  1452. case "QualifiedTypeIdentifier":
  1453. return (0, lines_1.concat)([
  1454. path.call(print, "qualification"),
  1455. ".",
  1456. path.call(print, "id"),
  1457. ]);
  1458. case "StringLiteralTypeAnnotation":
  1459. return (0, lines_1.fromString)(nodeStr(n.value, options), options);
  1460. case "NumberLiteralTypeAnnotation":
  1461. case "NumericLiteralTypeAnnotation":
  1462. assert_1.default.strictEqual(typeof n.value, "number");
  1463. return (0, lines_1.fromString)(JSON.stringify(n.value), options);
  1464. case "BigIntLiteralTypeAnnotation":
  1465. return (0, lines_1.fromString)(n.raw, options);
  1466. case "StringTypeAnnotation":
  1467. return (0, lines_1.fromString)("string", options);
  1468. case "DeclareTypeAlias":
  1469. parts.push("declare ");
  1470. // Fall through to TypeAlias...
  1471. case "TypeAlias":
  1472. return (0, lines_1.concat)([
  1473. "type ",
  1474. path.call(print, "id"),
  1475. path.call(print, "typeParameters"),
  1476. " = ",
  1477. path.call(print, "right"),
  1478. ";",
  1479. ]);
  1480. case "DeclareOpaqueType":
  1481. parts.push("declare ");
  1482. // Fall through to OpaqueType...
  1483. case "OpaqueType":
  1484. parts.push("opaque type ", path.call(print, "id"), path.call(print, "typeParameters"));
  1485. if (n["supertype"]) {
  1486. parts.push(": ", path.call(print, "supertype"));
  1487. }
  1488. if (n["impltype"]) {
  1489. parts.push(" = ", path.call(print, "impltype"));
  1490. }
  1491. parts.push(";");
  1492. return (0, lines_1.concat)(parts);
  1493. case "TypeCastExpression":
  1494. return (0, lines_1.concat)([
  1495. "(",
  1496. path.call(print, "expression"),
  1497. path.call(print, "typeAnnotation"),
  1498. ")",
  1499. ]);
  1500. case "TypeParameterDeclaration":
  1501. case "TypeParameterInstantiation":
  1502. return (0, lines_1.concat)([
  1503. "<",
  1504. (0, lines_1.fromString)(", ").join(path.map(print, "params")),
  1505. ">",
  1506. ]);
  1507. case "Variance":
  1508. if (n.kind === "plus") {
  1509. return (0, lines_1.fromString)("+");
  1510. }
  1511. if (n.kind === "minus") {
  1512. return (0, lines_1.fromString)("-");
  1513. }
  1514. return (0, lines_1.fromString)("");
  1515. case "TypeParameter":
  1516. if (n.variance) {
  1517. parts.push(printVariance(path, print));
  1518. }
  1519. parts.push(path.call(print, "name"));
  1520. if (n.bound) {
  1521. parts.push(path.call(print, "bound"));
  1522. }
  1523. if (n["default"]) {
  1524. parts.push("=", path.call(print, "default"));
  1525. }
  1526. return (0, lines_1.concat)(parts);
  1527. case "TypeofTypeAnnotation":
  1528. return (0, lines_1.concat)([
  1529. (0, lines_1.fromString)("typeof ", options),
  1530. path.call(print, "argument"),
  1531. ]);
  1532. case "IndexedAccessType":
  1533. case "OptionalIndexedAccessType":
  1534. return (0, lines_1.concat)([
  1535. path.call(print, "objectType"),
  1536. n.optional ? "?." : "",
  1537. "[",
  1538. path.call(print, "indexType"),
  1539. "]",
  1540. ]);
  1541. case "UnionTypeAnnotation":
  1542. return (0, lines_1.fromString)(" | ").join(path.map(print, "types"));
  1543. case "VoidTypeAnnotation":
  1544. return (0, lines_1.fromString)("void", options);
  1545. case "NullTypeAnnotation":
  1546. return (0, lines_1.fromString)("null", options);
  1547. case "SymbolTypeAnnotation":
  1548. return (0, lines_1.fromString)("symbol", options);
  1549. case "BigIntTypeAnnotation":
  1550. return (0, lines_1.fromString)("bigint", options);
  1551. // Type Annotations for TypeScript (when using Babylon as parser)
  1552. case "TSType":
  1553. throw new Error("unprintable type: " + JSON.stringify(n.type));
  1554. case "TSNumberKeyword":
  1555. return (0, lines_1.fromString)("number", options);
  1556. case "TSBigIntKeyword":
  1557. return (0, lines_1.fromString)("bigint", options);
  1558. case "TSObjectKeyword":
  1559. return (0, lines_1.fromString)("object", options);
  1560. case "TSBooleanKeyword":
  1561. return (0, lines_1.fromString)("boolean", options);
  1562. case "TSStringKeyword":
  1563. return (0, lines_1.fromString)("string", options);
  1564. case "TSSymbolKeyword":
  1565. return (0, lines_1.fromString)("symbol", options);
  1566. case "TSAnyKeyword":
  1567. return (0, lines_1.fromString)("any", options);
  1568. case "TSVoidKeyword":
  1569. return (0, lines_1.fromString)("void", options);
  1570. case "TSIntrinsicKeyword":
  1571. return (0, lines_1.fromString)("intrinsic", options);
  1572. case "TSThisType":
  1573. return (0, lines_1.fromString)("this", options);
  1574. case "TSNullKeyword":
  1575. return (0, lines_1.fromString)("null", options);
  1576. case "TSUndefinedKeyword":
  1577. return (0, lines_1.fromString)("undefined", options);
  1578. case "TSUnknownKeyword":
  1579. return (0, lines_1.fromString)("unknown", options);
  1580. case "TSNeverKeyword":
  1581. return (0, lines_1.fromString)("never", options);
  1582. case "TSArrayType":
  1583. return (0, lines_1.concat)([path.call(print, "elementType"), "[]"]);
  1584. case "TSLiteralType":
  1585. return path.call(print, "literal");
  1586. case "TSUnionType":
  1587. return (0, lines_1.fromString)(" | ").join(path.map(print, "types"));
  1588. case "TSIntersectionType":
  1589. return (0, lines_1.fromString)(" & ").join(path.map(print, "types"));
  1590. case "TSConditionalType":
  1591. parts.push(path.call(print, "checkType"), " extends ", path.call(print, "extendsType"), " ? ", path.call(print, "trueType"), " : ", path.call(print, "falseType"));
  1592. return (0, lines_1.concat)(parts);
  1593. case "TSInferType":
  1594. parts.push("infer ", path.call(print, "typeParameter"));
  1595. return (0, lines_1.concat)(parts);
  1596. case "TSParenthesizedType":
  1597. return (0, lines_1.concat)(["(", path.call(print, "typeAnnotation"), ")"]);
  1598. case "TSFunctionType":
  1599. return (0, lines_1.concat)([
  1600. path.call(print, "typeParameters"),
  1601. "(",
  1602. printFunctionParams(path, options, print),
  1603. ") => ",
  1604. path.call(print, "typeAnnotation", "typeAnnotation"),
  1605. ]);
  1606. case "TSConstructorType":
  1607. return (0, lines_1.concat)([
  1608. "new ",
  1609. path.call(print, "typeParameters"),
  1610. "(",
  1611. printFunctionParams(path, options, print),
  1612. ") => ",
  1613. path.call(print, "typeAnnotation", "typeAnnotation"),
  1614. ]);
  1615. case "TSMappedType": {
  1616. parts.push(n.readonly ? "readonly " : "", "[", path.call(print, "typeParameter"), "]", n.optional ? "?" : "");
  1617. if (n.typeAnnotation) {
  1618. parts.push(": ", path.call(print, "typeAnnotation"), ";");
  1619. }
  1620. return (0, lines_1.concat)(["{\n", (0, lines_1.concat)(parts).indent(options.tabWidth), "\n}"]);
  1621. }
  1622. case "TSTupleType":
  1623. return (0, lines_1.concat)([
  1624. "[",
  1625. (0, lines_1.fromString)(", ").join(path.map(print, "elementTypes")),
  1626. "]",
  1627. ]);
  1628. case "TSNamedTupleMember":
  1629. parts.push(path.call(print, "label"));
  1630. if (n.optional) {
  1631. parts.push("?");
  1632. }
  1633. parts.push(": ", path.call(print, "elementType"));
  1634. return (0, lines_1.concat)(parts);
  1635. case "TSRestType":
  1636. return (0, lines_1.concat)(["...", path.call(print, "typeAnnotation")]);
  1637. case "TSOptionalType":
  1638. return (0, lines_1.concat)([path.call(print, "typeAnnotation"), "?"]);
  1639. case "TSIndexedAccessType":
  1640. return (0, lines_1.concat)([
  1641. path.call(print, "objectType"),
  1642. "[",
  1643. path.call(print, "indexType"),
  1644. "]",
  1645. ]);
  1646. case "TSTypeOperator":
  1647. return (0, lines_1.concat)([
  1648. path.call(print, "operator"),
  1649. " ",
  1650. path.call(print, "typeAnnotation"),
  1651. ]);
  1652. case "TSTypeLiteral": {
  1653. var members = (0, lines_1.fromString)("\n").join(path.map(print, "members").map(function (member) {
  1654. if (lastNonSpaceCharacter(member) !== ";") {
  1655. return member.concat(";");
  1656. }
  1657. return member;
  1658. }));
  1659. if (members.isEmpty()) {
  1660. return (0, lines_1.fromString)("{}", options);
  1661. }
  1662. parts.push("{\n", members.indent(options.tabWidth), "\n}");
  1663. return (0, lines_1.concat)(parts);
  1664. }
  1665. case "TSEnumMember":
  1666. parts.push(path.call(print, "id"));
  1667. if (n.initializer) {
  1668. parts.push(" = ", path.call(print, "initializer"));
  1669. }
  1670. return (0, lines_1.concat)(parts);
  1671. case "TSTypeQuery":
  1672. return (0, lines_1.concat)(["typeof ", path.call(print, "exprName")]);
  1673. case "TSParameterProperty":
  1674. if (n.accessibility) {
  1675. parts.push(n.accessibility, " ");
  1676. }
  1677. if (n.export) {
  1678. parts.push("export ");
  1679. }
  1680. if (n.static) {
  1681. parts.push("static ");
  1682. }
  1683. if (n.readonly) {
  1684. parts.push("readonly ");
  1685. }
  1686. parts.push(path.call(print, "parameter"));
  1687. return (0, lines_1.concat)(parts);
  1688. case "TSTypeReference":
  1689. return (0, lines_1.concat)([
  1690. path.call(print, "typeName"),
  1691. path.call(print, "typeParameters"),
  1692. ]);
  1693. case "TSQualifiedName":
  1694. return (0, lines_1.concat)([path.call(print, "left"), ".", path.call(print, "right")]);
  1695. case "TSAsExpression":
  1696. case "TSSatisfiesExpression":
  1697. {
  1698. var expression = path.call(print, "expression");
  1699. parts.push(expression, n.type === "TSSatisfiesExpression" ? " satisfies " : " as ", path.call(print, "typeAnnotation"));
  1700. return (0, lines_1.concat)(parts);
  1701. }
  1702. case "TSTypeCastExpression":
  1703. return (0, lines_1.concat)([
  1704. path.call(print, "expression"),
  1705. path.call(print, "typeAnnotation"),
  1706. ]);
  1707. case "TSNonNullExpression":
  1708. return (0, lines_1.concat)([path.call(print, "expression"), "!"]);
  1709. case "TSTypeAnnotation":
  1710. return (0, lines_1.concat)([": ", path.call(print, "typeAnnotation")]);
  1711. case "TSIndexSignature":
  1712. return (0, lines_1.concat)([
  1713. n.readonly ? "readonly " : "",
  1714. "[",
  1715. path.map(print, "parameters"),
  1716. "]",
  1717. path.call(print, "typeAnnotation"),
  1718. ]);
  1719. case "TSPropertySignature":
  1720. parts.push(printVariance(path, print), n.readonly ? "readonly " : "");
  1721. if (n.computed) {
  1722. parts.push("[", path.call(print, "key"), "]");
  1723. }
  1724. else {
  1725. parts.push(path.call(print, "key"));
  1726. }
  1727. parts.push(n.optional ? "?" : "", path.call(print, "typeAnnotation"));
  1728. return (0, lines_1.concat)(parts);
  1729. case "TSMethodSignature":
  1730. if (n.computed) {
  1731. parts.push("[", path.call(print, "key"), "]");
  1732. }
  1733. else {
  1734. parts.push(path.call(print, "key"));
  1735. }
  1736. if (n.optional) {
  1737. parts.push("?");
  1738. }
  1739. parts.push(path.call(print, "typeParameters"), "(", printFunctionParams(path, options, print), ")", path.call(print, "typeAnnotation"));
  1740. return (0, lines_1.concat)(parts);
  1741. case "TSTypePredicate":
  1742. if (n.asserts) {
  1743. parts.push("asserts ");
  1744. }
  1745. parts.push(path.call(print, "parameterName"));
  1746. if (n.typeAnnotation) {
  1747. parts.push(" is ", path.call(print, "typeAnnotation", "typeAnnotation"));
  1748. }
  1749. return (0, lines_1.concat)(parts);
  1750. case "TSCallSignatureDeclaration":
  1751. return (0, lines_1.concat)([
  1752. path.call(print, "typeParameters"),
  1753. "(",
  1754. printFunctionParams(path, options, print),
  1755. ")",
  1756. path.call(print, "typeAnnotation"),
  1757. ]);
  1758. case "TSConstructSignatureDeclaration":
  1759. if (n.typeParameters) {
  1760. parts.push("new", path.call(print, "typeParameters"));
  1761. }
  1762. else {
  1763. parts.push("new ");
  1764. }
  1765. parts.push("(", printFunctionParams(path, options, print), ")", path.call(print, "typeAnnotation"));
  1766. return (0, lines_1.concat)(parts);
  1767. case "TSTypeAliasDeclaration":
  1768. return (0, lines_1.concat)([
  1769. n.declare ? "declare " : "",
  1770. "type ",
  1771. path.call(print, "id"),
  1772. path.call(print, "typeParameters"),
  1773. " = ",
  1774. path.call(print, "typeAnnotation"),
  1775. ";",
  1776. ]);
  1777. case "TSTypeParameter": {
  1778. parts.push(path.call(print, "name"));
  1779. // ambiguous because of TSMappedType
  1780. var parent = path.getParentNode(0);
  1781. var isInMappedType = namedTypes.TSMappedType.check(parent);
  1782. if (n.constraint) {
  1783. parts.push(isInMappedType ? " in " : " extends ", path.call(print, "constraint"));
  1784. }
  1785. if (n["default"]) {
  1786. parts.push(" = ", path.call(print, "default"));
  1787. }
  1788. return (0, lines_1.concat)(parts);
  1789. }
  1790. case "TSTypeAssertion": {
  1791. parts.push("<", path.call(print, "typeAnnotation"), "> ", path.call(print, "expression"));
  1792. return (0, lines_1.concat)(parts);
  1793. }
  1794. case "TSTypeParameterDeclaration":
  1795. case "TSTypeParameterInstantiation":
  1796. return (0, lines_1.concat)([
  1797. "<",
  1798. (0, lines_1.fromString)(", ").join(path.map(print, "params")),
  1799. ">",
  1800. ]);
  1801. case "TSEnumDeclaration": {
  1802. parts.push(n.declare ? "declare " : "", n.const ? "const " : "", "enum ", path.call(print, "id"));
  1803. var memberLines = (0, lines_1.fromString)(",\n").join(path.map(print, "members"));
  1804. if (memberLines.isEmpty()) {
  1805. parts.push(" {}");
  1806. }
  1807. else {
  1808. parts.push(" {\n", memberLines.indent(options.tabWidth), "\n}");
  1809. }
  1810. return (0, lines_1.concat)(parts);
  1811. }
  1812. case "TSExpressionWithTypeArguments":
  1813. return (0, lines_1.concat)([
  1814. path.call(print, "expression"),
  1815. path.call(print, "typeParameters"),
  1816. ]);
  1817. case "TSInterfaceBody": {
  1818. var lines = (0, lines_1.fromString)("\n").join(path.map(print, "body").map(function (element) {
  1819. if (lastNonSpaceCharacter(element) !== ";") {
  1820. return element.concat(";");
  1821. }
  1822. return element;
  1823. }));
  1824. if (lines.isEmpty()) {
  1825. return (0, lines_1.fromString)("{}", options);
  1826. }
  1827. return (0, lines_1.concat)(["{\n", lines.indent(options.tabWidth), "\n}"]);
  1828. }
  1829. case "TSImportType":
  1830. parts.push("import(", path.call(print, "argument"), ")");
  1831. if (n.qualifier) {
  1832. parts.push(".", path.call(print, "qualifier"));
  1833. }
  1834. if (n.typeParameters) {
  1835. parts.push(path.call(print, "typeParameters"));
  1836. }
  1837. return (0, lines_1.concat)(parts);
  1838. case "TSImportEqualsDeclaration":
  1839. if (n.isExport) {
  1840. parts.push("export ");
  1841. }
  1842. parts.push("import ", path.call(print, "id"), " = ", path.call(print, "moduleReference"));
  1843. return maybeAddSemicolon((0, lines_1.concat)(parts));
  1844. case "TSExternalModuleReference":
  1845. return (0, lines_1.concat)(["require(", path.call(print, "expression"), ")"]);
  1846. case "TSModuleDeclaration": {
  1847. var parent = path.getParentNode();
  1848. if (parent.type === "TSModuleDeclaration") {
  1849. parts.push(".");
  1850. }
  1851. else {
  1852. if (n.declare) {
  1853. parts.push("declare ");
  1854. }
  1855. if (!n.global) {
  1856. var isExternal = n.id.type === "StringLiteral" ||
  1857. (n.id.type === "Literal" && typeof n.id.value === "string");
  1858. if (isExternal) {
  1859. parts.push("module ");
  1860. }
  1861. else if (n.loc && n.loc.lines && n.id.loc) {
  1862. var prefix = n.loc.lines.sliceString(n.loc.start, n.id.loc.start);
  1863. // These keywords are fundamentally ambiguous in the
  1864. // Babylon parser, and not reflected in the AST, so
  1865. // the best we can do is to match the original code,
  1866. // when possible.
  1867. if (prefix.indexOf("module") >= 0) {
  1868. parts.push("module ");
  1869. }
  1870. else {
  1871. parts.push("namespace ");
  1872. }
  1873. }
  1874. else {
  1875. parts.push("namespace ");
  1876. }
  1877. }
  1878. }
  1879. parts.push(path.call(print, "id"));
  1880. if (n.body) {
  1881. parts.push(" ");
  1882. parts.push(path.call(print, "body"));
  1883. }
  1884. return (0, lines_1.concat)(parts);
  1885. }
  1886. case "TSModuleBlock": {
  1887. var naked = path.call(function (bodyPath) { return printStatementSequence(bodyPath, options, print); }, "body");
  1888. if (naked.isEmpty()) {
  1889. parts.push("{}");
  1890. }
  1891. else {
  1892. parts.push("{\n", naked.indent(options.tabWidth), "\n}");
  1893. }
  1894. return (0, lines_1.concat)(parts);
  1895. }
  1896. case "TSInstantiationExpression": {
  1897. parts.push(path.call(print, "expression"), path.call(print, "typeParameters"));
  1898. return (0, lines_1.concat)(parts);
  1899. }
  1900. // https://github.com/babel/babel/pull/10148
  1901. case "V8IntrinsicIdentifier":
  1902. return (0, lines_1.concat)(["%", path.call(print, "name")]);
  1903. // https://github.com/babel/babel/pull/13191
  1904. case "TopicReference":
  1905. return (0, lines_1.fromString)("#");
  1906. // Unhandled types below. If encountered, nodes of these types should
  1907. // be either left alone or desugared into AST types that are fully
  1908. // supported by the pretty-printer.
  1909. case "ClassHeritage": // TODO
  1910. case "ComprehensionBlock": // TODO
  1911. case "ComprehensionExpression": // TODO
  1912. case "Glob": // TODO
  1913. case "GeneratorExpression": // TODO
  1914. case "LetStatement": // TODO
  1915. case "LetExpression": // TODO
  1916. case "GraphExpression": // TODO
  1917. case "GraphIndexExpression": // TODO
  1918. case "XMLDefaultDeclaration":
  1919. case "XMLAnyName":
  1920. case "XMLQualifiedIdentifier":
  1921. case "XMLFunctionQualifiedIdentifier":
  1922. case "XMLAttributeSelector":
  1923. case "XMLFilterExpression":
  1924. case "XML":
  1925. case "XMLElement":
  1926. case "XMLList":
  1927. case "XMLEscape":
  1928. case "XMLText":
  1929. case "XMLStartTag":
  1930. case "XMLEndTag":
  1931. case "XMLPointTag":
  1932. case "XMLName":
  1933. case "XMLAttribute":
  1934. case "XMLCdata":
  1935. case "XMLComment":
  1936. case "XMLProcessingInstruction":
  1937. default:
  1938. debugger;
  1939. throw new Error("unknown type: " + JSON.stringify(n.type));
  1940. }
  1941. }
  1942. function printDecorators(path, printPath) {
  1943. var parts = [];
  1944. var node = path.getValue();
  1945. if (node.decorators &&
  1946. node.decorators.length > 0 &&
  1947. // If the parent node is an export declaration, it will be
  1948. // responsible for printing node.decorators.
  1949. !util.getParentExportDeclaration(path)) {
  1950. path.each(function (decoratorPath) {
  1951. parts.push(printPath(decoratorPath), "\n");
  1952. }, "decorators");
  1953. }
  1954. else if (util.isExportDeclaration(node) &&
  1955. node.declaration &&
  1956. node.declaration.decorators) {
  1957. // Export declarations are responsible for printing any decorators
  1958. // that logically apply to node.declaration.
  1959. path.each(function (decoratorPath) {
  1960. parts.push(printPath(decoratorPath), "\n");
  1961. }, "declaration", "decorators");
  1962. }
  1963. return (0, lines_1.concat)(parts);
  1964. }
  1965. function printStatementSequence(path, options, print) {
  1966. var filtered = [];
  1967. var sawComment = false;
  1968. var sawStatement = false;
  1969. path.each(function (stmtPath) {
  1970. var stmt = stmtPath.getValue();
  1971. // Just in case the AST has been modified to contain falsy
  1972. // "statements," it's safer simply to skip them.
  1973. if (!stmt) {
  1974. return;
  1975. }
  1976. // Skip printing EmptyStatement nodes to avoid leaving stray
  1977. // semicolons lying around.
  1978. if (stmt.type === "EmptyStatement" &&
  1979. !(stmt.comments && stmt.comments.length > 0)) {
  1980. return;
  1981. }
  1982. if (namedTypes.Comment.check(stmt)) {
  1983. // The pretty printer allows a dangling Comment node to act as
  1984. // a Statement when the Comment can't be attached to any other
  1985. // non-Comment node in the tree.
  1986. sawComment = true;
  1987. }
  1988. else if (namedTypes.Statement.check(stmt)) {
  1989. sawStatement = true;
  1990. }
  1991. else {
  1992. // When the pretty printer encounters a string instead of an
  1993. // AST node, it just prints the string. This behavior can be
  1994. // useful for fine-grained formatting decisions like inserting
  1995. // blank lines.
  1996. isString.assert(stmt);
  1997. }
  1998. // We can't hang onto stmtPath outside of this function, because
  1999. // it's just a reference to a mutable FastPath object, so we have
  2000. // to go ahead and print it here.
  2001. filtered.push({
  2002. node: stmt,
  2003. printed: print(stmtPath),
  2004. });
  2005. });
  2006. if (sawComment) {
  2007. assert_1.default.strictEqual(sawStatement, false, "Comments may appear as statements in otherwise empty statement " +
  2008. "lists, but may not coexist with non-Comment nodes.");
  2009. }
  2010. var prevTrailingSpace = null;
  2011. var len = filtered.length;
  2012. var parts = [];
  2013. filtered.forEach(function (info, i) {
  2014. var printed = info.printed;
  2015. var stmt = info.node;
  2016. var multiLine = printed.length > 1;
  2017. var notFirst = i > 0;
  2018. var notLast = i < len - 1;
  2019. var leadingSpace;
  2020. var trailingSpace;
  2021. var lines = stmt && stmt.loc && stmt.loc.lines;
  2022. var trueLoc = lines && options.reuseWhitespace && util.getTrueLoc(stmt, lines);
  2023. if (notFirst) {
  2024. if (trueLoc) {
  2025. var beforeStart = lines.skipSpaces(trueLoc.start, true);
  2026. var beforeStartLine = beforeStart ? beforeStart.line : 1;
  2027. var leadingGap = trueLoc.start.line - beforeStartLine;
  2028. leadingSpace = Array(leadingGap + 1).join("\n");
  2029. }
  2030. else {
  2031. leadingSpace = multiLine ? "\n\n" : "\n";
  2032. }
  2033. }
  2034. else {
  2035. leadingSpace = "";
  2036. }
  2037. if (notLast) {
  2038. if (trueLoc) {
  2039. var afterEnd = lines.skipSpaces(trueLoc.end);
  2040. var afterEndLine = afterEnd ? afterEnd.line : lines.length;
  2041. var trailingGap = afterEndLine - trueLoc.end.line;
  2042. trailingSpace = Array(trailingGap + 1).join("\n");
  2043. }
  2044. else {
  2045. trailingSpace = multiLine ? "\n\n" : "\n";
  2046. }
  2047. }
  2048. else {
  2049. trailingSpace = "";
  2050. }
  2051. parts.push(maxSpace(prevTrailingSpace, leadingSpace), printed);
  2052. if (notLast) {
  2053. prevTrailingSpace = trailingSpace;
  2054. }
  2055. else if (trailingSpace) {
  2056. parts.push(trailingSpace);
  2057. }
  2058. });
  2059. return (0, lines_1.concat)(parts);
  2060. }
  2061. function maxSpace(s1, s2) {
  2062. if (!s1 && !s2) {
  2063. return (0, lines_1.fromString)("");
  2064. }
  2065. if (!s1) {
  2066. return (0, lines_1.fromString)(s2);
  2067. }
  2068. if (!s2) {
  2069. return (0, lines_1.fromString)(s1);
  2070. }
  2071. var spaceLines1 = (0, lines_1.fromString)(s1);
  2072. var spaceLines2 = (0, lines_1.fromString)(s2);
  2073. if (spaceLines2.length > spaceLines1.length) {
  2074. return spaceLines2;
  2075. }
  2076. return spaceLines1;
  2077. }
  2078. function printClassMemberModifiers(node) {
  2079. var parts = [];
  2080. if (node.declare) {
  2081. parts.push("declare ");
  2082. }
  2083. var access = node.accessibility || node.access;
  2084. if (typeof access === "string") {
  2085. parts.push(access, " ");
  2086. }
  2087. if (node.static) {
  2088. parts.push("static ");
  2089. }
  2090. if (node.override) {
  2091. parts.push("override ");
  2092. }
  2093. if (node.abstract) {
  2094. parts.push("abstract ");
  2095. }
  2096. if (node.readonly) {
  2097. parts.push("readonly ");
  2098. }
  2099. return parts;
  2100. }
  2101. function printMethod(path, options, print) {
  2102. var node = path.getNode();
  2103. var kind = node.kind;
  2104. var parts = [];
  2105. var nodeValue = node.value;
  2106. if (!namedTypes.FunctionExpression.check(nodeValue)) {
  2107. nodeValue = node;
  2108. }
  2109. parts.push.apply(parts, printClassMemberModifiers(node));
  2110. if (nodeValue.async) {
  2111. parts.push("async ");
  2112. }
  2113. if (nodeValue.generator) {
  2114. parts.push("*");
  2115. }
  2116. if (kind === "get" || kind === "set") {
  2117. parts.push(kind, " ");
  2118. }
  2119. var key = path.call(print, "key");
  2120. if (node.computed) {
  2121. key = (0, lines_1.concat)(["[", key, "]"]);
  2122. }
  2123. parts.push(key);
  2124. if (node.optional) {
  2125. parts.push("?");
  2126. }
  2127. if (node === nodeValue) {
  2128. parts.push(path.call(print, "typeParameters"), "(", printFunctionParams(path, options, print), ")", path.call(print, "returnType"));
  2129. if (node.body) {
  2130. parts.push(" ", path.call(print, "body"));
  2131. }
  2132. else {
  2133. parts.push(";");
  2134. }
  2135. }
  2136. else {
  2137. parts.push(path.call(print, "value", "typeParameters"), "(", path.call(function (valuePath) { return printFunctionParams(valuePath, options, print); }, "value"), ")", path.call(print, "value", "returnType"));
  2138. if (nodeValue.body) {
  2139. parts.push(" ", path.call(print, "value", "body"));
  2140. }
  2141. else {
  2142. parts.push(";");
  2143. }
  2144. }
  2145. return (0, lines_1.concat)(parts);
  2146. }
  2147. function printArgumentsList(path, options, print) {
  2148. var printed = path.map(print, "arguments");
  2149. var trailingComma = util.isTrailingCommaEnabled(options, "parameters");
  2150. var joined = (0, lines_1.fromString)(", ").join(printed);
  2151. if (joined.getLineLength(1) > options.wrapColumn) {
  2152. joined = (0, lines_1.fromString)(",\n").join(printed);
  2153. return (0, lines_1.concat)([
  2154. "(\n",
  2155. joined.indent(options.tabWidth),
  2156. trailingComma ? ",\n)" : "\n)",
  2157. ]);
  2158. }
  2159. return (0, lines_1.concat)(["(", joined, ")"]);
  2160. }
  2161. function printFunctionParams(path, options, print) {
  2162. var fun = path.getValue();
  2163. var params;
  2164. var printed = [];
  2165. if (fun.params) {
  2166. params = fun.params;
  2167. printed = path.map(print, "params");
  2168. }
  2169. else if (fun.parameters) {
  2170. params = fun.parameters;
  2171. printed = path.map(print, "parameters");
  2172. }
  2173. if (fun.defaults) {
  2174. path.each(function (defExprPath) {
  2175. var i = defExprPath.getName();
  2176. var p = printed[i];
  2177. if (p && defExprPath.getValue()) {
  2178. printed[i] = (0, lines_1.concat)([p, " = ", print(defExprPath)]);
  2179. }
  2180. }, "defaults");
  2181. }
  2182. if (fun.rest) {
  2183. printed.push((0, lines_1.concat)(["...", path.call(print, "rest")]));
  2184. }
  2185. var joined = (0, lines_1.fromString)(", ").join(printed);
  2186. if (joined.length > 1 || joined.getLineLength(1) > options.wrapColumn) {
  2187. joined = (0, lines_1.fromString)(",\n").join(printed);
  2188. if (util.isTrailingCommaEnabled(options, "parameters") &&
  2189. !fun.rest &&
  2190. params[params.length - 1].type !== "RestElement") {
  2191. joined = (0, lines_1.concat)([joined, ",\n"]);
  2192. }
  2193. else {
  2194. joined = (0, lines_1.concat)([joined, "\n"]);
  2195. }
  2196. return (0, lines_1.concat)(["\n", joined.indent(options.tabWidth)]);
  2197. }
  2198. return joined;
  2199. }
  2200. function maybePrintImportAssertions(path, options, print) {
  2201. var n = path.getValue();
  2202. if (n.assertions && n.assertions.length > 0) {
  2203. var parts = [" assert {"];
  2204. var printed = path.map(print, "assertions");
  2205. var flat = (0, lines_1.fromString)(", ").join(printed);
  2206. if (flat.length > 1 || flat.getLineLength(1) > options.wrapColumn) {
  2207. parts.push("\n", (0, lines_1.fromString)(",\n").join(printed).indent(options.tabWidth), "\n}");
  2208. }
  2209. else {
  2210. parts.push(" ", flat, " }");
  2211. }
  2212. return (0, lines_1.concat)(parts);
  2213. }
  2214. return (0, lines_1.fromString)("");
  2215. }
  2216. function printExportDeclaration(path, options, print) {
  2217. var decl = path.getValue();
  2218. var parts = ["export "];
  2219. if (decl.exportKind && decl.exportKind === "type") {
  2220. if (!decl.declaration) {
  2221. parts.push("type ");
  2222. }
  2223. }
  2224. var shouldPrintSpaces = options.objectCurlySpacing;
  2225. namedTypes.Declaration.assert(decl);
  2226. if (decl["default"] || decl.type === "ExportDefaultDeclaration") {
  2227. parts.push("default ");
  2228. }
  2229. if (decl.declaration) {
  2230. parts.push(path.call(print, "declaration"));
  2231. }
  2232. else if (decl.specifiers) {
  2233. if (decl.specifiers.length === 1 &&
  2234. decl.specifiers[0].type === "ExportBatchSpecifier") {
  2235. parts.push("*");
  2236. }
  2237. else if (decl.specifiers.length === 0) {
  2238. parts.push("{}");
  2239. }
  2240. else if (decl.specifiers[0].type === "ExportDefaultSpecifier") {
  2241. var unbracedSpecifiers_2 = [];
  2242. var bracedSpecifiers_2 = [];
  2243. path.each(function (specifierPath) {
  2244. var spec = specifierPath.getValue();
  2245. if (spec.type === "ExportDefaultSpecifier") {
  2246. unbracedSpecifiers_2.push(print(specifierPath));
  2247. }
  2248. else {
  2249. bracedSpecifiers_2.push(print(specifierPath));
  2250. }
  2251. }, "specifiers");
  2252. unbracedSpecifiers_2.forEach(function (lines, i) {
  2253. if (i > 0) {
  2254. parts.push(", ");
  2255. }
  2256. parts.push(lines);
  2257. });
  2258. if (bracedSpecifiers_2.length > 0) {
  2259. var lines_2 = (0, lines_1.fromString)(", ").join(bracedSpecifiers_2);
  2260. if (lines_2.getLineLength(1) > options.wrapColumn) {
  2261. lines_2 = (0, lines_1.concat)([
  2262. (0, lines_1.fromString)(",\n").join(bracedSpecifiers_2).indent(options.tabWidth),
  2263. ",",
  2264. ]);
  2265. }
  2266. if (unbracedSpecifiers_2.length > 0) {
  2267. parts.push(", ");
  2268. }
  2269. if (lines_2.length > 1) {
  2270. parts.push("{\n", lines_2, "\n}");
  2271. }
  2272. else if (options.objectCurlySpacing) {
  2273. parts.push("{ ", lines_2, " }");
  2274. }
  2275. else {
  2276. parts.push("{", lines_2, "}");
  2277. }
  2278. }
  2279. }
  2280. else {
  2281. parts.push(shouldPrintSpaces ? "{ " : "{", (0, lines_1.fromString)(", ").join(path.map(print, "specifiers")), shouldPrintSpaces ? " }" : "}");
  2282. }
  2283. if (decl.source) {
  2284. parts.push(" from ", path.call(print, "source"), maybePrintImportAssertions(path, options, print));
  2285. }
  2286. }
  2287. var lines = (0, lines_1.concat)(parts);
  2288. if (lastNonSpaceCharacter(lines) !== ";" &&
  2289. !(decl.declaration &&
  2290. (decl.declaration.type === "FunctionDeclaration" ||
  2291. decl.declaration.type === "ClassDeclaration" ||
  2292. decl.declaration.type === "TSModuleDeclaration" ||
  2293. decl.declaration.type === "TSInterfaceDeclaration" ||
  2294. decl.declaration.type === "TSEnumDeclaration"))) {
  2295. lines = (0, lines_1.concat)([lines, ";"]);
  2296. }
  2297. return lines;
  2298. }
  2299. function printFlowDeclaration(path, parts) {
  2300. var parentExportDecl = util.getParentExportDeclaration(path);
  2301. if (parentExportDecl) {
  2302. assert_1.default.strictEqual(parentExportDecl.type, "DeclareExportDeclaration");
  2303. }
  2304. else {
  2305. // If the parent node has type DeclareExportDeclaration, then it
  2306. // will be responsible for printing the "declare" token. Otherwise
  2307. // it needs to be printed with this non-exported declaration node.
  2308. parts.unshift("declare ");
  2309. }
  2310. return (0, lines_1.concat)(parts);
  2311. }
  2312. function printVariance(path, print) {
  2313. return path.call(function (variancePath) {
  2314. var value = variancePath.getValue();
  2315. if (value) {
  2316. if (value === "plus") {
  2317. return (0, lines_1.fromString)("+");
  2318. }
  2319. if (value === "minus") {
  2320. return (0, lines_1.fromString)("-");
  2321. }
  2322. return print(variancePath);
  2323. }
  2324. return (0, lines_1.fromString)("");
  2325. }, "variance");
  2326. }
  2327. function adjustClause(clause, options) {
  2328. if (clause.length > 1)
  2329. return (0, lines_1.concat)([" ", clause]);
  2330. return (0, lines_1.concat)(["\n", maybeAddSemicolon(clause).indent(options.tabWidth)]);
  2331. }
  2332. function lastNonSpaceCharacter(lines) {
  2333. var pos = lines.lastPos();
  2334. do {
  2335. var ch = lines.charAt(pos);
  2336. if (/\S/.test(ch))
  2337. return ch;
  2338. } while (lines.prevPos(pos));
  2339. }
  2340. function endsWithBrace(lines) {
  2341. return lastNonSpaceCharacter(lines) === "}";
  2342. }
  2343. function swapQuotes(str) {
  2344. return str.replace(/['"]/g, function (m) { return (m === '"' ? "'" : '"'); });
  2345. }
  2346. function getPossibleRaw(node) {
  2347. var value = types.getFieldValue(node, "value");
  2348. var extra = types.getFieldValue(node, "extra");
  2349. if (extra && typeof extra.raw === "string" && value == extra.rawValue) {
  2350. return extra.raw;
  2351. }
  2352. if (node.type === "Literal") {
  2353. var raw = node.raw;
  2354. if (typeof raw === "string" && value == raw) {
  2355. return raw;
  2356. }
  2357. }
  2358. }
  2359. function jsSafeStringify(str) {
  2360. return JSON.stringify(str).replace(/[\u2028\u2029]/g, function (m) {
  2361. return "\\u" + m.charCodeAt(0).toString(16);
  2362. });
  2363. }
  2364. function nodeStr(str, options) {
  2365. isString.assert(str);
  2366. switch (options.quote) {
  2367. case "auto": {
  2368. var double = jsSafeStringify(str);
  2369. var single = swapQuotes(jsSafeStringify(swapQuotes(str)));
  2370. return double.length > single.length ? single : double;
  2371. }
  2372. case "single":
  2373. return swapQuotes(jsSafeStringify(swapQuotes(str)));
  2374. case "double":
  2375. default:
  2376. return jsSafeStringify(str);
  2377. }
  2378. }
  2379. function maybeAddSemicolon(lines) {
  2380. var eoc = lastNonSpaceCharacter(lines);
  2381. if (!eoc || "\n};".indexOf(eoc) < 0)
  2382. return (0, lines_1.concat)([lines, ";"]);
  2383. return lines;
  2384. }