forked from babel/babel
/
classes.ts
172 lines (147 loc) 路 3.63 KB
/
classes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import type Printer from "../printer";
import {
isExportDefaultDeclaration,
isExportNamedDeclaration,
} from "@babel/types";
import type * as t from "@babel/types";
import * as charCodes from "charcodes";
export function ClassDeclaration(
this: Printer,
node: t.ClassDeclaration,
parent: any,
) {
if (
!this.format.decoratorsBeforeExport ||
(!isExportDefaultDeclaration(parent) && !isExportNamedDeclaration(parent))
) {
this.printJoin(node.decorators, node);
}
if (node.declare) {
// TS
this.word("declare");
this.space();
}
if (node.abstract) {
// TS
this.word("abstract");
this.space();
}
this.word("class");
this.printInnerComments(node);
if (node.id) {
this.space();
this.print(node.id, node);
}
this.print(node.typeParameters, node);
if (node.superClass) {
this.space();
this.word("extends");
this.space();
this.print(node.superClass, node);
this.print(node.superTypeParameters, node);
}
if (node.implements) {
this.space();
this.word("implements");
this.space();
this.printList(node.implements, node);
}
this.space();
this.print(node.body, node);
}
export { ClassDeclaration as ClassExpression };
export function ClassBody(this: Printer, node: t.ClassBody) {
this.token("{");
this.printInnerComments(node);
if (node.body.length === 0) {
this.token("}");
} else {
this.newline();
this.indent();
this.printSequence(node.body, node);
this.dedent();
if (!this.endsWith(charCodes.lineFeed)) this.newline();
this.rightBrace();
}
}
export function ClassProperty(this: Printer, node: t.ClassProperty) {
this.printJoin(node.decorators, node);
// catch up to property key, avoid line break
// between member modifiers and the property key.
this.source("end", node.key.loc);
this.tsPrintClassMemberModifiers(node, /* isField */ true);
if (node.computed) {
this.token("[");
this.print(node.key, node);
this.token("]");
} else {
this._variance(node);
this.print(node.key, node);
}
// TS
if (node.optional) {
this.token("?");
}
if (node.definite) {
this.token("!");
}
this.print(node.typeAnnotation, node);
if (node.value) {
this.space();
this.token("=");
this.space();
this.print(node.value, node);
}
this.semicolon();
}
export function ClassPrivateProperty(
this: Printer,
node: t.ClassPrivateProperty,
) {
this.printJoin(node.decorators, node);
if (node.static) {
this.word("static");
this.space();
}
this.print(node.key, node);
this.print(node.typeAnnotation, node);
if (node.value) {
this.space();
this.token("=");
this.space();
this.print(node.value, node);
}
this.semicolon();
}
export function ClassMethod(this: Printer, node: t.ClassMethod) {
this._classMethodHead(node);
this.space();
this.print(node.body, node);
}
export function ClassPrivateMethod(this: Printer, node: t.ClassPrivateMethod) {
this._classMethodHead(node);
this.space();
this.print(node.body, node);
}
export function _classMethodHead(this: Printer, node) {
this.printJoin(node.decorators, node);
// catch up to method key, avoid line break
// between member modifiers/method heads and the method key.
this.source("end", node.key.loc);
this.tsPrintClassMemberModifiers(node, /* isField */ false);
this._methodHead(node);
}
export function StaticBlock(node: t.StaticBlock) {
this.word("static");
this.space();
this.token("{");
if (node.body.length === 0) {
this.token("}");
} else {
this.newline();
this.printSequence(node.body, node, {
indent: true,
});
this.rightBrace();
}
}