printer.js 20 KB

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