@needle-tools/needle-component-compiler 2.3.0-pre → 2.4.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,8 +4,11 @@ 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.3.0-pre] - 2022-11-22
7
+ ## [2.4.0-pre] - 2022-11-22
8
+ # Blender compiler
9
+ - Add: emit types from declared class implementing `IComponent` e.g. ``declare class MyComp implements IComponent``
8
10
 
11
+ ## [2.3.0-pre] - 2022-11-22
9
12
  # Blender compiler
10
13
  - Add emit array
11
14
  - Add emit types by class name if unknown
package/Readme.md CHANGED
@@ -13,4 +13,7 @@ Please run ``npm install`` first before using.
13
13
  - ``@serializeField`` field decorator, similar to ``[SerializeField]`` in Unity
14
14
  - ``@nonSerialized`` field or method decorator to skip generating c# code for a field or a method, similar to ``[NonSerialized]`` in Unity
15
15
  - ``@type MyNamespace.MyType`` decorator for fields or classes, specifiy C# type of field or class
16
- - ``@ifdef MY_IFDEF`` field decorator only at the moment
16
+ - ``@ifdef MY_IFDEF`` field decorator only at the moment
17
+
18
+ ### Test
19
+ - Run single test: `` npm run test:single -- -r ts-node/register "./test/blender/blender.from_typedef.test.ts"``
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/needle-component-compiler",
3
- "version": "2.3.0-pre",
3
+ "version": "2.4.0-pre",
4
4
  "description": "Compile mock unity components from typescript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -9,7 +9,8 @@
9
9
  "compile": "tsc",
10
10
  "test" : "npm run test:csharp & npm run test:blender",
11
11
  "test:csharp": "mocha -r ts-node/register test/csharp/**.test.ts",
12
- "test:blender": "mocha -r ts-node/register test/blender/**.test.ts"
12
+ "test:blender": "mocha -r ts-node/register test/blender/**.test.ts",
13
+ "test:single": "mocha"
13
14
  },
14
15
  "dependencies": {
15
16
  "chokidar": "^3.5.3",
@@ -209,7 +209,7 @@ var Compiler = /** @class */ (function () {
209
209
  if (node.heritageClauses) {
210
210
  for (var _i = 0, _a = node.heritageClauses; _i < _a.length; _i++) {
211
211
  var clause = _a[_i];
212
- if (clause.token == ts.SyntaxKind.ExtendsKeyword) {
212
+ if (clause.token == ts.SyntaxKind.ExtendsKeyword || clause.token == ts.SyntaxKind.ImplementsKeyword) {
213
213
  for (var _b = 0, _c = clause.types; _b < _c.length; _b++) {
214
214
  var type = _c[_b];
215
215
  var typeName = type.expression.getText();
@@ -5,6 +5,7 @@ var fs_1 = require("fs");
5
5
  var base_compiler_1 = require("./base-compiler");
6
6
  var commands_1 = require("./commands");
7
7
  var supportedTypes = ["string", "float", "int", "bool", "col", "obj", "evt", "fn", "vec2", "vec3", "comp"];
8
+ var knownBaseTypes = ["Behaviour", "IComponent"];
8
9
  var BlenderWriter = /** @class */ (function () {
9
10
  function BlenderWriter(_sink) {
10
11
  this._sink = _sink;
@@ -105,7 +106,15 @@ var BlenderWriter = /** @class */ (function () {
105
106
  delete this._currentlyProcessingFiles[filePath];
106
107
  };
107
108
  BlenderWriter.prototype.startNewType = function (filePath, typeName, baseTypes, comments) {
108
- if (baseTypes.includes("Behaviour") == false)
109
+ var isSupportedType = false;
110
+ for (var i = 0; i < knownBaseTypes.length; i++) {
111
+ if (baseTypes.includes(knownBaseTypes[i])) {
112
+ isSupportedType = true;
113
+ break;
114
+ }
115
+ }
116
+ // console.log(baseTypes, knownBaseTypes, isSupportedType)
117
+ if (!isSupportedType)
109
118
  return false;
110
119
  this.writer.beginBlock("{");
111
120
  if (comments)
package/src/test.js CHANGED
@@ -1,10 +1,58 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //@type("MyComponent")
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ exports.__esModule = true;
18
+ exports.MyTestComponent = void 0;
19
+ var MyTestComponent = /** @class */ (function (_super) {
20
+ __extends(MyTestComponent, _super);
21
+ function MyTestComponent() {
22
+ var _this = _super !== null && _super.apply(this, arguments) || this;
23
+ _this.myVector2 = new Vector2(1, .5);
24
+ return _this;
25
+ }
26
+ MyTestComponent.prototype.myMethod = function () {
27
+ };
28
+ return MyTestComponent;
29
+ }(Behaviour));
30
+ exports.MyTestComponent = MyTestComponent;
31
+ // export class SkipFieldAndMethod extends Behaviour {
32
+ // //@nonSerialized
33
+ // myMethod() {
34
+ // }
35
+ // // @nonSerialized
36
+ // myField : string;
37
+ // }
38
+ // export class ComponentWithUnknownType extends Behaviour {
39
+ // views: SomeUnknownType;
40
+ // }
41
+ // export class ComponentWithAnimationClip extends Behaviour implements IPointerClickHandler {
42
+ // @serializeable(AnimationClip)
43
+ // animation?: THREE.AnimationClip;
44
+ // }
45
+ // export abstract class MyAbstractComponent extends Behaviour {
46
+ // abstract myMethod();
47
+ // }
4
48
  // export class CameraView extends Behaviour {
5
49
  // static views: CameraView[] = [];
6
50
  // }
7
- // export class SocLoader extends Behaviour {
51
+ // export class EventComponent extends Behaviour {
52
+ // @serializeable(EventList)
53
+ // roomChanged: EventList = new EventList();
54
+ // }
55
+ // export class SocLoader extends Behaviour {
8
56
  // @serializeable(AssetReference)
9
57
  // scenes: Array<AssetReference> = [];
10
58
  // }
@@ -24,32 +72,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
24
72
  // map2 : Map<object> = new Map<object>();
25
73
  // private myThing : Test123;
26
74
  // }
27
- // // @type UnityEngine.MonoBehaviour
75
+ // //@type UnityEngine.MonoBehaviour
28
76
  // export class ButtonObject extends Interactable implements IPointerClickHandler, ISerializable {
29
77
  // //@type UnityEngine.Transform[]
30
78
  // myType?: SceneFXWindow;
31
79
  // }
32
- // export class EventComponent extends Behaviour {
33
- // @serializeable(EventList)
34
- // roomChanged: EventList = new EventList();
35
- // }
36
- // export class SkipFieldAndMethod extends Behaviour {
37
- // //@nonSerialized
38
- // myMethod() {
39
- // }
40
- // // @nonSerialized
41
- // myField : string;
42
- // }
43
- // export class ComponentWithUnknownType extends Behaviour {
44
- // views: SomeUnknownType;
45
- // }
46
- // export class ComponentWithAnimationClip extends Behaviour implements IPointerClickHandler {
47
- // @serializeable(AnimationClip)
48
- // animation?: THREE.AnimationClip;
49
- // }
50
- // export abstract class MyAbstractComponent extends Behaviour {
51
- // abstract myMethod();
52
- // }
53
80
  // import { Behaviour } from "needle.tiny.engine/engine-components/Component";
54
81
  // import { RoomEntity } from "./Room";
55
82
  // import { Behaviour } from "needle.tiny.engine/engine-components/Component";
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ exports.__esModule = 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
+ });
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ exports.__esModule = 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
+ });
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ exports.__esModule = 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
+ });
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ exports.__esModule = 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
+ });
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ exports.__esModule = 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;