@needle-tools/needle-component-compiler 1.11.0 → 1.11.2

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,6 +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
+ ## [1.11.2] - 2023-12-16
8
+ - Fix: property setter emitting invalid C# code
9
+
10
+ ## [1.11.1] - 2023-12-02
11
+ - Fix: Use Unity AnimatorController type
12
+ - Fix: method inline anonymous type declaration (e.g. `myMethod(arg: {x:number})`)
13
+
7
14
  ## [1.11.0] - 2023-11-18
8
15
  - Add: support to wrap fields with `#if UNITY_EDITOR` if their namespace contains UnityEditor
9
16
  - Add: more threejs types to generate Unity Material fields for
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/needle-component-compiler",
3
- "version": "1.11.0",
3
+ "version": "1.11.2",
4
4
  "description": "Compile mock unity components from typescript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -19,7 +19,7 @@
19
19
  "chai": "^4.3.6",
20
20
  "mocha": "^10.0.0",
21
21
  "ts-node": "^10.9.1",
22
- "npm-watch": "^0.11.0"
22
+ "npm-watch": "^0.13.0"
23
23
  },
24
24
  "watch": {
25
25
  "compile": {
@@ -270,7 +270,17 @@ function run(program, outputDir, sourceFile) {
270
270
  var type = tryResolveTypeRecursive(param);
271
271
  if (type === undefined)
272
272
  type = "object";
273
- paramsStr += type + " @" + param.name.getText();
273
+ var paramName = "";
274
+ var paramNameKind = param.name.kind;
275
+ switch (paramNameKind) {
276
+ case ts.SyntaxKind.ObjectBindingPattern:
277
+ paramName = "obj";
278
+ break;
279
+ default:
280
+ paramName = param.name.getText();
281
+ break;
282
+ }
283
+ paramsStr += type + " @" + paramName;
274
284
  }
275
285
  var methodName = meth.name.getText();
276
286
  switch (methodName) {
@@ -292,7 +302,7 @@ function run(program, outputDir, sourceFile) {
292
302
  if (!context)
293
303
  break;
294
304
  if (allowDebugLogs)
295
- console.log("Found variable", node.getText());
305
+ console.log("Found variable", ts.SyntaxKind[node.kind], "\n", node.getText());
296
306
  var vardec = node;
297
307
  var varName = "@" + vardec.name.getText();
298
308
  var pub = shouldEmitMethod(vardec);
@@ -610,8 +620,30 @@ function run(program, outputDir, sourceFile) {
610
620
  typeName = varDec.type.getText();
611
621
  }
612
622
  var res = undefined;
623
+ // if it's a { x: number } inline object type:
624
+ if (typeName.startsWith("{")) {
625
+ // check if it has XYZW we assume it's a vector
626
+ var hasX = typeName.includes("x");
627
+ var hasY = typeName.includes("y");
628
+ var hasZ = typeName.includes("z");
629
+ var hasW = typeName.includes("w");
630
+ if (hasX && hasY && hasZ && hasW) {
631
+ return tryGetTypeFromText("Vector4");
632
+ }
633
+ else if (hasX && hasY && hasZ) {
634
+ return tryGetTypeFromText("Vector3");
635
+ }
636
+ else if (hasX && hasY) {
637
+ return tryGetTypeFromText("Vector2");
638
+ }
639
+ return "object";
640
+ }
641
+ // const kindName = ts.SyntaxKind[node.kind];
613
642
  // console.log("Unknown type: " + typeName);
614
643
  switch (node.kind) {
644
+ case ts.SyntaxKind.ObjectBindingPattern:
645
+ res = "object";
646
+ break;
615
647
  // case ts.SyntaxKind.SyntaxList:
616
648
  // const list = node as ts.SyntaxList;
617
649
  // for (const ch of list._children) {
@@ -669,8 +701,14 @@ function run(program, outputDir, sourceFile) {
669
701
  var isInGenericDeclaration = false;
670
702
  for (var _b = 0, _c = node.getChildren(); _b < _c.length; _b++) {
671
703
  var child = _c[_b];
672
- // if (res !== undefined) break;
673
- // console.log("Child type: " + ts.SyntaxKind[child.kind]);
704
+ // Fix https://linear.app/needle/issue/NE-4423
705
+ if (child.kind === ts.SyntaxKind.Block) {
706
+ if (allowDebugLogs)
707
+ console.log("Skip block");
708
+ continue;
709
+ }
710
+ if (allowDebugLogs)
711
+ console.log("Child type: " + ts.SyntaxKind[child.kind]);
674
712
  var isGenericStart = false;
675
713
  var isAssignment = false;
676
714
  switch (child.kind) {
@@ -305,7 +305,17 @@ export function run(program: ts.Program, outputDir: string | null, sourceFile: t
305
305
  if (paramsStr.length > 0) paramsStr += ", ";
306
306
  let type = tryResolveTypeRecursive(param);
307
307
  if (type === undefined) type = "object";
308
- paramsStr += type + " @" + param.name.getText();
308
+ let paramName = "";
309
+ const paramNameKind = param.name.kind;
310
+ switch (paramNameKind) {
311
+ case ts.SyntaxKind.ObjectBindingPattern:
312
+ paramName = "obj"
313
+ break;
314
+ default:
315
+ paramName = param.name.getText();
316
+ break;
317
+ }
318
+ paramsStr += type + " @" + paramName;
309
319
  }
310
320
  let methodName = meth.name.getText();
311
321
  switch (methodName) {
@@ -324,7 +334,7 @@ export function run(program: ts.Program, outputDir: string | null, sourceFile: t
324
334
  resetExportNextClass();
325
335
  if (!context) break;
326
336
  if (allowDebugLogs)
327
- console.log("Found variable", node.getText());
337
+ console.log("Found variable", ts.SyntaxKind[node.kind], "\n", node.getText());
328
338
  const vardec = node as ts.VariableDeclaration;
329
339
 
330
340
  const varName = "@" + vardec.name.getText();
@@ -639,11 +649,33 @@ export function run(program: ts.Program, outputDir: string | null, sourceFile: t
639
649
 
640
650
  let res: string | undefined = undefined;
641
651
 
652
+ // if it's a { x: number } inline object type:
653
+ if (typeName.startsWith("{")) {
654
+ // check if it has XYZW we assume it's a vector
655
+ const hasX = typeName.includes("x");
656
+ const hasY = typeName.includes("y");
657
+ const hasZ = typeName.includes("z");
658
+ const hasW = typeName.includes("w");
659
+ if (hasX && hasY && hasZ && hasW) {
660
+ return tryGetTypeFromText("Vector4");
661
+ }
662
+ else if (hasX && hasY && hasZ) {
663
+ return tryGetTypeFromText("Vector3");
664
+ }
665
+ else if (hasX && hasY) {
666
+ return tryGetTypeFromText("Vector2");
667
+ }
642
668
 
669
+ return "object";
670
+ }
643
671
 
672
+ // const kindName = ts.SyntaxKind[node.kind];
644
673
  // console.log("Unknown type: " + typeName);
645
674
 
646
675
  switch (node.kind) {
676
+ case ts.SyntaxKind.ObjectBindingPattern:
677
+ res = "object";
678
+ break;
647
679
  // case ts.SyntaxKind.SyntaxList:
648
680
  // const list = node as ts.SyntaxList;
649
681
  // for (const ch of list._children) {
@@ -703,8 +735,15 @@ export function run(program: ts.Program, outputDir: string | null, sourceFile: t
703
735
 
704
736
  let isInGenericDeclaration = false;
705
737
  for (const child of node.getChildren()) {
706
- // if (res !== undefined) break;
707
- // console.log("Child type: " + ts.SyntaxKind[child.kind]);
738
+
739
+
740
+ // Fix https://linear.app/needle/issue/NE-4423
741
+ if (child.kind === ts.SyntaxKind.Block) {
742
+ if(allowDebugLogs) console.log("Skip block");
743
+ continue;
744
+ }
745
+
746
+ if(allowDebugLogs) console.log("Child type: " + ts.SyntaxKind[child.kind]);
708
747
  let isGenericStart = false;
709
748
  let isAssignment = false;
710
749
  switch (child.kind) {
package/src/types.js CHANGED
@@ -37,6 +37,7 @@ const dict = {
37
37
  "AnimationClip" : "UnityEngine.AnimationClip",
38
38
  "SignalAsset" : "UnityEngine.Timeline.SignalAsset",
39
39
  "PlayableDirector" : "UnityEngine.Playables.PlayableDirector",
40
+ "AnimatorController": "UnityEditor.Animations.AnimatorController",
40
41
  // Rendering
41
42
  "Renderer" : "UnityEngine.Renderer",
42
43
  "Material" : "UnityEngine.Material",