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(gatsby): convert gatsby-link to typescript #37543

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Kornil
Copy link
Contributor

@Kornil Kornil commented Jan 28, 2023

Description

Convert gatsby-link to TS.

Documentation

Related Issues

The previous PR was written 3 years ago and closed more than 6 months ago, as suggested in the closing message, I took the liberty of redoing it and open a new PR #22027

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jan 28, 2023
@Kornil
Copy link
Contributor Author

Kornil commented Jan 28, 2023

I do have a pesky ref type issue in packages/gatsby-link/src/index.tsx line 129 that I'd gladly accept suggestions on how to fix.

@Kornil Kornil changed the title Conver gatsby link to typescript chore(gatsby): convert gatsby-link to typescript Jan 28, 2023
@LekoArts LekoArts added topic: TypeScript Issues and PRs related to TS in general, public typings or gatsby-plugin-typescript and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jan 30, 2023
Copy link
Contributor

@LekoArts LekoArts left a comment

Choose a reason for hiding this comment

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

Hi, thanks for the PR!

Our CI should be running against your PR now so you can also check that to see your progress.

I've left a bunch of review comments on conventions, code style etc.

Ideally this PR doesn't change any behavior and only converts every piece. For this we'll also need to check the generated type definition file and compare it against the manually written one.

@@ -1 +1,8 @@
{}
{
Copy link
Contributor

Choose a reason for hiding this comment

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

This file change is not necessary since we use microbundle to generate the output.

You can check out https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-script to see how it's used (also has relevant package.json bits)

beforeEach(() => {
global.__BASE_PATH__ = ``
global.__PATH_PREFIX__ = ``
})

afterEach(cleanup)

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
Copy link
Contributor

Choose a reason for hiding this comment

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

This PR has a lot of eslint-disable for the explicit function return type. We have that linting rule for a reason 😅

Please remove all instances of that eslint-disable and add the return types

global.__BASE_PATH__ = pathPrefix
const source = createMemorySource(sourcePath)
const history = createHistory(source)

const utils = render(
<LocationProvider history={history}>
<Link
{...linkProps}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was that change necessary?

@@ -386,7 +419,7 @@ describe(`ref forwarding`, () => {
})

it(`handles a RefObject (React >=16.4)`, () => {
const ref = React.createRef(null)
const ref = React.createRef()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was that change necessary?

@@ -1,6 +1,17 @@
import * as React from "react"
import { NavigateFn, LinkProps } from "@reach/router" // These come from `@types/reach__router`

declare global {
Copy link
Contributor

Choose a reason for hiding this comment

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

The types should be autogenerated from the source now and the manual file should no longer exist


beforeEach(() => {
global.__TRAILING_SLASH__ = ``
global.__PATH_PREFIX__ = undefined
})

const getRewriteLinkPath = (option = `legacy`, pathPrefix = undefined) => {
const getRewriteLinkPath = (
option: string = `legacy`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of a string this should be the TrailingSlash type

@@ -110,7 +133,7 @@ class GatsbyLink extends React.Component {
// If IO supported and element reference found, setup Observer functionality
this.io = createIntersectionObserver(ref, inViewPort => {
if (inViewPort) {
this.abortPrefetch = this._prefetch()
this.abortPrefetch = this._prefetch() as { abort: () => void } | null
Copy link
Contributor

Choose a reason for hiding this comment

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

Type-casting is a code smell, it seems like abortPrefetch is not properly typed then

this.abortPrefetch = null
this.handleRef = this.handleRef.bind(this)
class GatsbyLink extends React.Component<IGatsbyLinkProps, IGatsbyLinkState> {
readonly state: IGatsbyLinkState = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Two things here:

So I'd go with the constructor for now to keep the functional changes minimal in this PR (more straight-forward conversion)

@@ -1,23 +1,28 @@
import { resolve } from "@gatsbyjs/reach-router"
// Specific import to treeshake Node.js stuff
import { applyTrailingSlashOption } from "gatsby-page-utils/apply-trailing-slash-option"
import { applyTrailingSlashOption, type TrailingSlash } from "gatsby-page-utils"
Copy link
Contributor

Choose a reason for hiding this comment

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

The import from gatsby-page-utils/apply-trailing-slash-option was on purpose for better tree-shaking. So if you need the type from the root, add an additional import instead:

import type { TrailingSlash } from "gatsby-page-utils"

@@ -0,0 +1,3 @@
{
Copy link
Contributor

@LekoArts LekoArts Jan 30, 2023

Choose a reason for hiding this comment

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

Re-use what gatsby-script has:

{
  "extends": "../../tsconfig.json",
  "exclude": ["node_modules", "src/__tests__", "dist"],
  "compilerOptions": {
    "declaration": false
  }
}

@LekoArts LekoArts added the topic: frontend Relates to frontend issues (e.g. reach/router, gatsby-link, page-loading, asset-loading, navigation) label Jan 30, 2023
@Kornil
Copy link
Contributor Author

Kornil commented Feb 16, 2023

Sorry I got busy irl and kinda forgot about this PR, I'll address all the feedback these days!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: frontend Relates to frontend issues (e.g. reach/router, gatsby-link, page-loading, asset-loading, navigation) topic: TypeScript Issues and PRs related to TS in general, public typings or gatsby-plugin-typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants