From 0b282ad227869383271e7f9709ea9b0ab1a3268d Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Wed, 7 Aug 2024 14:01:37 +0100 Subject: [PATCH] chore(common): remove unused helpers (#3018) --- .../src/codegen/utils/extractUserTypes.ts | 64 --------------- packages/common/src/codegen/utils/index.ts | 2 - .../src/codegen/utils/loadUserTypesFile.ts | 79 ------------------- 3 files changed, 145 deletions(-) delete mode 100644 packages/common/src/codegen/utils/extractUserTypes.ts delete mode 100644 packages/common/src/codegen/utils/loadUserTypesFile.ts diff --git a/packages/common/src/codegen/utils/extractUserTypes.ts b/packages/common/src/codegen/utils/extractUserTypes.ts deleted file mode 100644 index 8bdc960699..0000000000 --- a/packages/common/src/codegen/utils/extractUserTypes.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { parse, visit } from "@solidity-parser/parser"; -import { MUDError } from "../../errors"; - -export interface SolidityUserDefinedType { - /** Fully-qualified name of the user-defined type (may include a library name as prefix) */ - typeId: string; - /** Name of the wrapped primitive type */ - internalTypeId: string; - /** Symbol which must be imported to use the type (either the type name, or the library name where the type is defined) */ - importSymbol: string; - /** Path to the solidity file which contains the user type */ - fromPath: string; - /** Whether `fromPath` is relative */ - isRelativePath: boolean; -} - -/** - * Parse the solidity data to extract user-defined type information. - * @param data contents of a solidity file with the user types declarations - * @param userTypeNames names of the user types to extract - * @param fromPath path to the solidity file from which the user types are extracted - * @returns record of type names mapped to the extracted type information - */ -export function extractUserTypes( - data: string, - userTypeNames: string[], - fromPath: string, -): Record { - const ast = parse(data); - - const isRelativePath = fromPath.at(0) === "."; - const userDefinedTypes: Record = {}; - - visit(ast, { - TypeDefinition({ name, definition }, parent) { - if (definition.name.includes("fixed")) throw new MUDError(`Fixed point numbers are not supported by MUD`); - if (userTypeNames.includes(name)) { - if (name in userDefinedTypes) { - throw new MUDError(`File has multiple user types with the same name: ${name}`); - } - - if (parent?.type === "ContractDefinition") { - userDefinedTypes[name] = { - typeId: `${parent.name}.${name}`, - internalTypeId: definition.name, - importSymbol: parent.name, - fromPath, - isRelativePath, - }; - } else { - userDefinedTypes[name] = { - typeId: name, - internalTypeId: definition.name, - importSymbol: name, - fromPath, - isRelativePath, - }; - } - } - }, - }); - - return userDefinedTypes; -} diff --git a/packages/common/src/codegen/utils/index.ts b/packages/common/src/codegen/utils/index.ts index 6e24e2081d..b037143393 100644 --- a/packages/common/src/codegen/utils/index.ts +++ b/packages/common/src/codegen/utils/index.ts @@ -1,5 +1,3 @@ export * from "./contractToInterface"; -export * from "./extractUserTypes"; export * from "./format"; export * from "./formatAndWrite"; -export * from "./loadUserTypesFile"; diff --git a/packages/common/src/codegen/utils/loadUserTypesFile.ts b/packages/common/src/codegen/utils/loadUserTypesFile.ts deleted file mode 100644 index f08608daf3..0000000000 --- a/packages/common/src/codegen/utils/loadUserTypesFile.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { readFileSync } from "fs"; -import path from "path"; -import { SolidityUserDefinedType, extractUserTypes } from "./extractUserTypes"; -import { MUDError } from "../../errors"; -import { SchemaAbiType } from "@latticexyz/schema-type/internal"; - -export type UserType = { - filePath: string; - internalType: SchemaAbiType; -}; - -/** - * Load the user type files and extract type information from them. - * @param userTypes record of user type data mapped by type names - * @param outputBaseDirectory base path to the output directory - * @param remappings solc remappings - * @returns record of the user type information mapped by type names - */ -export function loadAndExtractUserTypes( - userTypes: Record, - outputBaseDirectory: string, - remappings: [string, string][], -): Record { - const userTypesPerFile: Record = {}; - for (const [userTypeName, { filePath: unresolvedFilePath }] of Object.entries(userTypes)) { - if (!(unresolvedFilePath in userTypesPerFile)) { - userTypesPerFile[unresolvedFilePath] = []; - } - userTypesPerFile[unresolvedFilePath].push(userTypeName); - } - let extractedUserTypes: Record = {}; - for (const [unresolvedFilePath, userTypeNames] of Object.entries(userTypesPerFile)) { - const { filePath, data } = loadUserTypesFile(outputBaseDirectory, unresolvedFilePath, remappings); - const userTypesInFile = extractUserTypes(data, userTypeNames, filePath); - - // Verify the actual user type matches the internalType specified in the config - for (const [userTypeName, userType] of Object.entries(userTypesInFile)) { - if (userType.internalTypeId !== userTypes[userTypeName].internalType) { - throw new MUDError( - `User type "${userTypeName}" has internal type "${userType.internalTypeId}" but config specifies "${userTypes[userTypeName].internalType}"`, - ); - } - } - - extractedUserTypes = { ...extractedUserTypes, ...userTypesInFile }; - } - return extractedUserTypes; -} - -function loadUserTypesFile( - outputBaseDirectory: string, - unresolvedFilePath: string, - remappings: [string, string][], -): { - filePath: string; - data: string; -} { - if (unresolvedFilePath.at(0) === ".") { - const relativePath = path.relative(outputBaseDirectory, unresolvedFilePath); - return { - filePath: "./" + relativePath, // solc doesn't like relative paths without "./" - data: readFileSync(unresolvedFilePath, "utf8"), - }; - } else { - // apply remappings to read the file via node - let remappedFilePath = unresolvedFilePath; - for (const [from, to] of remappings) { - if (remappedFilePath.includes(from)) { - remappedFilePath = remappedFilePath.replace(from, to); - break; - } - } - - return { - filePath: unresolvedFilePath, - data: readFileSync(remappedFilePath, "utf8"), - }; - } -}