UNPKG

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