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

chore: bump @typescript-eslint/no-unused-vars to error internally #11173

Merged
merged 8 commits into from
Jul 24, 2024

Conversation

JoshuaKGoldberg
Copy link
Contributor

Changes

(please forgive me for wiping the template for this internal-only chore)

Upgrades @typescript-eslint/no-unused-vars from 'warn' to 'error' in the ESLint config, then resolves the roughly 20 complaints that were hanging around from it. It's a nice amount of deletions, I think. 🙂

I don't believe any of the changed types impact users. But just to be safe, added comments inline.

Copy link

changeset-bot bot commented May 31, 2024

⚠️ No Changeset found

Latest commit: 2f923ca

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added pkg: integration Related to any renderer integration (scope) pkg: astro Related to the core `astro` package (scope) pr: docs A PR that includes documentation for review labels May 31, 2024
@@ -42,7 +42,7 @@ const statusToCodeMap: Record<number, ActionErrorCode> = Object.entries(codeToSt

export type ErrorInferenceObject = Record<string, any>;

export class ActionError<T extends ErrorInferenceObject = ErrorInferenceObject> extends Error {
export class ActionError extends Error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This T was unused 🔪 . And, as a result, SafeResult's TInput later on becomes unused as well. 🔪

Copy link
Member

Choose a reason for hiding this comment

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

lgtm, @bholmesdev can you confirm?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is required to preserve the error object when using isInputError! It's a strange pattern, I'm aware, but we should definitely keep that around.

Copy link
Contributor

@bholmesdev bholmesdev Jul 22, 2024

Choose a reason for hiding this comment

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

I've documented the reason for this generic type here. The generic is used by ActionInputError (a descendant of ActionError). However, I've found that if you remove this generic from ActionError, the isInputError() utility is unable to apply the generic correctly. If you have a suggestion to avoid this, please let me know @JoshuaKGoldberg. We now have a type test to assert whether removing this generic causes any issue

Copy link
Contributor Author

@JoshuaKGoldberg JoshuaKGoldberg Jul 23, 2024

Choose a reason for hiding this comment

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

Aha! Thanks, that's helpful.

The root problem here is that your isInputError type is not fully type-safe. It violates the "Golden Rule of Generics". Nothing in the body of isInputError actually uses the type parameter T. You could call it like...

const example = new ActionError({ code: "BAD_REQUEST" });

if (isInputError<{ gotcha: "whoops" }>(example) {
	example.fields.gotcha;
	//             ^? string[] | undefined

}

...and TypeScript wouldn't know to complain.

Aside: which we recently codified that golden rule in typescript-eslint as @typescript-eslint/no-unnecessary-type-parameters... but the lint rule doesn't (yet?) have the ability to check type parameters passed as type arguments - see its Limitations). Pity. This wouldn't be caught by linting right now.

I don't have enough context to refactor things to not violate the golden rule. Sometimes the right approach actually is to just eslint-disable-next-line the unused thing and/or only-used-once type parameter. So I'll revert these removals for now with a comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, that's an interesting callout. I've stumbled into TypeScript before learning to rules, so I'm not surprised I violated one 😄

I know what I want to achieve with the generic, I'm just not sure how to go about it differently. I'll make a lower-priority note to revisit that. This is the second time we've had a "what the heck?" moment with that code.

},
},
};
},
Copy link
Contributor Author

@JoshuaKGoldberg JoshuaKGoldberg May 31, 2024

Choose a reason for hiding this comment

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

All this 'build:before' hook was doing was assigning to the ssrPluginContext variable... which was then never used. So I believe removing it altogether is safe, right?

@JoshuaKGoldberg JoshuaKGoldberg changed the title chore: enable @typescript-eslint/no-unused-vars internally chore: bump @typescript-eslint/no-unused-vars to error internally May 31, 2024
packages/astro/e2e/view-transitions.test.js Show resolved Hide resolved
@@ -42,7 +42,7 @@ const statusToCodeMap: Record<number, ActionErrorCode> = Object.entries(codeToSt

export type ErrorInferenceObject = Record<string, any>;

export class ActionError<T extends ErrorInferenceObject = ErrorInferenceObject> extends Error {
export class ActionError extends Error {
Copy link
Member

Choose a reason for hiding this comment

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

lgtm, @bholmesdev can you confirm?

@@ -4,8 +4,7 @@ export const isValidUrl = (s: any) => {
return false;
}
try {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const dummy = new URL(s);
new URL(s);
Copy link
Member

Choose a reason for hiding this comment

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

Could we even use URL.canParse?

florian-lefebvre and others added 3 commits July 9, 2024 15:13
Thought it is better to use the count than to delete it :)
@bluwy
Copy link
Member

bluwy commented Jul 22, 2024

We've discussed this among the platform team and think this is a good change. Would need the conflicts resolved first before merging.

@github-actions github-actions bot added feat: markdown Related to Markdown (scope) pkg: svelte Related to Svelte (scope) pkg: vue Related to Vue (scope) pkg: example Related to an example package (scope) pkg: react Related to React (scope) pkg: preact Related to Preact (scope) pkg: solid Related to Solid (scope) pkg: lit Related to Lit (scope) pkg: create-astro Related to the `create-astro` package (scope) labels Jul 22, 2024
Copy link
Contributor

@bholmesdev bholmesdev left a comment

Choose a reason for hiding this comment

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

I've responded to the comment above on the unused generic for the ActionError object. I would vote to preserve this generic with a clear comment on why it is present. If we can find a TypeScript refactor for isInputError() to preserves the generic, that would be best.

@github-actions github-actions bot removed feat: markdown Related to Markdown (scope) pkg: svelte Related to Svelte (scope) labels Jul 23, 2024
@github-actions github-actions bot removed pkg: vue Related to Vue (scope) pkg: example Related to an example package (scope) pkg: react Related to React (scope) pkg: preact Related to Preact (scope) pkg: solid Related to Solid (scope) pkg: lit Related to Lit (scope) pkg: create-astro Related to the `create-astro` package (scope) labels Jul 23, 2024
@bluwy bluwy merged commit 87c179a into withastro:main Jul 24, 2024
14 checks passed
@JoshuaKGoldberg JoshuaKGoldberg deleted the no-unused-vars-error branch July 24, 2024 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope) pr: docs A PR that includes documentation for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants