methods.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.FunctionDeclaration = undefined;
  4. exports._params = _params;
  5. exports._method = _method;
  6. exports.FunctionExpression = FunctionExpression;
  7. exports.ArrowFunctionExpression = ArrowFunctionExpression;
  8. var _babelTypes = require("babel-types");
  9. var t = _interopRequireWildcard(_babelTypes);
  10. 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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  11. function _params(node) {
  12. var _this = this;
  13. this.print(node.typeParameters, node);
  14. this.token("(");
  15. this.printList(node.params, node, {
  16. iterator: function iterator(node) {
  17. if (node.optional) _this.token("?");
  18. _this.print(node.typeAnnotation, node);
  19. }
  20. });
  21. this.token(")");
  22. if (node.returnType) {
  23. this.print(node.returnType, node);
  24. }
  25. }
  26. function _method(node) {
  27. var kind = node.kind;
  28. var key = node.key;
  29. if (kind === "method" || kind === "init") {
  30. if (node.generator) {
  31. this.token("*");
  32. }
  33. }
  34. if (kind === "get" || kind === "set") {
  35. this.word(kind);
  36. this.space();
  37. }
  38. if (node.async) {
  39. this.word("async");
  40. this.space();
  41. }
  42. if (node.computed) {
  43. this.token("[");
  44. this.print(key, node);
  45. this.token("]");
  46. } else {
  47. this.print(key, node);
  48. }
  49. this._params(node);
  50. this.space();
  51. this.print(node.body, node);
  52. }
  53. function FunctionExpression(node) {
  54. if (node.async) {
  55. this.word("async");
  56. this.space();
  57. }
  58. this.word("function");
  59. if (node.generator) this.token("*");
  60. if (node.id) {
  61. this.space();
  62. this.print(node.id, node);
  63. } else {
  64. this.space();
  65. }
  66. this._params(node);
  67. this.space();
  68. this.print(node.body, node);
  69. }
  70. exports.FunctionDeclaration = FunctionExpression;
  71. function ArrowFunctionExpression(node) {
  72. if (node.async) {
  73. this.word("async");
  74. this.space();
  75. }
  76. var firstParam = node.params[0];
  77. if (node.params.length === 1 && t.isIdentifier(firstParam) && !hasTypes(node, firstParam)) {
  78. this.print(firstParam, node);
  79. } else {
  80. this._params(node);
  81. }
  82. this.space();
  83. this.token("=>");
  84. this.space();
  85. this.print(node.body, node);
  86. }
  87. function hasTypes(node, param) {
  88. return node.typeParameters || node.returnType || param.typeAnnotation || param.optional || param.trailingComments;
  89. }