methods.js 5.7 KB

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