methods.js 5.0 KB

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