printer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. isProgram,
  12. isFile,
  13. isEmptyStatement
  14. } = _t;
  15. const SCIENTIFIC_NOTATION = /e/i;
  16. const ZERO_DECIMAL_INTEGER = /\.0+$/;
  17. const NON_DECIMAL_LITERAL = /^0[box]/;
  18. const PURE_ANNOTATION_RE = /^\s*[@#]__PURE__\s*$/;
  19. const {
  20. needsParens,
  21. needsWhitespaceAfter,
  22. needsWhitespaceBefore
  23. } = n;
  24. class Printer {
  25. constructor(format, map) {
  26. this.inForStatementInitCounter = 0;
  27. this._printStack = [];
  28. this._indent = 0;
  29. this._insideAux = false;
  30. this._parenPushNewlineState = null;
  31. this._noLineTerminator = false;
  32. this._printAuxAfterOnNextUserNode = false;
  33. this._printedComments = new WeakSet();
  34. this._endsWithInteger = false;
  35. this._endsWithWord = false;
  36. this.format = format;
  37. this._buf = new _buffer.default(map);
  38. }
  39. generate(ast) {
  40. this.print(ast);
  41. this._maybeAddAuxComment();
  42. return this._buf.get();
  43. }
  44. indent() {
  45. if (this.format.compact || this.format.concise) return;
  46. this._indent++;
  47. }
  48. dedent() {
  49. if (this.format.compact || this.format.concise) return;
  50. this._indent--;
  51. }
  52. semicolon(force = false) {
  53. this._maybeAddAuxComment();
  54. this._append(";", !force);
  55. }
  56. rightBrace() {
  57. if (this.format.minified) {
  58. this._buf.removeLastSemicolon();
  59. }
  60. this.token("}");
  61. }
  62. space(force = false) {
  63. if (this.format.compact) return;
  64. if (force) {
  65. this._space();
  66. } else if (this._buf.hasContent()) {
  67. const lastCp = this.getLastChar();
  68. if (lastCp !== 32 && lastCp !== 10) {
  69. this._space();
  70. }
  71. }
  72. }
  73. word(str) {
  74. if (this._endsWithWord || this.endsWith(47) && str.charCodeAt(0) === 47) {
  75. this._space();
  76. }
  77. this._maybeAddAuxComment();
  78. this._append(str);
  79. this._endsWithWord = true;
  80. }
  81. number(str) {
  82. this.word(str);
  83. 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;
  84. }
  85. token(str) {
  86. const lastChar = this.getLastChar();
  87. const strFirst = str.charCodeAt(0);
  88. if (str === "--" && lastChar === 33 || strFirst === 43 && lastChar === 43 || strFirst === 45 && lastChar === 45 || strFirst === 46 && this._endsWithInteger) {
  89. this._space();
  90. }
  91. this._maybeAddAuxComment();
  92. this._append(str);
  93. }
  94. newline(i = 1) {
  95. if (this.format.retainLines || this.format.compact) return;
  96. if (this.format.concise) {
  97. this.space();
  98. return;
  99. }
  100. const charBeforeNewline = this.endsWithCharAndNewline();
  101. if (charBeforeNewline === 10) return;
  102. if (charBeforeNewline === 123 || charBeforeNewline === 58) {
  103. i--;
  104. }
  105. if (i <= 0) return;
  106. for (let j = 0; j < i; j++) {
  107. this._newline();
  108. }
  109. }
  110. endsWith(char) {
  111. return this.getLastChar() === char;
  112. }
  113. getLastChar() {
  114. return this._buf.getLastChar();
  115. }
  116. endsWithCharAndNewline() {
  117. return this._buf.endsWithCharAndNewline();
  118. }
  119. removeTrailingNewline() {
  120. this._buf.removeTrailingNewline();
  121. }
  122. exactSource(loc, cb) {
  123. this._catchUp("start", loc);
  124. this._buf.exactSource(loc, cb);
  125. }
  126. source(prop, loc) {
  127. this._catchUp(prop, loc);
  128. this._buf.source(prop, loc);
  129. }
  130. withSource(prop, loc, cb) {
  131. this._catchUp(prop, loc);
  132. this._buf.withSource(prop, loc, cb);
  133. }
  134. _space() {
  135. this._append(" ", true);
  136. }
  137. _newline() {
  138. this._append("\n", true);
  139. }
  140. _append(str, queue = false) {
  141. this._maybeAddParen(str);
  142. this._maybeIndent(str);
  143. if (queue) this._buf.queue(str);else this._buf.append(str);
  144. this._endsWithWord = false;
  145. this._endsWithInteger = false;
  146. }
  147. _maybeIndent(str) {
  148. if (this._indent && this.endsWith(10) && str.charCodeAt(0) !== 10) {
  149. this._buf.queue(this._getIndent());
  150. }
  151. }
  152. _maybeAddParen(str) {
  153. const parenPushNewlineState = this._parenPushNewlineState;
  154. if (!parenPushNewlineState) return;
  155. let i;
  156. for (i = 0; i < str.length && str[i] === " "; i++) continue;
  157. if (i === str.length) {
  158. return;
  159. }
  160. const cha = str[i];
  161. if (cha !== "\n") {
  162. if (cha !== "/" || i + 1 === str.length) {
  163. this._parenPushNewlineState = null;
  164. return;
  165. }
  166. const chaPost = str[i + 1];
  167. if (chaPost === "*") {
  168. if (PURE_ANNOTATION_RE.test(str.slice(i + 2, str.length - 2))) {
  169. return;
  170. }
  171. } else if (chaPost !== "/") {
  172. this._parenPushNewlineState = null;
  173. return;
  174. }
  175. }
  176. this.token("(");
  177. this.indent();
  178. parenPushNewlineState.printed = true;
  179. }
  180. _catchUp(prop, loc) {
  181. if (!this.format.retainLines) return;
  182. const pos = loc ? loc[prop] : null;
  183. if ((pos == null ? void 0 : pos.line) != null) {
  184. const count = pos.line - this._buf.getCurrentLine();
  185. for (let i = 0; i < count; i++) {
  186. this._newline();
  187. }
  188. }
  189. }
  190. _getIndent() {
  191. return this.format.indent.style.repeat(this._indent);
  192. }
  193. startTerminatorless(isLabel = false) {
  194. if (isLabel) {
  195. this._noLineTerminator = true;
  196. return null;
  197. } else {
  198. return this._parenPushNewlineState = {
  199. printed: false
  200. };
  201. }
  202. }
  203. endTerminatorless(state) {
  204. this._noLineTerminator = false;
  205. if (state != null && state.printed) {
  206. this.dedent();
  207. this.newline();
  208. this.token(")");
  209. }
  210. }
  211. print(node, parent) {
  212. if (!node) return;
  213. const oldConcise = this.format.concise;
  214. if (node._compact) {
  215. this.format.concise = true;
  216. }
  217. const printMethod = this[node.type];
  218. if (!printMethod) {
  219. throw new ReferenceError(`unknown node of type ${JSON.stringify(node.type)} with constructor ${JSON.stringify(node == null ? void 0 : node.constructor.name)}`);
  220. }
  221. this._printStack.push(node);
  222. const oldInAux = this._insideAux;
  223. this._insideAux = !node.loc;
  224. this._maybeAddAuxComment(this._insideAux && !oldInAux);
  225. let shouldPrintParens = needsParens(node, parent, this._printStack);
  226. if (this.format.retainFunctionParens && node.type === "FunctionExpression" && node.extra && node.extra.parenthesized) {
  227. shouldPrintParens = true;
  228. }
  229. if (shouldPrintParens) this.token("(");
  230. this._printLeadingComments(node);
  231. const loc = isProgram(node) || isFile(node) ? null : node.loc;
  232. this.withSource("start", loc, () => {
  233. printMethod.call(this, node, parent);
  234. });
  235. this._printTrailingComments(node);
  236. if (shouldPrintParens) this.token(")");
  237. this._printStack.pop();
  238. this.format.concise = oldConcise;
  239. this._insideAux = oldInAux;
  240. }
  241. _maybeAddAuxComment(enteredPositionlessNode) {
  242. if (enteredPositionlessNode) this._printAuxBeforeComment();
  243. if (!this._insideAux) this._printAuxAfterComment();
  244. }
  245. _printAuxBeforeComment() {
  246. if (this._printAuxAfterOnNextUserNode) return;
  247. this._printAuxAfterOnNextUserNode = true;
  248. const comment = this.format.auxiliaryCommentBefore;
  249. if (comment) {
  250. this._printComment({
  251. type: "CommentBlock",
  252. value: comment
  253. });
  254. }
  255. }
  256. _printAuxAfterComment() {
  257. if (!this._printAuxAfterOnNextUserNode) return;
  258. this._printAuxAfterOnNextUserNode = false;
  259. const comment = this.format.auxiliaryCommentAfter;
  260. if (comment) {
  261. this._printComment({
  262. type: "CommentBlock",
  263. value: comment
  264. });
  265. }
  266. }
  267. getPossibleRaw(node) {
  268. const extra = node.extra;
  269. if (extra && extra.raw != null && extra.rawValue != null && node.value === extra.rawValue) {
  270. return extra.raw;
  271. }
  272. }
  273. printJoin(nodes, parent, opts = {}) {
  274. if (!(nodes != null && nodes.length)) return;
  275. if (opts.indent) this.indent();
  276. const newlineOpts = {
  277. addNewlines: opts.addNewlines
  278. };
  279. for (let i = 0; i < nodes.length; i++) {
  280. const node = nodes[i];
  281. if (!node) continue;
  282. if (opts.statement) this._printNewline(true, node, parent, newlineOpts);
  283. this.print(node, parent);
  284. if (opts.iterator) {
  285. opts.iterator(node, i);
  286. }
  287. if (opts.separator && i < nodes.length - 1) {
  288. opts.separator.call(this);
  289. }
  290. if (opts.statement) this._printNewline(false, node, parent, newlineOpts);
  291. }
  292. if (opts.indent) this.dedent();
  293. }
  294. printAndIndentOnComments(node, parent) {
  295. const indent = node.leadingComments && node.leadingComments.length > 0;
  296. if (indent) this.indent();
  297. this.print(node, parent);
  298. if (indent) this.dedent();
  299. }
  300. printBlock(parent) {
  301. const node = parent.body;
  302. if (!isEmptyStatement(node)) {
  303. this.space();
  304. }
  305. this.print(node, parent);
  306. }
  307. _printTrailingComments(node) {
  308. this._printComments(this._getComments(false, node));
  309. }
  310. _printLeadingComments(node) {
  311. this._printComments(this._getComments(true, node), true);
  312. }
  313. printInnerComments(node, indent = true) {
  314. var _node$innerComments;
  315. if (!((_node$innerComments = node.innerComments) != null && _node$innerComments.length)) return;
  316. if (indent) this.indent();
  317. this._printComments(node.innerComments);
  318. if (indent) this.dedent();
  319. }
  320. printSequence(nodes, parent, opts = {}) {
  321. opts.statement = true;
  322. return this.printJoin(nodes, parent, opts);
  323. }
  324. printList(items, parent, opts = {}) {
  325. if (opts.separator == null) {
  326. opts.separator = commaSeparator;
  327. }
  328. return this.printJoin(items, parent, opts);
  329. }
  330. _printNewline(leading, node, parent, opts) {
  331. if (this.format.retainLines || this.format.compact) return;
  332. if (this.format.concise) {
  333. this.space();
  334. return;
  335. }
  336. let lines = 0;
  337. if (this._buf.hasContent()) {
  338. if (!leading) lines++;
  339. if (opts.addNewlines) lines += opts.addNewlines(leading, node) || 0;
  340. const needs = leading ? needsWhitespaceBefore : needsWhitespaceAfter;
  341. if (needs(node, parent)) lines++;
  342. }
  343. this.newline(Math.min(2, lines));
  344. }
  345. _getComments(leading, node) {
  346. return node && (leading ? node.leadingComments : node.trailingComments) || [];
  347. }
  348. _printComment(comment, skipNewLines) {
  349. if (!this.format.shouldPrintComment(comment.value)) return;
  350. if (comment.ignore) return;
  351. if (this._printedComments.has(comment)) return;
  352. this._printedComments.add(comment);
  353. const isBlockComment = comment.type === "CommentBlock";
  354. const printNewLines = isBlockComment && !skipNewLines && !this._noLineTerminator;
  355. if (printNewLines && this._buf.hasContent()) this.newline(1);
  356. const lastCharCode = this.getLastChar();
  357. if (lastCharCode !== 91 && lastCharCode !== 123) {
  358. this.space();
  359. }
  360. let val = !isBlockComment && !this._noLineTerminator ? `//${comment.value}\n` : `/*${comment.value}*/`;
  361. if (isBlockComment && this.format.indent.adjustMultilineComment) {
  362. var _comment$loc;
  363. const offset = (_comment$loc = comment.loc) == null ? void 0 : _comment$loc.start.column;
  364. if (offset) {
  365. const newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
  366. val = val.replace(newlineRegex, "\n");
  367. }
  368. const indentSize = Math.max(this._getIndent().length, this.format.retainLines ? 0 : this._buf.getCurrentColumn());
  369. val = val.replace(/\n(?!$)/g, `\n${" ".repeat(indentSize)}`);
  370. }
  371. if (this.endsWith(47)) this._space();
  372. this.withSource("start", comment.loc, () => {
  373. this._append(val);
  374. });
  375. if (printNewLines) this.newline(1);
  376. }
  377. _printComments(comments, inlinePureAnnotation) {
  378. if (!(comments != null && comments.length)) return;
  379. if (inlinePureAnnotation && comments.length === 1 && PURE_ANNOTATION_RE.test(comments[0].value)) {
  380. this._printComment(comments[0], this._buf.hasContent() && !this.endsWith(10));
  381. } else {
  382. for (const comment of comments) {
  383. this._printComment(comment);
  384. }
  385. }
  386. }
  387. printAssertions(node) {
  388. var _node$assertions;
  389. if ((_node$assertions = node.assertions) != null && _node$assertions.length) {
  390. this.space();
  391. this.word("assert");
  392. this.space();
  393. this.token("{");
  394. this.space();
  395. this.printList(node.assertions, node);
  396. this.space();
  397. this.token("}");
  398. }
  399. }
  400. }
  401. Object.assign(Printer.prototype, generatorFunctions);
  402. {
  403. Printer.prototype.Noop = function Noop() {};
  404. }
  405. var _default = Printer;
  406. exports.default = _default;
  407. function commaSeparator() {
  408. this.token(",");
  409. this.space();
  410. }