printer.js 21 KB

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