Skip to content

adding parse #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ jobs:
yarn build
yarn symlink

- name: Test @hyperweb/build 🔍
- name: Test @hyperweb/build
run: cd packages/build && yarn test

- name: Test @hyperweb/parse
run: cd packages/parse && yarn test

- name: Test @hyperweb/ts-json-schema 🔍
- name: Test @hyperweb/ts-json-schema
run: cd packages/ts-json-schema && yarn test

41 changes: 41 additions & 0 deletions packages/parse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Hyperweb Parse

<p align="center" width="100%">
<img src="https://raw.githubusercontent.com/hyperweb-io/.github/refs/heads/main/assets/logo.svg" alt="hyperweb" width="80"><br />
</p>

<p align="center" width="100%">
<a href="https://github.com/hyperweb-io/hyperweb-build/actions/workflows/run-tests.yml">
<img height="20" src="https://github.com/hyperweb-io/hyperweb-build/actions/workflows/run-tests.yml/badge.svg" />
</a>
<br />
<a href="https://github.com/hyperweb-io/hyperweb-build/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"></a>
<a href="https://www.npmjs.com/package/@hyperweb/build"><img height="20" src="https://img.shields.io/github/package-json/v/hyperweb-io/hyperweb-build?filename=packages%2Fbuild%2Fpackage.json"></a>
</p>


## Interchain JavaScript Stack

A unified toolkit for building applications and smart contracts in the Interchain ecosystem ⚛️

| Category | Tools | Description |
|----------------------|------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|
| **Chain Information** | [**Chain Registry**](https://github.com/hyperweb-io/chain-registry), [**Utils**](https://www.npmjs.com/package/@chain-registry/utils), [**Client**](https://www.npmjs.com/package/@chain-registry/client) | Everything from token symbols, logos, and IBC denominations for all assets you want to support in your application. |
| **Wallet Connectors**| [**Interchain Kit**](https://github.com/hyperweb-io/interchain-kit)<sup>beta</sup>, [**Cosmos Kit**](https://github.com/hyperweb-io/cosmos-kit) | Experience the convenience of connecting with a variety of web3 wallets through a single, streamlined interface. |
| **Signing Clients** | [**InterchainJS**](https://github.com/hyperweb-io/interchainjs)<sup>beta</sup>, [**CosmJS**](https://github.com/cosmos/cosmjs) | A single, universal signing interface for any network |
| **SDK Clients** | [**Telescope**](https://github.com/hyperweb-io/telescope) | Your Frontend Companion for Building with TypeScript with Cosmos SDK Modules. |
| **Starter Kits** | [**Create Interchain App**](https://github.com/hyperweb-io/create-interchain-app)<sup>beta</sup>, [**Create Cosmos App**](https://github.com/hyperweb-io/create-cosmos-app) | Set up a modern Interchain app by running one command. |
| **UI Kits** | [**Interchain UI**](https://github.com/hyperweb-io/interchain-ui) | The Interchain Design System, empowering developers with a flexible, easy-to-use UI kit. |
| **Testing Frameworks** | [**Starship**](https://github.com/hyperweb-io/starship) | Unified Testing and Development for the Interchain. |
| **TypeScript Smart Contracts** | [**Create Hyperweb App**](https://github.com/hyperweb-io/create-hyperweb-app) | Build and deploy full-stack blockchain applications with TypeScript |
| **CosmWasm Contracts** | [**CosmWasm TS Codegen**](https://github.com/CosmWasm/ts-codegen) | Convert your CosmWasm smart contracts into dev-friendly TypeScript classes. |

## Credits

🛠 Built by Hyperweb (formerly Cosmology) — if you like our tools, please checkout and contribute to [our github ⚛️](https://github.com/hyperweb-io)

## Disclaimer

AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED “AS IS”, AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.

No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
150 changes: 150 additions & 0 deletions packages/parse/__tests__/ContractAnalyzer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { ContractAnalyzer } from '../src/ContractAnalyzer';

describe('ContractAnalyzer', () => {
let analyzer: ContractAnalyzer;

beforeEach(() => {
analyzer = new ContractAnalyzer();
});

it('should identify query methods', () => {
const code = `
export default class Contract {
state: any;

getState() {
return this.state;
}

getValue() {
const value = this.state.value;
return value;
}
}
`;

const result = analyzer.analyzeFromCode(code);
expect(result.queries).toEqual(['getState', 'getValue']);
expect(result.mutations).toEqual([]);
});

it('should identify mutation methods', () => {
const code = `
export default class Contract {
state: any;

setState(newState: any) {
this.state = newState;
}

updateValue(value: any) {
this.state.value = value;
}
}
`;

const result = analyzer.analyzeFromCode(code);
expect(result.queries).toEqual([]);
expect(result.mutations).toEqual(['setState', 'updateValue']);
});

it('should identify both query and mutation methods', () => {
const code = `
export default class Contract {
state: any;

getState() {
return this.state;
}

setState(newState: any) {
this.state = newState;
}

updateAndGet() {
this.state.value = 42;
return this.state.value;
}
}
`;

const result = analyzer.analyzeFromCode(code);
expect(result.queries).toEqual(['getState']);
expect(result.mutations).toEqual(['setState', 'updateAndGet']);
});

it('should ignore static methods and constructors', () => {
const code = `
export default class Contract {
state: any;

constructor() {
this.state = {};
}

static getStaticState() {
return this.state;
}

getState() {
return this.state;
}
}
`;

const result = analyzer.analyzeFromCode(code);
expect(result.queries).toEqual(['getState']);
expect(result.mutations).toEqual([]);
});

it('should handle class expression with named export', () => {
const code = `
var Contract = class {
state = {};

initialize() {
this.state.value = 2.02234;
}

getState() {
return this.state.value;
}

exp() {
this.state.value = this.state.value * this.state.value;
return this.state.value;
}
};

export { Contract as default };
`;

const result = analyzer.analyzeFromCode(code);
expect(result.queries).toEqual(['getState']);
expect(result.mutations).toEqual(['initialize', 'exp']);
});

it('should throw error when no default export is found', () => {
const code = `
class Contract {
state: any;

getState() {
return this.state;
}
}
`;

expect(() => analyzer.analyzeFromCode(code)).toThrow('No default exported class found in the code');
});

it('should throw error when default export is not a class', () => {
const code = `
export default function Contract() {
return {};
}
`;

expect(() => analyzer.analyzeFromCode(code)).toThrow('No default exported class found in the code');
});
});
18 changes: 18 additions & 0 deletions packages/parse/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
babelConfig: false,
tsconfig: 'tsconfig.json',
},
],
},
transformIgnorePatterns: [`/node_modules/*`],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
modulePathIgnorePatterns: ['dist/*']
};
38 changes: 38 additions & 0 deletions packages/parse/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@hyperweb/parse",
"version": "1.0.1",
"author": "Hyperweb <[email protected]>",
"description": "Parse Hyperweb contracts",
"main": "index.js",
"module": "esm/index.js",
"types": "index.d.ts",
"homepage": "https://github.com/hyperweb-io/hyperweb-build",
"license": "SEE LICENSE IN LICENSE",
"publishConfig": {
"access": "public",
"directory": "dist"
},
"repository": {
"type": "git",
"url": "https://github.com/hyperweb-io/hyperweb-build"
},
"bugs": {
"url": "https://github.com/hyperweb-io/hyperweb-build/issues"
},
"scripts": {
"copy": "copyfiles -f ../../LICENSE README.md package.json dist",
"clean": "rimraf dist/**",
"prepare": "npm run build",
"build": "npm run clean; tsc; tsc -p tsconfig.esm.json; npm run copy",
"build:dev": "npm run clean; tsc --declarationMap; tsc -p tsconfig.esm.json; npm run copy",
"lint": "eslint . --fix",
"test": "jest",
"test:watch": "jest --watch"
},
"keywords": [],
"dependencies": {
"@babel/parser": "^7.24.0",
"@babel/traverse": "^7.24.0",
"@babel/types": "^7.24.0"
}
}
Loading