Skip to content

Commit

Permalink
replace use of renderList with .map
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Jul 24, 2024
1 parent fb1cfef commit 1124267
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 52 deletions.
9 changes: 1 addition & 8 deletions packages/common/src/codegen/render-solidity/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ pragma solidity >=0.8.24;
/* Autogenerated file. Do not edit manually. */`;

/**
* Renders a list of lines
*/
export function renderList<T>(list: T[], renderItem: (item: T, index: number) => string): string {
return internalRenderList("", list, renderItem);
}

/**
* Renders a comma-separated list of arguments for solidity functions, ignoring empty and undefined ones
*/
Expand Down Expand Up @@ -62,7 +55,7 @@ export function renderCommonData({

const _keyTupleDefinition = `
bytes32[] memory _keyTuple = new bytes32[](${keyTuple.length});
${renderList(keyTuple, (key, index) => `_keyTuple[${index}] = ${renderValueTypeToBytes32(key.name, key)};`)}
${keyTuple.map((key, index) => `_keyTuple[${index}] = ${renderValueTypeToBytes32(key.name, key)};`).join("\n")}
`;

return {
Expand Down
28 changes: 8 additions & 20 deletions packages/store/ts/codegen/record.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
RenderDynamicField,
renderArguments,
renderCommonData,
renderList,
renderWithStore,
} from "@latticexyz/common/codegen";
import { RenderDynamicField, renderArguments, renderCommonData, renderWithStore } from "@latticexyz/common/codegen";
import { renderDecodeValueType } from "./field";
import { RenderTableOptions } from "./types";

Expand Down Expand Up @@ -204,12 +198,7 @@ function renderDecodeFunctions({ structName, fields, staticFields, dynamicFields
function decodeStatic(bytes memory _blob) internal pure returns (${renderArguments(
staticFields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`),
)}) {
${renderList(
staticFields,
(field, index) => `
${field.name} = ${renderDecodeValueType(field, staticOffsets[index])};
`,
)}
${staticFields.map((field, index) => `${field.name} = ${renderDecodeValueType(field, staticOffsets[index])};`).join(" ")}
}
`;
}
Expand All @@ -222,11 +211,10 @@ function renderDecodeFunctions({ structName, fields, staticFields, dynamicFields
function decodeDynamic(EncodedLengths _encodedLengths, bytes memory _blob) internal pure returns (${renderArguments(
dynamicFields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`),
)}) {
${renderList(
dynamicFields,
// unchecked is only dangerous if _encodedLengths (and _blob) is invalid,
// but it's assumed to be valid, and this function is meant to be mostly used internally
(field, index) => {
${dynamicFields
.map((field, index) => {
// unchecked is only dangerous if _encodedLengths (and _blob) is invalid,
// but it's assumed to be valid, and this function is meant to be mostly used internally
if (index === 0) {
return `
uint256 _start;
Expand All @@ -245,8 +233,8 @@ function renderDecodeFunctions({ structName, fields, staticFields, dynamicFields
${field.name} = ${renderDecodeDynamicFieldPartial(field)};
`;
}
},
)}
})
.join("\n")}
}
`;
}
Expand Down
13 changes: 6 additions & 7 deletions packages/store/ts/codegen/renderTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
RenderDynamicField,
renderArguments,
renderCommonData,
renderList,
renderImports,
renderTableId,
renderTypeHelpers,
Expand Down Expand Up @@ -64,13 +63,13 @@ export function renderTable(options: RenderTableOptions) {
}
${
!structName
? ""
: `
structName
? `
struct ${structName} {
${renderList(fields, ({ name, typeId }) => `${typeId} ${name};`)}
${fields.map(({ name, typeId }) => `${typeId} ${name};`).join(" ")}
}
`
: ""
}
library ${libraryName} {
Expand All @@ -93,7 +92,7 @@ export function renderTable(options: RenderTableOptions) {
*/
function getKeyNames() internal pure returns (string[] memory keyNames) {
keyNames = new string[](${keyTuple.length});
${renderList(keyTuple, (keyElement, index) => `keyNames[${index}] = "${keyElement.name}";`)}
${keyTuple.map((element, index) => `keyNames[${index}] = "${element.name}";`).join(" ")}
}
/**
Expand All @@ -102,7 +101,7 @@ export function renderTable(options: RenderTableOptions) {
*/
function getFieldNames() internal pure returns (string[] memory fieldNames) {
fieldNames = new string[](${fields.length});
${renderList(fields, (field, index) => `fieldNames[${index}] = "${field.name}";`)}
${fields.map((field, index) => `fieldNames[${index}] = "${field.name}";`).join(" ")}
}
${renderWithStore(
Expand Down
14 changes: 7 additions & 7 deletions packages/store/ts/codegen/renderTableIndex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { posixPath, renderList, renderedSolidityHeader } from "@latticexyz/common/codegen";
import { posixPath, renderedSolidityHeader } from "@latticexyz/common/codegen";
import { TableOptions } from "./tableOptions";

/**
Expand All @@ -10,11 +10,11 @@ export function renderTableIndex(options: TableOptions[]) {
return `
${renderedSolidityHeader}
${renderList(options, ({ outputPath, tableName, renderOptions: { structName } }) => {
const imports = [tableName];
if (structName) imports.push(structName);
return `import { ${imports.join(", ")} } from "./${posixPath(outputPath)}";`;
})}
${options
.map(({ outputPath, tableName, renderOptions: { structName } }) => {
const imports = [tableName, ...(structName ? [structName] : [])];
return `import { ${imports.join(", ")} } from "./${posixPath(outputPath)}";`;
})
.join("\n")}
`;
}
21 changes: 11 additions & 10 deletions packages/world/ts/node/render-solidity/renderSystemInterface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderArguments, renderList, renderedSolidityHeader, renderImports } from "@latticexyz/common/codegen";
import { renderArguments, renderedSolidityHeader, renderImports } from "@latticexyz/common/codegen";
import { RenderSystemInterfaceOptions } from "./types";

export function renderSystemInterface(options: RenderSystemInterfaceOptions) {
Expand All @@ -15,16 +15,17 @@ export function renderSystemInterface(options: RenderSystemInterfaceOptions) {
* @dev This interface is automatically generated from the corresponding system contract. Do not edit manually.
*/
interface ${name} {
${renderList(errors, ({ name, parameters }) => `error ${name}(${renderArguments(parameters)});`)}
${errors.map(({ name, parameters }) => `error ${name}(${renderArguments(parameters)});`).join("\n")}
${renderList(
functions,
({ name, parameters, stateMutability, returnParameters }) => `
function ${functionPrefix}${name}(
${renderArguments(parameters)}
) external ${stateMutability} ${renderReturnParameters(returnParameters)};
`,
)}
${functions
.map(
({ name, parameters, stateMutability, returnParameters }) => `
function ${functionPrefix}${name}(
${renderArguments(parameters)}
) external ${stateMutability} ${renderReturnParameters(returnParameters)};
`,
)
.join("\n")}
}
`;
}
Expand Down

0 comments on commit 1124267

Please sign in to comment.