expressions.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 = AwaitExpression;
  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 = YieldExpression;
  31. exports._shouldPrintDecoratorsBeforeExport = _shouldPrintDecoratorsBeforeExport;
  32. var _t = require("@babel/types");
  33. var _index = require("../node/index.js");
  34. const {
  35. isCallExpression,
  36. isLiteral,
  37. isMemberExpression,
  38. isNewExpression,
  39. isPattern
  40. } = _t;
  41. function UnaryExpression(node) {
  42. const {
  43. operator
  44. } = node;
  45. if (operator === "void" || operator === "delete" || operator === "typeof" || operator === "throw") {
  46. this.word(operator);
  47. this.space();
  48. } else {
  49. this.token(operator);
  50. }
  51. this.print(node.argument);
  52. }
  53. function DoExpression(node) {
  54. if (node.async) {
  55. this.word("async", true);
  56. this.space();
  57. }
  58. this.word("do");
  59. this.space();
  60. this.print(node.body);
  61. }
  62. function ParenthesizedExpression(node) {
  63. this.tokenChar(40);
  64. const exit = this.enterDelimited();
  65. this.print(node.expression);
  66. exit();
  67. this.rightParens(node);
  68. }
  69. function UpdateExpression(node) {
  70. if (node.prefix) {
  71. this.token(node.operator);
  72. this.print(node.argument);
  73. } else {
  74. this.print(node.argument, true);
  75. this.token(node.operator);
  76. }
  77. }
  78. function ConditionalExpression(node) {
  79. this.print(node.test);
  80. this.space();
  81. this.tokenChar(63);
  82. this.space();
  83. this.print(node.consequent);
  84. this.space();
  85. this.tokenChar(58);
  86. this.space();
  87. this.print(node.alternate);
  88. }
  89. function NewExpression(node, parent) {
  90. this.word("new");
  91. this.space();
  92. this.print(node.callee);
  93. if (this.format.minified && node.arguments.length === 0 && !node.optional && !isCallExpression(parent, {
  94. callee: node
  95. }) && !isMemberExpression(parent) && !isNewExpression(parent)) {
  96. return;
  97. }
  98. this.print(node.typeArguments);
  99. this.print(node.typeParameters);
  100. if (node.optional) {
  101. this.token("?.");
  102. }
  103. if (node.arguments.length === 0 && this.tokenMap && !this.tokenMap.endMatches(node, ")")) {
  104. return;
  105. }
  106. this.tokenChar(40);
  107. const exit = this.enterDelimited();
  108. this.printList(node.arguments, {
  109. printTrailingSeparator: this.shouldPrintTrailingComma(")")
  110. });
  111. exit();
  112. this.rightParens(node);
  113. }
  114. function SequenceExpression(node) {
  115. this.printList(node.expressions);
  116. }
  117. function ThisExpression() {
  118. this.word("this");
  119. }
  120. function Super() {
  121. this.word("super");
  122. }
  123. function _shouldPrintDecoratorsBeforeExport(node) {
  124. if (typeof this.format.decoratorsBeforeExport === "boolean") {
  125. return this.format.decoratorsBeforeExport;
  126. }
  127. return typeof node.start === "number" && node.start === node.declaration.start;
  128. }
  129. function Decorator(node) {
  130. this.tokenChar(64);
  131. this.print(node.expression);
  132. this.newline();
  133. }
  134. function OptionalMemberExpression(node) {
  135. let {
  136. computed
  137. } = node;
  138. const {
  139. optional,
  140. property
  141. } = node;
  142. this.print(node.object);
  143. if (!computed && isMemberExpression(property)) {
  144. throw new TypeError("Got a MemberExpression for MemberExpression property");
  145. }
  146. if (isLiteral(property) && typeof property.value === "number") {
  147. computed = true;
  148. }
  149. if (optional) {
  150. this.token("?.");
  151. }
  152. if (computed) {
  153. this.tokenChar(91);
  154. this.print(property);
  155. this.tokenChar(93);
  156. } else {
  157. if (!optional) {
  158. this.tokenChar(46);
  159. }
  160. this.print(property);
  161. }
  162. }
  163. function OptionalCallExpression(node) {
  164. this.print(node.callee);
  165. this.print(node.typeParameters);
  166. if (node.optional) {
  167. this.token("?.");
  168. }
  169. this.print(node.typeArguments);
  170. this.tokenChar(40);
  171. const exit = this.enterDelimited();
  172. this.printList(node.arguments);
  173. exit();
  174. this.rightParens(node);
  175. }
  176. function CallExpression(node) {
  177. this.print(node.callee);
  178. this.print(node.typeArguments);
  179. this.print(node.typeParameters);
  180. this.tokenChar(40);
  181. const exit = this.enterDelimited();
  182. this.printList(node.arguments, {
  183. printTrailingSeparator: this.shouldPrintTrailingComma(")")
  184. });
  185. exit();
  186. this.rightParens(node);
  187. }
  188. function Import() {
  189. this.word("import");
  190. }
  191. function AwaitExpression(node) {
  192. this.word("await");
  193. if (node.argument) {
  194. this.space();
  195. this.printTerminatorless(node.argument);
  196. }
  197. }
  198. function YieldExpression(node) {
  199. this.word("yield", true);
  200. if (node.delegate) {
  201. this.tokenChar(42);
  202. if (node.argument) {
  203. this.space();
  204. this.print(node.argument);
  205. }
  206. } else {
  207. if (node.argument) {
  208. this.space();
  209. this.printTerminatorless(node.argument);
  210. }
  211. }
  212. }
  213. function EmptyStatement() {
  214. this.semicolon(true);
  215. }
  216. function ExpressionStatement(node) {
  217. this.tokenContext |= _index.TokenContext.expressionStatement;
  218. this.print(node.expression);
  219. this.semicolon();
  220. }
  221. function AssignmentPattern(node) {
  222. this.print(node.left);
  223. if (node.left.type === "Identifier" || isPattern(node.left)) {
  224. if (node.left.optional) this.tokenChar(63);
  225. this.print(node.left.typeAnnotation);
  226. }
  227. this.space();
  228. this.tokenChar(61);
  229. this.space();
  230. this.print(node.right);
  231. }
  232. function AssignmentExpression(node) {
  233. this.print(node.left);
  234. this.space();
  235. if (node.operator === "in" || node.operator === "instanceof") {
  236. this.word(node.operator);
  237. } else {
  238. this.token(node.operator);
  239. this._endsWithDiv = node.operator === "/";
  240. }
  241. this.space();
  242. this.print(node.right);
  243. }
  244. function BindExpression(node) {
  245. this.print(node.object);
  246. this.token("::");
  247. this.print(node.callee);
  248. }
  249. function MemberExpression(node) {
  250. this.print(node.object);
  251. if (!node.computed && isMemberExpression(node.property)) {
  252. throw new TypeError("Got a MemberExpression for MemberExpression property");
  253. }
  254. let computed = node.computed;
  255. if (isLiteral(node.property) && typeof node.property.value === "number") {
  256. computed = true;
  257. }
  258. if (computed) {
  259. const exit = this.enterDelimited();
  260. this.tokenChar(91);
  261. this.print(node.property);
  262. this.tokenChar(93);
  263. exit();
  264. } else {
  265. this.tokenChar(46);
  266. this.print(node.property);
  267. }
  268. }
  269. function MetaProperty(node) {
  270. this.print(node.meta);
  271. this.tokenChar(46);
  272. this.print(node.property);
  273. }
  274. function PrivateName(node) {
  275. this.tokenChar(35);
  276. this.print(node.id);
  277. }
  278. function V8IntrinsicIdentifier(node) {
  279. this.tokenChar(37);
  280. this.word(node.name);
  281. }
  282. function ModuleExpression(node) {
  283. this.word("module", true);
  284. this.space();
  285. this.tokenChar(123);
  286. this.indent();
  287. const {
  288. body
  289. } = node;
  290. if (body.body.length || body.directives.length) {
  291. this.newline();
  292. }
  293. this.print(body);
  294. this.dedent();
  295. this.rightBrace(node);
  296. }
  297. //# sourceMappingURL=expressions.js.map