expressions.js 7.7 KB

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