-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathphpClass.ts
More file actions
335 lines (273 loc) · 9.34 KB
/
phpClass.ts
File metadata and controls
335 lines (273 loc) · 9.34 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import {
InterfaceDeclaration,
isPropertySignature,
PropertySignature,
JSDoc,
isIdentifier,
TypeNode,
IntersectionTypeNode,
isUnionTypeNode,
SyntaxKind,
ModuleDeclaration,
Node,
isModuleBlock,
isVariableDeclarationList,
isVariableDeclaration,
isTypeLiteralNode,
isLiteralTypeNode,
isTypeNode,
forEachChild,
isExportDeclaration,
isExportSpecifier,
HeritageClause,
isExpressionWithTypeArguments,
TypeLiteralNode,
NodeArray,
TypeElement,
UnionTypeNode
} from 'typescript';
import { PhpType, TypeConverter } from './typeConverter';
import { NodeMap } from './nodeMap';
function forEachDescendant(node, callback) {
callback(node);
forEachChild(node, (childNode: Node) => {
forEachDescendant(childNode, callback);
});
}
export function constantsFromModule(module: ModuleDeclaration): PhpConstants {
const constants = new PhpConstants();
forEachDescendant(module, (node: Node) => {
if (!isVariableDeclaration(node)) {
return false;
}
if (!isIdentifier(node.name)) {
return false;
}
if (node.type && isLiteralTypeNode(node.type)) {
constants.set(node.name.escapedText.toString(), new PhpConstant(
node.name.escapedText.toString(),
node.type.literal.getText()
));
return false;
}
if (node.initializer) {
constants.set(node.name.escapedText.toString(), new PhpConstant(
node.name.escapedText.toString(),
node.initializer.getText()
));
return false;
}
});
return constants;
}
export abstract class PhpClassLike {
kind: string;
name: string;
docs: string[] = [];
constructor(name: string, docs: string[] = []) {
this.name = name;
this.docs = docs;
}
}
export class PhpInterface extends PhpClassLike {
kind: string = 'interface';
constants: PhpConstants
constructor(name: string, docs: string[] = [], phpConstants: PhpConstants = new Map()) {
super(name);
this.constants = phpConstants;
}
}
export class PhpConstant {
name: string;
rawValue: string;
constructor(name: string, rawValue: string) {
if (name.toLowerCase() === 'class') {
name = 'class_';
}
this.name = name;
this.rawValue = rawValue;
}
}
export function isPhpInterface(phpClass: PhpClassLike): phpClass is PhpInterface {
return phpClass.kind === 'interface';
}
export function isPhpClass(phpClass: PhpClassLike): phpClass is PhpClass {
return phpClass.kind === 'class';
}
export class PhpClass extends PhpClassLike {
kind: string = 'class';
properties: Properties = new Properties();
extends: string | null = null;
mixins: string[] = [];
}
export class Properties extends Map<string, Property> {
}
export class PhpConstants extends Map<string, PhpConstant> {
}
export class PhpClasses extends Map<string, PhpClassLike> {
}
export class Property {
name: string;
type: PhpType;
nullable: boolean;
docs: string[] = [];
special: string[] = [];
}
export class PhpClassResolver {
nodeMap: NodeMap;
typeConverter: TypeConverter;
constructor(nodeMap: NodeMap, typeConverter: TypeConverter) {
this.nodeMap = nodeMap;
this.typeConverter = typeConverter;
}
public fromNodeMap(nodeMap: NodeMap): PhpClasses {
const classes = new PhpClasses();
nodeMap.modules.forEach((module: ModuleDeclaration, name: string) => {
const phpInterface = this.interfaceFromModule(name, module);
if (null === phpInterface) {
return;
}
classes.set(name, phpInterface);
});
nodeMap.interfaces.forEach((interfaceDeclaration: InterfaceDeclaration) => {
const phpClass = this.fromInterface(interfaceDeclaration);
classes.set(phpClass.name, this.hydateMixins(phpClass));
});
nodeMap.intersections.forEach((type: IntersectionTypeNode, name: string) => {
classes.set(name, this.fromIntersectionTypeNode(name, type));
});
nodeMap.typeLiterals.forEach((type, name: string) => {
classes.set(name, this.fromTypeLiteral(name, type));
});
return classes;
}
private fromTypeLiteral(name: string, type: TypeLiteralNode): PhpClass {
const phpClass = new PhpClass(name);
const properties = this.mapProperties(type.members);
phpClass.properties = properties;
return phpClass;
}
private fromIntersectionTypeNode(name: string, type: IntersectionTypeNode): PhpClass {
const phpClass = new PhpClass(name);
type.types.forEach((type: TypeNode) => {
if (isTypeLiteralNode(type)) {
let properties = phpClass.properties;
properties = new Map([...properties, ... this.mapProperties(type.members)]);
phpClass.properties = properties;
return;
}
phpClass.mixins.push(this.typeConverter.phpType(type).real);
});
return this.hydateMixins(phpClass);
}
private fromInterface(declaration: InterfaceDeclaration): PhpClass {
const docs = this.jsDocs(declaration);
let phpClass = new PhpClass(declaration.name.escapedText.toString(), docs);
// change to Set
const mixins = Array<string>();
for (const heritageClause of declaration.heritageClauses ?? []) {
if (SyntaxKind[heritageClause.token] === 'ExtendsKeyword') {
phpClass.extends = this.resolveClassParent(heritageClause);
}
}
const properties = this.mapProperties(declaration.members);
if (declaration.heritageClauses) {
for (const clause of declaration.heritageClauses) {
for (const type of clause.types) {
if (!isIdentifier(type.expression)) {
continue;
}
const mixinName = type.expression.escapedText.toString();
mixins.push(mixinName);
}
}
}
phpClass.properties = properties;
phpClass.mixins = mixins;
return phpClass;
}
private mapProperties(members: NodeArray<TypeElement>): Map<string,Property> {
var properties = new Map<string, Property>();
for (const property of members) {
let special = [];
if (!isPropertySignature(property)) {
continue;
}
if (!isIdentifier(property.name)) {
continue;
}
if (property.type.getText().match(/^DocumentUri$/)) {
special.push('uri');
}
const classProperty: Property = {
docs: this.jsDocs(property),
name: property.name.escapedText.toString(),
type: this.typeConverter.phpType(property.type),
nullable: this.isNullable(property),
special
};
properties.set(classProperty.name, classProperty);
}
return properties;
}
private interfaceFromModule(name: string, module: ModuleDeclaration): PhpInterface | null {
const constants = constantsFromModule(module);
if (constants.size === 0) {
return null;
}
return new PhpInterface(name, this.jsDocs(module), constants);
}
private hydateMixins(phpClass: PhpClass): PhpClass {
var properties = phpClass.properties;
phpClass.mixins.forEach((mixinName: string) => {
if (!this.nodeMap.interfaces.has(mixinName)) {
console.warn(`MixIn ${mixinName} is not a mapped interface`);
return;
}
const mixinClass = this.fromInterface(this.nodeMap.interfaces.get(mixinName));
properties = new Map([...properties, ... this.hydateMixins(mixinClass).properties]);
});
phpClass.properties = properties;
return phpClass;
}
private jsDocs(property: PropertySignature | InterfaceDeclaration | ModuleDeclaration): string[] {
const jsDocs: JSDoc[] = property['jsDoc'];
if (!jsDocs) {
return [];
}
return [].concat.apply([], jsDocs.map((doc: JSDoc) => {
if (!doc.comment) {
return;
}
return doc.comment.split("\r\n");
}));
}
private isNullable(property: PropertySignature): boolean {
if (property.questionToken) {
return true;
}
if (!property.type) {
return false;
}
if (!isUnionTypeNode(property.type)) {
return false;
}
for (const type of property.type.types) {
if (SyntaxKind[type.kind] == 'NullKeyword') {
return true;
}
}
return false;
}
private resolveClassParent(extendsClause: HeritageClause): string | null {
const types = extendsClause.types.filter((element) => {
return isExpressionWithTypeArguments(element);
}).map((element) => {
return element.getText();
});
if (types.length === 0) {
return null;
}
return types[0];
}
}