Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(read-manifest): improve type safety of manifest validation logic #7345

Merged
merged 5 commits into from
Nov 28, 2023

Conversation

gluxon
Copy link
Member

@gluxon gluxon commented Nov 26, 2023

Follow up to #7278. I happened to be in this code path while working on #7072 and noticed a few ways the validation logic could be more type safe.

There should be no behavior changes in this PR. Just moving some functions and type annotations around.

@@ -9,11 +9,8 @@ export interface WorkspaceManifest {

export async function readWorkspaceManifest (dir: string): Promise<WorkspaceManifest | undefined> {
const manifest = await readManifestRaw(dir)
if (validateWorkspaceManifest(manifest)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if statement here was a bit redundant since validateWorkspaceManifest throws an error if the manifest isn't valid.

@@ -30,11 +27,10 @@ async function readManifestRaw (dir: string): Promise<unknown> {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function validateWorkspaceManifest (manifest: any): manifest is WorkspaceManifest | undefined {
function validateWorkspaceManifest (manifest: unknown): asserts manifest is WorkspaceManifest | undefined {
Copy link
Member Author

@gluxon gluxon Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript has 2 kinds of user-defined type guards.

  1. Predicates that simply return true or false without throwing.
  2. Assertion functions that return void and throw if the assertion fails.

This function is currently written as an in-between. I think the intention was for this to be an Assertion function, which avoids having to return true and an if statement in the calling path.

@@ -30,11 +27,10 @@ async function readManifestRaw (dir: string): Promise<unknown> {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function validateWorkspaceManifest (manifest: any): manifest is WorkspaceManifest | undefined {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the any in favor of unknown. The later allows us to use TypeScript itself to ensure our validation logic is correct.

@gluxon
Copy link
Member Author

gluxon commented Nov 26, 2023

@JasonMan34 Thanks for the validation logic in #7278! That initial PR was very helpful. 🙂 I had a few suggestions in this PR if you've got time to review.

@gluxon
Copy link
Member Author

gluxon commented Nov 26, 2023

Fixing the --rerun-from test flakes here: #7346

@gluxon gluxon marked this pull request as ready for review November 26, 2023 22:33
@gluxon gluxon requested a review from zkochan as a code owner November 26, 2023 22:33
@zkochan zkochan merged commit e8926e9 into pnpm:main Nov 28, 2023
7 of 10 checks passed
@gluxon gluxon deleted the manifest-validation-improvements branch January 1, 2024 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants