-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@latticexyz/store": patch | ||
--- | ||
|
||
Refactored tablegen in preparation for multiple namespaces and addressed a few edge cases: | ||
|
||
- User types configured with a relative `filePath` are now resolved relative to the project root (where the `mud.config.ts` lives) rather than the current working directory. | ||
- User types inside libraries now need to be referenced with their fully-qualified code path (e.g. `LibraryName.UserTypeName`). |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./common"; | ||
export * from "./renderEnums"; | ||
export * from "./renderImportPath"; | ||
export * from "./renderTypeHelpers"; | ||
export * from "./types"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { renderImportPath } from "./renderImportPath"; | ||
|
||
describe("renderImportPath", () => { | ||
it("returns valid path for package imports", () => { | ||
expect(renderImportPath("@latticexyz/store/src")).toMatchInlineSnapshot('"@latticexyz/store/src"'); | ||
expect(renderImportPath("@latticexyz/store/src/")).toMatchInlineSnapshot('"@latticexyz/store/src"'); | ||
expect(renderImportPath("@latticexyz/store/src", "IStore.sol")).toMatchInlineSnapshot( | ||
'"@latticexyz/store/src/IStore.sol"', | ||
); | ||
expect(renderImportPath("@latticexyz/store/src/", "IStore.sol")).toMatchInlineSnapshot( | ||
'"@latticexyz/store/src/IStore.sol"', | ||
); | ||
expect(renderImportPath("@latticexyz/store/src", "codegen/tables/Tables.sol")).toMatchInlineSnapshot( | ||
'"@latticexyz/store/src/codegen/tables/Tables.sol"', | ||
); | ||
expect(renderImportPath("@latticexyz/store/src/", "codegen/tables/Tables.sol")).toMatchInlineSnapshot( | ||
'"@latticexyz/store/src/codegen/tables/Tables.sol"', | ||
); | ||
expect(renderImportPath("@latticexyz/store/src", "./codegen/tables/Tables.sol")).toMatchInlineSnapshot( | ||
'"@latticexyz/store/src/codegen/tables/Tables.sol"', | ||
); | ||
expect(renderImportPath("@latticexyz/store/src/", "./codegen/tables/Tables.sol")).toMatchInlineSnapshot( | ||
'"@latticexyz/store/src/codegen/tables/Tables.sol"', | ||
); | ||
expect(renderImportPath("@latticexyz/store/src", "../test/codegen/common.sol")).toMatchInlineSnapshot( | ||
'"@latticexyz/store/test/codegen/common.sol"', | ||
); | ||
expect(renderImportPath("@latticexyz/store/src/", "../test/codegen/common.sol")).toMatchInlineSnapshot( | ||
'"@latticexyz/store/test/codegen/common.sol"', | ||
); | ||
}); | ||
|
||
it("returns valid path for relative imports", () => { | ||
expect(renderImportPath(".")).toMatchInlineSnapshot('"."'); | ||
expect(renderImportPath("./")).toMatchInlineSnapshot('"."'); | ||
expect(renderImportPath(".", "IStore.sol")).toMatchInlineSnapshot('"./IStore.sol"'); | ||
expect(renderImportPath("./", "IStore.sol")).toMatchInlineSnapshot('"./IStore.sol"'); | ||
expect(renderImportPath("./src")).toMatchInlineSnapshot('"./src"'); | ||
expect(renderImportPath("./src/")).toMatchInlineSnapshot('"./src"'); | ||
expect(renderImportPath("./src", "IStore.sol")).toMatchInlineSnapshot('"./src/IStore.sol"'); | ||
expect(renderImportPath("../test/", "IStore.sol")).toMatchInlineSnapshot('"../test/IStore.sol"'); | ||
expect(renderImportPath("../test")).toMatchInlineSnapshot('"../test"'); | ||
expect(renderImportPath("../test/")).toMatchInlineSnapshot('"../test"'); | ||
expect(renderImportPath("../test", "IStore.sol")).toMatchInlineSnapshot('"../test/IStore.sol"'); | ||
expect(renderImportPath("../test/", "IStore.sol")).toMatchInlineSnapshot('"../test/IStore.sol"'); | ||
expect(renderImportPath(".", "codegen/tables/Tables.sol")).toMatchInlineSnapshot('"./codegen/tables/Tables.sol"'); | ||
expect(renderImportPath("./", "codegen/tables/Tables.sol")).toMatchInlineSnapshot('"./codegen/tables/Tables.sol"'); | ||
expect(renderImportPath(".", "./codegen/tables/Tables.sol")).toMatchInlineSnapshot('"./codegen/tables/Tables.sol"'); | ||
expect(renderImportPath("./", "./codegen/tables/Tables.sol")).toMatchInlineSnapshot( | ||
'"./codegen/tables/Tables.sol"', | ||
); | ||
expect(renderImportPath(".", "../test/codegen/common.sol")).toMatchInlineSnapshot('"../test/codegen/common.sol"'); | ||
expect(renderImportPath("./", "../test/codegen/common.sol")).toMatchInlineSnapshot('"../test/codegen/common.sol"'); | ||
expect(renderImportPath("./src", "codegen/tables/Tables.sol")).toMatchInlineSnapshot( | ||
'"./src/codegen/tables/Tables.sol"', | ||
); | ||
expect(renderImportPath("./src/", "codegen/tables/Tables.sol")).toMatchInlineSnapshot( | ||
'"./src/codegen/tables/Tables.sol"', | ||
); | ||
expect(renderImportPath("./src", "./codegen/tables/Tables.sol")).toMatchInlineSnapshot( | ||
'"./src/codegen/tables/Tables.sol"', | ||
); | ||
expect(renderImportPath("./src/", "./codegen/tables/Tables.sol")).toMatchInlineSnapshot( | ||
'"./src/codegen/tables/Tables.sol"', | ||
); | ||
expect(renderImportPath("./src", "../test/codegen/common.sol")).toMatchInlineSnapshot( | ||
'"./test/codegen/common.sol"', | ||
); | ||
expect(renderImportPath("./src/", "../test/codegen/common.sol")).toMatchInlineSnapshot( | ||
'"./test/codegen/common.sol"', | ||
); | ||
}); | ||
|
||
it("normalizes to POSIX paths", () => { | ||
expect(renderImportPath("C:\\src")).toMatchInlineSnapshot('"C:/src"'); | ||
expect(renderImportPath("C:\\src\\")).toMatchInlineSnapshot('"C:/src"'); | ||
expect(renderImportPath("C:\\src", "./IStore.sol")).toMatchInlineSnapshot('"C:/src/IStore.sol"'); | ||
expect(renderImportPath("C:\\src\\", "./IStore.sol")).toMatchInlineSnapshot('"C:/src/IStore.sol"'); | ||
expect(renderImportPath("./src", ".\\IStore.sol")).toMatchInlineSnapshot('"./src/IStore.sol"'); | ||
expect(renderImportPath("./src", ".\\IStore.sol")).toMatchInlineSnapshot('"./src/IStore.sol"'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import path from "node:path"; | ||
|
||
// This will probably break for backslash-escaped POSIX paths, | ||
// but we'll worry about that later. | ||
function winToPosix(segment: string): string { | ||
return segment.replaceAll(path.win32.sep, path.posix.sep); | ||
} | ||
|
||
export function renderImportPath(basePath: string, ...segments: readonly string[]): string { | ||
// Solidity compiler expects POSIX paths | ||
const fullPath = path.posix | ||
.join(winToPosix(basePath), ...segments.map(winToPosix)) | ||
// remove trailing slash | ||
.replace(/\/$/, ""); | ||
|
||
// `path.join` strips the leading `./` | ||
// so if we started with a relative path, make it relative again | ||
if (basePath.startsWith(".")) { | ||
const relativePath = "./" + fullPath; | ||
return relativePath.replace(/^(\.\/)+\./, "."); | ||
} | ||
|
||
return fullPath; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.