@colyseus/schema 2.0.4 → 2.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -4
- package/build/cjs/index.js +48 -48
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.mjs +130 -104
- package/build/esm/index.mjs.map +1 -1
- package/build/umd/index.js +50 -50
- package/lib/Reflection.js +87 -119
- package/lib/Reflection.js.map +1 -1
- package/lib/Schema.js +195 -257
- package/lib/Schema.js.map +1 -1
- package/lib/annotations.d.ts +6 -6
- package/lib/annotations.js +64 -92
- package/lib/annotations.js.map +1 -1
- package/lib/changes/ChangeTree.d.ts +1 -1
- package/lib/changes/ChangeTree.js +63 -70
- package/lib/changes/ChangeTree.js.map +1 -1
- package/lib/changes/ReferenceTracker.js +24 -27
- package/lib/changes/ReferenceTracker.js.map +1 -1
- package/lib/codegen/api.js +9 -9
- package/lib/codegen/api.js.map +1 -1
- package/lib/codegen/argv.d.ts +1 -1
- package/lib/codegen/argv.js +11 -11
- package/lib/codegen/argv.js.map +1 -1
- package/lib/codegen/cli.js +21 -10
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/languages/cpp.js +126 -77
- package/lib/codegen/languages/cpp.js.map +1 -1
- package/lib/codegen/languages/csharp.js +121 -62
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/languages/haxe.js +34 -26
- package/lib/codegen/languages/haxe.js.map +1 -1
- package/lib/codegen/languages/java.js +39 -27
- package/lib/codegen/languages/java.js.map +1 -1
- package/lib/codegen/languages/js.js +48 -32
- package/lib/codegen/languages/js.js.map +1 -1
- package/lib/codegen/languages/lua.js +35 -24
- package/lib/codegen/languages/lua.js.map +1 -1
- package/lib/codegen/languages/ts.js +63 -68
- package/lib/codegen/languages/ts.js.map +1 -1
- package/lib/codegen/parser.d.ts +9 -1
- package/lib/codegen/parser.js +88 -46
- package/lib/codegen/parser.js.map +1 -1
- package/lib/codegen/types.d.ts +8 -0
- package/lib/codegen/types.js +64 -54
- package/lib/codegen/types.js.map +1 -1
- package/lib/encoding/decode.js +15 -15
- package/lib/encoding/decode.js.map +1 -1
- package/lib/encoding/encode.js +14 -14
- package/lib/encoding/encode.js.map +1 -1
- package/lib/events/EventEmitter.d.ts +1 -1
- package/lib/events/EventEmitter.js +16 -47
- package/lib/events/EventEmitter.js.map +1 -1
- package/lib/filters/index.js +7 -8
- package/lib/filters/index.js.map +1 -1
- package/lib/index.js +11 -11
- package/lib/index.js.map +1 -1
- package/lib/types/ArraySchema.d.ts +1 -1
- package/lib/types/ArraySchema.js +161 -219
- package/lib/types/ArraySchema.js.map +1 -1
- package/lib/types/CollectionSchema.d.ts +1 -1
- package/lib/types/CollectionSchema.js +63 -71
- package/lib/types/CollectionSchema.js.map +1 -1
- package/lib/types/HelperTypes.d.ts +9 -9
- package/lib/types/MapSchema.d.ts +16 -16
- package/lib/types/MapSchema.js +68 -78
- package/lib/types/MapSchema.js.map +1 -1
- package/lib/types/SetSchema.js +62 -71
- package/lib/types/SetSchema.js.map +1 -1
- package/lib/types/index.js +1 -1
- package/lib/types/index.js.map +1 -1
- package/lib/types/typeRegistry.js +1 -1
- package/lib/types/typeRegistry.js.map +1 -1
- package/lib/types/utils.js +9 -10
- package/lib/types/utils.js.map +1 -1
- package/lib/utils.js +10 -13
- package/lib/utils.js.map +1 -1
- package/package.json +18 -15
- package/src/Reflection.ts +159 -0
- package/src/Schema.ts +1024 -0
- package/src/annotations.ts +400 -0
- package/src/changes/ChangeTree.ts +295 -0
- package/src/changes/ReferenceTracker.ts +81 -0
- package/src/codegen/api.ts +46 -0
- package/src/codegen/argv.ts +40 -0
- package/src/codegen/cli.ts +65 -0
- package/src/codegen/languages/cpp.ts +297 -0
- package/src/codegen/languages/csharp.ts +208 -0
- package/src/codegen/languages/haxe.ts +110 -0
- package/src/codegen/languages/java.ts +115 -0
- package/src/codegen/languages/js.ts +115 -0
- package/src/codegen/languages/lua.ts +125 -0
- package/src/codegen/languages/ts.ts +129 -0
- package/src/codegen/parser.ts +299 -0
- package/src/codegen/types.ts +177 -0
- package/src/encoding/decode.ts +278 -0
- package/src/encoding/encode.ts +283 -0
- package/src/filters/index.ts +23 -0
- package/src/index.ts +59 -0
- package/src/spec.ts +49 -0
- package/src/types/ArraySchema.ts +612 -0
- package/src/types/CollectionSchema.ts +199 -0
- package/src/types/HelperTypes.ts +34 -0
- package/src/types/MapSchema.ts +268 -0
- package/src/types/SetSchema.ts +208 -0
- package/src/types/typeRegistry.ts +19 -0
- package/src/types/utils.ts +62 -0
- package/src/utils.ts +28 -0
package/lib/codegen/parser.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseFiles = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
exports.getDecorators = exports.parseFiles = void 0;
|
|
4
|
+
const ts = require("typescript");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const types_1 = require("./types");
|
|
8
|
+
let currentStructure;
|
|
9
|
+
let currentProperty;
|
|
10
|
+
let globalContext;
|
|
11
11
|
function defineProperty(property, initializer) {
|
|
12
12
|
if (ts.isIdentifier(initializer)) {
|
|
13
13
|
property.type = "ref";
|
|
@@ -26,19 +26,18 @@ function defineProperty(property, initializer) {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
function inspectNode(node, context, decoratorName) {
|
|
29
|
-
var _a, _b, _c, _d, _e;
|
|
30
29
|
switch (node.kind) {
|
|
31
30
|
case ts.SyntaxKind.ImportClause:
|
|
32
|
-
|
|
31
|
+
const specifier = node.parent.moduleSpecifier;
|
|
33
32
|
if (specifier && specifier.text.startsWith('.')) {
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
const currentDir = path.dirname(node.getSourceFile().fileName);
|
|
34
|
+
const pathToImport = path.resolve(currentDir, specifier.text);
|
|
36
35
|
parseFiles([pathToImport], decoratorName, globalContext);
|
|
37
36
|
}
|
|
38
37
|
break;
|
|
39
38
|
case ts.SyntaxKind.ClassDeclaration:
|
|
40
39
|
currentStructure = new types_1.Class();
|
|
41
|
-
|
|
40
|
+
const heritageClauses = node.heritageClauses;
|
|
42
41
|
if (heritageClauses && heritageClauses.length > 0) {
|
|
43
42
|
currentStructure.extends = heritageClauses[0].types[0].expression.getText();
|
|
44
43
|
}
|
|
@@ -49,25 +48,31 @@ function inspectNode(node, context, decoratorName) {
|
|
|
49
48
|
// Only generate Interfaces if it has "Message" on its name.
|
|
50
49
|
// Example: MyMessage
|
|
51
50
|
//
|
|
52
|
-
|
|
51
|
+
const interfaceName = node.name.escapedText.toString();
|
|
53
52
|
if (interfaceName.indexOf("Message") !== -1) {
|
|
54
53
|
currentStructure = new types_1.Interface();
|
|
55
54
|
currentStructure.name = interfaceName;
|
|
56
55
|
context.addStructure(currentStructure);
|
|
57
56
|
}
|
|
58
57
|
break;
|
|
58
|
+
case ts.SyntaxKind.EnumDeclaration:
|
|
59
|
+
const enumName = node.name.escapedText.toString();
|
|
60
|
+
currentStructure = new types_1.Enum();
|
|
61
|
+
currentStructure.name = enumName;
|
|
62
|
+
context.addStructure(currentStructure);
|
|
63
|
+
break;
|
|
59
64
|
case ts.SyntaxKind.ExtendsKeyword:
|
|
60
65
|
// console.log(node.getText());
|
|
61
66
|
break;
|
|
62
67
|
case ts.SyntaxKind.PropertySignature:
|
|
63
68
|
if (currentStructure instanceof types_1.Interface) {
|
|
64
|
-
|
|
69
|
+
const interfaceDeclaration = node.parent;
|
|
65
70
|
if (currentStructure.name !== interfaceDeclaration.name.escapedText.toString()) {
|
|
66
71
|
// skip if property if for a another interface than the one we're interested in.
|
|
67
72
|
break;
|
|
68
73
|
}
|
|
69
74
|
// define a property of an interface
|
|
70
|
-
|
|
75
|
+
const property = new types_1.Property();
|
|
71
76
|
property.name = node.name.escapedText.toString();
|
|
72
77
|
property.type = node.type.getText();
|
|
73
78
|
currentStructure.addProperty(property);
|
|
@@ -81,13 +86,14 @@ function inspectNode(node, context, decoratorName) {
|
|
|
81
86
|
break;
|
|
82
87
|
}
|
|
83
88
|
if (node.getText() === decoratorName) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
89
|
+
const prop = node.parent?.parent?.parent;
|
|
90
|
+
const propDecorator = getDecorators(prop);
|
|
91
|
+
const hasExpression = prop?.expression?.arguments;
|
|
92
|
+
const hasDecorator = (propDecorator?.length > 0);
|
|
87
93
|
/**
|
|
88
94
|
* neither a `@type()` decorator or `type()` call. skip.
|
|
89
95
|
*/
|
|
90
|
-
if (!
|
|
96
|
+
if (!hasDecorator && !hasExpression) {
|
|
91
97
|
break;
|
|
92
98
|
}
|
|
93
99
|
// using as decorator
|
|
@@ -95,24 +101,24 @@ function inspectNode(node, context, decoratorName) {
|
|
|
95
101
|
/**
|
|
96
102
|
* Calling `@type()` as decorator
|
|
97
103
|
*/
|
|
98
|
-
|
|
104
|
+
const typeDecorator = propDecorator.find((decorator => {
|
|
99
105
|
return decorator.expression.expression.escapedText === decoratorName;
|
|
100
106
|
})).expression;
|
|
101
|
-
|
|
107
|
+
const property = currentProperty || new types_1.Property();
|
|
102
108
|
property.name = prop.name.escapedText;
|
|
103
109
|
currentStructure.addProperty(property);
|
|
104
|
-
|
|
110
|
+
const typeArgument = typeDecorator.arguments[0];
|
|
105
111
|
defineProperty(property, typeArgument);
|
|
106
112
|
}
|
|
107
|
-
else if (
|
|
108
|
-
|
|
113
|
+
else if (prop.expression.arguments?.[1] &&
|
|
114
|
+
prop.expression.expression.arguments?.[0]) {
|
|
109
115
|
/**
|
|
110
116
|
* Calling `type()` as a regular method
|
|
111
117
|
*/
|
|
112
|
-
|
|
118
|
+
const property = currentProperty || new types_1.Property();
|
|
113
119
|
property.name = prop.expression.arguments[1].text;
|
|
114
120
|
currentStructure.addProperty(property);
|
|
115
|
-
|
|
121
|
+
const typeArgument = prop.expression.expression.arguments[0];
|
|
116
122
|
defineProperty(property, typeArgument);
|
|
117
123
|
}
|
|
118
124
|
}
|
|
@@ -123,18 +129,18 @@ function inspectNode(node, context, decoratorName) {
|
|
|
123
129
|
* JavaScript source file (`.js`)
|
|
124
130
|
* Using `defineTypes()`
|
|
125
131
|
*/
|
|
126
|
-
|
|
132
|
+
const callExpression = (node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)
|
|
127
133
|
? node.parent.parent
|
|
128
134
|
: node.parent;
|
|
129
135
|
if (callExpression.kind !== ts.SyntaxKind.CallExpression) {
|
|
130
136
|
break;
|
|
131
137
|
}
|
|
132
|
-
|
|
138
|
+
const className = callExpression.arguments[0].getText();
|
|
133
139
|
currentStructure.name = className;
|
|
134
|
-
|
|
135
|
-
for (
|
|
136
|
-
|
|
137
|
-
|
|
140
|
+
const types = callExpression.arguments[1];
|
|
141
|
+
for (let i = 0; i < types.properties.length; i++) {
|
|
142
|
+
const prop = types.properties[i];
|
|
143
|
+
const property = currentProperty || new types_1.Property();
|
|
138
144
|
property.name = prop.name.escapedText;
|
|
139
145
|
currentStructure.addProperty(property);
|
|
140
146
|
defineProperty(property, prop.initializer);
|
|
@@ -145,13 +151,24 @@ function inspectNode(node, context, decoratorName) {
|
|
|
145
151
|
}
|
|
146
152
|
currentProperty = undefined;
|
|
147
153
|
break;
|
|
154
|
+
case ts.SyntaxKind.EnumMember:
|
|
155
|
+
if (currentStructure instanceof types_1.Enum) {
|
|
156
|
+
const initializer = node.initializer?.getText();
|
|
157
|
+
const name = node.getFirstToken().getText();
|
|
158
|
+
const property = currentProperty || new types_1.Property();
|
|
159
|
+
property.name = name;
|
|
160
|
+
if (initializer !== undefined) {
|
|
161
|
+
property.type = initializer;
|
|
162
|
+
}
|
|
163
|
+
currentStructure.addProperty(property);
|
|
164
|
+
currentProperty = undefined;
|
|
165
|
+
}
|
|
166
|
+
break;
|
|
148
167
|
}
|
|
149
|
-
ts.forEachChild(node,
|
|
168
|
+
ts.forEachChild(node, (n) => inspectNode(n, context, decoratorName));
|
|
150
169
|
}
|
|
151
|
-
|
|
152
|
-
function parseFiles(fileNames, decoratorName, context) {
|
|
153
|
-
if (decoratorName === void 0) { decoratorName = "type"; }
|
|
154
|
-
if (context === void 0) { context = new types_1.Context(); }
|
|
170
|
+
let parsedFiles;
|
|
171
|
+
function parseFiles(fileNames, decoratorName = "type", context = new types_1.Context()) {
|
|
155
172
|
/**
|
|
156
173
|
* Re-set globalContext for each test case
|
|
157
174
|
*/
|
|
@@ -159,26 +176,26 @@ function parseFiles(fileNames, decoratorName, context) {
|
|
|
159
176
|
parsedFiles = {};
|
|
160
177
|
globalContext = context;
|
|
161
178
|
}
|
|
162
|
-
fileNames.forEach(
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
179
|
+
fileNames.forEach((fileName) => {
|
|
180
|
+
let sourceFile;
|
|
181
|
+
let sourceFileName;
|
|
182
|
+
const fileNameAlternatives = [];
|
|
166
183
|
if (!fileName.endsWith(".ts") &&
|
|
167
184
|
!fileName.endsWith(".js") &&
|
|
168
185
|
!fileName.endsWith(".mjs")) {
|
|
169
|
-
fileNameAlternatives.push(fileName
|
|
170
|
-
fileNameAlternatives.push(fileName
|
|
186
|
+
fileNameAlternatives.push(`${fileName}.ts`);
|
|
187
|
+
fileNameAlternatives.push(`${fileName}/index.ts`);
|
|
171
188
|
}
|
|
172
189
|
else {
|
|
173
190
|
fileNameAlternatives.push(fileName);
|
|
174
191
|
}
|
|
175
|
-
for (
|
|
192
|
+
for (let i = 0; i < fileNameAlternatives.length; i++) {
|
|
176
193
|
try {
|
|
177
194
|
sourceFileName = path.resolve(fileNameAlternatives[i]);
|
|
178
195
|
if (parsedFiles[sourceFileName]) {
|
|
179
196
|
break;
|
|
180
197
|
}
|
|
181
|
-
sourceFile = ts.createSourceFile(sourceFileName, fs_1.readFileSync(sourceFileName).toString(), ts.ScriptTarget.Latest, true);
|
|
198
|
+
sourceFile = ts.createSourceFile(sourceFileName, (0, fs_1.readFileSync)(sourceFileName).toString(), ts.ScriptTarget.Latest, true);
|
|
182
199
|
parsedFiles[sourceFileName] = true;
|
|
183
200
|
break;
|
|
184
201
|
}
|
|
@@ -193,4 +210,29 @@ function parseFiles(fileNames, decoratorName, context) {
|
|
|
193
210
|
return context.getStructures();
|
|
194
211
|
}
|
|
195
212
|
exports.parseFiles = parseFiles;
|
|
213
|
+
/**
|
|
214
|
+
* TypeScript 4.8+ has introduced a change on how to access decorators.
|
|
215
|
+
* - https://github.com/microsoft/TypeScript/pull/49089
|
|
216
|
+
* - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees
|
|
217
|
+
*/
|
|
218
|
+
function getDecorators(node) {
|
|
219
|
+
if (node == undefined) {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
// TypeScript 4.7 and below
|
|
223
|
+
// @ts-ignore
|
|
224
|
+
if (node.decorators) {
|
|
225
|
+
return node.decorators;
|
|
226
|
+
}
|
|
227
|
+
// TypeScript 4.8 and above
|
|
228
|
+
// @ts-ignore
|
|
229
|
+
if (ts.canHaveDecorators && ts.canHaveDecorators(node)) {
|
|
230
|
+
// @ts-ignore
|
|
231
|
+
const decorators = ts.getDecorators(node);
|
|
232
|
+
return decorators ? Array.from(decorators) : undefined;
|
|
233
|
+
}
|
|
234
|
+
// @ts-ignore
|
|
235
|
+
return node.modifiers?.filter(ts.isDecorator);
|
|
236
|
+
}
|
|
237
|
+
exports.getDecorators = getDecorators;
|
|
196
238
|
//# sourceMappingURL=parser.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../../src/codegen/parser.ts"],"names":[],"mappings":";;;AAAA,+BAAiC;AACjC,2BAA6B;AAC7B,yBAAkC;AAClC,iCAA0E;AAE1E,IAAI,gBAA4B,CAAC;AACjC,IAAI,eAAyB,CAAC;AAE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;QAED,IAAI,UAAU,EAAE;YACZ,WAAW,CAAC,UAAU,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC;SACnD;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC,aAAa,EAAE,CAAC;AACnC,CAAC;AA5DD,gCA4DC","sourcesContent":["import * as ts from \"typescript\";\nimport * as path from \"path\";\nimport { readFileSync } from \"fs\";\nimport { IStructure, Class, Interface, Property, Context } from \"./types\";\n\nlet currentStructure: IStructure;\nlet currentProperty: Property;\n\nlet globalContext: Context;\n\nfunction defineProperty(property: Property, initializer: any) {\n if (ts.isIdentifier(initializer)) {\n property.type = \"ref\";\n property.childType = initializer.text;\n\n } else if (initializer.kind == ts.SyntaxKind.ObjectLiteralExpression) {\n property.type = initializer.properties[0].name.text;\n property.childType = initializer.properties[0].initializer.text;\n\n } else if (initializer.kind == ts.SyntaxKind.ArrayLiteralExpression) {\n property.type = \"array\";\n property.childType = initializer.elements[0].text;\n\n } else {\n property.type = initializer.text;\n }\n}\n\nfunction inspectNode(node: ts.Node, context: Context, decoratorName: string) {\n switch (node.kind) {\n case ts.SyntaxKind.ImportClause:\n const specifier = (node.parent as any).moduleSpecifier;\n if (specifier && (specifier.text as string).startsWith('.')) {\n const currentDir = path.dirname(node.getSourceFile().fileName);\n const pathToImport = path.resolve(currentDir, specifier.text);\n parseFiles([pathToImport], decoratorName, globalContext);\n }\n break;\n\n case ts.SyntaxKind.ClassDeclaration:\n currentStructure = new Class();\n\n const heritageClauses = (node as ts.ClassLikeDeclarationBase).heritageClauses;\n if (heritageClauses && heritageClauses.length > 0) {\n (currentStructure as Class).extends = heritageClauses[0].types[0].expression.getText();\n }\n\n context.addStructure(currentStructure);\n break;\n\n case ts.SyntaxKind.InterfaceDeclaration:\n //\n // Only generate Interfaces if it has \"Message\" on its name.\n // Example: MyMessage\n //\n const interfaceName = (node as ts.TypeParameterDeclaration).name.escapedText.toString();\n if (interfaceName.indexOf(\"Message\") !== -1) {\n currentStructure = new Interface();\n currentStructure.name = interfaceName;\n\n context.addStructure(currentStructure);\n }\n break;\n\n case ts.SyntaxKind.ExtendsKeyword:\n // console.log(node.getText());\n break;\n\n case ts.SyntaxKind.PropertySignature:\n if (currentStructure instanceof Interface) {\n const interfaceDeclaration = node.parent;\n\n if (\n currentStructure.name !== (interfaceDeclaration as ts.TypeParameterDeclaration).name.escapedText.toString()\n ) {\n // skip if property if for a another interface than the one we're interested in.\n break;\n }\n\n // define a property of an interface\n const property = new Property();\n property.name = (node as any).name.escapedText.toString();\n property.type = (node as any).type.getText();\n currentStructure.addProperty(property);\n }\n break;\n\n case ts.SyntaxKind.Identifier:\n if (\n node.getText() === \"deprecated\" &&\n node.parent.kind !== ts.SyntaxKind.ImportSpecifier\n ) {\n currentProperty = new Property();\n currentProperty.deprecated = true;\n break;\n }\n\n if (node.getText() === decoratorName) {\n const prop: any = node.parent?.parent?.parent;\n const propDecorator = prop?.decorators;\n const hasExpression = prop?.expression?.arguments;\n\n /**\n * neither a `@type()` decorator or `type()` call. skip.\n */\n if (!propDecorator && !hasExpression) {\n break;\n }\n\n // using as decorator\n if (propDecorator) {\n /**\n * Calling `@type()` as decorator\n */\n const typeDecorator: any = propDecorator.find((decorator => {\n return (decorator.expression as any).expression.escapedText === decoratorName;\n })).expression;\n\n const property = currentProperty || new Property();\n property.name = prop.name.escapedText;\n currentStructure.addProperty(property);\n\n const typeArgument = typeDecorator.arguments[0];\n defineProperty(property, typeArgument);\n\n } else if (\n prop.expression.arguments?.[1] &&\n prop.expression.expression.arguments?.[0]\n ) {\n /**\n * Calling `type()` as a regular method\n */\n const property = currentProperty || new Property();\n property.name = prop.expression.arguments[1].text;\n currentStructure.addProperty(property);\n\n const typeArgument = prop.expression.expression.arguments[0];\n defineProperty(property, typeArgument);\n }\n\n\n } else if (\n node.getText() === \"defineTypes\" &&\n (\n node.parent.kind === ts.SyntaxKind.CallExpression ||\n node.parent.kind === ts.SyntaxKind.PropertyAccessExpression\n )\n ) {\n /**\n * JavaScript source file (`.js`)\n * Using `defineTypes()`\n */\n const callExpression = (node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)\n ? node.parent.parent as ts.CallExpression\n : node.parent as ts.CallExpression;\n\n if (callExpression.kind !== ts.SyntaxKind.CallExpression) {\n break;\n }\n\n const className = callExpression.arguments[0].getText()\n currentStructure.name = className;\n\n const types = callExpression.arguments[1] as any;\n for (let i=0; i<types.properties.length; i++) {\n const prop = types.properties[i];\n\n const property = currentProperty || new Property();\n property.name = prop.name.escapedText;\n currentStructure.addProperty(property);\n\n defineProperty(property, prop.initializer);\n }\n\n }\n\n if (node.parent.kind === ts.SyntaxKind.ClassDeclaration) {\n currentStructure.name = node.getText();\n }\n\n currentProperty = undefined;\n\n break;\n }\n\n ts.forEachChild(node, (n) => inspectNode(n, context, decoratorName));\n}\n\nlet parsedFiles: { [filename: string]: boolean };\n\nexport function parseFiles(\n fileNames: string[],\n decoratorName: string = \"type\",\n context: Context = new Context()\n) {\n /**\n * Re-set globalContext for each test case\n */\n if (globalContext !== context) {\n parsedFiles = {};\n globalContext = context;\n }\n\n fileNames.forEach((fileName) => {\n let sourceFile: ts.Node;\n let sourceFileName: string;\n\n const fileNameAlternatives = [];\n\n if (\n !fileName.endsWith(\".ts\") &&\n !fileName.endsWith(\".js\") &&\n !fileName.endsWith(\".mjs\")\n ) {\n fileNameAlternatives.push(`${fileName}.ts`);\n fileNameAlternatives.push(`${fileName}/index.ts`);\n\n } else {\n fileNameAlternatives.push(fileName);\n }\n\n for (let i = 0; i < fileNameAlternatives.length; i++) {\n try {\n sourceFileName = path.resolve(fileNameAlternatives[i]);\n\n if (parsedFiles[sourceFileName]) {\n break;\n }\n\n sourceFile = ts.createSourceFile(\n sourceFileName,\n readFileSync(sourceFileName).toString(),\n ts.ScriptTarget.Latest,\n true\n );\n\n parsedFiles[sourceFileName] = true;\n\n break;\n } catch (e) {\n // console.log(`${fileNameAlternatives[i]} => ${e.message}`);\n }\n }\n\n if (sourceFile) {\n inspectNode(sourceFile, context, decoratorName);\n }\n });\n\n return context.getStructures();\n}"]}
|
|
1
|
+
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../../src/codegen/parser.ts"],"names":[],"mappings":";;;AAAA,iCAAiC;AACjC,6BAA6B;AAC7B,2BAAkC;AAClC,mCAAgF;AAEhF,IAAI,gBAA4B,CAAC;AACjC,IAAI,eAAyB,CAAC;AAE9B,IAAI,aAAsB,CAAC;AAE3B,SAAS,cAAc,CAAC,QAAkB,EAAE,WAAgB;IACxD,IAAI,EAAE,CAAC,YAAY,CAAC,WAAW,CAAC,EAAE;QAC9B,QAAQ,CAAC,IAAI,GAAG,KAAK,CAAC;QACtB,QAAQ,CAAC,SAAS,GAAG,WAAW,CAAC,IAAI,CAAC;KAEzC;SAAM,IAAI,WAAW,CAAC,IAAI,IAAI,EAAE,CAAC,UAAU,CAAC,uBAAuB,EAAE;QAClE,QAAQ,CAAC,IAAI,GAAG,WAAW,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;QACpD,QAAQ,CAAC,SAAS,GAAG,WAAW,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,IAAI,CAAC;KAEnE;SAAM,IAAI,WAAW,CAAC,IAAI,IAAI,EAAE,CAAC,UAAU,CAAC,sBAAsB,EAAE;QACjE,QAAQ,CAAC,IAAI,GAAG,OAAO,CAAC;QACxB,QAAQ,CAAC,SAAS,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;KAErD;SAAM;QACH,QAAQ,CAAC,IAAI,GAAG,WAAW,CAAC,IAAI,CAAC;KACpC;AACL,CAAC;AAED,SAAS,WAAW,CAAC,IAAa,EAAE,OAAgB,EAAE,aAAqB;IACvE,QAAQ,IAAI,CAAC,IAAI,EAAE;QACf,KAAK,EAAE,CAAC,UAAU,CAAC,YAAY;YAC3B,MAAM,SAAS,GAAI,IAAI,CAAC,MAAc,CAAC,eAAe,CAAC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aAAa,CAAC,IAAgC;IAC1D,IAAI,IAAI,IAAI,SAAS,EAAE;QAAE,OAAO,SAAS,CAAC;KAAE;IAE5C,2BAA2B;IAC3B,aAAa;IACb,IAAI,IAAI,CAAC,UAAU,EAAE;QAAE,OAAO,IAAI,CAAC,UAAU,CAAC;KAAE;IAEhD,2BAA2B;IAC3B,aAAa;IACb,IAAI,EAAE,CAAC,iBAAiB,IAAI,EAAE,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE;QACpD,aAAa;QACb,MAAM,UAAU,GAAG,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;QAC1C,OAAO,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;KAC1D;IAED,aAAa;IACb,OAAO,IAAI,CAAC,SAAS,EAAE,MAAM,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC;AAClD,CAAC;AAjBD,sCAiBC","sourcesContent":["import * as ts from \"typescript\";\nimport * as path from \"path\";\nimport { readFileSync } from \"fs\";\nimport { IStructure, Class, Interface, Property, Context, Enum } from \"./types\";\n\nlet currentStructure: IStructure;\nlet currentProperty: Property;\n\nlet globalContext: Context;\n\nfunction defineProperty(property: Property, initializer: any) {\n if (ts.isIdentifier(initializer)) {\n property.type = \"ref\";\n property.childType = initializer.text;\n\n } else if (initializer.kind == ts.SyntaxKind.ObjectLiteralExpression) {\n property.type = initializer.properties[0].name.text;\n property.childType = initializer.properties[0].initializer.text;\n\n } else if (initializer.kind == ts.SyntaxKind.ArrayLiteralExpression) {\n property.type = \"array\";\n property.childType = initializer.elements[0].text;\n\n } else {\n property.type = initializer.text;\n }\n}\n\nfunction inspectNode(node: ts.Node, context: Context, decoratorName: string) {\n switch (node.kind) {\n case ts.SyntaxKind.ImportClause:\n const specifier = (node.parent as any).moduleSpecifier;\n if (specifier && (specifier.text as string).startsWith('.')) {\n const currentDir = path.dirname(node.getSourceFile().fileName);\n const pathToImport = path.resolve(currentDir, specifier.text);\n parseFiles([pathToImport], decoratorName, globalContext);\n }\n break;\n\n case ts.SyntaxKind.ClassDeclaration:\n currentStructure = new Class();\n\n const heritageClauses = (node as ts.ClassLikeDeclarationBase).heritageClauses;\n if (heritageClauses && heritageClauses.length > 0) {\n (currentStructure as Class).extends = heritageClauses[0].types[0].expression.getText();\n }\n\n context.addStructure(currentStructure);\n break;\n\n case ts.SyntaxKind.InterfaceDeclaration:\n //\n // Only generate Interfaces if it has \"Message\" on its name.\n // Example: MyMessage\n //\n const interfaceName = (node as ts.TypeParameterDeclaration).name.escapedText.toString();\n if (interfaceName.indexOf(\"Message\") !== -1) {\n currentStructure = new Interface();\n currentStructure.name = interfaceName;\n\n context.addStructure(currentStructure);\n }\n break;\n\n case ts.SyntaxKind.EnumDeclaration:\n const enumName = (\n node as ts.EnumDeclaration\n ).name.escapedText.toString();\n currentStructure = new Enum();\n currentStructure.name = enumName;\n context.addStructure(currentStructure);\n break;\n\n case ts.SyntaxKind.ExtendsKeyword:\n // console.log(node.getText());\n break;\n\n case ts.SyntaxKind.PropertySignature:\n if (currentStructure instanceof Interface) {\n const interfaceDeclaration = node.parent;\n\n if (\n currentStructure.name !== (interfaceDeclaration as ts.TypeParameterDeclaration).name.escapedText.toString()\n ) {\n // skip if property if for a another interface than the one we're interested in.\n break;\n }\n\n // define a property of an interface\n const property = new Property();\n property.name = (node as any).name.escapedText.toString();\n property.type = (node as any).type.getText();\n currentStructure.addProperty(property);\n }\n break;\n\n case ts.SyntaxKind.Identifier:\n if (\n node.getText() === \"deprecated\" &&\n node.parent.kind !== ts.SyntaxKind.ImportSpecifier\n ) {\n currentProperty = new Property();\n currentProperty.deprecated = true;\n break;\n }\n\n if (node.getText() === decoratorName) {\n const prop: any = node.parent?.parent?.parent;\n const propDecorator = getDecorators(prop);\n const hasExpression = prop?.expression?.arguments;\n const hasDecorator = (propDecorator?.length > 0);\n\n /**\n * neither a `@type()` decorator or `type()` call. skip.\n */\n if (!hasDecorator && !hasExpression) {\n break;\n }\n\n // using as decorator\n if (propDecorator) {\n /**\n * Calling `@type()` as decorator\n */\n const typeDecorator: any = propDecorator.find((decorator => {\n return (decorator.expression as any).expression.escapedText === decoratorName;\n })).expression;\n\n const property = currentProperty || new Property();\n property.name = prop.name.escapedText;\n currentStructure.addProperty(property);\n\n const typeArgument = typeDecorator.arguments[0];\n defineProperty(property, typeArgument);\n\n } else if (\n prop.expression.arguments?.[1] &&\n prop.expression.expression.arguments?.[0]\n ) {\n /**\n * Calling `type()` as a regular method\n */\n const property = currentProperty || new Property();\n property.name = prop.expression.arguments[1].text;\n currentStructure.addProperty(property);\n\n const typeArgument = prop.expression.expression.arguments[0];\n defineProperty(property, typeArgument);\n }\n\n\n } else if (\n node.getText() === \"defineTypes\" &&\n (\n node.parent.kind === ts.SyntaxKind.CallExpression ||\n node.parent.kind === ts.SyntaxKind.PropertyAccessExpression\n )\n ) {\n /**\n * JavaScript source file (`.js`)\n * Using `defineTypes()`\n */\n const callExpression = (node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)\n ? node.parent.parent as ts.CallExpression\n : node.parent as ts.CallExpression;\n\n if (callExpression.kind !== ts.SyntaxKind.CallExpression) {\n break;\n }\n\n const className = callExpression.arguments[0].getText()\n currentStructure.name = className;\n\n const types = callExpression.arguments[1] as any;\n for (let i=0; i<types.properties.length; i++) {\n const prop = types.properties[i];\n\n const property = currentProperty || new Property();\n property.name = prop.name.escapedText;\n currentStructure.addProperty(property);\n\n defineProperty(property, prop.initializer);\n }\n\n }\n\n if (node.parent.kind === ts.SyntaxKind.ClassDeclaration) {\n currentStructure.name = node.getText();\n }\n\n currentProperty = undefined;\n\n break;\n\n case ts.SyntaxKind.EnumMember:\n if (currentStructure instanceof Enum) {\n const initializer = (node as any).initializer?.getText();\n const name = node.getFirstToken().getText();\n const property = currentProperty || new Property();\n property.name = name;\n if (initializer !== undefined) {\n property.type = initializer;\n }\n currentStructure.addProperty(property);\n currentProperty = undefined;\n }\n break;\n }\n\n ts.forEachChild(node, (n) => inspectNode(n, context, decoratorName));\n}\n\nlet parsedFiles: { [filename: string]: boolean };\n\nexport function parseFiles(\n fileNames: string[],\n decoratorName: string = \"type\",\n context: Context = new Context()\n) {\n /**\n * Re-set globalContext for each test case\n */\n if (globalContext !== context) {\n parsedFiles = {};\n globalContext = context;\n }\n\n fileNames.forEach((fileName) => {\n let sourceFile: ts.Node;\n let sourceFileName: string;\n\n const fileNameAlternatives = [];\n\n if (\n !fileName.endsWith(\".ts\") &&\n !fileName.endsWith(\".js\") &&\n !fileName.endsWith(\".mjs\")\n ) {\n fileNameAlternatives.push(`${fileName}.ts`);\n fileNameAlternatives.push(`${fileName}/index.ts`);\n\n } else {\n fileNameAlternatives.push(fileName);\n }\n\n for (let i = 0; i < fileNameAlternatives.length; i++) {\n try {\n sourceFileName = path.resolve(fileNameAlternatives[i]);\n\n if (parsedFiles[sourceFileName]) {\n break;\n }\n\n sourceFile = ts.createSourceFile(\n sourceFileName,\n readFileSync(sourceFileName).toString(),\n ts.ScriptTarget.Latest,\n true\n );\n\n parsedFiles[sourceFileName] = true;\n\n break;\n } catch (e) {\n // console.log(`${fileNameAlternatives[i]} => ${e.message}`);\n }\n }\n\n if (sourceFile) {\n inspectNode(sourceFile, context, decoratorName);\n }\n });\n\n return context.getStructures();\n}\n\n/**\n * TypeScript 4.8+ has introduced a change on how to access decorators.\n * - https://github.com/microsoft/TypeScript/pull/49089\n * - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees\n */\nexport function getDecorators(node: ts.Node | null | undefined,): undefined | ts.Decorator[] {\n if (node == undefined) { return undefined; }\n\n // TypeScript 4.7 and below\n // @ts-ignore\n if (node.decorators) { return node.decorators; }\n\n // TypeScript 4.8 and above\n // @ts-ignore\n if (ts.canHaveDecorators && ts.canHaveDecorators(node)) {\n // @ts-ignore\n const decorators = ts.getDecorators(node);\n return decorators ? Array.from(decorators) : undefined;\n }\n\n // @ts-ignore\n return node.modifiers?.filter(ts.isDecorator);\n}\n"]}
|
package/lib/codegen/types.d.ts
CHANGED
|
@@ -2,9 +2,11 @@ export declare function getCommentHeader(singleLineComment?: string): string;
|
|
|
2
2
|
export declare class Context {
|
|
3
3
|
classes: Class[];
|
|
4
4
|
interfaces: Interface[];
|
|
5
|
+
enums: Enum[];
|
|
5
6
|
getStructures(): {
|
|
6
7
|
classes: Class[];
|
|
7
8
|
interfaces: Interface[];
|
|
9
|
+
enums: Enum[];
|
|
8
10
|
};
|
|
9
11
|
addStructure(structure: IStructure): void;
|
|
10
12
|
private getParentClass;
|
|
@@ -30,6 +32,12 @@ export declare class Class implements IStructure {
|
|
|
30
32
|
addProperty(property: Property): void;
|
|
31
33
|
postProcessing(): void;
|
|
32
34
|
}
|
|
35
|
+
export declare class Enum implements IStructure {
|
|
36
|
+
context: Context;
|
|
37
|
+
name: string;
|
|
38
|
+
properties: Property[];
|
|
39
|
+
addProperty(property: Property): void;
|
|
40
|
+
}
|
|
33
41
|
export declare class Property {
|
|
34
42
|
index: number;
|
|
35
43
|
name: string;
|
package/lib/codegen/types.js
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getInheritanceTree = exports.Property = exports.Class = exports.Interface = exports.Context = exports.getCommentHeader = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
exports.getInheritanceTree = exports.Property = exports.Enum = exports.Class = exports.Interface = exports.Context = exports.getCommentHeader = void 0;
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const VERSION = JSON.parse(fs.readFileSync(__dirname + "/../../package.json").toString()).version;
|
|
6
|
+
const COMMENT_HEADER = `
|
|
7
|
+
THIS FILE HAS BEEN GENERATED AUTOMATICALLY
|
|
8
|
+
DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
|
|
9
|
+
|
|
10
|
+
GENERATED USING @colyseus/schema ${VERSION}
|
|
11
|
+
`;
|
|
12
|
+
function getCommentHeader(singleLineComment = "//") {
|
|
13
|
+
return `${COMMENT_HEADER.split("\n").map(line => `${singleLineComment} ${line}`).join("\n")}`;
|
|
10
14
|
}
|
|
11
15
|
exports.getCommentHeader = getCommentHeader;
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
class Context {
|
|
17
|
+
constructor() {
|
|
14
18
|
this.classes = [];
|
|
15
19
|
this.interfaces = [];
|
|
20
|
+
this.enums = [];
|
|
16
21
|
}
|
|
17
|
-
|
|
18
|
-
var _this = this;
|
|
22
|
+
getStructures() {
|
|
19
23
|
return {
|
|
20
|
-
classes: this.classes.filter(
|
|
21
|
-
if (
|
|
24
|
+
classes: this.classes.filter(klass => {
|
|
25
|
+
if (this.isSchemaClass(klass)) {
|
|
22
26
|
return true;
|
|
23
27
|
}
|
|
24
28
|
else {
|
|
25
|
-
|
|
26
|
-
while (parentClass =
|
|
27
|
-
if (
|
|
29
|
+
let parentClass = klass;
|
|
30
|
+
while (parentClass = this.getParentClass(parentClass)) {
|
|
31
|
+
if (this.isSchemaClass(parentClass)) {
|
|
28
32
|
return true;
|
|
29
33
|
}
|
|
30
34
|
}
|
|
@@ -32,9 +36,10 @@ var Context = /** @class */ (function () {
|
|
|
32
36
|
return false;
|
|
33
37
|
}),
|
|
34
38
|
interfaces: this.interfaces,
|
|
39
|
+
enums: this.enums,
|
|
35
40
|
};
|
|
36
|
-
}
|
|
37
|
-
|
|
41
|
+
}
|
|
42
|
+
addStructure(structure) {
|
|
38
43
|
structure.context = this;
|
|
39
44
|
if (structure instanceof Class) {
|
|
40
45
|
this.classes.push(structure);
|
|
@@ -42,13 +47,16 @@ var Context = /** @class */ (function () {
|
|
|
42
47
|
else if (structure instanceof Interface) {
|
|
43
48
|
this.interfaces.push(structure);
|
|
44
49
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
else if (structure instanceof Enum) {
|
|
51
|
+
this.enums.push(structure);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
getParentClass(klass) {
|
|
55
|
+
return this.classes.find(c => c.name === klass.extends);
|
|
56
|
+
}
|
|
57
|
+
isSchemaClass(klass) {
|
|
58
|
+
let isSchema = false;
|
|
59
|
+
let currentClass = klass;
|
|
52
60
|
while (!isSchema && currentClass) {
|
|
53
61
|
//
|
|
54
62
|
// TODO: ideally we should check for actual @colyseus/schema module
|
|
@@ -67,15 +75,14 @@ var Context = /** @class */ (function () {
|
|
|
67
75
|
currentClass = this.getParentClass(currentClass);
|
|
68
76
|
}
|
|
69
77
|
return isSchema;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
}());
|
|
78
|
+
}
|
|
79
|
+
}
|
|
73
80
|
exports.Context = Context;
|
|
74
|
-
|
|
75
|
-
|
|
81
|
+
class Interface {
|
|
82
|
+
constructor() {
|
|
76
83
|
this.properties = [];
|
|
77
84
|
}
|
|
78
|
-
|
|
85
|
+
addProperty(property) {
|
|
79
86
|
if (property.type.indexOf("[]") >= 0) {
|
|
80
87
|
// is array!
|
|
81
88
|
property.childType = property.type.match(/([^\[]+)/i)[1];
|
|
@@ -85,48 +92,51 @@ var Interface = /** @class */ (function () {
|
|
|
85
92
|
else {
|
|
86
93
|
this.properties.push(property);
|
|
87
94
|
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
}());
|
|
95
|
+
}
|
|
96
|
+
}
|
|
91
97
|
exports.Interface = Interface;
|
|
92
|
-
|
|
93
|
-
|
|
98
|
+
class Class {
|
|
99
|
+
constructor() {
|
|
94
100
|
this.properties = [];
|
|
95
101
|
}
|
|
96
|
-
|
|
102
|
+
addProperty(property) {
|
|
97
103
|
property.index = this.properties.length;
|
|
98
104
|
this.properties.push(property);
|
|
99
|
-
}
|
|
100
|
-
|
|
105
|
+
}
|
|
106
|
+
postProcessing() {
|
|
101
107
|
/**
|
|
102
108
|
* Ensure the proprierties `index` are correct using inheritance
|
|
103
109
|
*/
|
|
104
|
-
|
|
110
|
+
let parentKlass = this;
|
|
105
111
|
while (parentKlass &&
|
|
106
|
-
(parentKlass = this.context.classes.find(
|
|
107
|
-
this.properties.forEach(
|
|
112
|
+
(parentKlass = this.context.classes.find(k => k.name === parentKlass.extends))) {
|
|
113
|
+
this.properties.forEach(prop => {
|
|
108
114
|
prop.index += parentKlass.properties.length;
|
|
109
115
|
});
|
|
110
116
|
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
}());
|
|
117
|
+
}
|
|
118
|
+
}
|
|
114
119
|
exports.Class = Class;
|
|
115
|
-
|
|
116
|
-
|
|
120
|
+
class Enum {
|
|
121
|
+
constructor() {
|
|
122
|
+
this.properties = [];
|
|
117
123
|
}
|
|
118
|
-
|
|
119
|
-
|
|
124
|
+
addProperty(property) {
|
|
125
|
+
this.properties.push(property);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
exports.Enum = Enum;
|
|
129
|
+
class Property {
|
|
130
|
+
}
|
|
120
131
|
exports.Property = Property;
|
|
121
|
-
function getInheritanceTree(klass, allClasses, includeSelf) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
var inheritanceTree = [];
|
|
132
|
+
function getInheritanceTree(klass, allClasses, includeSelf = true) {
|
|
133
|
+
let currentClass = klass;
|
|
134
|
+
let inheritanceTree = [];
|
|
125
135
|
if (includeSelf) {
|
|
126
136
|
inheritanceTree.push(currentClass);
|
|
127
137
|
}
|
|
128
138
|
while (currentClass.extends !== "Schema") {
|
|
129
|
-
currentClass = allClasses.find(
|
|
139
|
+
currentClass = allClasses.find(klass => klass.name == currentClass.extends);
|
|
130
140
|
inheritanceTree.push(currentClass);
|
|
131
141
|
}
|
|
132
142
|
return inheritanceTree;
|
package/lib/codegen/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/codegen/types.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/codegen/types.ts"],"names":[],"mappings":";;;AAAA,yBAAyB;AAEzB,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,SAAS,GAAG,qBAAqB,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC;AAClG,MAAM,cAAc,GAAG;;;;mCAIY,OAAO;CACzC,CAAC;AAEF,SAAgB,gBAAgB,CAAC,oBAA4B,IAAI;IAC7D,OAAO,GAAG,cAAc,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,iBAAiB,IAAI,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;AAClG,CAAC;AAFD,4CAEC;AAED,MAAa,OAAO;IAApB;QACI,YAAO,GAAY,EAAE,CAAC;QACtB,eAAU,GAAgB,EAAE,CAAC;QAC7B,UAAK,GAAW,EAAE,CAAC;IAmEvB,CAAC;IAjEG,aAAa;QACT,OAAO;YACH,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;gBACjC,IAAI,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE;oBAC3B,OAAO,IAAI,CAAC;iBAEf;qBAAM;oBACH,IAAI,WAAW,GAAG,KAAK,CAAC;oBACxB,OAAO,WAAW,GAAG,IAAI,CAAC,cAAc,CAAC,WAAW,CAAC,EAAE;wBACnD,IAAI,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,EAAE;4BACjC,OAAO,IAAI,CAAC;yBACf;qBACJ;iBACJ;gBACD,OAAO,KAAK,CAAC;YACjB,CAAC,CAAC;YACF,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,KAAK,EAA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AC1B,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACnC,CAAC;CACJ;AARD,oBAQC;AAED,MAAa,QAAQ;CAMpB;AAND,4BAMC;AAOD,SAAgB,kBAAkB,CAAC,KAAY,EAAE,UAAmB,EAAE,cAAuB,IAAI;IAC7F,IAAI,YAAY,GAAG,KAAK,CAAC;IACzB,IAAI,eAAe,GAAY,EAAE,CAAC;IAElC,IAAI,WAAW,EAAE;QACb,eAAe,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;KACtC;IAED,OAAO,YAAY,CAAC,OAAO,KAAK,QAAQ,EAAE;QACtC,YAAY,GAAG,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,IAAI,YAAY,CAAC,OAAO,CAAC,CAAC;QAC5E,eAAe,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;KACtC;IAED,OAAO,eAAe,CAAC;AAC3B,CAAC;AAdD,gDAcC","sourcesContent":["import * as fs from \"fs\";\n\nconst VERSION = JSON.parse(fs.readFileSync(__dirname + \"/../../package.json\").toString()).version;\nconst COMMENT_HEADER = `\nTHIS FILE HAS BEEN GENERATED AUTOMATICALLY\nDO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING\n\nGENERATED USING @colyseus/schema ${VERSION}\n`;\n\nexport function getCommentHeader(singleLineComment: string = \"//\") {\n return `${COMMENT_HEADER.split(\"\\n\").map(line => `${singleLineComment} ${line}`).join(\"\\n\")}`;\n}\n\nexport class Context {\n classes: Class[] = [];\n interfaces: Interface[] = [];\n enums: Enum[] = [];\n\n getStructures() {\n return {\n classes: this.classes.filter(klass => {\n if (this.isSchemaClass(klass)) {\n return true;\n\n } else {\n let parentClass = klass;\n while (parentClass = this.getParentClass(parentClass)) {\n if (this.isSchemaClass(parentClass)) {\n return true;\n }\n }\n }\n return false;\n }),\n interfaces: this.interfaces,\n enums: this.enums,\n };\n }\n\n addStructure(structure: IStructure) {\n structure.context = this;\n\n if (structure instanceof Class) {\n this.classes.push(structure);\n } else if (structure instanceof Interface) {\n this.interfaces.push(structure);\n } else if (structure instanceof Enum) {\n this.enums.push(structure);\n }\n }\n\n private getParentClass(klass: Class) {\n return this.classes.find(c => c.name === klass.extends);\n }\n\n private isSchemaClass(klass: Class) {\n let isSchema: boolean = false;\n\n let currentClass = klass;\n while (!isSchema && currentClass) {\n //\n // TODO: ideally we should check for actual @colyseus/schema module\n // reference rather than arbitrary strings.\n //\n isSchema = (\n currentClass.extends === \"Schema\" ||\n currentClass.extends === \"schema.Schema\" ||\n currentClass.extends === \"Schema.Schema\"\n );\n\n //\n // When extending from `schema.Schema`, it is required to\n // normalize as \"Schema\" for code generation.\n //\n if (currentClass === klass && isSchema) {\n klass.extends = \"Schema\";\n }\n\n currentClass = this.getParentClass(currentClass);\n }\n\n return isSchema;\n }\n}\n\nexport interface IStructure {\n context: Context;\n name: string;\n properties: Property[];\n addProperty(property: Property);\n}\n\nexport class Interface implements IStructure {\n context: Context;\n name: string;\n properties: Property[] = [];\n\n addProperty(property: Property) {\n if (property.type.indexOf(\"[]\") >= 0) {\n // is array!\n property.childType = property.type.match(/([^\\[]+)/i)[1];\n property.type = \"array\";\n this.properties.push(property);\n\n } else {\n this.properties.push(property);\n }\n }\n}\n\nexport class Class implements IStructure {\n context: Context;\n name: string;\n properties: Property[] = [];\n extends: string;\n\n addProperty(property: Property) {\n property.index = this.properties.length;\n this.properties.push(property);\n }\n\n postProcessing() {\n /**\n * Ensure the proprierties `index` are correct using inheritance\n */\n let parentKlass: Class = this;\n\n while (\n parentKlass &&\n (parentKlass = this.context.classes.find(k => k.name === parentKlass.extends))\n ) {\n this.properties.forEach(prop => {\n prop.index += parentKlass.properties.length;\n });\n }\n }\n}\n\nexport class Enum implements IStructure {\n context: Context;\n name: string;\n properties: Property[] = [];\n\n addProperty(property: Property) {\n this.properties.push(property);\n }\n}\n\nexport class Property {\n index: number;\n name: string;\n type: string;\n childType: string;\n deprecated?: boolean;\n}\n\nexport interface File {\n name: string\n content: string;\n}\n\nexport function getInheritanceTree(klass: Class, allClasses: Class[], includeSelf: boolean = true) {\n let currentClass = klass;\n let inheritanceTree: Class[] = [];\n\n if (includeSelf) {\n inheritanceTree.push(currentClass);\n }\n\n while (currentClass.extends !== \"Schema\") {\n currentClass = allClasses.find(klass => klass.name == currentClass.extends);\n inheritanceTree.push(currentClass);\n }\n\n return inheritanceTree;\n}\n"]}
|