methods.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. var _t = require("@babel/types");
  14. const {
  15. isIdentifier
  16. } = _t;
  17. function _params(node) {
  18. this.print(node.typeParameters, node);
  19. this.token("(");
  20. this._parameters(node.params, node);
  21. this.token(")");
  22. this.print(node.returnType, node);
  23. }
  24. function _parameters(parameters, parent) {
  25. for (let i = 0; i < parameters.length; i++) {
  26. this._param(parameters[i], parent);
  27. if (i < parameters.length - 1) {
  28. this.token(",");
  29. this.space();
  30. }
  31. }
  32. }
  33. function _param(parameter, parent) {
  34. this.printJoin(parameter.decorators, parameter);
  35. this.print(parameter, parent);
  36. if (parameter.optional) this.token("?");
  37. this.print(parameter.typeAnnotation, parameter);
  38. }
  39. function _methodHead(node) {
  40. const kind = node.kind;
  41. const key = node.key;
  42. if (kind === "get" || kind === "set") {
  43. this.word(kind);
  44. this.space();
  45. }
  46. if (node.async) {
  47. this._catchUp("start", key.loc);
  48. this.word("async");
  49. this.space();
  50. }
  51. if (kind === "method" || kind === "init") {
  52. if (node.generator) {
  53. this.token("*");
  54. }
  55. }
  56. if (node.computed) {
  57. this.token("[");
  58. this.print(key, node);
  59. this.token("]");
  60. } else {
  61. this.print(key, node);
  62. }
  63. if (node.optional) {
  64. this.token("?");
  65. }
  66. this._params(node);
  67. }
  68. function _predicate(node) {
  69. if (node.predicate) {
  70. if (!node.returnType) {
  71. this.token(":");
  72. }
  73. this.space();
  74. this.print(node.predicate, node);
  75. }
  76. }
  77. function _functionHead(node) {
  78. if (node.async) {
  79. this.word("async");
  80. this.space();
  81. }
  82. this.word("function");
  83. if (node.generator) this.token("*");
  84. this.space();
  85. if (node.id) {
  86. this.print(node.id, node);
  87. }
  88. this._params(node);
  89. this._predicate(node);
  90. }
  91. function FunctionExpression(node) {
  92. this._functionHead(node);
  93. this.space();
  94. this.print(node.body, node);
  95. }
  96. function ArrowFunctionExpression(node) {
  97. if (node.async) {
  98. this.word("async");
  99. this.space();
  100. }
  101. const firstParam = node.params[0];
  102. if (!this.format.retainLines && !this.format.auxiliaryCommentBefore && !this.format.auxiliaryCommentAfter && node.params.length === 1 && isIdentifier(firstParam) && !hasTypesOrComments(node, firstParam)) {
  103. this.print(firstParam, node);
  104. } else {
  105. this._params(node);
  106. }
  107. this._predicate(node);
  108. this.space();
  109. this.token("=>");
  110. this.space();
  111. this.print(node.body, node);
  112. }
  113. function hasTypesOrComments(node, param) {
  114. var _param$leadingComment, _param$trailingCommen;
  115. 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);
  116. }