@cosmwasm/ts-codegen
Version:
@cosmwasm/ts-codegen converts your CosmWasm smart contracts into dev-friendly TypeScript classes so you can focus on shipping code.
44 lines (43 loc) • 1.46 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parser = void 0;
const parser_1 = require("@babel/parser");
const traverse_1 = __importDefault(require("@babel/traverse"));
const parser = (codes) => {
const hash = {};
codes.forEach(code => {
const plugins = [
'typescript',
];
const ast = (0, parser_1.parse)(code, {
sourceType: 'module',
plugins
});
const visitor = visitorFn({
addType(key, node) {
hash[key] = node;
}
});
(0, traverse_1.default)(ast, visitor);
});
return hash;
};
exports.parser = parser;
const visitorFn = (parser) => ({
TSTypeAliasDeclaration(path) {
parser.addType(path.node.id.name, path.parentPath.node);
// if (path.node.id.name.endsWith('For_Empty')) {
// const newName = path.node.id.name.replace(/For_Empty$/, '_for_Empty');
// path.parentPath.node.declaration.id.name = newName;
// parser.addType(newName, path.parentPath.node);
// } else {
// parser.addType(path.node.id.name, path.parentPath.node);
// }
},
TSInterfaceDeclaration(path) {
parser.addType(path.node.id.name, path.parentPath.node);
}
});
;