@dedot/codegen 0.9.0 → 0.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.
@@ -40,56 +40,74 @@ class TypesGen extends index_js_1.BaseTypesGen {
40
40
  return false;
41
41
  }
42
42
  // Find out does type depends on known types
43
- // Example: `type TypeA = Struct { field: AccountId }` => includeKnownTypes(TypeA) === true
44
- #includeKnownTypes(typeId) {
43
+ //
44
+ // Example:
45
+ // type TypeA = Struct { field: AccountId } => includeKnownTypes(TypeA) === true
46
+ // type TypeA = Struct { field: TypeB }; type TypeB = Struct { field: AccountId, type: TypeA } => includeKnownTypes(TypeA) === true
47
+ //
48
+ // `checked` is used to prevent circular dependencies
49
+ #includeKnownTypes(typeId, checked = new Set()) {
50
+ // Return `false` because `typeId` is already checked
51
+ // If it were a known type, the first check would have returned `true`.
52
+ if (checked.has(typeId))
53
+ return false;
45
54
  if (this.knownTypes[typeId])
46
55
  return true;
56
+ checked.add(typeId);
47
57
  const { types } = this.contractMetadata;
48
58
  const defA = (0, contracts_1.normalizeContractTypeDef)(types[typeId].type.def);
49
59
  const { type, value } = defA;
50
60
  switch (type) {
51
61
  case 'Struct':
52
- return value.fields.some(({ typeId }) => this.#includeKnownTypes(typeId));
62
+ return value.fields.some(({ typeId }) => this.#includeKnownTypes(typeId, checked));
53
63
  case 'Enum':
54
- return value.members.some(({ fields }) => fields.some(({ typeId }) => this.#includeKnownTypes(typeId)));
64
+ return value.members.some(({ fields }) => fields.some(({ typeId }) => this.#includeKnownTypes(typeId, checked)));
55
65
  case 'Tuple':
56
- return value.fields.some((typeId) => this.#includeKnownTypes(typeId));
66
+ return value.fields.some((typeId) => this.#includeKnownTypes(typeId, checked));
57
67
  case 'Sequence':
58
68
  case 'SizedVec':
69
+ case 'Compact':
59
70
  const $innerType = this.types[value.typeParam].typeDef;
60
71
  if ($innerType.type === 'Primitive' && $innerType.value.kind === 'u8') {
61
72
  return true; // ByteLikes
62
73
  }
63
74
  else {
64
- return this.#includeKnownTypes(value.typeParam);
75
+ return this.#includeKnownTypes(value.typeParam, checked);
65
76
  }
66
77
  case 'Primitive':
67
- case 'Compact':
68
78
  case 'BitSequence':
69
79
  return false;
70
80
  }
71
81
  }
72
82
  // Find out does typeA depends on typeB
73
- // Example: `type TypeA = Struct { field: TypeB }` => typeDependOn(TypeA, TypeB) === true
74
- #typeDependOn(typeA, typeB) {
83
+ //
84
+ // Example:
85
+ // type TypeA = Struct { field: TypeB } => typeDependOn(TypeA, TypeB) === true
86
+ // type TypeA = Struct { field: TypeB }; type TypeB = Struct { field: TypeA } => typeDependOn(TypeA, TypeB) === true
87
+ //
88
+ // `checked` is used to prevent circular dependencies
89
+ #typeDependOn(typeA, typeB, checked = new Set()) {
90
+ if (checked.has(typeA))
91
+ return false;
75
92
  if (typeA === typeB)
76
93
  return true;
94
+ checked.add(typeA);
77
95
  const { types } = this.contractMetadata;
78
96
  const defA = (0, contracts_1.normalizeContractTypeDef)(types[typeA].type.def);
79
97
  const { type, value } = defA;
80
98
  // prettier-ignore
81
99
  switch (type) {
82
100
  case 'Struct':
83
- return value.fields.reduce((a, { typeId }) => a || this.#typeDependOn(typeId, typeB), false);
101
+ return value.fields.reduce((a, { typeId }) => a || this.#typeDependOn(typeId, typeB, checked), false);
84
102
  case 'Enum':
85
- return value.members.reduce((a, { fields }) => a || fields.reduce((a, { typeId }) => a || this.#typeDependOn(typeId, typeB), false), false);
103
+ return value.members.reduce((a, { fields }) => a || fields.reduce((a, { typeId }) => a || this.#typeDependOn(typeId, typeB, checked), false), false);
86
104
  case 'Tuple':
87
- return value.fields.reduce((a, typeId) => a || this.#typeDependOn(typeId, typeB), false);
105
+ return value.fields.reduce((a, typeId) => a || this.#typeDependOn(typeId, typeB, checked), false);
88
106
  case 'Sequence':
89
107
  case 'SizedVec':
90
- return this.#typeDependOn(value.typeParam, typeB);
91
- case 'Primitive':
92
108
  case 'Compact':
109
+ return this.#typeDependOn(value.typeParam, typeB, checked);
110
+ case 'Primitive':
93
111
  case 'BitSequence':
94
112
  return false;
95
113
  }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@dedot/codegen",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "description": "Generate types",
5
- "author": "Thang X. Vu <thang@coongcrafts.io>",
6
- "homepage": "https://github.com/dedotdev/dedot/tree/main/packages/codegen",
5
+ "author": "Thang X. Vu <thang@dedot.dev>",
6
+ "homepage": "https://dedot.dev",
7
7
  "repository": {
8
8
  "directory": "packages/codegen",
9
9
  "type": "git",
@@ -18,14 +18,14 @@
18
18
  "copy": "cp -R ./src/chaintypes/templates ./dist/chaintypes && cp -R ./src/chaintypes/templates ./dist/cjs/chaintypes && cp -R ./src/typink/templates ./dist/typink && cp -R ./src/typink/templates ./dist/cjs/typink"
19
19
  },
20
20
  "dependencies": {
21
- "@dedot/api": "0.9.0",
22
- "@dedot/codecs": "0.9.0",
23
- "@dedot/contracts": "0.9.0",
24
- "@dedot/providers": "0.9.0",
25
- "@dedot/runtime-specs": "0.9.0",
26
- "@dedot/shape": "0.9.0",
27
- "@dedot/types": "0.9.0",
28
- "@dedot/utils": "0.9.0",
21
+ "@dedot/api": "0.9.2",
22
+ "@dedot/codecs": "0.9.2",
23
+ "@dedot/contracts": "0.9.2",
24
+ "@dedot/providers": "0.9.2",
25
+ "@dedot/runtime-specs": "0.9.2",
26
+ "@dedot/shape": "0.9.2",
27
+ "@dedot/types": "0.9.2",
28
+ "@dedot/utils": "0.9.2",
29
29
  "handlebars": "^4.7.8",
30
30
  "prettier": "^3.4.2"
31
31
  },
@@ -37,7 +37,7 @@
37
37
  "node": ">=18"
38
38
  },
39
39
  "license": "Apache-2.0",
40
- "gitHead": "cebae55a3525e2c9bcdd70322027855a594a287d",
40
+ "gitHead": "b28ae48371785d44bf734cf45a5b8d0716d9bd70",
41
41
  "module": "./index.js",
42
42
  "types": "./index.d.ts",
43
43
  "exports": {
@@ -37,56 +37,74 @@ export class TypesGen extends BaseTypesGen {
37
37
  return false;
38
38
  }
39
39
  // Find out does type depends on known types
40
- // Example: `type TypeA = Struct { field: AccountId }` => includeKnownTypes(TypeA) === true
41
- #includeKnownTypes(typeId) {
40
+ //
41
+ // Example:
42
+ // type TypeA = Struct { field: AccountId } => includeKnownTypes(TypeA) === true
43
+ // type TypeA = Struct { field: TypeB }; type TypeB = Struct { field: AccountId, type: TypeA } => includeKnownTypes(TypeA) === true
44
+ //
45
+ // `checked` is used to prevent circular dependencies
46
+ #includeKnownTypes(typeId, checked = new Set()) {
47
+ // Return `false` because `typeId` is already checked
48
+ // If it were a known type, the first check would have returned `true`.
49
+ if (checked.has(typeId))
50
+ return false;
42
51
  if (this.knownTypes[typeId])
43
52
  return true;
53
+ checked.add(typeId);
44
54
  const { types } = this.contractMetadata;
45
55
  const defA = normalizeContractTypeDef(types[typeId].type.def);
46
56
  const { type, value } = defA;
47
57
  switch (type) {
48
58
  case 'Struct':
49
- return value.fields.some(({ typeId }) => this.#includeKnownTypes(typeId));
59
+ return value.fields.some(({ typeId }) => this.#includeKnownTypes(typeId, checked));
50
60
  case 'Enum':
51
- return value.members.some(({ fields }) => fields.some(({ typeId }) => this.#includeKnownTypes(typeId)));
61
+ return value.members.some(({ fields }) => fields.some(({ typeId }) => this.#includeKnownTypes(typeId, checked)));
52
62
  case 'Tuple':
53
- return value.fields.some((typeId) => this.#includeKnownTypes(typeId));
63
+ return value.fields.some((typeId) => this.#includeKnownTypes(typeId, checked));
54
64
  case 'Sequence':
55
65
  case 'SizedVec':
66
+ case 'Compact':
56
67
  const $innerType = this.types[value.typeParam].typeDef;
57
68
  if ($innerType.type === 'Primitive' && $innerType.value.kind === 'u8') {
58
69
  return true; // ByteLikes
59
70
  }
60
71
  else {
61
- return this.#includeKnownTypes(value.typeParam);
72
+ return this.#includeKnownTypes(value.typeParam, checked);
62
73
  }
63
74
  case 'Primitive':
64
- case 'Compact':
65
75
  case 'BitSequence':
66
76
  return false;
67
77
  }
68
78
  }
69
79
  // Find out does typeA depends on typeB
70
- // Example: `type TypeA = Struct { field: TypeB }` => typeDependOn(TypeA, TypeB) === true
71
- #typeDependOn(typeA, typeB) {
80
+ //
81
+ // Example:
82
+ // type TypeA = Struct { field: TypeB } => typeDependOn(TypeA, TypeB) === true
83
+ // type TypeA = Struct { field: TypeB }; type TypeB = Struct { field: TypeA } => typeDependOn(TypeA, TypeB) === true
84
+ //
85
+ // `checked` is used to prevent circular dependencies
86
+ #typeDependOn(typeA, typeB, checked = new Set()) {
87
+ if (checked.has(typeA))
88
+ return false;
72
89
  if (typeA === typeB)
73
90
  return true;
91
+ checked.add(typeA);
74
92
  const { types } = this.contractMetadata;
75
93
  const defA = normalizeContractTypeDef(types[typeA].type.def);
76
94
  const { type, value } = defA;
77
95
  // prettier-ignore
78
96
  switch (type) {
79
97
  case 'Struct':
80
- return value.fields.reduce((a, { typeId }) => a || this.#typeDependOn(typeId, typeB), false);
98
+ return value.fields.reduce((a, { typeId }) => a || this.#typeDependOn(typeId, typeB, checked), false);
81
99
  case 'Enum':
82
- return value.members.reduce((a, { fields }) => a || fields.reduce((a, { typeId }) => a || this.#typeDependOn(typeId, typeB), false), false);
100
+ return value.members.reduce((a, { fields }) => a || fields.reduce((a, { typeId }) => a || this.#typeDependOn(typeId, typeB, checked), false), false);
83
101
  case 'Tuple':
84
- return value.fields.reduce((a, typeId) => a || this.#typeDependOn(typeId, typeB), false);
102
+ return value.fields.reduce((a, typeId) => a || this.#typeDependOn(typeId, typeB, checked), false);
85
103
  case 'Sequence':
86
104
  case 'SizedVec':
87
- return this.#typeDependOn(value.typeParam, typeB);
88
- case 'Primitive':
89
105
  case 'Compact':
106
+ return this.#typeDependOn(value.typeParam, typeB, checked);
107
+ case 'Primitive':
90
108
  case 'BitSequence':
91
109
  return false;
92
110
  }