@needle-tools/needle-component-compiler 1.9.0 → 1.9.1

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.
Files changed (86) hide show
  1. package/Changelog.md +3 -0
  2. package/PUBLISH.bat +1 -1
  3. package/package.json +1 -1
  4. package/src/component-compiler.js +18 -2
  5. package/src/component-compiler.ts +16 -3
  6. package/src/test.js +194 -0
  7. package/src/test.ts +1 -0
  8. package/test/component.basic.test.js +26 -0
  9. package/test/component.methods.test.js +82 -0
  10. package/test/component.nonserialized.test.js +20 -0
  11. package/test/component.primitives.test.js +47 -0
  12. package/test/component.primitives.test.ts +4 -2
  13. package/test/helpers.js +54 -0
  14. package/workspace.code-workspace +11 -7
  15. package/test/codegen/abstract method/expected.cs +0 -14
  16. package/test/codegen/abstract method/input.js +0 -27
  17. package/test/codegen/abstract method/input.ts +0 -4
  18. package/test/codegen/abstract method/output.cs +0 -14
  19. package/test/codegen/basic/expected.cs +0 -13
  20. package/test/codegen/basic/input.js +0 -26
  21. package/test/codegen/basic/input.ts +0 -2
  22. package/test/codegen/basic/output.cs +0 -13
  23. package/test/codegen/basic-no-export/expected.cs +0 -0
  24. package/test/codegen/basic-no-export/input.js +0 -27
  25. package/test/codegen/basic-no-export/input.ts +0 -4
  26. package/test/codegen/basic-no-export/output.cs +0 -0
  27. package/test/codegen/bool/expected.cs +0 -14
  28. package/test/codegen/bool/input.js +0 -26
  29. package/test/codegen/bool/input.ts +0 -3
  30. package/test/codegen/bool/output.cs +0 -14
  31. package/test/codegen/fields with default values/expected.cs +0 -17
  32. package/test/codegen/fields with default values/input.js +0 -31
  33. package/test/codegen/fields with default values/input.ts +0 -6
  34. package/test/codegen/fields with default values/output.cs +0 -17
  35. package/test/codegen/method ignore inline type declaration/expected.cs +0 -14
  36. package/test/codegen/method ignore inline type declaration/input.js +0 -27
  37. package/test/codegen/method ignore inline type declaration/input.ts +0 -3
  38. package/test/codegen/method ignore inline type declaration/output.cs +0 -14
  39. package/test/codegen/nonserialized ignore field/expected.cs +0 -13
  40. package/test/codegen/nonserialized ignore field/input.js +0 -26
  41. package/test/codegen/nonserialized ignore field/input.ts +0 -4
  42. package/test/codegen/nonserialized ignore field/output.cs +0 -13
  43. package/test/codegen/nonserialized on property does not affect method/expected.cs +0 -14
  44. package/test/codegen/nonserialized on property does not affect method/input.js +0 -34
  45. package/test/codegen/nonserialized on property does not affect method/input.ts +0 -8
  46. package/test/codegen/nonserialized on property does not affect method/output.cs +0 -13
  47. package/test/codegen/number/expected.cs +0 -14
  48. package/test/codegen/number/input.js +0 -26
  49. package/test/codegen/number/input.ts +0 -3
  50. package/test/codegen/number/output.cs +0 -14
  51. package/test/codegen/object/expected.cs +0 -14
  52. package/test/codegen/object/input.js +0 -26
  53. package/test/codegen/object/input.ts +0 -3
  54. package/test/codegen/object/output.cs +0 -14
  55. package/test/codegen/private.methods/expected.cs +0 -13
  56. package/test/codegen/private.methods/input.js +0 -27
  57. package/test/codegen/private.methods/input.ts +0 -3
  58. package/test/codegen/private.methods/output.cs +0 -13
  59. package/test/codegen/protected.methods/expected.cs +0 -13
  60. package/test/codegen/protected.methods/input.js +0 -27
  61. package/test/codegen/protected.methods/input.ts +0 -3
  62. package/test/codegen/protected.methods/output.cs +0 -13
  63. package/test/codegen/public method with args/expected.cs +0 -14
  64. package/test/codegen/public method with args/input.js +0 -27
  65. package/test/codegen/public method with args/input.ts +0 -3
  66. package/test/codegen/public method with args/output.cs +0 -14
  67. package/test/codegen/public method with multiple args/expected.cs +0 -14
  68. package/test/codegen/public method with multiple args/input.js +0 -27
  69. package/test/codegen/public method with multiple args/input.ts +0 -3
  70. package/test/codegen/public method with multiple args/output.cs +0 -14
  71. package/test/codegen/public.methods/expected.cs +0 -14
  72. package/test/codegen/public.methods/input.js +0 -27
  73. package/test/codegen/public.methods/input.ts +0 -3
  74. package/test/codegen/public.methods/output.cs +0 -14
  75. package/test/codegen/static method/expected.cs +0 -13
  76. package/test/codegen/static method/input.js +0 -27
  77. package/test/codegen/static method/input.ts +0 -3
  78. package/test/codegen/static method/output.cs +0 -14
  79. package/test/codegen/string/expected.cs +0 -14
  80. package/test/codegen/string/input.js +0 -26
  81. package/test/codegen/string/input.ts +0 -3
  82. package/test/codegen/string/output.cs +0 -14
  83. package/test/codegen/stringArray/expected.cs +0 -14
  84. package/test/codegen/stringArray/input.js +0 -26
  85. package/test/codegen/stringArray/input.ts +0 -3
  86. package/test/codegen/stringArray/output.cs +0 -14
package/Changelog.md CHANGED
@@ -4,6 +4,9 @@ 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
+ ## [1.9.1] - 2022-11-05
8
+ - Fix ``new Vector2(1, .5)`` generating invalid C# where number arguments were emitted as double instead of float
9
+
7
10
  ## [1.9.0] - 2022-10-18
8
11
  - Change: emit ``onEnable`` as ``OnEnable`` and ``onDisable`` as ``OnDisable`` so Unity creates component checkboxes
9
12
  - Change: ignore private and protected methods
package/PUBLISH.bat CHANGED
@@ -3,6 +3,6 @@ echo Press a key to publish! (close the window if you changed your mind)
3
3
  pause
4
4
  echo Publishing in 2 sec
5
5
  timeout 2
6
- npm set registry https://registry.npmjs.org && npm publish
6
+ npm run compile & npm set registry https://registry.npmjs.org && npm publish & pause
7
7
  echo Finished...
8
8
  timeout 10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/needle-component-compiler",
3
- "version": "1.9.0",
3
+ "version": "1.9.1",
4
4
  "description": "Compile mock unity components from typescript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -462,14 +462,30 @@ function run(program, outputDir, sourceFile) {
462
462
  type = tryGetTypeFromText(text);
463
463
  break;
464
464
  case ts.SyntaxKind.SyntaxList:
465
- args = text;
465
+ for (var _b = 0, _c = ch.getChildren(); _b < _c.length; _b++) {
466
+ var arg = _c[_b];
467
+ if (args === undefined)
468
+ args = "";
469
+ var res = getTypeForAssignment(arg);
470
+ // handle floats being assigned with "f" suffix
471
+ if (Number.parseFloat(res) >= 0) {
472
+ args += res + "f";
473
+ }
474
+ else {
475
+ args += res;
476
+ if (res === ",")
477
+ args += " ";
478
+ }
479
+ }
466
480
  break;
467
481
  }
468
482
  }
469
483
  if (!args)
470
484
  args = "";
471
- if (type)
485
+ if (type) {
486
+ console.log(type, args);
472
487
  return "new " + type + "(" + args + ")";
488
+ }
473
489
  // const expType = node.getChildren().find(c => c.kind === ts.SyntaxKind.Identifier);
474
490
  break;
475
491
  }
@@ -273,7 +273,7 @@ export function run(program: ts.Program, outputDir: string | null, sourceFile: t
273
273
  // const isCoroutine = func.asteriskToken;
274
274
  if (!skip && meth.name) {
275
275
  const pub = isPublic(meth);
276
- if(!pub) return;
276
+ if (!pub) return;
277
277
 
278
278
  let paramsStr = "";
279
279
  for (let param of meth.parameters) {
@@ -485,13 +485,26 @@ export function run(program: ts.Program, outputDir: string | null, sourceFile: t
485
485
  type = tryGetTypeFromText(text);
486
486
  break;
487
487
  case ts.SyntaxKind.SyntaxList:
488
- args = text;
488
+ for (const arg of ch.getChildren()) {
489
+ if (args === undefined) args = "";
490
+ const res = getTypeForAssignment(arg);
491
+ // handle floats being assigned with "f" suffix
492
+ if (Number.parseFloat(res) >= 0) {
493
+ args += res + "f";
494
+ }
495
+ else {
496
+ args += res;
497
+ if (res === ",") args += " ";
498
+ }
499
+ }
489
500
  break;
490
501
  }
491
502
  }
492
503
  if (!args) args = "";
493
- if (type)
504
+ if (type) {
505
+ console.log(type, args)
494
506
  return "new " + type + "(" + args + ")";
507
+ }
495
508
  // const expType = node.getChildren().find(c => c.kind === ts.SyntaxKind.Identifier);
496
509
  break;
497
510
  }
package/src/test.js ADDED
@@ -0,0 +1,194 @@
1
+ "use strict";
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
+ // }
48
+ // export class CameraView extends Behaviour {
49
+ // static views: CameraView[] = [];
50
+ // }
51
+ // export class EventComponent extends Behaviour {
52
+ // @serializeable(EventList)
53
+ // roomChanged: EventList = new EventList();
54
+ // }
55
+ // export class SocLoader extends Behaviour {
56
+ // @serializeable(AssetReference)
57
+ // scenes: Array<AssetReference> = [];
58
+ // }
59
+ // // class Test123 {}
60
+ // export class Bla extends Behaviour
61
+ // {
62
+ // myNumber:number = 42;
63
+ // myBool:boolean = true;
64
+ // myString:string = "test";
65
+ // numberArr: number[] = [1,2,3];
66
+ // myColor : THREE.Color = new THREE.Color(255, 0, 0);
67
+ // renderers = new Array<Renderer>();
68
+ // renderers2 : Renderer[] = [];
69
+ // objArr : object[];
70
+ // colArr: THREE.Color[] = [new THREE.Color(1,2,3)];
71
+ // map : Map<string> = new Map<string>();
72
+ // map2 : Map<object> = new Map<object>();
73
+ // private myThing : Test123;
74
+ // }
75
+ // //@type UnityEngine.MonoBehaviour
76
+ // export class ButtonObject extends Interactable implements IPointerClickHandler, ISerializable {
77
+ // //@type UnityEngine.Transform[]
78
+ // myType?: SceneFXWindow;
79
+ // }
80
+ // import { Behaviour } from "needle.tiny.engine/engine-components/Component";
81
+ // import { RoomEntity } from "./Room";
82
+ // import { Behaviour } from "needle.tiny.engine/engine-components/Component";
83
+ // export class MyNewScript extends DriveClient
84
+ // {
85
+ // //@type test
86
+ // texture : RenderTexture;
87
+ // }
88
+ // namespace Hello.World
89
+ // {
90
+ // namespace Deep {
91
+ // export class MyClass extends Behaviour {
92
+ // //@ifdef TEST
93
+ // public myFloat :number;
94
+ // }
95
+ // }
96
+ // }
97
+ // class OtherClass extends Behaviour {
98
+ // }
99
+ //@type (RoomEntity)
100
+ // export class NavigationManager extends RoomEntity {
101
+ // fl:number = 1;
102
+ // nav_forward() {
103
+ // }
104
+ // nav_backward() {
105
+ // }
106
+ // }
107
+ // export abstract class NavComponent extends Behaviour {
108
+ // abstract next();
109
+ // abstract prev();
110
+ // abstract isAtEnd():boolean;
111
+ // }
112
+ // export class PointOfInterest extends Behaviour {
113
+ // myVal:number = 12;
114
+ // // @type(HELLO)
115
+ // myFunction(){
116
+ // }
117
+ // // @type(UnityEngine.Camera)
118
+ // view?:Camera;
119
+ // test:string = "123";
120
+ // // test
121
+ // }
122
+ // export class MaterialColorHandler extends Behaviour {
123
+ // @serializeable(Renderer)
124
+ // renderer?: Renderer[];
125
+ // }
126
+ // export class MyArray extends Behaviour {
127
+ // arr? : Array<number> = [1,2,3];
128
+ // }
129
+ // export class PrivateSerializedField extends Behaviour {
130
+ // //@serializeField
131
+ // private color? : THREE.Color;
132
+ // }
133
+ // export class MyPropertyClass extends Behaviour {
134
+ // set color(col: THREE.Color) {
135
+ // }
136
+ // }
137
+ // export class MyClassWithAFloat extends Behaviour {
138
+ // myfloat:number = .5;
139
+ // private myString : string;
140
+ // }
141
+ // export class GltfExport extends Behaviour {
142
+ // binary: boolean = true;
143
+ // "$serializedTypes" = {
144
+ // url:null
145
+ // }
146
+ // // @contextmenu enable this
147
+ // test(){
148
+ // }
149
+ // }
150
+ // export class SetColor extends Behaviour {
151
+ // "@serializedTypes" = {
152
+ // col: Number,
153
+ // }
154
+ // }
155
+ // class Behaviour {
156
+ // }
157
+ // export class PlatformerMusic extends Behaviour implements IPlaymodeChangeListener {
158
+ // source?: AudioSource;
159
+ // editMode?: string;
160
+ // playMode?: string;
161
+ // onPlaymodeChange(playmode: PlayMode): void {
162
+ // console.log(this);
163
+ // if(!this.source) return;
164
+ // const clip = playmode.isInPlayMode ? this.playMode : this.editMode;
165
+ // console.log("PLAY", clip);
166
+ // this.source.play(clip);
167
+ // }
168
+ // }
169
+ // // TODO: export UnityEvent like this
170
+ // // disable codegen
171
+ // class UnityEvent {
172
+ // methods: Function[] = [];
173
+ // invoke() {
174
+ // for (const m of this.methods) {
175
+ // m();
176
+ // }
177
+ // }
178
+ // }
179
+ // export class MyClass extends Behaviour {
180
+ // myFloat: number = 15;
181
+ // myBool: boolean;
182
+ // // just some default values
183
+ // myArray: number[] = [1, 2, 3];
184
+ // // comment for myString
185
+ // myString: string = "this is a string1";
186
+ // myObject: THREE.Object3D;
187
+ // myBool2: boolean;
188
+ // myFunction() { }
189
+ // myFunctionWithStringParameter(string: string) { }
190
+ // myFunctionWithSomeObjectAndArray(obj: THREE.Object3D, arr: number[]) { }
191
+ // myFunctionWithoutParamTypes(test) { }
192
+ // someOtherStuff: THREE.Object3D[] | null = null;
193
+ // myEvent: UnityEvent;
194
+ // }
package/src/test.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ThisExpression } from "typescript";
2
2
 
3
3
  export class MyTestComponent extends Behaviour {
4
+ public myVector2: Vector2 = new Vector2(1, .5);
4
5
  private myMethod(){
5
6
 
6
7
  }
@@ -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
+ });
@@ -91,7 +91,7 @@ describe('Typescript with fields', () => {
91
91
  });
92
92
 
93
93
 
94
-
94
+
95
95
 
96
96
  it('should generate component with default values', () => {
97
97
  compareCodegenWithDefaultContext("fields with default values",
@@ -101,6 +101,7 @@ describe('Typescript with fields', () => {
101
101
  public myField2: string = "hello";
102
102
  public myField3: boolean = true;
103
103
  public myField4: object = null;
104
+ public myVector2: Vector2 = new Vector2(1, .5);
104
105
  }
105
106
  `,
106
107
  // EXPECTED
@@ -109,7 +110,8 @@ describe('Typescript with fields', () => {
109
110
  public float @myField = 42f;
110
111
  public string @myField2 = "hello";
111
112
  public bool @myField3 = true;
112
- public UnityEngine.Object @myField4 = null;
113
+ public UnityEngine.Object @myField4;
114
+ public UnityEngine.Vector2 @myVector2 = new UnityEngine.Vector2(1f, .5f);
113
115
  }`
114
116
  );
115
117
  });
@@ -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;
@@ -1,8 +1,12 @@
1
- {
2
- "folders": [
3
- {
4
- "path": "."
5
- }
6
- ],
7
- "settings": {}
1
+ {
2
+ "folders": [
3
+ {
4
+ "path": "."
5
+ }
6
+ ],
7
+ "settings": {
8
+ "files.exclude": {
9
+ "test/*.js": true
10
+ }
11
+ }
8
12
  }
@@ -1,14 +0,0 @@
1
- // NEEDLE_CODEGEN_START
2
- // auto generated code - do not edit directly
3
-
4
- #pragma warning disable
5
-
6
- namespace Needle.Typescript.GeneratedComponents
7
- {
8
- public partial class MyComponent : UnityEngine.MonoBehaviour
9
- {
10
- public void OnDisable(){}
11
- }
12
- }
13
-
14
- // NEEDLE_CODEGEN_END
@@ -1,27 +0,0 @@
1
- "use strict";
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.MyComponent = void 0;
19
- var MyComponent = /** @class */ (function (_super) {
20
- __extends(MyComponent, _super);
21
- function MyComponent() {
22
- return _super !== null && _super.apply(this, arguments) || this;
23
- }
24
- MyComponent.prototype.onDisable = function () { };
25
- return MyComponent;
26
- }(Behaviour));
27
- exports.MyComponent = MyComponent;
@@ -1,4 +0,0 @@
1
- export class MyComponent extends Behaviour {
2
- onDisable() {}
3
- }
4
- }
@@ -1,14 +0,0 @@
1
- // NEEDLE_CODEGEN_START
2
- // auto generated code - do not edit directly
3
-
4
- #pragma warning disable
5
-
6
- namespace Needle.Typescript.GeneratedComponents
7
- {
8
- public partial class MyComponent : UnityEngine.MonoBehaviour
9
- {
10
- public void OnDisable(){}
11
- }
12
- }
13
-
14
- // NEEDLE_CODEGEN_END
@@ -1,13 +0,0 @@
1
- // NEEDLE_CODEGEN_START
2
- // auto generated code - do not edit directly
3
-
4
- #pragma warning disable
5
-
6
- namespace Needle.Typescript.GeneratedComponents
7
- {
8
- public partial class BasicComponet : UnityEngine.MonoBehaviour
9
- {
10
- }
11
- }
12
-
13
- // NEEDLE_CODEGEN_END