@needle-tools/needle-component-compiler 2.2.0-pre → 2.3.0-pre

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/Changelog.md CHANGED
@@ -4,7 +4,13 @@ All notable changes to this package will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
- ## [2.2.0-pre] - 2022-11-13
7
+ ## [2.3.0-pre] - 2022-11-22
8
+
9
+ # Blender compiler
10
+ - Add emit array
11
+ - Add emit types by class name if unknown
12
+
13
+ ## [2.2.0-pre.1] - 2022-11-13
8
14
  - Add: watcher now also generates the register_types file
9
15
  - Add: deleting code files now also deletes the generated files and reports it
10
16
 
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "@needle-tools/needle-component-compiler",
3
- "version": "2.2.0-pre",
3
+ "version": "2.3.0-pre",
4
4
  "description": "Compile mock unity components from typescript",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "tsc": "tsc",
8
8
  "dev": "npm-watch compile || exit 1",
9
9
  "compile": "tsc",
10
- "test": "mocha -r ts-node/register test/**.test.ts"
10
+ "test" : "npm run test:csharp & npm run test:blender",
11
+ "test:csharp": "mocha -r ts-node/register test/csharp/**.test.ts",
12
+ "test:blender": "mocha -r ts-node/register test/blender/**.test.ts"
11
13
  },
12
14
  "dependencies": {
13
15
  "chokidar": "^3.5.3",
@@ -25,11 +27,9 @@
25
27
  "watch": {
26
28
  "compile": {
27
29
  "patterns": [
28
- "src/base-compiler.ts",
29
- "src/blender-compiler.ts",
30
- "src/component-compiler.ts",
31
- "src/watcher.ts",
32
- "src/register-types.ts"
30
+ "src/**/*.ts",
31
+ "test/*/*.ts",
32
+ "test/*.ts"
33
33
  ],
34
34
  "ignore": "src/test.ts",
35
35
  "extensions": "ts",
@@ -45,4 +45,4 @@
45
45
  "type": "git",
46
46
  "url": "git+https://github.com/needle-tools/needle-tiny-component-compiler.git"
47
47
  }
48
- }
48
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Compiler = exports.runFromFile = exports.handleDeletedFile = exports.CodeTextWriter = exports.Visibility = void 0;
3
+ exports.Compiler = exports.runFromFile = exports.handleDeletedFile = exports.CodeTextWriter = exports.MemorySink = exports.FileSink = exports.Visibility = void 0;
4
4
  var fs_1 = require("fs");
5
5
  var ts = require("typescript");
6
6
  var fs = require("fs");
@@ -11,6 +11,36 @@ var Visibility;
11
11
  Visibility[Visibility["Protected"] = 1] = "Protected";
12
12
  Visibility[Visibility["Private"] = 2] = "Private";
13
13
  })(Visibility = exports.Visibility || (exports.Visibility = {}));
14
+ var FileSink = /** @class */ (function () {
15
+ function FileSink(directory) {
16
+ this.directory = directory;
17
+ }
18
+ FileSink.prototype.flush = function (id, str) {
19
+ if (!fs.existsSync(this.directory)) {
20
+ fs.mkdirSync(this.directory, { recursive: true });
21
+ }
22
+ console.log("Writing " + id + " to " + this.directory);
23
+ var fullPath = this.directory + "/" + id;
24
+ fs.writeFileSync(fullPath, str);
25
+ return fs.realpathSync(fullPath);
26
+ };
27
+ return FileSink;
28
+ }());
29
+ exports.FileSink = FileSink;
30
+ var MemorySink = /** @class */ (function () {
31
+ function MemorySink() {
32
+ this.results = [];
33
+ }
34
+ MemorySink.prototype.clear = function () {
35
+ this.results.length = 0;
36
+ };
37
+ MemorySink.prototype.flush = function (id, str) {
38
+ this.results.push(str);
39
+ return "";
40
+ };
41
+ return MemorySink;
42
+ }());
43
+ exports.MemorySink = MemorySink;
14
44
  var CodeTextWriter = /** @class */ (function () {
15
45
  function CodeTextWriter() {
16
46
  this.indent = 0;
@@ -69,18 +99,20 @@ var CodeTextWriter = /** @class */ (function () {
69
99
  // }
70
100
  // endArray() {
71
101
  // }
72
- CodeTextWriter.prototype.flush = function (directory, filename) {
73
- if (this.lines.length === 0)
74
- return;
75
- if (!fs.existsSync(directory)) {
76
- fs.mkdirSync(directory, { recursive: true });
77
- }
78
- console.log("Writing " + filename + " to " + directory);
79
- var content = this.toString();
102
+ CodeTextWriter.prototype.flush = function () {
103
+ var str = this.toString();
80
104
  this.lines.length = 0;
81
- var fullPath = directory + "/" + filename;
82
- fs.writeFileSync(fullPath, content);
83
- return fs.realpathSync(fullPath);
105
+ return str;
106
+ // if (this.lines.length === 0) return;
107
+ // if (!fs.existsSync(directory)) {
108
+ // fs.mkdirSync(directory, { recursive: true });
109
+ // }
110
+ // console.log("Writing " + filename + " to " + directory);
111
+ // const content = this.toString();
112
+ // this.lines.length = 0;
113
+ // const fullPath = directory + "/" + filename;
114
+ // fs.writeFileSync(fullPath, content);
115
+ // return fs.realpathSync(fullPath);
84
116
  };
85
117
  CodeTextWriter.prototype.clear = function () {
86
118
  this.lines.length = 0;
@@ -172,9 +204,22 @@ var Compiler = /** @class */ (function () {
172
204
  Compiler.prototype.visitClassDeclaration = function (filePath, node, writer) {
173
205
  var _this = this;
174
206
  var name = node.name.text;
175
- var base = node.heritageClauses && node.heritageClauses[0].types[0].expression.getText();
176
- this.debugLog("CLASS START", name, base);
177
- var res = writer.startNewType(filePath, name, base, this.getComments(node));
207
+ // get all parent types
208
+ var baseTypes = [];
209
+ if (node.heritageClauses) {
210
+ for (var _i = 0, _a = node.heritageClauses; _i < _a.length; _i++) {
211
+ var clause = _a[_i];
212
+ if (clause.token == ts.SyntaxKind.ExtendsKeyword) {
213
+ for (var _b = 0, _c = clause.types; _b < _c.length; _b++) {
214
+ var type = _c[_b];
215
+ var typeName = type.expression.getText();
216
+ baseTypes.push(typeName);
217
+ }
218
+ }
219
+ }
220
+ }
221
+ this.debugLog("CLASS START", name, baseTypes);
222
+ var res = writer.startNewType(filePath, name, baseTypes, this.getComments(node));
178
223
  if (res === false) {
179
224
  this.debugLog("CLASS SKIPPED", name);
180
225
  return;
@@ -282,6 +327,7 @@ var Compiler = /** @class */ (function () {
282
327
  }
283
328
  };
284
329
  Compiler.prototype.resolveType = function (typeNode, writer) {
330
+ var _a;
285
331
  if (!typeNode)
286
332
  return undefined;
287
333
  // check if its an array
@@ -330,6 +376,19 @@ var Compiler = /** @class */ (function () {
330
376
  console.error("!!!!! ----- Unknown array type", typeNode.getText());
331
377
  }
332
378
  var type = typeNode.getText();
379
+ var baseTypes = [];
380
+ if (typeNode.kind === ts.SyntaxKind.TypeReference) {
381
+ var typeReference = typeNode;
382
+ if ((_a = typeReference.typeArguments) === null || _a === void 0 ? void 0 : _a.length) {
383
+ for (var _i = 0, _b = typeReference.typeArguments; _i < _b.length; _i++) {
384
+ var arg = _b[_i];
385
+ var type_1 = this.resolveType(arg, writer);
386
+ if (type_1) {
387
+ baseTypes.push(type_1);
388
+ }
389
+ }
390
+ }
391
+ }
333
392
  return this.resolveTypeFromString(type, writer);
334
393
  };
335
394
  Compiler.prototype.resolveTypeFromString = function (type, writer) {
@@ -4,13 +4,13 @@ exports.BlenderWriter = void 0;
4
4
  var fs_1 = require("fs");
5
5
  var base_compiler_1 = require("./base-compiler");
6
6
  var commands_1 = require("./commands");
7
- var supportedTypes = ["string", "float", "int", "bool", "col", "obj", "evt", "fn", "vec2", "vec3"];
7
+ var supportedTypes = ["string", "float", "int", "bool", "col", "obj", "evt", "fn", "vec2", "vec3", "comp"];
8
8
  var BlenderWriter = /** @class */ (function () {
9
- function BlenderWriter(outputDirectory) {
9
+ function BlenderWriter(_sink) {
10
+ this._sink = _sink;
10
11
  this.writer = new base_compiler_1.CodeTextWriter();
11
12
  this._createdSchemesPerFile = {};
12
13
  this._currentlyProcessingFiles = {};
13
- this._outputDirectory = outputDirectory;
14
14
  }
15
15
  Object.defineProperty(BlenderWriter.prototype, "outputInfo", {
16
16
  get: function () {
@@ -19,9 +19,9 @@ var BlenderWriter = /** @class */ (function () {
19
19
  enumerable: false,
20
20
  configurable: true
21
21
  });
22
- Object.defineProperty(BlenderWriter.prototype, "outputDirectory", {
22
+ Object.defineProperty(BlenderWriter.prototype, "sink", {
23
23
  get: function () {
24
- return this._outputDirectory;
24
+ return this._sink;
25
25
  },
26
26
  enumerable: false,
27
27
  configurable: true
@@ -55,9 +55,10 @@ var BlenderWriter = /** @class */ (function () {
55
55
  return "col";
56
56
  case "eventlist":
57
57
  return "evt";
58
- case "renderer":
58
+ case "behaviour":
59
59
  return "comp";
60
60
  }
61
+ return typeName;
61
62
  };
62
63
  BlenderWriter.prototype.begin = function (filePath) {
63
64
  if (!this._currentlyProcessingFiles[filePath]) {
@@ -103,8 +104,8 @@ var BlenderWriter = /** @class */ (function () {
103
104
  }
104
105
  delete this._currentlyProcessingFiles[filePath];
105
106
  };
106
- BlenderWriter.prototype.startNewType = function (filePath, typeName, baseType, comments) {
107
- if (baseType !== "Behaviour")
107
+ BlenderWriter.prototype.startNewType = function (filePath, typeName, baseTypes, comments) {
108
+ if (baseTypes.includes("Behaviour") == false)
108
109
  return false;
109
110
  this.writer.beginBlock("{");
110
111
  if (comments)
@@ -119,10 +120,10 @@ var BlenderWriter = /** @class */ (function () {
119
120
  BlenderWriter.prototype.writeMember = function (visibility, name, isArray, type, initialValue, comments) {
120
121
  if (visibility !== base_compiler_1.Visibility.Public)
121
122
  return;
122
- if (!supportedTypes.includes(type)) {
123
- console.log("-- unsupported type:", type);
124
- return;
125
- }
123
+ // if (!supportedTypes.includes(type)) {
124
+ // console.log("-- unsupported type:", type);
125
+ // return;
126
+ // }
126
127
  this.writer.beginBlock("\"" + name + "\": {");
127
128
  this.writer.writeLine("\"type\": \"" + type + "\",");
128
129
  if (initialValue && !isArray) {
@@ -139,7 +140,7 @@ var BlenderWriter = /** @class */ (function () {
139
140
  this.writer.writeLine("\"value\": " + initialValue);
140
141
  }
141
142
  if (isArray) {
142
- this.writer.writeLine("\"isArray\": true");
143
+ this.writer.writeLine("\"value\": []");
143
144
  }
144
145
  this.writer.endBlock("},");
145
146
  };
@@ -154,13 +155,13 @@ var BlenderWriter = /** @class */ (function () {
154
155
  console.log("new type:", typeName);
155
156
  };
156
157
  BlenderWriter.prototype.writeScheme = function (processingFilePath, component) {
157
- var outputPath = this.writer.flush(this._outputDirectory, component + ".component.json");
158
+ var res = this.sink.flush(component + ".component.json", this.writer.flush());
158
159
  // if an output path is returned it means a file has been written to that path
159
- if (outputPath) {
160
- (0, commands_1.sendFileWrittenCommand)(outputPath);
160
+ if (res && (0, fs_1.existsSync)(res)) {
161
+ (0, commands_1.sendFileWrittenCommand)(res);
161
162
  // add the scheme to the list of created schemes
162
163
  if (processingFilePath && this._currentlyProcessingFiles[processingFilePath]) {
163
- this._currentlyProcessingFiles[processingFilePath].push({ componentName: component, filePath: outputPath });
164
+ this._currentlyProcessingFiles[processingFilePath].push({ componentName: component, filePath: res });
164
165
  }
165
166
  }
166
167
  };
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CSharpWriter = void 0;
4
+ var fs_1 = require("fs");
5
+ var path = require("path");
6
+ var allowDebugLogs = false;
7
+ var CSharpWriter = /** @class */ (function () {
8
+ function CSharpWriter(_outputDir) {
9
+ this._outputDir = _outputDir;
10
+ }
11
+ Object.defineProperty(CSharpWriter.prototype, "outputInfo", {
12
+ get: function () {
13
+ return this._outputInfo;
14
+ },
15
+ enumerable: false,
16
+ configurable: true
17
+ });
18
+ CSharpWriter.prototype.resolveTypeName = function (typescriptTypeName) {
19
+ if (this._typesFileContent === undefined) {
20
+ this._typesFileContent = null;
21
+ var filePath = path.dirname(__dirname) + "/src/types.json";
22
+ if ((0, fs_1.existsSync)(filePath)) {
23
+ if (allowDebugLogs)
24
+ console.log("Reading types file");
25
+ var content = (0, fs_1.readFileSync)(filePath, "utf8");
26
+ this._typesFileContent = JSON.parse(content);
27
+ }
28
+ }
29
+ if (this._typesFileContent) {
30
+ var fullType = this._typesFileContent[typescriptTypeName];
31
+ if (fullType && allowDebugLogs)
32
+ console.log(fullType);
33
+ return fullType;
34
+ }
35
+ return null;
36
+ };
37
+ CSharpWriter.prototype.begin = function (filePath) {
38
+ };
39
+ CSharpWriter.prototype.end = function (filePath) {
40
+ };
41
+ CSharpWriter.prototype.startNewType = function (filePath, typeName, baseType, comments) {
42
+ throw new Error("Method not implemented.");
43
+ };
44
+ CSharpWriter.prototype.endNewType = function (filePath, typeName) {
45
+ throw new Error("Method not implemented.");
46
+ };
47
+ CSharpWriter.prototype.writeMember = function (visibility, name, isArray, type, initialValue, comments) {
48
+ throw new Error("Method not implemented.");
49
+ };
50
+ CSharpWriter.prototype.writeMethod = function (visibility, name, returnType, args, comments) {
51
+ throw new Error("Method not implemented.");
52
+ };
53
+ CSharpWriter.prototype.writeNewTypeExpression = function (typeName, args) {
54
+ throw new Error("Method not implemented.");
55
+ };
56
+ return CSharpWriter;
57
+ }());
58
+ exports.CSharpWriter = CSharpWriter;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ReactThreeFiberCompiler = void 0;
4
+ var ReactThreeFiberCompiler = /** @class */ (function () {
5
+ function ReactThreeFiberCompiler() {
6
+ this._typeSourceInformation = {};
7
+ }
8
+ Object.defineProperty(ReactThreeFiberCompiler.prototype, "outputInfo", {
9
+ get: function () {
10
+ return this._typeSourceInformation;
11
+ },
12
+ enumerable: false,
13
+ configurable: true
14
+ });
15
+ ReactThreeFiberCompiler.prototype.resolveTypeName = function (typescriptTypeName) {
16
+ return typescriptTypeName;
17
+ };
18
+ ReactThreeFiberCompiler.prototype.begin = function (filePath) {
19
+ };
20
+ ReactThreeFiberCompiler.prototype.end = function (filePath) {
21
+ };
22
+ ReactThreeFiberCompiler.prototype.startNewType = function (filePath, typeName, baseType, comments) {
23
+ };
24
+ ReactThreeFiberCompiler.prototype.endNewType = function (filePath, typeName) {
25
+ };
26
+ ReactThreeFiberCompiler.prototype.writeMember = function (visibility, name, isArray, type, initialValue, comments) {
27
+ };
28
+ ReactThreeFiberCompiler.prototype.writeMethod = function (visibility, name, returnType, args, comments) {
29
+ };
30
+ ReactThreeFiberCompiler.prototype.writeNewTypeExpression = function (typeName, args) {
31
+ };
32
+ return ReactThreeFiberCompiler;
33
+ }());
34
+ exports.ReactThreeFiberCompiler = ReactThreeFiberCompiler;
@@ -26,7 +26,7 @@ function writeTypeRegistry(types, registerTypesPath) {
26
26
  }
27
27
  }
28
28
  writer.writeLine("\n// Made with 🌵 Needle");
29
- writer.writeLine("\n// www.needle.tools");
29
+ writer.writeLine("// https://needle.tools");
30
30
  var code = writer.toString();
31
31
  file.write(code);
32
32
  file.end();
package/src/watcher.js CHANGED
@@ -74,4 +74,5 @@ if (fs.existsSync(outputDirectory))
74
74
  fs.rmSync(outputDirectory, { recursive: true });
75
75
  console.log("Watch: " + directoryToWatch + " and output to: " + outputDirectory);
76
76
  var watcher = new DirectoryWatcher();
77
- watcher.startWatching(directoryToWatch, new blender_compiler_1.BlenderWriter(outputDirectory), registerTypesPath);
77
+ var sink = new base_compiler_1.FileSink(outputDirectory);
78
+ watcher.startWatching(directoryToWatch, new blender_compiler_1.BlenderWriter(sink), registerTypesPath);
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var helpers_1 = require("./helpers");
4
- describe('Basic typescript', function () {
5
- it('should generate component', function () {
6
- (0, helpers_1.compareCodegenWithDefaultContext)("basic",
7
- // INPUT
8
- "export class BasicComponet extends Behaviour {\n}\n",
9
- // EXPECTED
10
- "public partial class BasicComponet : UnityEngine.MonoBehaviour\n\t{\n\t}");
11
- });
12
- it('should not generate component', function () {
13
- (0, helpers_1.compareCodegen)("basic-no-export",
14
- // INPUT
15
- "\n //@dont-generate-component\n export class BasicComponet extends Behaviour {\n}\n",
16
- // EXPECTED
17
- "");
18
- });
19
- it('should ignore abstract type', function () {
20
- (0, helpers_1.compareCodegen)("basic-no-export",
21
- // INPUT
22
- "\n //@dont-generate-component\n export abstract class BasicComponet extends Behaviour {\n}\n",
23
- // EXPECTED
24
- "");
25
- });
26
- });
@@ -1,82 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var helpers_1 = require("./helpers");
4
- describe('Typescript with public methods', function () {
5
- it('should generate component with public method', function () {
6
- (0, helpers_1.compareCodegenWithDefaultContext)("public.methods",
7
- // INPUT
8
- "export class MyComponent extends Behaviour {\n public myMethod(): void {}\n}\n",
9
- // EXPECTED
10
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public void myMethod(){}\n\t}");
11
- });
12
- it('should generate component method and number args', function () {
13
- (0, helpers_1.compareCodegenWithDefaultContext)("public method with args",
14
- // INPUT
15
- "export class MyComponent extends Behaviour {\n public myMethod(myNumber: number): void {}\n}\n",
16
- // EXPECTED
17
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public void myMethod(float @myNumber){}\n\t}");
18
- });
19
- it('should generate component method and multiple number args', function () {
20
- (0, helpers_1.compareCodegenWithDefaultContext)("public method with multiple args",
21
- // INPUT
22
- "export class MyComponent extends Behaviour {\n public myMethod(myNumber: number, myOtherNumber : number): void {}\n}\n",
23
- // EXPECTED
24
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public void myMethod(float @myNumber, float @myOtherNumber){}\n\t}");
25
- });
26
- // https://github.com/needle-tools/needle-tiny-component-compiler/issues/11
27
- it('should generate component method but ignore inline type declaration', function () {
28
- (0, helpers_1.compareCodegenWithDefaultContext)("method ignore inline type declaration",
29
- // INPUT
30
- "export class MyComponent extends Behaviour {\n public myMethod(myNumber: { x : number, y:number, z : number}): void {}\n}\n",
31
- // EXPECTED
32
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public void myMethod(object @myNumber){}\n\t}");
33
- });
34
- });
35
- describe('Typescript with private or protected methods', function () {
36
- // https://github.com/needle-tools/needle-tiny-component-compiler/issues/13
37
- it('should ignore private method', function () {
38
- (0, helpers_1.compareCodegenWithDefaultContext)("private.methods",
39
- // INPUT
40
- "export class MyComponent extends Behaviour {\n private myMethod(): void {}\n}\n",
41
- // EXPECTED
42
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n\t}");
43
- });
44
- // https://github.com/needle-tools/needle-tiny-component-compiler/issues/13
45
- it('should ignore protected method', function () {
46
- (0, helpers_1.compareCodegenWithDefaultContext)("protected.methods",
47
- // INPUT
48
- "export class MyComponent extends Behaviour {\n protected myMethod(): void {}\n}\n",
49
- // EXPECTED
50
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n\t}");
51
- });
52
- });
53
- describe('Typescript with methods', function () {
54
- it('should not generate static method', function () {
55
- (0, helpers_1.compareCodegenWithDefaultContext)("static method",
56
- // INPUT
57
- "export class MyComponent extends Behaviour {\n public static myMethod(): void {}\n}\n",
58
- // EXPECTED
59
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n\t}");
60
- });
61
- it('should not generate abstract method', function () {
62
- (0, helpers_1.compareCodegenWithDefaultContext)("abstract method",
63
- // INPUT
64
- "export class MyComponent extends Behaviour {\n public abstract myMethod(): void {}\n}\n",
65
- // EXPECTED
66
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n\t}");
67
- });
68
- it('should convert onEnable to OnEnable', function () {
69
- (0, helpers_1.compareCodegenWithDefaultContext)("abstract method",
70
- // INPUT
71
- "export class MyComponent extends Behaviour {\n onEnable() {}\n }\n}\n",
72
- // EXPECTED
73
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public void OnEnable(){}\n\t}");
74
- });
75
- it('should convert onDisable to OnDisable', function () {
76
- (0, helpers_1.compareCodegenWithDefaultContext)("abstract method",
77
- // INPUT
78
- "export class MyComponent extends Behaviour {\n onDisable() {}\n }\n}\n",
79
- // EXPECTED
80
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public void OnDisable(){}\n\t}");
81
- });
82
- });
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var helpers_1 = require("./helpers");
4
- describe('Typescript with nonSerialized', function () {
5
- it('should ignore field', function () {
6
- (0, helpers_1.compareCodegenWithDefaultContext)("nonserialized ignore field",
7
- // INPUT
8
- "export class MyComponent extends Behaviour {\n //@nonSerialized\n public myField: number;\n}\n",
9
- // EXPECTED
10
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n\t}");
11
- });
12
- // https://github.com/needle-tools/needle-tiny-component-compiler/issues/14
13
- it('should not affect next method', function () {
14
- (0, helpers_1.compareCodegenWithDefaultContext)("nonserialized on property does not affect method",
15
- // INPUT
16
- "export class MyComponent extends Behaviour {\n //@nonSerialized\n public get myField(): number { return 0; }\n\n public myMethod() {\n\n }\n}\n",
17
- // EXPECTED
18
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public void myMethod() {}\n\t}");
19
- });
20
- });
@@ -1,47 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var helpers_1 = require("./helpers");
4
- describe('Typescript with fields', function () {
5
- it('should generate component with number', function () {
6
- (0, helpers_1.compareCodegenWithDefaultContext)("number",
7
- // INPUT
8
- "export class MyComponent extends Behaviour {\n public myField: number;\n}\n",
9
- // EXPECTED
10
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public float @myField;\n\t}");
11
- });
12
- it('should generate component with string', function () {
13
- (0, helpers_1.compareCodegenWithDefaultContext)("string",
14
- // INPUT
15
- "export class MyComponent extends Behaviour {\n public myField: string;\n}\n",
16
- // EXPECTED
17
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public string @myField;\n\t}");
18
- });
19
- it('should generate component with string array', function () {
20
- (0, helpers_1.compareCodegenWithDefaultContext)("stringArray",
21
- // INPUT
22
- "export class MyComponent extends Behaviour {\n public myField: string[];\n}\n",
23
- // EXPECTED
24
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public string[] @myField;\n\t}");
25
- });
26
- it('should generate component with boolean', function () {
27
- (0, helpers_1.compareCodegenWithDefaultContext)("bool",
28
- // INPUT
29
- "export class MyComponent extends Behaviour {\n public myField: boolean;\n}\n",
30
- // EXPECTED
31
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public bool @myField;\n\t}");
32
- });
33
- it('should generate component with object', function () {
34
- (0, helpers_1.compareCodegenWithDefaultContext)("object",
35
- // INPUT
36
- "export class MyComponent extends Behaviour {\n public myField: object;\n}\n",
37
- // EXPECTED
38
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public UnityEngine.Object @myField;\n\t}");
39
- });
40
- it('should generate component with default values', function () {
41
- (0, helpers_1.compareCodegenWithDefaultContext)("fields with default values",
42
- // INPUT
43
- "export class MyComponent extends Behaviour {\n public myField: number = 42;\n public myField2: string = \"hello\";\n public myField3: boolean = true;\n public myField4: object = null;\n public myVector2: Vector2 = new Vector2(1, .5);\n}\n",
44
- // EXPECTED
45
- "public partial class MyComponent : UnityEngine.MonoBehaviour\n\t{\n public float @myField = 42f;\n public string @myField2 = \"hello\";\n public bool @myField3 = true;\n public UnityEngine.Object @myField4;\n\t\tpublic UnityEngine.Vector2 @myVector2 = new UnityEngine.Vector2(1f, .5f);\n\t}");
46
- });
47
- });
package/test/helpers.js DELETED
@@ -1,54 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compareCodegenWithDefaultContext = exports.compareCodegen = exports.testCompile = void 0;
4
- var component_compiler_1 = require("../src/component-compiler");
5
- var chai_1 = require("chai");
6
- var fs = require("fs");
7
- var TestOptions = /** @class */ (function () {
8
- function TestOptions() {
9
- this.ignoreWhiteSpace = true;
10
- }
11
- return TestOptions;
12
- }());
13
- function testCompile(code) {
14
- var res = (0, component_compiler_1.compile)(code, "test", null, false);
15
- var output = (res === null || res === void 0 ? void 0 : res.length) > 0 ? res[0] : "";
16
- return output;
17
- }
18
- exports.testCompile = testCompile;
19
- function compareByLine(output, expected) {
20
- var splitCode = /\r?\n/;
21
- if (output == null) {
22
- (0, chai_1.expect)(expected).to.equal(output);
23
- }
24
- else {
25
- var outputLines = output.split(splitCode);
26
- var expectedLines = expected.split(splitCode);
27
- for (var i = 0; i < outputLines.length; i++) {
28
- var outputLine = outputLines[i].trim();
29
- var expectedLine = expectedLines[i].trim();
30
- (0, chai_1.expect)(outputLine).to.equal(expectedLine, "Line ".concat(i, " does not match"));
31
- }
32
- }
33
- }
34
- function compareCodegen(id, input, expected) {
35
- var baseDir = "test/codegen";
36
- if (!fs.existsSync(baseDir))
37
- fs.mkdirSync(baseDir);
38
- var dir = "".concat(baseDir, "/").concat(id);
39
- if (!fs.existsSync(dir))
40
- fs.mkdirSync(dir);
41
- fs.writeFileSync("".concat(dir, "\\input.ts"), input);
42
- var result = testCompile(input);
43
- if (result !== null)
44
- fs.writeFileSync("".concat(dir, "\\output.cs"), result);
45
- fs.writeFileSync("".concat(dir, "\\expected.cs"), expected);
46
- compareByLine(result, expected);
47
- // expect(result).to.equal(expected);
48
- }
49
- exports.compareCodegen = compareCodegen;
50
- function compareCodegenWithDefaultContext(id, input, expected) {
51
- expected = "// NEEDLE_CODEGEN_START\n // auto generated code - do not edit directly\n \n #pragma warning disable\n \n namespace Needle.Typescript.GeneratedComponents\n {\n \t" + expected + "\n }\n \n // NEEDLE_CODEGEN_END";
52
- return compareCodegen(id, input, expected);
53
- }
54
- exports.compareCodegenWithDefaultContext = compareCodegenWithDefaultContext;
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es5",
4
- "types": [
5
- "mocha",
6
- "node"
7
- ]
8
- }
9
- }
@@ -1,12 +0,0 @@
1
- {
2
- "folders": [
3
- {
4
- "path": "."
5
- }
6
- ],
7
- "settings": {
8
- "files.exclude": {
9
- "test/*.js": true
10
- }
11
- }
12
- }