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

Mark a bunch more object and array types read-only #5186

Merged
merged 10 commits into from
Jan 8, 2022

Conversation

gnprice
Copy link
Member

@gnprice gnprice commented Jan 8, 2022

Including all the object and array types in our actions and other Redux-related types, and a bunch of miscellaneous others.

The lack of read-only markings on the type for (Redux actions for) update_message events, EventUpdateMessageAction, produced some very confusing Flow errors in tests when I started changing those (for #5187), sending me down a path of at least a half-hour of debugging. (It was a real type error, it turned out -- a missing subject property, which for now our types believe is required there.)

And it turns out I had a draft branch from last summer with that and the other action types, and a few more changes. So, here's that branch finished up, along with some further changes in the same direction.

This will make it possible to still instantiate `Dispatch` with our
`Action` type while marking its `type` property as read-only.
Noticed one of these while writing the previous commit.  Went and
fixed them all, because `any` delenda est.

The `*` syntax is deprecated entirely, and mostly just means `any`:
  https://flow.org/en/docs/types/utilities/#toc-existential-type
  https://flow.org/en/docs/linting/rule-reference/#toc-deprecated-type

All of these `any` uses (including the `*`) look like they were
driven by the lack of appropriate variance annotations `+` and `-`:
  https://flow.org/en/docs/types/generics/#toc-variance-sigils
  https://flow.org/en/docs/types/interfaces/#toc-interface-property-variance-read-only-and-write-only

In each case, the appropriate real (non-`any`) type becomes available
only when the appropriate variance annotations are added elsewhere
(including the one added to `Dispatch`'s type parameter in the
previous commit.)

So add the needed variance annotations, and then give these the
appropriate not-`any` types.
This will let us mark all the array types in our various Redux actions
read-only, too.
This was capable of getting Flow confused, unsure of which of these
two branches to try.  (In particular that would happen after making
all these action types read-only.)
In particular this covers all the numerous branches of our
`Action` union.

It doesn't cover the various object types embedded inside these;
we'll get to those separately.

Done with:

  $ perl -i -0pe '
        s<type\ \w+\ =\ \K( \{\| $ .*? ^ \|\} );> (\$ReadOnly<$1>;)gsmx;
        s<type\ \w+\ =\ \K( \{   $ .*? ^   \} );> (\$ReadOnly<$1>;)gsmx
      ' src/{action,api/event}Types.js
Done with:

  $ perl -i -0pe '
          s<( \w+ ) \[\]> (\$ReadOnlyArray<$1>)gsmx;
          s/\bArray\b</\$ReadOnlyArray</g
        ' src/{action,api/event}Types.js
Previously we covered all the object types that are given names,
with `type Foo = …` declarations.  This covers the remainder,
found more deeply nested inside such types.

Found relevant types with:

  $ git grep -C2  '^ .*\{' src/{action,api/event}Types.js

and then just edited by hand.  We also take the opportunity to tighten
the formatting slightly in a couple of places.
Much like the preceding three commits, but on reduxTypes.js .
This became unnecessary when this line's fixme was lifted
in d96c6ca.
Done with:

  $ perl -i -0pe '
          s<( \w+ ) \[\]> (\$ReadOnlyArray<$1>)gsmx;
          s/\bArray\b</\$ReadOnlyArray</g
        ' src/**/*.js
  $ tools/fmt

followed by reviewing the changes, and reverting those that weren't
appropriate (which were perhaps 1/3 of the total) with commands like
`git restore -p src/foo/bar.js`.
@chrisbobbe chrisbobbe merged commit 1a281fa into zulip:main Jan 8, 2022
@chrisbobbe
Copy link
Contributor

Great, thanks a lot! LGTM, merged.

@gnprice gnprice deleted the pr-readonly-types branch January 10, 2022 18:52
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