• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports._params = _params;
7exports._parameters = _parameters;
8exports._param = _param;
9exports._methodHead = _methodHead;
10exports._predicate = _predicate;
11exports._functionHead = _functionHead;
12exports.FunctionDeclaration = exports.FunctionExpression = FunctionExpression;
13exports.ArrowFunctionExpression = ArrowFunctionExpression;
14
15function t() {
16  const data = _interopRequireWildcard(require("@babel/types"));
17
18  t = function () {
19    return data;
20  };
21
22  return data;
23}
24
25function _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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
26
27function _params(node) {
28  this.print(node.typeParameters, node);
29  this.token("(");
30
31  this._parameters(node.params, node);
32
33  this.token(")");
34  this.print(node.returnType, node);
35}
36
37function _parameters(parameters, parent) {
38  for (let i = 0; i < parameters.length; i++) {
39    this._param(parameters[i], parent);
40
41    if (i < parameters.length - 1) {
42      this.token(",");
43      this.space();
44    }
45  }
46}
47
48function _param(parameter, parent) {
49  this.printJoin(parameter.decorators, parameter);
50  this.print(parameter, parent);
51  if (parameter.optional) this.token("?");
52  this.print(parameter.typeAnnotation, parameter);
53}
54
55function _methodHead(node) {
56  const kind = node.kind;
57  const key = node.key;
58
59  if (kind === "get" || kind === "set") {
60    this.word(kind);
61    this.space();
62  }
63
64  if (node.async) {
65    this.word("async");
66    this.space();
67  }
68
69  if (kind === "method" || kind === "init") {
70    if (node.generator) {
71      this.token("*");
72    }
73  }
74
75  if (node.computed) {
76    this.token("[");
77    this.print(key, node);
78    this.token("]");
79  } else {
80    this.print(key, node);
81  }
82
83  if (node.optional) {
84    this.token("?");
85  }
86
87  this._params(node);
88}
89
90function _predicate(node) {
91  if (node.predicate) {
92    if (!node.returnType) {
93      this.token(":");
94    }
95
96    this.space();
97    this.print(node.predicate, node);
98  }
99}
100
101function _functionHead(node) {
102  if (node.async) {
103    this.word("async");
104    this.space();
105  }
106
107  this.word("function");
108  if (node.generator) this.token("*");
109  this.space();
110
111  if (node.id) {
112    this.print(node.id, node);
113  }
114
115  this._params(node);
116
117  this._predicate(node);
118}
119
120function FunctionExpression(node) {
121  this._functionHead(node);
122
123  this.space();
124  this.print(node.body, node);
125}
126
127function ArrowFunctionExpression(node) {
128  if (node.async) {
129    this.word("async");
130    this.space();
131  }
132
133  const firstParam = node.params[0];
134
135  if (node.params.length === 1 && t().isIdentifier(firstParam) && !hasTypes(node, firstParam)) {
136    if (this.format.retainLines && node.loc && node.body.loc && node.loc.start.line < node.body.loc.start.line) {
137      this.token("(");
138
139      if (firstParam.loc && firstParam.loc.start.line > node.loc.start.line) {
140        this.indent();
141        this.print(firstParam, node);
142        this.dedent();
143
144        this._catchUp("start", node.body.loc);
145      } else {
146        this.print(firstParam, node);
147      }
148
149      this.token(")");
150    } else {
151      this.print(firstParam, node);
152    }
153  } else {
154    this._params(node);
155  }
156
157  this._predicate(node);
158
159  this.space();
160  this.token("=>");
161  this.space();
162  this.print(node.body, node);
163}
164
165function hasTypes(node, param) {
166  return node.typeParameters || node.returnType || param.typeAnnotation || param.optional || param.trailingComments;
167}