wasm-ast-types 0.3.1 → 0.3.4

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.
@@ -7,7 +7,7 @@ var _typeof = require("@babel/runtime/helpers/typeof");
7
7
  Object.defineProperty(exports, "__esModule", {
8
8
  value: true
9
9
  });
10
- exports.getType = exports.getPropertyType = exports.forEmptyNameFix = exports.createTypedObjectParams = void 0;
10
+ exports.getType = exports.getPropertyType = exports.createTypedObjectParams = void 0;
11
11
 
12
12
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
13
 
@@ -23,15 +23,19 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
23
23
 
24
24
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
25
25
 
26
- var forEmptyNameFix = function forEmptyNameFix(name) {
27
- if (name.endsWith('For_Empty')) {
28
- return name.replace(/For_Empty$/, '_for_Empty');
29
- }
26
+ var getTypeStrFromRef = function getTypeStrFromRef($ref) {
27
+ switch ($ref) {
28
+ case '#/definitions/Binary':
29
+ return 'Binary';
30
30
 
31
- return name;
32
- };
31
+ default:
32
+ if ($ref !== null && $ref !== void 0 && $ref.startsWith('#/definitions/')) {
33
+ return $ref.replace('#/definitions/', '');
34
+ }
33
35
 
34
- exports.forEmptyNameFix = forEmptyNameFix;
36
+ throw new Error('what is $ref: ' + $ref);
37
+ }
38
+ };
35
39
 
36
40
  var getTypeFromRef = function getTypeFromRef($ref) {
37
41
  switch ($ref) {
@@ -182,7 +186,7 @@ var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
182
186
  var typedParams = keys.map(function (prop) {
183
187
  if (jsonschema.properties[prop].type === 'object') {
184
188
  if (jsonschema.properties[prop].title) {
185
- return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(jsonschema.properties[prop].title)))));
189
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
186
190
  } else {
187
191
  throw new Error('createTypedObjectParams() contact maintainer');
188
192
  }
@@ -194,36 +198,36 @@ var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
194
198
  var isOptional = !((_jsonschema$required = jsonschema.required) !== null && _jsonschema$required !== void 0 && _jsonschema$required.includes(prop));
195
199
  var unionTypes = jsonschema.properties[prop].allOf.map(function (el) {
196
200
  if (el.title) return el.title;
201
+ if (el.$ref) return getTypeStrFromRef(el.$ref);
197
202
  return el.type;
198
203
  });
199
204
  var uniqUnionTypes = (0, _toConsumableArray2["default"])(new Set(unionTypes));
200
205
 
201
206
  if (uniqUnionTypes.length === 1) {
202
- return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(uniqUnionTypes[0])))), isOptional);
207
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
203
208
  } else {
204
209
  return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(function (typ) {
205
- return t.tsTypeReference(t.identifier(forEmptyNameFix(typ)));
210
+ return t.tsTypeReference(t.identifier(typ));
206
211
  }))), isOptional);
207
212
  }
208
213
  } else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
209
214
  var _jsonschema$required2;
210
215
 
211
- var oneOf = JSON.stringify(jsonschema.properties[prop].oneOf, null, 2);
212
-
213
216
  var _isOptional = !((_jsonschema$required2 = jsonschema.required) !== null && _jsonschema$required2 !== void 0 && _jsonschema$required2.includes(prop));
214
217
 
215
218
  var _unionTypes = jsonschema.properties[prop].oneOf.map(function (el) {
216
219
  if (el.title) return el.title;
220
+ if (el.$ref) return getTypeStrFromRef(el.$ref);
217
221
  return el.type;
218
222
  });
219
223
 
220
224
  var _uniqUnionTypes = (0, _toConsumableArray2["default"])(new Set(_unionTypes));
221
225
 
222
226
  if (_uniqUnionTypes.length === 1) {
223
- return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(_uniqUnionTypes[0])))), _isOptional);
227
+ return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(_uniqUnionTypes[0]))), _isOptional);
224
228
  } else {
225
229
  return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(_uniqUnionTypes.map(function (typ) {
226
- return t.tsTypeReference(t.identifier(forEmptyNameFix(typ)));
230
+ return t.tsTypeReference(t.identifier(typ));
227
231
  }))), _isOptional);
228
232
  }
229
233
  }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _generator = _interopRequireDefault(require("@babel/generator"));
6
+
7
+ var _execute_msg = _interopRequireDefault(require("./../../../__fixtures__/daodao/cw-named-groups/execute_msg.json"));
8
+
9
+ var _wasm = require("./wasm");
10
+
11
+ var expectCode = function expectCode(ast) {
12
+ expect((0, _generator["default"])(ast).code).toMatchSnapshot();
13
+ };
14
+
15
+ var printCode = function printCode(ast) {
16
+ console.log((0, _generator["default"])(ast).code);
17
+ };
18
+
19
+ it('execute_msg', function () {
20
+ expectCode((0, _wasm.createTypeInterface)(_execute_msg["default"]));
21
+ });
22
+ it('query classes', function () {
23
+ expectCode((0, _wasm.createQueryClass)('SG721QueryClient', 'SG721ReadOnlyInstance', _execute_msg["default"]));
24
+ });
25
+ it('execute classes array types', function () {
26
+ expectCode((0, _wasm.createExecuteClass)('SG721Client', 'SG721Instance', null, _execute_msg["default"]));
27
+ });
28
+ it('execute interfaces no extends', function () {
29
+ expectCode((0, _wasm.createExecuteInterface)('SG721Instance', null, _execute_msg["default"]));
30
+ });
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _generator = _interopRequireDefault(require("@babel/generator"));
6
+
7
+ var _execute_msg = _interopRequireDefault(require("./../../../__fixtures__/daodao/cw-proposal-single/execute_msg.json"));
8
+
9
+ var _query_msg = _interopRequireDefault(require("./../../../__fixtures__/daodao/cw-proposal-single/query_msg.json"));
10
+
11
+ var _wasm = require("./wasm");
12
+
13
+ var expectCode = function expectCode(ast) {
14
+ expect((0, _generator["default"])(ast).code).toMatchSnapshot();
15
+ };
16
+
17
+ var printCode = function printCode(ast) {
18
+ console.log((0, _generator["default"])(ast).code);
19
+ };
20
+
21
+ it('execute_msg_for', function () {
22
+ expectCode((0, _wasm.createTypeInterface)(_execute_msg["default"]));
23
+ });
24
+ it('query classes', function () {
25
+ expectCode((0, _wasm.createQueryClass)('SG721QueryClient', 'SG721ReadOnlyInstance', _query_msg["default"]));
26
+ });
27
+ it('execute classes array types', function () {
28
+ expectCode((0, _wasm.createExecuteClass)('SG721Client', 'SG721Instance', null, _execute_msg["default"]));
29
+ });
30
+ it('execute interfaces no extends', function () {
31
+ expectCode((0, _wasm.createExecuteInterface)('SG721Instance', null, _execute_msg["default"]));
32
+ });
package/main/wasm.js CHANGED
@@ -163,7 +163,7 @@ var createTypeOrInterface = function createTypeOrInterface(Type, jsonschema) {
163
163
 
164
164
  if (jsonschema.type !== 'object') {
165
165
  if (!jsonschema.type) {
166
- return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, t.tsTypeReference(t.identifier((0, _types2.forEmptyNameFix)(jsonschema.title)))));
166
+ return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, t.tsTypeReference(t.identifier(jsonschema.title))));
167
167
  }
168
168
 
169
169
  return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, (0, _types2.getType)(jsonschema.type)));
@@ -1,12 +1,19 @@
1
1
  import * as t from '@babel/types';
2
2
  import { camel } from 'case';
3
3
  import { propertySignature } from './babel';
4
- export const forEmptyNameFix = name => {
5
- if (name.endsWith('For_Empty')) {
6
- return name.replace(/For_Empty$/, '_for_Empty');
7
- }
8
4
 
9
- return name;
5
+ const getTypeStrFromRef = $ref => {
6
+ switch ($ref) {
7
+ case '#/definitions/Binary':
8
+ return 'Binary';
9
+
10
+ default:
11
+ if ($ref?.startsWith('#/definitions/')) {
12
+ return $ref.replace('#/definitions/', '');
13
+ }
14
+
15
+ throw new Error('what is $ref: ' + $ref);
16
+ }
10
17
  };
11
18
 
12
19
  const getTypeFromRef = $ref => {
@@ -143,7 +150,7 @@ export const createTypedObjectParams = (jsonschema, camelize = true) => {
143
150
  const typedParams = keys.map(prop => {
144
151
  if (jsonschema.properties[prop].type === 'object') {
145
152
  if (jsonschema.properties[prop].title) {
146
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(jsonschema.properties[prop].title)))));
153
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
147
154
  } else {
148
155
  throw new Error('createTypedObjectParams() contact maintainer');
149
156
  }
@@ -153,28 +160,29 @@ export const createTypedObjectParams = (jsonschema, camelize = true) => {
153
160
  const isOptional = !jsonschema.required?.includes(prop);
154
161
  const unionTypes = jsonschema.properties[prop].allOf.map(el => {
155
162
  if (el.title) return el.title;
163
+ if (el.$ref) return getTypeStrFromRef(el.$ref);
156
164
  return el.type;
157
165
  });
158
166
  const uniqUnionTypes = [...new Set(unionTypes)];
159
167
 
160
168
  if (uniqUnionTypes.length === 1) {
161
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(uniqUnionTypes[0])))), isOptional);
169
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
162
170
  } else {
163
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(forEmptyNameFix(typ)))))), isOptional);
171
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
164
172
  }
165
173
  } else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
166
- const oneOf = JSON.stringify(jsonschema.properties[prop].oneOf, null, 2);
167
174
  const isOptional = !jsonschema.required?.includes(prop);
168
175
  const unionTypes = jsonschema.properties[prop].oneOf.map(el => {
169
176
  if (el.title) return el.title;
177
+ if (el.$ref) return getTypeStrFromRef(el.$ref);
170
178
  return el.type;
171
179
  });
172
180
  const uniqUnionTypes = [...new Set(unionTypes)];
173
181
 
174
182
  if (uniqUnionTypes.length === 1) {
175
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(forEmptyNameFix(uniqUnionTypes[0])))), isOptional);
183
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
176
184
  } else {
177
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(forEmptyNameFix(typ)))))), isOptional);
185
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
178
186
  }
179
187
  }
180
188
 
@@ -0,0 +1,24 @@
1
+ import generate from '@babel/generator';
2
+ import execute_msg from './../../../__fixtures__/daodao/cw-named-groups/execute_msg.json';
3
+ import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from './wasm';
4
+
5
+ const expectCode = ast => {
6
+ expect(generate(ast).code).toMatchSnapshot();
7
+ };
8
+
9
+ const printCode = ast => {
10
+ console.log(generate(ast).code);
11
+ };
12
+
13
+ it('execute_msg', () => {
14
+ expectCode(createTypeInterface(execute_msg));
15
+ });
16
+ it('query classes', () => {
17
+ expectCode(createQueryClass('SG721QueryClient', 'SG721ReadOnlyInstance', execute_msg));
18
+ });
19
+ it('execute classes array types', () => {
20
+ expectCode(createExecuteClass('SG721Client', 'SG721Instance', null, execute_msg));
21
+ });
22
+ it('execute interfaces no extends', () => {
23
+ expectCode(createExecuteInterface('SG721Instance', null, execute_msg));
24
+ });
@@ -0,0 +1,25 @@
1
+ import generate from '@babel/generator';
2
+ import execute_msg from './../../../__fixtures__/daodao/cw-proposal-single/execute_msg.json';
3
+ import query_msg from './../../../__fixtures__/daodao/cw-proposal-single/query_msg.json';
4
+ import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from './wasm';
5
+
6
+ const expectCode = ast => {
7
+ expect(generate(ast).code).toMatchSnapshot();
8
+ };
9
+
10
+ const printCode = ast => {
11
+ console.log(generate(ast).code);
12
+ };
13
+
14
+ it('execute_msg_for', () => {
15
+ expectCode(createTypeInterface(execute_msg));
16
+ });
17
+ it('query classes', () => {
18
+ expectCode(createQueryClass('SG721QueryClient', 'SG721ReadOnlyInstance', query_msg));
19
+ });
20
+ it('execute classes array types', () => {
21
+ expectCode(createExecuteClass('SG721Client', 'SG721Instance', null, execute_msg));
22
+ });
23
+ it('execute interfaces no extends', () => {
24
+ expectCode(createExecuteInterface('SG721Instance', null, execute_msg));
25
+ });
package/module/wasm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as t from '@babel/types';
2
2
  import { camel, pascal } from 'case';
3
3
  import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression, getMessageProperties } from './utils';
4
- import { getPropertyType, getType, createTypedObjectParams, forEmptyNameFix } from './utils/types';
4
+ import { getPropertyType, getType, createTypedObjectParams } from './utils/types';
5
5
  import { identifier, tsTypeOperator, propertySignature } from './utils/babel';
6
6
  export const createWasmQueryMethod = jsonschema => {
7
7
  const underscoreName = Object.keys(jsonschema.properties)[0];
@@ -103,7 +103,7 @@ export const createQueryInterface = (className, queryMsg) => {
103
103
  export const createTypeOrInterface = (Type, jsonschema) => {
104
104
  if (jsonschema.type !== 'object') {
105
105
  if (!jsonschema.type) {
106
- return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, t.tsTypeReference(t.identifier(forEmptyNameFix(jsonschema.title)))));
106
+ return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, t.tsTypeReference(t.identifier(jsonschema.title))));
107
107
  }
108
108
 
109
109
  return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, getType(jsonschema.type)));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.3.1",
3
+ "version": "0.3.4",
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",
@@ -42,48 +42,48 @@
42
42
  },
43
43
  "repository": {
44
44
  "type": "git",
45
- "url": "https://github.com/pyramation/cosmwasm-typescript-gen/tree/master/packages/wasm-ast-types"
45
+ "url": "https://github.com/pyramation/cosmwasm-typescript-gen"
46
46
  },
47
47
  "keywords": [],
48
48
  "bugs": {
49
- "url": "https://github.com/pyramation/cosmwasm-typescript-gen/tree/master/packages/wasm-ast-types/issues"
49
+ "url": "https://github.com/pyramation/cosmwasm-typescript-gen/issues"
50
50
  },
51
51
  "devDependencies": {
52
- "@babel/cli": "7.17.6",
53
- "@babel/core": "7.17.8",
54
- "@babel/eslint-parser": "^7.5.4",
55
- "@babel/generator": "7.17.7",
56
- "@babel/node": "^7.10.5",
57
- "@babel/parser": "^7.17.8",
58
- "@babel/plugin-proposal-class-properties": "7.16.7",
59
- "@babel/plugin-proposal-export-default-from": "7.16.7",
60
- "@babel/plugin-proposal-object-rest-spread": "7.17.3",
61
- "@babel/plugin-transform-runtime": "7.17.0",
62
- "@babel/preset-env": "7.16.11",
63
- "@babel/preset-typescript": "^7.16.7",
64
- "@babel/traverse": "7.17.3",
65
- "@types/jest": "^27.4.1",
52
+ "@babel/cli": "7.17.10",
53
+ "@babel/core": "7.18.5",
54
+ "@babel/eslint-parser": "^7.18.2",
55
+ "@babel/generator": "7.18.2",
56
+ "@babel/node": "^7.18.5",
57
+ "@babel/parser": "^7.18.5",
58
+ "@babel/plugin-proposal-class-properties": "7.17.12",
59
+ "@babel/plugin-proposal-export-default-from": "7.17.12",
60
+ "@babel/plugin-proposal-object-rest-spread": "7.18.0",
61
+ "@babel/plugin-transform-runtime": "7.18.5",
62
+ "@babel/preset-env": "7.18.2",
63
+ "@babel/preset-typescript": "^7.17.12",
64
+ "@babel/traverse": "7.18.5",
65
+ "@types/jest": "^28.1.2",
66
66
  "babel-core": "7.0.0-bridge.0",
67
- "babel-jest": "27.5.1",
67
+ "babel-jest": "28.1.1",
68
68
  "babel-watch": "^7.0.0",
69
69
  "cross-env": "^7.0.2",
70
- "eslint": "8.12.0",
70
+ "eslint": "8.17.0",
71
71
  "eslint-config-prettier": "^8.5.0",
72
72
  "eslint-plugin-prettier": "^4.0.0",
73
- "glob": "7.2.0",
74
- "jest": "^27.5.1",
73
+ "glob": "8.0.3",
74
+ "jest": "^28.1.1",
75
75
  "jest-in-case": "^1.0.2",
76
76
  "mkdirp": "1.0.4",
77
- "prettier": "^2.6.2",
77
+ "prettier": "^2.7.1",
78
78
  "regenerator-runtime": "^0.13.7",
79
- "ts-jest": "^27.1.4",
80
- "typescript": "^4.6.2"
79
+ "ts-jest": "^28.0.5",
80
+ "typescript": "^4.7.4"
81
81
  },
82
82
  "dependencies": {
83
- "@babel/runtime": "^7.11.2",
84
- "@babel/types": "7.17.0",
83
+ "@babel/runtime": "^7.18.3",
84
+ "@babel/types": "7.18.4",
85
85
  "ast-stringify": "0.1.0",
86
86
  "case": "1.6.3"
87
87
  },
88
- "gitHead": "aade6ffc08df51adc9922d4cd813fe8ef5bebfa1"
88
+ "gitHead": "5222ebac8404ddceecad0156fc4a95a9356682cd"
89
89
  }