printer.js 12 KB

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