methods.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ArrowFunctionExpression = ArrowFunctionExpression;
  6. exports.FunctionDeclaration = exports.FunctionExpression = FunctionExpression;
  7. exports._functionHead = _functionHead;
  8. exports._methodHead = _methodHead;
  9. exports._param = _param;
  10. exports._parameters = _parameters;
  11. exports._params = _params;
  12. exports._predicate = _predicate;
  13. var _t = require("@babel/types");
  14. var _index = require("../node/index.js");
  15. const {
  16. isIdentifier
  17. } = _t;
  18. function _params(node, idNode, parentNode) {
  19. this.print(node.typeParameters);
  20. const nameInfo = _getFuncIdName.call(this, idNode, parentNode);
  21. if (nameInfo) {
  22. this.sourceIdentifierName(nameInfo.name, nameInfo.pos);
  23. }
  24. this.tokenChar(40);
  25. this._parameters(node.params);
  26. this.tokenChar(41);
  27. const noLineTerminator = node.type === "ArrowFunctionExpression";
  28. this.print(node.returnType, noLineTerminator);
  29. this._noLineTerminator = noLineTerminator;
  30. }
  31. function _parameters(parameters) {
  32. const exit = this.enterForStatementInit(false);
  33. const paramLength = parameters.length;
  34. for (let i = 0; i < paramLength; i++) {
  35. this._param(parameters[i]);
  36. if (i < parameters.length - 1) {
  37. this.tokenChar(44);
  38. this.space();
  39. }
  40. }
  41. exit();
  42. }
  43. function _param(parameter) {
  44. this.printJoin(parameter.decorators);
  45. this.print(parameter);
  46. if (parameter.optional) {
  47. this.tokenChar(63);
  48. }
  49. this.print(parameter.typeAnnotation);
  50. }
  51. function _methodHead(node) {
  52. const kind = node.kind;
  53. const key = node.key;
  54. if (kind === "get" || kind === "set") {
  55. this.word(kind);
  56. this.space();
  57. }
  58. if (node.async) {
  59. this.word("async", true);
  60. this.space();
  61. }
  62. if (kind === "method" || kind === "init") {
  63. if (node.generator) {
  64. this.tokenChar(42);
  65. }
  66. }
  67. if (node.computed) {
  68. this.tokenChar(91);
  69. this.print(key);
  70. this.tokenChar(93);
  71. } else {
  72. this.print(key);
  73. }
  74. if (node.optional) {
  75. this.tokenChar(63);
  76. }
  77. this._params(node, node.computed && node.key.type !== "StringLiteral" ? undefined : node.key, undefined);
  78. }
  79. function _predicate(node, noLineTerminatorAfter) {
  80. if (node.predicate) {
  81. if (!node.returnType) {
  82. this.tokenChar(58);
  83. }
  84. this.space();
  85. this.print(node.predicate, noLineTerminatorAfter);
  86. }
  87. }
  88. function _functionHead(node, parent) {
  89. if (node.async) {
  90. this.word("async");
  91. this._endsWithInnerRaw = false;
  92. this.space();
  93. }
  94. this.word("function");
  95. if (node.generator) {
  96. this._endsWithInnerRaw = false;
  97. this.tokenChar(42);
  98. }
  99. this.space();
  100. if (node.id) {
  101. this.print(node.id);
  102. }
  103. this._params(node, node.id, parent);
  104. if (node.type !== "TSDeclareFunction") {
  105. this._predicate(node);
  106. }
  107. }
  108. function FunctionExpression(node, parent) {
  109. this._functionHead(node, parent);
  110. this.space();
  111. this.print(node.body);
  112. }
  113. function ArrowFunctionExpression(node, parent) {
  114. if (node.async) {
  115. this.word("async", true);
  116. this.space();
  117. }
  118. let firstParam;
  119. if (!this.format.retainLines && node.params.length === 1 && isIdentifier(firstParam = node.params[0]) && !hasTypesOrComments(node, firstParam)) {
  120. this.print(firstParam, true);
  121. } else {
  122. this._params(node, undefined, parent);
  123. }
  124. this._predicate(node, true);
  125. this.space();
  126. this.printInnerComments();
  127. this.token("=>");
  128. this.space();
  129. this.tokenContext |= _index.TokenContext.arrowBody;
  130. this.print(node.body);
  131. }
  132. function hasTypesOrComments(node, param) {
  133. var _param$leadingComment, _param$trailingCommen;
  134. return !!(node.typeParameters || node.returnType || node.predicate || param.typeAnnotation || param.optional || (_param$leadingComment = param.leadingComments) != null && _param$leadingComment.length || (_param$trailingCommen = param.trailingComments) != null && _param$trailingCommen.length);
  135. }
  136. function _getFuncIdName(idNode, parent) {
  137. let id = idNode;
  138. if (!id && parent) {
  139. const parentType = parent.type;
  140. if (parentType === "VariableDeclarator") {
  141. id = parent.id;
  142. } else if (parentType === "AssignmentExpression" || parentType === "AssignmentPattern") {
  143. id = parent.left;
  144. } else if (parentType === "ObjectProperty" || parentType === "ClassProperty") {
  145. if (!parent.computed || parent.key.type === "StringLiteral") {
  146. id = parent.key;
  147. }
  148. } else if (parentType === "ClassPrivateProperty" || parentType === "ClassAccessorProperty") {
  149. id = parent.key;
  150. }
  151. }
  152. if (!id) return;
  153. let nameInfo;
  154. if (id.type === "Identifier") {
  155. var _id$loc, _id$loc2;
  156. nameInfo = {
  157. pos: (_id$loc = id.loc) == null ? void 0 : _id$loc.start,
  158. name: ((_id$loc2 = id.loc) == null ? void 0 : _id$loc2.identifierName) || id.name
  159. };
  160. } else if (id.type === "PrivateName") {
  161. var _id$loc3;
  162. nameInfo = {
  163. pos: (_id$loc3 = id.loc) == null ? void 0 : _id$loc3.start,
  164. name: "#" + id.id.name
  165. };
  166. } else if (id.type === "StringLiteral") {
  167. var _id$loc4;
  168. nameInfo = {
  169. pos: (_id$loc4 = id.loc) == null ? void 0 : _id$loc4.start,
  170. name: id.value
  171. };
  172. }
  173. return nameInfo;
  174. }
  175. //# sourceMappingURL=methods.js.map