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 cache.js to TypeScript #20622

Closed
wants to merge 2 commits into from

Conversation

ascorbic
Copy link
Contributor

Description

This includes the changes to enable Jest to run on TypeScript files that were in #20457. It also has a couple of additions to tsconfig to reflect the fact we're running in Node not the browser

Documentation

n/a

Related Issues

#20457

@ascorbic ascorbic requested a review from a team as a code owner January 15, 2020 14:23

get<T = unknown>(key): Promise<T | undefined> {
return new Promise(resolve => {
// eslint-disable-next-line no-unused-expressions
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ESLint is complaining here, which I think is because it's an older version that doesn't recognize optional chaining

@@ -1,5 +1,8 @@
{
"compilerOptions": {
"target": "ESNext",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is the right target. I don't think it affects the actual compilation, as that's up to Bable. It's just for typechecking, so it knows which language features are available

},
{
store: this.store,
ttl: TTL,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

cache-manager says this is a required proeprty, even though the fs-hash store uses the one inside options

@@ -1,5 +1,8 @@
{
"compilerOptions": {
"target": "ESNext",
"lib": ["ES2015"],
"moduleResolution": "node",
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey there! Can you explain these config changes to me?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing.

  • target Even though Babel chooses the actual compilation target, the type checker uses this to see if the target supports certain language features. I added this target because it defaults to ES3, which doesn't support getters and setters, so TS complains if you use them.
  • lib. The default lib includes DOM, which is incorrect and was causing a conflic with Cache
  • moduelResolution. Just telling it that we're using node. It was complaining about the json resolution option without it

key: string,
value: T,
args: CachingConfig = { ttl: TTL }
): Promise<T | undefined> {
Copy link
Contributor

Choose a reason for hiding this comment

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

We had a goal to avoid using generics as much as possible. Do you think it's possible to do here? I'm not totally familiar with the cache module yet. Is the value supposed to be any specific expected types? I would look through the codebase and see when we use this if we align on any specific types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Generics are unavoidable if you're typing Promises. I'm also using them for the cache getters and setters, because the return type depends on the input type.

return new Promise(resolve => {
// eslint-disable-next-line no-unused-expressions
this.cache?.set(key, value, args, err => {
resolve(err ? undefined : value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use git mv to preserve git history? It's hard to review this file because I can't tell what's different. E.g., this resolve(undefined) on error feels like a bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm a bit surpised it didn't pick up that this was a rename. That resolve(undefined) was in the original file.

@@ -34,8 +34,7 @@ module.exports = {
`__tests__/fixtures`,
],
transform: {
"^.+\\.js$": `<rootDir>/jest-transformer.js`,
"^.+\\.tsx?$": `<rootDir>/node_modules/ts-jest/preprocessor.js`,
"^.+\\.[jt]sx?$": `<rootDir>/jest-transformer.js`,
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@blainekasten blainekasten left a comment

Choose a reason for hiding this comment

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

Thanks for jumping in here! Really love the community getting involved in this TS transformation. My main request at this oint is that we need to use git mv to change the file to ts instead of creating a new git file.

To fix this you should do this:

  1. Change file back to .js and commit it
  2. Use the command git mv packages/gatsby/src/utils/cache.js packages/gatsby/src/utils/cache.ts and commit it

@ascorbic
Copy link
Contributor Author

Replacing with #20626

@ascorbic ascorbic closed this Jan 15, 2020
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.

2 participants