Skip to content

Commit

Permalink
🪲 Fix issues with type literal size limits when exporting deployments (…
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Oct 27, 2024
1 parent 05340d7 commit 7a59242
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-onions-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@layerzerolabs/export-deployments": patch
---

Work around the compiler type literal limits
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ docker-*.yaml
.github
.husky
.turbo
.netrc
.env
.env*

# Misc
*ignore
Expand Down
26 changes: 25 additions & 1 deletion packages/export-deployments/src/generator/typescript/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import {
createExportConst,
createIdentifier,
createStringLiteral,
createTypeDeclaration,
createTypeOf,
createTypeReferenceNode,
creteAsConst,
normalizeIdentifierName,
printTSFile,
recordToObjectLiteral,
recordToRecordType,
runtimeObjectToExpressionSafe,
} from './typescript'
import { OutputFile } from '../types'
Expand Down Expand Up @@ -111,6 +115,8 @@ const contractsInformationSafe = flow(
*/
const createAbiIdentifier = (index: number) => createIdentifier(`abi${index}`)

const createAbiTypeIdentifier = (index: number) => createIdentifier(`Abi${index}`)

/**
* Converts GroupedContractInformation into a TypeScript file contents string.
*
Expand Down Expand Up @@ -146,12 +152,30 @@ const transformGroupedContractInformation = ({ addresses, abis, transactionHashe
E.map((declarations) => [
// We'll return all the variable declarations
...declarations,
// Then we'll add their types
...pipe(
declarations,
A.mapWithIndex((index) =>
pipe(
createTypeDeclaration()(createAbiTypeIdentifier(index))(
createTypeOf(createAbiIdentifier(index))
)
)
)
),
// Then we'll add the type of the 'abis' object
pipe(
abiIndexesByNetworkName,
R.map(createAbiTypeIdentifier),
recordToRecordType,
createTypeDeclaration()(createIdentifier('Abis'))
),
// And we'll add an object export to map network names to the ABIs
pipe(
abiIndexesByNetworkName,
R.map(createAbiIdentifier),
recordToObjectLiteral,
createExportConst('abis')
createExportConst('abis', createTypeReferenceNode('Abis'))
),
])
),
Expand Down
33 changes: 27 additions & 6 deletions packages/export-deployments/src/generator/typescript/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,42 @@ import {
SyntaxKind,
createPrinter,
ListFormat,
Identifier,
TypeNode,
} from 'typescript'
import { stringify } from 'fp-ts/lib/Json'
import { Ord } from 'fp-ts/lib/string'

export const createConst =
(modifiers: ModifierLike[] = []) =>
(name: string | BindingName) =>
(name: string | BindingName, type?: TypeNode) =>
(expression: Expression) =>
factory.createVariableStatement(
modifiers,
factory.createVariableDeclarationList(
[factory.createVariableDeclaration(name, undefined, undefined, expression)],
[factory.createVariableDeclaration(name, undefined, type, expression)],
NodeFlags.Const
)
)

export const createTypeOf = factory.createTypeQueryNode

export const createTypeReferenceNode = factory.createTypeReferenceNode

export const createTypeDeclaration =
(modifiers: ModifierLike[] = []) =>
(name: Identifier) =>
(type: TypeNode) =>
factory.createTypeAliasDeclaration(modifiers, name, undefined, type)

/**
* Wraps an expression with "as const"
*
* @param {Expression} expression
* @returns {Expression}
*/
export const creteAsConst = (expression: Expression): Expression =>
factory.createAsExpression(
expression,
factory.createTypeReferenceNode(factory.createIdentifier('const'), undefined)
)
factory.createAsExpression(expression, createTypeReferenceNode(createIdentifier('const'), undefined))

export const createIdentifier = factory.createIdentifier

Expand Down Expand Up @@ -70,6 +79,18 @@ export const recordToObjectLiteral = flow(
(properties) => factory.createObjectLiteralExpression(properties, true)
)

export const recordToRecordType = flow(
R.collect(Ord)((key: string, value: Identifier) =>
factory.createPropertySignature(
undefined,
factory.createStringLiteral(key),
undefined,
createTypeReferenceNode(value)
)
),
(properties) => factory.createTypeLiteralNode(properties)
)

/**
* Parses a JSON string into TypeScript AST
*
Expand Down

0 comments on commit 7a59242

Please sign in to comment.