Skip to content

Commit

Permalink
Add linting for all dapps and sdk packages (MystenLabs#12507)
Browse files Browse the repository at this point in the history
## Description 

This adds eslint and prettier for all files in the `sdk` and `dapps`
directories.

It also standardizes the prettier configs to apply consistent formatting
across all directories.

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
hayes-mysten authored Jun 15, 2023
1 parent 0a8837a commit efc8301
Show file tree
Hide file tree
Showing 1,061 changed files with 60,790 additions and 67,794 deletions.
556 changes: 270 additions & 286 deletions .eslintrc.js

Large diffs are not rendered by default.

10 changes: 1 addition & 9 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@
!/.config
!/*.js

# Ignroe generated icons
/apps/icons/src

# Ignore packages that didn't use prettier yet (will be enabled in a future PR)
/sdk/bcs
/sdk/wallet-adapter
/dapps/offline-signer-helper
/dapps/sponsored-transactions

# ignore generated files
/apps/icons/src
npm-debug.log*
package-lock.json
yarn-debug.log*
Expand Down
90 changes: 45 additions & 45 deletions apps/core/package.json
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
{
"name": "@mysten/core",
"main": "src/index.ts",
"private": true,
"sideEffects": false,
"author": "Mysten Labs <build@mystenlabs.com>",
"repository": {
"type": "git",
"url": "github.com:MystenLabs/sui.git"
},
"license": "Apache-2.0",
"scripts": {
"prettier:check": "prettier -c --ignore-unknown .",
"prettier:fix": "prettier -w --ignore-unknown .",
"prettier:fix:watch": "onchange '**' -i -f add -f change -j 5 -- prettier -w --ignore-unknown {{file}}",
"eslint:check": "eslint --max-warnings=0 .",
"eslint:fix": "pnpm run eslint:check --fix",
"lint": "pnpm run eslint:check && pnpm run prettier:check",
"lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix",
"test": "vitest run",
"test:watch": "vitest"
},
"dependencies": {
"@amplitude/analytics-client-common": "^0.7.0",
"@amplitude/analytics-core": "^0.13.3",
"@amplitude/analytics-types": "^0.20.0",
"@growthbook/growthbook-react": "^0.15.0",
"@mysten/sui.js": "workspace:*",
"@sentry/react": "^7.47.0",
"@tanstack/react-query": "^4.29.3",
"bignumber.js": "^9.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"vanilla-cookieconsent": "^2.9.1"
},
"devDependencies": {
"@headlessui/tailwindcss": "^0.1.3",
"@tailwindcss/aspect-ratio": "^0.4.2",
"@tailwindcss/forms": "^0.5.3",
"@types/react": "^18.0.35",
"postcss": "^8.4.24",
"tailwindcss": "^3.3.1",
"typescript": "^5.0.4",
"vite": "^4.2.3",
"vitest": "^0.32.0"
}
"name": "@mysten/core",
"main": "src/index.ts",
"private": true,
"sideEffects": false,
"author": "Mysten Labs <build@mystenlabs.com>",
"repository": {
"type": "git",
"url": "github.com:MystenLabs/sui.git"
},
"license": "Apache-2.0",
"scripts": {
"prettier:check": "prettier -c --ignore-unknown .",
"prettier:fix": "prettier -w --ignore-unknown .",
"prettier:fix:watch": "onchange '**' -i -f add -f change -j 5 -- prettier -w --ignore-unknown {{file}}",
"eslint:check": "eslint --max-warnings=0 .",
"eslint:fix": "pnpm run eslint:check --fix",
"lint": "pnpm run eslint:check && pnpm run prettier:check",
"lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix",
"test": "vitest run",
"test:watch": "vitest"
},
"dependencies": {
"@amplitude/analytics-client-common": "^0.7.0",
"@amplitude/analytics-core": "^0.13.3",
"@amplitude/analytics-types": "^0.20.0",
"@growthbook/growthbook-react": "^0.15.0",
"@mysten/sui.js": "workspace:*",
"@sentry/react": "^7.47.0",
"@tanstack/react-query": "^4.29.3",
"bignumber.js": "^9.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"vanilla-cookieconsent": "^2.9.1"
},
"devDependencies": {
"@headlessui/tailwindcss": "^0.1.3",
"@tailwindcss/aspect-ratio": "^0.4.2",
"@tailwindcss/forms": "^0.5.3",
"@types/react": "^18.0.35",
"postcss": "^8.4.24",
"tailwindcss": "^3.3.1",
"typescript": "^5.0.4",
"vite": "^4.2.3",
"vitest": "^0.32.0"
}
}
14 changes: 6 additions & 8 deletions apps/core/src/api/RpcClientContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import { type JsonRpcProvider } from '@mysten/sui.js';
import { createContext, useContext } from 'react';

export const RpcClientContext = createContext<JsonRpcProvider | undefined>(
undefined
);
export const RpcClientContext = createContext<JsonRpcProvider | undefined>(undefined);

export function useRpcClient() {
const rpcClient = useContext(RpcClientContext);
if (!rpcClient) {
throw new Error('useRpcClient must be within RpcClientContext');
}
return rpcClient;
const rpcClient = useContext(RpcClientContext);
if (!rpcClient) {
throw new Error('useRpcClient must be within RpcClientContext');
}
return rpcClient;
}
66 changes: 30 additions & 36 deletions apps/core/src/api/SentryRpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,37 @@ import { JsonRpcClient } from '@mysten/sui.js';
import * as Sentry from '@sentry/react';

export class SentryRpcClient extends JsonRpcClient {
#url: string;
constructor(url: string) {
super(url);
this.#url = url;
}
#url: string;
constructor(url: string) {
super(url);
this.#url = url;
}

async #withRequest(
name: string,
data: Record<string, unknown>,
handler: () => Promise<unknown>
) {
const transaction = Sentry.startTransaction({
name,
op: 'http.rpc-request',
data: data,
tags: {
url: this.#url,
},
});
async #withRequest(name: string, data: Record<string, unknown>, handler: () => Promise<unknown>) {
const transaction = Sentry.startTransaction({
name,
op: 'http.rpc-request',
data: data,
tags: {
url: this.#url,
},
});

try {
const res = await handler();
const status: Sentry.SpanStatusType = 'ok';
transaction.setStatus(status);
return res;
} catch (e) {
const status: Sentry.SpanStatusType = 'internal_error';
transaction.setStatus(status);
throw e;
} finally {
transaction.finish();
}
}
try {
const res = await handler();
const status: Sentry.SpanStatusType = 'ok';
transaction.setStatus(status);
return res;
} catch (e) {
const status: Sentry.SpanStatusType = 'internal_error';
transaction.setStatus(status);
throw e;
} finally {
transaction.finish();
}
}

async request(method: string, args: any) {
return this.#withRequest(method, { args }, () =>
super.request(method, args)
);
}
async request(method: string, args: any) {
return this.#withRequest(method, { args }, () => super.request(method, args));
}
}
4 changes: 2 additions & 2 deletions apps/core/src/css.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

declare module '*.css' {
const css: string;
export default css;
const css: string;
export default css;
}
98 changes: 41 additions & 57 deletions apps/core/src/hooks/__tests__/useFormatCoin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,48 @@ import { formatBalance, CoinFormat } from '../useFormatCoin';
const SUI_DECIMALS = 9;

function toMist(sui: string) {
return new BigNumber(sui).shiftedBy(SUI_DECIMALS).toString();
return new BigNumber(sui).shiftedBy(SUI_DECIMALS).toString();
}

describe('formatBalance', () => {
it('formats zero amounts correctly', () => {
expect(formatBalance('0', 0)).toEqual('0');
expect(formatBalance('0', SUI_DECIMALS)).toEqual('0');
});

it('formats decimal amounts correctly', () => {
expect(formatBalance('0', SUI_DECIMALS)).toEqual('0');
expect(formatBalance('0.000', SUI_DECIMALS)).toEqual('0');
});

it('formats integer amounts correctly', () => {
expect(formatBalance(toMist('1'), SUI_DECIMALS)).toEqual('1');
expect(formatBalance(toMist('1.0001'), SUI_DECIMALS)).toEqual('1');
expect(formatBalance(toMist('1.1201'), SUI_DECIMALS)).toEqual('1.12');
expect(formatBalance(toMist('1.1234'), SUI_DECIMALS)).toEqual('1.12');
expect(formatBalance(toMist('1.1239'), SUI_DECIMALS)).toEqual('1.12');

expect(formatBalance(toMist('9999.9999'), SUI_DECIMALS)).toEqual(
'9,999.99'
);
// 10k + handling:
expect(formatBalance(toMist('10000'), SUI_DECIMALS)).toEqual('10 K');
expect(formatBalance(toMist('12345'), SUI_DECIMALS)).toEqual('12.34 K');
// Millions:
expect(formatBalance(toMist('1234000'), SUI_DECIMALS)).toEqual(
'1.23 M'
);
// Billions:
expect(formatBalance(toMist('1234000000'), SUI_DECIMALS)).toEqual(
'1.23 B'
);
});

it('formats integer amounts with full CoinFormat', () => {
expect(
formatBalance(toMist('1'), SUI_DECIMALS, CoinFormat.FULL)
).toEqual('1');
expect(
formatBalance(toMist('1.123456789'), SUI_DECIMALS, CoinFormat.FULL)
).toEqual('1.123456789');
expect(
formatBalance(toMist('9999.9999'), SUI_DECIMALS, CoinFormat.FULL)
).toEqual('9,999.9999');
expect(
formatBalance(toMist('10000'), SUI_DECIMALS, CoinFormat.FULL)
).toEqual('10,000');
expect(
formatBalance(toMist('12345'), SUI_DECIMALS, CoinFormat.FULL)
).toEqual('12,345');
expect(
formatBalance(toMist('1234000'), SUI_DECIMALS, CoinFormat.FULL)
).toEqual('1,234,000');
expect(
formatBalance(toMist('1234000000'), SUI_DECIMALS, CoinFormat.FULL)
).toEqual('1,234,000,000');
});
it('formats zero amounts correctly', () => {
expect(formatBalance('0', 0)).toEqual('0');
expect(formatBalance('0', SUI_DECIMALS)).toEqual('0');
});

it('formats decimal amounts correctly', () => {
expect(formatBalance('0', SUI_DECIMALS)).toEqual('0');
expect(formatBalance('0.000', SUI_DECIMALS)).toEqual('0');
});

it('formats integer amounts correctly', () => {
expect(formatBalance(toMist('1'), SUI_DECIMALS)).toEqual('1');
expect(formatBalance(toMist('1.0001'), SUI_DECIMALS)).toEqual('1');
expect(formatBalance(toMist('1.1201'), SUI_DECIMALS)).toEqual('1.12');
expect(formatBalance(toMist('1.1234'), SUI_DECIMALS)).toEqual('1.12');
expect(formatBalance(toMist('1.1239'), SUI_DECIMALS)).toEqual('1.12');

expect(formatBalance(toMist('9999.9999'), SUI_DECIMALS)).toEqual('9,999.99');
// 10k + handling:
expect(formatBalance(toMist('10000'), SUI_DECIMALS)).toEqual('10 K');
expect(formatBalance(toMist('12345'), SUI_DECIMALS)).toEqual('12.34 K');
// Millions:
expect(formatBalance(toMist('1234000'), SUI_DECIMALS)).toEqual('1.23 M');
// Billions:
expect(formatBalance(toMist('1234000000'), SUI_DECIMALS)).toEqual('1.23 B');
});

it('formats integer amounts with full CoinFormat', () => {
expect(formatBalance(toMist('1'), SUI_DECIMALS, CoinFormat.FULL)).toEqual('1');
expect(formatBalance(toMist('1.123456789'), SUI_DECIMALS, CoinFormat.FULL)).toEqual(
'1.123456789',
);
expect(formatBalance(toMist('9999.9999'), SUI_DECIMALS, CoinFormat.FULL)).toEqual('9,999.9999');
expect(formatBalance(toMist('10000'), SUI_DECIMALS, CoinFormat.FULL)).toEqual('10,000');
expect(formatBalance(toMist('12345'), SUI_DECIMALS, CoinFormat.FULL)).toEqual('12,345');
expect(formatBalance(toMist('1234000'), SUI_DECIMALS, CoinFormat.FULL)).toEqual('1,234,000');
expect(formatBalance(toMist('1234000000'), SUI_DECIMALS, CoinFormat.FULL)).toEqual(
'1,234,000,000',
);
});
});
Loading

0 comments on commit efc8301

Please sign in to comment.