Skip to content

Commit

Permalink
chore(common,store): remove unused codegen vars [N-06] (#2103)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Jan 11, 2024
1 parent 3bebc44 commit f3d1ac8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 55 deletions.
41 changes: 23 additions & 18 deletions packages/common/src/codegen/render-solidity/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,12 @@ export function renderCommonData({
staticResourceData?: StaticResourceData;
keyTuple: RenderKeyTuple[];
}): {
_tableId: string;
_typedTableId: string;
_keyArgs: string;
_typedKeyArgs: string;
_keyTupleDefinition: string;
} {
// static resource means static tableId as well, and no tableId arguments
const _tableId = staticResourceData ? "" : "_tableId";
const _typedTableId = staticResourceData ? "" : "ResourceId _tableId";

const _keyArgs = renderArguments(keyTuple.map(({ name }) => name));
const _typedKeyArgs = renderArguments(keyTuple.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`));

const _keyTupleDefinition = `
Expand All @@ -55,9 +50,7 @@ export function renderCommonData({
`;

return {
_tableId,
_typedTableId,
_keyArgs,
_typedKeyArgs,
_keyTupleDefinition,
};
Expand Down Expand Up @@ -127,21 +120,33 @@ export function renderAbsoluteImports(imports: AbsoluteImportDatum[]): string {

export function renderWithStore(
storeArgument: boolean,
callback: (
_typedStore: string | undefined,
_store: string,
_commentSuffix: string,
_untypedStore: string | undefined,
_methodPrefix: string,
_internal?: boolean
) => string
callback: (data: {
_typedStore: string | undefined;
_store: string;
_commentSuffix: string;
_methodNamePrefix: string;
_internal?: boolean;
}) => string
): string {
let result = "";
result += callback(undefined, "StoreSwitch", "", undefined, "");
result += callback(undefined, "StoreCore", "", undefined, "_", true);
result += callback({ _typedStore: undefined, _store: "StoreSwitch", _commentSuffix: "", _methodNamePrefix: "" });
result += callback({
_typedStore: undefined,
_store: "StoreCore",
_commentSuffix: "",
_methodNamePrefix: "_",
_internal: true,
});

if (storeArgument) {
result += "\n" + callback("IStore _store", "_store", " (using the specified store)", "_store", "");
result +=
"\n" +
callback({
_typedStore: "IStore _store",
_store: "_store",
_commentSuffix: " (using the specified store)",
_methodNamePrefix: "",
});
}

return result;
Expand Down
19 changes: 9 additions & 10 deletions packages/store/ts/codegen/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function renderFieldMethods(options: RenderTableOptions) {
result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => `
/**
* @notice Get ${field.name}${_commentSuffix}.
*/
Expand Down Expand Up @@ -57,7 +57,7 @@ export function renderFieldMethods(options: RenderTableOptions) {
}

result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(storeArgument, (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => {
renderWithStore(storeArgument, ({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => {
const externalArguments = renderArguments([_typedStore, _typedTableId, _typedKeyArgs, _typedFieldName]);
const setFieldMethod = field.isDynamic ? "setDynamicField" : "setStaticField";
const encodeFieldSingle = renderEncodeFieldSingle(field);
Expand Down Expand Up @@ -85,7 +85,7 @@ export function renderFieldMethods(options: RenderTableOptions) {
result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => `
/**
* @notice Get the length of ${field.name}${_commentSuffix}.
*/
Expand All @@ -107,7 +107,7 @@ export function renderFieldMethods(options: RenderTableOptions) {
result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => `
/**
* @notice Get an item of ${field.name}${_commentSuffix}.
* @dev Reverts with Store_IndexOutOfBounds if \`_index\` is out of bounds for the array.
Expand Down Expand Up @@ -138,7 +138,7 @@ export function renderFieldMethods(options: RenderTableOptions) {
result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => `
/**
* @notice Push ${portionData.title} to ${field.name}${_commentSuffix}.
*/
Expand All @@ -158,7 +158,7 @@ export function renderFieldMethods(options: RenderTableOptions) {
result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => `
/**
* @notice Pop ${portionData.title} from ${field.name}${_commentSuffix}.
*/
Expand All @@ -175,7 +175,7 @@ export function renderFieldMethods(options: RenderTableOptions) {
);

result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) =>
renderWithStore(storeArgument, (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => {
renderWithStore(storeArgument, ({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => {
const externalArguments = renderArguments([
_typedStore,
_typedTableId,
Expand Down Expand Up @@ -253,10 +253,9 @@ function renderCastStaticBytesToType(field: RenderType, staticBytes: string) {

/** bytes/string are dynamic, but aren't really arrays */
function fieldPortionData(field: RenderField) {
const methodNameSuffix = "";
if (field.arrayElement) {
const name = "_element";
const elementFieldData = { ...field.arrayElement, arrayElement: undefined, name, methodNameSuffix };
const elementFieldData = { ...field.arrayElement, arrayElement: undefined, name };
return {
typeWithLocation: field.arrayElement.typeWithLocation,
name,
Expand All @@ -267,7 +266,7 @@ function fieldPortionData(field: RenderField) {
};
} else {
const name = "_slice";
const elementFieldData = { ...field, name, methodNameSuffix };
const elementFieldData = { ...field, name };
return {
typeWithLocation: `${field.typeId} memory`,
name,
Expand Down
42 changes: 18 additions & 24 deletions packages/store/ts/codegen/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function renderRecordMethods(options: RenderTableOptions) {
if (options.withGetters) {
result += renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => `
/**
* @notice Get the full data${_commentSuffix}.
*/
Expand All @@ -39,20 +39,18 @@ export function renderRecordMethods(options: RenderTableOptions) {
);
}

result += renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix, _internal) => {
const externalArguments = renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
renderArguments(options.fields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`)),
]);
result += renderWithStore(storeArgument, ({ _typedStore, _store, _commentSuffix, _methodNamePrefix, _internal }) => {
const externalArguments = renderArguments([
_typedStore,
_typedTableId,
_typedKeyArgs,
renderArguments(options.fields.map(({ name, typeWithLocation }) => `${typeWithLocation} ${name}`)),
]);

const internalArguments =
"_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData" + (_internal ? ", _fieldLayout" : "");
const internalArguments =
"_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData" + (_internal ? ", _fieldLayout" : "");

return `
return `
/**
* @notice Set the full data using individual values${_commentSuffix}.
*/
Expand All @@ -64,13 +62,12 @@ export function renderRecordMethods(options: RenderTableOptions) {
${_store}.setRecord(${internalArguments});
}
`;
}
);
});

if (structName !== undefined) {
result += renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix, _internal) => {
({ _typedStore, _store, _commentSuffix, _methodNamePrefix, _internal }) => {
const externalArguments = renderArguments([
_typedStore,
_typedTableId,
Expand Down Expand Up @@ -137,13 +134,11 @@ export function renderDeleteRecordMethods(options: RenderTableOptions) {
const { storeArgument } = options;
const { _typedTableId, _typedKeyArgs, _keyTupleDefinition } = renderCommonData(options);

return renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix, _internal) => {
const externalArguments = renderArguments([_typedStore, _typedTableId, _typedKeyArgs]);
const internalArguments = "_tableId, _keyTuple" + (_internal ? ", _fieldLayout" : "");
return renderWithStore(storeArgument, ({ _typedStore, _store, _commentSuffix, _methodNamePrefix, _internal }) => {
const externalArguments = renderArguments([_typedStore, _typedTableId, _typedKeyArgs]);
const internalArguments = "_tableId, _keyTuple" + (_internal ? ", _fieldLayout" : "");

return `
return `
/**
* @notice Delete all data for given keys${_commentSuffix}.
*/
Expand All @@ -152,8 +147,7 @@ export function renderDeleteRecordMethods(options: RenderTableOptions) {
${_store}.deleteRecord(${internalArguments});
}
`;
}
);
});
}

// Renders the `decode` function that parses a bytes blob into the table data
Expand Down
2 changes: 1 addition & 1 deletion packages/store/ts/codegen/renderTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export function renderTable(options: RenderTableOptions) {
${renderWithStore(
storeArgument,
(_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => `
({ _typedStore, _store, _commentSuffix, _methodNamePrefix }) => `
/**
* @notice Register the table with its config${_commentSuffix}.
*/
Expand Down
2 changes: 0 additions & 2 deletions packages/store/ts/codegen/userType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { parseStaticArray } from "@latticexyz/config";
import { ImportDatum, RenderType, SolidityUserDefinedType } from "@latticexyz/common/codegen";
import { StoreConfig } from "../config";

export type UserTypeInfo = ReturnType<typeof getUserTypeInfo>;

/**
* Resolve an abi or user type into a SchemaType and RenderType
*/
Expand Down

0 comments on commit f3d1ac8

Please sign in to comment.