Skip to content

Conversation

kumar303
Copy link
Contributor

Fixes mozilla/addons#10241

I have entered into the valley of darkness [1] to seek out the Light of Truth. Who will walk with me?

I started by annotating one component (I swear). I tried to follow its dependencies and annotate each one but I didn't finish because we have so much shared logic. I think this paints an interesting picture of what Flow can buy us.

Here are some observations:

  • Declaring API response objects will be very helpful not only in catching errors but also in understanding how to work with the API. In my own experience, one already has to start by understanding the API data (either from the docs or with browser devtools) so using Flow will simply require us to document our findings.
  • Declaring state objects will be very helpful for all kinds of things (reducers, components) because we have lots of code that depends on specific state structures. It has been pretty hard to catch all mistakes using our test suite.
  • We can finally validate our use of component properties!
  • Static typing reveals the truth about what your code is trying to accomplish and I have already spotted a few code problems after adding annotations.
  • Flow adds verbosity and the syntax sometimes looks odd but I think the verbosity will make understanding how objects interact with each other much easier.
  • Flow-typing tests is going to be hard because it will require some architectural changes around dependency injection. It may take longer to use Flow in our tests.

Let me know what you think!

[1] during my 8-hour flight back from London to Chicago

@kumar303
Copy link
Contributor Author

@tofumatt thanks for the thorough review. I expanded the docs to help clarify everything and addressed each issue. Can you take another look?

@kumar303 kumar303 requested a review from tofumatt April 10, 2017 22:54
Copy link
Contributor

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

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

I'm curious about those single pipes in the type declarations and the filenames... mostly the rest of the stuff is r+wc types of things. This is looking great, I'm definitely getting more comfortable with the syntax and the comments/docs looks great.


Here is our Flow manifesto:

* We use Flow to **declare the intention of our code** and help others
Copy link
Contributor

Choose a reason for hiding this comment

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

These docs are really awesome now, thanks so much for doing another pass. Super easy to grok and the links are great 👍

README.md Outdated
'Inexact type is incompatible with exact type' but that's a
[bug](https://github.com/facebook/flow/issues/2405).
You can use the `Exact<T>` workaround from
[`src/core/types/coreTypes`](https://github.com/mozilla/addons-frontend/blob/master/src/core/types/coreTypes.js)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we call the file coreTypes when it's already in the types/ dir? I think I at least meant to ask but forgot or it got buried. Seems redundant though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I did change the name but I forgot to update this link! Fixed.

README.md Outdated
into a bug or if you hit something that's making you bang your head on
the keyboard. If it's something you think is unfixable then use
`$FLOW_IGNORE` instead. Please explain your rationale in the comment and link
to a github issue if possible.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick but GitHub is a proper noun.

"dev:disco": "better-npm-run dev:disco",
"eslint": "eslint .",
"flow:check": "flow check",
"flow:dev": "chokidar .flowconfig flow/ src/ tests/ -i flow/logs/flow.log -c 'flow status' --initial",
Copy link
Contributor

Choose a reason for hiding this comment

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

I forget, is this safe to run if you don't have chokidar installed globally? I'm rusty on my npm run command knowledge. Maybe it's okay?

Copy link
Contributor Author

@kumar303 kumar303 Apr 12, 2017

Choose a reason for hiding this comment

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

Yep, it's safe. "In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts."

return new Promise(
(resolve) => {
const data = { rating, version: versionId, body, title };
const data = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick but postData, ratingData, or reviewData might be a bit less generic. data is a vague variable name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I changed it. However, I feel like a short and concise variable name makes code easier to understand when it only spans 10 lines or so. Otherwise the verbosity can slow a reader down.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, naming it review instead achieves the same conciseness that data provided.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sweet, I didn't even consider that, oops 😲

}

type CallApiParams = {|
endpoint: string,
Copy link
Contributor

Choose a reason for hiding this comment

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

This would be nice to alphabetise I think.

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 did another sweep and I think all param lists should be alphabetized now

|};

export type AddonTypeProp =
typeof ADDON_TYPE_DICT |
Copy link
Contributor

Choose a reason for hiding this comment

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

These look like they should be || ORs not bitwise ORs, as I assume we want string here not 0. Odd that the type being 0 didn't mess stuff up somewhere...

review_url: string,
slug: string,
status: 'beta'
| 'lite'
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see single pipes being used here too. I know the pipes can surround the object syntax and it's something special in Flow. But are they also used as regular OR in Flow? What would a double pipe here do? Normally I'm used to catching bitwise OR operators as typos so they set off alarm bells in my head. 😖

@@ -0,0 +1,6 @@
/* @flow */
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the reason for wanting types in the folder and filename?

// It may be possible to use an official library for this.
// See: https://github.com/reactjs/react-redux/pull/389
// and: https://github.com/reactjs/redux/pull/1887/files#diff-46d86d39c8da613247f843ee8ca43ebc
export type DispatchFn = (action: Object) => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be changed to DispatchFunc like other function type names.

Copy link
Contributor

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

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

Just added my comment about bitwise pipes.

GitHub was having me comment on old code so my thoughts on filenames are no longer a thing.

A few things needing cleaning up but I'm mostly wondering about the single pipes in src/core/types/addons.js.

review_url: string,
slug: string,
status: 'beta'
| 'lite'
Copy link
Contributor

Choose a reason for hiding this comment

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

These look like they should be || ORs not bitwise ORs. Odd that this resulting in 0 didn't mess stuff up somewhere...

Are they also used as regular OR in Flow? What would a double pipe here do? Normally I'm used to catching bitwise OR operators as typos so they set off alarm bells in my head. 😖

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is a Union type so I think it's correct but, huh, maybe there is a bug here because it's not protecting me from referencing an invalid status value (which is what I intended). Well, at the very least it's good documentation and when the bug is fixed it will produce errors.

@kumar303 kumar303 requested a review from tofumatt April 12, 2017 19:06
@kumar303
Copy link
Contributor Author

@tofumatt thanks, this helped me dig into a few more issues. It should be ready for another look.

Copy link
Contributor

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

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

Thanks for dealing with my constant nitpicks and for doing all this work. Let's hope Flow helps us bash our heads on keyboards less! 👍 😆 🎉

@kumar303 kumar303 merged commit 3f41057 into mozilla:master Apr 17, 2017
@kumar303 kumar303 deleted the flow-iss2044 branch April 17, 2017 18:37
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.

Experiment with introducing Flow for static analysis

4 participants