expressions.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.LogicalExpression = exports.BinaryExpression = exports.AwaitExpression = exports.YieldExpression = undefined;
  4. exports.UnaryExpression = UnaryExpression;
  5. exports.DoExpression = DoExpression;
  6. exports.ParenthesizedExpression = ParenthesizedExpression;
  7. exports.UpdateExpression = UpdateExpression;
  8. exports.ConditionalExpression = ConditionalExpression;
  9. exports.NewExpression = NewExpression;
  10. exports.SequenceExpression = SequenceExpression;
  11. exports.ThisExpression = ThisExpression;
  12. exports.Super = Super;
  13. exports.Decorator = Decorator;
  14. exports.CallExpression = CallExpression;
  15. exports.Import = Import;
  16. exports.EmptyStatement = EmptyStatement;
  17. exports.ExpressionStatement = ExpressionStatement;
  18. exports.AssignmentPattern = AssignmentPattern;
  19. exports.AssignmentExpression = AssignmentExpression;
  20. exports.BindExpression = BindExpression;
  21. exports.MemberExpression = MemberExpression;
  22. exports.MetaProperty = MetaProperty;
  23. var _babelTypes = require("babel-types");
  24. var t = _interopRequireWildcard(_babelTypes);
  25. var _node = require("../node");
  26. var n = _interopRequireWildcard(_node);
  27. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  28. function UnaryExpression(node) {
  29. if (node.operator === "void" || node.operator === "delete" || node.operator === "typeof") {
  30. this.word(node.operator);
  31. this.space();
  32. } else {
  33. this.token(node.operator);
  34. }
  35. this.print(node.argument, node);
  36. }
  37. function DoExpression(node) {
  38. this.word("do");
  39. this.space();
  40. this.print(node.body, node);
  41. }
  42. function ParenthesizedExpression(node) {
  43. this.token("(");
  44. this.print(node.expression, node);
  45. this.token(")");
  46. }
  47. function UpdateExpression(node) {
  48. if (node.prefix) {
  49. this.token(node.operator);
  50. this.print(node.argument, node);
  51. } else {
  52. this.print(node.argument, node);
  53. this.token(node.operator);
  54. }
  55. }
  56. function ConditionalExpression(node) {
  57. this.print(node.test, node);
  58. this.space();
  59. this.token("?");
  60. this.space();
  61. this.print(node.consequent, node);
  62. this.space();
  63. this.token(":");
  64. this.space();
  65. this.print(node.alternate, node);
  66. }
  67. function NewExpression(node, parent) {
  68. this.word("new");
  69. this.space();
  70. this.print(node.callee, node);
  71. if (node.arguments.length === 0 && this.format.minified && !t.isCallExpression(parent, { callee: node }) && !t.isMemberExpression(parent) && !t.isNewExpression(parent)) return;
  72. this.token("(");
  73. this.printList(node.arguments, node);
  74. this.token(")");
  75. }
  76. function SequenceExpression(node) {
  77. this.printList(node.expressions, node);
  78. }
  79. function ThisExpression() {
  80. this.word("this");
  81. }
  82. function Super() {
  83. this.word("super");
  84. }
  85. function Decorator(node) {
  86. this.token("@");
  87. this.print(node.expression, node);
  88. this.newline();
  89. }
  90. function commaSeparatorNewline() {
  91. this.token(",");
  92. this.newline();
  93. if (!this.endsWith("\n")) this.space();
  94. }
  95. function CallExpression(node) {
  96. this.print(node.callee, node);
  97. this.token("(");
  98. var isPrettyCall = node._prettyCall;
  99. var separator = void 0;
  100. if (isPrettyCall) {
  101. separator = commaSeparatorNewline;
  102. this.newline();
  103. this.indent();
  104. }
  105. this.printList(node.arguments, node, { separator: separator });
  106. if (isPrettyCall) {
  107. this.newline();
  108. this.dedent();
  109. }
  110. this.token(")");
  111. }
  112. function Import() {
  113. this.word("import");
  114. }
  115. function buildYieldAwait(keyword) {
  116. return function (node) {
  117. this.word(keyword);
  118. if (node.delegate) {
  119. this.token("*");
  120. }
  121. if (node.argument) {
  122. this.space();
  123. var terminatorState = this.startTerminatorless();
  124. this.print(node.argument, node);
  125. this.endTerminatorless(terminatorState);
  126. }
  127. };
  128. }
  129. var YieldExpression = exports.YieldExpression = buildYieldAwait("yield");
  130. var AwaitExpression = exports.AwaitExpression = buildYieldAwait("await");
  131. function EmptyStatement() {
  132. this.semicolon(true);
  133. }
  134. function ExpressionStatement(node) {
  135. this.print(node.expression, node);
  136. this.semicolon();
  137. }
  138. function AssignmentPattern(node) {
  139. this.print(node.left, node);
  140. if (node.left.optional) this.token("?");
  141. this.print(node.left.typeAnnotation, node);
  142. this.space();
  143. this.token("=");
  144. this.space();
  145. this.print(node.right, node);
  146. }
  147. function AssignmentExpression(node, parent) {
  148. var parens = this.inForStatementInitCounter && node.operator === "in" && !n.needsParens(node, parent);
  149. if (parens) {
  150. this.token("(");
  151. }
  152. this.print(node.left, node);
  153. this.space();
  154. if (node.operator === "in" || node.operator === "instanceof") {
  155. this.word(node.operator);
  156. } else {
  157. this.token(node.operator);
  158. }
  159. this.space();
  160. this.print(node.right, node);
  161. if (parens) {
  162. this.token(")");
  163. }
  164. }
  165. function BindExpression(node) {
  166. this.print(node.object, node);
  167. this.token("::");
  168. this.print(node.callee, node);
  169. }
  170. exports.BinaryExpression = AssignmentExpression;
  171. exports.LogicalExpression = AssignmentExpression;
  172. function MemberExpression(node) {
  173. this.print(node.object, node);
  174. if (!node.computed && t.isMemberExpression(node.property)) {
  175. throw new TypeError("Got a MemberExpression for MemberExpression property");
  176. }
  177. var computed = node.computed;
  178. if (t.isLiteral(node.property) && typeof node.property.value === "number") {
  179. computed = true;
  180. }
  181. if (computed) {
  182. this.token("[");
  183. this.print(node.property, node);
  184. this.token("]");
  185. } else {
  186. this.token(".");
  187. this.print(node.property, node);
  188. }
  189. }
  190. function MetaProperty(node) {
  191. this.print(node.meta, node);
  192. this.token(".");
  193. this.print(node.property, node);
  194. }