node-opcua-convert-nodeset-to-javascript 2.76.0 → 2.77.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.
Files changed (44) hide show
  1. package/dist/_dataType.d.ts +10 -0
  2. package/dist/_dataType.js +153 -0
  3. package/dist/_dataType.js.map +1 -0
  4. package/dist/convert_namespace_to_typescript.d.ts +3 -3
  5. package/dist/convert_namespace_to_typescript.js +182 -182
  6. package/dist/convert_to_typescript.d.ts +105 -110
  7. package/dist/convert_to_typescript.js +753 -987
  8. package/dist/convert_to_typescript.js.map +1 -1
  9. package/dist/index.d.ts +3 -3
  10. package/dist/index.js +19 -19
  11. package/dist/main.d.ts +1 -1
  12. package/dist/main.js +77 -74
  13. package/dist/main.js.map +1 -1
  14. package/dist/official_namespaces.d.ts +23 -23
  15. package/dist/official_namespaces.js +35 -35
  16. package/dist/official_namespaces.js.map +1 -1
  17. package/dist/options.d.ts +5 -5
  18. package/dist/options.js +2 -2
  19. package/dist/private/cache.d.ts +61 -59
  20. package/dist/private/cache.js +236 -221
  21. package/dist/private/cache.js.map +1 -1
  22. package/dist/private/get_corresponding_data_type.d.ts +14 -0
  23. package/dist/private/get_corresponding_data_type.js +104 -0
  24. package/dist/private/get_corresponding_data_type.js.map +1 -0
  25. package/dist/private/to_filename.d.ts +1 -1
  26. package/dist/private/to_filename.js +9 -9
  27. package/dist/private/utils.d.ts +31 -31
  28. package/dist/private/utils.js +289 -289
  29. package/dist/private-stuff.d.ts +4 -4
  30. package/dist/private-stuff.js +20 -20
  31. package/dist/utils2.d.ts +7 -0
  32. package/dist/utils2.js +58 -0
  33. package/dist/utils2.js.map +1 -0
  34. package/dist/walk_through.d.ts +13 -13
  35. package/dist/walk_through.js +86 -86
  36. package/package.json +19 -18
  37. package/source/_dataType.ts +164 -0
  38. package/source/convert_to_typescript.ts +4 -252
  39. package/source/main.ts +5 -2
  40. package/source/official_namespaces.ts +0 -1
  41. package/source/private/cache.ts +24 -7
  42. package/source/private/get_corresponding_data_type.ts +104 -0
  43. package/source/utils2.ts +55 -0
  44. package/tsconfig-generated.json +4 -1
package/dist/utils2.js ADDED
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.f1 = exports.f2 = exports.quotifyIfNecessary = exports.toJavascritPropertyName = exports.toComment = void 0;
4
+ const node_opcua_utils_1 = require("node-opcua-utils");
5
+ const wrap = require("wordwrap");
6
+ const wrapText = wrap(0, 50);
7
+ function toComment(prefix, description) {
8
+ const d = wrapText(description);
9
+ return d
10
+ .split("\n")
11
+ .map((x) => prefix + x)
12
+ .join("\n");
13
+ }
14
+ exports.toComment = toComment;
15
+ // to avoid clashes
16
+ function toJavascritPropertyName(childName, { ignoreConflictingName }) {
17
+ childName = (0, node_opcua_utils_1.lowerFirstLetter)(childName);
18
+ if (ignoreConflictingName) {
19
+ if (childName === "namespaceUri") {
20
+ childName = "$namespaceUri";
21
+ }
22
+ if (childName === "rolePermissions") {
23
+ childName = "$rolePermissions";
24
+ }
25
+ if (childName === "displayName") {
26
+ childName = "$displayName";
27
+ }
28
+ if (childName === "eventNotifier") {
29
+ childName = "$eventNotifier";
30
+ }
31
+ if (childName === "description") {
32
+ childName = "$description";
33
+ }
34
+ if (childName === "decode") {
35
+ childName = "$decode";
36
+ }
37
+ if (childName === "encode") {
38
+ childName = "$encode";
39
+ }
40
+ }
41
+ return childName.replace(/</g, "$").replace(/>/g, "$").replace(/ |\./g, "_").replace(/#/g, "_");
42
+ }
43
+ exports.toJavascritPropertyName = toJavascritPropertyName;
44
+ function quotifyIfNecessary(s) {
45
+ if (s.match(/(^[^a-zA-Z])|([^a-zA-Z_0-9])/)) {
46
+ return `"${s}"`;
47
+ }
48
+ if (s === "nodeClass") {
49
+ return `["$nodeClass"]`;
50
+ }
51
+ return s;
52
+ }
53
+ exports.quotifyIfNecessary = quotifyIfNecessary;
54
+ const f2 = (str) => str.padEnd(50, "-");
55
+ exports.f2 = f2;
56
+ const f1 = (str) => str.padEnd(50, " ");
57
+ exports.f1 = f1;
58
+ //# sourceMappingURL=utils2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils2.js","sourceRoot":"","sources":["../source/utils2.ts"],"names":[],"mappings":";;;AAAA,uDAAoD;AACpD,iCAAiC;AAEjC,MAAM,QAAQ,GAAG,IAAI,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAE7B,SAAgB,SAAS,CAAC,MAAc,EAAE,WAAmB;IACzD,MAAM,CAAC,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAC;IAChC,OAAO,CAAC;SACH,KAAK,CAAC,IAAI,CAAC;SACX,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC;SACtB,IAAI,CAAC,IAAI,CAAC,CAAC;AACpB,CAAC;AAND,8BAMC;AAED,mBAAmB;AACnB,SAAgB,uBAAuB,CAAC,SAAiB,EAAE,EAAE,qBAAqB,EAAsC;IACpH,SAAS,GAAG,IAAA,mCAAgB,EAAC,SAAS,CAAC,CAAC;IAExC,IAAI,qBAAqB,EAAE;QACvB,IAAI,SAAS,KAAK,cAAc,EAAE;YAC9B,SAAS,GAAG,eAAe,CAAC;SAC/B;QACD,IAAI,SAAS,KAAK,iBAAiB,EAAE;YACjC,SAAS,GAAG,kBAAkB,CAAC;SAClC;QACD,IAAI,SAAS,KAAK,aAAa,EAAE;YAC7B,SAAS,GAAG,cAAc,CAAC;SAC9B;QACD,IAAI,SAAS,KAAK,eAAe,EAAE;YAC/B,SAAS,GAAG,gBAAgB,CAAC;SAChC;QACD,IAAI,SAAS,KAAK,aAAa,EAAE;YAC7B,SAAS,GAAG,cAAc,CAAC;SAC9B;QACD,IAAI,SAAS,KAAK,QAAQ,EAAE;YACxB,SAAS,GAAG,SAAS,CAAC;SACzB;QACD,IAAI,SAAS,KAAK,QAAQ,EAAE;YACxB,SAAS,GAAG,SAAS,CAAC;SACzB;KACJ;IACD,OAAO,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;AACpG,CAAC;AA3BD,0DA2BC;AAED,SAAgB,kBAAkB,CAAC,CAAS;IACxC,IAAI,CAAC,CAAC,KAAK,CAAC,8BAA8B,CAAC,EAAE;QACzC,OAAO,IAAI,CAAC,GAAG,CAAC;KACnB;IACD,IAAI,CAAC,KAAK,WAAW,EAAE;QACnB,OAAO,gBAAgB,CAAC;KAC3B;IACD,OAAO,CAAC,CAAC;AACb,CAAC;AARD,gDAQC;AAEM,MAAM,EAAE,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;AAA1C,QAAA,EAAE,MAAwC;AAChD,MAAM,EAAE,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;AAA1C,QAAA,EAAE,MAAwC"}
@@ -1,13 +1,13 @@
1
- import { IBasicSession } from "node-opcua-pseudo-session";
2
- import { ReferenceDescription } from "node-opcua-types";
3
- export interface ReferenceDescriptionEx extends ReferenceDescription {
4
- parent: ReferenceDescriptionEx;
5
- }
6
- export interface NodeVisitor {
7
- visit(reference: ReferenceDescriptionEx, level: number): Promise<void>;
8
- }
9
- export declare function walkThroughReferenceTypes(session: IBasicSession, nodeVisitor?: NodeVisitor): Promise<void>;
10
- export declare function walkThroughDataTypes(session: IBasicSession, nodeVisitor?: NodeVisitor): Promise<void>;
11
- export declare function walkThroughInterfaceTypes(session: IBasicSession, nodeVisitor?: NodeVisitor): Promise<void>;
12
- export declare function walkThroughObjectTypes(session: IBasicSession, nodeVisitor?: NodeVisitor): Promise<void>;
13
- export declare function walkThroughVariableTypes(session: IBasicSession, nodeVisitor?: NodeVisitor): Promise<void>;
1
+ import { IBasicSession } from "node-opcua-pseudo-session";
2
+ import { ReferenceDescription } from "node-opcua-types";
3
+ export interface ReferenceDescriptionEx extends ReferenceDescription {
4
+ parent: ReferenceDescriptionEx;
5
+ }
6
+ export interface NodeVisitor {
7
+ visit(reference: ReferenceDescriptionEx, level: number): Promise<void>;
8
+ }
9
+ export declare function walkThroughReferenceTypes(session: IBasicSession, nodeVisitor?: NodeVisitor): Promise<void>;
10
+ export declare function walkThroughDataTypes(session: IBasicSession, nodeVisitor?: NodeVisitor): Promise<void>;
11
+ export declare function walkThroughInterfaceTypes(session: IBasicSession, nodeVisitor?: NodeVisitor): Promise<void>;
12
+ export declare function walkThroughObjectTypes(session: IBasicSession, nodeVisitor?: NodeVisitor): Promise<void>;
13
+ export declare function walkThroughVariableTypes(session: IBasicSession, nodeVisitor?: NodeVisitor): Promise<void>;
@@ -1,87 +1,87 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.walkThroughVariableTypes = exports.walkThroughObjectTypes = exports.walkThroughInterfaceTypes = exports.walkThroughDataTypes = exports.walkThroughReferenceTypes = void 0;
13
- const node_opcua_data_model_1 = require("node-opcua-data-model");
14
- const node_opcua_nodeid_1 = require("node-opcua-nodeid");
15
- const node_opcua_types_1 = require("node-opcua-types");
16
- function _walkThroughTypes(session, nodeId, visitor, level, parent) {
17
- return __awaiter(this, void 0, void 0, function* () {
18
- level = level || 0;
19
- if (!parent) {
20
- const dataValues = yield session.read([
21
- { nodeId, attributeId: node_opcua_data_model_1.AttributeIds.BrowseName },
22
- { nodeId, attributeId: node_opcua_data_model_1.AttributeIds.NodeClass }
23
- ]);
24
- const browseName = dataValues[0].value.value;
25
- const nodeClass = dataValues[1].value.value;
26
- parent = new node_opcua_types_1.ReferenceDescription({
27
- isForward: true,
28
- nodeId: nodeId,
29
- browseName,
30
- nodeClass,
31
- referenceTypeId: (0, node_opcua_nodeid_1.resolveNodeId)("HasSubtype")
32
- // typeDefinition,
33
- });
34
- }
35
- const browseResult = yield session.browse({
36
- browseDirection: node_opcua_data_model_1.BrowseDirection.Forward,
37
- nodeId,
38
- includeSubtypes: true,
39
- referenceTypeId: (0, node_opcua_nodeid_1.resolveNodeId)("HasSubtype"),
40
- resultMask: 0xff
41
- });
42
- for (const reference of browseResult.references || []) {
43
- if (visitor) {
44
- const r = reference;
45
- r.parent = parent;
46
- yield visitor.visit(r, level);
47
- }
48
- yield _walkThroughTypes(session, reference.nodeId, visitor, level + 1, reference);
49
- }
50
- });
51
- }
52
- function walkThroughReferenceTypes(session, nodeVisitor) {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- const baseReferenceTypeNodeId = (0, node_opcua_nodeid_1.resolveNodeId)("References");
55
- yield _walkThroughTypes(session, baseReferenceTypeNodeId, nodeVisitor);
56
- });
57
- }
58
- exports.walkThroughReferenceTypes = walkThroughReferenceTypes;
59
- function walkThroughDataTypes(session, nodeVisitor) {
60
- return __awaiter(this, void 0, void 0, function* () {
61
- const baseDataTypeTypeNodeId = (0, node_opcua_nodeid_1.resolveNodeId)("BaseDataType");
62
- yield _walkThroughTypes(session, baseDataTypeTypeNodeId, nodeVisitor);
63
- });
64
- }
65
- exports.walkThroughDataTypes = walkThroughDataTypes;
66
- function walkThroughInterfaceTypes(session, nodeVisitor) {
67
- return __awaiter(this, void 0, void 0, function* () {
68
- const baseInterfaceTypeNodeId = (0, node_opcua_nodeid_1.resolveNodeId)("BaseInterfaceType");
69
- yield _walkThroughTypes(session, baseInterfaceTypeNodeId, nodeVisitor);
70
- });
71
- }
72
- exports.walkThroughInterfaceTypes = walkThroughInterfaceTypes;
73
- function walkThroughObjectTypes(session, nodeVisitor) {
74
- return __awaiter(this, void 0, void 0, function* () {
75
- const baseObjectType = (0, node_opcua_nodeid_1.resolveNodeId)("BaseObjectType");
76
- yield _walkThroughTypes(session, baseObjectType, nodeVisitor);
77
- });
78
- }
79
- exports.walkThroughObjectTypes = walkThroughObjectTypes;
80
- function walkThroughVariableTypes(session, nodeVisitor) {
81
- return __awaiter(this, void 0, void 0, function* () {
82
- const baseObjectType = (0, node_opcua_nodeid_1.resolveNodeId)("BaseVariableType");
83
- yield _walkThroughTypes(session, baseObjectType, nodeVisitor);
84
- });
85
- }
86
- exports.walkThroughVariableTypes = walkThroughVariableTypes;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.walkThroughVariableTypes = exports.walkThroughObjectTypes = exports.walkThroughInterfaceTypes = exports.walkThroughDataTypes = exports.walkThroughReferenceTypes = void 0;
13
+ const node_opcua_data_model_1 = require("node-opcua-data-model");
14
+ const node_opcua_nodeid_1 = require("node-opcua-nodeid");
15
+ const node_opcua_types_1 = require("node-opcua-types");
16
+ function _walkThroughTypes(session, nodeId, visitor, level, parent) {
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ level = level || 0;
19
+ if (!parent) {
20
+ const dataValues = yield session.read([
21
+ { nodeId, attributeId: node_opcua_data_model_1.AttributeIds.BrowseName },
22
+ { nodeId, attributeId: node_opcua_data_model_1.AttributeIds.NodeClass }
23
+ ]);
24
+ const browseName = dataValues[0].value.value;
25
+ const nodeClass = dataValues[1].value.value;
26
+ parent = new node_opcua_types_1.ReferenceDescription({
27
+ isForward: true,
28
+ nodeId: nodeId,
29
+ browseName,
30
+ nodeClass,
31
+ referenceTypeId: (0, node_opcua_nodeid_1.resolveNodeId)("HasSubtype")
32
+ // typeDefinition,
33
+ });
34
+ }
35
+ const browseResult = yield session.browse({
36
+ browseDirection: node_opcua_data_model_1.BrowseDirection.Forward,
37
+ nodeId,
38
+ includeSubtypes: true,
39
+ referenceTypeId: (0, node_opcua_nodeid_1.resolveNodeId)("HasSubtype"),
40
+ resultMask: 0xff
41
+ });
42
+ for (const reference of browseResult.references || []) {
43
+ if (visitor) {
44
+ const r = reference;
45
+ r.parent = parent;
46
+ yield visitor.visit(r, level);
47
+ }
48
+ yield _walkThroughTypes(session, reference.nodeId, visitor, level + 1, reference);
49
+ }
50
+ });
51
+ }
52
+ function walkThroughReferenceTypes(session, nodeVisitor) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const baseReferenceTypeNodeId = (0, node_opcua_nodeid_1.resolveNodeId)("References");
55
+ yield _walkThroughTypes(session, baseReferenceTypeNodeId, nodeVisitor);
56
+ });
57
+ }
58
+ exports.walkThroughReferenceTypes = walkThroughReferenceTypes;
59
+ function walkThroughDataTypes(session, nodeVisitor) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ const baseDataTypeTypeNodeId = (0, node_opcua_nodeid_1.resolveNodeId)("BaseDataType");
62
+ yield _walkThroughTypes(session, baseDataTypeTypeNodeId, nodeVisitor);
63
+ });
64
+ }
65
+ exports.walkThroughDataTypes = walkThroughDataTypes;
66
+ function walkThroughInterfaceTypes(session, nodeVisitor) {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ const baseInterfaceTypeNodeId = (0, node_opcua_nodeid_1.resolveNodeId)("BaseInterfaceType");
69
+ yield _walkThroughTypes(session, baseInterfaceTypeNodeId, nodeVisitor);
70
+ });
71
+ }
72
+ exports.walkThroughInterfaceTypes = walkThroughInterfaceTypes;
73
+ function walkThroughObjectTypes(session, nodeVisitor) {
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ const baseObjectType = (0, node_opcua_nodeid_1.resolveNodeId)("BaseObjectType");
76
+ yield _walkThroughTypes(session, baseObjectType, nodeVisitor);
77
+ });
78
+ }
79
+ exports.walkThroughObjectTypes = walkThroughObjectTypes;
80
+ function walkThroughVariableTypes(session, nodeVisitor) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ const baseObjectType = (0, node_opcua_nodeid_1.resolveNodeId)("BaseVariableType");
83
+ yield _walkThroughTypes(session, baseObjectType, nodeVisitor);
84
+ });
85
+ }
86
+ exports.walkThroughVariableTypes = walkThroughVariableTypes;
87
87
  //# sourceMappingURL=walk_through.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-opcua-convert-nodeset-to-javascript",
3
- "version": "2.76.0",
3
+ "version": "2.77.0",
4
4
  "description": "pure nodejs OPCUA SDK - module -convert-nodeset-to-javascript",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",
@@ -15,25 +15,26 @@
15
15
  "@types/wordwrap": "^1.0.1",
16
16
  "case-anything": "1.1.5",
17
17
  "chalk": "4.1.2",
18
- "node-opcua-address-space": "2.76.0",
19
- "node-opcua-address-space-base": "2.76.0",
20
- "node-opcua-assert": "2.76.0",
21
- "node-opcua-constants": "2.74.0",
22
- "node-opcua-data-model": "2.76.0",
23
- "node-opcua-debug": "2.76.0",
24
- "node-opcua-nodeid": "2.76.0",
25
- "node-opcua-nodesets": "2.74.0",
26
- "node-opcua-pseudo-session": "2.76.0",
27
- "node-opcua-status-code": "2.76.0",
28
- "node-opcua-types": "2.76.0",
29
- "node-opcua-utils": "2.76.0",
30
- "node-opcua-variant": "2.76.0",
18
+ "node-opcua-address-space": "2.77.0",
19
+ "node-opcua-address-space-base": "2.77.0",
20
+ "node-opcua-assert": "2.77.0",
21
+ "node-opcua-constants": "2.77.0",
22
+ "node-opcua-data-model": "2.77.0",
23
+ "node-opcua-debug": "2.77.0",
24
+ "node-opcua-extension-object": "2.77.0",
25
+ "node-opcua-nodeid": "2.77.0",
26
+ "node-opcua-nodesets": "2.77.0",
27
+ "node-opcua-pseudo-session": "2.77.0",
28
+ "node-opcua-status-code": "2.77.0",
29
+ "node-opcua-types": "2.77.0",
30
+ "node-opcua-utils": "2.77.0",
31
+ "node-opcua-variant": "2.77.0",
31
32
  "wordwrap": "^1.0.0"
32
33
  },
33
34
  "devDependencies": {
34
- "node-opcua-leak-detector": "2.76.0",
35
- "node-opcua-packet-analyzer": "2.76.0",
36
- "node-opcua-service-translate-browse-path": "2.76.0",
35
+ "node-opcua-leak-detector": "2.77.0",
36
+ "node-opcua-packet-analyzer": "2.77.0",
37
+ "node-opcua-service-translate-browse-path": "2.77.0",
37
38
  "should": "^13.2.3"
38
39
  },
39
40
  "author": "Etienne Rossignon",
@@ -51,5 +52,5 @@
51
52
  "internet of things"
52
53
  ],
53
54
  "homepage": "http://node-opcua.github.io/",
54
- "gitHead": "75d9b8cf894c8fbadf77d2c4a48a730d055465e7"
55
+ "gitHead": "5c8d45772d786fa4ba59369dd26679353ab5482b"
55
56
  }
@@ -0,0 +1,164 @@
1
+ import { NodeClass } from "node-opcua-data-model";
2
+ import { NodeId } from "node-opcua-nodeid";
3
+ import { IBasicSession } from "node-opcua-pseudo-session";
4
+ import { EnumDefinition, StructureDefinition, StructureField } from "node-opcua-types";
5
+ import { LineFile } from "node-opcua-utils";
6
+ import { DataType } from "node-opcua-variant";
7
+ import { Type } from "./convert_to_typescript";
8
+ import {
9
+ Import,
10
+ getDescription,
11
+ getDefinition,
12
+ getBrowseName,
13
+ getIsAbstract,
14
+ makeTypeNameNew,
15
+ getSubtypeNodeId
16
+ } from "./private-stuff";
17
+ import { Cache, makeName2 } from "./private/cache";
18
+ import { getCorrepondingJavascriptType } from "./private/get_corresponding_data_type";
19
+ import { f1, f2, quotifyIfNecessary, toComment, toJavascritPropertyName } from "./utils2";
20
+
21
+ // eslint-disable-next-line max-statements, complexity
22
+ export async function _exportDataTypeToTypescript(
23
+ session: IBasicSession,
24
+ nodeId: NodeId,
25
+ cache: Cache,
26
+ f?: LineFile
27
+ ): Promise<{ type: Type; content: string; typeName: string }> {
28
+ const importCollect: any = undefined;
29
+ const referenceBasicType = (name: string): string => {
30
+ const t = { name, namespace: -1, module: "BasicType" };
31
+ importCollect && importCollect(t);
32
+ cache.ensureImported(t);
33
+ return t.name;
34
+ };
35
+
36
+ f = f || new LineFile();
37
+
38
+ const importCollector = (i: Import) => {
39
+ cache.ensureImported(i);
40
+ };
41
+ const nodeClass = NodeClass.DataType;
42
+ const description = await getDescription(session, nodeId);
43
+ const definition = await getDefinition(session, nodeId);
44
+ const browseName = await getBrowseName(session, nodeId);
45
+ const isAbstract = await getIsAbstract(session, nodeId);
46
+
47
+ const interfaceImport: Import = makeTypeNameNew(nodeClass, definition, browseName);
48
+ const interfaceName = interfaceImport.name;
49
+
50
+ const superType = await getSubtypeNodeId(session, nodeId);
51
+ const baseInterfaceImport: Import = makeTypeNameNew(nodeClass, definition, superType.browseName);
52
+
53
+ cache.ensureImported(baseInterfaceImport);
54
+
55
+ const baseInterfaceName = baseInterfaceImport.name;
56
+ // f.write(superType.toString());
57
+ f.write(`/**`);
58
+ if (description.text) {
59
+ f.write(toComment(" * ", description.text || ""));
60
+ f.write(` *`);
61
+ }
62
+ f.write(` * | |${f1(" ")}|`);
63
+ f.write(` * |-----------|${f2("-")}|`);
64
+ f.write(` * | namespace |${f1(cache.namespace[nodeId.namespace].namespaceUri)}|`);
65
+ f.write(` * | nodeClass |${f1(NodeClass[nodeClass])}|`);
66
+ f.write(` * | name |${f1(browseName.toString())}|`);
67
+ f.write(` * | isAbstract|${f1(isAbstract.toString())}|`);
68
+ f.write(` */`);
69
+
70
+ let type: "basic" | "structure" | "enum" | "ua" = "basic";
71
+ if (definition instanceof EnumDefinition) {
72
+ type = "enum";
73
+ f.write(`export enum ${interfaceName} {`);
74
+ for (const field of definition.fields!) {
75
+ if (field.description.text) {
76
+ f.write(` /**`);
77
+ f.write(toComment(" * ", field.description.text || ""));
78
+ f.write(` */`);
79
+ }
80
+ f.write(` ${quotifyIfNecessary(field.name!)} = ${field.value[1]},`);
81
+ }
82
+ f.write(`}`);
83
+ } else if (definition instanceof StructureDefinition) {
84
+ if (baseInterfaceName === "DTUnion") {
85
+ type = "structure";
86
+ for (let i = 0; i < definition.fields!.length; i++) {
87
+ f.write(`export interface ${interfaceName}_${i} extends ${baseInterfaceName} {`);
88
+ for (let j = 0; j < definition.fields!.length; j++) {
89
+ const field = definition.fields![j];
90
+ const fieldName = toJavascritPropertyName(field.name!, { ignoreConflictingName: false });
91
+ if (j === i) {
92
+ await outputStructureField(f, field);
93
+ } else {
94
+ f.write(` ${quotifyIfNecessary(fieldName)}?: never`);
95
+ }
96
+ }
97
+ f.write(`}`);
98
+ }
99
+ f.write(`export type ${interfaceName} = `);
100
+ for (let i = 0; i < definition.fields!.length; i++) {
101
+ f.write(` | ${interfaceName}_${i}`);
102
+ }
103
+ f.write(` ;`);
104
+ } else {
105
+ type = "structure";
106
+ if (!definition.fields!.length && interfaceName !== "DTStructure") {
107
+ f.write(`export type ${interfaceName} = ${baseInterfaceName};`);
108
+ } else {
109
+ if (interfaceName === "DTStructure") {
110
+ f.write(`export interface ${interfaceName} {`);
111
+ } else {
112
+ f.write(`export interface ${interfaceName} extends ${baseInterfaceName} {`);
113
+ }
114
+ for (const field of definition.fields!) {
115
+ await outputStructureField(f, field);
116
+ }
117
+ f.write(`}`);
118
+ //
119
+
120
+ referenceBasicType("ExtensionObject");
121
+ // ache.ensureImported({ module: "BasicType", name: "ExtensionObject", namespace: 0 });
122
+ // interface TighteningResultOptions extends Partial<DTTighteningResult> {}
123
+ // interface TighteningResult extends ExtensionObject, DTTighteningResult {}
124
+ const full = makeName2(interfaceName);
125
+
126
+ // filter issue with UDTOpticalVerifierScanResult that has a decode:Uint32 conflicting with the ExtensionObject decode method
127
+
128
+ const toAvoid = ["decode", "encode"];
129
+ const collidingNames = definition
130
+ .fields!.map((a) => toJavascritPropertyName(a.name! ,{ ignoreConflictingName: false }))
131
+ .filter((d) => toAvoid.indexOf(d.toLowerCase()) !== -1);
132
+
133
+ const adpatedIntefaceName =
134
+ collidingNames.length === 0
135
+ ? interfaceName
136
+ : `Omit<${interfaceName},${collidingNames.map((a) => `"${a}"`).join(",")}>`;
137
+
138
+ f.write(`export interface ${full} extends ExtensionObject, ${adpatedIntefaceName} {};`);
139
+ }
140
+ }
141
+ } else {
142
+ type = "basic";
143
+ f.write(`// NO DEFINITION`);
144
+ f.write(`export type ${interfaceName} = ${baseInterfaceName};`);
145
+ // throw new Error("Invalid " + definition?.constructor.name);
146
+ }
147
+ return { type, content: f.toString(), typeName: interfaceName };
148
+
149
+ async function outputStructureField(f: LineFile, field: StructureField) {
150
+ const fieldName = toJavascritPropertyName(field.name!, { ignoreConflictingName: false });
151
+ // special case ! fieldName=
152
+ if (field.description.text) {
153
+ f.write(` /** ${field.description.text}*/`);
154
+ }
155
+ const opt = field.isOptional ? "?" : "";
156
+ const arrayMarker = field.valueRank >= 1 ? "[]" : "";
157
+ const { dataType, jtype } = await getCorrepondingJavascriptType(session, field.dataType, cache, importCollector);
158
+ f.write(
159
+ ` ${quotifyIfNecessary(fieldName)}${opt}: ${jtype}${arrayMarker}; // ${
160
+ DataType[dataType]
161
+ } ${field.dataType.toString()}`
162
+ );
163
+ }
164
+ }