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

First pass adding flow annotations. #187

Merged
merged 2 commits into from
Oct 9, 2015
Merged

First pass adding flow annotations. #187

merged 2 commits into from
Oct 9, 2015

Conversation

ludofischer
Copy link
Contributor

As @chenglou suggested in #180 , here’s a pull request with flow checks added to just a subset of the project files. I am only checking files where Flow recognizes all language constructs. Since Flow already merged the fixes related to const destructuring and export default, it seems pointless to change the project coding style, only to go back when the next Flow version releases.

Among the project dependencies, I ignored some files because we hit facebook/fbjs#44 with React 0.14.0-rc1; I also ignored a dummy package.json file used to test npmconf because flow mistakes it for the real deal.

@chenglou
Copy link
Owner

chenglou commented Oct 4, 2015

Awesome!

@@ -1,4 +1,6 @@
export default function hasReachedStyle(currentStyle, style) {
/* @flow */
export default function hasReachedStyle(currentStyle: Object,
Copy link
Owner

Choose a reason for hiding this comment

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

Is it possible theoretically to type check this as something more precise than Object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That’s possible. What additional constraints would you like to enforce?

Copy link
Owner

Choose a reason for hiding this comment

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

Humm on second thought it could be a bit complicated. The thing is, currentStyle looks like this: {height: {val: 10, config: [1, 2]}, width: justANumber, randomKey: someRandomValue}. The randomKey part screws things up. It's needed because for TransitionMotion you might need to carry around a pack of info around so that you can still access them when the item's gone everywhere else. Do you know what I mean?

If this is type-checkable it'd be great. Say it's type A. TransitionMotion's styles would be typed as a map of {stringKey: A}. Avoids some confusion if you use types everywhere.

So right now it's not easily typeable. 2 solutions I can think of:

  1. Impose that recognized keys, e.g. height, transform, opacity, etc. Have that Number | ConfigObject type. This isn't cross-platform though.
  2. Impose that anything you carry around be in a key called data, of type Any. Everything else should be Number | ConfigObject.

Any other solution? If not, either way we'd need to leave this as Object for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. I don’t understand why it isn’t cross-platform. Because you would need to hardcode all the possible keys? But then we would have to track all possible CSS properties. I do not think we should enforce the validity of the CSS property at the type level.
  2. I prefer this solution. But couldn’t randomKey become an extra parameter instead of living inside the same configuration object. We would have a config that includes the essentials that every animation needs, and could be precisley type-checked and a separate object for random stuff. Right now the situation is a bit contradictory as the code operates on the style objects with functions that are extremely generic and would almost work for any type of object, yet we also wish to impose constraints on those same objects. 'Isolate the parts that change from those that don’t' might also apply to types.

Copy link
Owner

Choose a reason for hiding this comment

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

  1. Well partially, yes. This also has to do with the fact that it's not clear that you'd want height rounded to integer on e.g. react-canvas (if they even care about react-motion).
  2. The extra data carried around is only for TransitionMotion. How would you do this:
<TransitionMotion styles={stuff}>{interpolated => Object.keys(interpolated).map(style => randomKeyWithMyData...)}

Passing as an extra param would work well here, would it?

cc @phaistonian

Copy link
Collaborator

Choose a reason for hiding this comment

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

For now it could be a simple type Options which equals to Object. Later Options can be updated with more specific type. And it will be in one place rather then updating (and possibly missing some) all places in the code that use the same Options

Copy link
Collaborator

Choose a reason for hiding this comment

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

@chenglou I've first read your commend in the other way and that was funny.

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’ve had mixed experiences with type aliases for simple types in Scala. I guess it would not change much whether we define a new Options type in this case. Do you know whether we can export type definitions from a module like any other variable?

Copy link
Owner

Choose a reason for hiding this comment

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

Yes you can. Also, I'm ambivalent as to aliasing it to something else.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I really think we need alias. It is not about Options being equal to Object, it is about idiomatic function API.

It also gives you ability to do compile-time checks if arguments are actually the same type. So if in one place you will use Options and in the other place Object - check will fail.

Often I have separate PropTypes file that exports something like:

export const id = React.PropTypes.number;
export const name = React.PropTypes.string;

@chenglou
Copy link
Owner

chenglou commented Oct 5, 2015

Humm, I guess it's all set?

const errorMargin = 0.0001;

export default function stepper(frameRate, x, v, destX, k, b) {
export default function stepper(frameRate: number,
x: number,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry for nitpicking but I can't recall us using this awkward indentation and it will not pass ESLint check.

Should be something like

export default function stepper(
  frameRate: number,
  x: number,
  v: number,
  destX: number,
  k: number,
  b: number): [number, number] {

Disregard if I'm wrong.

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 does pass ESLint checks (you might want to change the ESLint config), but I can change it anyway. Is it just the first argument on a new line?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is 2 space indentation, and first arg on a new line too.
Thanks.

@chenglou
Copy link
Owner

chenglou commented Oct 8, 2015

So I guess this is good to go? Beside the nits =D

@ludofischer
Copy link
Contributor Author

I have fixed the indentation issue that @nkbt highlighted and created a Style type. Now that Flow 0.17 is out we can try to check the rest of the code base. Do you want to merge this one right now or do you prefer if I try to annotate more files first?

@ludofischer
Copy link
Contributor Author

Interestingly, if you update to the latest ESLint and AirBnB style guides, the lint npm task reports 169 errors… that Flow does not care about.

@chenglou
Copy link
Owner

chenglou commented Oct 9, 2015

Do you want to merge this one right now or do you prefer if I try to annotate more files first?
We should merge this right now. I don't understand the lint errors though. Variable names too short? Since when?

@ludofischer
Copy link
Contributor Author

You get lint errors only if you change the eslint-config-airbnb to 0.1.0, so it’s not something to worry about now. I’ve opened an issue to keep track of this: #211

@chenglou
Copy link
Owner

chenglou commented Oct 9, 2015

Alright, merging! =D

Thanks a lot!

chenglou added a commit that referenced this pull request Oct 9, 2015
First pass adding flow annotations.
@chenglou chenglou merged commit 64156bc into chenglou:master Oct 9, 2015
import type {Style} from './Types.js';

export default function hasReachedStyle(currentStyle: Style,
style: Style): boolean {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was expecting this indentation to be fixed everywhere not only in the place I left my comment ;)

@nkbt
Copy link
Collaborator

nkbt commented Oct 10, 2015

Sorry for post-review, I somehow missed updates

chenglou added a commit that referenced this pull request Oct 10, 2015
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.

3 participants