Skip to content

Commit

Permalink
style: linted
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Dec 12, 2023
1 parent bf4a738 commit bc85844
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 50 deletions.
33 changes: 13 additions & 20 deletions src/commands/contract/finders.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Antelope from '@wharfkit/antelope'
import { ABI } from "@wharfkit/antelope"
import { capitalize, extractDecorator, formatInternalType, parseType, trim } from "./helpers"
import { formatClassName } from "../../utils"
import type {ABI} from '@wharfkit/antelope'
import {capitalize, extractDecorator, formatInternalType, parseType, trim} from './helpers'
import {formatClassName} from '../../utils'

const ANTELOPE_CLASSES: string[] = []
Object.keys(Antelope).map((key) => {
Expand All @@ -14,10 +14,7 @@ export const ANTELOPE_CLASS_MAPPINGS = {
block_timestamp_type: 'BlockTimestamp',
}

export const ANTELOPE_CLASS_WITHOUT_TYPES = [
'BlockTimestamp',
'TimePointSec',
]
export const ANTELOPE_CLASS_WITHOUT_TYPES = ['BlockTimestamp', 'TimePointSec']

export function findTypeFromAlias(typeString: string, abi: ABI.Def): string | undefined {
const {type: typeStringWithoutDecorator, decorator} = extractDecorator(typeString)
Expand All @@ -29,33 +26,29 @@ export function findTypeFromAlias(typeString: string, abi: ABI.Def): string | un
export function findAliasFromType(typeString: string, abi: ABI.Def): string | undefined {
const {type: typeStringWithoutDecorator, decorator} = extractDecorator(typeString)

const alias = abi.types.find((type) => type.type === typeStringWithoutDecorator ||
type.type === `${typeStringWithoutDecorator}[]`)
const alias = abi.types.find(
(type) =>
type.type === typeStringWithoutDecorator ||
type.type === `${typeStringWithoutDecorator}[]`
)

return alias?.new_type_name && `${alias?.new_type_name}${decorator || ''}`

}

export function findAbiStruct(
type: string,
abi: ABI.Def,
): ABI.Struct | undefined {
export function findAbiStruct(type: string, abi: ABI.Def): ABI.Struct | undefined {
const extractDecoratorResponse = extractDecorator(type)
const typeString = extractDecoratorResponse.type

const aliasType = findTypeFromAlias(typeString, abi)

let abiStruct = abi.structs.find(
const abiStruct = abi.structs.find(
(abiType) => abiType.name === extractDecorator(aliasType || typeString).type
)

return abiStruct
}

export function findVariant(
typeString: string,
abi: ABI.Def
): ABI.Variant | undefined {
export function findVariant(typeString: string, abi: ABI.Def): ABI.Variant | undefined {
const {type: typeStringWithoutDecorator} = extractDecorator(typeString)

const aliastype = findTypeFromAlias(typeStringWithoutDecorator, abi)
Expand Down Expand Up @@ -141,4 +134,4 @@ export function findInternalType(

// TODO: inside findType, namespace is prefixed, but format internal is doing the same
return formatInternalType(typeString, typeNamespace, abi, decorator)
}
}
37 changes: 21 additions & 16 deletions src/commands/contract/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {ABI} from '@wharfkit/antelope'
import * as ts from 'typescript'
import {formatClassName} from '../../utils'
import {findAbiType, findTypeFromAlias, findCoreClass, findCoreType, findVariant, findAliasFromType} from './finders'
import { TypeInterfaceDeclaration } from './interfaces'
import {findAbiType, findAliasFromType, findCoreClass, findCoreType, findVariant} from './finders'
import type {TypeInterfaceDeclaration} from './interfaces'

export function getCoreImports(abi: ABI.Def) {
const coreImports: string[] = []
Expand All @@ -12,7 +12,7 @@ export function getCoreImports(abi: ABI.Def) {
for (const field of struct.fields) {
const variant = findVariant(field.type, abi)

for (const type of (variant?.types || [field.type])) {
for (const type of variant?.types || [field.type]) {
const fieldTypeWithoutDecorator = extractDecorator(type).type

const fieldTypeIsStruct = abi.structs.find(
Expand Down Expand Up @@ -81,15 +81,18 @@ function structIsUsedInActionParams(struct: ABI.Struct, abi: ABI.Def) {
const alias = findAliasFromType(struct.name, abi)

const structsUsingStruct = abi.structs.filter((abiStruct) => {
return abiStruct.fields.some((field) => extractDecorator(field.type).type === (alias || struct.name))
return abiStruct.fields.some(
(field) => extractDecorator(field.type).type === (alias || struct.name)
)
})

if (structsUsingStruct.length === 0) {
return false
}

isUsedByActionStruct =
abi.actions.some((action) => structsUsingStruct.map(s => s.name).includes(action.type))
isUsedByActionStruct = abi.actions.some((action) =>
structsUsingStruct.map((s) => s.name).includes(action.type)
)

if (!isUsedByActionStruct) {
structsUsingStruct.forEach((structUsingStruct) => {
Expand Down Expand Up @@ -249,20 +252,22 @@ export function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}

export function removeDuplicateInterfaces(interfaces: TypeInterfaceDeclaration[]): TypeInterfaceDeclaration[] {
const seen: string[] = [];
export function removeDuplicateInterfaces(
interfaces: TypeInterfaceDeclaration[]
): TypeInterfaceDeclaration[] {
const seen: string[] = []

return interfaces.filter(interfaceDeclaration => {
const name = String(interfaceDeclaration.name.escapedText);
return interfaces.filter((interfaceDeclaration) => {
const name = String(interfaceDeclaration.name.escapedText)

if (seen.includes(name)) {
return false;
return false
}
seen.push(name);
return true;
});
seen.push(name)
return true
})
}

export function removeCommas(interfaceName: string): string {
return interfaceName.replace(/\./g, '');
}
return interfaceName.replace(/\./g, '')
}
25 changes: 13 additions & 12 deletions src/commands/contract/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {ABI} from '@wharfkit/antelope'
import ts from 'typescript'
import {parseType, removeCommas, removeDuplicateInterfaces} from './helpers'
import {getActionFieldFromAbi} from './structs'
import { findAbiStruct, findTypeFromAlias, findExternalType, findVariant } from './finders'
import {findAbiStruct, findExternalType, findTypeFromAlias, findVariant} from './finders'

export function generateActionNamesInterface(abi: ABI.Def): ts.InterfaceDeclaration {
// Generate property signatures for each action
Expand All @@ -29,7 +29,10 @@ export function generateActionNamesInterface(abi: ABI.Def): ts.InterfaceDeclarat

export type TypeInterfaceDeclaration = ts.InterfaceDeclaration | ts.TypeAliasDeclaration

export function generateActionInterface(struct, abi): { actionInterface: ts.InterfaceDeclaration, typeInterfaces: TypeInterfaceDeclaration[] } {
export function generateActionInterface(
struct,
abi
): {actionInterface: ts.InterfaceDeclaration; typeInterfaces: TypeInterfaceDeclaration[]} {
const typeInterfaces: TypeInterfaceDeclaration[] = []

const members = struct.fields.map((field) => {
Expand All @@ -42,18 +45,18 @@ export function generateActionInterface(struct, abi): { actionInterface: ts.Inte
if (abiVariant) {
types = abiVariant.types

variantType = `${struct.name}_${field.name}_variant`;
variantType = `${struct.name}_${field.name}_variant`

const variantTypeNodes = types.map((type) =>
const variantTypeNodes = types.map((type) =>
ts.factory.createTypeReferenceNode(findExternalType(type, 'Types.', abi))
);
)
const variantTypeAlias = ts.factory.createTypeAliasDeclaration(
undefined,
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
variantType,
undefined,
ts.factory.createUnionTypeNode(variantTypeNodes)
);
)

typeInterfaces.push(variantTypeAlias)
} else {
Expand All @@ -67,7 +70,7 @@ export function generateActionInterface(struct, abi): { actionInterface: ts.Inte

if (typeStruct) {
const interfaces = generateActionInterface(typeStruct, abi)

typeInterfaces.push(interfaces.actionInterface, ...interfaces.typeInterfaces)
}
})
Expand All @@ -92,7 +95,7 @@ export function generateActionInterface(struct, abi): { actionInterface: ts.Inte
members
)

return { actionInterface, typeInterfaces: removeDuplicateInterfaces(typeInterfaces) }
return {actionInterface, typeInterfaces: removeDuplicateInterfaces(typeInterfaces)}
}

export function generateActionsNamespace(abi: ABI.Def): ts.ModuleDeclaration {
Expand All @@ -110,16 +113,14 @@ export function generateActionsNamespace(abi: ABI.Def): ts.ModuleDeclaration {
if (interfaces.actionInterface) {
typeInterfaces.push(...interfaces.typeInterfaces)
}

return interfaces.actionInterface
})

const actionParamsTypes = ts.factory.createModuleDeclaration(
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
ts.factory.createIdentifier('Types'),
ts.factory.createModuleBlock(
removeDuplicateInterfaces(typeInterfaces)
),
ts.factory.createModuleBlock(removeDuplicateInterfaces(typeInterfaces)),
ts.NodeFlags.Namespace
)

Expand Down
3 changes: 1 addition & 2 deletions src/commands/contract/structs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type {ABI} from '@wharfkit/session'
import ts from 'typescript'
import {capitalize} from '@wharfkit/contract'
import {extractDecorator, parseType} from './helpers'
import {formatClassName} from '../../utils'
import { findInternalType } from './finders'
import {findInternalType} from './finders'

interface FieldType {
name: string
Expand Down

0 comments on commit bc85844

Please sign in to comment.