Skip to content

Commit dafba04

Browse files
authored
Merge pull request #25 from algorandfoundation/refactor-visitor
refactor: capture generic type info only for boxes, global and local states
2 parents 7b195f7 + 6d05031 commit dafba04

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

src/test-execution-context.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ export class TestExecutionContext implements internal.ExecutionContext {
6464
this.txn.appendLog(value)
6565
}
6666

67-
/* @internal */
6867
exportLogs<const T extends [...LogDecoding[]]>(appId: uint64, ...decoding: T): DecodedLogs<T> {
6968
return this.txn.exportLogs(appId, ...decoding)
7069
}

src/test-transformer/node-factory.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,17 @@ export const nodeFactory = {
9494
)
9595
},
9696

97-
callStubbedFunction(functionName: string, node: ts.CallExpression, ...typeInfos: (TypeInfo | undefined)[]) {
98-
const infoStringArray = typeInfos.length ? typeInfos.map((typeInfo) => JSON.stringify(typeInfo)) : undefined
97+
callStubbedFunction(functionName: string, node: ts.CallExpression, typeInfo?: TypeInfo) {
98+
const typeInfoArg = typeInfo ? factory.createStringLiteral(JSON.stringify(typeInfo)) : undefined
9999
const updatedPropertyAccessExpression = factory.createPropertyAccessExpression(
100100
factory.createIdentifier('runtimeHelpers'),
101101
`${functionName}Impl`,
102102
)
103-
const typeInfoArgs = infoStringArray
104-
? infoStringArray?.filter((s) => !!s).map((infoString) => factory.createStringLiteral(infoString))
105-
: undefined
103+
106104
return factory.createCallExpression(
107105
updatedPropertyAccessExpression,
108106
node.typeArguments,
109-
[...(typeInfoArgs ?? []), ...(node.arguments ?? [])].filter((arg) => !!arg),
107+
[typeInfoArg, ...(node.arguments ?? [])].filter((arg) => !!arg),
110108
)
111109
},
112110
} satisfies Record<string, (...args: DeliberateAny[]) => ts.Node>

src/test-transformer/visitors.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class ExpressionVisitor {
9090
private context: ts.TransformationContext,
9191
private helper: VisitorHelper,
9292
private expressionNode: ts.Expression,
93+
private stubbedFunctionName?: string,
9394
) {}
9495

9596
public result(): ts.Expression {
@@ -104,6 +105,7 @@ class ExpressionVisitor {
104105
if (type instanceof ptypes.FunctionPType) type = type.returnType
105106

106107
const isGeneric = isGenericType(type)
108+
const needsToCaptureTypeInfo = isGeneric && isStateOrBoxType(type)
107109
const isArc4Encoded = isArc4EncodedType(type)
108110
const info = isGeneric || isArc4Encoded ? getGenericTypeInfo(type) : undefined
109111
let updatedNode = node
@@ -113,20 +115,22 @@ class ExpressionVisitor {
113115
updatedNode = nodeFactory.instantiateARC4EncodedType(updatedNode, info)
114116
}
115117
}
118+
116119
if (ts.isCallExpression(updatedNode)) {
117-
const stubbedFunctionName = tryGetStubbedFunctionName(updatedNode, this.helper)
118-
const infos = [info]
120+
const stubbedFunctionName = this.stubbedFunctionName ?? tryGetStubbedFunctionName(updatedNode, this.helper)
121+
this.stubbedFunctionName = undefined
122+
let infoArg = info
119123
if (isCallingEmit(stubbedFunctionName)) {
120-
infos[0] = this.helper.resolveTypeParameters(updatedNode).map(getGenericTypeInfo)[0]
124+
infoArg = this.helper.resolveTypeParameters(updatedNode).map(getGenericTypeInfo)[0]
121125
}
122126
if (isCallingDecodeArc4(stubbedFunctionName)) {
123127
const targetType = ptypes.ptypeToArc4EncodedType(type, this.helper.sourceLocation(node))
124128
const targetTypeInfo = getGenericTypeInfo(targetType)
125-
infos[0] = targetTypeInfo
129+
infoArg = targetTypeInfo
126130
}
127-
updatedNode = stubbedFunctionName ? nodeFactory.callStubbedFunction(stubbedFunctionName, updatedNode, ...infos) : updatedNode
131+
updatedNode = stubbedFunctionName ? nodeFactory.callStubbedFunction(stubbedFunctionName, updatedNode, infoArg) : updatedNode
128132
}
129-
return isGeneric
133+
return needsToCaptureTypeInfo
130134
? nodeFactory.captureGenericTypeInfo(ts.visitEachChild(updatedNode, this.visit, this.context), JSON.stringify(info))
131135
: ts.visitEachChild(updatedNode, this.visit, this.context)
132136
}
@@ -218,8 +222,11 @@ class FunctionOrMethodVisitor {
218222
if (ts.isNewExpression(node)) {
219223
return new ExpressionVisitor(this.context, this.helper, node).result()
220224
}
221-
if (ts.isCallExpression(node) && tryGetStubbedFunctionName(node, this.helper)) {
222-
return new ExpressionVisitor(this.context, this.helper, node).result()
225+
if (ts.isCallExpression(node)) {
226+
const stubbedFunctionName = tryGetStubbedFunctionName(node, this.helper)
227+
if (stubbedFunctionName) {
228+
return new ExpressionVisitor(this.context, this.helper, node, stubbedFunctionName).result()
229+
}
223230
}
224231

225232
return node
@@ -305,6 +312,9 @@ const isGenericType = (type: ptypes.PType): boolean =>
305312
ptypes.TuplePType,
306313
)
307314

315+
const isStateOrBoxType = (type: ptypes.PType): boolean =>
316+
instanceOfAny(type, ptypes.BoxMapPType, ptypes.BoxPType, ptypes.GlobalStateType, ptypes.LocalStateType)
317+
308318
const isArc4EncodedType = (type: ptypes.PType): boolean =>
309319
instanceOfAny(
310320
type,

0 commit comments

Comments
 (0)