Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Convert 6 utils to TypeScript #1023

Merged
merged 13 commits into from
Mar 1, 2022
Merged

Convert 6 utils to TypeScript #1023

merged 13 commits into from
Mar 1, 2022

Conversation

dhruvkb
Copy link
Member

@dhruvkb dhruvkb commented Feb 28, 2022

Fixes

Works on milestone 10 i.e. TypeScriptification

Fixes #922
Fixes #924
Fixes #925
Fixes #926
Fixes #936
Fixes #938

Description

This PR

  • converts a number of utilities to TypeScript
  • updates usages of TS-ified utilities
  • adds a lint rule for proper TSDoc comments
  • converts the Nuxt config to TS and fixes type mismatches
  • refactors code as needed

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

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@dhruvkb dhruvkb added 🟨 priority: medium Not blocking but should be addressed soon ✨ goal: improvement Improvement to an existing user-facing feature 🤖 aspect: dx Concerns developers' experience with the codebase labels Feb 28, 2022
Copy link
Contributor

@sarayourfriend sarayourfriend left a 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 🎉

src/utils/decode-data.ts Show resolved Hide resolved
Comment on lines +33 to +34
* @param data - the list of data points to interpolate
* @param threshold - the number of expected data points from the array
Copy link
Contributor

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.

Copy link
Member Author

@dhruvkb dhruvkb Feb 28, 2022

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.

Copy link
Contributor

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.

Copy link
Member Author

@dhruvkb dhruvkb Feb 28, 2022

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.

Copy link
Contributor

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.

@dhruvkb
Copy link
Member Author

dhruvkb commented Feb 28, 2022

@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.

@sarayourfriend
Copy link
Contributor

sarayourfriend commented Feb 28, 2022

@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)

Comment on lines 1 to 6
export const getLogger = (level: 'log' | 'warn' | 'error') =>
process.env.NODE_ENV !== 'production'
? console[level]
: () => {
// do nothing
}
Copy link
Contributor

@sarayourfriend sarayourfriend Feb 28, 2022

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')

Copy link
Contributor

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 😁

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 makes sense, updated in 3ad59c2.

Copy link
Contributor

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?

Copy link
Member Author

@dhruvkb dhruvkb Mar 1, 2022

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

@dhruvkb
Copy link
Member Author

dhruvkb commented Feb 28, 2022

@sarayourfriend you're right. In that case, I'll stop this PR here and open it to review.

@dhruvkb dhruvkb marked this pull request as ready for review February 28, 2022 18:09
@dhruvkb dhruvkb requested a review from a team as a code owner February 28, 2022 18:09
@@ -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 .
Copy link
Contributor

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!!

Copy link
Contributor

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?

Copy link
Contributor

@sarayourfriend sarayourfriend Feb 28, 2022

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.

Copy link
Contributor

@rbadillap rbadillap Feb 28, 2022

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.

Copy link
Contributor

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).

Copy link
Member Author

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.

Copy link
Contributor

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/locales/scripts/types.d.ts Outdated Show resolved Hide resolved
src/utils/dev.ts Outdated Show resolved Hide resolved
src/utils/env.ts Outdated
Comment on lines 5 to 10
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',
}
Copy link
Contributor

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?

Copy link
Member Author

@dhruvkb dhruvkb Feb 28, 2022

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.

Copy link
Contributor

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 😕

Copy link
Member Author

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",
Copy link
Contributor

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.

Copy link
Contributor

@sarayourfriend sarayourfriend left a 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',
Copy link
Contributor

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.

Copy link
Member Author

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.

Comment on lines -36 to +38
type: process.env.NODE_ENV === 'test' ? Object : String,
type: isTest ? Object : String,
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Opened #1030.

Copy link
Contributor

@obulat obulat left a 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 :)

@dhruvkb dhruvkb changed the title Convert utils to TypeScript Convert 6 utils to TypeScript Mar 1, 2022
@dhruvkb dhruvkb merged commit 49b54b5 into main Mar 1, 2022
@dhruvkb dhruvkb deleted the tsify-utils branch March 1, 2022 19:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🤖 aspect: dx Concerns developers' experience with the codebase ✨ goal: improvement Improvement to an existing user-facing feature 🟨 priority: medium Not blocking but should be addressed soon
Projects
None yet
4 participants