You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.1 KiB

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