@needle-tools/needle-component-compiler 1.2.1 → 1.3.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.3.0] - 2022-06-27
8
+ - add support for typescript namespace
9
+
7
10
  ## [1.2.1] - 2022-06-15
8
11
  - allow adding ``@type(MyNamespace.MyType)`` to class declaration
9
12
 
package/Readme.md CHANGED
@@ -11,4 +11,4 @@ Please run ``npm install`` first before using.
11
11
  - ``@dont-generate-component`` add before class to skip generating a component
12
12
  - ``@generate-component`` to enforce generating a component (not required)
13
13
  - ``@serializeField`` field decorator, similar to ``[SerializeField]``
14
- - ``@type(MyNamespace.MyType)`` field decorator, specifiy C# type of field
14
+ - ``@type(MyNamespace.MyType)`` decorator for fields or classes, specifiy C# type of field or class
package/dist/MyClass.cs CHANGED
@@ -1,23 +1,10 @@
1
- // auto generated code - do not edit
2
- namespace Needle.Typescript.GeneratedComponents
1
+ // auto generated code - do not edit directly
2
+
3
+ #pragma warning disable
4
+
5
+ namespace Hello.World.Deep
3
6
  {
4
- // source: C:\git\needle-tiny-playground\projects\Compiled_Export\myProject\src\scripts\MyClass.ts
5
- public class MyClass : UnityEngine.MonoBehaviour
7
+ public partial class MyClass : UnityEngine.MonoBehaviour
6
8
  {
7
- public void start(){}
8
- public float myFloat = 15;
9
- public bool myBool;
10
- // just some default values
11
- public float[] myArray = new float[]{ 1, 2, 3 };
12
- // comment for myString
13
- public string myString = "this is a string";
14
- public UnityEngine.Transform myObject;
15
- public bool myBool2;
16
- public void myFunction(){}
17
- public void myFunctionWithStringParameter(string @string){}
18
- public void myFunctionWithSomeObjectAndArray(UnityEngine.Transform @obj, float[] @arr){}
19
- public void myFunctionWithoutParamTypes(object @test){}
20
- public UnityEngine.Transform[] someOtherStuff;
21
- public UnityEngine.Events.UnityEvent myEvent;
22
9
  }
23
10
  }
@@ -0,0 +1,10 @@
1
+ // auto generated code - do not edit directly
2
+
3
+ #pragma warning disable
4
+
5
+ namespace Needle.Typescript.GeneratedComponents
6
+ {
7
+ public partial class OtherClass : UnityEngine.MonoBehaviour
8
+ {
9
+ }
10
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@needle-tools/needle-component-compiler",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "Compile mock unity components from typescript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -78,7 +78,7 @@ function run(program, outputDir, sourceFile) {
78
78
  ts.forEachChild(node, traverseFile);
79
79
  }
80
80
  function visit(node) {
81
- var _a, _b, _c, _d;
81
+ var _a, _b, _c, _d, _e;
82
82
  var context = contexts.length > 0 ? contexts[contexts.length - 1] : null;
83
83
  if (context) {
84
84
  if ((context === null || context === void 0 ? void 0 : context.classEnd) > 0 && node.pos >= (context === null || context === void 0 ? void 0 : context.classEnd)) {
@@ -126,6 +126,15 @@ function run(program, outputDir, sourceFile) {
126
126
  }
127
127
  }
128
128
  switch (node.kind) {
129
+ // Namespace
130
+ // case ts.SyntaxKind.ModuleDeclaration:
131
+ // const mod = node as ts.ModuleDeclaration;
132
+ // console.log(ts.SyntaxKind[mod.getChildAt(1).kind])
133
+ // const type = mod.getChildAt(1) as ts.Identifier;
134
+ // console.log("MODULE", type.text)
135
+ // break;
136
+ // case ts.SyntaxKind.Identifier:
137
+ // break;
129
138
  // case ts.SyntaxKind.ClassDeclaration:
130
139
  // case ts.SyntaxKind.SingleLineCommentTrivia:
131
140
  // console.log("comment: " + node.getText())
@@ -143,8 +152,8 @@ function run(program, outputDir, sourceFile) {
143
152
  // const isCoroutine = func.asteriskToken;
144
153
  if (meth.name) {
145
154
  var paramsStr = "";
146
- for (var _e = 0, _f = meth.parameters; _e < _f.length; _e++) {
147
- var param = _f[_e];
155
+ for (var _f = 0, _g = meth.parameters; _f < _g.length; _f++) {
156
+ var param = _g[_f];
148
157
  if (!param || !param.name)
149
158
  continue;
150
159
  if (paramsStr.length > 0)
@@ -177,8 +186,8 @@ function run(program, outputDir, sourceFile) {
177
186
  var prefix = typeString === undefined ? "// " : "";
178
187
  var assignment = "";
179
188
  if (typeString !== undefined) {
180
- for (var _g = 0, _h = node.getChildren(); _g < _h.length; _g++) {
181
- var ch = _h[_g];
189
+ for (var _h = 0, _j = node.getChildren(); _h < _j.length; _h++) {
190
+ var ch = _j[_h];
182
191
  switch (ch.kind) {
183
192
  case ts.SyntaxKind.FalseKeyword:
184
193
  case ts.SyntaxKind.TrueKeyword:
@@ -219,12 +228,14 @@ function run(program, outputDir, sourceFile) {
219
228
  resetExportNextClass();
220
229
  var name_2 = (_d = dec.name) === null || _d === void 0 ? void 0 : _d.escapedText;
221
230
  console.log("Found class: ", name_2);
231
+ var namespace = (_e = tryParseNamespace(node)) !== null && _e !== void 0 ? _e : "Needle.Typescript.GeneratedComponents";
232
+ console.log("NAMESPACE", namespace);
222
233
  var newContext = new ExportContext(outputDir, name_2 + ".cs");
223
234
  newContext.appendLine("// auto generated code - do not edit directly");
224
235
  newContext.appendLine("");
225
236
  newContext.appendLine("#pragma warning disable");
226
237
  newContext.appendLine("");
227
- newContext.appendLine("namespace Needle.Typescript.GeneratedComponents");
238
+ newContext.appendLine("namespace " + namespace);
228
239
  newContext.appendLine("{");
229
240
  newContext.indentLevel += 1;
230
241
  // newContext.appendLine("// source: " + path.resolve(sourceFile.fileName));
@@ -277,6 +288,31 @@ function run(program, outputDir, sourceFile) {
277
288
  }
278
289
  return true;
279
290
  }
291
+ function tryParseNamespace(node, namespace) {
292
+ // console.log("TRAVERSE - " + ts.SyntaxKind[node.kind]);
293
+ switch (node.kind) {
294
+ case ts.SyntaxKind.ModuleDeclaration:
295
+ for (var _i = 0, _a = node.getChildren(); _i < _a.length; _i++) {
296
+ var ch = _a[_i];
297
+ // console.log("-- TRAVERSE - " + ts.SyntaxKind[ch.kind]);
298
+ switch (ch.kind) {
299
+ case ts.SyntaxKind.Identifier:
300
+ var id = ch;
301
+ if (id.text) {
302
+ if (!namespace)
303
+ namespace = "";
304
+ namespace = id.text + (namespace ? "." : "") + namespace;
305
+ }
306
+ break;
307
+ }
308
+ }
309
+ break;
310
+ }
311
+ if (node.parent) {
312
+ return tryParseNamespace(node.parent, namespace);
313
+ }
314
+ return namespace;
315
+ }
280
316
  function tryResolveTypeRecursive(node) {
281
317
  var _a;
282
318
  if (!node)
@@ -4,6 +4,7 @@ import * as fs from "fs";
4
4
  import * as path from "path";
5
5
 
6
6
  import * as types from "./types";
7
+ import { traceDeprecation } from "process";
7
8
  const dict = types.dict;
8
9
 
9
10
  // add either of these two comments above a class to enforce code gen or disable it for the next class
@@ -148,6 +149,15 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
148
149
  }
149
150
 
150
151
  switch (node.kind) {
152
+ // Namespace
153
+ // case ts.SyntaxKind.ModuleDeclaration:
154
+ // const mod = node as ts.ModuleDeclaration;
155
+ // console.log(ts.SyntaxKind[mod.getChildAt(1).kind])
156
+ // const type = mod.getChildAt(1) as ts.Identifier;
157
+ // console.log("MODULE", type.text)
158
+ // break;
159
+ // case ts.SyntaxKind.Identifier:
160
+ // break;
151
161
  // case ts.SyntaxKind.ClassDeclaration:
152
162
  // case ts.SyntaxKind.SingleLineCommentTrivia:
153
163
  // console.log("comment: " + node.getText())
@@ -236,12 +246,14 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
236
246
  resetExportNextClass();
237
247
  const name = dec.name?.escapedText;
238
248
  console.log("Found class: ", name);
249
+ const namespace = tryParseNamespace(node) ?? "Needle.Typescript.GeneratedComponents";
250
+ console.log("NAMESPACE", namespace);
239
251
  const newContext = new ExportContext(outputDir, name + ".cs");
240
252
  newContext.appendLine("// auto generated code - do not edit directly");
241
253
  newContext.appendLine("");
242
254
  newContext.appendLine("#pragma warning disable");
243
255
  newContext.appendLine("");
244
- newContext.appendLine("namespace Needle.Typescript.GeneratedComponents");
256
+ newContext.appendLine("namespace " + namespace);
245
257
  newContext.appendLine("{");
246
258
  newContext.indentLevel += 1;
247
259
  // newContext.appendLine("// source: " + path.resolve(sourceFile.fileName));
@@ -291,6 +303,30 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
291
303
  return true;
292
304
  }
293
305
 
306
+ function tryParseNamespace(node: ts.Node, namespace?: string): string | null | undefined {
307
+ // console.log("TRAVERSE - " + ts.SyntaxKind[node.kind]);
308
+ switch (node.kind) {
309
+ case ts.SyntaxKind.ModuleDeclaration:
310
+ for (const ch of node.getChildren()) {
311
+ // console.log("-- TRAVERSE - " + ts.SyntaxKind[ch.kind]);
312
+ switch (ch.kind) {
313
+ case ts.SyntaxKind.Identifier:
314
+ const id = ch as ts.Identifier;
315
+ if (id.text) {
316
+ if(!namespace) namespace = "";
317
+ namespace = id.text + (namespace ? "." : "") + namespace;
318
+ }
319
+ break;
320
+ }
321
+ }
322
+ break;
323
+ }
324
+ if (node.parent) {
325
+ return tryParseNamespace(node.parent, namespace);
326
+ }
327
+ return namespace;
328
+ }
329
+
294
330
  function tryResolveTypeRecursive(node: ts.Node | ts.VariableDeclaration): string | undefined {
295
331
  if (!node) return undefined;
296
332
 
package/src/test.ts CHANGED
@@ -1,27 +1,41 @@
1
1
 
2
- import { Behaviour } from "needle.tiny.engine/engine-components/Component";
3
- import { RoomEntity } from "./Room";
2
+ // import { Behaviour } from "needle.tiny.engine/engine-components/Component";
3
+ // import { RoomEntity } from "./Room";
4
+
5
+
6
+ namespace Hello.World
7
+ {
8
+ namespace Deep {
9
+ export class MyClass extends Behaviour {
10
+
11
+ }
12
+ }
13
+ }
14
+
15
+ class OtherClass extends Behaviour {
16
+
17
+ }
4
18
 
5
19
  //@type (RoomEntity)
6
- export class NavigationManager extends RoomEntity {
20
+ // export class NavigationManager extends RoomEntity {
7
21
 
8
- fl:number = 1;
22
+ // fl:number = 1;
9
23
 
10
- nav_forward() {
24
+ // nav_forward() {
11
25
 
12
- }
26
+ // }
13
27
 
14
- nav_backward() {
28
+ // nav_backward() {
15
29
 
16
- }
17
- }
30
+ // }
31
+ // }
18
32
 
19
- export abstract class NavComponent extends Behaviour {
33
+ // export abstract class NavComponent extends Behaviour {
20
34
 
21
- abstract next();
22
- abstract prev();
23
- abstract isAtEnd():boolean;
24
- }
35
+ // abstract next();
36
+ // abstract prev();
37
+ // abstract isAtEnd():boolean;
38
+ // }
25
39
 
26
40
 
27
41
  // export class PointOfInterest extends Behaviour {