wasm-ast-types 0.10.0 → 0.11.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.
@@ -8,7 +8,13 @@ var _client = require("../client");
8
8
 
9
9
  var _testUtils = require("../../../test-utils");
10
10
 
11
+ var _utils = require("../../utils");
12
+
11
13
  var ctx = (0, _testUtils.makeContext)(_arrays["default"]);
14
+ it('getPropertyType', function () {
15
+ var ast = (0, _utils.getPropertyType)(ctx, _arrays["default"].oneOf[0].properties.update_edges, 'edges3');
16
+ (0, _testUtils.expectCode)(ast.type); // printCode(ast.type)
17
+ });
12
18
  it('execute_msg_for__empty', function () {
13
19
  (0, _testUtils.expectCode)((0, _client.createTypeInterface)(ctx, _arrays["default"]));
14
20
  });
@@ -7,11 +7,11 @@ var _typeof3 = require("@babel/runtime/helpers/typeof");
7
7
  Object.defineProperty(exports, "__esModule", {
8
8
  value: true
9
9
  });
10
- exports.getParamsTypeAnnotation = exports.createTypedObjectParams = void 0;
10
+ exports.getParamsTypeAnnotation = exports.detectType = exports.createTypedObjectParams = void 0;
11
11
  exports.getPropertySignatureFromProp = getPropertySignatureFromProp;
12
12
  exports.getPropertyType = void 0;
13
13
  exports.getResponseType = getResponseType;
14
- exports.getTypeFromRef = exports.getType = void 0;
14
+ exports.getTypeInfo = exports.getTypeFromRef = exports.getType = void 0;
15
15
 
16
16
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
17
17
 
@@ -70,7 +70,9 @@ var getTypeOrRef = function getTypeOrRef(obj) {
70
70
  };
71
71
 
72
72
  var getArrayTypeFromItems = function getArrayTypeFromItems(items) {
73
- if (items.type === 'array') {
73
+ var detect = detectType(items.type);
74
+
75
+ if (detect.type === 'array') {
74
76
  if (Array.isArray(items.items)) {
75
77
  return t.tsArrayType(t.tsArrayType(getTypeOrRef(items.items[0])));
76
78
  } else {
@@ -78,42 +80,43 @@ var getArrayTypeFromItems = function getArrayTypeFromItems(items) {
78
80
  }
79
81
  }
80
82
 
81
- return t.tsArrayType(getType(items.type));
83
+ return t.tsArrayType(getType(detect.type));
82
84
  };
83
85
 
84
- var getType = function getType(type) {
85
- switch (type) {
86
- case 'string':
87
- return t.tsStringKeyword();
86
+ var detectType = function detectType(type) {
87
+ var optional = false;
88
+ var theType = '';
88
89
 
89
- case 'boolean':
90
- return t.tSBooleanKeyword();
91
-
92
- case 'integer':
93
- return t.tsNumberKeyword();
90
+ if (Array.isArray(type)) {
91
+ if (type.length !== 2) {
92
+ throw new Error('[getType(array length)] case not handled by transpiler. contact maintainers.');
93
+ }
94
94
 
95
- default:
96
- throw new Error('contact maintainers [unknown type]: ' + type);
97
- }
98
- };
95
+ var _type = (0, _slicedToArray2["default"])(type, 2),
96
+ nullableType = _type[0],
97
+ nullType = _type[1];
99
98
 
100
- exports.getType = getType;
99
+ if (nullType !== 'null') {
100
+ throw new Error('[getType(null)] case not handled by transpiler. contact maintainers.');
101
+ }
101
102
 
102
- var getPropertyType = function getPropertyType(context, schema, prop) {
103
- var _schema$properties, _schema$required, _schema$required2;
103
+ theType = nullableType;
104
+ optional = true;
105
+ } else {
106
+ theType = type;
107
+ }
104
108
 
105
- var props = (_schema$properties = schema.properties) !== null && _schema$properties !== void 0 ? _schema$properties : {};
106
- var info = props[prop];
107
- var type = null;
108
- var optional = !((_schema$required = schema.required) !== null && _schema$required !== void 0 && _schema$required.includes(prop));
109
+ return {
110
+ type: theType,
111
+ optional: optional
112
+ };
113
+ };
109
114
 
110
- if (info.allOf && info.allOf.length === 1) {
111
- info = info.allOf[0];
112
- }
115
+ exports.detectType = detectType;
113
116
 
114
- if (typeof info.$ref === 'string') {
115
- type = getTypeFromRef(info.$ref);
116
- }
117
+ var getTypeInfo = function getTypeInfo(info) {
118
+ var type = undefined;
119
+ var optional = undefined;
117
120
 
118
121
  if (Array.isArray(info.anyOf)) {
119
122
  // assuming 2nd is null, but let's check to ensure
@@ -158,7 +161,9 @@ var getPropertyType = function getPropertyType(context, schema, prop) {
158
161
  throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
159
162
  }
160
163
  } else {
161
- type = getType(info.type);
164
+ var detect = detectType(info.type);
165
+ type = getType(detect.type);
166
+ optional = detect.optional;
162
167
  }
163
168
  }
164
169
 
@@ -177,7 +182,16 @@ var getPropertyType = function getPropertyType(context, schema, prop) {
177
182
  }
178
183
 
179
184
  if (_nullableType === 'array' && (0, _typeof2["default"])(info.items) === 'object' && !Array.isArray(info.items)) {
180
- type = t.tsArrayType(getType(info.items.type));
185
+ var _detect = detectType(info.items.type);
186
+
187
+ if (_detect.type === 'array') {
188
+ // wen recursion?
189
+ type = t.tsArrayType(getArrayTypeFromItems(info.items));
190
+ } else {
191
+ type = t.tsArrayType(getType(_detect.type));
192
+ }
193
+
194
+ optional = _detect.optional;
181
195
  } else {
182
196
  type = getType(_nullableType);
183
197
  }
@@ -185,6 +199,58 @@ var getPropertyType = function getPropertyType(context, schema, prop) {
185
199
  optional = true;
186
200
  }
187
201
 
202
+ return {
203
+ type: type,
204
+ optional: optional
205
+ };
206
+ };
207
+
208
+ exports.getTypeInfo = getTypeInfo;
209
+
210
+ var getType = function getType(type) {
211
+ switch (type) {
212
+ case 'string':
213
+ return t.tsStringKeyword();
214
+
215
+ case 'boolean':
216
+ return t.tSBooleanKeyword();
217
+
218
+ case 'integer':
219
+ return t.tsNumberKeyword();
220
+
221
+ default:
222
+ throw new Error('contact maintainers [unknown type]: ' + type);
223
+ }
224
+ };
225
+
226
+ exports.getType = getType;
227
+
228
+ var getPropertyType = function getPropertyType(context, schema, prop) {
229
+ var _schema$properties, _schema$required, _schema$required2;
230
+
231
+ var props = (_schema$properties = schema.properties) !== null && _schema$properties !== void 0 ? _schema$properties : {};
232
+ var info = props[prop];
233
+ var type = null;
234
+ var optional = !((_schema$required = schema.required) !== null && _schema$required !== void 0 && _schema$required.includes(prop));
235
+
236
+ if (info.allOf && info.allOf.length === 1) {
237
+ info = info.allOf[0];
238
+ }
239
+
240
+ if (typeof info.$ref === 'string') {
241
+ type = getTypeFromRef(info.$ref);
242
+ }
243
+
244
+ var typeInfo = getTypeInfo(info);
245
+
246
+ if (typeof typeInfo.optional !== 'undefined') {
247
+ optional = typeInfo.optional;
248
+ }
249
+
250
+ if (typeof typeInfo.type !== 'undefined') {
251
+ type = typeInfo.type;
252
+ }
253
+
188
254
  if (!type) {
189
255
  throw new Error('cannot find type for ' + JSON.stringify(info));
190
256
  }
@@ -1,7 +1,12 @@
1
1
  import message from '../../../../../__fixtures__/misc/schema/arrays.json';
2
2
  import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from '../client';
3
3
  import { expectCode, makeContext } from '../../../test-utils';
4
+ import { getPropertyType } from '../../utils';
4
5
  const ctx = makeContext(message);
6
+ it('getPropertyType', () => {
7
+ const ast = getPropertyType(ctx, message.oneOf[0].properties.update_edges, 'edges3');
8
+ expectCode(ast.type); // printCode(ast.type)
9
+ });
5
10
  it('execute_msg_for__empty', () => {
6
11
  expectCode(createTypeInterface(ctx, message));
7
12
  });
@@ -37,7 +37,9 @@ const getTypeOrRef = obj => {
37
37
  };
38
38
 
39
39
  const getArrayTypeFromItems = items => {
40
- if (items.type === 'array') {
40
+ const detect = detectType(items.type);
41
+
42
+ if (detect.type === 'array') {
41
43
  if (Array.isArray(items.items)) {
42
44
  return t.tsArrayType(t.tsArrayType(getTypeOrRef(items.items[0])));
43
45
  } else {
@@ -45,37 +47,38 @@ const getArrayTypeFromItems = items => {
45
47
  }
46
48
  }
47
49
 
48
- return t.tsArrayType(getType(items.type));
50
+ return t.tsArrayType(getType(detect.type));
49
51
  };
50
52
 
51
- export const getType = type => {
52
- switch (type) {
53
- case 'string':
54
- return t.tsStringKeyword();
53
+ export const detectType = type => {
54
+ let optional = false;
55
+ let theType = '';
55
56
 
56
- case 'boolean':
57
- return t.tSBooleanKeyword();
57
+ if (Array.isArray(type)) {
58
+ if (type.length !== 2) {
59
+ throw new Error('[getType(array length)] case not handled by transpiler. contact maintainers.');
60
+ }
58
61
 
59
- case 'integer':
60
- return t.tsNumberKeyword();
62
+ const [nullableType, nullType] = type;
61
63
 
62
- default:
63
- throw new Error('contact maintainers [unknown type]: ' + type);
64
- }
65
- };
66
- export const getPropertyType = (context, schema, prop) => {
67
- const props = schema.properties ?? {};
68
- let info = props[prop];
69
- let type = null;
70
- let optional = !schema.required?.includes(prop);
64
+ if (nullType !== 'null') {
65
+ throw new Error('[getType(null)] case not handled by transpiler. contact maintainers.');
66
+ }
71
67
 
72
- if (info.allOf && info.allOf.length === 1) {
73
- info = info.allOf[0];
68
+ theType = nullableType;
69
+ optional = true;
70
+ } else {
71
+ theType = type;
74
72
  }
75
73
 
76
- if (typeof info.$ref === 'string') {
77
- type = getTypeFromRef(info.$ref);
78
- }
74
+ return {
75
+ type: theType,
76
+ optional
77
+ };
78
+ };
79
+ export const getTypeInfo = info => {
80
+ let type = undefined;
81
+ let optional = undefined;
79
82
 
80
83
  if (Array.isArray(info.anyOf)) {
81
84
  // assuming 2nd is null, but let's check to ensure
@@ -118,7 +121,9 @@ export const getPropertyType = (context, schema, prop) => {
118
121
  throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
119
122
  }
120
123
  } else {
121
- type = getType(info.type);
124
+ const detect = detectType(info.type);
125
+ type = getType(detect.type);
126
+ optional = detect.optional;
122
127
  }
123
128
  }
124
129
 
@@ -135,7 +140,16 @@ export const getPropertyType = (context, schema, prop) => {
135
140
  }
136
141
 
137
142
  if (nullableType === 'array' && typeof info.items === 'object' && !Array.isArray(info.items)) {
138
- type = t.tsArrayType(getType(info.items.type));
143
+ const detect = detectType(info.items.type);
144
+
145
+ if (detect.type === 'array') {
146
+ // wen recursion?
147
+ type = t.tsArrayType(getArrayTypeFromItems(info.items));
148
+ } else {
149
+ type = t.tsArrayType(getType(detect.type));
150
+ }
151
+
152
+ optional = detect.optional;
139
153
  } else {
140
154
  type = getType(nullableType);
141
155
  }
@@ -143,6 +157,50 @@ export const getPropertyType = (context, schema, prop) => {
143
157
  optional = true;
144
158
  }
145
159
 
160
+ return {
161
+ type,
162
+ optional
163
+ };
164
+ };
165
+ export const getType = type => {
166
+ switch (type) {
167
+ case 'string':
168
+ return t.tsStringKeyword();
169
+
170
+ case 'boolean':
171
+ return t.tSBooleanKeyword();
172
+
173
+ case 'integer':
174
+ return t.tsNumberKeyword();
175
+
176
+ default:
177
+ throw new Error('contact maintainers [unknown type]: ' + type);
178
+ }
179
+ };
180
+ export const getPropertyType = (context, schema, prop) => {
181
+ const props = schema.properties ?? {};
182
+ let info = props[prop];
183
+ let type = null;
184
+ let optional = !schema.required?.includes(prop);
185
+
186
+ if (info.allOf && info.allOf.length === 1) {
187
+ info = info.allOf[0];
188
+ }
189
+
190
+ if (typeof info.$ref === 'string') {
191
+ type = getTypeFromRef(info.$ref);
192
+ }
193
+
194
+ const typeInfo = getTypeInfo(info);
195
+
196
+ if (typeof typeInfo.optional !== 'undefined') {
197
+ optional = typeInfo.optional;
198
+ }
199
+
200
+ if (typeof typeInfo.type !== 'undefined') {
201
+ type = typeInfo.type;
202
+ }
203
+
146
204
  if (!type) {
147
205
  throw new Error('cannot find type for ' + JSON.stringify(info));
148
206
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "CosmWasm TypeScript AST generation",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/pyramation/cosmwasm-typescript-gen/tree/master/packages/wasm-ast-types#readme",
@@ -86,5 +86,5 @@
86
86
  "case": "1.6.3",
87
87
  "deepmerge": "4.2.2"
88
88
  },
89
- "gitHead": "94c5785d947e329df1cc9108cc33bb7a0c21af60"
89
+ "gitHead": "d7f8c715f76edac8c5c2762e58fe8c73351d2af5"
90
90
  }
@@ -4,7 +4,15 @@ import { RenderContext } from '../context';
4
4
  import { JSONSchema } from '../types';
5
5
  export declare function getResponseType(context: RenderContext, underscoreName: string): string;
6
6
  export declare const getTypeFromRef: ($ref: any) => t.TSTypeReference;
7
- export declare const getType: (type: any) => t.TSBooleanKeyword | t.TSNumberKeyword | t.TSStringKeyword;
7
+ export declare const detectType: (type: string | string[]) => {
8
+ type: string;
9
+ optional: boolean;
10
+ };
11
+ export declare const getTypeInfo: (info: JSONSchema) => {
12
+ type: any;
13
+ optional: any;
14
+ };
15
+ export declare const getType: (type: string) => t.TSBooleanKeyword | t.TSNumberKeyword | t.TSStringKeyword;
8
16
  export declare const getPropertyType: (context: RenderContext, schema: JSONSchema, prop: string) => {
9
17
  type: any;
10
18
  optional: boolean;