From b5009bcec8d53d8e9560a623f9aef0b4e13b9c1c Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 20 Jun 2024 20:37:49 -0700 Subject: [PATCH 1/2] useDeclareKeyword option --- README.md | 1 + .../ts-client.declare.test.ts.snap | 699 ++++++++++++++++++ .../client/ts-client.declare.test.ts | 67 ++ packages/ast/src/client/client.ts | 14 +- packages/ast/src/context/context.ts | 5 +- packages/ast/src/types/plugins.ts | 1 + packages/ast/src/utils/babel.ts | 24 +- packages/ts-codegen/README.md | 1 + packages/ts-codegen/test-utils/index.ts | 1 + 9 files changed, 797 insertions(+), 16 deletions(-) create mode 100644 packages/ast/__tests__/client/__snapshots__/ts-client.declare.test.ts.snap create mode 100644 packages/ast/__tests__/client/ts-client.declare.test.ts diff --git a/README.md b/README.md index 435b06ab..019b7172 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ The `client` plugin will generate TS client classes for your contracts. This opt | `client.enabled` | generate TS client classes for your contracts | | `client.execExtendsQuery` | execute should extend query message clients | | `client.noImplicitOverride` | should match your tsconfig noImplicitOverride option | +| `client.useDeclareKeyword` | use declare keyword for inherited class fields | ### React Query diff --git a/packages/ast/__tests__/client/__snapshots__/ts-client.declare.test.ts.snap b/packages/ast/__tests__/client/__snapshots__/ts-client.declare.test.ts.snap new file mode 100644 index 00000000..6c1a8bc6 --- /dev/null +++ b/packages/ast/__tests__/client/__snapshots__/ts-client.declare.test.ts.snap @@ -0,0 +1,699 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`noDeclare, execExtends, ExtendsClass 1`] = ` +"export class SG721Client extends ExtendsClassName implements SG721Instance { + client: SigningCosmWasmClient; + sender: string; + contractAddress: string; + constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) { + super(client, contractAddress); + this.client = client; + this.sender = sender; + this.contractAddress = contractAddress; + this.transferNft = this.transferNft.bind(this); + this.sendNft = this.sendNft.bind(this); + this.approve = this.approve.bind(this); + this.revoke = this.revoke.bind(this); + this.approveAll = this.approveAll.bind(this); + this.revokeAll = this.revokeAll.bind(this); + this.mint = this.mint.bind(this); + this.burn = this.burn.bind(this); + } + transferNft = async ({ + recipient, + tokenId + }: { + recipient: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + transfer_nft: { + recipient, + token_id: tokenId + } + }, fee, memo, _funds); + }; + sendNft = async ({ + contract, + msg, + tokenId + }: { + contract: string; + msg: Binary; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + send_nft: { + contract, + msg, + token_id: tokenId + } + }, fee, memo, _funds); + }; + approve = async ({ + expires, + spender, + tokenId + }: { + expires?: Expiration; + spender: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + approve: { + expires, + spender, + token_id: tokenId + } + }, fee, memo, _funds); + }; + revoke = async ({ + spender, + tokenId + }: { + spender: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + revoke: { + spender, + token_id: tokenId + } + }, fee, memo, _funds); + }; + approveAll = async ({ + expires, + operator + }: { + expires?: Expiration; + operator: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + approve_all: { + expires, + operator + } + }, fee, memo, _funds); + }; + revokeAll = async ({ + operator + }: { + operator: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + revoke_all: { + operator + } + }, fee, memo, _funds); + }; + mint = async ({ + extension, + owner, + tokenId, + tokenUri + }: { + extension: Empty; + owner: string; + tokenId: string; + tokenUri?: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + mint: { + extension, + owner, + token_id: tokenId, + token_uri: tokenUri + } + }, fee, memo, _funds); + }; + burn = async ({ + tokenId + }: { + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + burn: { + token_id: tokenId + } + }, fee, memo, _funds); + }; +}" +`; + +exports[`noDeclare, execExtends, noExtendsClass 1`] = ` +"export class SG721Client implements SG721Instance { + client: SigningCosmWasmClient; + sender: string; + contractAddress: string; + constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) { + this.client = client; + this.sender = sender; + this.contractAddress = contractAddress; + this.transferNft = this.transferNft.bind(this); + this.sendNft = this.sendNft.bind(this); + this.approve = this.approve.bind(this); + this.revoke = this.revoke.bind(this); + this.approveAll = this.approveAll.bind(this); + this.revokeAll = this.revokeAll.bind(this); + this.mint = this.mint.bind(this); + this.burn = this.burn.bind(this); + } + transferNft = async ({ + recipient, + tokenId + }: { + recipient: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + transfer_nft: { + recipient, + token_id: tokenId + } + }, fee, memo, _funds); + }; + sendNft = async ({ + contract, + msg, + tokenId + }: { + contract: string; + msg: Binary; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + send_nft: { + contract, + msg, + token_id: tokenId + } + }, fee, memo, _funds); + }; + approve = async ({ + expires, + spender, + tokenId + }: { + expires?: Expiration; + spender: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + approve: { + expires, + spender, + token_id: tokenId + } + }, fee, memo, _funds); + }; + revoke = async ({ + spender, + tokenId + }: { + spender: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + revoke: { + spender, + token_id: tokenId + } + }, fee, memo, _funds); + }; + approveAll = async ({ + expires, + operator + }: { + expires?: Expiration; + operator: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + approve_all: { + expires, + operator + } + }, fee, memo, _funds); + }; + revokeAll = async ({ + operator + }: { + operator: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + revoke_all: { + operator + } + }, fee, memo, _funds); + }; + mint = async ({ + extension, + owner, + tokenId, + tokenUri + }: { + extension: Empty; + owner: string; + tokenId: string; + tokenUri?: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + mint: { + extension, + owner, + token_id: tokenId, + token_uri: tokenUri + } + }, fee, memo, _funds); + }; + burn = async ({ + tokenId + }: { + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + burn: { + token_id: tokenId + } + }, fee, memo, _funds); + }; +}" +`; + +exports[`useDeclare, execExtends, ExtendsClass 1`] = ` +"export class SG721Client extends ExtendsClassName implements SG721Instance { + declare client: SigningCosmWasmClient; + sender: string; + declare contractAddress: string; + constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) { + super(client, contractAddress); + this.client = client; + this.sender = sender; + this.contractAddress = contractAddress; + this.transferNft = this.transferNft.bind(this); + this.sendNft = this.sendNft.bind(this); + this.approve = this.approve.bind(this); + this.revoke = this.revoke.bind(this); + this.approveAll = this.approveAll.bind(this); + this.revokeAll = this.revokeAll.bind(this); + this.mint = this.mint.bind(this); + this.burn = this.burn.bind(this); + } + transferNft = async ({ + recipient, + tokenId + }: { + recipient: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + transfer_nft: { + recipient, + token_id: tokenId + } + }, fee, memo, _funds); + }; + sendNft = async ({ + contract, + msg, + tokenId + }: { + contract: string; + msg: Binary; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + send_nft: { + contract, + msg, + token_id: tokenId + } + }, fee, memo, _funds); + }; + approve = async ({ + expires, + spender, + tokenId + }: { + expires?: Expiration; + spender: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + approve: { + expires, + spender, + token_id: tokenId + } + }, fee, memo, _funds); + }; + revoke = async ({ + spender, + tokenId + }: { + spender: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + revoke: { + spender, + token_id: tokenId + } + }, fee, memo, _funds); + }; + approveAll = async ({ + expires, + operator + }: { + expires?: Expiration; + operator: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + approve_all: { + expires, + operator + } + }, fee, memo, _funds); + }; + revokeAll = async ({ + operator + }: { + operator: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + revoke_all: { + operator + } + }, fee, memo, _funds); + }; + mint = async ({ + extension, + owner, + tokenId, + tokenUri + }: { + extension: Empty; + owner: string; + tokenId: string; + tokenUri?: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + mint: { + extension, + owner, + token_id: tokenId, + token_uri: tokenUri + } + }, fee, memo, _funds); + }; + burn = async ({ + tokenId + }: { + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + burn: { + token_id: tokenId + } + }, fee, memo, _funds); + }; +}" +`; + +exports[`useDeclare, noExecExtends, ExtendsClass 1`] = ` +"export class SG721Client extends ExtendsClassName implements SG721Instance { + client: SigningCosmWasmClient; + sender: string; + contractAddress: string; + constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) { + super(client, contractAddress); + this.client = client; + this.sender = sender; + this.contractAddress = contractAddress; + this.transferNft = this.transferNft.bind(this); + this.sendNft = this.sendNft.bind(this); + this.approve = this.approve.bind(this); + this.revoke = this.revoke.bind(this); + this.approveAll = this.approveAll.bind(this); + this.revokeAll = this.revokeAll.bind(this); + this.mint = this.mint.bind(this); + this.burn = this.burn.bind(this); + } + transferNft = async ({ + recipient, + tokenId + }: { + recipient: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + transfer_nft: { + recipient, + token_id: tokenId + } + }, fee, memo, _funds); + }; + sendNft = async ({ + contract, + msg, + tokenId + }: { + contract: string; + msg: Binary; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + send_nft: { + contract, + msg, + token_id: tokenId + } + }, fee, memo, _funds); + }; + approve = async ({ + expires, + spender, + tokenId + }: { + expires?: Expiration; + spender: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + approve: { + expires, + spender, + token_id: tokenId + } + }, fee, memo, _funds); + }; + revoke = async ({ + spender, + tokenId + }: { + spender: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + revoke: { + spender, + token_id: tokenId + } + }, fee, memo, _funds); + }; + approveAll = async ({ + expires, + operator + }: { + expires?: Expiration; + operator: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + approve_all: { + expires, + operator + } + }, fee, memo, _funds); + }; + revokeAll = async ({ + operator + }: { + operator: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + revoke_all: { + operator + } + }, fee, memo, _funds); + }; + mint = async ({ + extension, + owner, + tokenId, + tokenUri + }: { + extension: Empty; + owner: string; + tokenId: string; + tokenUri?: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + mint: { + extension, + owner, + token_id: tokenId, + token_uri: tokenUri + } + }, fee, memo, _funds); + }; + burn = async ({ + tokenId + }: { + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + burn: { + token_id: tokenId + } + }, fee, memo, _funds); + }; +}" +`; + +exports[`useDeclare, noExecExtends, noExtendsClass 1`] = ` +"export class SG721Client implements SG721Instance { + client: SigningCosmWasmClient; + sender: string; + contractAddress: string; + constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) { + this.client = client; + this.sender = sender; + this.contractAddress = contractAddress; + this.transferNft = this.transferNft.bind(this); + this.sendNft = this.sendNft.bind(this); + this.approve = this.approve.bind(this); + this.revoke = this.revoke.bind(this); + this.approveAll = this.approveAll.bind(this); + this.revokeAll = this.revokeAll.bind(this); + this.mint = this.mint.bind(this); + this.burn = this.burn.bind(this); + } + transferNft = async ({ + recipient, + tokenId + }: { + recipient: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + transfer_nft: { + recipient, + token_id: tokenId + } + }, fee, memo, _funds); + }; + sendNft = async ({ + contract, + msg, + tokenId + }: { + contract: string; + msg: Binary; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + send_nft: { + contract, + msg, + token_id: tokenId + } + }, fee, memo, _funds); + }; + approve = async ({ + expires, + spender, + tokenId + }: { + expires?: Expiration; + spender: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + approve: { + expires, + spender, + token_id: tokenId + } + }, fee, memo, _funds); + }; + revoke = async ({ + spender, + tokenId + }: { + spender: string; + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + revoke: { + spender, + token_id: tokenId + } + }, fee, memo, _funds); + }; + approveAll = async ({ + expires, + operator + }: { + expires?: Expiration; + operator: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + approve_all: { + expires, + operator + } + }, fee, memo, _funds); + }; + revokeAll = async ({ + operator + }: { + operator: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + revoke_all: { + operator + } + }, fee, memo, _funds); + }; + mint = async ({ + extension, + owner, + tokenId, + tokenUri + }: { + extension: Empty; + owner: string; + tokenId: string; + tokenUri?: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + mint: { + extension, + owner, + token_id: tokenId, + token_uri: tokenUri + } + }, fee, memo, _funds); + }; + burn = async ({ + tokenId + }: { + tokenId: string; + }, fee: number | StdFee | "auto" = "auto", memo?: string, _funds?: Coin[]): Promise => { + return await this.client.execute(this.sender, this.contractAddress, { + burn: { + token_id: tokenId + } + }, fee, memo, _funds); + }; +}" +`; diff --git a/packages/ast/__tests__/client/ts-client.declare.test.ts b/packages/ast/__tests__/client/ts-client.declare.test.ts new file mode 100644 index 00000000..99262cf8 --- /dev/null +++ b/packages/ast/__tests__/client/ts-client.declare.test.ts @@ -0,0 +1,67 @@ +import { + createExecuteClass +} from '../../src'; +import { expectCode, getMsgExecuteLegacyFixture, makeContext } from '../../test-utils'; + +const execMsg = getMsgExecuteLegacyFixture('sg721', '/execute_msg_for__empty.json'); +const ctx = makeContext(execMsg); + +it('noDeclare, execExtends, noExtendsClass', () => { + ctx.options.client.useDeclareKeyword = false; + ctx.options.client.execExtendsQuery = true; + expectCode(createExecuteClass( + ctx, + 'SG721Client', + 'SG721Instance', + null, + execMsg + )); +}); + +it('noDeclare, execExtends, ExtendsClass', () => { + ctx.options.client.useDeclareKeyword = false; + ctx.options.client.execExtendsQuery = true; + expectCode(createExecuteClass( + ctx, + 'SG721Client', + 'SG721Instance', + 'ExtendsClassName', + execMsg + )); +}); + +it('useDeclare, execExtends, ExtendsClass', () => { + ctx.options.client.useDeclareKeyword = true; + ctx.options.client.execExtendsQuery = true; + expectCode(createExecuteClass( + ctx, + 'SG721Client', + 'SG721Instance', + 'ExtendsClassName', + execMsg + )); +}); + +it('useDeclare, noExecExtends, ExtendsClass', () => { + ctx.options.client.useDeclareKeyword = true; + ctx.options.client.execExtendsQuery = false; + expectCode(createExecuteClass( + ctx, + 'SG721Client', + 'SG721Instance', + 'ExtendsClassName', + execMsg + )); +}); + +it('useDeclare, noExecExtends, noExtendsClass', () => { + ctx.options.client.useDeclareKeyword = true; + ctx.options.client.execExtendsQuery = false; + expectCode(createExecuteClass( + ctx, + 'SG721Client', + 'SG721Instance', + null, + execMsg + )); +}); diff --git a/packages/ast/src/client/client.ts b/packages/ast/src/client/client.ts index 43cd0cc7..8fe44711 100644 --- a/packages/ast/src/client/client.ts +++ b/packages/ast/src/client/client.ts @@ -13,7 +13,8 @@ import { getTypeOrRef, OPTIONAL_FUNDS_PARAM, promiseTypeAnnotation, - typedIdentifier} from '../utils'; + typedIdentifier +} from '../utils'; import { identifier, propertySignature } from '../utils/babel'; import { createTypedObjectParams, @@ -351,6 +352,11 @@ export const createExecuteClass = ( extendsClassName && context.options.client.execExtendsQuery; + const useDeclareKeyword = + context.options.client.useDeclareKeyword && + extendsClassName && + context.options.client.execExtendsQuery; + return t.exportNamedDeclaration( classDeclaration( className, @@ -363,7 +369,8 @@ export const createExecuteClass = ( ), false, false, - noImplicitOverride + noImplicitOverride, + useDeclareKeyword ), // sender @@ -375,7 +382,8 @@ export const createExecuteClass = ( t.tsTypeAnnotation(t.tsStringKeyword()), false, false, - noImplicitOverride + noImplicitOverride, + useDeclareKeyword ), // constructor diff --git a/packages/ast/src/context/context.ts b/packages/ast/src/context/context.ts index 3e829ffc..555abc4e 100644 --- a/packages/ast/src/context/context.ts +++ b/packages/ast/src/context/context.ts @@ -36,6 +36,7 @@ export const defaultOptions: RenderOptions = { enabled: true, execExtendsQuery: true, noImplicitOverride: false, + useDeclareKeyword: false, }, recoil: { enabled: false @@ -99,7 +100,7 @@ export class BuilderContext { [key: string]: { [key: string]: ProviderInfo; }; - } { + } { return this.providers; } } @@ -144,7 +145,7 @@ export abstract class RenderContextBase implements IRender [key: string]: { [key: string]: ProviderInfo; }; - } { + } { return this.builderContext.providers; } getImports(registeredUtils?: UtilMapping, filepath?: string): (t.ImportNamespaceSpecifier | t.ImportDeclaration | t.ImportDefaultSpecifier)[] { diff --git a/packages/ast/src/types/plugins.ts b/packages/ast/src/types/plugins.ts index f34e4625..ae75606c 100644 --- a/packages/ast/src/types/plugins.ts +++ b/packages/ast/src/types/plugins.ts @@ -12,6 +12,7 @@ export interface TSClientOptions { enabled?: boolean; execExtendsQuery?: boolean; noImplicitOverride?: boolean; + useDeclareKeyword?: boolean; } export interface MessageComposerOptions { enabled?: boolean; diff --git a/packages/ast/src/utils/babel.ts b/packages/ast/src/utils/babel.ts index 8e87ff75..ac140351 100644 --- a/packages/ast/src/utils/babel.ts +++ b/packages/ast/src/utils/babel.ts @@ -97,18 +97,18 @@ export const bindMethod = (name: string) => { t.thisExpression(), t.identifier(name) ), - t.callExpression( - t.memberExpression( + t.callExpression( t.memberExpression( - t.thisExpression(), - t.identifier(name) + t.memberExpression( + t.thisExpression(), + t.identifier(name) + ), + t.identifier('bind') ), - t.identifier('bind') - ), - [ - t.thisExpression() - ] - ) + [ + t.thisExpression() + ] + ) ) ); }; @@ -162,13 +162,15 @@ export const classProperty = ( typeAnnotation: TSTypeAnnotation = null, isReadonly: boolean = false, isStatic: boolean = false, - noImplicitOverride: boolean = false + noImplicitOverride: boolean = false, + useDeclareKeyword: boolean = false, ) => { const prop = t.classProperty(t.identifier(name)); if (isReadonly) prop.readonly = true; if (isStatic) prop.static = true; if (typeAnnotation) prop.typeAnnotation = typeAnnotation; if (noImplicitOverride) prop.override = true; + if (useDeclareKeyword) prop.declare = true; return prop; }; diff --git a/packages/ts-codegen/README.md b/packages/ts-codegen/README.md index 435b06ab..019b7172 100644 --- a/packages/ts-codegen/README.md +++ b/packages/ts-codegen/README.md @@ -132,6 +132,7 @@ The `client` plugin will generate TS client classes for your contracts. This opt | `client.enabled` | generate TS client classes for your contracts | | `client.execExtendsQuery` | execute should extend query message clients | | `client.noImplicitOverride` | should match your tsconfig noImplicitOverride option | +| `client.useDeclareKeyword` | use declare keyword for inherited class fields | ### React Query diff --git a/packages/ts-codegen/test-utils/index.ts b/packages/ts-codegen/test-utils/index.ts index 3b22497b..e9fe52fb 100644 --- a/packages/ts-codegen/test-utils/index.ts +++ b/packages/ts-codegen/test-utils/index.ts @@ -31,6 +31,7 @@ export const testDefaultOptions: RenderOptions = { enabled: true, execExtendsQuery: true, noImplicitOverride: false, + useDeclareKeyword: false, }, recoil: { enabled: true, From 6c4d0805c9e2881297d5e6234d92f2b3b272210c Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Thu, 20 Jun 2024 20:46:14 -0700 Subject: [PATCH 2/2] snap --- .../__snapshots__/builder.test.ts.snap | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/ts-codegen/__tests__/__snapshots__/builder.test.ts.snap b/packages/ts-codegen/__tests__/__snapshots__/builder.test.ts.snap index 5d15369b..517f6db3 100644 --- a/packages/ts-codegen/__tests__/__snapshots__/builder.test.ts.snap +++ b/packages/ts-codegen/__tests__/__snapshots__/builder.test.ts.snap @@ -16,6 +16,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -60,6 +61,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -105,6 +107,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -150,6 +153,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -195,6 +199,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -240,6 +245,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -287,6 +293,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -332,6 +339,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -381,6 +389,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -438,6 +447,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -482,6 +492,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -527,6 +538,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -572,6 +584,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -617,6 +630,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -662,6 +676,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -709,6 +724,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -754,6 +770,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": { @@ -803,6 +820,7 @@ TSBuilder { "enabled": true, "execExtendsQuery": true, "noImplicitOverride": false, + "useDeclareKeyword": false, }, "enabled": true, "messageBuilder": {