Skip to content
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

Add TypeScript typechecking to snaps-cli #2783

Merged
merged 13 commits into from
Oct 11, 2024
15 changes: 11 additions & 4 deletions .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ jobs:
test:
name: Test
runs-on: ubuntu-latest
needs: prepare
needs:
- prepare
- build
strategy:
fail-fast: false
matrix:
Expand All @@ -212,11 +214,16 @@ jobs:
packages/snaps-execution-environments/dist/browserify
key: snaps-execution-environments-build-${{ runner.os }}-${{ matrix.node-version }}-${{ github.sha }}
fail-on-cache-miss: true
- name: Restore build files
uses: actions/cache@v4
with:
path: |
packages/*/dist
key: build-source-${{ runner.os }}-${{ github.sha }}
fail-on-cache-miss: true
- run: yarn --immutable --immutable-cache
- name: Install Google Chrome
run: yarn install-chrome
- run: yarn workspace @metamask/snaps-sdk run build
if: ${{ matrix.package-name == '@metamask/snaps-cli' }}
- run: yarn workspace ${{ matrix.package-name }} run test
- name: Get coverage folder
id: get-coverage-folder
Expand Down Expand Up @@ -320,5 +327,5 @@ jobs:
node-version-file: '.nvmrc'
cache: yarn
- run: yarn --immutable
- run: yarn workspace @metamask/snaps-sdk run build
- run: yarn build:ci
- run: yarn workspace @metamask/snaps-cli run test
3 changes: 3 additions & 0 deletions packages/examples/packages/bip32/snap.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const config: SnapConfig = {
server: {
port: 8001,
},
typescript: {
enabled: true,
},
stats: {
buffer: false,
builtIns: {
Expand Down
7 changes: 4 additions & 3 deletions packages/examples/packages/bip32/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ import { getPrivateNode, getPublicKey } from './utils';
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
switch (request.method) {
case 'getPublicKey':
return await getPublicKey(
request.params as unknown as GetBip32PublicKeyParams,
);
return await getPublicKey(request.params as GetBip32PublicKeyParams);

case 'signMessage': {
const { message, curve, ...params } = request.params as SignMessageParams;

if (!message || typeof message !== 'string') {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new InvalidParamsError(`Invalid signature data: "${message}".`);
}

Expand Down Expand Up @@ -75,6 +74,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
});

if (!approved) {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new UserRejectedRequestError();
}

Expand All @@ -100,6 +100,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
}

default:
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new MethodNotFoundError({ method: request.method });
}
};
19 changes: 3 additions & 16 deletions packages/examples/packages/bip32/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@metamask/*": ["../../../*/src"]
}
"composite": false,
"baseUrl": "./"
},
"include": ["src", "snap.config.ts"],
"references": [
{
"path": "../../../snaps-sdk"
},
{
"path": "../../../snaps-jest"
},
{
"path": "../../../snaps-cli"
}
]
"include": ["src", "snap.config.ts"]
}
3 changes: 3 additions & 0 deletions packages/examples/packages/bip44/snap.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const config: SnapConfig = {
server: {
port: 8002,
},
typescript: {
enabled: true,
},
stats: {
buffer: false,
builtIns: {
Expand Down
2 changes: 2 additions & 0 deletions packages/examples/packages/bip44/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
});

if (!approved) {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new UserRejectedRequestError();
}

Expand All @@ -67,6 +68,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
}

default:
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new MethodNotFoundError({ method: request.method });
}
};
19 changes: 3 additions & 16 deletions packages/examples/packages/bip44/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@metamask/*": ["../../../*/src"]
}
"composite": false,
"baseUrl": "./"
},
"include": ["src", "snap.config.ts"],
"references": [
{
"path": "../../../snaps-sdk"
},
{
"path": "../../../snaps-jest"
},
{
"path": "../../../snaps-cli"
}
]
"include": ["src", "snap.config.ts"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
return 'Hello from Browserify!';

default: {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new MethodNotFoundError({ method: request.method });
}
}
Expand Down
17 changes: 2 additions & 15 deletions packages/examples/packages/browserify-plugin/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,7 @@
"compilerOptions": {
"resolveJsonModule": true,
"baseUrl": "./",
"paths": {
"@metamask/*": ["../../../*/src"]
}
"composite": false
},
"include": ["src", "scripts", "babel.config.json"],
"references": [
{
"path": "../../../snaps-sdk"
},
{
"path": "../../../snaps-browserify-plugin"
},
{
"path": "../../../snaps-jest"
}
]
"include": ["src", "scripts", "babel.config.json"]
}
1 change: 1 addition & 0 deletions packages/examples/packages/browserify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
return 'Hello from the MetaMask Snaps CLI using Browserify!';

default: {
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new MethodNotFoundError({ method: request.method });
}
}
Expand Down
18 changes: 2 additions & 16 deletions packages/examples/packages/browserify/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,7 @@
"compilerOptions": {
"resolveJsonModule": true,
"baseUrl": "./",
"paths": {
"@metamask/*/node": ["../../../*/src/node"],
"@metamask/*": ["../../../*/src"]
}
"composite": false
},
"include": ["src", "snap.config.ts"],
"references": [
{
"path": "../../../snaps-sdk"
},
{
"path": "../../../snaps-jest"
},
{
"path": "../../../snaps-cli"
}
]
"include": ["src", "snap.config.ts"]
}
3 changes: 3 additions & 0 deletions packages/examples/packages/client-status/snap.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const config: SnapConfig = {
server: {
port: 8027,
},
typescript: {
enabled: true,
},
stats: {
buffer: false,
},
Expand Down
1 change: 1 addition & 0 deletions packages/examples/packages/client-status/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
}

default:
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new MethodNotFoundError({ method: request.method });
}
};
19 changes: 3 additions & 16 deletions packages/examples/packages/client-status/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@metamask/*": ["../../../*/src"]
}
"composite": false,
"baseUrl": "./"
},
"include": ["src", "snap.config.ts"],
"references": [
{
"path": "../../../snaps-sdk"
},
{
"path": "../../../snaps-jest"
},
{
"path": "../../../snaps-cli"
}
]
"include": ["src", "snap.config.ts"]
}
3 changes: 3 additions & 0 deletions packages/examples/packages/cronjobs/snap.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const config: SnapConfig = {
server: {
port: 8004,
},
typescript: {
enabled: true,
},
stats: {
buffer: false,
},
Expand Down
3 changes: 2 additions & 1 deletion packages/examples/packages/cronjobs/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe('onCronjob', () => {
]),
);

await ui.ok();
// TODO(ritave): Fix types in SnapInterface
await (ui as any).ok();
Comment on lines +25 to +26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this happening now?

Copy link
Member Author

@ritave ritave Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was always happening, but jest doesn't check types. TSC run over test files and found those issues.


const response = await request;
expect(response).toRespondWith(null);
Expand Down
1 change: 1 addition & 0 deletions packages/examples/packages/cronjobs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const onCronjob: OnCronjobHandler = async ({ request }) => {
},
});
default:
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new MethodNotFoundError({ method: request.method });
}
};
19 changes: 3 additions & 16 deletions packages/examples/packages/cronjobs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@metamask/*": ["../../../*/src"]
}
"composite": false,
"baseUrl": "./"
},
"include": ["src", "snap.config.ts"],
"references": [
{
"path": "../../../snaps-sdk"
},
{
"path": "../../../snaps-jest"
},
{
"path": "../../../snaps-cli"
}
]
"include": ["src", "snap.config.ts"]
}
3 changes: 3 additions & 0 deletions packages/examples/packages/dialogs/snap.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const config: SnapConfig = {
server: {
port: 8005,
},
typescript: {
enabled: true,
},
stats: {
buffer: false,
},
Expand Down
1 change: 1 addition & 0 deletions packages/examples/packages/dialogs/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
});

default:
// eslint-disable-next-line @typescript-eslint/no-throw-literal
throw new MethodNotFoundError({ method: request.method });
}
};
Expand Down
19 changes: 2 additions & 17 deletions packages/examples/packages/dialogs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,7 @@
"compilerOptions": {
"baseUrl": "./",
"jsx": "react-jsxdev",
"paths": {
"@metamask/*/jsx": ["../../../*/src/jsx"],
"@metamask/*/jsx-dev-runtime": ["../../../*/src/jsx"],
"@metamask/*": ["../../../*/src"]
}
"composite": false
},
"include": ["src", "snap.config.ts"],
"references": [
{
"path": "../../../snaps-sdk"
},
{
"path": "../../../snaps-jest"
},
{
"path": "../../../snaps-cli"
}
]
"include": ["src", "snap.config.ts"]
}
3 changes: 3 additions & 0 deletions packages/examples/packages/errors/snap.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const config: SnapConfig = {
server: {
port: 8006,
},
typescript: {
enabled: true,
},
};

export default config;
19 changes: 3 additions & 16 deletions packages/examples/packages/errors/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@metamask/*": ["../../../*/src"]
}
"composite": false,
"baseUrl": "./"
},
"include": ["src", "snap.config.ts"],
"references": [
{
"path": "../../../snaps-sdk"
},
{
"path": "../../../snaps-jest"
},
{
"path": "../../../snaps-cli"
}
]
"include": ["src", "snap.config.ts"]
}
Loading
Loading