b7099baf5cae0517380c6341de240ebae51de3c5bbc8da51e50570c099d5b66b168c024c6725d22824c6d18cad2d0bd6a974d4ca950ce102621808f2c34a4c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ArgumentPlaceholder = ArgumentPlaceholder;
  6. exports.ArrayPattern = exports.ArrayExpression = ArrayExpression;
  7. exports.BigIntLiteral = BigIntLiteral;
  8. exports.BooleanLiteral = BooleanLiteral;
  9. exports.Identifier = Identifier;
  10. exports.NullLiteral = NullLiteral;
  11. exports.NumericLiteral = NumericLiteral;
  12. exports.ObjectPattern = exports.ObjectExpression = ObjectExpression;
  13. exports.ObjectMethod = ObjectMethod;
  14. exports.ObjectProperty = ObjectProperty;
  15. exports.PipelineBareFunction = PipelineBareFunction;
  16. exports.PipelinePrimaryTopicReference = PipelinePrimaryTopicReference;
  17. exports.PipelineTopicExpression = PipelineTopicExpression;
  18. exports.RecordExpression = RecordExpression;
  19. exports.RegExpLiteral = RegExpLiteral;
  20. exports.SpreadElement = exports.RestElement = RestElement;
  21. exports.StringLiteral = StringLiteral;
  22. exports.TopicReference = TopicReference;
  23. exports.TupleExpression = TupleExpression;
  24. exports._getRawIdentifier = _getRawIdentifier;
  25. var _t = require("@babel/types");
  26. var _jsesc = require("jsesc");
  27. const {
  28. isAssignmentPattern,
  29. isIdentifier
  30. } = _t;
  31. let lastRawIdentNode = null;
  32. let lastRawIdentResult = "";
  33. function _getRawIdentifier(node) {
  34. if (node === lastRawIdentNode) return lastRawIdentResult;
  35. lastRawIdentNode = node;
  36. const {
  37. name
  38. } = node;
  39. const token = this.tokenMap.find(node, tok => tok.value === name);
  40. if (token) {
  41. lastRawIdentResult = this._originalCode.slice(token.start, token.end);
  42. return lastRawIdentResult;
  43. }
  44. return lastRawIdentResult = node.name;
  45. }
  46. function Identifier(node) {
  47. var _node$loc;
  48. this.sourceIdentifierName(((_node$loc = node.loc) == null ? void 0 : _node$loc.identifierName) || node.name);
  49. this.word(this.tokenMap ? this._getRawIdentifier(node) : node.name);
  50. }
  51. function ArgumentPlaceholder() {
  52. this.tokenChar(63);
  53. }
  54. function RestElement(node) {
  55. this.token("...");
  56. this.print(node.argument);
  57. }
  58. function ObjectExpression(node) {
  59. const props = node.properties;
  60. this.tokenChar(123);
  61. if (props.length) {
  62. const exit = this.enterDelimited();
  63. this.space();
  64. this.printList(props, this.shouldPrintTrailingComma("}"), true, true);
  65. this.space();
  66. exit();
  67. }
  68. this.sourceWithOffset("end", node.loc, -1);
  69. this.tokenChar(125);
  70. }
  71. function ObjectMethod(node) {
  72. this.printJoin(node.decorators);
  73. this._methodHead(node);
  74. this.space();
  75. this.print(node.body);
  76. }
  77. function ObjectProperty(node) {
  78. this.printJoin(node.decorators);
  79. if (node.computed) {
  80. this.tokenChar(91);
  81. this.print(node.key);
  82. this.tokenChar(93);
  83. } else {
  84. if (isAssignmentPattern(node.value) && isIdentifier(node.key) && node.key.name === node.value.left.name) {
  85. this.print(node.value);
  86. return;
  87. }
  88. this.print(node.key);
  89. if (node.shorthand && isIdentifier(node.key) && isIdentifier(node.value) && node.key.name === node.value.name) {
  90. return;
  91. }
  92. }
  93. this.tokenChar(58);
  94. this.space();
  95. this.print(node.value);
  96. }
  97. function ArrayExpression(node) {
  98. const elems = node.elements;
  99. const len = elems.length;
  100. this.tokenChar(91);
  101. const exit = this.enterDelimited();
  102. for (let i = 0; i < elems.length; i++) {
  103. const elem = elems[i];
  104. if (elem) {
  105. if (i > 0) this.space();
  106. this.print(elem);
  107. if (i < len - 1 || this.shouldPrintTrailingComma("]")) {
  108. this.token(",", false, i);
  109. }
  110. } else {
  111. this.token(",", false, i);
  112. }
  113. }
  114. exit();
  115. this.tokenChar(93);
  116. }
  117. function RecordExpression(node) {
  118. const props = node.properties;
  119. let startToken;
  120. let endToken;
  121. {
  122. if (this.format.recordAndTupleSyntaxType === "bar") {
  123. startToken = "{|";
  124. endToken = "|}";
  125. } else if (this.format.recordAndTupleSyntaxType !== "hash" && this.format.recordAndTupleSyntaxType != null) {
  126. throw new Error(`The "recordAndTupleSyntaxType" generator option must be "bar" or "hash" (${JSON.stringify(this.format.recordAndTupleSyntaxType)} received).`);
  127. } else {
  128. startToken = "#{";
  129. endToken = "}";
  130. }
  131. }
  132. this.token(startToken);
  133. if (props.length) {
  134. this.space();
  135. this.printList(props, this.shouldPrintTrailingComma(endToken), true, true);
  136. this.space();
  137. }
  138. this.token(endToken);
  139. }
  140. function TupleExpression(node) {
  141. const elems = node.elements;
  142. const len = elems.length;
  143. let startToken;
  144. let endToken;
  145. {
  146. if (this.format.recordAndTupleSyntaxType === "bar") {
  147. startToken = "[|";
  148. endToken = "|]";
  149. } else if (this.format.recordAndTupleSyntaxType === "hash") {
  150. startToken = "#[";
  151. endToken = "]";
  152. } else {
  153. throw new Error(`${this.format.recordAndTupleSyntaxType} is not a valid recordAndTuple syntax type`);
  154. }
  155. }
  156. this.token(startToken);
  157. for (let i = 0; i < elems.length; i++) {
  158. const elem = elems[i];
  159. if (elem) {
  160. if (i > 0) this.space();
  161. this.print(elem);
  162. if (i < len - 1 || this.shouldPrintTrailingComma(endToken)) {
  163. this.token(",", false, i);
  164. }
  165. }
  166. }
  167. this.token(endToken);
  168. }
  169. function RegExpLiteral(node) {
  170. this.word(`/${node.pattern}/${node.flags}`);
  171. }
  172. function BooleanLiteral(node) {
  173. this.word(node.value ? "true" : "false");
  174. }
  175. function NullLiteral() {
  176. this.word("null");
  177. }
  178. function NumericLiteral(node) {
  179. const raw = this.getPossibleRaw(node);
  180. const opts = this.format.jsescOption;
  181. const value = node.value;
  182. const str = value + "";
  183. if (opts.numbers) {
  184. this.number(_jsesc(value, opts), value);
  185. } else if (raw == null) {
  186. this.number(str, value);
  187. } else if (this.format.minified) {
  188. this.number(raw.length < str.length ? raw : str, value);
  189. } else {
  190. this.number(raw, value);
  191. }
  192. }
  193. function StringLiteral(node) {
  194. const raw = this.getPossibleRaw(node);
  195. if (!this.format.minified && raw !== undefined) {
  196. this.token(raw);
  197. return;
  198. }
  199. const val = _jsesc(node.value, this.format.jsescOption);
  200. this.token(val);
  201. }
  202. function BigIntLiteral(node) {
  203. const raw = this.getPossibleRaw(node);
  204. if (!this.format.minified && raw !== undefined) {
  205. this.word(raw);
  206. return;
  207. }
  208. this.word(node.value + "n");
  209. }
  210. const validTopicTokenSet = new Set(["^^", "@@", "^", "%", "#"]);
  211. function TopicReference() {
  212. const {
  213. topicToken
  214. } = this.format;
  215. if (validTopicTokenSet.has(topicToken)) {
  216. this.token(topicToken);
  217. } else {
  218. const givenTopicTokenJSON = JSON.stringify(topicToken);
  219. const validTopics = Array.from(validTopicTokenSet, v => JSON.stringify(v));
  220. throw new Error(`The "topicToken" generator option must be one of ` + `${validTopics.join(", ")} (${givenTopicTokenJSON} received instead).`);
  221. }
  222. }
  223. function PipelineTopicExpression(node) {
  224. this.print(node.expression);
  225. }
  226. function PipelineBareFunction(node) {
  227. this.print(node.callee);
  228. }
  229. function PipelinePrimaryTopicReference() {
  230. this.tokenChar(35);
  231. }
  232. //# sourceMappingURL=types.js.map