methods.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports._params = _params;
  6. exports._parameters = _parameters;
  7. exports._param = _param;
  8. exports._methodHead = _methodHead;
  9. exports._predicate = _predicate;
  10. exports._functionHead = _functionHead;
  11. exports.FunctionDeclaration = exports.FunctionExpression = FunctionExpression;
  12. exports.ArrowFunctionExpression = ArrowFunctionExpression;
  13. function t() {
  14. const data = _interopRequireWildcard(require("@babel/types"));
  15. t = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. 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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  21. function _params(node) {
  22. this.print(node.typeParameters, node);
  23. this.token("(");
  24. this._parameters(node.params, node);
  25. this.token(")");
  26. this.print(node.returnType, node);
  27. }
  28. function _parameters(parameters, parent) {
  29. for (let i = 0; i < parameters.length; i++) {
  30. this._param(parameters[i], parent);
  31. if (i < parameters.length - 1) {
  32. this.token(",");
  33. this.space();
  34. }
  35. }
  36. }
  37. function _param(parameter, parent) {
  38. this.printJoin(parameter.decorators, parameter);
  39. this.print(parameter, parent);
  40. if (parameter.optional) this.token("?");
  41. this.print(parameter.typeAnnotation, parameter);
  42. }
  43. function _methodHead(node) {
  44. const kind = node.kind;
  45. const key = node.key;
  46. if (kind === "get" || kind === "set") {
  47. this.word(kind);
  48. this.space();
  49. }
  50. if (node.async) {
  51. this.word("async");
  52. this.space();
  53. }
  54. if (kind === "method" || kind === "init") {
  55. if (node.generator) {
  56. this.token("*");
  57. }
  58. }
  59. if (node.computed) {
  60. this.token("[");
  61. this.print(key, node);
  62. this.token("]");
  63. } else {
  64. this.print(key, node);
  65. }
  66. if (node.optional) {
  67. this.token("?");
  68. }
  69. this._params(node);
  70. }
  71. function _predicate(node) {
  72. if (node.predicate) {
  73. if (!node.returnType) {
  74. this.token(":");
  75. }
  76. this.space();
  77. this.print(node.predicate, node);
  78. }
  79. }
  80. function _functionHead(node) {
  81. if (node.async) {
  82. this.word("async");
  83. this.space();
  84. }
  85. this.word("function");
  86. if (node.generator) this.token("*");
  87. this.space();
  88. if (node.id) {
  89. this.print(node.id, node);
  90. }
  91. this._params(node);
  92. this._predicate(node);
  93. }
  94. function FunctionExpression(node) {
  95. this._functionHead(node);
  96. this.space();
  97. this.print(node.body, node);
  98. }
  99. function ArrowFunctionExpression(node) {
  100. if (node.async) {
  101. this.word("async");
  102. this.space();
  103. }
  104. const firstParam = node.params[0];
  105. if (node.params.length === 1 && t().isIdentifier(firstParam) && !hasTypes(node, firstParam)) {
  106. if (this.format.retainLines && node.loc && node.body.loc && node.loc.start.line < node.body.loc.start.line) {
  107. this.token("(");
  108. if (firstParam.loc && firstParam.loc.start.line > node.loc.start.line) {
  109. this.indent();
  110. this.print(firstParam, node);
  111. this.dedent();
  112. this._catchUp("start", node.body.loc);
  113. } else {
  114. this.print(firstParam, node);
  115. }
  116. this.token(")");
  117. } else {
  118. this.print(firstParam, node);
  119. }
  120. } else {
  121. this._params(node);
  122. }
  123. this._predicate(node);
  124. this.space();
  125. this.token("=>");
  126. this.space();
  127. this.print(node.body, node);
  128. }
  129. function hasTypes(node, param) {
  130. return node.typeParameters || node.returnType || param.typeAnnotation || param.optional || param.trailingComments;
  131. }