printer.js 22 KB

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