web 3d图形渲染器
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.

516 lines
13 KiB

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