From 2f81ea89e6e244cbc02735bd4832d207b3551d33 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Wed, 29 Nov 2023 10:39:23 -0700 Subject: [PATCH 01/10] Remove useless escape characters --- .eslintrc.js | 1 - examples/ledger_canister/test/tests.ts | 4 ++-- src/compiler/new_command.ts | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index e3fb91c636..5aff5faa11 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -14,7 +14,6 @@ const temporarilyDisabledRules = { 'no-prototype-builtins': 'off', 'no-undef': 'off', 'no-useless-catch': 'off', - 'no-useless-escape': 'off', 'no-var': 'off', 'prefer-const': 'off' }; diff --git a/examples/ledger_canister/test/tests.ts b/examples/ledger_canister/test/tests.ts index d41f8c7bf0..a6844b149f 100644 --- a/examples/ledger_canister/test/tests.ts +++ b/examples/ledger_canister/test/tests.ts @@ -194,7 +194,7 @@ function getTransferErrorTests( name: 'deploy icpLedger', prep: async () => { execSync( - `dfx deploy icp_ledger --argument=\'(record {minting_account = "\'$(dfx ledger account-id)\'"; initial_values = vec {}; send_whitelist = vec {}})\'`, + `dfx deploy icp_ledger --argument='(record {minting_account = "'$(dfx ledger account-id)'"; initial_values = vec {}; send_whitelist = vec {}})'`, { stdio: 'inherit' } @@ -261,7 +261,7 @@ function getTransferErrorTests( name: 'deploy icpLedger', prep: async () => { execSync( - `dfx deploy icp_ledger --argument=\'(record {minting_account = "\'$(dfx ledger account-id)\'"; initial_values = vec { record { "\'$(dfx ledger account-id --of-canister ledger_canister)\'"; record { e8s=100_000_000_000 } }; }; send_whitelist = vec {}})\'`, + `dfx deploy icp_ledger --argument='(record {minting_account = "'$(dfx ledger account-id)'"; initial_values = vec { record { "'$(dfx ledger account-id --of-canister ledger_canister)'"; record { e8s=100_000_000_000 } }; }; send_whitelist = vec {}})'`, { stdio: 'inherit' } diff --git a/src/compiler/new_command.ts b/src/compiler/new_command.ts index ec9d100cb5..e6017a36c7 100644 --- a/src/compiler/new_command.ts +++ b/src/compiler/new_command.ts @@ -60,14 +60,14 @@ function generatePackageJson( ): string { return `{ "scripts": { - "dfx_install": "DFX_VERSION=${dfxVersion} sh -ci \\\"$(curl -fsSL https://sdk.dfinity.org/install.sh)\\\"", + "dfx_install": "DFX_VERSION=${dfxVersion} sh -ci \\"$(curl -fsSL https://sdk.dfinity.org/install.sh)\\"", "replica_start": "dfx start --background", "replica_stop": "dfx stop", "canister_deploy_local": "dfx deploy ${projectName}", "canister_deploy_mainnet": "dfx deploy --network ic ${projectName}", "canister_uninstall": "dfx canister uninstall-code ${projectName}", "canister_call_get_message": "dfx canister call ${projectName} getMessage", - "canister_call_set_message": "dfx canister call ${projectName} setMessage '(\\\"Hello world!\\\")'" + "canister_call_set_message": "dfx canister call ${projectName} setMessage '(\\"Hello world!\\")'" }, "dependencies": { "azle": "${azleVersion}" From be0e1d50b99b3ec9833fbcd8f5f73aff90699cd2 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Wed, 29 Nov 2023 10:41:25 -0700 Subject: [PATCH 02/10] Remove useless try/catch --- .eslintrc.js | 1 - .../reference/service/canister_function/index.ts | 16 ++++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 5aff5faa11..ab7ed9c2b9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -13,7 +13,6 @@ const temporarilyDisabledRules = { 'no-empty': 'off', 'no-prototype-builtins': 'off', 'no-undef': 'off', - 'no-useless-catch': 'off', 'no-var': 'off', 'prefer-const': 'off' }; diff --git a/src/lib/candid/types/reference/service/canister_function/index.ts b/src/lib/candid/types/reference/service/canister_function/index.ts index 9303201c69..ba9ef180ce 100644 --- a/src/lib/candid/types/reference/service/canister_function/index.ts +++ b/src/lib/candid/types/reference/service/canister_function/index.ts @@ -174,16 +174,12 @@ function serviceCall( const encodedArgs = encode(paramCandidTypes, args); if (notify) { - try { - return (callFunction as NotifyRawFunction)( - canisterId, - methodName, - encodedArgs, - cycles - ); - } catch (error) { - throw error; - } + return (callFunction as NotifyRawFunction)( + canisterId, + methodName, + encodedArgs, + cycles + ); } else { const encodedResult = await (callFunction as CallRawFunction)( canisterId, From 8ce44978516a11544e75087b2276e06c3b70b412 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Wed, 29 Nov 2023 11:00:06 -0700 Subject: [PATCH 03/10] Correct banned TS types --- .eslintrc.js | 1 - examples/stable_structures/src/canister3/stable_map_16.ts | 2 +- examples/stable_structures/src/canister3/stable_map_17.ts | 2 +- .../arbitraries/candid/constructed/tuple_arb/base.ts | 4 +++- .../arbitraries/candid/constructed/tuple_arb/index.ts | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index ab7ed9c2b9..ba63ce1de4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,7 +2,6 @@ // This is tech-debt. We should go through and re-enable these at some point. const temporarilyDisabledRules = { '@typescript-eslint/ban-ts-comment': 'off', - '@typescript-eslint/ban-types': 'off', '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/no-loss-of-precision': 'off', '@typescript-eslint/no-namespace': 'off', diff --git a/examples/stable_structures/src/canister3/stable_map_16.ts b/examples/stable_structures/src/canister3/stable_map_16.ts index c1090be395..636e3723c3 100644 --- a/examples/stable_structures/src/canister3/stable_map_16.ts +++ b/examples/stable_structures/src/canister3/stable_map_16.ts @@ -13,7 +13,7 @@ import { Vec } from 'azle'; -let stableMap16 = StableBTreeMap(text, StableJson(), 16); +let stableMap16 = StableBTreeMap(text, StableJson(), 16); export const stableMap16Methods = { stableMap16ContainsKey: query([text], bool, (key) => { diff --git a/examples/stable_structures/src/canister3/stable_map_17.ts b/examples/stable_structures/src/canister3/stable_map_17.ts index c30b3ac39e..b4dcc4fea0 100644 --- a/examples/stable_structures/src/canister3/stable_map_17.ts +++ b/examples/stable_structures/src/canister3/stable_map_17.ts @@ -11,7 +11,7 @@ import { Vec } from 'azle'; -let stableMap17 = StableBTreeMap<{}, text>(StableJson(), text, 17); +let stableMap17 = StableBTreeMap(StableJson(), text, 17); export const stableMap17Methods = { stableMap17ContainsKey: query([text], bool, (key) => { diff --git a/property_tests/arbitraries/candid/constructed/tuple_arb/base.ts b/property_tests/arbitraries/candid/constructed/tuple_arb/base.ts index f19353a6fc..e881ef4efc 100644 --- a/property_tests/arbitraries/candid/constructed/tuple_arb/base.ts +++ b/property_tests/arbitraries/candid/constructed/tuple_arb/base.ts @@ -53,7 +53,9 @@ function generateVale(fields: CandidMeta[]) { return fields.map((field) => field.agentArgumentValue); } -function generateExpectedVale(fields: CandidMeta[]): Tuple | {} { +function generateExpectedVale( + fields: CandidMeta[] +): Tuple | Record { if (fields.length === 0) { return {}; } diff --git a/property_tests/arbitraries/candid/constructed/tuple_arb/index.ts b/property_tests/arbitraries/candid/constructed/tuple_arb/index.ts index 7b32141484..3962e08e66 100644 --- a/property_tests/arbitraries/candid/constructed/tuple_arb/index.ts +++ b/property_tests/arbitraries/candid/constructed/tuple_arb/index.ts @@ -2,6 +2,6 @@ import { CandidType, CandidTypeArb } from '../../candid_type_arb'; import { TupleArb as Base } from './base'; export type Tuple = CandidType[]; -export type ReturnTuple = Tuple | {}; +export type ReturnTuple = Tuple | Record; export const TupleArb = Base(CandidTypeArb); From 19f121e6c3fa73e128703af869122f37f1c9bfcc Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Wed, 29 Nov 2023 13:20:54 -0700 Subject: [PATCH 04/10] Reduce no-unused-vars offenses to 33 The remainder are largely from the primitive_ops example, JS Doc comments, and our crazy type declarations. --- .eslintignore | 3 +++ examples/bitcoin/test/test.ts | 2 +- examples/bitcoin/test/tests.ts | 4 ++-- examples/canister/test/tests.ts | 2 +- examples/ckbtc/test/tests.ts | 2 -- examples/guard_functions/test/tests.ts | 17 +++++++-------- examples/ic_api/src/index.ts | 20 ++++++------------ examples/inspect_message/test/tests.ts | 5 ++--- examples/ledger_canister/test/tests.ts | 2 +- examples/list_of_lists/test/tests.ts | 9 +------- examples/management_canister/test/tests.ts | 21 +++++++------------ .../motoko_examples/simple-to-do/src/index.ts | 2 +- examples/notify_raw/test/tests.ts | 2 +- examples/primitive_types/test/tests.ts | 6 ++---- examples/stable_memory/test/tests.ts | 4 ++-- examples/stable_structures/test/test.ts | 1 - examples/tuple_types/src/index.ts | 3 +-- .../candid/constructed/vec_arb/base.ts | 2 +- .../arbitraries/candid/to_src_literal/void.ts | 2 +- property_tests/tests/null/test/test.ts | 2 +- .../tests/query_methods/test/test.ts | 2 +- src/compiler/compile_rust_code.ts | 3 +-- .../candid/did_file/visitor/visit/variant.ts | 2 +- src/lib/candid/type_mapping.ts | 2 +- src/lib/candid/types/reference/func.ts | 1 - .../service/canister_function/index.ts | 1 - .../canister_function/system_methods.ts | 1 - src/lib/ic/instruction_counter.ts | 1 - src/lib/ic/stable_64_read.ts | 3 --- src/lib/ic/stable_64_size.ts | 1 - src/lib/ic/stable_read.ts | 1 - src/lib/ic/stable_size.ts | 1 - src/lib/ic/stable_write.ts | 1 - src/lib/ic/time.ts | 1 - 34 files changed, 46 insertions(+), 86 deletions(-) diff --git a/.eslintignore b/.eslintignore index f8f99417b4..6bc2c5fd02 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,4 +3,7 @@ the_azle_book type_tests run_time_errors **/webpack.config.js +dfx_generate dfx_generated +declarations +property_tests/tests/**/src/* diff --git a/examples/bitcoin/test/test.ts b/examples/bitcoin/test/test.ts index 1faef4440d..e11eb83ab4 100644 --- a/examples/bitcoin/test/test.ts +++ b/examples/bitcoin/test/test.ts @@ -1,4 +1,4 @@ -import { getCanisterId, ok, runTests, Test } from 'azle/test'; +import { getCanisterId, runTests, Test } from 'azle/test'; import { bitcoinCli } from './bitcoin_cli'; import { createActor } from './dfx_generated/bitcoin'; diff --git a/examples/bitcoin/test/tests.ts b/examples/bitcoin/test/tests.ts index caaa63bda4..e97806b214 100644 --- a/examples/bitcoin/test/tests.ts +++ b/examples/bitcoin/test/tests.ts @@ -1,6 +1,6 @@ -import { Test } from 'azle/test'; +// import { Test } from 'azle/test'; -import { _SERVICE } from './dfx_generated/bitcoin/bitcoin.did'; +// import { _SERVICE } from './dfx_generated/bitcoin/bitcoin.did'; // import { ActorSubclass } from '@dfinity/agent'; // export function get_tests(update_canister: ActorSubclass<_SERVICE>): Test[] { diff --git a/examples/canister/test/tests.ts b/examples/canister/test/tests.ts index 30fbee98b7..0b66ce05f7 100644 --- a/examples/canister/test/tests.ts +++ b/examples/canister/test/tests.ts @@ -6,7 +6,7 @@ import { _SERVICE } from './dfx_generated/canister/canister.did'; // TODO these tests should be rewritten to use @dfinity/agent once this issue is resolved: https://github.com/dfinity/agent-js/issues/702 // TODO this issue also needs to be resolved: https://forum.dfinity.org/t/services-wont-deserialize-properly-if-functions-arent-in-alphabetical-order/20885 -export function getTests(canister: ActorSubclass<_SERVICE>): Test[] { +export function getTests(_canister: ActorSubclass<_SERVICE>): Test[] { return [ { name: 'canisterParam', diff --git a/examples/ckbtc/test/tests.ts b/examples/ckbtc/test/tests.ts index 008806eba0..bbaca2f630 100644 --- a/examples/ckbtc/test/tests.ts +++ b/examples/ckbtc/test/tests.ts @@ -86,8 +86,6 @@ export function getTests(): Test[] { return { Err: Object.keys(updateBalanceResult.Err)[0] }; } - const okValue = updateBalanceResult.Ok; - return { Ok: true }; } }, diff --git a/examples/guard_functions/test/tests.ts b/examples/guard_functions/test/tests.ts index 70460d1406..a5bdfe3fbc 100644 --- a/examples/guard_functions/test/tests.ts +++ b/examples/guard_functions/test/tests.ts @@ -1,7 +1,6 @@ import { ActorSubclass } from '@dfinity/agent'; import { AgentError } from '@dfinity/agent/lib/cjs/errors'; import { Test } from 'azle/test'; -import { execSync } from 'child_process'; import { _SERVICE } from './dfx_generated/guard_functions/guard_functions.did'; @@ -82,8 +81,8 @@ export function getTests( name: 'tightlyGuarded', test: async () => { try { - const result = - await guardFunctionsCanister.tightlyGuarded(); + await guardFunctionsCanister.tightlyGuarded(); + return { Err: 'Expected tightlyGuarded function to throw' }; @@ -100,8 +99,8 @@ export function getTests( name: 'errorStringGuarded', test: async () => { try { - const result = - await guardFunctionsCanister.errorStringGuarded(); + await guardFunctionsCanister.errorStringGuarded(); + return { Err: 'Expected errorStringGuarded function to throw' }; @@ -118,8 +117,8 @@ export function getTests( name: 'customErrorGuarded', test: async () => { try { - const result = - await guardFunctionsCanister.customErrorGuarded(); + await guardFunctionsCanister.customErrorGuarded(); + return { Err: 'Expected customErrorGuarded function to throw' }; @@ -136,8 +135,8 @@ export function getTests( name: 'nonStringErrValueGuarded', test: async () => { try { - const result = - await guardFunctionsCanister.nonStringErrValueGuarded(); + await guardFunctionsCanister.nonStringErrValueGuarded(); + return { Err: 'Expected nonStringErrValueGuarded function to throw' }; diff --git a/examples/ic_api/src/index.ts b/examples/ic_api/src/index.ts index bfd5f40f86..1061becd8a 100644 --- a/examples/ic_api/src/index.ts +++ b/examples/ic_api/src/index.ts @@ -54,21 +54,13 @@ export default Canister({ // } // returns the argument data as bytes. - argDataRaw: query( - [blob, int8, bool, text], - blob, - (arg1, arg2, arg3, arg4) => { - return ic.argDataRaw(); - } - ), + argDataRaw: query([blob, int8, bool, text], blob, () => { + return ic.argDataRaw(); + }), // returns the length of the argument data in bytes - argDataRawSize: query( - [blob, int8, bool, text], - nat32, - (arg1, arg2, arg3, arg4) => { - return ic.argDataRawSize(); - } - ), + argDataRawSize: query([blob, int8, bool, text], nat32, () => { + return ic.argDataRawSize(); + }), // returns the principal of the identity that called this function caller: query([], Principal, () => { return ic.caller(); diff --git a/examples/inspect_message/test/tests.ts b/examples/inspect_message/test/tests.ts index 24a8997699..6b9fe8a456 100644 --- a/examples/inspect_message/test/tests.ts +++ b/examples/inspect_message/test/tests.ts @@ -27,7 +27,7 @@ export function getTests( name: 'not calling `ic.acceptMessage` in inspectMessage', test: async () => { try { - const result = await inspectMessageCanister.inaccessible(); + await inspectMessageCanister.inaccessible(); return { Ok: false }; @@ -44,8 +44,7 @@ export function getTests( name: 'throwing in `inspectMessage`', test: async () => { try { - const result = - await inspectMessageCanister.alsoInaccessible(); + await inspectMessageCanister.alsoInaccessible(); return { Ok: false diff --git a/examples/ledger_canister/test/tests.ts b/examples/ledger_canister/test/tests.ts index a6844b149f..026d648b2e 100644 --- a/examples/ledger_canister/test/tests.ts +++ b/examples/ledger_canister/test/tests.ts @@ -1,6 +1,6 @@ import { ActorSubclass } from '@dfinity/agent'; import { Principal } from '@dfinity/principal'; -import { ok, Test } from 'azle/test'; +import { Test } from 'azle/test'; import { execSync } from 'child_process'; import { _SERVICE } from './dfx_generated/ledger_canister/ledger_canister.did'; diff --git a/examples/list_of_lists/test/tests.ts b/examples/list_of_lists/test/tests.ts index eee97ae07b..fe2dde5cf5 100644 --- a/examples/list_of_lists/test/tests.ts +++ b/examples/list_of_lists/test/tests.ts @@ -178,7 +178,7 @@ export function getTests(listOfListsCanister: ActorSubclass<_SERVICE>): Test[] { name: 'listOfEmpty test', test: async () => { try { - const result = await listOfListsCanister.listOfEmpty(); + await listOfListsCanister.listOfEmpty(); } catch (error) { return { Ok: (error as any).message.startsWith('Call failed') @@ -238,13 +238,6 @@ export function getTests(listOfListsCanister: ActorSubclass<_SERVICE>): Test[] { const expectedResult = [[[Principal.fromText('aaaaa-aa')]]]; const result = await listOfListsCanister.listOfPrincipal(expectedResult); - const principalEq = (a: any, b: any) => { - return ( - 'toText' in a && - 'toText' in b && - a.toText() === b.toText() - ); - }; return { Ok: diff --git a/examples/management_canister/test/tests.ts b/examples/management_canister/test/tests.ts index 30e1cf3376..4cb35e3ea8 100644 --- a/examples/management_canister/test/tests.ts +++ b/examples/management_canister/test/tests.ts @@ -24,8 +24,7 @@ export function getTests(managementCanister: ActorSubclass<_SERVICE>): Test[] { const canisterId = await managementCanister.getCreatedCanisterId(); - const executeUpdateSettingsResult = - await managementCanister.executeUpdateSettings(canisterId); + await managementCanister.executeUpdateSettings(canisterId); const getCanisterStatusResult = await managementCanister.getCanisterStatus({ @@ -75,8 +74,7 @@ export function getTests(managementCanister: ActorSubclass<_SERVICE>): Test[] { const cyclesBefore = statusBefore.cycles; - const depositCyclesResult = - await managementCanister.executeDepositCycles(canisterId); + await managementCanister.executeDepositCycles(canisterId); const statusAfter = await managementCanister.getCanisterStatus({ canister_id: canisterId @@ -95,8 +93,7 @@ export function getTests(managementCanister: ActorSubclass<_SERVICE>): Test[] { const canisterId = await managementCanister.getCreatedCanisterId(); - const executeUninstallCodeResult = - await managementCanister.executeUninstallCode(canisterId); + await managementCanister.executeUninstallCode(canisterId); const getCanisterStatusResult = await managementCanister.getCanisterStatus({ @@ -116,8 +113,7 @@ export function getTests(managementCanister: ActorSubclass<_SERVICE>): Test[] { const canisterId = await managementCanister.getCreatedCanisterId(); - const executeStopCanisterResult = - await managementCanister.executeStopCanister(canisterId); + await managementCanister.executeStopCanister(canisterId); const getCanisterStatusResult = await managementCanister.getCanisterStatus({ @@ -150,8 +146,7 @@ export function getTests(managementCanister: ActorSubclass<_SERVICE>): Test[] { }; } - const executeStartCanisterResult = - await managementCanister.executeStartCanister(canisterId); + await managementCanister.executeStartCanister(canisterId); const getCanisterStatusAfterResult = await managementCanister.getCanisterStatus({ @@ -219,11 +214,9 @@ export function getTests(managementCanister: ActorSubclass<_SERVICE>): Test[] { const canisterId = await managementCanister.getCreatedCanisterId(); - const executeStopCanisterResult = - await managementCanister.executeStopCanister(canisterId); + await managementCanister.executeStopCanister(canisterId); - const executeDeleteCanisterResult = - await managementCanister.executeDeleteCanister(canisterId); + await managementCanister.executeDeleteCanister(canisterId); return { Ok: true diff --git a/examples/motoko_examples/simple-to-do/src/index.ts b/examples/motoko_examples/simple-to-do/src/index.ts index de5b931cc9..dc9665f99e 100644 --- a/examples/motoko_examples/simple-to-do/src/index.ts +++ b/examples/motoko_examples/simple-to-do/src/index.ts @@ -63,6 +63,6 @@ export default Canister({ // ); // ``` // See: https://github.com/demergent-labs/azle/issues/574 - todos = new Map([...todos].filter(([key, value]) => !value.completed)); + todos = new Map([...todos].filter(([_key, value]) => !value.completed)); }) }); diff --git a/examples/notify_raw/test/tests.ts b/examples/notify_raw/test/tests.ts index b2f97fce47..9efc3c91e6 100644 --- a/examples/notify_raw/test/tests.ts +++ b/examples/notify_raw/test/tests.ts @@ -1,5 +1,5 @@ import { ActorSubclass } from '@dfinity/agent'; -import { ok, Test } from 'azle/test'; +import { Test } from 'azle/test'; import { _SERVICE as CANISTER1_SERVICE } from './dfx_generated/canister1/canister1.did'; import { _SERVICE as CANISTER2_SERVICE } from './dfx_generated/canister2/canister2.did'; diff --git a/examples/primitive_types/test/tests.ts b/examples/primitive_types/test/tests.ts index 4a672f2cc3..e483b6d6bf 100644 --- a/examples/primitive_types/test/tests.ts +++ b/examples/primitive_types/test/tests.ts @@ -498,7 +498,7 @@ export function getTests( name: 'getEmpty', test: async () => { try { - const result = await primitiveTypesCanister.getEmpty(); + await primitiveTypesCanister.getEmpty(); } catch (error) { return { Ok: (error as any).message.startsWith('Call failed') @@ -514,9 +514,7 @@ export function getTests( name: 'printEmpty', test: async () => { try { - const result = await primitiveTypesCanister.printEmpty( - undefined as never - ); + await primitiveTypesCanister.printEmpty(undefined as never); } catch (error) { return { Ok: ((error as any).message as string).includes( diff --git a/examples/stable_memory/test/tests.ts b/examples/stable_memory/test/tests.ts index 46ddb9560f..3f4b95f1bf 100644 --- a/examples/stable_memory/test/tests.ts +++ b/examples/stable_memory/test/tests.ts @@ -201,7 +201,7 @@ export function getTests( name: 'stable grow out of memory', test: async () => { try { - const result = await stableMemoryCanister.stableGrow(1); + await stableMemoryCanister.stableGrow(1); } catch (e: any) { return { Ok: e @@ -232,7 +232,7 @@ export function getTests( name: 'stable64 grow out of memory', test: async () => { try { - const result = await stableMemoryCanister.stable64Grow(1n); + await stableMemoryCanister.stable64Grow(1n); } catch (e: any) { return { Ok: e diff --git a/examples/stable_structures/test/test.ts b/examples/stable_structures/test/test.ts index 888b02ffd0..11baeb1989 100644 --- a/examples/stable_structures/test/test.ts +++ b/examples/stable_structures/test/test.ts @@ -3,7 +3,6 @@ import { getCanisterId, runTests } from 'azle/test'; import { createActor as createActorCanister1 } from './dfx_generated/canister1'; import { createActor as createActorCanister2 } from './dfx_generated/canister2'; import { createActor as createActorCanister3 } from './dfx_generated/canister3'; -import { _SERVICE } from './dfx_generated/canister3/canister3.did'; import { getTests } from './tests'; const stableStructuresCanister_1 = createActorCanister1( diff --git a/examples/tuple_types/src/index.ts b/examples/tuple_types/src/index.ts index 5659439d0f..6f8982afb3 100644 --- a/examples/tuple_types/src/index.ts +++ b/examples/tuple_types/src/index.ts @@ -14,8 +14,7 @@ import { text, Tuple, Variant, - Vec, - Void + Vec } from 'azle'; // TODO maybe we should write tests for canister and stable storage? diff --git a/property_tests/arbitraries/candid/constructed/vec_arb/base.ts b/property_tests/arbitraries/candid/constructed/vec_arb/base.ts index dc0174b6e3..aad1bf9dd2 100644 --- a/property_tests/arbitraries/candid/constructed/vec_arb/base.ts +++ b/property_tests/arbitraries/candid/constructed/vec_arb/base.ts @@ -1,4 +1,4 @@ -import fc, { sample } from 'fast-check'; +import fc from 'fast-check'; import { UniqueIdentifierArb } from '../../../unique_identifier_arb'; import { CandidMeta, Src } from '../../candid_arb'; diff --git a/property_tests/arbitraries/candid/to_src_literal/void.ts b/property_tests/arbitraries/candid/to_src_literal/void.ts index c04cfbf647..a20652892d 100644 --- a/property_tests/arbitraries/candid/to_src_literal/void.ts +++ b/property_tests/arbitraries/candid/to_src_literal/void.ts @@ -1,3 +1,3 @@ -export function voidToSrcLiteral(value: undefined): string { +export function voidToSrcLiteral(_value: undefined): string { return 'undefined'; } diff --git a/property_tests/tests/null/test/test.ts b/property_tests/tests/null/test/test.ts index 4215e9995e..fe928633f6 100644 --- a/property_tests/tests/null/test/test.ts +++ b/property_tests/tests/null/test/test.ts @@ -74,7 +74,7 @@ function generateTest( ); return { - Ok: deepEqual(result, null) + Ok: deepEqual(result, returnNull.agentResponseValue) }; } }; diff --git a/property_tests/tests/query_methods/test/test.ts b/property_tests/tests/query_methods/test/test.ts index a8a72457a3..fc0be49c7a 100644 --- a/property_tests/tests/query_methods/test/test.ts +++ b/property_tests/tests/query_methods/test/test.ts @@ -2,7 +2,7 @@ import { execSync } from 'child_process'; import fc from 'fast-check'; import { writeFileSync } from 'fs'; -import { getCanisterId, runTests, Test } from '../../../../test'; +import { runTests, Test } from '../../../../test'; import { getActor } from '../../../get_actor'; // TODO Canister diff --git a/src/compiler/compile_rust_code.ts b/src/compiler/compile_rust_code.ts index 8be4891d28..a7711f4fd8 100644 --- a/src/compiler/compile_rust_code.ts +++ b/src/compiler/compile_rust_code.ts @@ -3,8 +3,7 @@ import { execSync, IOType } from 'child_process'; import { GLOBAL_AZLE_RUST_BIN_DIR, GLOBAL_AZLE_RUST_DIR, - GLOBAL_AZLE_TARGET_DIR, - time + GLOBAL_AZLE_TARGET_DIR } from './utils'; export function compileRustCode( diff --git a/src/lib/candid/did_file/visitor/visit/variant.ts b/src/lib/candid/did_file/visitor/visit/variant.ts index 7f07d15456..e623f47258 100644 --- a/src/lib/candid/did_file/visitor/visit/variant.ts +++ b/src/lib/candid/did_file/visitor/visit/variant.ts @@ -9,7 +9,7 @@ export function visitVariant( didVisitor: DidVisitor, data: VisitorData ): VisitorResult { - const candidFields = fields.map(([key, value]) => + const candidFields = fields.map(([_key, value]) => value.accept(didVisitor, { ...data, isOnService: false }) ); const candid = extractCandid(candidFields); diff --git a/src/lib/candid/type_mapping.ts b/src/lib/candid/type_mapping.ts index bb9f5c4bd0..c84c44777d 100644 --- a/src/lib/candid/type_mapping.ts +++ b/src/lib/candid/type_mapping.ts @@ -118,7 +118,7 @@ export type TypeMapping = RecursionLevel extends 10 : T extends typeof AzleNull ? Null : T extends typeof AzleReserved - ? any + ? reserved : T extends typeof AzleEmpty ? empty : T extends { tsType: infer U } diff --git a/src/lib/candid/types/reference/func.ts b/src/lib/candid/types/reference/func.ts index 25fc505bfa..3d88f669ca 100644 --- a/src/lib/candid/types/reference/func.ts +++ b/src/lib/candid/types/reference/func.ts @@ -1,6 +1,5 @@ import { IDL } from '@dfinity/candid'; -import { Serializable } from '../../../stable_structures/stable_b_tree_map'; import { CandidType, Parent, toIdlArray } from '../../index'; import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; diff --git a/src/lib/candid/types/reference/service/canister_function/index.ts b/src/lib/candid/types/reference/service/canister_function/index.ts index ba9ef180ce..7bee6765c9 100644 --- a/src/lib/candid/types/reference/service/canister_function/index.ts +++ b/src/lib/candid/types/reference/service/canister_function/index.ts @@ -3,7 +3,6 @@ import { IDL } from '@dfinity/candid'; import { CanisterMethodInfo } from '../../../../../canister_methods/types/canister_method_info'; import { ic } from '../../../../../ic'; import { CandidType, Parent, toIdlArray } from '../../../../index'; -import { _AzleRecursiveFunction } from '../../../../recursive'; import { decode, encode } from '../../../../serde'; import { Principal } from '../../principal'; import { createQueryMethods, createUpdateMethods } from './query_update'; diff --git a/src/lib/candid/types/reference/service/canister_function/system_methods.ts b/src/lib/candid/types/reference/service/canister_function/system_methods.ts index c89b16d557..157d4dd1b6 100644 --- a/src/lib/candid/types/reference/service/canister_function/system_methods.ts +++ b/src/lib/candid/types/reference/service/canister_function/system_methods.ts @@ -1,7 +1,6 @@ import { IDL } from '@dfinity/candid'; import { Parent, toIdlArray } from '../../../../index'; -import { _AzleRecursiveFunction } from '../../../../recursive'; import { CanisterOptions, ServiceFunctionInfo } from '.'; type SystemMethod = { name: string } | undefined; diff --git a/src/lib/ic/instruction_counter.ts b/src/lib/ic/instruction_counter.ts index 5299044570..7ab9810780 100644 --- a/src/lib/ic/instruction_counter.ts +++ b/src/lib/ic/instruction_counter.ts @@ -1,4 +1,3 @@ -import { decode } from '../candid/serde/decode'; import { nat64 } from '../candid/types/primitive/nats/nat64'; /** diff --git a/src/lib/ic/stable_64_read.ts b/src/lib/ic/stable_64_read.ts index c3d765f4ae..5a54c76784 100644 --- a/src/lib/ic/stable_64_read.ts +++ b/src/lib/ic/stable_64_read.ts @@ -1,4 +1,3 @@ -import { encode } from '../candid/serde/encode'; import { nat64 } from '../candid/types/primitive/nats/nat64'; /** @@ -13,8 +12,6 @@ export function stable64Read(offset: nat64, length: nat64): Uint8Array { return undefined as any; } - const paramsCandidBytes = encode([nat64, nat64], [offset, length]).buffer; - return new Uint8Array( globalThis._azleIc.stable64Read(offset.toString(), length.toString()) ); diff --git a/src/lib/ic/stable_64_size.ts b/src/lib/ic/stable_64_size.ts index 69bc08c2d5..bc8ed9d40c 100644 --- a/src/lib/ic/stable_64_size.ts +++ b/src/lib/ic/stable_64_size.ts @@ -1,4 +1,3 @@ -import { decode } from '../candid/serde/decode'; import { nat64 } from '../candid/types/primitive/nats/nat64'; /** diff --git a/src/lib/ic/stable_read.ts b/src/lib/ic/stable_read.ts index fb4ebb3ebb..28e1a7d060 100644 --- a/src/lib/ic/stable_read.ts +++ b/src/lib/ic/stable_read.ts @@ -1,4 +1,3 @@ -import { encode } from '../candid/serde/encode'; import { nat32 } from '../candid/types/primitive/nats/nat32'; /** diff --git a/src/lib/ic/stable_size.ts b/src/lib/ic/stable_size.ts index 6816473b4c..0157145fc6 100644 --- a/src/lib/ic/stable_size.ts +++ b/src/lib/ic/stable_size.ts @@ -1,4 +1,3 @@ -import { decode } from '../candid/serde/decode'; import { nat32 } from '../candid/types/primitive/nats/nat32'; /** diff --git a/src/lib/ic/stable_write.ts b/src/lib/ic/stable_write.ts index 448f896614..e44d3548cd 100644 --- a/src/lib/ic/stable_write.ts +++ b/src/lib/ic/stable_write.ts @@ -1,4 +1,3 @@ -import { encode } from '../candid/serde/encode'; import { blob } from '../candid/types/constructed/blob'; import { nat32 } from '../candid/types/primitive/nats/nat32'; diff --git a/src/lib/ic/time.ts b/src/lib/ic/time.ts index 8d0478ebb2..ce18b02de3 100644 --- a/src/lib/ic/time.ts +++ b/src/lib/ic/time.ts @@ -1,4 +1,3 @@ -import { decode } from '../candid/serde/decode'; import { nat64 } from '../candid/types/primitive/nats/nat64'; /** From d94873da70d9d9205629a52392491634ef769c7b Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Wed, 29 Nov 2023 13:28:05 -0700 Subject: [PATCH 05/10] Enable no-loss-of-precision and no-namespace --- .eslintrc.js | 28 +++++++++++++++------------- src/lib/system_types/result.ts | 6 ++++++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index ba63ce1de4..23cb03544a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,19 +1,17 @@ // TODO: These rules should be enabled, but we had offenses when we enabled ESLint. // This is tech-debt. We should go through and re-enable these at some point. const temporarilyDisabledRules = { - '@typescript-eslint/ban-ts-comment': 'off', - '@typescript-eslint/no-explicit-any': 'off', - '@typescript-eslint/no-loss-of-precision': 'off', - '@typescript-eslint/no-namespace': 'off', - '@typescript-eslint/no-unused-vars': 'off', - '@typescript-eslint/no-var-requires': 'off', - '@typescript-eslint/prefer-as-const': 'off', - 'no-constant-condition': 'off', - 'no-empty': 'off', - 'no-prototype-builtins': 'off', - 'no-undef': 'off', - 'no-var': 'off', - 'prefer-const': 'off' + '@typescript-eslint/ban-ts-comment': 'off', // 39 problems + '@typescript-eslint/no-explicit-any': 'off', // 321 problems + '@typescript-eslint/no-unused-vars': 'off', // 33 problems + '@typescript-eslint/no-var-requires': 'off', // 12 problems + '@typescript-eslint/prefer-as-const': 'off', // 45 problems + 'no-constant-condition': 'off', // 1 problem + 'no-empty': 'off', // 9 problems + 'no-prototype-builtins': 'off', // 2 problems + 'no-undef': 'off', // 11 problems + 'no-var': 'off', // 6 problems + 'prefer-const': 'off' // 137 problems }; module.exports = { @@ -46,6 +44,10 @@ module.exports = { rules: { 'simple-import-sort/exports': 'error', 'simple-import-sort/imports': 'error', + '@typescript-eslint/no-unused-vars': [ + 'error', + { argsIgnorePattern: '^_' } + ], ...temporarilyDisabledRules } }; diff --git a/src/lib/system_types/result.ts b/src/lib/system_types/result.ts index 2861cabbe8..03cf6657fa 100644 --- a/src/lib/system_types/result.ts +++ b/src/lib/system_types/result.ts @@ -33,6 +33,12 @@ export type Result = RequireExactlyOne<{ Err: Err; }>; +// Using a namespace is the only way we can see to get Result exported as: +// - a type +// - a function +// - a namespace for the Ok and Err functions +// +// eslint-disable-next-line @typescript-eslint/no-namespace export namespace Result { export function Ok(value: T) { return { Ok: value }; From b6442849cf39c6a82a9884e0613e8f5be6c8c514 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Wed, 29 Nov 2023 14:01:42 -0700 Subject: [PATCH 06/10] Enable no-prototype-builtins rule --- .eslintrc.js | 1 - scripts/reorder_dfx_json.js | 7 ++++++- src/lib/candid/types/constructed/to_idl_map.ts | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 23cb03544a..8082730218 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -8,7 +8,6 @@ const temporarilyDisabledRules = { '@typescript-eslint/prefer-as-const': 'off', // 45 problems 'no-constant-condition': 'off', // 1 problem 'no-empty': 'off', // 9 problems - 'no-prototype-builtins': 'off', // 2 problems 'no-undef': 'off', // 11 problems 'no-var': 'off', // 6 problems 'prefer-const': 'off' // 137 problems diff --git a/scripts/reorder_dfx_json.js b/scripts/reorder_dfx_json.js index f8646b376b..430c037037 100644 --- a/scripts/reorder_dfx_json.js +++ b/scripts/reorder_dfx_json.js @@ -32,7 +32,12 @@ fs.readdir(dirPath, (err, folders) => { fs.readJson(dfxJsonPath) .then((dfxConfig) => { for (const canister in dfxConfig.canisters) { - if (dfxConfig.canisters.hasOwnProperty(canister)) { + if ( + Object.prototype.hasOwnProperty.call( + dfxConfig.canisters, + canister + ) + ) { dfxConfig.canisters[canister] = reorderKeys( dfxConfig.canisters[canister], [ diff --git a/src/lib/candid/types/constructed/to_idl_map.ts b/src/lib/candid/types/constructed/to_idl_map.ts index f9d6ec3c9d..58c9353571 100644 --- a/src/lib/candid/types/constructed/to_idl_map.ts +++ b/src/lib/candid/types/constructed/to_idl_map.ts @@ -10,7 +10,7 @@ export function toIdlMap(candidMap: CandidMap, parent: Parent[]): IdlMap { const idlMap: IdlMap = {}; for (const key in candidMap) { - if (candidMap.hasOwnProperty(key)) { + if (Object.prototype.hasOwnProperty.call(candidMap, key)) { const candidType = candidMap[key]; idlMap[key] = toIdl(candidType, parent); } From 334402ea2cb7335017b71502d470272eaa5006e1 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Wed, 29 Nov 2023 14:05:40 -0700 Subject: [PATCH 07/10] Enable no-empty rule --- .eslintrc.js | 1 - examples/rejections/src/rejections/index.ts | 16 ++++++++++++---- examples/robust_imports/test/tests.ts | 20 +++++++++++++++----- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 8082730218..94189c0055 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -7,7 +7,6 @@ const temporarilyDisabledRules = { '@typescript-eslint/no-var-requires': 'off', // 12 problems '@typescript-eslint/prefer-as-const': 'off', // 45 problems 'no-constant-condition': 'off', // 1 problem - 'no-empty': 'off', // 9 problems 'no-undef': 'off', // 11 problems 'no-var': 'off', // 6 problems 'prefer-const': 'off' // 137 problems diff --git a/examples/rejections/src/rejections/index.ts b/examples/rejections/src/rejections/index.ts index a831c22b9c..29d85f2d5a 100644 --- a/examples/rejections/src/rejections/index.ts +++ b/examples/rejections/src/rejections/index.ts @@ -39,28 +39,36 @@ export default Canister({ getRejectionCodeDestinationInvalid: update([], RejectionCode, async () => { try { await ic.call(nonexistentCanister.method); - } catch (error) {} + } catch (error) { + // continue regardless of error + } return ic.rejectCode(); }), getRejectionCodeCanisterReject: update([], RejectionCode, async () => { try { await ic.call(someCanister.reject, { args: ['reject'] }); - } catch (error) {} + } catch (error) { + // continue regardless of error + } return ic.rejectCode(); }), getRejectionCodeCanisterError: update([], RejectionCode, async () => { try { await ic.call(someCanister.error); - } catch (error) {} + } catch (error) { + // continue regardless of error + } return ic.rejectCode(); }), getRejectionMessage: update([text], text, async (message: text) => { try { await ic.call(someCanister.reject, { args: [message] }); - } catch (error) {} + } catch (error) { + // continue regardless of error + } return ic.rejectMessage(); }) diff --git a/examples/robust_imports/test/tests.ts b/examples/robust_imports/test/tests.ts index 12096f5933..2e0aa65dca 100644 --- a/examples/robust_imports/test/tests.ts +++ b/examples/robust_imports/test/tests.ts @@ -179,14 +179,18 @@ function getAzleCoverageTests(fruit: ActorSubclass<_SERVICE>): Test[] { try { await fruit.checkWatermelonForSeeds(false, watermelon); return { Ok: false }; - } catch (err) {} + } catch (err) { + // continue regardless of error + } try { await fruit.checkWatermelonForSeeds( true, seedlessWatermelon ); return { Ok: false }; - } catch (err) {} + } catch (err) { + // continue regardless of error + } try { await fruit.checkWatermelonForSeeds( false, @@ -310,11 +314,15 @@ function getAzleCoverageTests(fruit: ActorSubclass<_SERVICE>): Test[] { try { await fruit.dirtyIlama(); return { Ok: false }; - } catch {} + } catch { + // continue regardless of error + } try { await fruit.pickElderberry(); return { Ok: false }; - } catch {} + } catch { + // continue regardless of error + } return { Ok: !(await fruit.isFruitPrepared()) }; @@ -396,7 +404,9 @@ function getTypeAliasDeclTests(canister: ActorSubclass<_SERVICE>): Test[] { try { await canister.deepEmptyAlias(); return { Ok: false }; - } catch {} + } catch { + // continue regardless of error + } return { Ok: true }; } }, From 7f499e44c7e84d0d2fbb2ca2aa6fb0d2b311d5de Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Wed, 29 Nov 2023 14:44:57 -0700 Subject: [PATCH 08/10] Resolve 4 no-var-requires problems --- .eslintrc.js | 2 +- package-lock.json | 7 +++++++ package.json | 1 + src/compiler/generate_candid_and_canister_methods.ts | 8 ++++---- src/compiler/utils/global_paths.ts | 3 ++- src/lib/globals.ts | 5 +++-- 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 94189c0055..7caf2f19fe 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -4,7 +4,7 @@ const temporarilyDisabledRules = { '@typescript-eslint/ban-ts-comment': 'off', // 39 problems '@typescript-eslint/no-explicit-any': 'off', // 321 problems '@typescript-eslint/no-unused-vars': 'off', // 33 problems - '@typescript-eslint/no-var-requires': 'off', // 12 problems + '@typescript-eslint/no-var-requires': 'off', // 8 problems '@typescript-eslint/prefer-as-const': 'off', // 45 problems 'no-constant-condition': 'off', // 1 problem 'no-undef': 'off', // 11 problems diff --git a/package-lock.json b/package-lock.json index e09b7d82c0..00922321fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,6 +28,7 @@ "devDependencies": { "@dfinity/agent": "^0.19.2", "@types/fs-extra": "9.0.13", + "@types/text-encoding": "^0.0.39", "@typescript-eslint/eslint-plugin": "^6.13.0", "@typescript-eslint/parser": "^6.13.0", "eslint": "^8.54.0", @@ -811,6 +812,12 @@ "integrity": "sha512-dn1l8LaMea/IjDoHNd9J52uBbInB796CDffS6VdIxvqYCPSG0V0DzHp76GpaWnlhg88uYyPbXCDIowa86ybd5A==", "dev": true }, + "node_modules/@types/text-encoding": { + "version": "0.0.39", + "resolved": "https://registry.npmjs.org/@types/text-encoding/-/text-encoding-0.0.39.tgz", + "integrity": "sha512-gRPvgL1aMgP6Pv92Rs310cJvVQ86DSF62E7K30g1FoGmmYWXoNuXT8PV835iAVeiAZkRwr2IW37KuyDn9ljmeA==", + "dev": true + }, "node_modules/@types/uuid": { "version": "9.0.5", "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-9.0.5.tgz", diff --git a/package.json b/package.json index e709fb4b07..42c3a2beef 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "devDependencies": { "@dfinity/agent": "^0.19.2", "@types/fs-extra": "9.0.13", + "@types/text-encoding": "^0.0.39", "@typescript-eslint/eslint-plugin": "^6.13.0", "@typescript-eslint/parser": "^6.13.0", "eslint": "^8.54.0", diff --git a/src/compiler/generate_candid_and_canister_methods.ts b/src/compiler/generate_candid_and_canister_methods.ts index fa87a79986..e2bb069b28 100644 --- a/src/compiler/generate_candid_and_canister_methods.ts +++ b/src/compiler/generate_candid_and_canister_methods.ts @@ -1,3 +1,5 @@ +import { createContext, Script } from 'vm'; + import { toDidString } from '../lib/candid/did_file'; import { DidVisitor, @@ -9,8 +11,6 @@ export function generateCandidAndCanisterMethods(mainJs: string): { candid: string; canisterMethods: CanisterMethods; } { - const vm = require('vm'); - const sandbox = { globalThis: {}, crypto: { @@ -29,9 +29,9 @@ export function generateCandidAndCanisterMethods(mainJs: string): { TextDecoder, TextEncoder }; - const context = new vm.createContext(sandbox); + const context = createContext(sandbox); - const script = new vm.Script(mainJs); + const script = new Script(mainJs); script.runInContext(context); const canisterMethods = (sandbox.exports as any).canisterMethods; diff --git a/src/compiler/utils/global_paths.ts b/src/compiler/utils/global_paths.ts index bad70e6c17..3c0a0f18fd 100644 --- a/src/compiler/utils/global_paths.ts +++ b/src/compiler/utils/global_paths.ts @@ -1,9 +1,10 @@ +import { homedir } from 'os'; import { join, resolve } from 'path'; import { rust_version, version as azleVersion } from '../../../package.json'; export const GLOBAL_AZLE_CONFIG_DIR = resolve( - require('os').homedir(), + homedir(), join('.config', 'azle') ); export const GLOBAL_AZLE_RUST_DIR = join( diff --git a/src/lib/globals.ts b/src/lib/globals.ts index 4729ebd2da..5f9bcf3904 100644 --- a/src/lib/globals.ts +++ b/src/lib/globals.ts @@ -1,4 +1,5 @@ import { Buffer } from 'buffer'; +import { TextDecoder, TextEncoder } from 'text-encoding'; import { ic } from './ic'; import { AzleIc } from './ic/types/azle_ic'; @@ -12,8 +13,8 @@ declare global { var _azleGuardFunctions: { [key: string]: () => any }; } -globalThis.TextDecoder = require('text-encoding').TextDecoder; -globalThis.TextEncoder = require('text-encoding').TextEncoder; +globalThis.TextDecoder = TextDecoder; +globalThis.TextEncoder = TextEncoder; globalThis._azleIcTimers = {}; globalThis._azleResolveIds = {}; globalThis._azleRejectIds = {}; From 74890415259268b94fda5c337cf917a5a85aef07 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Wed, 29 Nov 2023 14:52:21 -0700 Subject: [PATCH 09/10] Enable prefer-as-const rule --- .eslintrc.js | 1 - src/lib/candid/types/constructed/blob.ts | 4 ++-- src/lib/candid/types/constructed/opt.ts | 4 ++-- src/lib/candid/types/constructed/tuple.ts | 4 ++-- src/lib/candid/types/constructed/vec.ts | 4 ++-- src/lib/candid/types/primitive/bool.ts | 4 ++-- src/lib/candid/types/primitive/empty.ts | 4 ++-- src/lib/candid/types/primitive/floats/float32.ts | 4 ++-- src/lib/candid/types/primitive/floats/float64.ts | 4 ++-- src/lib/candid/types/primitive/ints/int.ts | 4 ++-- src/lib/candid/types/primitive/ints/int16.ts | 4 ++-- src/lib/candid/types/primitive/ints/int32.ts | 4 ++-- src/lib/candid/types/primitive/ints/int64.ts | 4 ++-- src/lib/candid/types/primitive/ints/int8.ts | 4 ++-- src/lib/candid/types/primitive/nats/nat.ts | 4 ++-- src/lib/candid/types/primitive/nats/nat16.ts | 4 ++-- src/lib/candid/types/primitive/nats/nat32.ts | 4 ++-- src/lib/candid/types/primitive/nats/nat64.ts | 4 ++-- src/lib/candid/types/primitive/nats/nat8.ts | 4 ++-- src/lib/candid/types/primitive/null.ts | 4 ++-- src/lib/candid/types/primitive/reserved.ts | 4 ++-- src/lib/candid/types/primitive/text.ts | 4 ++-- src/lib/candid/types/primitive/void.ts | 4 ++-- src/lib/candid/types/reference/principal.ts | 2 +- 24 files changed, 45 insertions(+), 46 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 7caf2f19fe..4e6d16d3f2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -5,7 +5,6 @@ const temporarilyDisabledRules = { '@typescript-eslint/no-explicit-any': 'off', // 321 problems '@typescript-eslint/no-unused-vars': 'off', // 33 problems '@typescript-eslint/no-var-requires': 'off', // 8 problems - '@typescript-eslint/prefer-as-const': 'off', // 45 problems 'no-constant-condition': 'off', // 1 problem 'no-undef': 'off', // 11 problems 'no-var': 'off', // 6 problems diff --git a/src/lib/candid/types/constructed/blob.ts b/src/lib/candid/types/constructed/blob.ts index 9077c43533..dce10ec0c6 100644 --- a/src/lib/candid/types/constructed/blob.ts +++ b/src/lib/candid/types/constructed/blob.ts @@ -4,8 +4,8 @@ import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; export class AzleBlob { - _azleKind: 'AzleBlob' = 'AzleBlob'; - static _azleKind: 'AzleBlob' = 'AzleBlob'; + _azleKind = 'AzleBlob' as const; + static _azleKind = 'AzleBlob' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/constructed/opt.ts b/src/lib/candid/types/constructed/opt.ts index 3e550b0f35..4ad30cdc13 100644 --- a/src/lib/candid/types/constructed/opt.ts +++ b/src/lib/candid/types/constructed/opt.ts @@ -36,8 +36,8 @@ export class AzleOpt { innerType: CandidType; - _azleKind: 'AzleOpt' = 'AzleOpt'; - static _azleKind: 'AzleOpt' = 'AzleOpt'; + _azleKind = 'AzleOpt' as const; + static _azleKind = 'AzleOpt' as const; toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/constructed/tuple.ts b/src/lib/candid/types/constructed/tuple.ts index 71bd75b929..7f2c2dd0cb 100644 --- a/src/lib/candid/types/constructed/tuple.ts +++ b/src/lib/candid/types/constructed/tuple.ts @@ -17,8 +17,8 @@ export class AzleTuple { innerTypes: CandidType[]; - _azleKind: 'AzleTuple' = 'AzleTuple'; - static _azleKind: 'AzleTuple' = 'AzleTuple'; + _azleKind = 'AzleTuple' as const; + static _azleKind = 'AzleTuple' as const; toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/constructed/vec.ts b/src/lib/candid/types/constructed/vec.ts index 4139252d01..627d8f0250 100644 --- a/src/lib/candid/types/constructed/vec.ts +++ b/src/lib/candid/types/constructed/vec.ts @@ -13,8 +13,8 @@ export class AzleVec { innerType: CandidType; - _azleKind: 'AzleVec' = 'AzleVec'; - static _azleKind: 'AzleVec' = 'AzleVec'; + _azleKind = 'AzleVec' as const; + static _azleKind = 'AzleVec' as const; toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/bool.ts b/src/lib/candid/types/primitive/bool.ts index 80b0c38acf..2460f88b7b 100644 --- a/src/lib/candid/types/primitive/bool.ts +++ b/src/lib/candid/types/primitive/bool.ts @@ -4,8 +4,8 @@ import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; export class AzleBool { - _azleKind: 'AzleBool' = 'AzleBool'; - static _azleKind: 'AzleBool' = 'AzleBool'; + _azleKind = 'AzleBool' as const; + static _azleKind = 'AzleBool' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/empty.ts b/src/lib/candid/types/primitive/empty.ts index c0a47979ba..e2e14f7948 100644 --- a/src/lib/candid/types/primitive/empty.ts +++ b/src/lib/candid/types/primitive/empty.ts @@ -4,8 +4,8 @@ import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; export class AzleEmpty { - _azleKind: 'AzleEmpty' = 'AzleEmpty'; - static _azleKind: 'AzleEmpty' = 'AzleEmpty'; + _azleKind = 'AzleEmpty' as const; + static _azleKind = 'AzleEmpty' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/floats/float32.ts b/src/lib/candid/types/primitive/floats/float32.ts index 1f0ca05fdf..3bd1e8d75f 100644 --- a/src/lib/candid/types/primitive/floats/float32.ts +++ b/src/lib/candid/types/primitive/floats/float32.ts @@ -4,8 +4,8 @@ import { decode } from '../../../serde/decode'; import { encode } from '../../../serde/encode'; export class AzleFloat32 { - _azleKind: 'AzleFloat32' = 'AzleFloat32'; - static _azleKind: 'AzleFloat32' = 'AzleFloat32'; + _azleKind = 'AzleFloat32' as const; + static _azleKind = 'AzleFloat32' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/floats/float64.ts b/src/lib/candid/types/primitive/floats/float64.ts index 8b7a757654..3b1d40f2b1 100644 --- a/src/lib/candid/types/primitive/floats/float64.ts +++ b/src/lib/candid/types/primitive/floats/float64.ts @@ -4,8 +4,8 @@ import { decode } from '../../../serde/decode'; import { encode } from '../../../serde/encode'; export class AzleFloat64 { - _azleKind: 'AzleFloat64' = 'AzleFloat64'; - static _azleKind: 'AzleFloat64' = 'AzleFloat64'; + _azleKind = 'AzleFloat64' as const; + static _azleKind = 'AzleFloat64' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/ints/int.ts b/src/lib/candid/types/primitive/ints/int.ts index 67d6ddab52..e1004bfca6 100644 --- a/src/lib/candid/types/primitive/ints/int.ts +++ b/src/lib/candid/types/primitive/ints/int.ts @@ -4,8 +4,8 @@ import { decode } from '../../../serde/decode'; import { encode } from '../../../serde/encode'; export class AzleInt { - _azleKind: 'AzleInt' = 'AzleInt'; - static _azleKind: 'AzleInt' = 'AzleInt'; + _azleKind = 'AzleInt' as const; + static _azleKind = 'AzleInt' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/ints/int16.ts b/src/lib/candid/types/primitive/ints/int16.ts index 15be5009bd..0718fad663 100644 --- a/src/lib/candid/types/primitive/ints/int16.ts +++ b/src/lib/candid/types/primitive/ints/int16.ts @@ -4,8 +4,8 @@ import { decode } from '../../../serde/decode'; import { encode } from '../../../serde/encode'; export class AzleInt16 { - _azleKind: 'AzleInt16' = 'AzleInt16'; - static _azleKind: 'AzleInt16' = 'AzleInt16'; + _azleKind = 'AzleInt16' as const; + static _azleKind = 'AzleInt16' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/ints/int32.ts b/src/lib/candid/types/primitive/ints/int32.ts index 2e0e24515c..5e5c4413eb 100644 --- a/src/lib/candid/types/primitive/ints/int32.ts +++ b/src/lib/candid/types/primitive/ints/int32.ts @@ -4,8 +4,8 @@ import { decode } from '../../../serde/decode'; import { encode } from '../../../serde/encode'; export class AzleInt32 { - _azleKind: 'AzleInt32' = 'AzleInt32'; - static _azleKind: 'AzleInt32' = 'AzleInt32'; + _azleKind = 'AzleInt32' as const; + static _azleKind = 'AzleInt32' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/ints/int64.ts b/src/lib/candid/types/primitive/ints/int64.ts index 53fbec5bc9..3a2d6c9093 100644 --- a/src/lib/candid/types/primitive/ints/int64.ts +++ b/src/lib/candid/types/primitive/ints/int64.ts @@ -4,8 +4,8 @@ import { decode } from '../../../serde/decode'; import { encode } from '../../../serde/encode'; export class AzleInt64 { - _azleKind: 'AzleInt64' = 'AzleInt64'; - static _azleKind: 'AzleInt64' = 'AzleInt64'; + _azleKind = 'AzleInt64' as const; + static _azleKind = 'AzleInt64' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/ints/int8.ts b/src/lib/candid/types/primitive/ints/int8.ts index ba1832c92d..67ebb240a0 100644 --- a/src/lib/candid/types/primitive/ints/int8.ts +++ b/src/lib/candid/types/primitive/ints/int8.ts @@ -4,8 +4,8 @@ import { decode } from '../../../serde/decode'; import { encode } from '../../../serde/encode'; export class AzleInt8 { - _azleKind: 'AzleInt8' = 'AzleInt8'; - static _azleKind: 'AzleInt8' = 'AzleInt8'; + _azleKind = 'AzleInt8' as const; + static _azleKind = 'AzleInt8' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/nats/nat.ts b/src/lib/candid/types/primitive/nats/nat.ts index b32da6df41..08aa70bb15 100644 --- a/src/lib/candid/types/primitive/nats/nat.ts +++ b/src/lib/candid/types/primitive/nats/nat.ts @@ -4,8 +4,8 @@ import { decode } from '../../../serde/decode'; import { encode } from '../../../serde/encode'; export class AzleNat { - _azleKind: 'AzleNat' = 'AzleNat'; - static _azleKind: 'AzleNat' = 'AzleNat'; + _azleKind = 'AzleNat' as const; + static _azleKind = 'AzleNat' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/nats/nat16.ts b/src/lib/candid/types/primitive/nats/nat16.ts index a3b25b041b..ebda87a9e0 100644 --- a/src/lib/candid/types/primitive/nats/nat16.ts +++ b/src/lib/candid/types/primitive/nats/nat16.ts @@ -4,8 +4,8 @@ import { decode } from '../../../serde/decode'; import { encode } from '../../../serde/encode'; export class AzleNat16 { - _azleKind: 'AzleNat16' = 'AzleNat16'; - static _azleKind: 'AzleNat16' = 'AzleNat16'; + _azleKind = 'AzleNat16' as const; + static _azleKind = 'AzleNat16' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/nats/nat32.ts b/src/lib/candid/types/primitive/nats/nat32.ts index 3c1e3b91f1..09dd451598 100644 --- a/src/lib/candid/types/primitive/nats/nat32.ts +++ b/src/lib/candid/types/primitive/nats/nat32.ts @@ -4,8 +4,8 @@ import { decode } from '../../../serde/decode'; import { encode } from '../../../serde/encode'; export class AzleNat32 { - _azleKind: 'AzleNat32' = 'AzleNat32'; - static _azleKind: 'AzleNat32' = 'AzleNat32'; + _azleKind = 'AzleNat32' as const; + static _azleKind = 'AzleNat32' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/nats/nat64.ts b/src/lib/candid/types/primitive/nats/nat64.ts index 4b22358a66..373dd526ee 100644 --- a/src/lib/candid/types/primitive/nats/nat64.ts +++ b/src/lib/candid/types/primitive/nats/nat64.ts @@ -4,8 +4,8 @@ import { decode } from '../../../serde/decode'; import { encode } from '../../../serde/encode'; export class AzleNat64 { - _azleKind: 'AzleNat64' = 'AzleNat64'; - static _azleKind: 'AzleNat64' = 'AzleNat64'; + _azleKind = 'AzleNat64' as const; + static _azleKind = 'AzleNat64' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/nats/nat8.ts b/src/lib/candid/types/primitive/nats/nat8.ts index dd912acb21..238e2ad79d 100644 --- a/src/lib/candid/types/primitive/nats/nat8.ts +++ b/src/lib/candid/types/primitive/nats/nat8.ts @@ -4,8 +4,8 @@ import { decode } from '../../../serde/decode'; import { encode } from '../../../serde/encode'; export class AzleNat8 { - _azleKind: 'AzleNat8' = 'AzleNat8'; - static _azleKind: 'AzleNat8' = 'AzleNat8'; + _azleKind = 'AzleNat8' as const; + static _azleKind = 'AzleNat8' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/null.ts b/src/lib/candid/types/primitive/null.ts index e1f6931e1d..30de0becae 100644 --- a/src/lib/candid/types/primitive/null.ts +++ b/src/lib/candid/types/primitive/null.ts @@ -4,8 +4,8 @@ import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; export class AzleNull { - _azleKind: 'AzleNull' = 'AzleNull'; - static _azleKind: 'AzleNull' = 'AzleNull'; + _azleKind = 'AzleNull' as const; + static _azleKind = 'AzleNull' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/reserved.ts b/src/lib/candid/types/primitive/reserved.ts index dfc4bf636c..d22027e128 100644 --- a/src/lib/candid/types/primitive/reserved.ts +++ b/src/lib/candid/types/primitive/reserved.ts @@ -4,8 +4,8 @@ import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; export class AzleReserved { - _azleKind: 'AzleReserved' = 'AzleReserved'; - static _azleKind: 'AzleReserved' = 'AzleReserved'; + _azleKind = 'AzleReserved' as const; + static _azleKind = 'AzleReserved' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/text.ts b/src/lib/candid/types/primitive/text.ts index f894a4e94d..0d912c17a6 100644 --- a/src/lib/candid/types/primitive/text.ts +++ b/src/lib/candid/types/primitive/text.ts @@ -4,8 +4,8 @@ import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; export class AzleText { - _azleKind: 'AzleText' = 'AzleText'; - static _azleKind: 'AzleText' = 'AzleText'; + _azleKind = 'AzleText' as const; + static _azleKind = 'AzleText' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/void.ts b/src/lib/candid/types/primitive/void.ts index 7a63412574..481b68130f 100644 --- a/src/lib/candid/types/primitive/void.ts +++ b/src/lib/candid/types/primitive/void.ts @@ -4,8 +4,8 @@ import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; export class AzleVoid { - _azleKind: 'AzleVoid' = 'AzleVoid'; - static _azleKind: 'AzleVoid' = 'AzleVoid'; + _azleKind = 'AzleVoid' as const; + static _azleKind = 'AzleVoid' as const; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/reference/principal.ts b/src/lib/candid/types/reference/principal.ts index ed2f09d131..852118de6f 100644 --- a/src/lib/candid/types/reference/principal.ts +++ b/src/lib/candid/types/reference/principal.ts @@ -6,7 +6,7 @@ import { encode } from '../../serde/encode'; import { Parent } from '../../to_idl'; export class Principal extends DfinityPrincipal { - static _azleKind: 'Principal' = 'Principal'; + static _azleKind = 'Principal' as const; static toBytes(data: any) { return encode(this, data); From 86debb458b17c00e4d18d2689a592b504c99876d Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Fri, 1 Dec 2023 16:11:03 -0700 Subject: [PATCH 10/10] use underscores instead of removing params This way we can hover over them to see the types. --- examples/ic_api/src/index.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/examples/ic_api/src/index.ts b/examples/ic_api/src/index.ts index 1061becd8a..0357a0ade9 100644 --- a/examples/ic_api/src/index.ts +++ b/examples/ic_api/src/index.ts @@ -54,13 +54,21 @@ export default Canister({ // } // returns the argument data as bytes. - argDataRaw: query([blob, int8, bool, text], blob, () => { - return ic.argDataRaw(); - }), + argDataRaw: query( + [blob, int8, bool, text], + blob, + (_arg1, _arg2, _arg3, _arg4) => { + return ic.argDataRaw(); + } + ), // returns the length of the argument data in bytes - argDataRawSize: query([blob, int8, bool, text], nat32, () => { - return ic.argDataRawSize(); - }), + argDataRawSize: query( + [blob, int8, bool, text], + nat32, + (_arg1, _arg2, _arg3, _arg4) => { + return ic.argDataRawSize(); + } + ), // returns the principal of the identity that called this function caller: query([], Principal, () => { return ic.caller();