mirror of
https://github.com/status-im/sourcecred.git
synced 2025-01-13 14:14:57 +00:00
Add the RepoIdString
type (#885)
This modifies `core/repoId` so that `repoIdToString` returns not a `string`, but an opaque subtype of `string` called `RepoIdString`. This allows us to store stringified `RepoId`s (which is useful whenever we want value-not-reference semantics, like for use as a map key) while still maintaining a type assertion that the strings, in fact, represent valid `RepoId`s. Test plan: A unit test verifies that it's a flow error to cast a string to a `RepoIdString`.
This commit is contained in:
parent
838092194b
commit
b506d40efd
@ -10,6 +10,8 @@ export opaque type RepoId: {|
|
|||||||
+owner: string,
|
+owner: string,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
|
export opaque type RepoIdString: string = string;
|
||||||
|
|
||||||
export const githubOwnerPattern = "[A-Za-z0-9-]+";
|
export const githubOwnerPattern = "[A-Za-z0-9-]+";
|
||||||
export const githubRepoPattern = "[A-Za-z0-9-._]+";
|
export const githubRepoPattern = "[A-Za-z0-9-._]+";
|
||||||
|
|
||||||
@ -33,6 +35,6 @@ export function stringToRepoId(x: string): RepoId {
|
|||||||
return makeRepoId(pieces[0], pieces[1]);
|
return makeRepoId(pieces[0], pieces[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function repoIdToString(x: RepoId): string {
|
export function repoIdToString(x: RepoId): RepoIdString {
|
||||||
return `${x.owner}/${x.name}`;
|
return `${x.owner}/${x.name}`;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import {
|
|||||||
stringToRepoId,
|
stringToRepoId,
|
||||||
repoIdToString,
|
repoIdToString,
|
||||||
type RepoId,
|
type RepoId,
|
||||||
|
type RepoIdString,
|
||||||
} from "./repoId";
|
} from "./repoId";
|
||||||
|
|
||||||
describe("core/repoId", () => {
|
describe("core/repoId", () => {
|
||||||
@ -22,6 +23,12 @@ describe("core/repoId", () => {
|
|||||||
const _unused_name: string = repoId.name;
|
const _unused_name: string = repoId.name;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe("RepoIdString type", () => {
|
||||||
|
it("manually constructing a RepoIdString is illegal", () => {
|
||||||
|
// $ExpectFlowError
|
||||||
|
const _unused_repoIdString: RepoIdString = "foobar";
|
||||||
|
});
|
||||||
|
});
|
||||||
describe("makeRepoId", () => {
|
describe("makeRepoId", () => {
|
||||||
it("allows a simple repoId", () => {
|
it("allows a simple repoId", () => {
|
||||||
makeRepoId("sourcecred", "sourcecred");
|
makeRepoId("sourcecred", "sourcecred");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user