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

fix(frontend): Use type-aware recommended ESLint rules #18662

Merged
merged 6 commits into from
Nov 17, 2023

Conversation

Twixes
Copy link
Member

@Twixes Twixes commented Nov 15, 2023

Problem

In #18469 I noticed one class of real issue in our codebase, where we mistakenly didn't await breakpoint(<delay>) in some Kea logics, which silently made the breakpoint a no-op – to the point of the logic not working as the developer intended at all.

I realized: wow, how come ESLint didn't just flag this? Turns out it's perfectly capable of it, we just never turned the rule on. It's called @typescript-eslint/no-floating-promises. So I added that.

But then I realized, why isn't such a simple check just part of the @typescript-eslint/recommended set of rules (which we had enabled)? Well, for some reason there's @typescript-eslint/recommended… and there's @typescript-eslint/recommended-type-checked. The latter is a far more useful superset of the former, and does include @typescript-eslint/no-floating-promises.

Changes

So I replaced @typescript-eslint/recommended with @typescript-eslint/recommended-type-checked, and fixed all the errors that showed up. Around 300 of them. Mostly cases of missing await/void, redundant async, or unexpected async. And autofix handled redundant type assertions (e.g. const x = 'foo'; func(x as string);).

How did you test this code?

pnpm lint:js

@Twixes Twixes requested a review from a team November 15, 2023 18:33
@Twixes Twixes force-pushed the no-floating-promises branch from 985e4e6 to 96b0a9f Compare November 15, 2023 18:47
@Twixes Twixes requested a review from mariusandra November 15, 2023 19:06
Copy link
Contributor

github-actions bot commented Nov 15, 2023

Size Change: -60 B (0%)

Total Size: 2.01 MB

Filename Size Change
frontend/dist/toolbar.js 2.01 MB -60 B (0%)

compressed-size-action

Copy link
Contributor

@benjackwhite benjackwhite left a comment

Choose a reason for hiding this comment

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

I didn't read through every change but saw some stuff to point out. Feels like a lot of changes that could have weird side effects so keen to test this properly

Comment on lines 58 to 60
const response = await api.get(
`api/projects/${teamLogic.values.currentTeamId}/activity_log/important_changes`
)) as ChangesResponse
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think get can take a type right? like api.get<ChangesResponse>(

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. This is fairly insignificant, because knowledge of this type isn't used in the function, and the return type is already specified because of the reducer's type – but better to retain the type info, so I made use of generics as you suggested

@@ -104,7 +104,7 @@ export const activityLogLogic = kea<activityLogLogicType>([
})),
listeners(({ actions }) => ({
setPage: async (_, breakpoint) => {
await breakpoint()
breakpoint()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this not the opposite of what we want? 🤔

Copy link
Member Author

@Twixes Twixes Nov 16, 2023

Choose a reason for hiding this comment

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

This is actually a bit tricky and eslint gets it right, which is that breakpoints without a delay are non-promises (there's nothing asynchronous going on then) – as Marius explains below

@@ -66,7 +66,7 @@ export const login2FALogic = kea<login2FALogicType>([
: null,
}),
submit: async ({ token }, breakpoint) => {
await breakpoint()
breakpoint()
Copy link
Contributor

Choose a reason for hiding this comment

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

These ones confuse me totally as I thought the whole thing that started off the linting exercise was realizing we weren't awaiting the breakpoints?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The await is only needed if you pass in an actual numeric value to wait. Otherwise the code executes synchronously and the await isn't needed. It should be just a no-op then and won't hurt, but it's effectively ignored.

Essentially:

function breakPoint(delay?: ms): void | Promise<void> {
  if (delay > 0) { 
    await sleep(delay)
  }
  if (actionWasCalledAgain()) {
    throw BreakpointException();
  }
}

Copy link
Collaborator

@mariusandra mariusandra left a comment

Choose a reason for hiding this comment

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

Looks green

@Twixes Twixes requested a review from benjackwhite November 16, 2023 17:13
@Twixes
Copy link
Member Author

Twixes commented Nov 17, 2023

From what I can tell, things are working correctly, so will consider your feedback addressed @benjackwhite

@Twixes Twixes merged commit f2da9dc into master Nov 17, 2023
74 checks passed
@Twixes Twixes deleted the no-floating-promises branch November 17, 2023 14:33
@jonasi
Copy link

jonasi commented Nov 17, 2023

Hi folks - this is breaking our persons view for a large majority of our users.

image

@daibhin
Copy link
Contributor

daibhin commented Nov 17, 2023

Yep, you're correct @jonasi. A fix has been shipped: #18716

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.

5 participants