Skip to content

Commit

Permalink
fix: handling names with periods
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Nov 7, 2023
1 parent 80252b7 commit e9ac6fa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
12 changes: 3 additions & 9 deletions src/commands/contract/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Antelope from '@wharfkit/antelope'
import type {ABI} from '@wharfkit/antelope'
import * as ts from 'typescript'
import { capitalizeName } from '../../utils'

const ANTELOPE_CLASSES: string[] = []
Object.keys(Antelope).map((key) => {
Expand Down Expand Up @@ -185,21 +186,14 @@ function formatInternalType(
let type

if (structNames.includes(typeString.toLowerCase())) {
type = `${namespace}${generateStructClassName(typeString)}`
type = `${namespace}${capitalizeName(typeString)}`
} else {
type = findCoreClass(typeString) || capitalize(typeString)
}

return `${type}${decorator}`
}

export function generateStructClassName(name) {
return name
.split('_')
.map((word) => capitalize(word))
.join('')
}

function findAliasType(typeString: string, abi: ABI.Def): string | undefined {
const {type: typeStringWithoutDecorator, decorator} = extractDecorator(typeString)
const alias = abi.types.find((type) => type.new_type_name === typeStringWithoutDecorator)
Expand Down Expand Up @@ -259,7 +253,7 @@ export function findAbiType(
const abiType = abi.structs.find((abiType) => abiType.name === typeString)?.name

if (abiType) {
return {type: `${typeNamespace}${generateStructClassName(abiType)}`, decorator}
return {type: `${typeNamespace}${capitalizeName(abiType)}`, decorator}
}

return {type: typeString, decorator}
Expand Down
12 changes: 7 additions & 5 deletions src/commands/contract/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type {ABI} from '@wharfkit/antelope'
import ts from 'typescript'
import {capitalize} from '@wharfkit/contract'
import {findExternalType, parseType} from './helpers'
import {getActionFieldFromAbi} from './structs'
import { capitalizeName } from '../../utils'

export function generateActionNamesInterface(abi: ABI.Def): ts.InterfaceDeclaration {
// Generate property signatures for each action
const members = abi.actions.map((action) => {
const actionName = String(action.name)
const actionNameCapitalized = capitalize(actionName)
let actionName = String(action.name)
const actionNameKey = actionName.includes('.') ? `'${actionName}'` : actionName;

const actionNameCapitalized = capitalizeName(actionName)

return ts.factory.createPropertySignature(
undefined,
actionName,
actionNameKey,
undefined,
ts.factory.createTypeReferenceNode(`ActionParams.${actionNameCapitalized}`)
)
Expand Down Expand Up @@ -43,7 +45,7 @@ export function generateActionInterface(actionStruct, abi): ts.InterfaceDeclarat

return ts.factory.createInterfaceDeclaration(
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
capitalize(actionStruct.structName),
capitalizeName(actionStruct.structName),
undefined,
undefined,
members
Expand Down
5 changes: 3 additions & 2 deletions src/commands/contract/structs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {ABI} from '@wharfkit/session'
import ts from 'typescript'
import {capitalize} from '@wharfkit/contract'
import {extractDecorator, findInternalType, generateStructClassName, parseType} from './helpers'
import {extractDecorator, findInternalType, parseType} from './helpers'
import { capitalizeName } from '../../utils'

interface FieldType {
name: string
Expand Down Expand Up @@ -73,7 +74,7 @@ export function generateStruct(struct, abi, isExport = false): ts.ClassDeclarati
isExport
? [...decorators, ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)]
: decorators,
ts.factory.createIdentifier(generateStructClassName(struct.structName)),
ts.factory.createIdentifier(capitalizeName(struct.structName)),
undefined, // typeParameters
[
ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {APIClient, FetchProvider} from '@wharfkit/antelope'
import { capitalize } from '@wharfkit/contract'
import fetch from 'node-fetch'

type logLevel = 'info' | 'debug'
Expand All @@ -13,3 +14,10 @@ export function log(message, level: logLevel = 'debug') {
process.stdout.write(`${message}\n`)
}
}

export function capitalizeName(text: string) {
return text
.split(/[._]/)
.map((part) => capitalize(part))
.join('');
}

0 comments on commit e9ac6fa

Please sign in to comment.