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/types #11715

Merged
merged 8 commits into from
Aug 16, 2024
Merged

Refactor/types #11715

merged 8 commits into from
Aug 16, 2024

Conversation

Princesseuh
Copy link
Member

@Princesseuh Princesseuh commented Aug 15, 2024

Changes

Back in 1997, when Astro was just getting started, someone created an innocent @types/astro.ts file, mostly intended for small types used throughout the codebase and types for untyped modules. With time, this file grew with a mix of internal, public, types from dependencies, unused types etc, to a total of 3000 lines of types.

This PR attempt to untangle that file, only keeping public what should actually be public and going away from the easily confused with DefinitelyTyped's stuff @types to a normal folder called.. types. I also took this opportunity to split the types into multiple files, just so we don't have to scroll 3000 lines of types to find something. No big thoughts went into the splits, just put things where it seemed they'd belong.

Throughout this journey, I discovered that more types than I thought were exposed as part of the public API, perhaps as a mistake.

Testing

It builds! We don't have a lot of tests regarding types, but the little we do have do pass too. Smoke tests will fail because of docs docgen script, which need to get adapted to this new structure.

Docs

withastro/docs#9109

Copy link

changeset-bot bot commented Aug 15, 2024

🦋 Changeset detected

Latest commit: da94ba7

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

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

@Princesseuh Princesseuh changed the base branch from main to next August 15, 2024 03:16
@github-actions github-actions bot added pkg: integration Related to any renderer integration (scope) pkg: astro Related to the core `astro` package (scope) semver: major Change triggers a `major` release labels Aug 15, 2024
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

This PR is blocked because it contains a major changeset. A reviewer will merge this at the next release if approved.

@@ -131,7 +131,7 @@ export function glob(globOptions: GlobOptions): Loader {
if (entryType.getRenderFunction) {
let render = renderFunctionByContentType.get(entryType);
if (!render) {
render = await entryType.getRenderFunction(settings);
render = await entryType.getRenderFunction(settings.config);
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the only runtime change I did, the way it was setup, it was exposing AstroSettings publicly, which isn't in line with what we typically do

@@ -0,0 +1,202 @@
// TODO: Should the types here really be public?
Copy link
Member Author

@Princesseuh Princesseuh Aug 15, 2024

Choose a reason for hiding this comment

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

A lot of things in this file feel like they shouldn't be public, but end up being for various reasons. The most concerning of which was RouteData, which is exposed on integrations hooks.

A lot of the SSR*** ones are also spooky, but they're used by renderers which is not too bad

@github-actions github-actions bot removed the pkg: integration Related to any renderer integration (scope) label Aug 15, 2024
Comment on lines -5 to -10
// eslint-disable-next-line @typescript-eslint/no-namespace
declare namespace App {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Locals {}
}

Copy link
Member Author

Choose a reason for hiding this comment

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

This is unnecessary, the default definition is already that. However, it was done in a .d.ts file, which don't get sent to dist so it never worked.

@@ -191,7 +185,6 @@ declare module '*.md' {
file,
url,
getHeadings,
getHeaders,
Copy link
Member Author

Choose a reason for hiding this comment

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

That method hasn't existed in a while.

Comment on lines -7 to -8
jsxImportSource: 'astro',
jsxTransformOptions,
Copy link
Member Author

Choose a reason for hiding this comment

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

Those options don't seem to exist anymore? There was a deprecated message on both of them.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I'm planning to remove the entire JSX handling here in a separate PR once I refactor the MDX intgeration stuff. Removing it here now is fine though.


/** Renders an endpoint request to completion, returning the body. */
export async function renderEndpoint(
mod: EndpointHandler,
mod: {
[method: string]: APIRoute;
Copy link
Member Author

Choose a reason for hiding this comment

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

Type was fully internal to this line, figured there was no point to it being a separate type.

@@ -198,10 +197,6 @@ export async function runHookConfigSetup({
addWatchFile: (path) => {
updatedSettings.watchFiles.push(path instanceof URL ? fileURLToPath(path) : path);
},
addDevOverlayPlugin: (entrypoint) => {
// TODO add a deprecation warning in Astro 5.
Copy link
Member Author

@Princesseuh Princesseuh Aug 15, 2024

Choose a reason for hiding this comment

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

The TODO here is wrong, this API was unsupported and undocumented before the toolbar actually became stable, Astro 5 is prime time for fully removing it (the only app that uses it, Sentry, only uses it as a fallback)

@github-actions github-actions bot added 🚨 action Modifies GitHub Actions pkg: integration Related to any renderer integration (scope) pr: docs A PR that includes documentation for review labels Aug 15, 2024
@Princesseuh Princesseuh marked this pull request as ready for review August 15, 2024 03:56
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

I hope you used some sort of refactoring tool that made this journey less painful 😅 But anyways I stepped through each of them and it looks fine to me.

  1. Should we also update
    - `@types/`: TypeScript types. These are centralized to cut down on circular dependencies
  2. In what case would we use the ts files in types/*.ts (not the public folder)? Should we place private shared types in types/astro.ts, or should we lean towards putting types inside each domains' file/folder instead? (for example, there's a lot of types.ts file.

@Princesseuh
Copy link
Member Author

Yea, should definitely update that!

I'm not sure! I think that it's fine for some very specific types that are used everywhere perhaps, but overall it seems like over time we've done more and more random types files closer to where the feature live. Do you have a preference?

@bluwy
Copy link
Member

bluwy commented Aug 15, 2024

I like to group them by domain, so the types.ts file is fine for me, or if it's inlined to the file that uses it first. If we're ok with this pattern too, it would be nice to put a comment in types/astro.ts that describes when to use or not use it. Maybe for types that really encompasses Astro, like AstroSettings is fine to be there though.

@Princesseuh
Copy link
Member Author

I'll add some documentation all around

@Princesseuh Princesseuh merged commit d74617c into next Aug 16, 2024
12 of 14 checks passed
@Princesseuh Princesseuh deleted the refactor/types branch August 16, 2024 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚨 action Modifies GitHub Actions 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 semver: major Change triggers a `major` release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants