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