methods.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.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.printInnerComments(node);
  85. this.space();
  86. if (node.id) {
  87. this.print(node.id, node);
  88. }
  89. this._params(node);
  90. this._predicate(node);
  91. }
  92. function FunctionExpression(node) {
  93. this._functionHead(node);
  94. this.space();
  95. this.print(node.body, node);
  96. }
  97. function ArrowFunctionExpression(node) {
  98. if (node.async) {
  99. this.word("async");
  100. this.space();
  101. }
  102. const firstParam = node.params[0];
  103. if (!this.format.retainLines && !this.format.auxiliaryCommentBefore && !this.format.auxiliaryCommentAfter && node.params.length === 1 && isIdentifier(firstParam) && !hasTypesOrComments(node, firstParam)) {
  104. this.print(firstParam, node);
  105. } else {
  106. this._params(node);
  107. }
  108. this._predicate(node);
  109. this.space();
  110. this.token("=>");
  111. this.space();
  112. this.print(node.body, node);
  113. }
  114. function hasTypesOrComments(node, param) {
  115. var _param$leadingComment, _param$trailingCommen;
  116. 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);
  117. }