Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | 5130x 1x 1x 1x 1x 1x 16x 16x 16x 16x 581x 3x 578x 16x 1x 32x 32x 32x 6x 90x 32x 32x 1156x 1156x 3790x 1156x 32x 1x 45x 3x 3x 45x 3x 1x 578x 578x 578x 578x 1895x 578x 1x 1895x 1895x 1895x 1895x 1895x 1895x 1895x 178x 1895x 17x 1895x 42x 1895x 219x 219x 1895x 1895x 1895x 178x 1717x 1895x 1x 16x 16x 581x 581x 16x 35949x 1x 842x 842x 172x 13x 842x 3159x 237194x 3159x 10710x 2896x 2896x 9502x 3159x 658x 3159x 40x 3119x 261x 261x 842x 1x 45x 45x 4x 41x 41x 18x 18x 18x 18x 18x 23x 1x 1895x 1895x 123x 1772x 1x 3909x 3909x 3909x 140x 3769x 114x 3655x 52x 3603x 2x 3601x 2x 127x 57x 70x | import type {ABI} from '@wharfkit/antelope'
import ts from 'typescript'
import {extractDecorator, parseType} from './helpers'
import {formatClassName} from '../../utils'
import {findInternalType} from './finders'
interface FieldType {
name: string
type: string
optional: boolean
extension: boolean
}
export interface StructData {
name: string
base?: string
fields: FieldType[]
variant: boolean
}
interface TypeAlias {
new_type_name: string
type: string
}
export function generateStructClasses(abi) {
const structs = getActionFieldFromAbi(abi)
const orderedStructs = orderStructs(structs, abi.types)
const structMembers: ts.ClassDeclaration[] = []
for (const struct of orderedStructs) {
if (struct.variant) {
structMembers.push(generateVariant(struct, abi, true))
} else {
structMembers.push(generateStruct(struct, abi, true))
}
}
return structMembers
}
export function getActionFieldFromAbi(abi: any): StructData[] {
const structTypes: StructData[] = []
if (abi && abi.variants) {
for (const variant of abi.variants) {
structTypes.push({
name: variant.name,
fields: variant.types.map((t) => {
return {
name: 'value',
type: t,
optional: false,
extension: false,
}
}),
variant: true,
})
}
}
if (abi && abi.structs) {
for (const struct of abi.structs) {
const fields: FieldType[] = []
for (const field of struct.fields) {
fields.push({
name: field.name,
type: parseType(field.type),
optional: field.type.endsWith('?') || field.type.endsWith('$'),
extension: field.type.endsWith('$'),
})
}
structTypes.push({name: struct.name, base: struct.base, fields, variant: false})
}
}
return structTypes
}
export function generateVariant(variant, abi: any, isExport = false): ts.ClassDeclaration {
const decoratorArguments: (ts.ObjectLiteralExpression | ts.StringLiteral | ts.Identifier)[] =
variant.fields.map((field) => findVariantStructType(field.type, undefined, abi))
const decorators = [
ts.factory.createDecorator(
ts.factory.createCallExpression(
ts.factory.createIdentifier('Variant.type'),
undefined,
[
ts.factory.createStringLiteral(variant.name),
ts.factory.createArrayLiteralExpression(decoratorArguments),
]
)
),
]
const valueField = ts.factory.createPropertyDeclaration(
[ts.factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
ts.factory.createIdentifier('value'),
undefined,
ts.factory.createUnionTypeNode(
variant.fields.map((field) => {
return ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(findFieldTypeString(field.type, undefined, abi)),
undefined
)
})
),
undefined
)
return ts.factory.createClassDeclaration(
isExport
? [...decorators, ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)]
: decorators,
ts.factory.createIdentifier(formatClassName(variant.name)),
undefined, // typeParameters
[
ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
ts.factory.createExpressionWithTypeArguments(
ts.factory.createIdentifier('Variant'),
[]
),
]),
], // heritageClauses
[valueField]
)
}
export function generateStruct(struct, abi, isExport = false): ts.ClassDeclaration {
const decorators = [
ts.factory.createDecorator(
ts.factory.createCallExpression(ts.factory.createIdentifier('Struct.type'), undefined, [
ts.factory.createStringLiteral(struct.name),
])
),
]
const baseClass = struct.base?.length ? struct.base : 'Struct'
const members: ts.ClassElement[] = []
for (const field of struct.fields) {
members.push(generateField(field, undefined, abi))
}
return ts.factory.createClassDeclaration(
isExport
? [...decorators, ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)]
: decorators,
ts.factory.createIdentifier(formatClassName(struct.name)),
undefined, // typeParameters
[
ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [
ts.factory.createExpressionWithTypeArguments(
ts.factory.createIdentifier(baseClass),
[]
),
]),
], // heritageClauses
members
)
}
export function generateField(
field: FieldType,
namespace: string | undefined,
abi: ABI.Def
): ts.PropertyDeclaration {
const fieldName = field.name
let isArray = field.type.endsWith('[]')
// Start with the main type argument
const decoratorArguments: (ts.ObjectLiteralExpression | ts.StringLiteral | ts.Identifier)[] = [
findFieldStructType(field.type, namespace, abi),
]
const structTypeString = findFieldTypeString(field.type, namespace, abi)
isArray = isArray || structTypeString.endsWith('[]')
// Build the options object if needed
const optionsProps: ts.ObjectLiteralElementLike[] = []
if (isArray) {
optionsProps.push(
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier('array'),
ts.factory.createTrue()
)
)
}
if (field.extension) {
optionsProps.push(
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier('extension'),
ts.factory.createTrue()
)
)
}
if (field.optional) {
optionsProps.push(
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier('optional'),
ts.factory.createTrue()
)
)
}
// If there are options, create the object and add to decorator arguments
if (optionsProps.length > 0) {
const optionsObject = ts.factory.createObjectLiteralExpression(optionsProps)
decoratorArguments.push(optionsObject)
}
const decorators = [
ts.factory.createDecorator(
ts.factory.createCallExpression(
ts.factory.createIdentifier('Struct.field'),
undefined,
decoratorArguments
)
),
ts.factory.createModifier(ts.SyntaxKind.DeclareKeyword),
]
const typeReferenceNode = ts.factory.createTypeReferenceNode(
extractDecorator(structTypeString).type
)
let typeNode: ts.TypeNode
if (isArray) {
typeNode = ts.factory.createArrayTypeNode(typeReferenceNode)
} else {
typeNode = typeReferenceNode
}
return ts.factory.createPropertyDeclaration(
decorators,
ts.factory.createIdentifier(fieldName),
field.optional ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined,
typeNode,
undefined // initializer
)
}
function orderStructs(structs, typeAliases: TypeAlias[] = []) {
const orderedStructs: StructData[] = []
for (const struct of structs) {
orderedStructs.push(...findDependencies(struct, structs, typeAliases))
orderedStructs.push(struct)
}
return orderedStructs.filter((struct, index, self) => {
return index === self.findIndex((s) => s.name === struct.name)
})
}
function findDependencies(
struct: StructData,
allStructs: StructData[],
typeAliases: TypeAlias[]
): StructData[] {
const dependencies: StructData[] = []
if (struct.base?.length) {
const baseStruct = allStructs.find((s) => s.name === struct.base)
if (baseStruct) dependencies.push(baseStruct)
}
for (const field of struct.fields) {
const {type: fieldType} = extractDecorator(field.type)
let dependencyStruct = allStructs.find((struct) => struct.name === fieldType)
if (!dependencyStruct) {
const typeAlias = typeAliases.find((typeAlias) => typeAlias.new_type_name === fieldType)
const typeAliasString = typeAlias && extractDecorator(typeAlias.type).type
dependencyStruct = typeAliasString
? allStructs.find((struct) => struct.name === typeAliasString)
: undefined
}
const alreadyIncluded = dependencies.some(
(struct) => struct.name === dependencyStruct?.name
)
if (alreadyIncluded || dependencyStruct?.name === struct.name) {
continue
}
if (dependencyStruct) {
dependencies.push(...findDependencies(dependencyStruct, allStructs, typeAliases))
dependencies.push(dependencyStruct)
}
}
return dependencies
}
function findVariantStructType(
typeString: string,
namespace: string | undefined,
abi: ABI.Def
): ts.Identifier | ts.StringLiteral | ts.ObjectLiteralExpression {
const variantTypeString = findFieldTypeString(typeString, namespace, abi)
if (['string', 'string[]', 'boolean', 'boolean[]'].includes(variantTypeString.toLowerCase())) {
return ts.factory.createStringLiteral(formatFieldString(variantTypeString))
}
const isArray = variantTypeString.endsWith('[]')
if (isArray) {
const optionsProps: ts.ObjectLiteralElementLike[] = []
optionsProps.push(
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier('type'),
ts.factory.createIdentifier(extractDecorator(variantTypeString).type)
)
)
optionsProps.push(
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier('array'),
ts.factory.createTrue()
)
)
const optionsObject = ts.factory.createObjectLiteralExpression(optionsProps)
return optionsObject
} else {
return ts.factory.createIdentifier(variantTypeString)
}
}
function findFieldStructType(
typeString: string,
namespace: string | undefined,
abi: ABI.Def
): ts.Identifier | ts.StringLiteral {
const fieldTypeString = extractDecorator(findFieldTypeString(typeString, namespace, abi)).type
if (['string', 'bool', 'boolean'].includes(fieldTypeString.toLowerCase())) {
return ts.factory.createStringLiteral(formatFieldString(fieldTypeString))
}
return ts.factory.createIdentifier(fieldTypeString)
}
export function findFieldTypeString(
typeString: string,
namespace: string | undefined,
abi: ABI.Def
): string {
const fieldType = findInternalType(typeString, namespace, abi)
const {type: fieldTypeWithoutDecorator, decorator = ''} = extractDecorator(fieldType)
if (['String', 'Number'].includes(fieldTypeWithoutDecorator)) {
return `${fieldTypeWithoutDecorator.toLowerCase()}${decorator}`
}
if (fieldTypeWithoutDecorator === 'Bool') {
return `boolean${decorator}`
}
if (fieldTypeWithoutDecorator === 'Symbol') {
return `Asset.Symbol${decorator}`
}
if (fieldTypeWithoutDecorator === 'Symbol_code') {
return `Asset.SymbolCode${decorator}`
}
return parseType(fieldType)
}
function formatFieldString(typeString: string): string {
if (typeString === 'boolean') {
return 'bool'
}
return parseType(typeString)
}
|