methods.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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) {
  18. this.print(node.typeParameters, node);
  19. this.tokenChar(40);
  20. this._parameters(node.params, node);
  21. this.tokenChar(41);
  22. this.print(node.returnType, node, node.type === "ArrowFunctionExpression");
  23. }
  24. function _parameters(parameters, parent) {
  25. const paramLength = parameters.length;
  26. for (let i = 0; i < paramLength; i++) {
  27. this._param(parameters[i], parent);
  28. if (i < parameters.length - 1) {
  29. this.tokenChar(44);
  30. this.space();
  31. }
  32. }
  33. }
  34. function _param(parameter, parent) {
  35. this.printJoin(parameter.decorators, parameter);
  36. this.print(parameter, parent);
  37. if (
  38. parameter.optional) {
  39. this.tokenChar(63);
  40. }
  41. this.print(
  42. parameter.typeAnnotation, parameter);
  43. }
  44. function _methodHead(node) {
  45. const kind = node.kind;
  46. const key = node.key;
  47. if (kind === "get" || kind === "set") {
  48. this.word(kind);
  49. this.space();
  50. }
  51. if (node.async) {
  52. this.word("async", true);
  53. this.space();
  54. }
  55. if (kind === "method" ||
  56. kind === "init") {
  57. if (node.generator) {
  58. this.tokenChar(42);
  59. }
  60. }
  61. if (node.computed) {
  62. this.tokenChar(91);
  63. this.print(key, node);
  64. this.tokenChar(93);
  65. } else {
  66. this.print(key, node);
  67. }
  68. if (
  69. node.optional) {
  70. this.tokenChar(63);
  71. }
  72. this._params(node);
  73. }
  74. function _predicate(node, noLineTerminatorAfter) {
  75. if (node.predicate) {
  76. if (!node.returnType) {
  77. this.tokenChar(58);
  78. }
  79. this.space();
  80. this.print(node.predicate, node, noLineTerminatorAfter);
  81. }
  82. }
  83. function _functionHead(node) {
  84. if (node.async) {
  85. this.word("async");
  86. this._endsWithInnerRaw = false;
  87. this.space();
  88. }
  89. this.word("function");
  90. if (node.generator) {
  91. this._endsWithInnerRaw = false;
  92. this.tokenChar(42);
  93. }
  94. this.space();
  95. if (node.id) {
  96. this.print(node.id, node);
  97. }
  98. this._params(node);
  99. if (node.type !== "TSDeclareFunction") {
  100. this._predicate(node);
  101. }
  102. }
  103. function FunctionExpression(node) {
  104. this._functionHead(node);
  105. this.space();
  106. this.print(node.body, node);
  107. }
  108. function ArrowFunctionExpression(node) {
  109. if (node.async) {
  110. this.word("async", true);
  111. this.space();
  112. }
  113. let firstParam;
  114. if (!this.format.retainLines && node.params.length === 1 && isIdentifier(firstParam = node.params[0]) && !hasTypesOrComments(node, firstParam)) {
  115. this.print(firstParam, node, true);
  116. } else {
  117. this._params(node);
  118. }
  119. this._predicate(node, true);
  120. this.space();
  121. this.printInnerComments();
  122. this.token("=>");
  123. this.space();
  124. this.print(node.body, node);
  125. }
  126. function hasTypesOrComments(node, param) {
  127. var _param$leadingComment, _param$trailingCommen;
  128. 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);
  129. }
  130. //# sourceMappingURL=methods.js.map