Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

printer.js 12KB

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