printer.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _buffer = require("./buffer.js");
  7. var n = require("./node/index.js");
  8. var _t = require("@babel/types");
  9. var generatorFunctions = require("./generators/index.js");
  10. const {
  11. isFunction,
  12. isStatement,
  13. isClassBody,
  14. isTSInterfaceBody,
  15. isTSEnumDeclaration
  16. } = _t;
  17. const SCIENTIFIC_NOTATION = /e/i;
  18. const ZERO_DECIMAL_INTEGER = /\.0+$/;
  19. const HAS_NEWLINE = /[\n\r\u2028\u2029]/;
  20. const HAS_NEWLINE_OR_BlOCK_COMMENT_END = /[\n\r\u2028\u2029]|\*\//;
  21. const {
  22. needsParens
  23. } = n;
  24. class Printer {
  25. constructor(format, map) {
  26. this.inForStatementInit = false;
  27. this.tokenContext = 0;
  28. this._currentNode = null;
  29. this._indent = 0;
  30. this._indentRepeat = 0;
  31. this._insideAux = false;
  32. this._parenPushNewlineState = null;
  33. this._noLineTerminator = false;
  34. this._printAuxAfterOnNextUserNode = false;
  35. this._printedComments = new Set();
  36. this._endsWithInteger = false;
  37. this._endsWithWord = false;
  38. this._lastCommentLine = 0;
  39. this._endsWithInnerRaw = false;
  40. this._indentInnerComments = true;
  41. this.format = format;
  42. this._indentRepeat = format.indent.style.length;
  43. this._inputMap = map == null ? void 0 : map._inputMap;
  44. this._buf = new _buffer.default(map, format.indent.style[0]);
  45. }
  46. enterForStatementInit(val) {
  47. const old = this.inForStatementInit;
  48. if (old === val) return () => {};
  49. this.inForStatementInit = val;
  50. return () => {
  51. this.inForStatementInit = old;
  52. };
  53. }
  54. generate(ast) {
  55. this.print(ast);
  56. this._maybeAddAuxComment();
  57. return this._buf.get();
  58. }
  59. indent() {
  60. if (this.format.compact || this.format.concise) return;
  61. this._indent++;
  62. }
  63. dedent() {
  64. if (this.format.compact || this.format.concise) return;
  65. this._indent--;
  66. }
  67. semicolon(force = false) {
  68. this._maybeAddAuxComment();
  69. if (force) {
  70. this._appendChar(59);
  71. } else {
  72. this._queue(59);
  73. }
  74. this._noLineTerminator = false;
  75. }
  76. rightBrace(node) {
  77. if (this.format.minified) {
  78. this._buf.removeLastSemicolon();
  79. }
  80. this.sourceWithOffset("end", node.loc, -1);
  81. this.tokenChar(125);
  82. }
  83. rightParens(node) {
  84. this.sourceWithOffset("end", node.loc, -1);
  85. this.tokenChar(41);
  86. }
  87. space(force = false) {
  88. if (this.format.compact) return;
  89. if (force) {
  90. this._space();
  91. } else if (this._buf.hasContent()) {
  92. const lastCp = this.getLastChar();
  93. if (lastCp !== 32 && lastCp !== 10) {
  94. this._space();
  95. }
  96. }
  97. }
  98. word(str, noLineTerminatorAfter = false) {
  99. this.tokenContext = 0;
  100. this._maybePrintInnerComments();
  101. if (this._endsWithWord || str.charCodeAt(0) === 47 && this.endsWith(47)) {
  102. this._space();
  103. }
  104. this._maybeAddAuxComment();
  105. this._append(str, false);
  106. this._endsWithWord = true;
  107. this._noLineTerminator = noLineTerminatorAfter;
  108. }
  109. number(str, number) {
  110. function isNonDecimalLiteral(str) {
  111. if (str.length > 2 && str.charCodeAt(0) === 48) {
  112. const secondChar = str.charCodeAt(1);
  113. return secondChar === 98 || secondChar === 111 || secondChar === 120;
  114. }
  115. return false;
  116. }
  117. this.word(str);
  118. this._endsWithInteger = Number.isInteger(number) && !isNonDecimalLiteral(str) && !SCIENTIFIC_NOTATION.test(str) && !ZERO_DECIMAL_INTEGER.test(str) && str.charCodeAt(str.length - 1) !== 46;
  119. }
  120. token(str, maybeNewline = false) {
  121. this.tokenContext = 0;
  122. this._maybePrintInnerComments();
  123. const lastChar = this.getLastChar();
  124. const strFirst = str.charCodeAt(0);
  125. if (lastChar === 33 && (str === "--" || strFirst === 61) || strFirst === 43 && lastChar === 43 || strFirst === 45 && lastChar === 45 || strFirst === 46 && this._endsWithInteger) {
  126. this._space();
  127. }
  128. this._maybeAddAuxComment();
  129. this._append(str, maybeNewline);
  130. this._noLineTerminator = false;
  131. }
  132. tokenChar(char) {
  133. this.tokenContext = 0;
  134. this._maybePrintInnerComments();
  135. const lastChar = this.getLastChar();
  136. if (char === 43 && lastChar === 43 || char === 45 && lastChar === 45 || char === 46 && this._endsWithInteger) {
  137. this._space();
  138. }
  139. this._maybeAddAuxComment();
  140. this._appendChar(char);
  141. this._noLineTerminator = false;
  142. }
  143. newline(i = 1, force) {
  144. if (i <= 0) return;
  145. if (!force) {
  146. if (this.format.retainLines || this.format.compact) return;
  147. if (this.format.concise) {
  148. this.space();
  149. return;
  150. }
  151. }
  152. if (i > 2) i = 2;
  153. i -= this._buf.getNewlineCount();
  154. for (let j = 0; j < i; j++) {
  155. this._newline();
  156. }
  157. return;
  158. }
  159. endsWith(char) {
  160. return this.getLastChar() === char;
  161. }
  162. getLastChar() {
  163. return this._buf.getLastChar();
  164. }
  165. endsWithCharAndNewline() {
  166. return this._buf.endsWithCharAndNewline();
  167. }
  168. removeTrailingNewline() {
  169. this._buf.removeTrailingNewline();
  170. }
  171. exactSource(loc, cb) {
  172. if (!loc) {
  173. cb();
  174. return;
  175. }
  176. this._catchUp("start", loc);
  177. this._buf.exactSource(loc, cb);
  178. }
  179. source(prop, loc) {
  180. if (!loc) return;
  181. this._catchUp(prop, loc);
  182. this._buf.source(prop, loc);
  183. }
  184. sourceWithOffset(prop, loc, columnOffset) {
  185. if (!loc) return;
  186. this._catchUp(prop, loc);
  187. this._buf.sourceWithOffset(prop, loc, columnOffset);
  188. }
  189. sourceIdentifierName(identifierName, pos) {
  190. if (!this._buf._canMarkIdName) return;
  191. const sourcePosition = this._buf._sourcePosition;
  192. sourcePosition.identifierNamePos = pos;
  193. sourcePosition.identifierName = identifierName;
  194. }
  195. _space() {
  196. this._queue(32);
  197. }
  198. _newline() {
  199. this._queue(10);
  200. }
  201. _append(str, maybeNewline) {
  202. this._maybeAddParen(str);
  203. this._maybeIndent(str.charCodeAt(0));
  204. this._buf.append(str, maybeNewline);
  205. this._endsWithWord = false;
  206. this._endsWithInteger = false;
  207. }
  208. _appendChar(char) {
  209. this._maybeAddParenChar(char);
  210. this._maybeIndent(char);
  211. this._buf.appendChar(char);
  212. this._endsWithWord = false;
  213. this._endsWithInteger = false;
  214. }
  215. _queue(char) {
  216. this._maybeAddParenChar(char);
  217. this._maybeIndent(char);
  218. this._buf.queue(char);
  219. this._endsWithWord = false;
  220. this._endsWithInteger = false;
  221. }
  222. _maybeIndent(firstChar) {
  223. if (this._indent && firstChar !== 10 && this.endsWith(10)) {
  224. this._buf.queueIndentation(this._getIndent());
  225. }
  226. }
  227. _shouldIndent(firstChar) {
  228. if (this._indent && firstChar !== 10 && this.endsWith(10)) {
  229. return true;
  230. }
  231. }
  232. _maybeAddParenChar(char) {
  233. const parenPushNewlineState = this._parenPushNewlineState;
  234. if (!parenPushNewlineState) return;
  235. if (char === 32) {
  236. return;
  237. }
  238. if (char !== 10) {
  239. this._parenPushNewlineState = null;
  240. return;
  241. }
  242. this.tokenChar(40);
  243. this.indent();
  244. parenPushNewlineState.printed = true;
  245. }
  246. _maybeAddParen(str) {
  247. const parenPushNewlineState = this._parenPushNewlineState;
  248. if (!parenPushNewlineState) return;
  249. const len = str.length;
  250. let i;
  251. for (i = 0; i < len && str.charCodeAt(i) === 32; i++) continue;
  252. if (i === len) {
  253. return;
  254. }
  255. const cha = str.charCodeAt(i);
  256. if (cha !== 10) {
  257. if (cha !== 47 || i + 1 === len) {
  258. this._parenPushNewlineState = null;
  259. return;
  260. }
  261. const chaPost = str.charCodeAt(i + 1);
  262. if (chaPost === 42) {
  263. return;
  264. } else if (chaPost !== 47) {
  265. this._parenPushNewlineState = null;
  266. return;
  267. }
  268. }
  269. this.tokenChar(40);
  270. this.indent();
  271. parenPushNewlineState.printed = true;
  272. }
  273. catchUp(line) {
  274. if (!this.format.retainLines) return;
  275. const count = line - this._buf.getCurrentLine();
  276. for (let i = 0; i < count; i++) {
  277. this._newline();
  278. }
  279. }
  280. _catchUp(prop, loc) {
  281. var _loc$prop;
  282. if (!this.format.retainLines) return;
  283. const line = loc == null || (_loc$prop = loc[prop]) == null ? void 0 : _loc$prop.line;
  284. if (line != null) {
  285. const count = line - this._buf.getCurrentLine();
  286. for (let i = 0; i < count; i++) {
  287. this._newline();
  288. }
  289. }
  290. }
  291. _getIndent() {
  292. return this._indentRepeat * this._indent;
  293. }
  294. printTerminatorless(node, parent, isLabel) {
  295. if (isLabel) {
  296. this._noLineTerminator = true;
  297. this.print(node, parent);
  298. } else {
  299. const terminatorState = {
  300. printed: false
  301. };
  302. this._parenPushNewlineState = terminatorState;
  303. this.print(node, parent);
  304. if (terminatorState.printed) {
  305. this.dedent();
  306. this.newline();
  307. this.tokenChar(41);
  308. }
  309. }
  310. }
  311. print(node, parent, noLineTerminatorAfter, trailingCommentsLineOffset, forceParens) {
  312. var _node$extra, _node$leadingComments;
  313. if (!node) return;
  314. this._endsWithInnerRaw = false;
  315. const nodeType = node.type;
  316. const format = this.format;
  317. const oldConcise = format.concise;
  318. if (node._compact) {
  319. format.concise = true;
  320. }
  321. const printMethod = this[nodeType];
  322. if (printMethod === undefined) {
  323. throw new ReferenceError(`unknown node of type ${JSON.stringify(nodeType)} with constructor ${JSON.stringify(node.constructor.name)}`);
  324. }
  325. const oldNode = this._currentNode;
  326. this._currentNode = node;
  327. const oldInAux = this._insideAux;
  328. this._insideAux = node.loc == null;
  329. this._maybeAddAuxComment(this._insideAux && !oldInAux);
  330. const parenthesized = (_node$extra = node.extra) == null ? void 0 : _node$extra.parenthesized;
  331. let shouldPrintParens = forceParens || parenthesized && format.retainFunctionParens && nodeType === "FunctionExpression" || needsParens(node, parent, this.tokenContext, this.inForStatementInit);
  332. if (!shouldPrintParens && parenthesized && (_node$leadingComments = node.leadingComments) != null && _node$leadingComments.length && node.leadingComments[0].type === "CommentBlock") {
  333. const parentType = parent == null ? void 0 : parent.type;
  334. switch (parentType) {
  335. case "ExpressionStatement":
  336. case "VariableDeclarator":
  337. case "AssignmentExpression":
  338. case "ReturnStatement":
  339. break;
  340. case "CallExpression":
  341. case "OptionalCallExpression":
  342. case "NewExpression":
  343. if (parent.callee !== node) break;
  344. default:
  345. shouldPrintParens = true;
  346. }
  347. }
  348. let exitInForStatementInit;
  349. if (shouldPrintParens) {
  350. this.tokenChar(40);
  351. this._endsWithInnerRaw = false;
  352. exitInForStatementInit = this.enterForStatementInit(false);
  353. }
  354. this._lastCommentLine = 0;
  355. this._printLeadingComments(node, parent);
  356. const loc = nodeType === "Program" || nodeType === "File" ? null : node.loc;
  357. this.exactSource(loc, printMethod.bind(this, node, parent));
  358. if (shouldPrintParens) {
  359. this._printTrailingComments(node, parent);
  360. this.tokenChar(41);
  361. this._noLineTerminator = noLineTerminatorAfter;
  362. exitInForStatementInit();
  363. } else if (noLineTerminatorAfter && !this._noLineTerminator) {
  364. this._noLineTerminator = true;
  365. this._printTrailingComments(node, parent);
  366. } else {
  367. this._printTrailingComments(node, parent, trailingCommentsLineOffset);
  368. }
  369. this._currentNode = oldNode;
  370. format.concise = oldConcise;
  371. this._insideAux = oldInAux;
  372. this._endsWithInnerRaw = false;
  373. }
  374. _maybeAddAuxComment(enteredPositionlessNode) {
  375. if (enteredPositionlessNode) this._printAuxBeforeComment();
  376. if (!this._insideAux) this._printAuxAfterComment();
  377. }
  378. _printAuxBeforeComment() {
  379. if (this._printAuxAfterOnNextUserNode) return;
  380. this._printAuxAfterOnNextUserNode = true;
  381. const comment = this.format.auxiliaryCommentBefore;
  382. if (comment) {
  383. this._printComment({
  384. type: "CommentBlock",
  385. value: comment
  386. }, 0);
  387. }
  388. }
  389. _printAuxAfterComment() {
  390. if (!this._printAuxAfterOnNextUserNode) return;
  391. this._printAuxAfterOnNextUserNode = false;
  392. const comment = this.format.auxiliaryCommentAfter;
  393. if (comment) {
  394. this._printComment({
  395. type: "CommentBlock",
  396. value: comment
  397. }, 0);
  398. }
  399. }
  400. getPossibleRaw(node) {
  401. const extra = node.extra;
  402. if ((extra == null ? void 0 : extra.raw) != null && extra.rawValue != null && node.value === extra.rawValue) {
  403. return extra.raw;
  404. }
  405. }
  406. printJoin(nodes, parent, opts = {}) {
  407. if (!(nodes != null && nodes.length)) return;
  408. let {
  409. indent
  410. } = opts;
  411. if (indent == null && this.format.retainLines) {
  412. var _nodes$0$loc;
  413. const startLine = (_nodes$0$loc = nodes[0].loc) == null ? void 0 : _nodes$0$loc.start.line;
  414. if (startLine != null && startLine !== this._buf.getCurrentLine()) {
  415. indent = true;
  416. }
  417. }
  418. if (indent) this.indent();
  419. const newlineOpts = {
  420. addNewlines: opts.addNewlines,
  421. nextNodeStartLine: 0
  422. };
  423. const separator = opts.separator ? opts.separator.bind(this) : null;
  424. const len = nodes.length;
  425. for (let i = 0; i < len; i++) {
  426. const node = nodes[i];
  427. if (!node) continue;
  428. if (opts.statement) this._printNewline(i === 0, newlineOpts);
  429. this.print(node, parent, undefined, opts.trailingCommentsLineOffset || 0);
  430. opts.iterator == null || opts.iterator(node, i);
  431. if (i < len - 1) separator == null || separator();
  432. if (opts.statement) {
  433. var _node$trailingComment;
  434. if (!((_node$trailingComment = node.trailingComments) != null && _node$trailingComment.length)) {
  435. this._lastCommentLine = 0;
  436. }
  437. if (i + 1 === len) {
  438. this.newline(1);
  439. } else {
  440. var _nextNode$loc;
  441. const nextNode = nodes[i + 1];
  442. newlineOpts.nextNodeStartLine = ((_nextNode$loc = nextNode.loc) == null ? void 0 : _nextNode$loc.start.line) || 0;
  443. this._printNewline(true, newlineOpts);
  444. }
  445. }
  446. }
  447. if (indent) this.dedent();
  448. }
  449. printAndIndentOnComments(node, parent) {
  450. const indent = node.leadingComments && node.leadingComments.length > 0;
  451. if (indent) this.indent();
  452. this.print(node, parent);
  453. if (indent) this.dedent();
  454. }
  455. printBlock(parent) {
  456. const node = parent.body;
  457. if (node.type !== "EmptyStatement") {
  458. this.space();
  459. }
  460. this.print(node, parent);
  461. }
  462. _printTrailingComments(node, parent, lineOffset) {
  463. const {
  464. innerComments,
  465. trailingComments
  466. } = node;
  467. if (innerComments != null && innerComments.length) {
  468. this._printComments(2, innerComments, node, parent, lineOffset);
  469. }
  470. if (trailingComments != null && trailingComments.length) {
  471. this._printComments(2, trailingComments, node, parent, lineOffset);
  472. }
  473. }
  474. _printLeadingComments(node, parent) {
  475. const comments = node.leadingComments;
  476. if (!(comments != null && comments.length)) return;
  477. this._printComments(0, comments, node, parent);
  478. }
  479. _maybePrintInnerComments() {
  480. if (this._endsWithInnerRaw) this.printInnerComments();
  481. this._endsWithInnerRaw = true;
  482. this._indentInnerComments = true;
  483. }
  484. printInnerComments() {
  485. const node = this._currentNode;
  486. const comments = node.innerComments;
  487. if (!(comments != null && comments.length)) return;
  488. const hasSpace = this.endsWith(32);
  489. const indent = this._indentInnerComments;
  490. const printedCommentsCount = this._printedComments.size;
  491. if (indent) this.indent();
  492. this._printComments(1, comments, node);
  493. if (hasSpace && printedCommentsCount !== this._printedComments.size) {
  494. this.space();
  495. }
  496. if (indent) this.dedent();
  497. }
  498. noIndentInnerCommentsHere() {
  499. this._indentInnerComments = false;
  500. }
  501. printSequence(nodes, parent, opts = {}) {
  502. var _opts$indent;
  503. opts.statement = true;
  504. (_opts$indent = opts.indent) != null ? _opts$indent : opts.indent = false;
  505. this.printJoin(nodes, parent, opts);
  506. }
  507. printList(items, parent, opts = {}) {
  508. if (opts.separator == null) {
  509. opts.separator = commaSeparator;
  510. }
  511. this.printJoin(items, parent, opts);
  512. }
  513. _printNewline(newLine, opts) {
  514. const format = this.format;
  515. if (format.retainLines || format.compact) return;
  516. if (format.concise) {
  517. this.space();
  518. return;
  519. }
  520. if (!newLine) {
  521. return;
  522. }
  523. const startLine = opts.nextNodeStartLine;
  524. const lastCommentLine = this._lastCommentLine;
  525. if (startLine > 0 && lastCommentLine > 0) {
  526. const offset = startLine - lastCommentLine;
  527. if (offset >= 0) {
  528. this.newline(offset || 1);
  529. return;
  530. }
  531. }
  532. if (this._buf.hasContent()) {
  533. this.newline(1);
  534. }
  535. }
  536. _shouldPrintComment(comment) {
  537. if (comment.ignore) return 0;
  538. if (this._printedComments.has(comment)) return 0;
  539. if (this._noLineTerminator && HAS_NEWLINE_OR_BlOCK_COMMENT_END.test(comment.value)) {
  540. return 2;
  541. }
  542. this._printedComments.add(comment);
  543. if (!this.format.shouldPrintComment(comment.value)) {
  544. return 0;
  545. }
  546. return 1;
  547. }
  548. _printComment(comment, skipNewLines) {
  549. const noLineTerminator = this._noLineTerminator;
  550. const isBlockComment = comment.type === "CommentBlock";
  551. const printNewLines = isBlockComment && skipNewLines !== 1 && !this._noLineTerminator;
  552. if (printNewLines && this._buf.hasContent() && skipNewLines !== 2) {
  553. this.newline(1);
  554. }
  555. const lastCharCode = this.getLastChar();
  556. if (lastCharCode !== 91 && lastCharCode !== 123) {
  557. this.space();
  558. }
  559. let val;
  560. if (isBlockComment) {
  561. const {
  562. _parenPushNewlineState
  563. } = this;
  564. if ((_parenPushNewlineState == null ? void 0 : _parenPushNewlineState.printed) === false && HAS_NEWLINE.test(comment.value)) {
  565. this.tokenChar(40);
  566. this.indent();
  567. _parenPushNewlineState.printed = true;
  568. }
  569. val = `/*${comment.value}*/`;
  570. if (this.format.indent.adjustMultilineComment) {
  571. var _comment$loc;
  572. const offset = (_comment$loc = comment.loc) == null ? void 0 : _comment$loc.start.column;
  573. if (offset) {
  574. const newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
  575. val = val.replace(newlineRegex, "\n");
  576. }
  577. if (this.format.concise) {
  578. val = val.replace(/\n(?!$)/g, `\n`);
  579. } else {
  580. let indentSize = this.format.retainLines ? 0 : this._buf.getCurrentColumn();
  581. if (this._shouldIndent(47) || this.format.retainLines) {
  582. indentSize += this._getIndent();
  583. }
  584. val = val.replace(/\n(?!$)/g, `\n${" ".repeat(indentSize)}`);
  585. }
  586. }
  587. } else if (!noLineTerminator) {
  588. val = `//${comment.value}`;
  589. } else {
  590. val = `/*${comment.value}*/`;
  591. }
  592. if (this.endsWith(47)) this._space();
  593. this.source("start", comment.loc);
  594. this._append(val, isBlockComment);
  595. if (!isBlockComment && !noLineTerminator) {
  596. this.newline(1, true);
  597. }
  598. if (printNewLines && skipNewLines !== 3) {
  599. this.newline(1);
  600. }
  601. }
  602. _printComments(type, comments, node, parent, lineOffset = 0) {
  603. const nodeLoc = node.loc;
  604. const len = comments.length;
  605. let hasLoc = !!nodeLoc;
  606. const nodeStartLine = hasLoc ? nodeLoc.start.line : 0;
  607. const nodeEndLine = hasLoc ? nodeLoc.end.line : 0;
  608. let lastLine = 0;
  609. let leadingCommentNewline = 0;
  610. const maybeNewline = this._noLineTerminator ? function () {} : this.newline.bind(this);
  611. for (let i = 0; i < len; i++) {
  612. const comment = comments[i];
  613. const shouldPrint = this._shouldPrintComment(comment);
  614. if (shouldPrint === 2) {
  615. hasLoc = false;
  616. break;
  617. }
  618. if (hasLoc && comment.loc && shouldPrint === 1) {
  619. const commentStartLine = comment.loc.start.line;
  620. const commentEndLine = comment.loc.end.line;
  621. if (type === 0) {
  622. let offset = 0;
  623. if (i === 0) {
  624. if (this._buf.hasContent() && (comment.type === "CommentLine" || commentStartLine !== commentEndLine)) {
  625. offset = leadingCommentNewline = 1;
  626. }
  627. } else {
  628. offset = commentStartLine - lastLine;
  629. }
  630. lastLine = commentEndLine;
  631. maybeNewline(offset);
  632. this._printComment(comment, 1);
  633. if (i + 1 === len) {
  634. maybeNewline(Math.max(nodeStartLine - lastLine, leadingCommentNewline));
  635. lastLine = nodeStartLine;
  636. }
  637. } else if (type === 1) {
  638. const offset = commentStartLine - (i === 0 ? nodeStartLine : lastLine);
  639. lastLine = commentEndLine;
  640. maybeNewline(offset);
  641. this._printComment(comment, 1);
  642. if (i + 1 === len) {
  643. maybeNewline(Math.min(1, nodeEndLine - lastLine));
  644. lastLine = nodeEndLine;
  645. }
  646. } else {
  647. const offset = commentStartLine - (i === 0 ? nodeEndLine - lineOffset : lastLine);
  648. lastLine = commentEndLine;
  649. maybeNewline(offset);
  650. this._printComment(comment, 1);
  651. }
  652. } else {
  653. hasLoc = false;
  654. if (shouldPrint !== 1) {
  655. continue;
  656. }
  657. if (len === 1) {
  658. const singleLine = comment.loc ? comment.loc.start.line === comment.loc.end.line : !HAS_NEWLINE.test(comment.value);
  659. const shouldSkipNewline = singleLine && !isStatement(node) && !isClassBody(parent) && !isTSInterfaceBody(parent) && !isTSEnumDeclaration(parent);
  660. if (type === 0) {
  661. this._printComment(comment, shouldSkipNewline && node.type !== "ObjectExpression" || singleLine && isFunction(parent, {
  662. body: node
  663. }) ? 1 : 0);
  664. } else if (shouldSkipNewline && type === 2) {
  665. this._printComment(comment, 1);
  666. } else {
  667. this._printComment(comment, 0);
  668. }
  669. } else if (type === 1 && !(node.type === "ObjectExpression" && node.properties.length > 1) && node.type !== "ClassBody" && node.type !== "TSInterfaceBody") {
  670. this._printComment(comment, i === 0 ? 2 : i === len - 1 ? 3 : 0);
  671. } else {
  672. this._printComment(comment, 0);
  673. }
  674. }
  675. }
  676. if (type === 2 && hasLoc && lastLine) {
  677. this._lastCommentLine = lastLine;
  678. }
  679. }
  680. }
  681. Object.assign(Printer.prototype, generatorFunctions);
  682. {
  683. Printer.prototype.Noop = function Noop() {};
  684. }
  685. var _default = exports.default = Printer;
  686. function commaSeparator() {
  687. this.tokenChar(44);
  688. this.space();
  689. }
  690. //# sourceMappingURL=printer.js.map