1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.ArrowFunctionExpression = ArrowFunctionExpression;
|
---|
7 | exports.FunctionDeclaration = exports.FunctionExpression = FunctionExpression;
|
---|
8 | exports._functionHead = _functionHead;
|
---|
9 | exports._methodHead = _methodHead;
|
---|
10 | exports._param = _param;
|
---|
11 | exports._parameters = _parameters;
|
---|
12 | exports._params = _params;
|
---|
13 | exports._predicate = _predicate;
|
---|
14 | exports._shouldPrintArrowParamsParens = _shouldPrintArrowParamsParens;
|
---|
15 | var _t = require("@babel/types");
|
---|
16 | var _index = require("../node/index.js");
|
---|
17 | const {
|
---|
18 | isIdentifier
|
---|
19 | } = _t;
|
---|
20 | function _params(node, idNode, parentNode) {
|
---|
21 | this.print(node.typeParameters);
|
---|
22 | const nameInfo = _getFuncIdName.call(this, idNode, parentNode);
|
---|
23 | if (nameInfo) {
|
---|
24 | this.sourceIdentifierName(nameInfo.name, nameInfo.pos);
|
---|
25 | }
|
---|
26 | this.tokenChar(40);
|
---|
27 | this._parameters(node.params, ")");
|
---|
28 | const noLineTerminator = node.type === "ArrowFunctionExpression";
|
---|
29 | this.print(node.returnType, noLineTerminator);
|
---|
30 | this._noLineTerminator = noLineTerminator;
|
---|
31 | }
|
---|
32 | function _parameters(parameters, endToken) {
|
---|
33 | const exit = this.enterDelimited();
|
---|
34 | const trailingComma = this.shouldPrintTrailingComma(endToken);
|
---|
35 | const paramLength = parameters.length;
|
---|
36 | for (let i = 0; i < paramLength; i++) {
|
---|
37 | this._param(parameters[i]);
|
---|
38 | if (trailingComma || i < paramLength - 1) {
|
---|
39 | this.token(",", null, i);
|
---|
40 | this.space();
|
---|
41 | }
|
---|
42 | }
|
---|
43 | this.token(endToken);
|
---|
44 | exit();
|
---|
45 | }
|
---|
46 | function _param(parameter) {
|
---|
47 | this.printJoin(parameter.decorators);
|
---|
48 | this.print(parameter);
|
---|
49 | if (parameter.optional) {
|
---|
50 | this.tokenChar(63);
|
---|
51 | }
|
---|
52 | this.print(parameter.typeAnnotation);
|
---|
53 | }
|
---|
54 | function _methodHead(node) {
|
---|
55 | const kind = node.kind;
|
---|
56 | const key = node.key;
|
---|
57 | if (kind === "get" || kind === "set") {
|
---|
58 | this.word(kind);
|
---|
59 | this.space();
|
---|
60 | }
|
---|
61 | if (node.async) {
|
---|
62 | this.word("async", true);
|
---|
63 | this.space();
|
---|
64 | }
|
---|
65 | if (kind === "method" || kind === "init") {
|
---|
66 | if (node.generator) {
|
---|
67 | this.tokenChar(42);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | if (node.computed) {
|
---|
71 | this.tokenChar(91);
|
---|
72 | this.print(key);
|
---|
73 | this.tokenChar(93);
|
---|
74 | } else {
|
---|
75 | this.print(key);
|
---|
76 | }
|
---|
77 | if (node.optional) {
|
---|
78 | this.tokenChar(63);
|
---|
79 | }
|
---|
80 | this._params(node, node.computed && node.key.type !== "StringLiteral" ? undefined : node.key, undefined);
|
---|
81 | }
|
---|
82 | function _predicate(node, noLineTerminatorAfter) {
|
---|
83 | if (node.predicate) {
|
---|
84 | if (!node.returnType) {
|
---|
85 | this.tokenChar(58);
|
---|
86 | }
|
---|
87 | this.space();
|
---|
88 | this.print(node.predicate, noLineTerminatorAfter);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | function _functionHead(node, parent) {
|
---|
92 | if (node.async) {
|
---|
93 | this.word("async");
|
---|
94 | if (!this.format.preserveFormat) {
|
---|
95 | this._endsWithInnerRaw = false;
|
---|
96 | }
|
---|
97 | this.space();
|
---|
98 | }
|
---|
99 | this.word("function");
|
---|
100 | if (node.generator) {
|
---|
101 | if (!this.format.preserveFormat) {
|
---|
102 | this._endsWithInnerRaw = false;
|
---|
103 | }
|
---|
104 | this.tokenChar(42);
|
---|
105 | }
|
---|
106 | this.space();
|
---|
107 | if (node.id) {
|
---|
108 | this.print(node.id);
|
---|
109 | }
|
---|
110 | this._params(node, node.id, parent);
|
---|
111 | if (node.type !== "TSDeclareFunction") {
|
---|
112 | this._predicate(node);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | function FunctionExpression(node, parent) {
|
---|
116 | this._functionHead(node, parent);
|
---|
117 | this.space();
|
---|
118 | this.print(node.body);
|
---|
119 | }
|
---|
120 | function ArrowFunctionExpression(node, parent) {
|
---|
121 | if (node.async) {
|
---|
122 | this.word("async", true);
|
---|
123 | this.space();
|
---|
124 | }
|
---|
125 | if (this._shouldPrintArrowParamsParens(node)) {
|
---|
126 | this._params(node, undefined, parent);
|
---|
127 | } else {
|
---|
128 | this.print(node.params[0], true);
|
---|
129 | }
|
---|
130 | this._predicate(node, true);
|
---|
131 | this.space();
|
---|
132 | this.printInnerComments();
|
---|
133 | this.token("=>");
|
---|
134 | this.space();
|
---|
135 | this.tokenContext |= _index.TokenContext.arrowBody;
|
---|
136 | this.print(node.body);
|
---|
137 | }
|
---|
138 | function _shouldPrintArrowParamsParens(node) {
|
---|
139 | var _firstParam$leadingCo, _firstParam$trailingC;
|
---|
140 | if (node.params.length !== 1) return true;
|
---|
141 | if (node.typeParameters || node.returnType || node.predicate) {
|
---|
142 | return true;
|
---|
143 | }
|
---|
144 | const firstParam = node.params[0];
|
---|
145 | if (!isIdentifier(firstParam) || firstParam.typeAnnotation || firstParam.optional || (_firstParam$leadingCo = firstParam.leadingComments) != null && _firstParam$leadingCo.length || (_firstParam$trailingC = firstParam.trailingComments) != null && _firstParam$trailingC.length) {
|
---|
146 | return true;
|
---|
147 | }
|
---|
148 | if (this.tokenMap) {
|
---|
149 | if (node.loc == null) return true;
|
---|
150 | if (this.tokenMap.findMatching(node, "(") !== null) return true;
|
---|
151 | const arrowToken = this.tokenMap.findMatching(node, "=>");
|
---|
152 | if ((arrowToken == null ? void 0 : arrowToken.loc) == null) return true;
|
---|
153 | return arrowToken.loc.start.line !== node.loc.start.line;
|
---|
154 | }
|
---|
155 | if (this.format.retainLines) return true;
|
---|
156 | return false;
|
---|
157 | }
|
---|
158 | function _getFuncIdName(idNode, parent) {
|
---|
159 | let id = idNode;
|
---|
160 | if (!id && parent) {
|
---|
161 | const parentType = parent.type;
|
---|
162 | if (parentType === "VariableDeclarator") {
|
---|
163 | id = parent.id;
|
---|
164 | } else if (parentType === "AssignmentExpression" || parentType === "AssignmentPattern") {
|
---|
165 | id = parent.left;
|
---|
166 | } else if (parentType === "ObjectProperty" || parentType === "ClassProperty") {
|
---|
167 | if (!parent.computed || parent.key.type === "StringLiteral") {
|
---|
168 | id = parent.key;
|
---|
169 | }
|
---|
170 | } else if (parentType === "ClassPrivateProperty" || parentType === "ClassAccessorProperty") {
|
---|
171 | id = parent.key;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | if (!id) return;
|
---|
175 | let nameInfo;
|
---|
176 | if (id.type === "Identifier") {
|
---|
177 | var _id$loc, _id$loc2;
|
---|
178 | nameInfo = {
|
---|
179 | pos: (_id$loc = id.loc) == null ? void 0 : _id$loc.start,
|
---|
180 | name: ((_id$loc2 = id.loc) == null ? void 0 : _id$loc2.identifierName) || id.name
|
---|
181 | };
|
---|
182 | } else if (id.type === "PrivateName") {
|
---|
183 | var _id$loc3;
|
---|
184 | nameInfo = {
|
---|
185 | pos: (_id$loc3 = id.loc) == null ? void 0 : _id$loc3.start,
|
---|
186 | name: "#" + id.id.name
|
---|
187 | };
|
---|
188 | } else if (id.type === "StringLiteral") {
|
---|
189 | var _id$loc4;
|
---|
190 | nameInfo = {
|
---|
191 | pos: (_id$loc4 = id.loc) == null ? void 0 : _id$loc4.start,
|
---|
192 | name: id.value
|
---|
193 | };
|
---|
194 | }
|
---|
195 | return nameInfo;
|
---|
196 | }
|
---|
197 |
|
---|
198 | //# sourceMappingURL=methods.js.map
|
---|