wasm-ast-types 0.10.0 → 0.11.1

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.
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _accountNft = _interopRequireDefault(require("../../../../../__fixtures__/idl-version/accounts-nft/account-nft.json"));
6
+
7
+ var _client = require("../client");
8
+
9
+ var _testUtils = require("../../../test-utils");
10
+
11
+ var message = _accountNft["default"].query;
12
+ var ctx = (0, _testUtils.makeContext)(message);
13
+ it('execute_msg_for__empty', function () {
14
+ (0, _testUtils.expectCode)((0, _client.createTypeInterface)(ctx, message));
15
+ });
16
+ it('query classes', function () {
17
+ (0, _testUtils.expectCode)((0, _client.createQueryClass)(ctx, 'SG721QueryClient', 'SG721ReadOnlyInstance', message));
18
+ });
19
+ it('execute classes array types', function () {
20
+ (0, _testUtils.expectCode)((0, _client.createExecuteClass)(ctx, 'SG721Client', 'SG721Instance', null, message));
21
+ });
22
+ it('execute interfaces no extends', function () {
23
+ (0, _testUtils.expectCode)((0, _client.createExecuteInterface)(ctx, 'SG721Instance', null, message));
24
+ });
@@ -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,15 @@ var getTypeOrRef = function getTypeOrRef(obj) {
70
70
  };
71
71
 
72
72
  var getArrayTypeFromItems = function getArrayTypeFromItems(items) {
73
- if (items.type === 'array') {
73
+ // passing in [{"type":"string"}]
74
+ if (Array.isArray(items)) {
75
+ return t.tsArrayType(t.tsArrayType(getTypeOrRef(items[0])));
76
+ } // passing in {"items": [{"type":"string"}]}
77
+
78
+
79
+ var detect = detectType(items.type);
80
+
81
+ if (detect.type === 'array') {
74
82
  if (Array.isArray(items.items)) {
75
83
  return t.tsArrayType(t.tsArrayType(getTypeOrRef(items.items[0])));
76
84
  } else {
@@ -78,42 +86,43 @@ var getArrayTypeFromItems = function getArrayTypeFromItems(items) {
78
86
  }
79
87
  }
80
88
 
81
- return t.tsArrayType(getType(items.type));
89
+ return t.tsArrayType(getType(detect.type));
82
90
  };
83
91
 
84
- var getType = function getType(type) {
85
- switch (type) {
86
- case 'string':
87
- return t.tsStringKeyword();
88
-
89
- case 'boolean':
90
- return t.tSBooleanKeyword();
92
+ var detectType = function detectType(type) {
93
+ var optional = false;
94
+ var theType = '';
91
95
 
92
- case 'integer':
93
- return t.tsNumberKeyword();
96
+ if (Array.isArray(type)) {
97
+ if (type.length !== 2) {
98
+ throw new Error('[getType(array length)] case not handled by transpiler. contact maintainers.');
99
+ }
94
100
 
95
- default:
96
- throw new Error('contact maintainers [unknown type]: ' + type);
97
- }
98
- };
101
+ var _type = (0, _slicedToArray2["default"])(type, 2),
102
+ nullableType = _type[0],
103
+ nullType = _type[1];
99
104
 
100
- exports.getType = getType;
105
+ if (nullType !== 'null') {
106
+ throw new Error('[getType(null)] case not handled by transpiler. contact maintainers.');
107
+ }
101
108
 
102
- var getPropertyType = function getPropertyType(context, schema, prop) {
103
- var _schema$properties, _schema$required, _schema$required2;
109
+ theType = nullableType;
110
+ optional = true;
111
+ } else {
112
+ theType = type;
113
+ }
104
114
 
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));
115
+ return {
116
+ type: theType,
117
+ optional: optional
118
+ };
119
+ };
109
120
 
110
- if (info.allOf && info.allOf.length === 1) {
111
- info = info.allOf[0];
112
- }
121
+ exports.detectType = detectType;
113
122
 
114
- if (typeof info.$ref === 'string') {
115
- type = getTypeFromRef(info.$ref);
116
- }
123
+ var getTypeInfo = function getTypeInfo(info) {
124
+ var type = undefined;
125
+ var optional = undefined;
117
126
 
118
127
  if (Array.isArray(info.anyOf)) {
119
128
  // assuming 2nd is null, but let's check to ensure
@@ -158,7 +167,9 @@ var getPropertyType = function getPropertyType(context, schema, prop) {
158
167
  throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
159
168
  }
160
169
  } else {
161
- type = getType(info.type);
170
+ var detect = detectType(info.type);
171
+ type = getType(detect.type);
172
+ optional = detect.optional;
162
173
  }
163
174
  }
164
175
 
@@ -177,14 +188,83 @@ var getPropertyType = function getPropertyType(context, schema, prop) {
177
188
  }
178
189
 
179
190
  if (_nullableType === 'array' && (0, _typeof2["default"])(info.items) === 'object' && !Array.isArray(info.items)) {
180
- type = t.tsArrayType(getType(info.items.type));
191
+ var _detect = detectType(info.items.type);
192
+
193
+ if (_detect.type === 'array') {
194
+ // wen recursion?
195
+ type = t.tsArrayType(getArrayTypeFromItems(info.items));
196
+ } else {
197
+ type = t.tsArrayType(getType(_detect.type));
198
+ }
199
+
200
+ optional = _detect.optional;
181
201
  } else {
182
- type = getType(_nullableType);
202
+ var _detect2 = detectType(_nullableType);
203
+
204
+ optional = _detect2.optional;
205
+
206
+ if (_detect2.type === 'array') {
207
+ type = getArrayTypeFromItems(info.items);
208
+ } else {
209
+ type = getType(_detect2.type);
210
+ }
183
211
  }
184
212
 
185
213
  optional = true;
186
214
  }
187
215
 
216
+ return {
217
+ type: type,
218
+ optional: optional
219
+ };
220
+ };
221
+
222
+ exports.getTypeInfo = getTypeInfo;
223
+
224
+ var getType = function getType(type) {
225
+ switch (type) {
226
+ case 'string':
227
+ return t.tsStringKeyword();
228
+
229
+ case 'boolean':
230
+ return t.tSBooleanKeyword();
231
+
232
+ case 'integer':
233
+ return t.tsNumberKeyword();
234
+
235
+ default:
236
+ throw new Error('contact maintainers [unknown type]: ' + type);
237
+ }
238
+ };
239
+
240
+ exports.getType = getType;
241
+
242
+ var getPropertyType = function getPropertyType(context, schema, prop) {
243
+ var _schema$properties, _schema$required, _schema$required2;
244
+
245
+ var props = (_schema$properties = schema.properties) !== null && _schema$properties !== void 0 ? _schema$properties : {};
246
+ var info = props[prop];
247
+ var type = null;
248
+ var optional = !((_schema$required = schema.required) !== null && _schema$required !== void 0 && _schema$required.includes(prop));
249
+
250
+ if (info.allOf && info.allOf.length === 1) {
251
+ info = info.allOf[0];
252
+ }
253
+
254
+ if (typeof info.$ref === 'string') {
255
+ type = getTypeFromRef(info.$ref);
256
+ }
257
+
258
+ var typeInfo = getTypeInfo(info);
259
+
260
+ if (typeof typeInfo.optional !== 'undefined') {
261
+ optional = typeInfo.optional;
262
+ }
263
+
264
+ if (typeof typeInfo.type !== 'undefined') {
265
+ type = typeInfo.type;
266
+ }
267
+
188
268
  if (!type) {
189
269
  throw new Error('cannot find type for ' + JSON.stringify(info));
190
270
  }
@@ -256,7 +336,7 @@ function getPropertySignatureFromProp(context, jsonschema, prop, camelize) {
256
336
  getPropertyType(context, jsonschema, prop);
257
337
  } catch (e) {
258
338
  console.log(e);
259
- console.log(jsonschema, prop);
339
+ console.log(JSON.stringify(jsonschema, null, 2), prop);
260
340
  }
261
341
 
262
342
  var _getPropertyType = getPropertyType(context, jsonschema, prop),
@@ -0,0 +1,17 @@
1
+ import contract from '../../../../../__fixtures__/idl-version/accounts-nft/account-nft.json';
2
+ import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from '../client';
3
+ import { expectCode, makeContext } from '../../../test-utils';
4
+ const message = contract.query;
5
+ const ctx = makeContext(message);
6
+ it('execute_msg_for__empty', () => {
7
+ expectCode(createTypeInterface(ctx, message));
8
+ });
9
+ it('query classes', () => {
10
+ expectCode(createQueryClass(ctx, 'SG721QueryClient', 'SG721ReadOnlyInstance', message));
11
+ });
12
+ it('execute classes array types', () => {
13
+ expectCode(createExecuteClass(ctx, 'SG721Client', 'SG721Instance', null, message));
14
+ });
15
+ it('execute interfaces no extends', () => {
16
+ expectCode(createExecuteInterface(ctx, 'SG721Instance', null, message));
17
+ });
@@ -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,15 @@ const getTypeOrRef = obj => {
37
37
  };
38
38
 
39
39
  const getArrayTypeFromItems = items => {
40
- if (items.type === 'array') {
40
+ // passing in [{"type":"string"}]
41
+ if (Array.isArray(items)) {
42
+ return t.tsArrayType(t.tsArrayType(getTypeOrRef(items[0])));
43
+ } // passing in {"items": [{"type":"string"}]}
44
+
45
+
46
+ const detect = detectType(items.type);
47
+
48
+ if (detect.type === 'array') {
41
49
  if (Array.isArray(items.items)) {
42
50
  return t.tsArrayType(t.tsArrayType(getTypeOrRef(items.items[0])));
43
51
  } else {
@@ -45,37 +53,38 @@ const getArrayTypeFromItems = items => {
45
53
  }
46
54
  }
47
55
 
48
- return t.tsArrayType(getType(items.type));
56
+ return t.tsArrayType(getType(detect.type));
49
57
  };
50
58
 
51
- export const getType = type => {
52
- switch (type) {
53
- case 'string':
54
- return t.tsStringKeyword();
59
+ export const detectType = type => {
60
+ let optional = false;
61
+ let theType = '';
55
62
 
56
- case 'boolean':
57
- return t.tSBooleanKeyword();
63
+ if (Array.isArray(type)) {
64
+ if (type.length !== 2) {
65
+ throw new Error('[getType(array length)] case not handled by transpiler. contact maintainers.');
66
+ }
58
67
 
59
- case 'integer':
60
- return t.tsNumberKeyword();
68
+ const [nullableType, nullType] = type;
61
69
 
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);
70
+ if (nullType !== 'null') {
71
+ throw new Error('[getType(null)] case not handled by transpiler. contact maintainers.');
72
+ }
71
73
 
72
- if (info.allOf && info.allOf.length === 1) {
73
- info = info.allOf[0];
74
+ theType = nullableType;
75
+ optional = true;
76
+ } else {
77
+ theType = type;
74
78
  }
75
79
 
76
- if (typeof info.$ref === 'string') {
77
- type = getTypeFromRef(info.$ref);
78
- }
80
+ return {
81
+ type: theType,
82
+ optional
83
+ };
84
+ };
85
+ export const getTypeInfo = info => {
86
+ let type = undefined;
87
+ let optional = undefined;
79
88
 
80
89
  if (Array.isArray(info.anyOf)) {
81
90
  // assuming 2nd is null, but let's check to ensure
@@ -118,7 +127,9 @@ export const getPropertyType = (context, schema, prop) => {
118
127
  throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
119
128
  }
120
129
  } else {
121
- type = getType(info.type);
130
+ const detect = detectType(info.type);
131
+ type = getType(detect.type);
132
+ optional = detect.optional;
122
133
  }
123
134
  }
124
135
 
@@ -135,14 +146,74 @@ export const getPropertyType = (context, schema, prop) => {
135
146
  }
136
147
 
137
148
  if (nullableType === 'array' && typeof info.items === 'object' && !Array.isArray(info.items)) {
138
- type = t.tsArrayType(getType(info.items.type));
149
+ const detect = detectType(info.items.type);
150
+
151
+ if (detect.type === 'array') {
152
+ // wen recursion?
153
+ type = t.tsArrayType(getArrayTypeFromItems(info.items));
154
+ } else {
155
+ type = t.tsArrayType(getType(detect.type));
156
+ }
157
+
158
+ optional = detect.optional;
139
159
  } else {
140
- type = getType(nullableType);
160
+ const detect = detectType(nullableType);
161
+ optional = detect.optional;
162
+
163
+ if (detect.type === 'array') {
164
+ type = getArrayTypeFromItems(info.items);
165
+ } else {
166
+ type = getType(detect.type);
167
+ }
141
168
  }
142
169
 
143
170
  optional = true;
144
171
  }
145
172
 
173
+ return {
174
+ type,
175
+ optional
176
+ };
177
+ };
178
+ export const getType = type => {
179
+ switch (type) {
180
+ case 'string':
181
+ return t.tsStringKeyword();
182
+
183
+ case 'boolean':
184
+ return t.tSBooleanKeyword();
185
+
186
+ case 'integer':
187
+ return t.tsNumberKeyword();
188
+
189
+ default:
190
+ throw new Error('contact maintainers [unknown type]: ' + type);
191
+ }
192
+ };
193
+ export const getPropertyType = (context, schema, prop) => {
194
+ const props = schema.properties ?? {};
195
+ let info = props[prop];
196
+ let type = null;
197
+ let optional = !schema.required?.includes(prop);
198
+
199
+ if (info.allOf && info.allOf.length === 1) {
200
+ info = info.allOf[0];
201
+ }
202
+
203
+ if (typeof info.$ref === 'string') {
204
+ type = getTypeFromRef(info.$ref);
205
+ }
206
+
207
+ const typeInfo = getTypeInfo(info);
208
+
209
+ if (typeof typeInfo.optional !== 'undefined') {
210
+ optional = typeInfo.optional;
211
+ }
212
+
213
+ if (typeof typeInfo.type !== 'undefined') {
214
+ type = typeInfo.type;
215
+ }
216
+
146
217
  if (!type) {
147
218
  throw new Error('cannot find type for ' + JSON.stringify(info));
148
219
  }
@@ -201,7 +272,7 @@ export function getPropertySignatureFromProp(context, jsonschema, prop, camelize
201
272
  getPropertyType(context, jsonschema, prop);
202
273
  } catch (e) {
203
274
  console.log(e);
204
- console.log(jsonschema, prop);
275
+ console.log(JSON.stringify(jsonschema, null, 2), prop);
205
276
  }
206
277
 
207
278
  const {
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.1",
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": "4d6287ab9d17ea8cef91b183c00e9bae6e8529e1"
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;