@needle-tools/needle-component-compiler 1.7.3 → 1.8.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 +4 -0
- package/Readme.md +2 -1
- package/package.json +1 -1
- package/src/component-compiler.js +16 -5
- package/src/component-compiler.ts +17 -5
- package/src/test.ts +21 -3
package/Changelog.md
CHANGED
|
@@ -4,6 +4,10 @@ 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.8.0] - 2022-09-02
|
|
8
|
+
- Add ``@nonSerialized`` decorator for fields and methods
|
|
9
|
+
- Fix comment gen for unknown types
|
|
10
|
+
|
|
7
11
|
## [1.7.3] - 2022-09-02
|
|
8
12
|
- Fix codegen for unknown array types
|
|
9
13
|
- Fix codegen for abstract classes
|
package/Readme.md
CHANGED
|
@@ -10,6 +10,7 @@ Please run ``npm install`` first before using.
|
|
|
10
10
|
### Command decorators
|
|
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
|
-
- ``@serializeField`` field decorator, similar to ``[SerializeField]``
|
|
13
|
+
- ``@serializeField`` field decorator, similar to ``[SerializeField]`` in Unity
|
|
14
|
+
- ``@nonSerialized`` field or method decorator to skip generating c# code for a field or a method, similar to ``[NonSerialized]`` in Unity
|
|
14
15
|
- ``@type MyNamespace.MyType`` decorator for fields or classes, specifiy C# type of field or class
|
|
15
16
|
- ``@ifdef MY_IFDEF`` field decorator only at the moment
|
package/package.json
CHANGED
|
@@ -12,6 +12,7 @@ var exportNextClassCommand = "@generate-component";
|
|
|
12
12
|
var dontExportNextClassCommand = "@dont-generate-component";
|
|
13
13
|
// add above field to add [SerializeField] attribute
|
|
14
14
|
var serializeCommand = "@serializeField";
|
|
15
|
+
var dontSerializeCommand = "@nonSerialized";
|
|
15
16
|
// https://regex101.com/r/ltpcKT/2
|
|
16
17
|
var typePattern = new RegExp("@type ?(?<type>.+)");
|
|
17
18
|
var ifdefPattern = new RegExp("@ifdef ?(?<ifdef>.+)");
|
|
@@ -21,6 +22,7 @@ var CODEGEN_MARKER_END = "// NEEDLE_CODEGEN_END";
|
|
|
21
22
|
var exportNextClass = false;
|
|
22
23
|
var dontExportNextClass = false;
|
|
23
24
|
var serializeField = false;
|
|
25
|
+
var dontSerialize = false;
|
|
24
26
|
function resetExportNextClass() {
|
|
25
27
|
dontExportNextClass = false;
|
|
26
28
|
exportNextClass = false;
|
|
@@ -146,6 +148,8 @@ function run(program, outputDir, sourceFile) {
|
|
|
146
148
|
}
|
|
147
149
|
else if (comment.includes(serializeCommand))
|
|
148
150
|
serializeField = true;
|
|
151
|
+
else if (comment.includes(dontSerializeCommand))
|
|
152
|
+
dontSerialize = true;
|
|
149
153
|
}
|
|
150
154
|
if (comment.includes(exportNextClassCommand))
|
|
151
155
|
exportNextClass = true;
|
|
@@ -168,6 +172,7 @@ function run(program, outputDir, sourceFile) {
|
|
|
168
172
|
}
|
|
169
173
|
}
|
|
170
174
|
}
|
|
175
|
+
var skip = dontSerialize;
|
|
171
176
|
switch (node.kind) {
|
|
172
177
|
// Namespace
|
|
173
178
|
// case ts.SyntaxKind.ModuleDeclaration:
|
|
@@ -187,13 +192,14 @@ function run(program, outputDir, sourceFile) {
|
|
|
187
192
|
case ts.SyntaxKind.MethodDeclaration:
|
|
188
193
|
lastTypeFound = null;
|
|
189
194
|
serializeField = false;
|
|
195
|
+
dontSerialize = false;
|
|
190
196
|
resetExportNextClass();
|
|
191
197
|
if (!context)
|
|
192
198
|
break;
|
|
193
199
|
// TODO: always emit at least OnEnable method per class so generated components have toggle in editor
|
|
194
200
|
var meth = node;
|
|
195
201
|
// const isCoroutine = func.asteriskToken;
|
|
196
|
-
if (meth.name) {
|
|
202
|
+
if (!skip && meth.name) {
|
|
197
203
|
var paramsStr = "";
|
|
198
204
|
for (var _g = 0, _h = meth.parameters; _g < _h.length; _g++) {
|
|
199
205
|
var param = _h[_g];
|
|
@@ -222,11 +228,14 @@ function run(program, outputDir, sourceFile) {
|
|
|
222
228
|
var pub = isPublic(vardec);
|
|
223
229
|
var visibility = pub ? "public" : "private";
|
|
224
230
|
var isAccessible = pub;
|
|
225
|
-
|
|
231
|
+
dontSerialize = false;
|
|
232
|
+
if (serializeField) {
|
|
226
233
|
console.log("[SerializeField]");
|
|
227
234
|
context.appendLine("[UnityEngine.SerializeField]");
|
|
228
235
|
isAccessible = true;
|
|
229
236
|
}
|
|
237
|
+
else if (skip)
|
|
238
|
+
isAccessible = false;
|
|
230
239
|
if (!isAccessible) {
|
|
231
240
|
console.log("Skip because not public or serializeable");
|
|
232
241
|
break;
|
|
@@ -238,6 +247,7 @@ function run(program, outputDir, sourceFile) {
|
|
|
238
247
|
var typeString = lastTypeFound !== null && lastTypeFound !== void 0 ? lastTypeFound : tryResolveTypeRecursive(node);
|
|
239
248
|
var postFix = "";
|
|
240
249
|
var typeName = (_c = vardec.type) === null || _c === void 0 ? void 0 : _c.getText();
|
|
250
|
+
var shouldCommentTheLine = typeString === undefined;
|
|
241
251
|
if (typeString === undefined) {
|
|
242
252
|
postFix = " → Could not resolve C# type";
|
|
243
253
|
}
|
|
@@ -285,11 +295,12 @@ function run(program, outputDir, sourceFile) {
|
|
|
285
295
|
typeString = typeName;
|
|
286
296
|
if (typeString === "[]") {
|
|
287
297
|
console.log("Unknown array type for \"" + varName + " " + typeName + "\" - your type is probably not known (did you just create it this session?) and you might need to regenerate the Typemap in Unity. Go to \"Needle Engine/Internal/Generate Type Map for component compiler");
|
|
288
|
-
typeString = "object[]";
|
|
289
|
-
assignment = " = new object[0]";
|
|
298
|
+
// typeString = "object[]";
|
|
299
|
+
// assignment = " = new object[0]";
|
|
300
|
+
shouldCommentTheLine = true;
|
|
290
301
|
}
|
|
291
302
|
console.log("EMIT member: \"" + typeString + "\" " + varName, assignment, "Last type found:", lastTypeFound);
|
|
292
|
-
var prefix =
|
|
303
|
+
var prefix = shouldCommentTheLine ? "// " : "";
|
|
293
304
|
context.append(prefix + visibility + " " + typeString + " " + varName + assignment + ";" + postFix + "\n");
|
|
294
305
|
lastTypeFound = null;
|
|
295
306
|
if (requireEndIf) {
|
|
@@ -12,6 +12,7 @@ const exportNextClassCommand = "@generate-component";
|
|
|
12
12
|
const dontExportNextClassCommand = "@dont-generate-component";
|
|
13
13
|
// add above field to add [SerializeField] attribute
|
|
14
14
|
const serializeCommand = "@serializeField";
|
|
15
|
+
const dontSerializeCommand = "@nonSerialized";
|
|
15
16
|
// https://regex101.com/r/ltpcKT/2
|
|
16
17
|
const typePattern = new RegExp("@type ?(?<type>.+)");
|
|
17
18
|
const ifdefPattern = new RegExp("@ifdef ?(?<ifdef>.+)")
|
|
@@ -23,6 +24,7 @@ const CODEGEN_MARKER_END = "// NEEDLE_CODEGEN_END";
|
|
|
23
24
|
let exportNextClass: boolean = false;
|
|
24
25
|
let dontExportNextClass: boolean = false;
|
|
25
26
|
let serializeField: boolean = false;
|
|
27
|
+
let dontSerialize: boolean = false;
|
|
26
28
|
function resetExportNextClass() {
|
|
27
29
|
dontExportNextClass = false;
|
|
28
30
|
exportNextClass = false;
|
|
@@ -171,6 +173,8 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
171
173
|
}
|
|
172
174
|
else if (comment.includes(serializeCommand))
|
|
173
175
|
serializeField = true;
|
|
176
|
+
else if (comment.includes(dontSerializeCommand))
|
|
177
|
+
dontSerialize = true;
|
|
174
178
|
}
|
|
175
179
|
if (comment.includes(exportNextClassCommand))
|
|
176
180
|
exportNextClass = true;
|
|
@@ -195,6 +199,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
195
199
|
}
|
|
196
200
|
}
|
|
197
201
|
|
|
202
|
+
const skip = dontSerialize;
|
|
198
203
|
switch (node.kind) {
|
|
199
204
|
// Namespace
|
|
200
205
|
// case ts.SyntaxKind.ModuleDeclaration:
|
|
@@ -214,12 +219,13 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
214
219
|
case ts.SyntaxKind.MethodDeclaration:
|
|
215
220
|
lastTypeFound = null;
|
|
216
221
|
serializeField = false;
|
|
222
|
+
dontSerialize = false;
|
|
217
223
|
resetExportNextClass();
|
|
218
224
|
if (!context) break;
|
|
219
225
|
// TODO: always emit at least OnEnable method per class so generated components have toggle in editor
|
|
220
226
|
const meth = node as ts.MethodDeclaration;
|
|
221
227
|
// const isCoroutine = func.asteriskToken;
|
|
222
|
-
if (meth.name) {
|
|
228
|
+
if (!skip && meth.name) {
|
|
223
229
|
let paramsStr = "";
|
|
224
230
|
for (let param of meth.parameters) {
|
|
225
231
|
if (!param || !param.name) continue;
|
|
@@ -246,11 +252,15 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
246
252
|
const pub = isPublic(vardec);
|
|
247
253
|
const visibility = pub ? "public" : "private";
|
|
248
254
|
let isAccessible = pub;
|
|
249
|
-
|
|
255
|
+
dontSerialize = false;
|
|
256
|
+
if (serializeField) {
|
|
250
257
|
console.log("[SerializeField]");
|
|
251
258
|
context.appendLine("[UnityEngine.SerializeField]");
|
|
252
259
|
isAccessible = true;
|
|
253
260
|
}
|
|
261
|
+
else if(skip)
|
|
262
|
+
isAccessible = false;
|
|
263
|
+
|
|
254
264
|
if (!isAccessible) {
|
|
255
265
|
console.log("Skip because not public or serializeable")
|
|
256
266
|
break;
|
|
@@ -262,6 +272,7 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
262
272
|
let typeString = lastTypeFound ?? tryResolveTypeRecursive(node);
|
|
263
273
|
let postFix = "";
|
|
264
274
|
let typeName = vardec.type?.getText();
|
|
275
|
+
let shouldCommentTheLine = typeString === undefined;
|
|
265
276
|
if (typeString === undefined) {
|
|
266
277
|
postFix = " → Could not resolve C# type";
|
|
267
278
|
}
|
|
@@ -307,11 +318,12 @@ export function run(program: ts.Program, outputDir: string, sourceFile: ts.Sourc
|
|
|
307
318
|
if (typeString === undefined) typeString = typeName;
|
|
308
319
|
if (typeString === "[]") {
|
|
309
320
|
console.log("Unknown array type for \"" + varName + " " + typeName + "\" - your type is probably not known (did you just create it this session?) and you might need to regenerate the Typemap in Unity. Go to \"Needle Engine/Internal/Generate Type Map for component compiler")
|
|
310
|
-
typeString = "object[]";
|
|
311
|
-
assignment = " = new object[0]";
|
|
321
|
+
// typeString = "object[]";
|
|
322
|
+
// assignment = " = new object[0]";
|
|
323
|
+
shouldCommentTheLine = true;
|
|
312
324
|
}
|
|
313
325
|
console.log("EMIT member: \"" + typeString + "\" " + varName, assignment, "Last type found:", lastTypeFound);
|
|
314
|
-
const prefix =
|
|
326
|
+
const prefix = shouldCommentTheLine ? "// " : "";
|
|
315
327
|
context.append(prefix + visibility + " " + typeString + " " + varName + assignment + ";" + postFix + "\n");
|
|
316
328
|
lastTypeFound = null;
|
|
317
329
|
if (requireEndIf) {
|
package/src/test.ts
CHANGED
|
@@ -1,15 +1,33 @@
|
|
|
1
1
|
import { ThisExpression } from "typescript";
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
export class SkipFieldAndMethod extends Behaviour {
|
|
5
|
+
|
|
6
|
+
//@nonSerialized
|
|
7
|
+
myMethod() {
|
|
8
|
+
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// @nonSerialized
|
|
12
|
+
myField : string;
|
|
13
|
+
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
// export class ComponentWithUnknownType extends Behaviour {
|
|
18
|
+
// views: SomeUnknownType;
|
|
19
|
+
// }
|
|
20
|
+
|
|
21
|
+
|
|
4
22
|
// export class ComponentWithAnimationClip extends Behaviour implements IPointerClickHandler {
|
|
5
23
|
|
|
6
24
|
// @serializeable(AnimationClip)
|
|
7
25
|
// animation?: THREE.AnimationClip;
|
|
8
26
|
// }
|
|
9
27
|
|
|
10
|
-
export abstract class MyAbstractComponent extends Behaviour {
|
|
11
|
-
|
|
12
|
-
}
|
|
28
|
+
// export abstract class MyAbstractComponent extends Behaviour {
|
|
29
|
+
// abstract myMethod();
|
|
30
|
+
// }
|
|
13
31
|
|
|
14
32
|
|
|
15
33
|
// export class CameraView extends Behaviour {
|