@colyseus/schema 1.0.39 → 1.0.40
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/parser.d.ts +7 -0
- package/lib/codegen/parser.js +24 -2
- package/lib/codegen/parser.js.map +1 -1
- package/package.json +2 -5
- package/src/codegen/parser.ts +21 -1
package/lib/codegen/parser.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
1
2
|
import { Class, Interface, Context } from "./types";
|
|
2
3
|
export declare function parseFiles(fileNames: string[], decoratorName?: string, context?: Context): {
|
|
3
4
|
classes: Class[];
|
|
4
5
|
interfaces: Interface[];
|
|
5
6
|
};
|
|
7
|
+
/**
|
|
8
|
+
* TypeScript 4.8+ has introduced a change on how to access decorators.
|
|
9
|
+
* - https://github.com/microsoft/TypeScript/pull/49089
|
|
10
|
+
* - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees
|
|
11
|
+
*/
|
|
12
|
+
export declare function getDecorators(node: ts.Node | null | undefined): undefined | ts.Decorator[];
|
package/lib/codegen/parser.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseFiles = void 0;
|
|
3
|
+
exports.getDecorators = exports.parseFiles = void 0;
|
|
4
4
|
var ts = require("typescript");
|
|
5
5
|
var path = require("path");
|
|
6
6
|
var fs_1 = require("fs");
|
|
@@ -82,7 +82,7 @@ function inspectNode(node, context, decoratorName) {
|
|
|
82
82
|
}
|
|
83
83
|
if (node.getText() === decoratorName) {
|
|
84
84
|
var prop = (_b = (_a = node.parent) === null || _a === void 0 ? void 0 : _a.parent) === null || _b === void 0 ? void 0 : _b.parent;
|
|
85
|
-
var propDecorator = prop
|
|
85
|
+
var propDecorator = getDecorators(prop);
|
|
86
86
|
var hasExpression = (_c = prop === null || prop === void 0 ? void 0 : prop.expression) === null || _c === void 0 ? void 0 : _c.arguments;
|
|
87
87
|
/**
|
|
88
88
|
* neither a `@type()` decorator or `type()` call. skip.
|
|
@@ -193,4 +193,26 @@ function parseFiles(fileNames, decoratorName, context) {
|
|
|
193
193
|
return context.getStructures();
|
|
194
194
|
}
|
|
195
195
|
exports.parseFiles = parseFiles;
|
|
196
|
+
/**
|
|
197
|
+
* TypeScript 4.8+ has introduced a change on how to access decorators.
|
|
198
|
+
* - https://github.com/microsoft/TypeScript/pull/49089
|
|
199
|
+
* - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees
|
|
200
|
+
*/
|
|
201
|
+
function getDecorators(node) {
|
|
202
|
+
var _a;
|
|
203
|
+
if (node == undefined) {
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
// TypeScript 4.7 and below
|
|
207
|
+
if (node.decorators) {
|
|
208
|
+
return node.decorators;
|
|
209
|
+
}
|
|
210
|
+
// TypeScript 4.8 and above
|
|
211
|
+
if (ts.canHaveDecorators(node)) {
|
|
212
|
+
var decorators = ts.getDecorators(node);
|
|
213
|
+
return decorators ? Array.from(decorators) : undefined;
|
|
214
|
+
}
|
|
215
|
+
return (_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.filter(ts.isDecorator);
|
|
216
|
+
}
|
|
217
|
+
exports.getDecorators = getDecorators;
|
|
196
218
|
//# 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;AAE9B,IAAI,aAAsB,CAAC;AAE3B,SAAS,cAAc,CAAC,QAAkB,EAAE,WAAgB;IACxD,IAAI,EA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E,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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,EACvC,EAAE,CAAC,YAAY,CAAC,MAAM,EACtB,IAAI,CACP,CAAC;gBAEF,WAAW,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC;gBAEnC,MAAM;aACT;YAAC,OAAO,CAAC,EAAE;gBACR,6DAA6D;aAChE;SACJ;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;AAED;;;;GAIG;AACH,SAAgB,aAAa,CAAC,IAAgC;;IAC1D,IAAI,IAAI,IAAI,SAAS,EAAE;QAAE,OAAO,SAAS,CAAC;KAAE;IAE5C,2BAA2B;IAC3B,IAAI,IAAI,CAAC,UAAU,EAAE;QAAE,OAAO,IAAI,CAAC,UAAU,CAAC;KAAE;IAEhD,2BAA2B;IAC3B,IAAI,EAAE,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE;QAC5B,IAAM,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,OAAO,MAAA,IAAI,CAAC,SAAS,0CAAE,MAAM,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC;AAClD,CAAC;AAbD,sCAaC","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 = getDecorators(prop);\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}\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 if (node.decorators) { return node.decorators; }\n\n // TypeScript 4.8 and above\n if (ts.canHaveDecorators(node)) {\n const decorators = ts.getDecorators(node);\n return decorators ? Array.from(decorators) : undefined;\n }\n\n return node.modifiers?.filter(ts.isDecorator);\n}"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@colyseus/schema",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.40",
|
|
4
4
|
"description": "Binary state serializer with delta encoding for games",
|
|
5
5
|
"bin": {
|
|
6
6
|
"schema-codegen": "./bin/schema-codegen"
|
|
@@ -8,10 +8,8 @@
|
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build": "tsc && rollup -c rollup.config.js",
|
|
10
10
|
"watch": "tsc -w",
|
|
11
|
-
|
|
12
11
|
"test": "mocha --require ts-node/register test/*Test.ts test/**/*Test.ts",
|
|
13
12
|
"coverage": "nyc mocha --require ts-node/register --require source-map-support/register --recursive test/**Test.ts",
|
|
14
|
-
|
|
15
13
|
"generate-test-1": "bin/schema-codegen test-external/PrimitiveTypes.ts --namespace SchemaTest.PrimitiveTypes --output ../colyseus-unity3d/Assets/Editor/ColyseusTests/Schema/PrimitiveTypes",
|
|
16
14
|
"generate-test-2": "bin/schema-codegen test-external/ChildSchemaTypes.ts --namespace SchemaTest.ChildSchemaTypes --output ../colyseus-unity3d/Assets/Editor/ColyseusTests/Schema/ChildSchemaTypes",
|
|
17
15
|
"generate-test-3": "bin/schema-codegen test-external/ArraySchemaTypes.ts --namespace SchemaTest.ArraySchemaTypes --output ../colyseus-unity3d/Assets/Editor/ColyseusTests/Schema/ArraySchemaTypes",
|
|
@@ -23,7 +21,6 @@
|
|
|
23
21
|
"generate-test-9": "bin/schema-codegen test-external/InstanceSharingTypes.ts --namespace SchemaTest.InstanceSharing --output ../colyseus-unity3d/Assets/Editor/ColyseusTests/Schema/InstanceSharingTypes",
|
|
24
22
|
"generate-test-10": "bin/schema-codegen test-external/Callbacks.ts --namespace SchemaTest.Callbacks --output ../colyseus-unity3d/Assets/Editor/ColyseusTests/Schema/Callbacks",
|
|
25
23
|
"generate-test-11": "bin/schema-codegen test-external/MapSchemaMoveNullifyType.ts --namespace SchemaTest.MapSchemaMoveNullifyType --output ../colyseus-unity3d/Assets/Editor/ColyseusTests/Schema/MapSchemaMoveNullifyType",
|
|
26
|
-
|
|
27
24
|
"prepublishOnly": "npm run build"
|
|
28
25
|
},
|
|
29
26
|
"files": [
|
|
@@ -78,7 +75,7 @@
|
|
|
78
75
|
"source-map-support": "^0.5.13",
|
|
79
76
|
"ts-node": "^7.0.1",
|
|
80
77
|
"tslib": "^2.1.0",
|
|
81
|
-
"typescript": "^4.
|
|
78
|
+
"typescript": "^4.8.4"
|
|
82
79
|
},
|
|
83
80
|
"nyc": {
|
|
84
81
|
"extension": [
|
package/src/codegen/parser.ts
CHANGED
|
@@ -97,7 +97,7 @@ function inspectNode(node: ts.Node, context: Context, decoratorName: string) {
|
|
|
97
97
|
|
|
98
98
|
if (node.getText() === decoratorName) {
|
|
99
99
|
const prop: any = node.parent?.parent?.parent;
|
|
100
|
-
const propDecorator =
|
|
100
|
+
const propDecorator = getDecorators(prop);
|
|
101
101
|
const hasExpression = prop?.expression?.arguments;
|
|
102
102
|
|
|
103
103
|
/**
|
|
@@ -248,4 +248,24 @@ export function parseFiles(
|
|
|
248
248
|
});
|
|
249
249
|
|
|
250
250
|
return context.getStructures();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* TypeScript 4.8+ has introduced a change on how to access decorators.
|
|
255
|
+
* - https://github.com/microsoft/TypeScript/pull/49089
|
|
256
|
+
* - https://devblogs.microsoft.com/typescript/announcing-typescript-4-8/#decorators-are-placed-on-modifiers-on-typescripts-syntax-trees
|
|
257
|
+
*/
|
|
258
|
+
export function getDecorators(node: ts.Node | null | undefined,): undefined | ts.Decorator[] {
|
|
259
|
+
if (node == undefined) { return undefined; }
|
|
260
|
+
|
|
261
|
+
// TypeScript 4.7 and below
|
|
262
|
+
if (node.decorators) { return node.decorators; }
|
|
263
|
+
|
|
264
|
+
// TypeScript 4.8 and above
|
|
265
|
+
if (ts.canHaveDecorators(node)) {
|
|
266
|
+
const decorators = ts.getDecorators(node);
|
|
267
|
+
return decorators ? Array.from(decorators) : undefined;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return node.modifiers?.filter(ts.isDecorator);
|
|
251
271
|
}
|