-
Notifications
You must be signed in to change notification settings - Fork 63
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking great so far! Will happily approve once un-drafted 🎉
* @param data - the list of data points to interpolate | ||
* @param threshold - the number of expected data points from the array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice to add some doc linting to either enforce or disallow the -
syntax in param descriptions. This PR uses both with and without it. I'm sure there's inconsistency with this in the rest of the codebase as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this in 6a3b521. I've currently restricted this rule to only apply to .ts
files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't mind, it'd probably be better to add it to #999 or just put it into a separate PR entirely. I'd like to go ahead and apply the rules to JS, TS and Vue SFCs as well in one go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this to JS and Vue is leading to a lot of errors because of the typedef
and JSDoc comments that are not compatible with TSDoc. It might be good to re-enable it for JS after a major chunk of the migration is complete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think it'd be better to use a generic JSDoc one than a TSDoc thing as TSDoc is actually a specific separate standard than JSDoc with different rules in some cases.
@sarayourfriend if that's okay I'm planning to hit all the utils in one go and reduce the overhead of approvals, merges and conflicts. |
@dhruvkb That might be fine for most of the utils, but I'd encourage splitting anything that has any runtime changes or substantial type additions into a separate PR, especially if the module isn't well unit tested. In my experience in adding types to various Gutenberg packages it was actually much faster go module by module mostly because it made it far easier for reviewers. If converting to TS just means renaming the file and adding parameter and return types to a function and nothing else then it's probably safe to batch those PRs. Otherwise the burden for the reviewer goes up a lot and I'd worry that you'd get stalled just waiting for reviews on a big PR. (Not to mention a bigger PR actually increases the merge conflict potential) |
src/utils/warn.ts
Outdated
export const getLogger = (level: 'log' | 'warn' | 'error') => | ||
process.env.NODE_ENV !== 'production' | ||
? console[level] | ||
: () => { | ||
// do nothing | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately this makes the types slightly worse than they were before. To get the right types we'll need to do something like this:
import { isNotProd } from '~/utils/dev'
type Console = typeof console
export const getLogger = <T extends keyof Console>(level: T): Console[T] =>
isNotProd
? console[level]
: (() => {
// do nothing
}) as Console[T]
export const warn = getLogger('warn')
export const log = getLogger('log')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably also rename the module to console
if it's going to include more than just warn
😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, updated in 3ad59c2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dhruvkb did you miss #1023 (comment) or was it something you think we shouldn't do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is slightly confusing because I'm still getting the right types. If I import log
or warn
from this module they show the same type signature as console.log
and console.warn
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the difference I see:
I'm not sure exactly why it squashes everything to (...data: any[]) => void
. I think because it's merging the empty function and the actual console function's type? But it doesn't fix it if you cast the empty function either so I don't think that's why really:
Anyway, it's not a big deal.
@sarayourfriend you're right. In that case, I'll stop this PR here and open it to review. |
@@ -76,7 +76,7 @@ COPY pnpm-lock.yaml . | |||
COPY .npmrc . | |||
|
|||
# copy the nuxt configuration file | |||
COPY --from=builder /usr/app/nuxt.config.js . | |||
COPY --from=builder /usr/app/nuxt.config.ts . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😱 For some reason I didn't actually think we could use TypeScript for the nuxt config until Nuxt bridge/Nuxt 3 but this is great!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: if the previous stage is the builder
and its entrypoint is npm run build
, does that means that we shouldn't have any .ts
file at this point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question 🤔 The Nuxt config is used both to generate the Webpack config but also to configure some runtime properties for Nuxt, so it's used both for building and for runtime. Nuxt's runtime doesn't use TypeScript though, at least not in Nuxt 2, I don't think. If this is working I'm not entire sure why unless the @nuxtjs/typescript
module added support for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I understand now, I went to the docs, and npm start
can differ its behavior depending the way you build the application (nuxt build
).
Since the deployment target is not being specified in the config file, then the default value is server
which means, we need Node.js to run the application from the generated .nuxt
folder.
nuxt start - Start the production server (after running nuxt build). Use it for Node.js hosting like Heroku, Digital Ocean, etc.
That means, at this point, if we want to keep using typescript (after building the application from the previous stage) we will require ts-node
. Or, my preferred option, as you said Sara, don't convert the config file so it can be read by Node.js runtime without any external dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'd prefer if we kept it as TS for now until we move to Nuxt bridge. To use TS for this properly it looks like we'd have to bring in another module for it: https://typescript.nuxtjs.org/guide/runtime/
We can always add type-checking to the file even if it stays as JSDoc though; it just can't consume actual TS files (which means anything we use in the nuxt.config.js
needs to stay as regular JS with JSDoc types instead of getting converted to TypeScript).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't use TS for nuxt-config
, we cannot import the TypeScript versions of the utils node-env
, env
and sentry-config
in it. I'm not entirely sure why making the nuxt-config
file TS works but if I change it to JS, it can no longer import any TS files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JS files can't import TS files without the JS files being compiled first. Because a .js
nuxt.config doesn't get compiled, it won't be able to import TS directly I guess (because it's not importing anything from the compiled bundle).
Considering the e2e tests are passing (they run on the pnpm build && pnpm start
version of the app, for @rbadillap's context) I guess it's safe.
Also don't know why it's working though (which scares me a little bit 😅)
src/utils/env.ts
Outdated
export const env: Record<string, string> = { | ||
apiUrl: process.env.API_URL ?? 'https://api.openverse.engineering/v1/', | ||
filterStorageKey: 'openverse-filter-visibility', | ||
notificationStorageKey: 'openverse-show-notification', | ||
enableInternalAnalytics: stringToBoolean( | ||
process.env.ENABLE_INTERNAL_ANALYTICS | ||
), | ||
enableInternalAnalytics: process.env.ENABLE_INTERNAL_ANALYTICS ?? 'false', | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use as const
here: it's perfect for this kind of thing. Record<string, string>
is actually even worse than what TypeScript infers as it swallows all the key names. as const
is the best though as it will turn constant strings into constant string types instead of just string
. Changing enableInternalAnalytics
to a string
boolean representation instead of just a plain boolean also strikes me as a step backwards, what was the reason for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is because Nuxt only accepts the env
as a mapping of string keys and string values which makes sense as env vars are always strings. Where we actually use enableInternalAnalytics
in store/usage-data.js
, we are already converting the string to boolean.
Changed to as const
in 5acea54.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense! I wonder what a good way to enforce that would be 🤔 I guess Record<string, string>
would be it but having the keys swallowed is a bit of a bummer 😕
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We wouldn't need to enforce this, tsc
will raise an error if we use anything other than a string as the value. Also using as const
with it works.
@@ -46,12 +46,10 @@ | |||
"src/data/api-service.js", | |||
"src/data/usage-data-service.js", | |||
"src/store/user.js", | |||
"src/utils/decode-data.js", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To get nuxt.config.ts
included in the type checking we'll need to change the first entry in includes
to just **/*.ts
I think. Right now it's only type checking stuff inside the src
file (which isn't good considering we'll be adding TypeScript to the test
directory as well with the visual regression stuff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Still not sure about the TSDoc linting instead of just JSDoc, would like others to give input there. The console types I'd personally like changed but are also not a blocker.
plugins: ['@typescript-eslint', 'vue', 'vuejs-accessibility', 'unicorn'], | ||
plugins: [ | ||
'@typescript-eslint', | ||
'eslint-plugin-tsdoc', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think we might want to use https://www.npmjs.com/package/eslint-plugin-jsdoc instead of a TSDoc one. We have no real idea of how long Vue files will be excluded from TS and it'd be nice to have consistency across the stack with our JSDoc comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's address the JSDoc changes in #1029.
type: process.env.NODE_ENV === 'test' ? Object : String, | ||
type: isTest ? Object : String, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to check that dead code elimination still works when you don't inline the NODE_ENV check. I don't think we have environment based dead code currently but it would be good to add that. We might end up having to revert these checks back to the inline but I'm not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll make a new issue for this, it seems that this should work because it'll evaluate to
if (false) {
// ...
}
in the compiled JS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened #1030.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exciting changes! Thank you for keeping this PR small. With all of the console
changes it already looks kind of intimidating :)
Fixes
Works on milestone 10 i.e. TypeScriptification
Fixes #922
Fixes #924
Fixes #925
Fixes #926
Fixes #936
Fixes #938
Description
This PR
Testing Instructions
Passing CI should be sufficient imo. You can try checking out the PR, running the dev server and playing with it for a bit. Repeat with the Storybook.
Checklist
Update index.md
).main
) or a parent feature branch.Developer Certificate of Origin
Developer Certificate of Origin