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.

291 lines
7.4 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.UnaryExpression = UnaryExpression;
  6. exports.DoExpression = DoExpression;
  7. exports.ParenthesizedExpression = ParenthesizedExpression;
  8. exports.UpdateExpression = UpdateExpression;
  9. exports.ConditionalExpression = ConditionalExpression;
  10. exports.NewExpression = NewExpression;
  11. exports.SequenceExpression = SequenceExpression;
  12. exports.ThisExpression = ThisExpression;
  13. exports.Super = Super;
  14. exports.Decorator = Decorator;
  15. exports.OptionalMemberExpression = OptionalMemberExpression;
  16. exports.OptionalCallExpression = OptionalCallExpression;
  17. exports.CallExpression = CallExpression;
  18. exports.Import = Import;
  19. exports.EmptyStatement = EmptyStatement;
  20. exports.ExpressionStatement = ExpressionStatement;
  21. exports.AssignmentPattern = AssignmentPattern;
  22. exports.LogicalExpression = exports.BinaryExpression = exports.AssignmentExpression = AssignmentExpression;
  23. exports.BindExpression = BindExpression;
  24. exports.MemberExpression = MemberExpression;
  25. exports.MetaProperty = MetaProperty;
  26. exports.PrivateName = PrivateName;
  27. exports.V8IntrinsicIdentifier = V8IntrinsicIdentifier;
  28. exports.AwaitExpression = exports.YieldExpression = void 0;
  29. var t = _interopRequireWildcard(require("@babel/types"));
  30. var n = _interopRequireWildcard(require("../node"));
  31. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  32. 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; }
  33. function UnaryExpression(node) {
  34. if (node.operator === "void" || node.operator === "delete" || node.operator === "typeof" || node.operator === "throw") {
  35. this.word(node.operator);
  36. this.space();
  37. } else {
  38. this.token(node.operator);
  39. }
  40. this.print(node.argument, node);
  41. }
  42. function DoExpression(node) {
  43. this.word("do");
  44. this.space();
  45. this.print(node.body, node);
  46. }
  47. function ParenthesizedExpression(node) {
  48. this.token("(");
  49. this.print(node.expression, node);
  50. this.token(")");
  51. }
  52. function UpdateExpression(node) {
  53. if (node.prefix) {
  54. this.token(node.operator);
  55. this.print(node.argument, node);
  56. } else {
  57. this.startTerminatorless(true);
  58. this.print(node.argument, node);
  59. this.endTerminatorless();
  60. this.token(node.operator);
  61. }
  62. }
  63. function ConditionalExpression(node) {
  64. this.print(node.test, node);
  65. this.space();
  66. this.token("?");
  67. this.space();
  68. this.print(node.consequent, node);
  69. this.space();
  70. this.token(":");
  71. this.space();
  72. this.print(node.alternate, node);
  73. }
  74. function NewExpression(node, parent) {
  75. this.word("new");
  76. this.space();
  77. this.print(node.callee, node);
  78. if (this.format.minified && node.arguments.length === 0 && !node.optional && !t.isCallExpression(parent, {
  79. callee: node
  80. }) && !t.isMemberExpression(parent) && !t.isNewExpression(parent)) {
  81. return;
  82. }
  83. this.print(node.typeArguments, node);
  84. this.print(node.typeParameters, node);
  85. if (node.optional) {
  86. this.token("?.");
  87. }
  88. this.token("(");
  89. this.printList(node.arguments, node);
  90. this.token(")");
  91. }
  92. function SequenceExpression(node) {
  93. this.printList(node.expressions, node);
  94. }
  95. function ThisExpression() {
  96. this.word("this");
  97. }
  98. function Super() {
  99. this.word("super");
  100. }
  101. function Decorator(node) {
  102. this.token("@");
  103. this.print(node.expression, node);
  104. this.newline();
  105. }
  106. function OptionalMemberExpression(node) {
  107. this.print(node.object, node);
  108. if (!node.computed && t.isMemberExpression(node.property)) {
  109. throw new TypeError("Got a MemberExpression for MemberExpression property");
  110. }
  111. let computed = node.computed;
  112. if (t.isLiteral(node.property) && typeof node.property.value === "number") {
  113. computed = true;
  114. }
  115. if (node.optional) {
  116. this.token("?.");
  117. }
  118. if (computed) {
  119. this.token("[");
  120. this.print(node.property, node);
  121. this.token("]");
  122. } else {
  123. if (!node.optional) {
  124. this.token(".");
  125. }
  126. this.print(node.property, node);
  127. }
  128. }
  129. function OptionalCallExpression(node) {
  130. this.print(node.callee, node);
  131. this.print(node.typeArguments, node);
  132. this.print(node.typeParameters, node);
  133. if (node.optional) {
  134. this.token("?.");
  135. }
  136. this.token("(");
  137. this.printList(node.arguments, node);
  138. this.token(")");
  139. }
  140. function CallExpression(node) {
  141. this.print(node.callee, node);
  142. this.print(node.typeArguments, node);
  143. this.print(node.typeParameters, node);
  144. this.token("(");
  145. this.printList(node.arguments, node);
  146. this.token(")");
  147. }
  148. function Import() {
  149. this.word("import");
  150. }
  151. function buildYieldAwait(keyword) {
  152. return function (node) {
  153. this.word(keyword);
  154. if (node.delegate) {
  155. this.token("*");
  156. }
  157. if (node.argument) {
  158. this.space();
  159. const terminatorState = this.startTerminatorless();
  160. this.print(node.argument, node);
  161. this.endTerminatorless(terminatorState);
  162. }
  163. };
  164. }
  165. const YieldExpression = buildYieldAwait("yield");
  166. exports.YieldExpression = YieldExpression;
  167. const AwaitExpression = buildYieldAwait("await");
  168. exports.AwaitExpression = AwaitExpression;
  169. function EmptyStatement() {
  170. this.semicolon(true);
  171. }
  172. function ExpressionStatement(node) {
  173. this.print(node.expression, node);
  174. this.semicolon();
  175. }
  176. function AssignmentPattern(node) {
  177. this.print(node.left, node);
  178. if (node.left.optional) this.token("?");
  179. this.print(node.left.typeAnnotation, node);
  180. this.space();
  181. this.token("=");
  182. this.space();
  183. this.print(node.right, node);
  184. }
  185. function AssignmentExpression(node, parent) {
  186. const parens = this.inForStatementInitCounter && node.operator === "in" && !n.needsParens(node, parent);
  187. if (parens) {
  188. this.token("(");
  189. }
  190. this.print(node.left, node);
  191. this.space();
  192. if (node.operator === "in" || node.operator === "instanceof") {
  193. this.word(node.operator);
  194. } else {
  195. this.token(node.operator);
  196. }
  197. this.space();
  198. this.print(node.right, node);
  199. if (parens) {
  200. this.token(")");
  201. }
  202. }
  203. function BindExpression(node) {
  204. this.print(node.object, node);
  205. this.token("::");
  206. this.print(node.callee, node);
  207. }
  208. function MemberExpression(node) {
  209. this.print(node.object, node);
  210. if (!node.computed && t.isMemberExpression(node.property)) {
  211. throw new TypeError("Got a MemberExpression for MemberExpression property");
  212. }
  213. let computed = node.computed;
  214. if (t.isLiteral(node.property) && typeof node.property.value === "number") {
  215. computed = true;
  216. }
  217. if (computed) {
  218. this.token("[");
  219. this.print(node.property, node);
  220. this.token("]");
  221. } else {
  222. this.token(".");
  223. this.print(node.property, node);
  224. }
  225. }
  226. function MetaProperty(node) {
  227. this.print(node.meta, node);
  228. this.token(".");
  229. this.print(node.property, node);
  230. }
  231. function PrivateName(node) {
  232. this.token("#");
  233. this.print(node.id, node);
  234. }
  235. function V8IntrinsicIdentifier(node) {
  236. this.token("%");
  237. this.word(node.name);
  238. }