From b506d40efddfc075876b59d5cb6ae00984ec1a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dandelion=20Man=C3=A9?= Date: Fri, 21 Sep 2018 16:54:20 -0700 Subject: [PATCH] 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`. --- src/core/repoId.js | 4 +++- src/core/repoId.test.js | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/core/repoId.js b/src/core/repoId.js index cea97cc..f99ad27 100644 --- a/src/core/repoId.js +++ b/src/core/repoId.js @@ -10,6 +10,8 @@ export opaque type RepoId: {| +owner: string, |}; +export opaque type RepoIdString: string = string; + export const githubOwnerPattern = "[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]); } -export function repoIdToString(x: RepoId): string { +export function repoIdToString(x: RepoId): RepoIdString { return `${x.owner}/${x.name}`; } diff --git a/src/core/repoId.test.js b/src/core/repoId.test.js index 4226755..a7813ab 100644 --- a/src/core/repoId.test.js +++ b/src/core/repoId.test.js @@ -5,6 +5,7 @@ import { stringToRepoId, repoIdToString, type RepoId, + type RepoIdString, } from "./repoId"; describe("core/repoId", () => { @@ -22,6 +23,12 @@ describe("core/repoId", () => { const _unused_name: string = repoId.name; }); }); + describe("RepoIdString type", () => { + it("manually constructing a RepoIdString is illegal", () => { + // $ExpectFlowError + const _unused_repoIdString: RepoIdString = "foobar"; + }); + }); describe("makeRepoId", () => { it("allows a simple repoId", () => { makeRepoId("sourcecred", "sourcecred");