@colyseus/schema 2.0.23 → 2.0.25
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/codegen/languages/csharp.js +1 -1
- package/lib/codegen/languages/csharp.js.map +1 -1
- package/lib/codegen/parser.js +1 -1
- package/lib/codegen/parser.js.map +1 -1
- package/lib/types/HelperTypes.d.ts +1 -1
- package/lib/types/HelperTypes.js.map +1 -1
- package/package.json +1 -1
- package/src/codegen/languages/csharp.ts +1 -1
- package/src/codegen/parser.ts +1 -1
- package/src/types/HelperTypes.ts +5 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"csharp.js","sourceRoot":"","sources":["../../../src/codegen/languages/csharp.ts"],"names":[],"mappings":";;;AAAA,oCAOkB;AAIlB,MAAM,QAAQ,GAAG;IACb,QAAQ,EAAE,QAAQ;IAClB,QAAQ,EAAE,OAAO;IACjB,SAAS,EAAE,MAAM;IACjB,MAAM,EAAE,OAAO;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,OAAO;IAChB,QAAQ,EAAE,QAAQ;IAClB,OAAO,EAAE,KAAK;IACd,QAAQ,EAAE,MAAM;IAChB,OAAO,EAAE,MAAM;IACf,QAAQ,EAAE,OAAO;IACjB,SAAS,EAAE,OAAO;IAClB,SAAS,EAAE,QAAQ;CACtB,CAAA;AAED;;GAEG;AACH,MAAM,UAAU,GAAG,CAAC,CAAC,EAAE,EAAE;IACrB,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,OAAO,EAAE,CAAA;IACpC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAClD,CAAC,CAAA;AAED,SAAgB,QAAQ,CAAC,OAAgB,EAAE,OAAwB;IAC/D,6BAA6B;IAC7B,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE;Q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IAAI,GAAG,CAAC,aAAa,CAAC;gBAClB,CAAC,CAAC,GAAG,cAAc,UAAU,IAAI,CAAC,SAAS,GAAG;gBAC9C,CAAC,CAAC,GAAG,cAAc,UAAU,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC;SAC1D;QACD,OAAO,IAAI,CAAC;KAEf;SAAM;QACH,OAAO,CAAC,IAAI,CAAC,IAAI,KAAK,OAAO,CAAC;YAC1B,CAAC,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,SAAS,IAAI;YACnD,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;KAC7B;AACL,CAAC","sourcesContent":["import {\n Class,\n Property,\n File,\n getCommentHeader,\n Interface,\n Enum,\n} from \"../types\";\nimport { GenerateOptions } from \"../api\";\nimport { Context } from \"../types\";\n\nconst typeMaps = {\n \"string\": \"string\",\n \"number\": \"float\",\n \"boolean\": \"bool\",\n \"int8\": \"sbyte\",\n \"uint8\": \"byte\",\n \"int16\": \"short\",\n \"uint16\": \"ushort\",\n \"int32\": \"int\",\n \"uint32\": \"uint\",\n \"int64\": \"long\",\n \"uint64\": \"ulong\",\n \"float32\": \"float\",\n \"float64\": \"double\",\n}\n\n/**\n * C# Code Generator\n */\nconst capitalize = (s) => {\n if (typeof s !== 'string') return ''\n return s.charAt(0).toUpperCase() + s.slice(1);\n}\n\nexport function generate(context: Context, options: GenerateOptions): File[] {\n // enrich typeMaps with enums\n context.enums.forEach((structure) => {\n typeMaps[structure.name] = structure.name;\n });\n return [\n ...context.classes.map(structure => ({\n name: `${structure.name}.cs`,\n content: generateClass(structure, options.namespace)\n })),\n ...context.interfaces.map(structure => ({\n name: `${structure.name}.cs`,\n content: generateInterface(structure, options.namespace),\n })),\n ...context.enums.filter(structure => structure.name !== 'OPERATION').map((structure) => ({\n name: `${structure.name}.cs`,\n content: generateEnum(structure, options.namespace),\n })),\n ];\n}\n\nfunction generateClass(klass: Class, namespace: string) {\n const indent = (namespace) ? \"\\t\" : \"\";\n return `${getCommentHeader()}\n\nusing Colyseus.Schema;\nusing Action = System.Action;\n${namespace ? `\\nnamespace ${namespace} {` : \"\"}\n${indent}public partial class ${klass.name} : ${klass.extends} {\n${klass.properties.map((prop) => generateProperty(prop, indent)).join(\"\\n\\n\")}\n\n${indent}\\t/*\n${indent}\\t * Support for individual property change callbacks below...\n${indent}\\t */\n\n${generateAllFieldCallbacks(klass, indent)}\n${indent}}\n${namespace ? \"}\" : \"\"}\n`;\n}\n\nfunction generateEnum(_enum: Enum, namespace: string) {\n const indent = namespace ? \"\\t\" : \"\";\n return `${getCommentHeader()}\n${namespace ? `\\nnamespace ${namespace} {` : \"\"}\n${indent}public struct ${_enum.name} {\n\n${_enum.properties\n .map((prop) => {\n let dataType: string = \"int\";\n let value: any;\n\n if(prop.type) {\n if(isNaN(Number(prop.type))) {\n value = prop.type;\n dataType = \"string\";\n } else {\n value = Number(prop.type);\n dataType = Number.isInteger(value)? 'int': 'float';\n }\n } else {\n value = _enum.properties.indexOf(prop);\n }\n return `${indent}\\tpublic const ${dataType} ${prop.name} = ${value};`;\n })\n .join(\"\\n\")}\n${indent}}\n${namespace ? \"}\" : \"\"}`\n}\n\nfunction generateProperty(prop: Property, indent: string = \"\") {\n let typeArgs = `\"${prop.type}\"`;\n let property = \"public\";\n let langType: string;\n let initializer = \"\";\n\n if (prop.childType) {\n const isUpcaseFirst = prop.childType.match(/^[A-Z]/);\n\n langType = getType(prop);\n typeArgs += `, typeof(${langType})`;\n\n if (!isUpcaseFirst) {\n typeArgs += `, \"${prop.childType}\"`;\n }\n\n initializer = `new ${langType}()`;\n\n } else {\n langType = getType(prop);\n initializer = `default(${langType})`;\n }\n\n property += ` ${langType} ${prop.name}`;\n\n let ret = (prop.deprecated) ? `\\t\\t[System.Obsolete(\"field '${prop.name}' is deprecated.\", true)]\\n` : '';\n\n return ret + `\\t${indent}[Type(${prop.index}, ${typeArgs})]\n\\t${indent}${property} = ${initializer};`;\n}\n\nfunction generateInterface(struct: Interface, namespace: string) {\n const indent = (namespace) ? \"\\t\" : \"\";\n return `${getCommentHeader()}\n\nusing Colyseus.Schema;\n${namespace ? `\\nnamespace ${namespace} {` : \"\"}\n${indent}public class ${struct.name} {\n${struct.properties.map(prop => `\\t${indent}public ${getType(prop)} ${prop.name};`).join(\"\\n\")}\n${indent}}\n${namespace ? \"}\" : \"\"}\n`;\n}\n\nfunction generateAllFieldCallbacks(klass: Class, indent: string) {\n //\n // TODO: improve me. It would be great to generate less boilerplate in favor\n // of a single implementation on C# Schema class itself.\n //\n const eventNames: string[] = [];\n return `${klass.properties\n .filter(prop => !prop.deprecated) // generate only for properties that haven't been deprecated.\n .map(prop => {\n const eventName = `__${prop.name}Change`;\n eventNames.push(eventName);\n\n const defaultNull = (prop.childType)\n ? \"null\"\n : `default(${getType(prop)})`;\n\n return `\\t${indent}protected event PropertyChangeHandler<${getType(prop)}> ${eventName};\n\\t${indent}public Action On${capitalize(prop.name)}Change(PropertyChangeHandler<${getType(prop)}> __handler, bool __immediate = true) {\n\\t${indent}\\tif (__callbacks == null) { __callbacks = new SchemaCallbacks(); }\n\\t${indent}\\t__callbacks.AddPropertyCallback(nameof(this.${prop.name}));\n\\t${indent}\\t${eventName} += __handler;\n\\t${indent}\\tif (__immediate && this.${prop.name} != ${defaultNull}) { __handler(this.${prop.name}, ${defaultNull}); }\n\\t${indent}\\treturn () => {\n\\t${indent}\\t\\t__callbacks.RemovePropertyCallback(nameof(${prop.name}));\n\\t${indent}\\t\\t${eventName} -= __handler;\n\\t${indent}\\t};\n\\t${indent}}`;\n }).join(\"\\n\\n\")}\n\n\\t${indent}protected override void TriggerFieldChange(DataChange change) {\n\\t${indent}\\tswitch (change.Field) {\n${klass.properties.filter(prop => !prop.deprecated).map((prop, i) => {\n return `\\t${indent}\\t\\tcase nameof(${prop.name}): ${eventNames[i]}?.Invoke((${getType(prop)}) change.Value, (${getType(prop)}) change.PreviousValue); break;`;\n}).join(\"\\n\")}\n\\t${indent}\\t\\tdefault: break;\n\\t\\t${indent}}\n\\t${indent}}`;\n}\n\nfunction getChildType(prop: Property) {\n return typeMaps[prop.childType];\n}\n\nfunction getType(prop: Property) {\n if (prop.childType) {\n const isUpcaseFirst = prop.childType.match(/^[A-Z]/);\n let type: string;\n\n if(prop.type === \"ref\") {\n type = (isUpcaseFirst)\n ? prop.childType\n : getChildType(prop);\n } else {\n const containerClass = capitalize(prop.type);\n type = (isUpcaseFirst)\n ? `${containerClass}Schema<${prop.childType}>`\n : `${containerClass}Schema<${getChildType(prop)}>`;\n }\n return type;\n\n } else {\n return (prop.type === \"array\")\n ? `${typeMaps[prop.childType] || prop.childType}[]`\n : typeMaps[prop.type];\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"csharp.js","sourceRoot":"","sources":["../../../src/codegen/languages/csharp.ts"],"names":[],"mappings":";;;AAAA,oCAOkB;AAIlB,MAAM,QAAQ,GAAG;IACb,QAAQ,EAAE,QAAQ;IAClB,QAAQ,EAAE,OAAO;IACjB,SAAS,EAAE,MAAM;IACjB,MAAM,EAAE,OAAO;IACf,OAAO,EAAE,MAAM;IACf,OAAO,EAAE,OAAO;IAChB,QAAQ,EAAE,QAAQ;IAClB,OAAO,EAAE,KAAK;IACd,QAAQ,EAAE,MAAM;IAChB,OAAO,EAAE,MAAM;IACf,QAAQ,EAAE,OAAO;IACjB,SAAS,EAAE,OAAO;IAClB,SAAS,EAAE,QAAQ;CACtB,CAAA;AAED;;GAEG;AACH,MAAM,UAAU,GAAG,CAAC,CAAC,EAAE,EAAE;IACrB,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,OAAO,EAAE,CAAA;IACpC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AAClD,CAAC,CAAA;AAED,SAAgB,QAAQ,CAAC,OAAgB,EAAE,OAAwB;IAC/D,6BA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SAC5B;aAAM;YACH,MAAM,cAAc,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC7C,IAAI,GAAG,CAAC,aAAa,CAAC;gBAClB,CAAC,CAAC,GAAG,cAAc,UAAU,IAAI,CAAC,SAAS,GAAG;gBAC9C,CAAC,CAAC,GAAG,cAAc,UAAU,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC;SAC1D;QACD,OAAO,IAAI,CAAC;KAEf;SAAM;QACH,OAAO,CAAC,IAAI,CAAC,IAAI,KAAK,OAAO,CAAC;YAC1B,CAAC,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,SAAS,IAAI;YACnD,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;KAC7B;AACL,CAAC","sourcesContent":["import {\n Class,\n Property,\n File,\n getCommentHeader,\n Interface,\n Enum,\n} from \"../types\";\nimport { GenerateOptions } from \"../api\";\nimport { Context } from \"../types\";\n\nconst typeMaps = {\n \"string\": \"string\",\n \"number\": \"float\",\n \"boolean\": \"bool\",\n \"int8\": \"sbyte\",\n \"uint8\": \"byte\",\n \"int16\": \"short\",\n \"uint16\": \"ushort\",\n \"int32\": \"int\",\n \"uint32\": \"uint\",\n \"int64\": \"long\",\n \"uint64\": \"ulong\",\n \"float32\": \"float\",\n \"float64\": \"double\",\n}\n\n/**\n * C# Code Generator\n */\nconst capitalize = (s) => {\n if (typeof s !== 'string') return ''\n return s.charAt(0).toUpperCase() + s.slice(1);\n}\n\nexport function generate(context: Context, options: GenerateOptions): File[] {\n // enrich typeMaps with enums\n context.enums.forEach((structure) => {\n typeMaps[structure.name] = structure.name;\n });\n return [\n ...context.classes.map(structure => ({\n name: `${structure.name}.cs`,\n content: generateClass(structure, options.namespace)\n })),\n ...context.interfaces.map(structure => ({\n name: `${structure.name}.cs`,\n content: generateInterface(structure, options.namespace),\n })),\n ...context.enums.filter(structure => structure.name !== 'OPERATION').map((structure) => ({\n name: `${structure.name}.cs`,\n content: generateEnum(structure, options.namespace),\n })),\n ];\n}\n\nfunction generateClass(klass: Class, namespace: string) {\n const indent = (namespace) ? \"\\t\" : \"\";\n return `${getCommentHeader()}\n\nusing Colyseus.Schema;\nusing Action = System.Action;\n${namespace ? `\\nnamespace ${namespace} {` : \"\"}\n${indent}public partial class ${klass.name} : ${klass.extends} {\n${klass.properties.map((prop) => generateProperty(prop, indent)).join(\"\\n\\n\")}\n\n${indent}\\t/*\n${indent}\\t * Support for individual property change callbacks below...\n${indent}\\t */\n\n${generateAllFieldCallbacks(klass, indent)}\n${indent}}\n${namespace ? \"}\" : \"\"}\n`;\n}\n\nfunction generateEnum(_enum: Enum, namespace: string) {\n const indent = namespace ? \"\\t\" : \"\";\n return `${getCommentHeader()}\n${namespace ? `\\nnamespace ${namespace} {` : \"\"}\n${indent}public struct ${_enum.name} {\n\n${_enum.properties\n .map((prop) => {\n let dataType: string = \"int\";\n let value: any;\n\n if(prop.type) {\n if(isNaN(Number(prop.type))) {\n value = `\"${prop.type}\"`;\n dataType = \"string\";\n } else {\n value = Number(prop.type);\n dataType = Number.isInteger(value)? 'int': 'float';\n }\n } else {\n value = _enum.properties.indexOf(prop);\n }\n return `${indent}\\tpublic const ${dataType} ${prop.name} = ${value};`;\n })\n .join(\"\\n\")}\n${indent}}\n${namespace ? \"}\" : \"\"}`\n}\n\nfunction generateProperty(prop: Property, indent: string = \"\") {\n let typeArgs = `\"${prop.type}\"`;\n let property = \"public\";\n let langType: string;\n let initializer = \"\";\n\n if (prop.childType) {\n const isUpcaseFirst = prop.childType.match(/^[A-Z]/);\n\n langType = getType(prop);\n typeArgs += `, typeof(${langType})`;\n\n if (!isUpcaseFirst) {\n typeArgs += `, \"${prop.childType}\"`;\n }\n\n initializer = `new ${langType}()`;\n\n } else {\n langType = getType(prop);\n initializer = `default(${langType})`;\n }\n\n property += ` ${langType} ${prop.name}`;\n\n let ret = (prop.deprecated) ? `\\t\\t[System.Obsolete(\"field '${prop.name}' is deprecated.\", true)]\\n` : '';\n\n return ret + `\\t${indent}[Type(${prop.index}, ${typeArgs})]\n\\t${indent}${property} = ${initializer};`;\n}\n\nfunction generateInterface(struct: Interface, namespace: string) {\n const indent = (namespace) ? \"\\t\" : \"\";\n return `${getCommentHeader()}\n\nusing Colyseus.Schema;\n${namespace ? `\\nnamespace ${namespace} {` : \"\"}\n${indent}public class ${struct.name} {\n${struct.properties.map(prop => `\\t${indent}public ${getType(prop)} ${prop.name};`).join(\"\\n\")}\n${indent}}\n${namespace ? \"}\" : \"\"}\n`;\n}\n\nfunction generateAllFieldCallbacks(klass: Class, indent: string) {\n //\n // TODO: improve me. It would be great to generate less boilerplate in favor\n // of a single implementation on C# Schema class itself.\n //\n const eventNames: string[] = [];\n return `${klass.properties\n .filter(prop => !prop.deprecated) // generate only for properties that haven't been deprecated.\n .map(prop => {\n const eventName = `__${prop.name}Change`;\n eventNames.push(eventName);\n\n const defaultNull = (prop.childType)\n ? \"null\"\n : `default(${getType(prop)})`;\n\n return `\\t${indent}protected event PropertyChangeHandler<${getType(prop)}> ${eventName};\n\\t${indent}public Action On${capitalize(prop.name)}Change(PropertyChangeHandler<${getType(prop)}> __handler, bool __immediate = true) {\n\\t${indent}\\tif (__callbacks == null) { __callbacks = new SchemaCallbacks(); }\n\\t${indent}\\t__callbacks.AddPropertyCallback(nameof(this.${prop.name}));\n\\t${indent}\\t${eventName} += __handler;\n\\t${indent}\\tif (__immediate && this.${prop.name} != ${defaultNull}) { __handler(this.${prop.name}, ${defaultNull}); }\n\\t${indent}\\treturn () => {\n\\t${indent}\\t\\t__callbacks.RemovePropertyCallback(nameof(${prop.name}));\n\\t${indent}\\t\\t${eventName} -= __handler;\n\\t${indent}\\t};\n\\t${indent}}`;\n }).join(\"\\n\\n\")}\n\n\\t${indent}protected override void TriggerFieldChange(DataChange change) {\n\\t${indent}\\tswitch (change.Field) {\n${klass.properties.filter(prop => !prop.deprecated).map((prop, i) => {\n return `\\t${indent}\\t\\tcase nameof(${prop.name}): ${eventNames[i]}?.Invoke((${getType(prop)}) change.Value, (${getType(prop)}) change.PreviousValue); break;`;\n}).join(\"\\n\")}\n\\t${indent}\\t\\tdefault: break;\n\\t\\t${indent}}\n\\t${indent}}`;\n}\n\nfunction getChildType(prop: Property) {\n return typeMaps[prop.childType];\n}\n\nfunction getType(prop: Property) {\n if (prop.childType) {\n const isUpcaseFirst = prop.childType.match(/^[A-Z]/);\n let type: string;\n\n if(prop.type === \"ref\") {\n type = (isUpcaseFirst)\n ? prop.childType\n : getChildType(prop);\n } else {\n const containerClass = capitalize(prop.type);\n type = (isUpcaseFirst)\n ? `${containerClass}Schema<${prop.childType}>`\n : `${containerClass}Schema<${getChildType(prop)}>`;\n }\n return type;\n\n } else {\n return (prop.type === \"array\")\n ? `${typeMaps[prop.childType] || prop.childType}[]`\n : typeMaps[prop.type];\n }\n}\n"]}
|
package/lib/codegen/parser.js
CHANGED
|
@@ -153,7 +153,7 @@ function inspectNode(node, context, decoratorName) {
|
|
|
153
153
|
break;
|
|
154
154
|
case ts.SyntaxKind.EnumMember:
|
|
155
155
|
if (currentStructure instanceof types_1.Enum) {
|
|
156
|
-
const initializer = node.initializer?.
|
|
156
|
+
const initializer = node.initializer?.text;
|
|
157
157
|
const name = node.getFirstToken().getText();
|
|
158
158
|
const property = currentProperty || new types_1.Property();
|
|
159
159
|
property.name = name;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../../src/codegen/parser.ts"],"names":[],"mappings":";;;AAAA,iCAAiC;AACjC,6BAA6B;AAC7B,2BAAgC;AAChC,mCAA8E;AAE9E,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,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AAU,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"]}
|
|
1
|
+
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../../src/codegen/parser.ts"],"names":[],"mappings":";;;AAAA,iCAAiC;AACjC,6BAA6B;AAC7B,2BAAgC;AAChC,mCAA8E;AAE9E,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,CAA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,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?.text;\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"]}
|
|
@@ -7,5 +7,5 @@ export type NonFunctionPropNames<T> = {
|
|
|
7
7
|
[K in keyof T]: T[K] extends Function ? never : K;
|
|
8
8
|
}[keyof T];
|
|
9
9
|
export type ToJSON<T> = NonFunctionProps<{
|
|
10
|
-
[K in keyof T]: T[K] extends MapSchema<infer U> ? Record<string, U> : T[K] extends ArraySchema<infer U> ? U[] : T[K];
|
|
10
|
+
[K in keyof T]: T[K] extends MapSchema<infer U> ? Record<string, U> : T[K] extends Map<string, infer U> ? Record<string, U> : T[K] extends ArraySchema<infer U> ? U[] : T[K];
|
|
11
11
|
}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HelperTypes.js","sourceRoot":"","sources":["../../src/types/HelperTypes.ts"],"names":[],"mappings":"","sourcesContent":["import { ArraySchema } from \"./ArraySchema\";\nimport { MapSchema } from \"./MapSchema\";\n\nexport type NonFunctionProps<T> = Omit<T, {\n [K in keyof T]: T[K] extends Function ? K : never;\n}[keyof T]>;\n\nexport type NonFunctionPropNames<T> = {\n [K in keyof T]: T[K] extends Function ? never : K\n}[keyof T];\n\nexport type ToJSON<T> = NonFunctionProps<{\n [K in keyof T]: T[K] extends MapSchema<infer U>\n ? Record<string, U>\n : T[K] extends
|
|
1
|
+
{"version":3,"file":"HelperTypes.js","sourceRoot":"","sources":["../../src/types/HelperTypes.ts"],"names":[],"mappings":"","sourcesContent":["import { ArraySchema } from \"./ArraySchema\";\nimport { MapSchema } from \"./MapSchema\";\n\nexport type NonFunctionProps<T> = Omit<T, {\n [K in keyof T]: T[K] extends Function ? K : never;\n}[keyof T]>;\n\nexport type NonFunctionPropNames<T> = {\n [K in keyof T]: T[K] extends Function ? never : K\n}[keyof T];\n\nexport type ToJSON<T> = NonFunctionProps<{\n [K in keyof T]: T[K] extends MapSchema<infer U>\n ? Record<string, U>\n : T[K] extends Map<string, infer U>\n ? Record<string, U>\n : T[K] extends ArraySchema<infer U>\n ? U[]\n : T[K]\n}>;"]}
|
package/package.json
CHANGED
package/src/codegen/parser.ts
CHANGED
|
@@ -194,7 +194,7 @@ function inspectNode(node: ts.Node, context: Context, decoratorName: string) {
|
|
|
194
194
|
|
|
195
195
|
case ts.SyntaxKind.EnumMember:
|
|
196
196
|
if (currentStructure instanceof Enum) {
|
|
197
|
-
const initializer = (node as any).initializer?.
|
|
197
|
+
const initializer = (node as any).initializer?.text;
|
|
198
198
|
const name = node.getFirstToken().getText();
|
|
199
199
|
const property = currentProperty || new Property();
|
|
200
200
|
property.name = name;
|
package/src/types/HelperTypes.ts
CHANGED
|
@@ -12,7 +12,9 @@ export type NonFunctionPropNames<T> = {
|
|
|
12
12
|
export type ToJSON<T> = NonFunctionProps<{
|
|
13
13
|
[K in keyof T]: T[K] extends MapSchema<infer U>
|
|
14
14
|
? Record<string, U>
|
|
15
|
-
: T[K] extends
|
|
16
|
-
? U
|
|
17
|
-
: T[K]
|
|
15
|
+
: T[K] extends Map<string, infer U>
|
|
16
|
+
? Record<string, U>
|
|
17
|
+
: T[K] extends ArraySchema<infer U>
|
|
18
|
+
? U[]
|
|
19
|
+
: T[K]
|
|
18
20
|
}>;
|