methods.js 6.2 KB

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