From e38d014306a014003fe4f906e71cc3b6da5a0e15 Mon Sep 17 00:00:00 2001 From: Daniyar Itegulov Date: Mon, 5 Dec 2022 20:02:47 +1100 Subject: [PATCH] fix: ensure that NEAR functions have explicit types (#322) * ensure that NEAR functions have explicit types --- examples/src/nested-collections.ts | 24 ++++++++++++++++++++---- lib/cli/abi.js | 3 +++ src/cli/abi.ts | 6 ++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/examples/src/nested-collections.ts b/examples/src/nested-collections.ts index 47f63278e..c94b33a61 100644 --- a/examples/src/nested-collections.ts +++ b/examples/src/nested-collections.ts @@ -11,7 +11,7 @@ export class Contract { } @call({}) - add({ id, text }) { + add({ id, text }: { id: string; text: string }) { const innerMap = this.outerMap.get(id, { reconstructor: UnorderedMap.reconstruct, defaultValue: new UnorderedMap("i_" + id + "_"), @@ -21,7 +21,7 @@ export class Contract { } @view({}) - get({ id, accountId }) { + get({ id, accountId }: { id: string; accountId: string }) { const innerMap = this.outerMap.get(id, { reconstructor: UnorderedMap.reconstruct, }); @@ -32,7 +32,15 @@ export class Contract { } @call({}) - add_to_group({ group, id, text }) { + add_to_group({ + group, + id, + text, + }: { + group: string; + id: string; + text: string; + }) { const groupMap = this.groups.get(group, { reconstructor: UnorderedMap.reconstruct, defaultValue: new UnorderedMap>("g_" + group + "_"), @@ -47,7 +55,15 @@ export class Contract { } @view({}) - get_from_group({ group, id, accountId }) { + get_from_group({ + group, + id, + accountId, + }: { + group: string; + id: string; + accountId: string; + }) { const groupMap = this.groups.get(group, { reconstructor: UnorderedMap.reconstruct, }); diff --git a/lib/cli/abi.js b/lib/cli/abi.js index 503d629eb..79ab8022a 100644 --- a/lib/cli/abi.js +++ b/lib/cli/abi.js @@ -150,6 +150,9 @@ export function runAbiCompilerPlugin(tsFile, packageJsonPath, tsConfigJsonPath) } else if (methodDeclaration.parameters.length === 1) { const jsonObjectParameter = methodDeclaration.parameters[0]; + if (!jsonObjectParameter.type) { + throw Error("Expected NEAR function to have explicit types, e.g. `{ id }: {id : string }`"); + } if (jsonObjectParameter.type.kind !== ts.SyntaxKind.TypeLiteral) { throw Error("Expected NEAR function to have a single object binding parameter, e.g. `{ id }: { id: string }`"); } diff --git a/src/cli/abi.ts b/src/cli/abi.ts index 8cf16181a..47c04abc9 100644 --- a/src/cli/abi.ts +++ b/src/cli/abi.ts @@ -159,6 +159,12 @@ export function runAbiCompilerPlugin( ); } else if (methodDeclaration.parameters.length === 1) { const jsonObjectParameter = methodDeclaration.parameters[0]; + if (!jsonObjectParameter.type) { + throw Error( + "Expected NEAR function to have explicit types, e.g. `{ id }: {id : string }`" + ); + } + if (jsonObjectParameter.type.kind !== ts.SyntaxKind.TypeLiteral) { throw Error( "Expected NEAR function to have a single object binding parameter, e.g. `{ id }: { id: string }`"