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

chore: adding type checking #3046

Merged
merged 20 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions .changeset/odd-seahorses-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

chore: adding type checking
arboleya marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 3 additions & 2 deletions apps/docs-snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"description": "",
"private": true,
"scripts": {
"pretest": "pnpm build:forc",
"build:forc": "pnpm fuels build"
"pretest": "run-s build:forc type:check",
"build:forc": "pnpm fuels build",
"type:check": "tsc --noEmit"
nedsalk marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@fuel-ts/account": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions apps/docs-snippets/src/guide/types/arrays.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('Arrays Types', () => {
contracts: [contract],
} = launched;
try {
// @ts-expect-error Will throw error because the array length is not 2
// #region arrays-3
// will throw error because the array length is not 2
await contract.functions.echo_u64_array([10000000]).simulate();
Expand Down
16 changes: 11 additions & 5 deletions apps/docs-snippets/src/guide/types/contract-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { Address } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

import { InputOutputTypesFactory } from '../../../test/typegen';
import type {
IdentityOutput,
AddressOutput,
ContractIdOutput,
} from '../../../test/typegen/contracts/InputOutputTypes';

/**
* @group node
Expand Down Expand Up @@ -87,9 +92,9 @@ describe('Contract Types', () => {

// #region identity-address-output
// #import { Address };
const identityFromOutput1 = callResponse1.value;
const addressStringFromOutput = identityFromOutput1.Address.bits;
const addressFromOutput: Address = Address.fromB256(addressStringFromOutput);
const identityFromOutput1: IdentityOutput = callResponse1.value;
const addressStringFromOutput: AddressOutput = identityFromOutput1.Address as AddressOutput;
const addressFromOutput: Address = Address.fromB256(addressStringFromOutput.bits);
// #endregion identity-address-output

// #region identity-contract-input
Expand All @@ -100,8 +105,9 @@ describe('Contract Types', () => {
const callResponse2 = await contract.functions.identity(contractIdentityInput).simulate();

// #region identity-contract-output
const identityFromOutput2 = callResponse2.value;
const contractIdFromOutput: string = identityFromOutput2.ContractId.bits;
const identityFromOutput2: IdentityOutput = callResponse2.value;
const contractIdOutput: ContractIdOutput = identityFromOutput2.ContractId as ContractIdOutput;
const contractIdFromOutput: string = contractIdOutput.bits;
// #endregion identity-contract-output

expect(addressFromOutput).toEqual(address);
Expand Down
3 changes: 3 additions & 0 deletions apps/docs-snippets/src/guide/types/enums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('Enums Types', () => {
const emumValue: number = 1;

await expectToThrowFuelError(
// @ts-expect-error number is not a valid type
() => contract.functions.echo_state_error_enum(emumValue).simulate(),
new FuelError(FuelError.CODES.INVALID_DECODE_VALUE, 'A field for the case must be provided.')
);
Expand All @@ -122,6 +123,7 @@ describe('Enums Types', () => {
const emumValue = 'NotStateEnumValue';

await expectToThrowFuelError(
// @ts-expect-error NotStateEnumValue is not a valid value
() => contract.functions.echo_state_error_enum(emumValue).simulate(),
new FuelError(FuelError.CODES.INVALID_DECODE_VALUE, 'Only one field must be provided.')
);
Expand All @@ -146,6 +148,7 @@ describe('Enums Types', () => {
const enumParam = { UnknownKey: 'Completed' };

await expectToThrowFuelError(
// @ts-expect-error UnknownKey is not a valid key
() => contract.functions.echo_error_enum(enumParam).simulate(),
new FuelError(
FuelError.CODES.INVALID_DECODE_VALUE,
Expand Down
2 changes: 1 addition & 1 deletion apps/docs-snippets/src/guide/types/vector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Vector Types', () => {
// #import { arrayify };
// #context import { BytecodeInputFactory } from '../path/to/typegen';

const bytecodeAsVecU8 = arrayify(BytecodeInputFactory.bytecode);
const bytecodeAsVecU8 = Array.from(arrayify(BytecodeInputFactory.bytecode));
maschad marked this conversation as resolved.
Show resolved Hide resolved

const { waitForResult } = await bytecodeContract.functions
.compute_bytecode_root(bytecodeAsVecU8)
Expand Down
7 changes: 5 additions & 2 deletions apps/docs-snippets/src/guide/wallets/connectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ class WalletConnector extends FuelConnector {
// #region fuel-connector-events-abis
const abis: Array<FuelABI> = [
{
encoding: '1',
types: [],
encodingVersion: '1',
specVersion: '1',
programType: 'contract',
concreteTypes: [],
metadataTypes: [],
loggedTypes: [],
functions: [],
messagesTypes: [],
Expand Down
5 changes: 5 additions & 0 deletions apps/docs-snippets/src/jsonc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// informs TS about `.jsonc` file extension
declare module '*.jsonc' {
const value: string;
export default value;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
"test:browser:filter": "vitest --run --coverage false --config vitest.browser.config.mts",
"test:e2e": "vitest --run --config vitest.node.config.mts $(scripts/tests-find.sh --e2e)",
"test:integration": "vitest --run --config vitest.node.config.mts $(scripts/tests-find.sh --integration)",
"lint": "run-s lint:check prettier:check",
"lint": "run-s type:check-tests lint:check prettier:check",
"lint:check": "eslint . --ext .ts --max-warnings 0",
"lint:fix": "pnpm lint:check --fix",
"lint:md-links": "tsx ./scripts/lint-md-links",
"lint:package-jsons": "tsx ./scripts/lint-package-jsons",
"type:check-tests": "tsc -p tsconfig.test.json",
"prettier:check": "prettier --check packages --check apps/docs-snippets",
"prettier:format": "prettier --write packages --check apps/docs-snippets",
"verify:package-exports": "tsx ./scripts/verify-package-exports",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ describe('NumberCoder', () => {
const invalidNumber = 'u64';

await expectToThrowFuelError(
// @ts-expect-error Expected to throw error
() => new NumberCoder(invalidNumber),
new FuelError(ErrorCode.TYPE_NOT_SUPPORTED, `Invalid number type: ${invalidNumber}`)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { EncodingVersion } from '../../utils/constants';
import { ENCODING_V1 } from '../../utils/constants';

import { getCoderForEncoding } from './getCoderForEncoding';
Expand All @@ -17,6 +18,8 @@ describe('getEncodingStrategy', () => {
});

it('throws for an unsupported encoding version', () => {
expect(() => getCoderForEncoding('2')).toThrowError('Encoding version 2 is unsupported.');
expect(() => getCoderForEncoding('2' as EncodingVersion)).toThrowError(
'Encoding version 2 is unsupported.'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('parseTypeArguments.ts', () => {
/*
Test helpers
*/
function bundleTypes(rawTypes = defautRawTypes) {
function bundleTypes(rawTypes: readonly JsonAbiType[] = defautRawTypes) {
const types = rawTypes.map((rawAbiType) => makeType({ rawAbiType }));
return types;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/abi-typegen/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src", "test"]
"include": ["src", "test"],
"exclude": ["**/__temp__/**"]
}
2 changes: 1 addition & 1 deletion packages/contract/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src", "src/**/*.json"]
"include": ["src", "src/**/*.json", "test"]
}
4 changes: 3 additions & 1 deletion packages/create-fuels/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
},
"license": "Apache-2.0",
"scripts": {
"build": "tsup && tsc --noEmit",
"build": "run-s build:source type:check",
"build:source": "tsup",
"type:check": "tsc --noEmit",
"prepublishOnly": "tsx ./scripts/prepublish.ts"
},
"dependencies": {
Expand Down
5 changes: 3 additions & 2 deletions packages/fuel-gauge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"description": "",
"author": "Fuel Labs <[email protected]> (https://fuel.network/)",
"scripts": {
"pretest": "run-s build:forc build:process-predicates",
"pretest": "run-s build:forc build:process-predicates type:check",
"build:forc": "pnpm fuels build",
"build:process-predicates": "tsx ./scripts/process-predicates.ts"
"build:process-predicates": "tsx ./scripts/process-predicates.ts",
"type:check": "tsc --noEmit"
},
"license": "Apache-2.0",
"dependencies": {
Expand Down
13 changes: 4 additions & 9 deletions packages/fuel-gauge/src/bytecode-sway-lib.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Predicate, arrayify } from 'fuels';
import { arrayify } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

import { defaultPredicateAbi } from '../test/fixtures/abi/predicate';
import { defaultPredicateBytecode } from '../test/fixtures/bytecode/predicate';
import { PredicateTrue } from '../test/typegen';
import { BytecodeSwayLibFactory } from '../test/typegen/contracts';

import { launchTestContract } from './utils';
Expand Down Expand Up @@ -58,16 +57,12 @@ describe('bytecode computations', () => {
provider,
} = launched;

const predicate = new Predicate({
bytecode: defaultPredicateBytecode,
abi: defaultPredicateAbi,
provider,
});
const predicate = new PredicateTrue({ provider });

const address = predicate.address;

const { waitForResult } = await contract.functions
.compute_predicate_address(Array.from(arrayify(defaultPredicateBytecode)))
.compute_predicate_address(Array.from(arrayify(PredicateTrue.bytecode)))
.call();

const { value } = await waitForResult();
Expand Down
17 changes: 9 additions & 8 deletions packages/fuel-gauge/src/vectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { BigNumberish, BN } from 'fuels';

import { VectorsFactory } from '../test/typegen/contracts';
import { SmallEnumInput } from '../test/typegen/contracts/CoverageContract';
import type { Vec } from '../test/typegen/contracts/common';

import { launchTestContract } from './utils';

Expand Down Expand Up @@ -119,7 +120,7 @@ describe('Vector Tests', () => {
it('should test (u8, u8) vector input/output', async () => {
using contractInstance = await setupContract();

const INPUT: Array<[BigNumberish, BigNumberish]> = [
const INPUT: Vec<[BigNumberish, BigNumberish]> = [
nedsalk marked this conversation as resolved.
Show resolved Hide resolved
[1, 2],
[3, 4],
[5, 6],
Expand All @@ -136,7 +137,7 @@ describe('Vector Tests', () => {
it('should test (u64, u64) vector input/output', async () => {
using contractInstance = await setupContract();

const INPUT: [BigNumberish, BigNumberish][] = [
const INPUT: Vec<[BigNumberish, BigNumberish]> = [
[111, 2222],
[333, 4445],
[5555, 6],
Expand Down Expand Up @@ -167,7 +168,7 @@ describe('Vector Tests', () => {
it('should test [u64; 5] vector input/output', async () => {
using contractInstance = await setupContract();

const INPUT: Array<[BigNumberish, BigNumberish, BigNumberish, BigNumberish, BigNumberish]> = [
const INPUT: Vec<[BigNumberish, BigNumberish, BigNumberish, BigNumberish, BigNumberish]> = [
[1, 2, 3, 4, 5],
[500, 600, 700, 9000, 9999],
[11500, 22600, 33700, 55000, 669999],
Expand All @@ -182,7 +183,7 @@ describe('Vector Tests', () => {
it('should test [bool; 2] vector input/output', async () => {
using contractInstance = await setupContract();

const INPUT: Array<[boolean, boolean]> = [
const INPUT: Vec<[boolean, boolean]> = [
[true, true],
[true, false],
[false, true],
Expand Down Expand Up @@ -282,7 +283,7 @@ describe('Vector Tests', () => {
it('should test SmallEnum vector input/output', async () => {
using contractInstance = await setupContract();

const INPUT = [
const INPUT: Vec<SmallEnumInput> = [
SmallEnumInput.Empty,
SmallEnumInput.Empty,
SmallEnumInput.Empty,
Expand Down Expand Up @@ -403,7 +404,7 @@ describe('Vector Tests', () => {
it('should test Vec<u8> and b256 tuple input/output', async () => {
using contractInstance = await setupContract();

const INPUT: [Array<number>, string] = [[1, 8, 3, 2, 55, 215], hexlify(randomBytes(32))];
const INPUT: [Vec<BigNumberish>, string] = [[1, 8, 3, 2, 55, 215], hexlify(randomBytes(32))];

const { waitForResult } = await contractInstance.functions
.echo_vector_and_b256_tuple(...INPUT)
Expand All @@ -416,7 +417,7 @@ describe('Vector Tests', () => {
it('should test two vectors tuple input/output', async () => {
using contractInstance = await setupContract();

const INPUT: [Array<number>, Array<number>] = [
const INPUT: [Vec<BigNumberish>, Vec<BigNumberish>] = [
[219, 229],
[1, 254, 55],
];
Expand All @@ -432,7 +433,7 @@ describe('Vector Tests', () => {
it('should test u32 and three different vectors tuple input/output', async () => {
using contractInstance = await setupContract();

const INPUT: [number, boolean[], number[], number[]] = [
const INPUT: [BigNumberish, Vec<boolean>, Vec<BigNumberish>, Vec<BigNumberish>] = [
91000,
[true, true, false],
[95000, 153333],
Expand Down
38 changes: 0 additions & 38 deletions packages/fuel-gauge/test/fixtures/abi/predicate.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/fuel-gauge/test/fixtures/bytecode/predicate.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ impl MyContract for Contract {
}

fn compute_predicate_address(bytecode: Vec<u8>) -> Address {
return Address::from(0x6b6ef590390f0a7de75f8275ab5d7877c17236caba2514039c6565ec15f79111);
return Address::from(0x68fec7a57e48f4ec6467d7e09c27272bd8ca72b312ea553a470b98731475ccf3);
maschad marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion packages/fuel-gauge/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"outDir": "./dist"
},
"include": ["src"]
"include": ["src", "test"]
}
4 changes: 3 additions & 1 deletion packages/fuels/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
"build:package": "tsup",
"build:browser": "pnpm vite build",
"build:minified": "pnpm uglifyjs --compress --mangle --output dist/browser.min.mjs -- dist/browser.mjs",
"postbuild": "tsc --emitDeclarationOnly -p tsconfig.dts.json",
"postbuild": "run-s type:declarations type:check",
"type:declarations": "tsc --emitDeclarationOnly -p tsconfig.dts.json",
"type:check": "tsc --noEmit",
nedsalk marked this conversation as resolved.
Show resolved Hide resolved
"prepublishOnly": "cp ../../README.md ./README.md"
},
"license": "Apache-2.0",
Expand Down
5 changes: 4 additions & 1 deletion packages/logger/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import type { AbstractAddress } from '@fuel-ts/interfaces';
import { BN } from '@fuel-ts/math';
import debug from 'debug';
import type { MockInstance } from 'vitest';

import { logger, prefixLogger, defaultLogger } from '../src/index';

type Debug = typeof debug;

/**
* @group node
*/
describe('Logger Tests', () => {
let debugSpy;
let debugSpy: MockInstance<Parameters<Debug['log']>, ReturnType<Debug['log']>>;

beforeEach(() => {
debug.enable('test');
Expand Down
Loading