expressions.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.LogicalExpression = exports.AssignmentExpression = AssignmentExpression;
  6. exports.AssignmentPattern = AssignmentPattern;
  7. exports.AwaitExpression = AwaitExpression;
  8. exports.BinaryExpression = BinaryExpression;
  9. exports.BindExpression = BindExpression;
  10. exports.CallExpression = CallExpression;
  11. exports.ConditionalExpression = ConditionalExpression;
  12. exports.Decorator = Decorator;
  13. exports.DoExpression = DoExpression;
  14. exports.EmptyStatement = EmptyStatement;
  15. exports.ExpressionStatement = ExpressionStatement;
  16. exports.Import = Import;
  17. exports.MemberExpression = MemberExpression;
  18. exports.MetaProperty = MetaProperty;
  19. exports.ModuleExpression = ModuleExpression;
  20. exports.NewExpression = NewExpression;
  21. exports.OptionalCallExpression = OptionalCallExpression;
  22. exports.OptionalMemberExpression = OptionalMemberExpression;
  23. exports.ParenthesizedExpression = ParenthesizedExpression;
  24. exports.PrivateName = PrivateName;
  25. exports.SequenceExpression = SequenceExpression;
  26. exports.Super = Super;
  27. exports.ThisExpression = ThisExpression;
  28. exports.UnaryExpression = UnaryExpression;
  29. exports.UpdateExpression = UpdateExpression;
  30. exports.V8IntrinsicIdentifier = V8IntrinsicIdentifier;
  31. exports.YieldExpression = YieldExpression;
  32. exports._shouldPrintDecoratorsBeforeExport = _shouldPrintDecoratorsBeforeExport;
  33. var _t = require("@babel/types");
  34. var _index = require("../node/index.js");
  35. const {
  36. isCallExpression,
  37. isLiteral,
  38. isMemberExpression,
  39. isNewExpression,
  40. isPattern
  41. } = _t;
  42. function UnaryExpression(node) {
  43. const {
  44. operator
  45. } = node;
  46. const firstChar = operator.charCodeAt(0);
  47. if (firstChar >= 97 && firstChar <= 122) {
  48. this.word(operator);
  49. this.space();
  50. } else {
  51. this.tokenChar(firstChar);
  52. }
  53. this.print(node.argument);
  54. }
  55. function DoExpression(node) {
  56. if (node.async) {
  57. this.word("async", true);
  58. this.space();
  59. }
  60. this.word("do");
  61. this.space();
  62. this.print(node.body);
  63. }
  64. function ParenthesizedExpression(node) {
  65. this.tokenChar(40);
  66. const oldNoLineTerminatorAfterNode = this.enterDelimited();
  67. this.print(node.expression, undefined, true);
  68. this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
  69. this.rightParens(node);
  70. }
  71. function UpdateExpression(node) {
  72. if (node.prefix) {
  73. this.token(node.operator, false, 0, true);
  74. this.print(node.argument);
  75. } else {
  76. this.print(node.argument, true);
  77. this.token(node.operator, false, 0, true);
  78. }
  79. }
  80. function ConditionalExpression(node) {
  81. this.print(node.test);
  82. this.space();
  83. this.tokenChar(63);
  84. this.space();
  85. this.print(node.consequent);
  86. this.space();
  87. this.tokenChar(58);
  88. this.space();
  89. this.print(node.alternate);
  90. }
  91. function NewExpression(node, parent) {
  92. this.word("new");
  93. this.space();
  94. this.print(node.callee);
  95. if (this.format.minified && node.arguments.length === 0 && !node.optional && !isCallExpression(parent, {
  96. callee: node
  97. }) && !isMemberExpression(parent) && !isNewExpression(parent)) {
  98. return;
  99. }
  100. this.print(node.typeArguments);
  101. this.print(node.typeParameters);
  102. if (node.optional) {
  103. this.token("?.");
  104. }
  105. if (node.arguments.length === 0 && this.tokenMap && !this.tokenMap.endMatches(node, ")")) {
  106. return;
  107. }
  108. this.tokenChar(40);
  109. const oldNoLineTerminatorAfterNode = this.enterDelimited();
  110. this.printList(node.arguments, this.shouldPrintTrailingComma(")"), undefined, undefined, undefined, true);
  111. this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
  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. const {
  132. expression
  133. } = node;
  134. this.print(expression);
  135. this.newline();
  136. }
  137. function OptionalMemberExpression(node) {
  138. let {
  139. computed
  140. } = node;
  141. const {
  142. optional,
  143. property
  144. } = node;
  145. this.print(node.object);
  146. if (!computed && isMemberExpression(property)) {
  147. throw new TypeError("Got a MemberExpression for MemberExpression property");
  148. }
  149. if (isLiteral(property) && typeof property.value === "number") {
  150. computed = true;
  151. }
  152. if (optional) {
  153. this.token("?.");
  154. }
  155. if (computed) {
  156. this.tokenChar(91);
  157. this.print(property);
  158. this.tokenChar(93);
  159. } else {
  160. if (!optional) {
  161. this.tokenChar(46);
  162. }
  163. this.print(property);
  164. }
  165. }
  166. function OptionalCallExpression(node) {
  167. this.print(node.callee);
  168. this.print(node.typeParameters);
  169. if (node.optional) {
  170. this.token("?.");
  171. }
  172. this.print(node.typeArguments);
  173. this.tokenChar(40);
  174. const oldNoLineTerminatorAfterNode = this.enterDelimited();
  175. this.printList(node.arguments, undefined, undefined, undefined, undefined, true);
  176. this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
  177. this.rightParens(node);
  178. }
  179. function CallExpression(node) {
  180. this.print(node.callee);
  181. this.print(node.typeArguments);
  182. this.print(node.typeParameters);
  183. this.tokenChar(40);
  184. const oldNoLineTerminatorAfterNode = this.enterDelimited();
  185. this.printList(node.arguments, this.shouldPrintTrailingComma(")"), undefined, undefined, undefined, true);
  186. this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
  187. this.rightParens(node);
  188. }
  189. function Import() {
  190. this.word("import");
  191. }
  192. function AwaitExpression(node) {
  193. this.word("await");
  194. this.space();
  195. this.print(node.argument);
  196. }
  197. function YieldExpression(node) {
  198. if (node.delegate) {
  199. this.word("yield", true);
  200. this.tokenChar(42);
  201. if (node.argument) {
  202. this.space();
  203. this.print(node.argument);
  204. }
  205. } else if (node.argument) {
  206. this.word("yield", true);
  207. this.space();
  208. this.print(node.argument);
  209. } else {
  210. this.word("yield");
  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. this.token(node.operator, false, 0, true);
  236. this.space();
  237. this.print(node.right);
  238. }
  239. function BinaryExpression(node) {
  240. this.print(node.left);
  241. this.space();
  242. const {
  243. operator
  244. } = node;
  245. if (operator.charCodeAt(0) === 105) {
  246. this.word(operator);
  247. } else {
  248. this.token(operator, false, 0, true);
  249. this.setLastChar(operator.charCodeAt(operator.length - 1));
  250. }
  251. this.space();
  252. this.print(node.right);
  253. }
  254. function BindExpression(node) {
  255. this.print(node.object);
  256. this.token("::");
  257. this.print(node.callee);
  258. }
  259. function MemberExpression(node) {
  260. this.print(node.object);
  261. if (!node.computed && isMemberExpression(node.property)) {
  262. throw new TypeError("Got a MemberExpression for MemberExpression property");
  263. }
  264. let computed = node.computed;
  265. if (isLiteral(node.property) && typeof node.property.value === "number") {
  266. computed = true;
  267. }
  268. if (computed) {
  269. const oldNoLineTerminatorAfterNode = this.enterDelimited();
  270. this.tokenChar(91);
  271. this.print(node.property, undefined, true);
  272. this.tokenChar(93);
  273. this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
  274. } else {
  275. this.tokenChar(46);
  276. this.print(node.property);
  277. }
  278. }
  279. function MetaProperty(node) {
  280. this.print(node.meta);
  281. this.tokenChar(46);
  282. this.print(node.property);
  283. }
  284. function PrivateName(node) {
  285. this.tokenChar(35);
  286. this.print(node.id);
  287. }
  288. function V8IntrinsicIdentifier(node) {
  289. this.tokenChar(37);
  290. this.word(node.name);
  291. }
  292. function ModuleExpression(node) {
  293. this.word("module", true);
  294. this.space();
  295. this.tokenChar(123);
  296. this.indent();
  297. const {
  298. body
  299. } = node;
  300. if (body.body.length || body.directives.length) {
  301. this.newline();
  302. }
  303. this.print(body);
  304. this.dedent();
  305. this.rightBrace(node);
  306. }
  307. //# sourceMappingURL=expressions.js.map