printer.js 13 KB

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