@colyseus/schema 4.0.1 → 4.0.3
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/{lib → build}/Metadata.d.ts +2 -2
- package/{lib → build}/Reflection.d.ts +13 -13
- package/{lib → build}/Schema.d.ts +10 -10
- package/{lib → build}/annotations.d.ts +10 -10
- package/build/codegen/languages/cpp.d.ts +3 -0
- package/build/codegen/languages/csharp.d.ts +4 -0
- package/build/codegen/languages/haxe.d.ts +3 -0
- package/{lib → build}/codegen/languages/java.d.ts +2 -2
- package/build/codegen/languages/js.d.ts +3 -0
- package/build/codegen/languages/lua.d.ts +3 -0
- package/build/codegen/languages/ts.d.ts +3 -0
- package/{lib → build}/codegen/parser.d.ts +1 -1
- package/{lib → build}/decoder/DecodeOperation.d.ts +5 -5
- package/{lib → build}/decoder/Decoder.d.ts +7 -7
- package/{lib → build}/decoder/ReferenceTracker.d.ts +1 -1
- package/{lib → build}/decoder/strategy/Callbacks.d.ts +11 -11
- package/{lib → build}/decoder/strategy/RawChanges.d.ts +2 -2
- package/{lib → build}/decoder/strategy/getDecoderStateCallbacks.d.ts +3 -3
- package/{lib → build}/encoder/ChangeTree.d.ts +11 -11
- package/{lib → build}/encoder/EncodeOperation.d.ts +5 -5
- package/{lib → build}/encoder/Encoder.d.ts +6 -6
- package/{lib → build}/encoder/Root.d.ts +2 -2
- package/{lib → build}/encoder/StateView.d.ts +1 -1
- package/{lib → build}/encoding/assert.d.ts +6 -6
- package/{lib → build}/encoding/decode.d.ts +1 -1
- package/{lib → build}/encoding/encode.d.ts +1 -1
- package/build/{cjs/index.js → index.cjs} +1 -1
- package/build/index.cjs.map +1 -0
- package/build/index.d.ts +32 -0
- package/build/index.mjs.map +1 -0
- package/{lib → build}/types/HelperTypes.d.ts +6 -6
- package/{lib → build}/types/TypeContext.d.ts +1 -1
- package/{lib → build}/types/custom/ArraySchema.d.ts +8 -8
- package/{lib → build}/types/custom/CollectionSchema.d.ts +7 -7
- package/{lib → build}/types/custom/MapSchema.d.ts +7 -7
- package/{lib → build}/types/custom/SetSchema.d.ts +7 -7
- package/{lib → build}/types/registry.d.ts +2 -2
- package/{lib → build}/utils.d.ts +1 -1
- package/package.json +15 -14
- package/src/Metadata.ts +5 -5
- package/src/Reflection.ts +7 -7
- package/src/Schema.ts +11 -11
- package/src/annotations.ts +13 -13
- package/src/codegen/api.ts +10 -3
- package/src/codegen/cli.ts +2 -2
- package/src/codegen/languages/cpp.ts +2 -2
- package/src/codegen/languages/csharp.ts +3 -3
- package/src/codegen/languages/haxe.ts +2 -2
- package/src/codegen/languages/java.ts +2 -2
- package/src/codegen/languages/js.ts +2 -2
- package/src/codegen/languages/lua.ts +2 -2
- package/src/codegen/languages/ts.ts +2 -2
- package/src/codegen/parser.ts +7 -2
- package/src/codegen/types.ts +5 -0
- package/src/decoder/DecodeOperation.ts +14 -14
- package/src/decoder/Decoder.ts +11 -11
- package/src/decoder/ReferenceTracker.ts +8 -8
- package/src/decoder/strategy/Callbacks.ts +12 -12
- package/src/decoder/strategy/RawChanges.ts +2 -2
- package/src/decoder/strategy/getDecoderStateCallbacks.ts +10 -10
- package/src/encoder/ChangeTree.ts +13 -13
- package/src/encoder/EncodeOperation.ts +9 -9
- package/src/encoder/Encoder.ts +10 -10
- package/src/encoder/Root.ts +4 -4
- package/src/encoder/StateView.ts +7 -7
- package/src/encoding/assert.ts +6 -6
- package/src/encoding/decode.ts +1 -1
- package/src/encoding/encode.ts +1 -1
- package/src/index.ts +27 -27
- package/src/types/HelperTypes.ts +6 -6
- package/src/types/TypeContext.ts +3 -3
- package/src/types/custom/ArraySchema.ts +11 -11
- package/src/types/custom/CollectionSchema.ts +9 -9
- package/src/types/custom/MapSchema.ts +10 -10
- package/src/types/custom/SetSchema.ts +9 -9
- package/src/types/registry.ts +3 -3
- package/src/utils.ts +3 -3
- package/src/v3_bench.ts +1 -1
- package/build/cjs/index.js.map +0 -1
- package/build/esm/index.mjs.map +0 -1
- package/lib/Metadata.js +0 -260
- package/lib/Metadata.js.map +0 -1
- package/lib/Reflection.js +0 -182
- package/lib/Reflection.js.map +0 -1
- package/lib/Schema.js +0 -356
- package/lib/Schema.js.map +0 -1
- package/lib/annotations.js +0 -484
- package/lib/annotations.js.map +0 -1
- package/lib/bench_encode.d.ts +0 -1
- package/lib/bench_encode.js +0 -126
- package/lib/bench_encode.js.map +0 -1
- package/lib/codegen/api.js +0 -87
- package/lib/codegen/api.js.map +0 -1
- package/lib/codegen/argv.js +0 -41
- package/lib/codegen/argv.js.map +0 -1
- package/lib/codegen/cli.js +0 -63
- package/lib/codegen/cli.js.map +0 -1
- package/lib/codegen/languages/cpp.d.ts +0 -3
- package/lib/codegen/languages/cpp.js +0 -261
- package/lib/codegen/languages/cpp.js.map +0 -1
- package/lib/codegen/languages/csharp.d.ts +0 -4
- package/lib/codegen/languages/csharp.js +0 -157
- package/lib/codegen/languages/csharp.js.map +0 -1
- package/lib/codegen/languages/haxe.d.ts +0 -3
- package/lib/codegen/languages/haxe.js +0 -103
- package/lib/codegen/languages/haxe.js.map +0 -1
- package/lib/codegen/languages/java.js +0 -103
- package/lib/codegen/languages/java.js.map +0 -1
- package/lib/codegen/languages/js.d.ts +0 -3
- package/lib/codegen/languages/js.js +0 -104
- package/lib/codegen/languages/js.js.map +0 -1
- package/lib/codegen/languages/lua.d.ts +0 -3
- package/lib/codegen/languages/lua.js +0 -106
- package/lib/codegen/languages/lua.js.map +0 -1
- package/lib/codegen/languages/ts.d.ts +0 -3
- package/lib/codegen/languages/ts.js +0 -119
- package/lib/codegen/languages/ts.js.map +0 -1
- package/lib/codegen/parser.js +0 -364
- package/lib/codegen/parser.js.map +0 -1
- package/lib/codegen/types.js +0 -181
- package/lib/codegen/types.js.map +0 -1
- package/lib/decoder/DecodeOperation.js +0 -255
- package/lib/decoder/DecodeOperation.js.map +0 -1
- package/lib/decoder/Decoder.js +0 -118
- package/lib/decoder/Decoder.js.map +0 -1
- package/lib/decoder/ReferenceTracker.js +0 -139
- package/lib/decoder/ReferenceTracker.js.map +0 -1
- package/lib/decoder/strategy/Callbacks.js +0 -340
- package/lib/decoder/strategy/Callbacks.js.map +0 -1
- package/lib/decoder/strategy/RawChanges.js +0 -7
- package/lib/decoder/strategy/RawChanges.js.map +0 -1
- package/lib/decoder/strategy/getDecoderStateCallbacks.js +0 -277
- package/lib/decoder/strategy/getDecoderStateCallbacks.js.map +0 -1
- package/lib/encoder/ChangeTree.js +0 -542
- package/lib/encoder/ChangeTree.js.map +0 -1
- package/lib/encoder/EncodeOperation.js +0 -139
- package/lib/encoder/EncodeOperation.js.map +0 -1
- package/lib/encoder/Encoder.js +0 -208
- package/lib/encoder/Encoder.js.map +0 -1
- package/lib/encoder/Root.js +0 -233
- package/lib/encoder/Root.js.map +0 -1
- package/lib/encoder/StateView.js +0 -284
- package/lib/encoder/StateView.js.map +0 -1
- package/lib/encoding/assert.js +0 -55
- package/lib/encoding/assert.js.map +0 -1
- package/lib/encoding/decode.js +0 -221
- package/lib/encoding/decode.js.map +0 -1
- package/lib/encoding/encode.js +0 -282
- package/lib/encoding/encode.js.map +0 -1
- package/lib/encoding/spec.js +0 -29
- package/lib/encoding/spec.js.map +0 -1
- package/lib/index.d.ts +0 -32
- package/lib/index.js +0 -81
- package/lib/index.js.map +0 -1
- package/lib/symbol.shim.js +0 -4
- package/lib/symbol.shim.js.map +0 -1
- package/lib/types/HelperTypes.js +0 -3
- package/lib/types/HelperTypes.js.map +0 -1
- package/lib/types/TypeContext.js +0 -147
- package/lib/types/TypeContext.js.map +0 -1
- package/lib/types/custom/ArraySchema.js +0 -737
- package/lib/types/custom/ArraySchema.js.map +0 -1
- package/lib/types/custom/CollectionSchema.js +0 -174
- package/lib/types/custom/CollectionSchema.js.map +0 -1
- package/lib/types/custom/MapSchema.js +0 -226
- package/lib/types/custom/MapSchema.js.map +0 -1
- package/lib/types/custom/SetSchema.js +0 -182
- package/lib/types/custom/SetSchema.js.map +0 -1
- package/lib/types/registry.js +0 -37
- package/lib/types/registry.js.map +0 -1
- package/lib/types/symbols.js +0 -37
- package/lib/types/symbols.js.map +0 -1
- package/lib/types/utils.js +0 -16
- package/lib/types/utils.js.map +0 -1
- package/lib/utils.js +0 -54
- package/lib/utils.js.map +0 -1
- package/lib/v3_bench.js +0 -130
- package/lib/v3_bench.js.map +0 -1
- package/src/bench_encode.ts +0 -64
- /package/{lib → build}/codegen/api.d.ts +0 -0
- /package/{lib → build}/codegen/argv.d.ts +0 -0
- /package/{lib → build}/codegen/cli.d.ts +0 -0
- /package/{lib → build}/codegen/types.d.ts +0 -0
- /package/{lib → build}/encoding/spec.d.ts +0 -0
- /package/build/{umd/index.js → index.js} +0 -0
- /package/build/{esm/index.mjs → index.mjs} +0 -0
- /package/{lib → build}/symbol.shim.d.ts +0 -0
- /package/{lib → build}/types/symbols.d.ts +0 -0
- /package/{lib → build}/types/utils.d.ts +0 -0
- /package/{lib → build}/v3_bench.d.ts +0 -0
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generate = generate;
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
/**
|
|
6
|
-
TODO:
|
|
7
|
-
- Support inheritance
|
|
8
|
-
- Support importing Schema dependencies
|
|
9
|
-
*/
|
|
10
|
-
const typeMaps = {
|
|
11
|
-
"string": "string",
|
|
12
|
-
"number": "number",
|
|
13
|
-
"boolean": "boolean",
|
|
14
|
-
"int8": "number",
|
|
15
|
-
"uint8": "number",
|
|
16
|
-
"int16": "number",
|
|
17
|
-
"uint16": "number",
|
|
18
|
-
"int32": "number",
|
|
19
|
-
"uint32": "number",
|
|
20
|
-
"int64": "number",
|
|
21
|
-
"uint64": "number",
|
|
22
|
-
"float32": "number",
|
|
23
|
-
"float64": "number",
|
|
24
|
-
};
|
|
25
|
-
const distinct = (value, index, self) => self.indexOf(value) === index;
|
|
26
|
-
function generate(context, options) {
|
|
27
|
-
return context.classes.map(klass => ({
|
|
28
|
-
name: klass.name + ".lua",
|
|
29
|
-
content: generateClass(klass, options.namespace, context.classes)
|
|
30
|
-
}));
|
|
31
|
-
}
|
|
32
|
-
function generateClass(klass, namespace, allClasses) {
|
|
33
|
-
const allRefs = [];
|
|
34
|
-
klass.properties.forEach(property => {
|
|
35
|
-
let type = property.type;
|
|
36
|
-
// keep all refs list
|
|
37
|
-
if ((type === "ref" || type === "array" || type === "map")) {
|
|
38
|
-
allRefs.push(property);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
// Inheritance support
|
|
42
|
-
const inherits = (klass.extends !== "Schema")
|
|
43
|
-
? `, ${klass.extends}`
|
|
44
|
-
: "";
|
|
45
|
-
return `${(0, types_1.getCommentHeader)().replace(/\/\//mg, "--")}
|
|
46
|
-
|
|
47
|
-
local schema = require 'colyseus.serializer.schema.schema'
|
|
48
|
-
${allRefs.
|
|
49
|
-
filter(ref => ref.childType && typeMaps[ref.childType] === undefined).
|
|
50
|
-
map(ref => ref.childType).
|
|
51
|
-
concat((0, types_1.getInheritanceTree)(klass, allClasses, false).map(klass => klass.name)).
|
|
52
|
-
filter(distinct).
|
|
53
|
-
map(childType => `local ${childType} = require '${(namespace ? `${namespace}.` : '')}${childType}'`).
|
|
54
|
-
join("\n")}
|
|
55
|
-
|
|
56
|
-
---@class ${klass.name}: ${klass.extends}
|
|
57
|
-
${klass.properties.map(prop => `---@field ${prop.name} ${getLUATypeAnnotation(prop)}`).join("\n")}
|
|
58
|
-
local ${klass.name} = schema.define({
|
|
59
|
-
${klass.properties.map(prop => generatePropertyDeclaration(prop)).join(",\n")},
|
|
60
|
-
["_fields_by_index"] = { ${klass.properties.map(prop => `"${prop.name}"`).join(", ")} },
|
|
61
|
-
}${inherits})
|
|
62
|
-
|
|
63
|
-
return ${klass.name}
|
|
64
|
-
`;
|
|
65
|
-
}
|
|
66
|
-
function generatePropertyDeclaration(prop) {
|
|
67
|
-
let typeArgs;
|
|
68
|
-
if (prop.childType) {
|
|
69
|
-
const isUpcaseFirst = prop.childType.match(/^[A-Z]/);
|
|
70
|
-
if (isUpcaseFirst) {
|
|
71
|
-
typeArgs += `${prop.childType}`;
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
typeArgs += `"${prop.childType}"`;
|
|
75
|
-
}
|
|
76
|
-
if (prop.type === "ref") {
|
|
77
|
-
typeArgs = (isUpcaseFirst)
|
|
78
|
-
? `${prop.childType}`
|
|
79
|
-
: `"${prop.childType}"`;
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
typeArgs = (isUpcaseFirst)
|
|
83
|
-
? `{ ${prop.type} = ${prop.childType} }`
|
|
84
|
-
: `{ ${prop.type} = "${prop.childType}" }`;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
typeArgs = `"${prop.type}"`;
|
|
89
|
-
}
|
|
90
|
-
return ` ["${prop.name}"] = ${typeArgs}`;
|
|
91
|
-
}
|
|
92
|
-
function getLUATypeAnnotation(prop) {
|
|
93
|
-
if (prop.type === "ref") {
|
|
94
|
-
return prop.childType;
|
|
95
|
-
}
|
|
96
|
-
else if (prop.type === "array") {
|
|
97
|
-
return "ArraySchema";
|
|
98
|
-
}
|
|
99
|
-
else if (prop.type === "map") {
|
|
100
|
-
return "MapSchema";
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
return typeMaps[prop.type];
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
//# sourceMappingURL=lua.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lua.js","sourceRoot":"","sources":["../../../src/codegen/languages/lua.ts"],"names":[],"mappings":";;AA4BA,4BAKC;AAjCD,oCAAgG;AAGhG;;;;EAIE;AAEF,MAAM,QAAQ,GAA8B;IACxC,QAAQ,EAAE,QAAQ;IAClB,QAAQ,EAAE,QAAQ;IAClB,SAAS,EAAE,SAAS;IACpB,MAAM,EAAE,QAAQ;IAChB,O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W,CAAC;IAEvB,CAAC;SAAM,CAAC;QACJ,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;AACL,CAAC","sourcesContent":["import { Class, Property, File, getCommentHeader, getInheritanceTree, Context } from \"../types\";\nimport { GenerateOptions } from \"../api\";\n\n/**\n TODO:\n - Support inheritance\n - Support importing Schema dependencies\n*/\n\nconst typeMaps: { [key: string]: string } = {\n \"string\": \"string\",\n \"number\": \"number\",\n \"boolean\": \"boolean\",\n \"int8\": \"number\",\n \"uint8\": \"number\",\n \"int16\": \"number\",\n \"uint16\": \"number\",\n \"int32\": \"number\",\n \"uint32\": \"number\",\n \"int64\": \"number\",\n \"uint64\": \"number\",\n \"float32\": \"number\",\n \"float64\": \"number\",\n}\n\nconst distinct = (value: string, index: number, self: string[]) =>\n self.indexOf(value) === index;\n\nexport function generate (context: Context, options: GenerateOptions): File[] {\n return context.classes.map(klass => ({\n name: klass.name + \".lua\",\n content: generateClass(klass, options.namespace, context.classes)\n }));\n}\n\nfunction generateClass(klass: Class, namespace: string, allClasses: Class[]) {\n const allRefs: Property[] = [];\n klass.properties.forEach(property => {\n let type = property.type;\n\n // keep all refs list\n if ((type === \"ref\" || type === \"array\" || type === \"map\")) {\n allRefs.push(property);\n }\n });\n\n // Inheritance support\n const inherits = (klass.extends !== \"Schema\")\n ? `, ${klass.extends}`\n : \"\";\n\n return `${getCommentHeader().replace(/\\/\\//mg, \"--\")}\n\nlocal schema = require 'colyseus.serializer.schema.schema'\n${allRefs.\n filter(ref => ref.childType && typeMaps[ref.childType] === undefined).\n map(ref => ref.childType).\n concat(getInheritanceTree(klass, allClasses, false).map(klass => klass.name)).\n filter(distinct).\n map(childType => `local ${childType} = require '${(namespace ? `${namespace}.` : '')}${childType}'`).\n join(\"\\n\")}\n\n---@class ${klass.name}: ${klass.extends}\n${klass.properties.map(prop => `---@field ${prop.name} ${getLUATypeAnnotation(prop)}`).join(\"\\n\")}\nlocal ${klass.name} = schema.define({\n${klass.properties.map(prop => generatePropertyDeclaration(prop)).join(\",\\n\")},\n [\"_fields_by_index\"] = { ${klass.properties.map(prop => `\"${prop.name}\"`).join(\", \")} },\n}${inherits})\n\nreturn ${klass.name}\n`;\n}\n\nfunction generatePropertyDeclaration(prop: Property) {\n let typeArgs: string;\n\n if (prop.childType) {\n const isUpcaseFirst = prop.childType.match(/^[A-Z]/);\n\n if (isUpcaseFirst) {\n typeArgs += `${prop.childType}`;\n\n } else {\n typeArgs += `\"${prop.childType}\"`;\n }\n\n if(prop.type === \"ref\") {\n typeArgs = (isUpcaseFirst)\n ? `${prop.childType}`\n : `\"${prop.childType}\"`;\n\n } else {\n typeArgs = (isUpcaseFirst)\n ? `{ ${prop.type} = ${prop.childType} }`\n : `{ ${prop.type} = \"${prop.childType}\" }`;\n }\n\n } else {\n typeArgs = `\"${prop.type}\"`;\n }\n\n return ` [\"${prop.name}\"] = ${typeArgs}`;\n}\n\nfunction getLUATypeAnnotation(prop: Property) {\n if (prop.type === \"ref\") {\n return prop.childType;\n\n } else if (prop.type === \"array\") {\n return \"ArraySchema\";\n\n } else if (prop.type === \"map\") {\n return \"MapSchema\";\n\n } else {\n return typeMaps[prop.type];\n }\n}"]}
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generate = generate;
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
const typeMaps = {
|
|
6
|
-
"string": "string",
|
|
7
|
-
"number": "number",
|
|
8
|
-
"boolean": "boolean",
|
|
9
|
-
"int8": "number",
|
|
10
|
-
"uint8": "number",
|
|
11
|
-
"int16": "number",
|
|
12
|
-
"uint16": "number",
|
|
13
|
-
"int32": "number",
|
|
14
|
-
"uint32": "number",
|
|
15
|
-
"int64": "number",
|
|
16
|
-
"uint64": "number",
|
|
17
|
-
"float32": "number",
|
|
18
|
-
"float64": "number",
|
|
19
|
-
};
|
|
20
|
-
const distinct = (value, index, self) => self.indexOf(value) === index;
|
|
21
|
-
function generate(context, options) {
|
|
22
|
-
return [
|
|
23
|
-
...context.classes.map(structure => ({
|
|
24
|
-
name: structure.name + ".ts",
|
|
25
|
-
content: generateClass(structure, options.namespace, context.classes)
|
|
26
|
-
})),
|
|
27
|
-
...context.interfaces.map(structure => ({
|
|
28
|
-
name: structure.name + ".ts",
|
|
29
|
-
content: generateInterface(structure, options.namespace, context.classes),
|
|
30
|
-
}))
|
|
31
|
-
];
|
|
32
|
-
}
|
|
33
|
-
function generateClass(klass, namespace, allClasses) {
|
|
34
|
-
const allRefs = [];
|
|
35
|
-
klass.properties.forEach(property => {
|
|
36
|
-
let type = property.type;
|
|
37
|
-
// keep all refs list
|
|
38
|
-
if ((type === "ref" || type === "array" || type === "map" || type === "set")) {
|
|
39
|
-
allRefs.push(property);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
return `${(0, types_1.getCommentHeader)()}
|
|
43
|
-
|
|
44
|
-
import { Schema, type, ArraySchema, MapSchema, SetSchema, DataChange } from '@colyseus/schema';
|
|
45
|
-
${allRefs.
|
|
46
|
-
filter(ref => ref.childType && typeMaps[ref.childType] === undefined).
|
|
47
|
-
map(ref => ref.childType).
|
|
48
|
-
concat((0, types_1.getInheritanceTree)(klass, allClasses, false).map(klass => klass.name)).
|
|
49
|
-
filter(distinct).
|
|
50
|
-
map(childType => `import { ${childType} } from './${childType}'`).
|
|
51
|
-
join("\n")}
|
|
52
|
-
|
|
53
|
-
export class ${klass.name} extends ${klass.extends} {
|
|
54
|
-
${klass.properties.map(prop => ` ${generateProperty(prop)}`).join("\n")}
|
|
55
|
-
}
|
|
56
|
-
`;
|
|
57
|
-
}
|
|
58
|
-
function generateProperty(prop) {
|
|
59
|
-
let langType;
|
|
60
|
-
let initializer = "";
|
|
61
|
-
let typeArgs;
|
|
62
|
-
if (prop.childType) {
|
|
63
|
-
const isUpcaseFirst = prop.childType.match(/^[A-Z]/);
|
|
64
|
-
if (isUpcaseFirst) {
|
|
65
|
-
typeArgs += `, ${prop.childType}`;
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
typeArgs += `, "${prop.childType}"`;
|
|
69
|
-
}
|
|
70
|
-
if (prop.type === "ref") {
|
|
71
|
-
langType = `${prop.childType}`;
|
|
72
|
-
initializer = `new ${prop.childType}()`;
|
|
73
|
-
typeArgs = `${prop.childType}`;
|
|
74
|
-
}
|
|
75
|
-
else if (prop.type === "array") {
|
|
76
|
-
langType = (isUpcaseFirst)
|
|
77
|
-
? `ArraySchema<${prop.childType}>`
|
|
78
|
-
: `ArraySchema<${typeMaps[prop.childType]}>`;
|
|
79
|
-
initializer = `new ${langType}()`;
|
|
80
|
-
typeArgs = (isUpcaseFirst)
|
|
81
|
-
? `[ ${prop.childType} ]`
|
|
82
|
-
: `[ "${prop.childType}" ]`;
|
|
83
|
-
}
|
|
84
|
-
else if (prop.type === "map") {
|
|
85
|
-
langType = (isUpcaseFirst)
|
|
86
|
-
? `MapSchema<${prop.childType}>`
|
|
87
|
-
: `MapSchema<${typeMaps[prop.childType]}>`;
|
|
88
|
-
initializer = `new ${langType}()`;
|
|
89
|
-
typeArgs = (isUpcaseFirst)
|
|
90
|
-
? `{ map: ${prop.childType} }`
|
|
91
|
-
: `{ map: "${prop.childType}" }`;
|
|
92
|
-
}
|
|
93
|
-
else if (prop.type === "set") {
|
|
94
|
-
langType = (isUpcaseFirst)
|
|
95
|
-
? `SetSchema<${prop.childType}>`
|
|
96
|
-
: `SetSchema<${typeMaps[prop.childType]}>`;
|
|
97
|
-
initializer = `new ${langType}()`;
|
|
98
|
-
typeArgs = (isUpcaseFirst)
|
|
99
|
-
? `{ set: ${prop.childType} }`
|
|
100
|
-
: `{ set: "${prop.childType}" }`;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
langType = typeMaps[prop.type];
|
|
105
|
-
typeArgs = `"${prop.type}"`;
|
|
106
|
-
}
|
|
107
|
-
// TS1263: "Declarations with initializers cannot also have definite assignment assertions"
|
|
108
|
-
const definiteAssertion = initializer ? "" : "!";
|
|
109
|
-
return `@type(${typeArgs}) public ${prop.name}${definiteAssertion}: ${langType}${(initializer) ? ` = ${initializer}` : ""};`;
|
|
110
|
-
}
|
|
111
|
-
function generateInterface(structure, namespace, allClasses) {
|
|
112
|
-
return `${(0, types_1.getCommentHeader)()}
|
|
113
|
-
|
|
114
|
-
export interface ${structure.name} {
|
|
115
|
-
${structure.properties.map(prop => ` ${prop.name}: ${prop.type};`).join("\n")}
|
|
116
|
-
}
|
|
117
|
-
`;
|
|
118
|
-
}
|
|
119
|
-
//# sourceMappingURL=ts.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ts.js","sourceRoot":"","sources":["../../../src/codegen/languages/ts.ts"],"names":[],"mappings":";;AAsBA,4BAWC;AAjCD,oCAA2G;AAG3G,MAAM,QAAQ,GAA8B;IACxC,QAAQ,EAAE,QAAQ;IAClB,QAAQ,EAAE,QAAQ;IAClB,SAAS,EAAE,SAAS;IACpB,MAAM,EAAE,QAAQ;IAChB,OAAO,EAAE,QAAQ;IACjB,OAAO,EAAE,QAAQ;IACjB,QAAQ,EAAE,QAAQ;IAClB,OAAO,EAAE,QAAQ;IACjB,QAAQ,EAAE,QAAQ;IAClB,OAAO,EAAE,QAAQ;IACjB,QAAQ,EAAE,QAAQ;IAClB,SAAS,EAAE,QAAQ;IACnB,SAAS,EAAE,QAAQ;CACtB,CAAA;AAED,MAAM,QAAQ,GAAG,CAAC,KAAa,EAAE,KAAa,EAAE,IAAc,EAAE,EAAE,CAC9D,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,KAAK,CAAC;AAElC,SAAgB,QAAQ,CAAE,OAAgB,EAAE,OAAwB;IAChE,OAAO;QACH,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;YACjC,IAAI,EAAE,SAAS,CAAC,IAAI,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C,GAAG,CAAC;IAEjD,OAAO,SAAS,QAAQ,YAAY,IAAI,CAAC,IAAI,GAAG,iBAAiB,KAAK,QAAQ,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,MAAM,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,CAAA;AAChI,CAAC;AAGD,SAAS,iBAAiB,CAAC,SAAoB,EAAE,SAAiB,EAAE,UAAmB;IACnF,OAAO,GAAG,IAAA,wBAAgB,GAAE;;mBAEb,SAAS,CAAC,IAAI;EAC/B,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,OAAO,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;;CAE/E,CAAC;AACF,CAAC","sourcesContent":["import { Class, Property, File, getCommentHeader, getInheritanceTree, Context, Interface } from \"../types\";\nimport { GenerateOptions } from \"../api\";\n\nconst typeMaps: { [key: string]: string } = {\n \"string\": \"string\",\n \"number\": \"number\",\n \"boolean\": \"boolean\",\n \"int8\": \"number\",\n \"uint8\": \"number\",\n \"int16\": \"number\",\n \"uint16\": \"number\",\n \"int32\": \"number\",\n \"uint32\": \"number\",\n \"int64\": \"number\",\n \"uint64\": \"number\",\n \"float32\": \"number\",\n \"float64\": \"number\",\n}\n\nconst distinct = (value: string, index: number, self: string[]) =>\n self.indexOf(value) === index;\n\nexport function generate (context: Context, options: GenerateOptions): File[] {\n return [\n ...context.classes.map(structure => ({\n name: structure.name + \".ts\",\n content: generateClass(structure, options.namespace, context.classes)\n })),\n ...context.interfaces.map(structure => ({\n name: structure.name + \".ts\",\n content: generateInterface(structure, options.namespace, context.classes),\n }))\n ];\n}\n\nfunction generateClass(klass: Class, namespace: string, allClasses: Class[]) {\n const allRefs: Property[] = [];\n klass.properties.forEach(property => {\n let type = property.type;\n\n // keep all refs list\n if ((type === \"ref\" || type === \"array\" || type === \"map\" || type === \"set\")) {\n allRefs.push(property);\n }\n });\n\n return `${getCommentHeader()}\n\nimport { Schema, type, ArraySchema, MapSchema, SetSchema, DataChange } from '@colyseus/schema';\n${allRefs.\n filter(ref => ref.childType && typeMaps[ref.childType] === undefined).\n map(ref => ref.childType).\n concat(getInheritanceTree(klass, allClasses, false).map(klass => klass.name)).\n filter(distinct).\n map(childType => `import { ${childType} } from './${childType}'`).\n join(\"\\n\")}\n\nexport class ${klass.name} extends ${klass.extends} {\n${klass.properties.map(prop => ` ${generateProperty(prop)}`).join(\"\\n\")}\n}\n`;\n}\n\nfunction generateProperty(prop: Property) {\n let langType: string;\n let initializer = \"\";\n let typeArgs: string;\n\n if (prop.childType) {\n const isUpcaseFirst = prop.childType.match(/^[A-Z]/);\n\n if (isUpcaseFirst) {\n typeArgs += `, ${prop.childType}`;\n\n } else {\n typeArgs += `, \"${prop.childType}\"`;\n }\n\n if(prop.type === \"ref\") {\n langType = `${prop.childType}`;\n initializer = `new ${prop.childType}()`;\n typeArgs = `${prop.childType}`;\n\n } else if(prop.type === \"array\") {\n langType = (isUpcaseFirst)\n ? `ArraySchema<${prop.childType}>`\n : `ArraySchema<${typeMaps[prop.childType]}>`;\n initializer = `new ${langType}()`;\n typeArgs = (isUpcaseFirst)\n ? `[ ${prop.childType} ]`\n : `[ \"${prop.childType}\" ]`;\n\n } else if(prop.type === \"map\") {\n langType = (isUpcaseFirst)\n ? `MapSchema<${prop.childType}>`\n : `MapSchema<${typeMaps[prop.childType]}>`;\n initializer = `new ${langType}()`;\n typeArgs = (isUpcaseFirst)\n ? `{ map: ${prop.childType} }`\n : `{ map: \"${prop.childType}\" }`;\n } else if (prop.type === \"set\") {\n langType = (isUpcaseFirst)\n ? `SetSchema<${prop.childType}>`\n : `SetSchema<${typeMaps[prop.childType]}>`;\n initializer = `new ${langType}()`;\n typeArgs = (isUpcaseFirst)\n ? `{ set: ${prop.childType} }`\n : `{ set: \"${prop.childType}\" }`;\n }\n\n } else {\n langType = typeMaps[prop.type];\n typeArgs = `\"${prop.type}\"`;\n }\n\n // TS1263: \"Declarations with initializers cannot also have definite assignment assertions\"\n const definiteAssertion = initializer ? \"\" : \"!\";\n\n return `@type(${typeArgs}) public ${prop.name}${definiteAssertion}: ${langType}${(initializer) ? ` = ${initializer}` : \"\"};`\n}\n\n\nfunction generateInterface(structure: Interface, namespace: string, allClasses: Class[]) {\n return `${getCommentHeader()}\n\nexport interface ${structure.name} {\n${structure.properties.map(prop => ` ${prop.name}: ${prop.type};`).join(\"\\n\")}\n}\n`;\n}\n"]}
|
package/lib/codegen/parser.js
DELETED
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.parseFiles = parseFiles;
|
|
37
|
-
exports.getDecorators = getDecorators;
|
|
38
|
-
const ts = __importStar(require("typescript"));
|
|
39
|
-
const path = __importStar(require("path"));
|
|
40
|
-
const fs_1 = require("fs");
|
|
41
|
-
const types_1 = require("./types");
|
|
42
|
-
let currentStructure;
|
|
43
|
-
let currentProperty;
|
|
44
|
-
let globalContext;
|
|
45
|
-
function defineProperty(property, initializer) {
|
|
46
|
-
if (ts.isIdentifier(initializer)) {
|
|
47
|
-
property.type = "ref";
|
|
48
|
-
property.childType = initializer.text;
|
|
49
|
-
}
|
|
50
|
-
else if (initializer.kind == ts.SyntaxKind.ObjectLiteralExpression) {
|
|
51
|
-
property.type = initializer.properties[0].name.text;
|
|
52
|
-
property.childType = initializer.properties[0].initializer.text;
|
|
53
|
-
}
|
|
54
|
-
else if (initializer.kind == ts.SyntaxKind.ArrayLiteralExpression) {
|
|
55
|
-
property.type = "array";
|
|
56
|
-
property.childType = initializer.elements[0].text;
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
property.type = initializer.text;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
function inspectNode(node, context, decoratorName) {
|
|
63
|
-
switch (node.kind) {
|
|
64
|
-
case ts.SyntaxKind.ImportClause:
|
|
65
|
-
const specifier = node.parent.moduleSpecifier;
|
|
66
|
-
if (specifier && specifier.text.startsWith('.')) {
|
|
67
|
-
const currentDir = path.dirname(node.getSourceFile().fileName);
|
|
68
|
-
const pathToImport = path.resolve(currentDir, specifier.text);
|
|
69
|
-
parseFiles([pathToImport], decoratorName, globalContext);
|
|
70
|
-
}
|
|
71
|
-
break;
|
|
72
|
-
case ts.SyntaxKind.ClassDeclaration:
|
|
73
|
-
currentStructure = new types_1.Class();
|
|
74
|
-
const heritageClauses = node.heritageClauses;
|
|
75
|
-
if (heritageClauses && heritageClauses.length > 0) {
|
|
76
|
-
currentStructure.extends = heritageClauses[0].types[0].expression.getText();
|
|
77
|
-
}
|
|
78
|
-
context.addStructure(currentStructure);
|
|
79
|
-
break;
|
|
80
|
-
case ts.SyntaxKind.InterfaceDeclaration:
|
|
81
|
-
//
|
|
82
|
-
// Only generate Interfaces if it has "Message" on its name.
|
|
83
|
-
// Example: MyMessage
|
|
84
|
-
//
|
|
85
|
-
const interfaceName = node.name.escapedText.toString();
|
|
86
|
-
if (interfaceName.indexOf("Message") !== -1) {
|
|
87
|
-
currentStructure = new types_1.Interface();
|
|
88
|
-
currentStructure.name = interfaceName;
|
|
89
|
-
context.addStructure(currentStructure);
|
|
90
|
-
}
|
|
91
|
-
break;
|
|
92
|
-
case ts.SyntaxKind.EnumDeclaration:
|
|
93
|
-
const enumName = node.name.escapedText.toString();
|
|
94
|
-
currentStructure = new types_1.Enum();
|
|
95
|
-
currentStructure.name = enumName;
|
|
96
|
-
context.addStructure(currentStructure);
|
|
97
|
-
break;
|
|
98
|
-
case ts.SyntaxKind.ExtendsKeyword:
|
|
99
|
-
// console.log(node.getText());
|
|
100
|
-
break;
|
|
101
|
-
case ts.SyntaxKind.PropertySignature:
|
|
102
|
-
if (currentStructure instanceof types_1.Interface) {
|
|
103
|
-
const interfaceDeclaration = node.parent;
|
|
104
|
-
if (currentStructure.name !== interfaceDeclaration.name.escapedText.toString()) {
|
|
105
|
-
// skip if property if for a another interface than the one we're interested in.
|
|
106
|
-
break;
|
|
107
|
-
}
|
|
108
|
-
// define a property of an interface
|
|
109
|
-
const property = new types_1.Property();
|
|
110
|
-
property.name = node.name.escapedText.toString();
|
|
111
|
-
property.type = node.type.getText();
|
|
112
|
-
currentStructure.addProperty(property);
|
|
113
|
-
}
|
|
114
|
-
break;
|
|
115
|
-
case ts.SyntaxKind.Identifier:
|
|
116
|
-
if (node.getText() === "deprecated" &&
|
|
117
|
-
node.parent.kind !== ts.SyntaxKind.ImportSpecifier) {
|
|
118
|
-
currentProperty = new types_1.Property();
|
|
119
|
-
currentProperty.deprecated = true;
|
|
120
|
-
break;
|
|
121
|
-
}
|
|
122
|
-
if (node.getText() === decoratorName) {
|
|
123
|
-
const prop = node.parent?.parent?.parent;
|
|
124
|
-
const propDecorator = getDecorators(prop);
|
|
125
|
-
const hasExpression = prop?.expression?.arguments;
|
|
126
|
-
const hasDecorator = (propDecorator?.length > 0);
|
|
127
|
-
/**
|
|
128
|
-
* neither a `@type()` decorator or `type()` call. skip.
|
|
129
|
-
*/
|
|
130
|
-
if (!hasDecorator && !hasExpression) {
|
|
131
|
-
break;
|
|
132
|
-
}
|
|
133
|
-
// using as decorator
|
|
134
|
-
if (propDecorator) {
|
|
135
|
-
/**
|
|
136
|
-
* Calling `@type()` as decorator
|
|
137
|
-
*/
|
|
138
|
-
const typeDecorator = propDecorator.find((decorator => {
|
|
139
|
-
return decorator.expression.expression.escapedText === decoratorName;
|
|
140
|
-
})).expression;
|
|
141
|
-
const property = currentProperty || new types_1.Property();
|
|
142
|
-
property.name = prop.name.escapedText;
|
|
143
|
-
currentStructure.addProperty(property);
|
|
144
|
-
const typeArgument = typeDecorator.arguments[0];
|
|
145
|
-
defineProperty(property, typeArgument);
|
|
146
|
-
}
|
|
147
|
-
else if (prop.expression.arguments?.[1] &&
|
|
148
|
-
prop.expression.expression.arguments?.[0]) {
|
|
149
|
-
/**
|
|
150
|
-
* Calling `type()` as a regular method
|
|
151
|
-
*/
|
|
152
|
-
const property = currentProperty || new types_1.Property();
|
|
153
|
-
property.name = prop.expression.arguments[1].text;
|
|
154
|
-
currentStructure.addProperty(property);
|
|
155
|
-
const typeArgument = prop.expression.expression.arguments[0];
|
|
156
|
-
defineProperty(property, typeArgument);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
else if (node.getText() === "setFields" &&
|
|
160
|
-
(node.parent.kind === ts.SyntaxKind.CallExpression ||
|
|
161
|
-
node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)) {
|
|
162
|
-
/**
|
|
163
|
-
* Metadata.setFields(klassName, { ... })
|
|
164
|
-
*/
|
|
165
|
-
const callExpression = (node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)
|
|
166
|
-
? node.parent.parent
|
|
167
|
-
: node.parent;
|
|
168
|
-
/**
|
|
169
|
-
* Skip if @codegen-ignore comment is found before the call expression
|
|
170
|
-
* TODO: currently, if @codegen-ignore is on the file, it will skip all the setFields calls.
|
|
171
|
-
*/
|
|
172
|
-
const sourceFile = node.getSourceFile();
|
|
173
|
-
const fullText = sourceFile.getFullText();
|
|
174
|
-
const nodeStart = callExpression.getFullStart();
|
|
175
|
-
const textBeforeNode = fullText.substring(0, nodeStart);
|
|
176
|
-
if (textBeforeNode.includes('@codegen-ignore')) {
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
if (callExpression.kind !== ts.SyntaxKind.CallExpression) {
|
|
180
|
-
break;
|
|
181
|
-
}
|
|
182
|
-
const classNameNode = callExpression.arguments[0];
|
|
183
|
-
const className = ts.isClassExpression(classNameNode)
|
|
184
|
-
? classNameNode.name?.escapedText.toString()
|
|
185
|
-
: classNameNode.getText();
|
|
186
|
-
// skip if no className is provided
|
|
187
|
-
if (!className) {
|
|
188
|
-
break;
|
|
189
|
-
}
|
|
190
|
-
if (currentStructure?.name !== className) {
|
|
191
|
-
currentStructure = new types_1.Class();
|
|
192
|
-
}
|
|
193
|
-
context.addStructure(currentStructure);
|
|
194
|
-
currentStructure.extends = "Schema"; // force extends to Schema
|
|
195
|
-
currentStructure.name = className;
|
|
196
|
-
const types = callExpression.arguments[1];
|
|
197
|
-
for (let i = 0; i < types.properties.length; i++) {
|
|
198
|
-
const prop = types.properties[i];
|
|
199
|
-
const property = currentProperty || new types_1.Property();
|
|
200
|
-
property.name = prop.name.escapedText;
|
|
201
|
-
currentStructure.addProperty(property);
|
|
202
|
-
defineProperty(property, prop.initializer);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
else if (node.getText() === "defineTypes" &&
|
|
206
|
-
(node.parent.kind === ts.SyntaxKind.CallExpression ||
|
|
207
|
-
node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)) {
|
|
208
|
-
/**
|
|
209
|
-
* JavaScript source file (`.js`)
|
|
210
|
-
* Using `defineTypes()`
|
|
211
|
-
*/
|
|
212
|
-
const callExpression = (node.parent.kind === ts.SyntaxKind.PropertyAccessExpression)
|
|
213
|
-
? node.parent.parent
|
|
214
|
-
: node.parent;
|
|
215
|
-
if (callExpression.kind !== ts.SyntaxKind.CallExpression) {
|
|
216
|
-
break;
|
|
217
|
-
}
|
|
218
|
-
const className = callExpression.arguments[0].getText();
|
|
219
|
-
currentStructure.name = className;
|
|
220
|
-
const types = callExpression.arguments[1];
|
|
221
|
-
for (let i = 0; i < types.properties.length; i++) {
|
|
222
|
-
const prop = types.properties[i];
|
|
223
|
-
const property = currentProperty || new types_1.Property();
|
|
224
|
-
property.name = prop.name.escapedText;
|
|
225
|
-
currentStructure.addProperty(property);
|
|
226
|
-
defineProperty(property, prop.initializer);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
if (node.parent.kind === ts.SyntaxKind.ClassDeclaration) {
|
|
230
|
-
currentStructure.name = node.getText();
|
|
231
|
-
}
|
|
232
|
-
currentProperty = undefined;
|
|
233
|
-
break;
|
|
234
|
-
case ts.SyntaxKind.CallExpression:
|
|
235
|
-
/**
|
|
236
|
-
* Defining schema via `schema.schema({ ... })`
|
|
237
|
-
* - schema.schema({})
|
|
238
|
-
* - schema({})
|
|
239
|
-
* - ClassName.extends({})
|
|
240
|
-
*/
|
|
241
|
-
if (((node.expression?.getText() === "schema.schema" ||
|
|
242
|
-
node.expression?.getText() === "schema") ||
|
|
243
|
-
(node.expression?.getText().indexOf(".extends") !== -1)) &&
|
|
244
|
-
node.arguments[0].kind === ts.SyntaxKind.ObjectLiteralExpression) {
|
|
245
|
-
const callExpression = node;
|
|
246
|
-
let className = callExpression.arguments[1]?.getText();
|
|
247
|
-
if (!className && callExpression.parent.kind === ts.SyntaxKind.VariableDeclaration) {
|
|
248
|
-
className = callExpression.parent.name?.getText();
|
|
249
|
-
}
|
|
250
|
-
// skip if no className is provided
|
|
251
|
-
if (!className) {
|
|
252
|
-
break;
|
|
253
|
-
}
|
|
254
|
-
if (currentStructure?.name !== className) {
|
|
255
|
-
currentStructure = new types_1.Class();
|
|
256
|
-
context.addStructure(currentStructure);
|
|
257
|
-
}
|
|
258
|
-
if (node.expression?.getText().indexOf(".extends") !== -1) {
|
|
259
|
-
// if it's using `.extends({})`
|
|
260
|
-
const extendsClass = node.expression?.expression?.escapedText;
|
|
261
|
-
// skip if no extendsClass is provided
|
|
262
|
-
if (!extendsClass) {
|
|
263
|
-
break;
|
|
264
|
-
}
|
|
265
|
-
currentStructure.extends = extendsClass;
|
|
266
|
-
}
|
|
267
|
-
else {
|
|
268
|
-
// if it's using `schema({})`
|
|
269
|
-
currentStructure.extends = "Schema"; // force extends to Schema
|
|
270
|
-
}
|
|
271
|
-
currentStructure.name = className;
|
|
272
|
-
const types = callExpression.arguments[0];
|
|
273
|
-
for (let i = 0; i < types.properties.length; i++) {
|
|
274
|
-
const prop = types.properties[i];
|
|
275
|
-
const property = currentProperty || new types_1.Property();
|
|
276
|
-
property.name = prop.name.escapedText;
|
|
277
|
-
currentStructure.addProperty(property);
|
|
278
|
-
defineProperty(property, prop.initializer);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
break;
|
|
282
|
-
case ts.SyntaxKind.EnumMember:
|
|
283
|
-
if (currentStructure instanceof types_1.Enum) {
|
|
284
|
-
const initializer = node.initializer?.text;
|
|
285
|
-
const name = node.getFirstToken().getText();
|
|
286
|
-
const property = currentProperty || new types_1.Property();
|
|
287
|
-
property.name = name;
|
|
288
|
-
if (initializer !== undefined) {
|
|
289
|
-
property.type = initializer;
|
|
290
|
-
}
|
|
291
|
-
currentStructure.addProperty(property);
|
|
292
|
-
currentProperty = undefined;
|
|
293
|
-
}
|
|
294
|
-
break;
|
|
295
|
-
}
|
|
296
|
-
ts.forEachChild(node, (n) => inspectNode(n, context, decoratorName));
|
|
297
|
-
}
|
|
298
|
-
let parsedFiles;
|
|
299
|
-
function parseFiles(fileNames, decoratorName = "type", context = new types_1.Context()) {
|
|
300
|
-
/**
|
|
301
|
-
* Re-set globalContext for each test case
|
|
302
|
-
*/
|
|
303
|
-
if (globalContext !== context) {
|
|
304
|
-
parsedFiles = {};
|
|
305
|
-
globalContext = context;
|
|
306
|
-
}
|
|
307
|
-
fileNames.forEach((fileName) => {
|
|
308
|
-
let sourceFile;
|
|
309
|
-
let sourceFileName;
|
|
310
|
-
const fileNameAlternatives = [];
|
|
311
|
-
if (!fileName.endsWith(".ts") &&
|
|
312
|
-
!fileName.endsWith(".js") &&
|
|
313
|
-
!fileName.endsWith(".mjs")) {
|
|
314
|
-
fileNameAlternatives.push(`${fileName}.ts`);
|
|
315
|
-
fileNameAlternatives.push(`${fileName}/index.ts`);
|
|
316
|
-
}
|
|
317
|
-
else {
|
|
318
|
-
fileNameAlternatives.push(fileName);
|
|
319
|
-
}
|
|
320
|
-
for (let i = 0; i < fileNameAlternatives.length; i++) {
|
|
321
|
-
try {
|
|
322
|
-
sourceFileName = path.resolve(fileNameAlternatives[i]);
|
|
323
|
-
if (parsedFiles[sourceFileName]) {
|
|
324
|
-
break;
|
|
325
|
-
}
|
|
326
|
-
sourceFile = ts.createSourceFile(sourceFileName, (0, fs_1.readFileSync)(sourceFileName).toString(), ts.ScriptTarget.Latest, true);
|
|
327
|
-
parsedFiles[sourceFileName] = true;
|
|
328
|
-
break;
|
|
329
|
-
}
|
|
330
|
-
catch (e) {
|
|
331
|
-
// console.log(`${fileNameAlternatives[i]} => ${e.message}`);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
if (sourceFile) {
|
|
335
|
-
inspectNode(sourceFile, context, decoratorName);
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
return context.getStructures();
|
|
339
|
-
}
|
|
340
|
-
/**
|
|
341
|
-
* TypeScript 4.8+ has introduced a change on how to access decorators.
|
|
342
|
-
* - https://github.com/microsoft/TypeScript/pull/49089
|
|
343
|
-
* - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees
|
|
344
|
-
*/
|
|
345
|
-
function getDecorators(node) {
|
|
346
|
-
if (node == undefined) {
|
|
347
|
-
return undefined;
|
|
348
|
-
}
|
|
349
|
-
// TypeScript 4.7 and below
|
|
350
|
-
// @ts-ignore
|
|
351
|
-
if (node.decorators) {
|
|
352
|
-
return node.decorators;
|
|
353
|
-
}
|
|
354
|
-
// TypeScript 4.8 and above
|
|
355
|
-
// @ts-ignore
|
|
356
|
-
if (ts.canHaveDecorators && ts.canHaveDecorators(node)) {
|
|
357
|
-
// @ts-ignore
|
|
358
|
-
const decorators = ts.getDecorators(node);
|
|
359
|
-
return decorators ? Array.from(decorators) : undefined;
|
|
360
|
-
}
|
|
361
|
-
// @ts-ignore
|
|
362
|
-
return node.modifiers?.filter(ts.isDecorator);
|
|
363
|
-
}
|
|
364
|
-
//# sourceMappingURL=parser.js.map
|