Skip to content

Commit 49550e0

Browse files
committed
prevent unnecessary processing of symbols loaded from testing package in puyaTsTransformer
1 parent 3cfccbf commit 49550e0

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

src/test-transformer/visitors.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@ const algotsModulePaths = [
2222
'/puya-ts/packages/algo-ts/',
2323
`${path.sep}puya-ts${path.sep}packages${path.sep}algo-ts${path.sep}`,
2424
]
25+
const algotsTestingModulePaths = (testingPackageName: string) => [
26+
testingPackageName,
27+
`${path.sep}algorand-typescript-testing${path.sep}src${path.sep}`,
28+
`${path.sep}algorand-typescript-testing${path.sep}dist${path.sep}`,
29+
]
30+
31+
const testingExamplePath = `${path.sep}algorand-typescript-testing${path.sep}examples${path.sep}`
2532

2633
type VisitorHelper = {
2734
additionalStatements: ts.Statement[]
2835
resolveType(node: ts.Node): ptypes.PType
2936
resolveTypeParameters(node: ts.CallExpression): ptypes.PType[]
3037
sourceLocation(node: ts.Node): SourceLocation
3138
tryGetSymbol(node: ts.Node): ts.Symbol | undefined
39+
getConfig(): TransformerConfig
3240
}
3341

3442
export class SourceFileVisitor {
@@ -38,7 +46,7 @@ export class SourceFileVisitor {
3846
private context: ts.TransformationContext,
3947
private sourceFile: ts.SourceFile,
4048
program: ts.Program,
41-
private config: TransformerConfig,
49+
config: TransformerConfig,
4250
) {
4351
const typeChecker = program.getTypeChecker()
4452
const loggingContext = LoggingContext.create()
@@ -74,21 +82,24 @@ export class SourceFileVisitor {
7482
return SourceLocation.None
7583
}
7684
},
85+
getConfig(): TransformerConfig {
86+
return config
87+
},
7788
}
7889
}
7990

8091
public result(): ts.SourceFile {
8192
const updatedSourceFile = ts.visitNode(this.sourceFile, this.visit) as ts.SourceFile
8293
return factory.updateSourceFile(updatedSourceFile, [
83-
...nodeFactory.importHelpers(this.config.testingPackageName),
94+
...nodeFactory.importHelpers(this.helper.getConfig().testingPackageName),
8495
...updatedSourceFile.statements,
8596
...this.helper.additionalStatements,
8697
])
8798
}
8899

89100
private visit = (node: ts.Node): ts.Node => {
90101
if (ts.isImportDeclaration(node)) {
91-
return new ImportDeclarationVisitor(this.context, this.helper, this.config, node).result()
102+
return new ImportDeclarationVisitor(node, this.helper).result()
92103
}
93104
if (ts.isFunctionLike(node)) {
94105
return new FunctionLikeDecVisitor(this.context, this.helper, node).result()
@@ -109,10 +120,8 @@ export class SourceFileVisitor {
109120

110121
class ImportDeclarationVisitor {
111122
constructor(
112-
private context: ts.TransformationContext,
113-
private helper: VisitorHelper,
114-
private config: TransformerConfig,
115123
private declarationNode: ts.ImportDeclaration,
124+
private helper: VisitorHelper,
116125
) {}
117126

118127
public result(): ts.ImportDeclaration {
@@ -132,7 +141,7 @@ class ImportDeclarationVisitor {
132141
: this.declarationNode.importClause,
133142
factory.createStringLiteral(
134143
moduleSpecifier
135-
.replace(algotsModuleSpecifier, testingInternalModuleSpecifier(this.config.testingPackageName))
144+
.replace(algotsModuleSpecifier, testingInternalModuleSpecifier(this.helper.getConfig().testingPackageName))
136145
.replace(/^("|')/, '')
137146
.replace(/("|')$/, ''),
138147
),
@@ -154,7 +163,9 @@ class ExpressionVisitor {
154163
}
155164

156165
private visit = (node: ts.Node): ts.Node => {
157-
handleTypeInfoCaputre: if (ts.isCallExpression(node) || ts.isNewExpression(node)) {
166+
handleTypeInfoCapture: if (ts.isCallExpression(node) || ts.isNewExpression(node)) {
167+
if (!tryGetAlgoTsSymbolName(node.expression, this.helper)) break handleTypeInfoCapture
168+
158169
let type = this.helper.resolveType(node)
159170

160171
// `voted = LocalState<uint64>()` is resolved to FunctionPType with returnType LocalState<uint64>
@@ -180,7 +191,7 @@ class ExpressionVisitor {
180191
// the nodes which have been created or updated by the node factory will not have source location,
181192
// and we do not need to process them further
182193
const sourceLocation = this.helper.sourceLocation(updatedNode)
183-
if (sourceLocation === SourceLocation.None) break handleTypeInfoCaputre
194+
if (sourceLocation === SourceLocation.None) break handleTypeInfoCapture
184195

185196
if (
186197
isCallingEmit(stubbedFunctionName) ||
@@ -518,12 +529,20 @@ const tryGetStubbedFunctionName = (node: ts.CallExpression, helper: VisitorHelpe
518529
}
519530

520531
const tryGetAlgoTsSymbolName = (node: ts.Node, helper: VisitorHelper): string | undefined => {
521-
const s = helper.tryGetSymbol(node)
522-
if (s) {
523-
const sourceFileName = s.valueDeclaration?.getSourceFile().fileName
524-
if (sourceFileName && !algotsModulePaths.some((s) => sourceFileName.includes(s))) return undefined
525-
}
526-
return s?.getName() ?? (ts.isMemberName(node) ? node.text : node.getText())
532+
const symbol = helper.tryGetSymbol(node)
533+
if (!symbol) return undefined
534+
535+
const sourceFileName = symbol.valueDeclaration?.getSourceFile().fileName
536+
if (!sourceFileName) return undefined
537+
538+
// If the symbol is from algorand-typescript package or testing example path, return its name
539+
if (algotsModulePaths.some((path) => sourceFileName.includes(path)) || sourceFileName.includes(testingExamplePath))
540+
return symbol.getName()
541+
542+
// If the symbol is from algorand-typescript-testing package, return undefined as they do not need to be processed
543+
if (algotsTestingModulePaths(helper.getConfig().testingPackageName).some((path) => sourceFileName.includes(path))) return undefined
544+
545+
return symbol.getName()
527546
}
528547

529548
const isCallingDecodeArc4 = (functionName: string | undefined): boolean => 'decodeArc4' === (functionName ?? '')

0 commit comments

Comments
 (0)