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: retroactively eslint other cron files #1993

Open
wants to merge 112 commits into
base: main
Choose a base branch
from

Conversation

gobengo
Copy link
Contributor

@gobengo gobengo commented Jun 16, 2022

Motivation:

  • in feat: create cron nft-ttr to measure nft time to retrievability #1945 I added some eslint rules (e.g. to avoid bugs due to dangling promises and stuff). Nothing too custom, just typescript-eslint/recommended. But I added exceptions for files I didn't touch as part of the functional goals of that PR. During review, @alanshaw encouraged me to lint those other files too. I wanted to do it as a followup PR since the other one was already pretty big.

gobengo added 30 commits May 25, 2022 14:45
@gobengo gobengo changed the title retroactively eslint other cron files refactor: retroactively eslint other cron files Jun 16, 2022
@@ -47,7 +49,12 @@ export async function updateDagSizes({ pg, after }) {
const countRes = await pg.query(COUNT_CONTENT_WITHOUT_SIZE, [
after.toISOString(),
])
const total = countRes.rows[0].count
assert.ok(countRes.rows[0])
Copy link
Contributor Author

@gobengo gobengo Jun 16, 2022

Choose a reason for hiding this comment

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

now that node.js assert.ok is typed as a tsc type guard, this is how i chose to get type safety.

It's at risk of throwing more AssertionError errors, but at least that's a fail-fast well-before any more mysterious runtime errors we have to track down by trolling through logs.

@alanshaw lmk if you'd rather I just use a type assertion (which may be incorrect at runtime) rather than the asserts.

Copy link
Contributor

@alanshaw alanshaw Jun 17, 2022

Choose a reason for hiding this comment

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

We have not typically used assert outside of tests. It seems to add a lot of noise to the code base and (at least for me) is making it quite difficult to read the actual code. It looks like you might end up asserting on types multiple times in multiple places and I think it would be more DRY to just use a @typedef or @type once. There's also probably some performance overhead we're suffering by continually type checking everything.

It definitely makes sense to use assert to validate types for inputs (such as in a REST endpoint, or user input from a CLI) at runtime (and likely in some other situations) but I wouldn't use it to obtain type safety like this in your editor or for the typescript compiler.

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 will update to use less runtime assert and instead use a type assertion via js comment.

@gobengo gobengo marked this pull request as ready for review June 16, 2022 21:19
@gobengo
Copy link
Contributor Author

gobengo commented Jun 16, 2022

I have to admit I don't really understand why all the #1945 commits appear in here. I think it's because I didn't rebase, but merged. But the rebase seemed like it would take forever and be bug prone? If it's important to have a cleaner commit log on this, I could use some advice on how to do that. (IMO it isn't too important?)

@gobengo gobengo requested a review from alanshaw June 16, 2022 21:23
@@ -47,7 +49,12 @@ export async function updateDagSizes({ pg, after }) {
const countRes = await pg.query(COUNT_CONTENT_WITHOUT_SIZE, [
after.toISOString(),
])
const total = countRes.rows[0].count
assert.ok(countRes.rows[0])
Copy link
Contributor

@alanshaw alanshaw Jun 17, 2022

Choose a reason for hiding this comment

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

We have not typically used assert outside of tests. It seems to add a lot of noise to the code base and (at least for me) is making it quite difficult to read the actual code. It looks like you might end up asserting on types multiple times in multiple places and I think it would be more DRY to just use a @typedef or @type once. There's also probably some performance overhead we're suffering by continually type checking everything.

It definitely makes sense to use assert to validate types for inputs (such as in a REST endpoint, or user input from a CLI) at runtime (and likely in some other situations) but I wouldn't use it to obtain type safety like this in your editor or for the typescript compiler.

@gobengo
Copy link
Contributor Author

gobengo commented Jun 20, 2022

All the places where the new lint rules were unhappy due to unsafety around pg.query, I added a type assertion of import('pg').QueryResult<Something>.

@gobengo gobengo requested a review from alanshaw June 20, 2022 18:50
@gobengo gobengo requested a review from vasco-santos June 29, 2022 23:16
body: JSON.stringify({ hashToPin: cid, ...(options || {}) }),
})
)
const pin = /** @type {PinataPin} */ (json)
Copy link
Contributor

Choose a reason for hiding this comment

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

wow, do we need the extra json const here to please the types?

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.

8 participants