@needle-tools/needle-component-compiler 1.9.2-exp → 1.9.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.
Files changed (112) hide show
  1. package/COMPILE.bat +1 -0
  2. package/Changelog.md +3 -2
  3. package/DEV.bat +1 -0
  4. package/INSTALL.bat +1 -0
  5. package/PUBLISH.bat +8 -0
  6. package/RUN_MANUAL_TEST.bat +1 -0
  7. package/RUN_TESTS.bat +1 -0
  8. package/package.json +12 -17
  9. package/schemes/MyNew.component.json +1 -0
  10. package/schemes/MyTest.component.json +1 -0
  11. package/src/component-compiler.js +6 -3
  12. package/src/component-compiler.ts +737 -0
  13. package/src/test.js +24 -26
  14. package/src/test.ts +234 -0
  15. package/test/blender/codegen/basic/expected.component.json +4 -0
  16. package/test/blender/codegen/basic/input.js +26 -0
  17. package/test/blender/codegen/basic/input.ts +3 -0
  18. package/test/blender/codegen/basic/output.component.json +1 -0
  19. package/test/blender/codegen/float field/expected.component.json +8 -0
  20. package/test/blender/codegen/float field/input.js +28 -0
  21. package/test/blender/codegen/float field/input.ts +4 -0
  22. package/test/blender/codegen/float field/output.component.json +1 -0
  23. package/test/blender/codegen/reference_animation/expected.component.json +7 -0
  24. package/test/blender/codegen/reference_animation/input.js +35 -0
  25. package/test/blender/codegen/reference_animation/input.ts +4 -0
  26. package/test/blender/codegen/reference_animation/output.component.json +1 -0
  27. package/test/blender/codegen/reference_behaviour/expected.component.json +7 -0
  28. package/test/blender/codegen/reference_behaviour/input.js +35 -0
  29. package/test/blender/codegen/reference_behaviour/input.ts +5 -0
  30. package/test/blender/codegen/reference_behaviour/output.component.json +1 -0
  31. package/test/component.basic.test.ts +53 -0
  32. package/test/component.methods.test.ts +185 -0
  33. package/test/component.nonserialized.test.ts +45 -0
  34. package/test/component.primitives.test.ts +123 -0
  35. package/test/csharp/codegen/abstract method/expected.cs +14 -0
  36. package/test/csharp/codegen/abstract method/input.js +27 -0
  37. package/test/csharp/codegen/abstract method/input.ts +4 -0
  38. package/test/csharp/codegen/abstract method/output.cs +14 -0
  39. package/test/csharp/codegen/basic/expected.cs +13 -0
  40. package/test/csharp/codegen/basic/input.js +26 -0
  41. package/test/csharp/codegen/basic/input.ts +2 -0
  42. package/test/csharp/codegen/basic/output.cs +13 -0
  43. package/test/csharp/codegen/basic-no-export/expected.cs +0 -0
  44. package/test/csharp/codegen/basic-no-export/input.js +27 -0
  45. package/test/csharp/codegen/basic-no-export/input.ts +4 -0
  46. package/test/csharp/codegen/basic-no-export/output.cs +0 -0
  47. package/test/csharp/codegen/bool/expected.cs +14 -0
  48. package/test/csharp/codegen/bool/input.js +26 -0
  49. package/test/csharp/codegen/bool/input.ts +3 -0
  50. package/test/csharp/codegen/bool/output.cs +14 -0
  51. package/test/csharp/codegen/fields with default values/expected.cs +18 -0
  52. package/test/csharp/codegen/fields with default values/input.js +32 -0
  53. package/test/csharp/codegen/fields with default values/input.ts +7 -0
  54. package/test/csharp/codegen/fields with default values/output.cs +18 -0
  55. package/test/csharp/codegen/method ignore inline type declaration/expected.cs +14 -0
  56. package/test/csharp/codegen/method ignore inline type declaration/input.js +27 -0
  57. package/test/csharp/codegen/method ignore inline type declaration/input.ts +3 -0
  58. package/test/csharp/codegen/method ignore inline type declaration/output.cs +14 -0
  59. package/test/csharp/codegen/nonserialized ignore field/expected.cs +13 -0
  60. package/test/csharp/codegen/nonserialized ignore field/input.js +26 -0
  61. package/test/csharp/codegen/nonserialized ignore field/input.ts +4 -0
  62. package/test/csharp/codegen/nonserialized ignore field/output.cs +13 -0
  63. package/test/csharp/codegen/nonserialized on property does not affect method/expected.cs +14 -0
  64. package/test/csharp/codegen/nonserialized on property does not affect method/input.js +34 -0
  65. package/test/csharp/codegen/nonserialized on property does not affect method/input.ts +8 -0
  66. package/test/csharp/codegen/nonserialized on property does not affect method/output.cs +13 -0
  67. package/test/csharp/codegen/number/expected.cs +14 -0
  68. package/test/csharp/codegen/number/input.js +26 -0
  69. package/test/csharp/codegen/number/input.ts +3 -0
  70. package/test/csharp/codegen/number/output.cs +14 -0
  71. package/test/csharp/codegen/object/expected.cs +14 -0
  72. package/test/csharp/codegen/object/input.js +26 -0
  73. package/test/csharp/codegen/object/input.ts +3 -0
  74. package/test/csharp/codegen/object/output.cs +14 -0
  75. package/test/csharp/codegen/private.methods/expected.cs +13 -0
  76. package/test/csharp/codegen/private.methods/input.js +27 -0
  77. package/test/csharp/codegen/private.methods/input.ts +3 -0
  78. package/test/csharp/codegen/private.methods/output.cs +13 -0
  79. package/test/csharp/codegen/protected.methods/expected.cs +13 -0
  80. package/test/csharp/codegen/protected.methods/input.js +27 -0
  81. package/test/csharp/codegen/protected.methods/input.ts +3 -0
  82. package/test/csharp/codegen/protected.methods/output.cs +13 -0
  83. package/test/csharp/codegen/public method with args/expected.cs +14 -0
  84. package/test/csharp/codegen/public method with args/input.js +27 -0
  85. package/test/csharp/codegen/public method with args/input.ts +3 -0
  86. package/test/csharp/codegen/public method with args/output.cs +14 -0
  87. package/test/csharp/codegen/public method with multiple args/expected.cs +14 -0
  88. package/test/csharp/codegen/public method with multiple args/input.js +27 -0
  89. package/test/csharp/codegen/public method with multiple args/input.ts +3 -0
  90. package/test/csharp/codegen/public method with multiple args/output.cs +14 -0
  91. package/test/csharp/codegen/public.methods/expected.cs +14 -0
  92. package/test/csharp/codegen/public.methods/input.js +27 -0
  93. package/test/csharp/codegen/public.methods/input.ts +3 -0
  94. package/test/csharp/codegen/public.methods/output.cs +14 -0
  95. package/test/csharp/codegen/static method/expected.cs +13 -0
  96. package/test/csharp/codegen/static method/input.js +27 -0
  97. package/test/csharp/codegen/static method/input.ts +3 -0
  98. package/test/csharp/codegen/static method/output.cs +14 -0
  99. package/test/csharp/codegen/string/expected.cs +14 -0
  100. package/test/csharp/codegen/string/input.js +26 -0
  101. package/test/csharp/codegen/string/input.ts +3 -0
  102. package/test/csharp/codegen/string/output.cs +14 -0
  103. package/test/csharp/codegen/stringArray/expected.cs +14 -0
  104. package/test/csharp/codegen/stringArray/input.js +26 -0
  105. package/test/csharp/codegen/stringArray/input.ts +3 -0
  106. package/test/csharp/codegen/stringArray/output.cs +14 -0
  107. package/test/helpers.ts +72 -0
  108. package/workspace.code-workspace +11 -11
  109. package/src/base-compiler.js +0 -404
  110. package/src/blender-compiler.js +0 -100
  111. package/src/commands.js +0 -11
  112. package/src/watcher.js +0 -64
@@ -0,0 +1,27 @@
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.myMethod = function () { };
25
+ return MyComponent;
26
+ }(Behaviour));
27
+ exports.MyComponent = MyComponent;
@@ -0,0 +1,3 @@
1
+ export class MyComponent extends Behaviour {
2
+ public static myMethod(): void {}
3
+ }
@@ -0,0 +1,14 @@
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 myMethod(){}
11
+ }
12
+ }
13
+
14
+ // NEEDLE_CODEGEN_END
@@ -0,0 +1,14 @@
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 string @myField;
11
+ }
12
+ }
13
+
14
+ // NEEDLE_CODEGEN_END
@@ -0,0 +1,26 @@
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
+ return MyComponent;
25
+ }(Behaviour));
26
+ exports.MyComponent = MyComponent;
@@ -0,0 +1,3 @@
1
+ export class MyComponent extends Behaviour {
2
+ public myField: string;
3
+ }
@@ -0,0 +1,14 @@
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 string @myField;
11
+ }
12
+ }
13
+
14
+ // NEEDLE_CODEGEN_END
@@ -0,0 +1,14 @@
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 string[] @myField;
11
+ }
12
+ }
13
+
14
+ // NEEDLE_CODEGEN_END
@@ -0,0 +1,26 @@
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
+ return MyComponent;
25
+ }(Behaviour));
26
+ exports.MyComponent = MyComponent;
@@ -0,0 +1,3 @@
1
+ export class MyComponent extends Behaviour {
2
+ public myField: string[];
3
+ }
@@ -0,0 +1,14 @@
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 string[] @myField;
11
+ }
12
+ }
13
+
14
+ // NEEDLE_CODEGEN_END
@@ -0,0 +1,72 @@
1
+ import { compile } from "../src/component-compiler";
2
+ import { expect } from 'chai';
3
+ import * as fs from "fs";
4
+ import { debuglog } from "util";
5
+
6
+ class TestOptions {
7
+ ignoreWhiteSpace: boolean = true;
8
+ }
9
+
10
+ export function testCompile(code: string): string | null {
11
+
12
+ const res = compile(code, "test", null, false);
13
+ const output = res?.length > 0 ? res[0] : "";
14
+
15
+ return output;
16
+
17
+ }
18
+
19
+ function compareByLine(output: string, expected: string) {
20
+ const splitCode = /\r?\n/;
21
+
22
+ if (output == null) {
23
+
24
+ expect(expected).to.equal(output);
25
+ }
26
+ else {
27
+
28
+ const outputLines = output.split(splitCode);
29
+ const expectedLines = expected.split(splitCode);
30
+ for (let i = 0; i < outputLines.length; i++) {
31
+ const outputLine = outputLines[i].trim();
32
+ const expectedLine = expectedLines[i].trim();
33
+ expect(outputLine).to.equal(expectedLine, `Line ${i} does not match`);
34
+ }
35
+ }
36
+
37
+ }
38
+
39
+ export function compareCodegen(id: string, input: string, expected: string) {
40
+ const baseDir = "test/codegen";
41
+ if (!fs.existsSync(baseDir))
42
+ fs.mkdirSync(baseDir);
43
+ const dir = `${baseDir}/${id}`;
44
+ if (!fs.existsSync(dir))
45
+ fs.mkdirSync(dir);
46
+ fs.writeFileSync(`${dir}\\input.ts`, input);
47
+ const result = testCompile(input);
48
+ if (result !== null)
49
+ fs.writeFileSync(`${dir}\\output.cs`, result);
50
+ fs.writeFileSync(`${dir}\\expected.cs`, expected);
51
+
52
+ compareByLine(result, expected);
53
+
54
+ // expect(result).to.equal(expected);
55
+ }
56
+
57
+ export function compareCodegenWithDefaultContext(id: string, input: string, expected: string) {
58
+
59
+
60
+ expected = `// NEEDLE_CODEGEN_START
61
+ // auto generated code - do not edit directly
62
+
63
+ #pragma warning disable
64
+
65
+ namespace Needle.Typescript.GeneratedComponents
66
+ {
67
+ \t` + expected + `
68
+ }
69
+
70
+ // NEEDLE_CODEGEN_END`;
71
+ return compareCodegen(id, input, expected);
72
+ }
@@ -1,12 +1,12 @@
1
- {
2
- "folders": [
3
- {
4
- "path": "."
5
- }
6
- ],
7
- "settings": {
8
- "files.exclude": {
9
- "test/*.js": true
10
- }
11
- }
1
+ {
2
+ "folders": [
3
+ {
4
+ "path": "."
5
+ }
6
+ ],
7
+ "settings": {
8
+ "files.exclude": {
9
+ "test/*.js": true
10
+ }
11
+ }
12
12
  }
@@ -1,404 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
3
- exports.Compiler = exports.runFromFile = exports.CodeTextWriter = exports.Visibility = void 0;
4
- var fs_1 = require("fs");
5
- var ts = require("typescript");
6
- var fs = require("fs");
7
- var Visibility;
8
- (function (Visibility) {
9
- Visibility[Visibility["Public"] = 0] = "Public";
10
- Visibility[Visibility["Protected"] = 1] = "Protected";
11
- Visibility[Visibility["Private"] = 2] = "Private";
12
- })(Visibility = exports.Visibility || (exports.Visibility = {}));
13
- var CodeTextWriter = /** @class */ (function () {
14
- function CodeTextWriter() {
15
- this.indent = 0;
16
- this.lines = [];
17
- }
18
- CodeTextWriter.prototype.beginBlock = function (line) {
19
- this.writeLine(line);
20
- this.indent++;
21
- };
22
- CodeTextWriter.prototype.endBlock = function (line) {
23
- // remove the comma from the last line
24
- if (this.lines.length > 0) {
25
- var prevLine = this.lines[this.lines.length - 1];
26
- if (prevLine.endsWith(",")) {
27
- this.lines[this.lines.length - 1] = prevLine.substring(0, prevLine.length - 1);
28
- }
29
- }
30
- this.indent--;
31
- this.writeLine(line);
32
- };
33
- CodeTextWriter.prototype.writeLine = function (line) {
34
- var indent = "";
35
- for (var i = 0; i < this.indent; i++) {
36
- indent += " ";
37
- }
38
- // remove whitespace so we can ensure we correctly end with a comma
39
- this.lines.push(indent + (line === null || line === void 0 ? void 0 : line.trim()));
40
- };
41
- CodeTextWriter.prototype.write = function (text) {
42
- var lines = text.split("\r");
43
- if (lines.length > 1) {
44
- for (var i = 0; i < lines.length; i++) {
45
- this.writeLine(lines[i]);
46
- }
47
- }
48
- else {
49
- if (this.lines.length == 0) {
50
- this.writeLine(text);
51
- }
52
- else {
53
- var lastLine = this.lines[this.lines.length - 1];
54
- this.lines[this.lines.length - 1] = lastLine + text;
55
- }
56
- }
57
- };
58
- CodeTextWriter.prototype.beginArray = function () {
59
- this.writeLine("[");
60
- this.indent++;
61
- };
62
- CodeTextWriter.prototype.insertArrayItem = function (item) {
63
- this.writeLine(item + ",");
64
- };
65
- CodeTextWriter.prototype.endArray = function () {
66
- };
67
- CodeTextWriter.prototype.flush = function (directory, filename) {
68
- if (this.lines.length === 0)
69
- return;
70
- if (!fs.existsSync(directory)) {
71
- fs.mkdirSync(directory, { recursive: true });
72
- }
73
- console.log("Writing " + filename + " to " + directory);
74
- var content = this.lines.join("\r");
75
- this.lines.length = 0;
76
- var fullPath = directory + "/" + filename;
77
- fs.writeFileSync(fullPath, content);
78
- return fs.realpathSync(fullPath);
79
- };
80
- return CodeTextWriter;
81
- }());
82
- exports.CodeTextWriter = CodeTextWriter;
83
- function runFromFile(writer, path) {
84
- if (!fs.existsSync(path)) {
85
- console.error("File not found", path);
86
- return;
87
- }
88
- var code = (0, fs_1.readFileSync)(path).toString();
89
- var compiler = new Compiler();
90
- compiler.compile(writer, code);
91
- return compiler;
92
- }
93
- exports.runFromFile = runFromFile;
94
- var Compiler = /** @class */ (function () {
95
- function Compiler() {
96
- }
97
- Compiler.prototype.compile = function (writer, code) {
98
- var file = "needle_compiled.ts";
99
- var sourceFile = ts.createSourceFile(file, code, ts.ScriptTarget.ES2015, true, /*setParentNodes */ ts.ScriptKind.TS);
100
- var prog = ts.createProgram([file], {});
101
- this.run(prog, writer, sourceFile);
102
- };
103
- Compiler.prototype.run = function (prog, writer, sourceFile) {
104
- var _this = this;
105
- writer.begin();
106
- ts.forEachChild(sourceFile, function (node) {
107
- _this.visit(node, writer);
108
- });
109
- writer.end();
110
- };
111
- Compiler.prototype.visit = function (node, writer) {
112
- var _this = this;
113
- if (ts.isClassDeclaration(node)) {
114
- this.visitClassDeclaration(node, writer);
115
- }
116
- else if (ts.isPropertyDeclaration(node)) {
117
- this.visitPropertyDeclaration(node, writer);
118
- }
119
- else if (ts.isMethodDeclaration(node)) {
120
- this.visitMethodDeclaration(node, writer);
121
- }
122
- else {
123
- ts.forEachChild(node, function (node) {
124
- _this.visit(node, writer);
125
- });
126
- }
127
- };
128
- Compiler.prototype.visitClassDeclaration = function (node, writer) {
129
- var _this = this;
130
- var name = node.name.text;
131
- var base = node.heritageClauses && node.heritageClauses[0].types[0].expression.getText();
132
- this.debugLog("CLASS START", name, base);
133
- var res = writer.startNewType(name, base, this.getComments(node));
134
- if (res === false) {
135
- this.debugLog("CLASS SKIPPED", name);
136
- return;
137
- }
138
- ts.forEachChild(node, function (node) {
139
- _this.visit(node, writer);
140
- });
141
- writer.endNewType(name);
142
- this.debugLog("CLASS END", name, "\n");
143
- };
144
- Compiler.prototype.visitPropertyDeclaration = function (node, writer) {
145
- var name = node.name.getText();
146
- var type = this.resolveType(node.type, writer);
147
- var visibility = this.getVisibility(node);
148
- var initialValue = node.initializer && this.resolveExpression(node.initializer, writer);
149
- var isArray = this.isArrayType(node.type);
150
- if (!type && node.initializer) {
151
- type = this.tryResolveTypeFromExpression(node.initializer, writer);
152
- }
153
- this.debugLog("PROPERTY", Visibility[visibility], name, isArray, type, initialValue);
154
- writer.writeMember(visibility, name, isArray, type, initialValue, this.getComments(node));
155
- };
156
- Compiler.prototype.visitMethodDeclaration = function (node, writer) {
157
- var _a;
158
- var name = node.name.getText();
159
- var visibility = this.getVisibility(node);
160
- var returnType = this.resolveType(node.type, writer);
161
- var args;
162
- if ((_a = node.parameters) === null || _a === void 0 ? void 0 : _a.length) {
163
- args = this.resolveParameters(node.parameters, writer);
164
- }
165
- this.debugLog("METHOD", Visibility[visibility], name, returnType, args);
166
- writer.writeMethod(visibility, name, returnType, args, this.getComments(node));
167
- };
168
- Compiler.prototype.resolveParameters = function (parameters, writer) {
169
- var result = [];
170
- for (var _i = 0, parameters_1 = parameters; _i < parameters_1.length; _i++) {
171
- var parameter = parameters_1[_i];
172
- var name_1 = parameter.name.getText();
173
- var type = this.resolveType(parameter.type, writer);
174
- var defaultValue = parameter.initializer && this.resolveExpression(parameter.initializer, writer);
175
- result.push({ name: name_1, type: type, defaultValue: defaultValue });
176
- }
177
- return result;
178
- };
179
- Compiler.prototype.debugLog = function () {
180
- var args = [];
181
- for (var _i = 0; _i < arguments.length; _i++) {
182
- args[_i] = arguments[_i];
183
- }
184
- console.log.apply(console, args);
185
- };
186
- Compiler.prototype.getComments = function (node) {
187
- var commentRanges = ts.getLeadingCommentRanges(node.getFullText(), 0);
188
- if (commentRanges) {
189
- var comments = [];
190
- for (var _i = 0, commentRanges_1 = commentRanges; _i < commentRanges_1.length; _i++) {
191
- var range = commentRanges_1[_i];
192
- var text = node.getFullText().substring(range.pos, range.end).trimStart();
193
- // match only lines that starts with //@ or // @
194
- var match = text.match(/\/\/\s*@(.+)/);
195
- if (match) {
196
- comments.push(text.substring(match.index + 2).trim());
197
- }
198
- }
199
- return comments;
200
- }
201
- };
202
- Compiler.prototype.getVisibility = function (node) {
203
- if (node.modifiers) {
204
- for (var _i = 0, _a = node.modifiers; _i < _a.length; _i++) {
205
- var modifier = _a[_i];
206
- if (modifier.kind === ts.SyntaxKind.PublicKeyword) {
207
- return Visibility.Public;
208
- }
209
- else if (modifier.kind === ts.SyntaxKind.ProtectedKeyword) {
210
- return Visibility.Protected;
211
- }
212
- else if (modifier.kind === ts.SyntaxKind.PrivateKeyword) {
213
- return Visibility.Private;
214
- }
215
- }
216
- }
217
- return Visibility.Public;
218
- };
219
- Compiler.prototype.tryResolveTypeFromExpression = function (exp, write) {
220
- var _a;
221
- if (ts.isNewExpression(exp)) {
222
- // try get generic type from expression
223
- if ((_a = exp.typeArguments) === null || _a === void 0 ? void 0 : _a.length) {
224
- for (var _i = 0, _b = exp.typeArguments; _i < _b.length; _i++) {
225
- var arg = _b[_i];
226
- var type = this.resolveType(arg, write);
227
- if (type) {
228
- return type;
229
- }
230
- }
231
- }
232
- var typeName = exp.expression.getText();
233
- // const typeName = exp.expression.getText();
234
- // console.log("NEW EXPRESSION", typeName);
235
- // const args = exp.arguments?.map(arg => this.resolveExpression(arg, write));
236
- // write.writeNewTypeExpression(typeName, args);
237
- return typeName;
238
- }
239
- };
240
- Compiler.prototype.resolveType = function (typeNode, writer) {
241
- if (!typeNode)
242
- return undefined;
243
- // check if its an array
244
- if (this.isArrayType(typeNode)) {
245
- if (typeNode.kind === ts.SyntaxKind.ArrayType) {
246
- var arrayType = typeNode;
247
- return this.resolveType(arrayType.elementType, writer);
248
- }
249
- // check if its Array<> or Map
250
- if (typeNode.kind === ts.SyntaxKind.TypeReference) {
251
- var typeReference = typeNode;
252
- if (typeReference.typeName.getText() === "Array") {
253
- var typeArgument = typeReference.typeArguments && typeReference.typeArguments[0];
254
- return this.resolveType(typeArgument, writer);
255
- }
256
- else if (typeReference.typeName.getText() === "Map") {
257
- var typeArgument = typeReference.typeArguments && typeReference.typeArguments[0];
258
- return this.resolveType(typeArgument, writer);
259
- }
260
- }
261
- // check if its dictionary
262
- if (typeNode.kind === ts.SyntaxKind.TypeLiteral) {
263
- var typeLiteral = typeNode;
264
- var members = typeLiteral.members;
265
- if (members.length === 2) {
266
- var firstMember = members[0];
267
- var secondMember = members[1];
268
- if (firstMember.kind === ts.SyntaxKind.IndexSignature && secondMember.kind === ts.SyntaxKind.IndexSignature) {
269
- var firstIndexSignature = firstMember;
270
- var secondIndexSignature = secondMember;
271
- var firstIndexType = firstIndexSignature.parameters[0].type;
272
- var secondIndexType = secondIndexSignature.parameters[0].type;
273
- if (firstIndexType.kind === ts.SyntaxKind.StringKeyword && secondIndexType.kind === ts.SyntaxKind.NumberKeyword) {
274
- var valueType = firstIndexSignature.type;
275
- return this.resolveType(valueType, writer);
276
- }
277
- }
278
- }
279
- }
280
- // is generic
281
- // if (typeNode.kind === ts.SyntaxKind.TypeReference) {
282
- // const typeReference = typeNode as ts.TypeReferenceNode;
283
- // const typeArgument = typeReference.typeArguments && typeReference.typeArguments[0];
284
- // return this.resolveType(typeArgument, writer);
285
- // }
286
- console.error("!!!!! ----- Unknown array type", typeNode.getText());
287
- }
288
- var type = typeNode.getText();
289
- return this.resolveTypeFromString(type, writer);
290
- };
291
- Compiler.prototype.resolveTypeFromString = function (type, writer) {
292
- var resolved = writer.resolveTypeName(type);
293
- return resolved || type;
294
- };
295
- Compiler.prototype.isArrayType = function (typeNode) {
296
- if (!typeNode)
297
- return false;
298
- // check if its an array
299
- if (typeNode.kind === ts.SyntaxKind.ArrayType) {
300
- return true;
301
- }
302
- // check if its Array<> or map
303
- if (typeNode.kind === ts.SyntaxKind.TypeReference) {
304
- var typeReference = typeNode;
305
- if (typeReference.typeName.getText() === "Array") {
306
- return true;
307
- }
308
- else if (typeReference.typeName.getText() === "Map") {
309
- return true;
310
- }
311
- }
312
- // check if its array or dictionary
313
- if (typeNode.kind === ts.SyntaxKind.TypeLiteral) {
314
- var typeLiteral = typeNode;
315
- if (typeLiteral.members.length === 2) {
316
- var first = typeLiteral.members[0];
317
- var second = typeLiteral.members[1];
318
- if (first.kind === ts.SyntaxKind.IndexSignature && second.kind === ts.SyntaxKind.IndexSignature) {
319
- return true;
320
- }
321
- }
322
- }
323
- return false;
324
- };
325
- Compiler.prototype.resolveExpression = function (node, writer) {
326
- var _this = this;
327
- if (ts.isStringLiteral(node)) {
328
- return this.resolveTypeFromString(node.text, writer);
329
- }
330
- else if (ts.isNumericLiteral(node)) {
331
- return this.resolveTypeFromString(node.text, writer);
332
- }
333
- else if (ts.isIdentifier(node)) {
334
- return this.resolveTypeFromString(node.text, writer);
335
- }
336
- // is true or false
337
- else if (node.kind === ts.SyntaxKind.TrueKeyword || node.kind === ts.SyntaxKind.FalseKeyword) {
338
- return this.resolveTypeFromString(node.getText(), writer);
339
- }
340
- else if (ts.isPropertyAccessExpression(node)) {
341
- var left = this.resolveExpression(node.expression, writer);
342
- if (left) {
343
- return left + "." + this.resolveTypeFromString(node.name.text, writer);
344
- }
345
- }
346
- else if (ts.isCallExpression(node)) {
347
- var left = this.resolveExpression(node.expression, writer);
348
- if (left) {
349
- var args = node.arguments.map(function (a) { return _this.resolveExpression(a, writer); }).filter(function (a) { return a; });
350
- return left + "(" + args.join(", ") + ")";
351
- }
352
- }
353
- else if (ts.isBinaryExpression(node)) {
354
- var left = this.resolveExpression(node.left, writer);
355
- var right = this.resolveExpression(node.right, writer);
356
- if (left && right) {
357
- return left + " " + node.operatorToken.getText() + " " + right;
358
- }
359
- }
360
- else if (ts.isPrefixUnaryExpression(node)) {
361
- var operand = this.resolveExpression(node.operand, writer);
362
- if (operand) {
363
- return node.operator + operand;
364
- }
365
- }
366
- else if (ts.isPostfixUnaryExpression(node)) {
367
- var operand = this.resolveExpression(node.operand, writer);
368
- if (operand) {
369
- return operand + node.operator;
370
- }
371
- }
372
- else if (ts.isParenthesizedExpression(node)) {
373
- return this.resolveExpression(node.expression, writer);
374
- }
375
- else if (ts.isArrayLiteralExpression(node)) {
376
- var elements = node.elements.map(function (e) { return _this.resolveExpression(e, writer); }).filter(function (e) { return e; });
377
- return "[" + elements.join(", ") + "]";
378
- }
379
- else if (ts.isObjectLiteralExpression(node)) {
380
- var properties = node.properties.map(function (p) {
381
- if (ts.isPropertyAssignment(p)) {
382
- var name_2 = p.name.getText();
383
- var value = _this.resolveExpression(p.initializer, writer);
384
- if (value) {
385
- return name_2 + ": " + value;
386
- }
387
- }
388
- }).filter(function (p) { return p; });
389
- return "{" + properties.join(", ") + "}";
390
- }
391
- else if (ts.isNewExpression(node)) {
392
- var typeName = this.resolveTypeFromString(node.expression.getText(), writer);
393
- if (typeName) {
394
- var args = node.arguments.map(function (a) { return _this.resolveExpression(a, writer); }).filter(function (a) { return a; });
395
- // writer.writeNewTypeExpression(typeName, args);
396
- // return;
397
- return "new " + typeName + "(" + args.join(", ") + ")";
398
- }
399
- }
400
- console.error("!!!!! ----- Unknown expression", ts.SyntaxKind[node.kind], node.getText());
401
- };
402
- return Compiler;
403
- }());
404
- exports.Compiler = Compiler;