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