@needle-tools/needle-component-compiler 1.1.0 → 1.2.0

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,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.1.0] - 2022-06-15
8
+ - allow adding ``@type(MyNamespace.MyType)`` to class declaration
9
+
7
10
  ## [1.1.0] - 2022-06-13
8
11
  - add ``@type(MyNamespace.MyType)`` decorator for fields to specifiy C# type
9
12
  - add minimal docs to readme
@@ -0,0 +1,13 @@
1
+ // auto generated code - do not edit directly
2
+
3
+ #pragma warning disable
4
+
5
+ namespace Needle.Typescript.GeneratedComponents
6
+ {
7
+ public partial class NavigationManager : RoomEntity
8
+ {
9
+ public float @fl = 1f;
10
+ public void nav_forward(){}
11
+ public void nav_backward(){}
12
+ }
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/needle-component-compiler",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Compile mock unity components from typescript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -11,7 +11,7 @@ var exportNextClassCommand = "@generate-component";
11
11
  var dontExportNextClassCommand = "@dont-generate-component";
12
12
  // add above field to add [SerializeField] attribute
13
13
  var serializeCommand = "@serializeField";
14
- var typePattern = new RegExp("@type\((?<type>.+)\)");
14
+ var typePattern = new RegExp("@type ?\((?<type>.+)\)");
15
15
  // will be set to true when e.g. a comment for export is found
16
16
  var exportNextClass = false;
17
17
  var dontExportNextClass = false;
@@ -78,16 +78,18 @@ function run(program, outputDir, sourceFile) {
78
78
  ts.forEachChild(node, traverseFile);
79
79
  }
80
80
  function visit(node) {
81
- var _a, _b, _c;
81
+ var _a, _b, _c, _d;
82
82
  var context = contexts.length > 0 ? contexts[contexts.length - 1] : null;
83
- if ((context === null || context === void 0 ? void 0 : context.classEnd) > 0 && node.pos >= (context === null || context === void 0 ? void 0 : context.classEnd)) {
84
- while (context.indentLevel > 0) {
85
- context.indentLevel -= 1;
86
- context.append("}\n");
83
+ if (context) {
84
+ if ((context === null || context === void 0 ? void 0 : context.classEnd) > 0 && node.pos >= (context === null || context === void 0 ? void 0 : context.classEnd)) {
85
+ while (context.indentLevel > 0) {
86
+ context.indentLevel -= 1;
87
+ context.append("}\n");
88
+ }
89
+ context.flush();
90
+ context = null;
91
+ contexts.pop();
87
92
  }
88
- context.flush();
89
- context = null;
90
- contexts.pop();
91
93
  }
92
94
  console.log("\t", ts.SyntaxKind[node.kind]);
93
95
  var commentRanges = ts.getLeadingCommentRanges(sourceFile.getFullText(), node.getFullStart());
@@ -107,26 +109,23 @@ function run(program, outputDir, sourceFile) {
107
109
  }
108
110
  else if (comment.includes(serializeCommand))
109
111
  serializeField = true;
110
- else {
111
- console.log(comment);
112
- var match = typePattern.exec(comment);
113
- if (match && match.groups) {
114
- // for some reason our regex does also match surrounding ( ) even tho: https://regex101.com/r/PoWK6V/1
115
- // so we remove them
116
- var type = match.groups["type"];
117
- if (type.startsWith("("))
118
- type = type.slice(1);
119
- if (type.endsWith(")"))
120
- type = type.slice(0, -1);
121
- console.log("found type: ", type);
122
- lastTypeFound = type;
123
- }
124
- }
125
112
  }
126
113
  if (comment.includes(exportNextClassCommand))
127
114
  exportNextClass = true;
128
115
  if (comment.includes(dontExportNextClassCommand))
129
116
  dontExportNextClass = true;
117
+ var match = typePattern.exec(comment);
118
+ if (match && match.groups) {
119
+ // for some reason our regex does also match surrounding ( ) even tho: https://regex101.com/r/PoWK6V/1
120
+ // so we remove them
121
+ var type = match.groups["type"];
122
+ if (type.startsWith("("))
123
+ type = type.slice(1);
124
+ if (type.endsWith(")"))
125
+ type = type.slice(0, -1);
126
+ console.log("found type: ", type);
127
+ lastTypeFound = type;
128
+ }
130
129
  }
131
130
  }
132
131
  switch (node.kind) {
@@ -147,8 +146,8 @@ function run(program, outputDir, sourceFile) {
147
146
  // const isCoroutine = func.asteriskToken;
148
147
  if (meth.name) {
149
148
  var paramsStr = "";
150
- for (var _d = 0, _e = meth.parameters; _d < _e.length; _d++) {
151
- var param = _e[_d];
149
+ for (var _e = 0, _f = meth.parameters; _e < _f.length; _e++) {
150
+ var param = _f[_e];
152
151
  if (!param || !param.name)
153
152
  continue;
154
153
  if (paramsStr.length > 0)
@@ -181,8 +180,8 @@ function run(program, outputDir, sourceFile) {
181
180
  var prefix = typeString === undefined ? "// " : "";
182
181
  var assignment = "";
183
182
  if (typeString !== undefined) {
184
- for (var _f = 0, _g = node.getChildren(); _f < _g.length; _f++) {
185
- var ch = _g[_f];
183
+ for (var _g = 0, _h = node.getChildren(); _g < _h.length; _g++) {
184
+ var ch = _h[_g];
186
185
  switch (ch.kind) {
187
186
  case ts.SyntaxKind.FalseKeyword:
188
187
  case ts.SyntaxKind.TrueKeyword:
@@ -217,12 +216,11 @@ function run(program, outputDir, sourceFile) {
217
216
  break;
218
217
  case ts.SyntaxKind.ClassDeclaration:
219
218
  serializeField = false;
220
- lastTypeFound = null;
221
219
  var dec = node;
222
220
  // a class must inherit a component
223
- if (!dontExportNextClass && (exportNextClass || testInheritsComponent(node))) {
221
+ if (!dontExportNextClass && (lastTypeFound || exportNextClass || testInheritsComponent(node))) {
224
222
  resetExportNextClass();
225
- var name_2 = dec.name.escapedText;
223
+ var name_2 = (_d = dec.name) === null || _d === void 0 ? void 0 : _d.escapedText;
226
224
  console.log("Found class: ", name_2);
227
225
  var newContext = new ExportContext(outputDir, name_2 + ".cs");
228
226
  newContext.appendLine("// auto generated code - do not edit directly");
@@ -233,12 +231,15 @@ function run(program, outputDir, sourceFile) {
233
231
  newContext.appendLine("{");
234
232
  newContext.indentLevel += 1;
235
233
  // newContext.appendLine("// source: " + path.resolve(sourceFile.fileName));
236
- newContext.appendLine("public partial class " + name_2 + " : UnityEngine.MonoBehaviour");
234
+ var typeName = lastTypeFound !== null && lastTypeFound !== void 0 ? lastTypeFound : "UnityEngine.MonoBehaviour";
235
+ console.log(name_2 + " inherits " + typeName);
236
+ newContext.appendLine("public partial class " + name_2 + " : " + typeName);
237
237
  newContext.appendLine("{");
238
238
  newContext.indentLevel += 1;
239
239
  newContext.classEnd = dec.end;
240
240
  contexts.push(newContext);
241
241
  }
242
+ lastTypeFound = null;
242
243
  break;
243
244
  }
244
245
  function testInheritsComponent(node) {
@@ -11,7 +11,7 @@ const exportNextClassCommand = "@generate-component";
11
11
  const dontExportNextClassCommand = "@dont-generate-component";
12
12
  // add above field to add [SerializeField] attribute
13
13
  const serializeCommand = "@serializeField";
14
- const typePattern = new RegExp("@type\((?<type>.+)\)");
14
+ const typePattern = new RegExp("@type ?\((?<type>.+)\)");
15
15
 
16
16
  // will be set to true when e.g. a comment for export is found
17
17
  let exportNextClass: boolean = false;
@@ -81,7 +81,7 @@ class ExportContext {
81
81
 
82
82
  const contexts: ExportContext[] = [];
83
83
 
84
- let lastTypeFound : string | null = null;
84
+ let lastTypeFound: string | null = null;
85
85
 
86
86
  export function run(program: ts.Program, outputDir: string, sourceFile: ts.SourceFile) {
87
87
 
@@ -100,14 +100,16 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
100
100
  function visit(node: ts.Node) {
101
101
  let context: ExportContext | null = contexts.length > 0 ? contexts[contexts.length - 1] : null;
102
102
 
103
- if (context?.classEnd > 0 && node.pos >= context?.classEnd) {
104
- while (context.indentLevel > 0) {
105
- context.indentLevel -= 1;
106
- context.append("}\n");
103
+ if (context) {
104
+ if (context?.classEnd > 0 && node.pos >= context?.classEnd) {
105
+ while (context.indentLevel > 0) {
106
+ context.indentLevel -= 1;
107
+ context.append("}\n");
108
+ }
109
+ context.flush();
110
+ context = null;
111
+ contexts.pop();
107
112
  }
108
- context.flush();
109
- context = null;
110
- contexts.pop();
111
113
  }
112
114
  console.log("\t", ts.SyntaxKind[node.kind]);
113
115
 
@@ -128,24 +130,21 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
128
130
  }
129
131
  else if (comment.includes(serializeCommand))
130
132
  serializeField = true;
131
- else {
132
- console.log(comment);
133
- const match = typePattern.exec(comment);
134
- if(match && match.groups){
135
- // for some reason our regex does also match surrounding ( ) even tho: https://regex101.com/r/PoWK6V/1
136
- // so we remove them
137
- let type = match.groups["type"];
138
- if(type.startsWith("(")) type = type.slice(1);
139
- if(type.endsWith(")")) type = type.slice(0, -1);
140
- console.log("found type: ", type);
141
- lastTypeFound = type;
142
- }
143
- }
144
133
  }
145
134
  if (comment.includes(exportNextClassCommand))
146
135
  exportNextClass = true;
147
136
  if (comment.includes(dontExportNextClassCommand))
148
137
  dontExportNextClass = true;
138
+ const match = typePattern.exec(comment);
139
+ if (match && match.groups) {
140
+ // for some reason our regex does also match surrounding ( ) even tho: https://regex101.com/r/PoWK6V/1
141
+ // so we remove them
142
+ let type = match.groups["type"];
143
+ if (type.startsWith("(")) type = type.slice(1);
144
+ if (type.endsWith(")")) type = type.slice(0, -1);
145
+ console.log("found type: ", type);
146
+ lastTypeFound = type;
147
+ }
149
148
  }
150
149
  }
151
150
 
@@ -232,12 +231,11 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
232
231
 
233
232
  case ts.SyntaxKind.ClassDeclaration:
234
233
  serializeField = false;
235
- lastTypeFound = null;
236
234
  const dec = <ts.ClassDeclaration>node;
237
235
  // a class must inherit a component
238
- if (!dontExportNextClass && (exportNextClass || testInheritsComponent(node))) {
236
+ if (!dontExportNextClass && (lastTypeFound || exportNextClass || testInheritsComponent(node))) {
239
237
  resetExportNextClass();
240
- const name = dec.name.escapedText;
238
+ const name = dec.name?.escapedText;
241
239
  console.log("Found class: ", name);
242
240
  const newContext = new ExportContext(outputDir, name + ".cs");
243
241
  newContext.appendLine("// auto generated code - do not edit directly");
@@ -248,12 +246,15 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
248
246
  newContext.appendLine("{");
249
247
  newContext.indentLevel += 1;
250
248
  // newContext.appendLine("// source: " + path.resolve(sourceFile.fileName));
251
- newContext.appendLine("public partial class " + name + " : UnityEngine.MonoBehaviour");
249
+ const typeName = lastTypeFound ?? "UnityEngine.MonoBehaviour";
250
+ console.log(name + " inherits " + typeName);
251
+ newContext.appendLine("public partial class " + name + " : " + typeName);
252
252
  newContext.appendLine("{");
253
253
  newContext.indentLevel += 1;
254
254
  newContext.classEnd = dec.end;
255
255
  contexts.push(newContext);
256
256
  }
257
+ lastTypeFound = null;
257
258
  break;
258
259
  }
259
260
 
package/src/test.ts CHANGED
@@ -1,22 +1,36 @@
1
1
 
2
2
  // @generate-component
3
3
 
4
+ //@type (RoomEntity)
5
+ export class NavigationManager extends RoomEntity {
4
6
 
5
- export class PointOfInterest extends Behaviour {
7
+ fl:number = 1;
6
8
 
7
- myVal:number = 12;
8
-
9
- // @type(HELLO)
10
- myFunction(){
9
+ nav_forward() {
11
10
 
12
11
  }
13
12
 
14
- // @type(UnityEngine.Camera)
15
- view?:Camera;
16
- test:string = "123";
17
- // test
13
+ nav_backward() {
14
+
15
+ }
18
16
  }
19
17
 
18
+
19
+ // export class PointOfInterest extends Behaviour {
20
+
21
+ // myVal:number = 12;
22
+
23
+ // // @type(HELLO)
24
+ // myFunction(){
25
+
26
+ // }
27
+
28
+ // // @type(UnityEngine.Camera)
29
+ // view?:Camera;
30
+ // test:string = "123";
31
+ // // test
32
+ // }
33
+
20
34
  // export class MaterialColorHandler extends Behaviour {
21
35
 
22
36
  // @serializeable(Renderer)