UNPKG

1.1 kBPlain TextView Raw
1import { SyntaxKind } from 'ts-morph';
2
3export function StringifyArrowFunction(af) {
4 let i = 0,
5 result = '(';
6 const len = af.parameters.length;
7 if (len === 1) {
8 result = '';
9 }
10 for (i; i < len; i++) {
11 if (af.parameters[i].name && af.parameters[i].name.escapedText) {
12 result += af.parameters[i].name.escapedText;
13 }
14 if (i < len - 1) {
15 result += ', ';
16 }
17 }
18 if (len > 1 || len === 0) {
19 result += ')';
20 }
21 // body
22 result += ' => ';
23 if (af.body) {
24 if (af.body.kind === SyntaxKind.Identifier && af.body.escapedText) {
25 result += af.body.escapedText;
26 } else if (
27 af.body.kind === SyntaxKind.PropertyAccessExpression &&
28 af.body.expression &&
29 af.body.name
30 ) {
31 result += af.body.expression.escapedText;
32 result += '.' + af.body.name.escapedText;
33 } else if (af.body.kind === SyntaxKind.StringLiteral && af.body.text) {
34 result += af.body.text;
35 }
36 }
37 return result;
38}