Skip to content

Commit 162d638

Browse files
committed
refactor: import ptypes namespace instead of individual types
1 parent f95312e commit 162d638

File tree

4 files changed

+43
-60
lines changed

4 files changed

+43
-60
lines changed

.nsprc

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
{
2-
"1100467": {
3-
"active": true,
4-
"notes": "Waiting for https://github.com/npm/cli/issues/7902 to be resolved",
5-
"expiry": "2024-12-31"
6-
}
7-
}
1+
{}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"tsx": "4.19.1",
5858
"typescript": "^5.6.2",
5959
"vitest": "2.1.2",
60-
"cross-spawn": "^7.0.6"
60+
"cross-spawn": "7.0.6"
6161
},
6262
"peerDependencies": {
6363
"tslib": "^2.6.2"

src/test-transformer/node-factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FunctionPType } from '@algorandfoundation/puya-ts'
1+
import { ptypes } from '@algorandfoundation/puya-ts'
22
import ts from 'typescript'
33
import { TypeInfo } from '../encoders'
44
import type { DeliberateAny } from '../typescript-helpers'
@@ -48,7 +48,7 @@ export const nodeFactory = {
4848
)
4949
},
5050

51-
attachMetaData(classIdentifier: ts.Identifier, method: ts.MethodDeclaration, functionType: FunctionPType) {
51+
attachMetaData(classIdentifier: ts.Identifier, method: ts.MethodDeclaration, functionType: ptypes.FunctionPType) {
5252
const methodName = getPropertyNameAsString(method.name)
5353
const metadata = factory.createObjectLiteralExpression([
5454
factory.createPropertyAssignment('methodName', methodName),

src/test-transformer/visitors.ts

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,4 @@
1-
import {
2-
anyPType,
3-
ARC4BooleanType,
4-
ARC4StringType,
5-
ARC4StructType,
6-
ARC4TupleType,
7-
BoxMapPType,
8-
BoxPType,
9-
ContractClassPType,
10-
DynamicArrayType,
11-
FunctionPType,
12-
GlobalStateType,
13-
LocalStateType,
14-
PType,
15-
SourceLocation,
16-
StaticArrayType,
17-
TypeResolver,
18-
UFixedNxMType,
19-
UintNType,
20-
} from '@algorandfoundation/puya-ts'
1+
import { ptypes, SourceLocation, TypeResolver } from '@algorandfoundation/puya-ts'
212
import ts from 'typescript'
223
import type { TypeInfo } from '../encoders'
234
import { instanceOfAny } from '../typescript-helpers'
@@ -33,7 +14,7 @@ const { factory } = ts
3314

3415
type VisitorHelper = {
3516
additionalStatements: ts.Statement[]
36-
resolveType(node: ts.Node): PType
17+
resolveType(node: ts.Node): ptypes.PType
3718
sourceLocation(node: ts.Node): SourceLocation
3819
}
3920

@@ -49,11 +30,11 @@ export class SourceFileVisitor {
4930

5031
this.helper = {
5132
additionalStatements: [],
52-
resolveType(node: ts.Node): PType {
33+
resolveType(node: ts.Node): ptypes.PType {
5334
try {
5435
return typeResolver.resolve(node, this.sourceLocation(node))
5536
} catch {
56-
return anyPType
37+
return ptypes.anyPType
5738
}
5839
},
5940
sourceLocation(node: ts.Node): SourceLocation {
@@ -106,7 +87,7 @@ class ExpressionVisitor {
10687
let type = this.helper.resolveType(node)
10788

10889
// `voted = LocalState<uint64>()` is resolved to FunctionPType with returnType LocalState<uint64>
109-
if (type instanceof FunctionPType) type = type.returnType
90+
if (type instanceof ptypes.FunctionPType) type = type.returnType
11091

11192
const isGeneric = isGenericType(type)
11293
const isArc4Encoded = isArc4EncodedType(type)
@@ -256,7 +237,7 @@ class ClassVisitor {
256237
private classDec: ts.ClassDeclaration,
257238
) {
258239
const classType = helper.resolveType(classDec)
259-
this.isArc4 = classType instanceof ContractClassPType && classType.isARC4
240+
this.isArc4 = classType instanceof ptypes.ContractClassPType && classType.isARC4
260241
}
261242

262243
public result(): ts.ClassDeclaration {
@@ -267,7 +248,7 @@ class ClassVisitor {
267248
if (ts.isMethodDeclaration(node)) {
268249
if (this.classDec.name && this.isArc4) {
269250
const methodType = this.helper.resolveType(node)
270-
if (methodType instanceof FunctionPType) {
251+
if (methodType instanceof ptypes.FunctionPType) {
271252
this.helper.additionalStatements.push(nodeFactory.attachMetaData(this.classDec.name, node, methodType))
272253
}
273254
}
@@ -282,50 +263,58 @@ class ClassVisitor {
282263
}
283264
}
284265

285-
const isGenericType = (type: PType): boolean =>
266+
const isGenericType = (type: ptypes.PType): boolean =>
286267
instanceOfAny(
287268
type,
288-
ARC4StructType,
289-
ARC4TupleType,
290-
BoxMapPType,
291-
BoxPType,
292-
DynamicArrayType,
293-
GlobalStateType,
294-
LocalStateType,
295-
StaticArrayType,
296-
UFixedNxMType,
297-
UintNType,
269+
ptypes.ARC4StructType,
270+
ptypes.ARC4TupleType,
271+
ptypes.BoxMapPType,
272+
ptypes.BoxPType,
273+
ptypes.DynamicArrayType,
274+
ptypes.GlobalStateType,
275+
ptypes.LocalStateType,
276+
ptypes.StaticArrayType,
277+
ptypes.UFixedNxMType,
278+
ptypes.UintNType,
298279
)
299280

300-
const isArc4EncodedType = (type: PType): boolean =>
301-
instanceOfAny(type, ARC4StructType, ARC4TupleType, DynamicArrayType, StaticArrayType, UFixedNxMType, UintNType) ||
302-
type === ARC4StringType ||
303-
type === ARC4BooleanType
304-
305-
const getGenericTypeInfo = (type: PType): TypeInfo => {
281+
const isArc4EncodedType = (type: ptypes.PType): boolean =>
282+
instanceOfAny(
283+
type,
284+
ptypes.ARC4StructType,
285+
ptypes.ARC4TupleType,
286+
ptypes.DynamicArrayType,
287+
ptypes.StaticArrayType,
288+
ptypes.UFixedNxMType,
289+
ptypes.UintNType,
290+
) ||
291+
type === ptypes.ARC4StringType ||
292+
type === ptypes.ARC4BooleanType
293+
294+
const getGenericTypeInfo = (type: ptypes.PType): TypeInfo => {
306295
const genericArgs: TypeInfo[] | Record<string, TypeInfo> = []
307296

308-
if (instanceOfAny(type, LocalStateType, GlobalStateType, BoxPType)) {
297+
if (instanceOfAny(type, ptypes.LocalStateType, ptypes.GlobalStateType, ptypes.BoxPType)) {
309298
genericArgs.push(getGenericTypeInfo(type.contentType))
310-
} else if (type instanceof BoxMapPType) {
299+
} else if (type instanceof ptypes.BoxMapPType) {
311300
genericArgs.push(getGenericTypeInfo(type.keyType))
312301
genericArgs.push(getGenericTypeInfo(type.contentType))
313-
} else if (instanceOfAny(type, StaticArrayType, DynamicArrayType)) {
302+
} else if (instanceOfAny(type, ptypes.StaticArrayType, ptypes.DynamicArrayType)) {
314303
genericArgs.push(getGenericTypeInfo(type.elementType))
315-
} else if (type instanceof UFixedNxMType) {
304+
} else if (type instanceof ptypes.UFixedNxMType) {
316305
genericArgs.push({ name: type.n.toString() })
317306
genericArgs.push({ name: type.m.toString() })
318-
} else if (type instanceof UintNType) {
307+
} else if (type instanceof ptypes.UintNType) {
319308
genericArgs.push({ name: type.n.toString() })
320-
} else if (type instanceof ARC4StructType) {
309+
} else if (type instanceof ptypes.ARC4StructType) {
321310
genericArgs.push(
322311
...Object.fromEntries(
323312
Object.entries(type.fields)
324313
.map(([key, value]) => [key, getGenericTypeInfo(value)])
325314
.filter((x) => !!x),
326315
),
327316
)
328-
} else if (type instanceof ARC4TupleType) {
317+
} else if (type instanceof ptypes.ARC4TupleType) {
329318
genericArgs.push(...type.items.map(getGenericTypeInfo))
330319
}
331320

0 commit comments

Comments
 (0)