Skip to content

Instantly share code, notes, and snippets.

@xjamundx
Last active October 16, 2017 12:30
Show Gist options
  • Save xjamundx/ffafa67925db6d7ffc4c86a7fb2bcb45 to your computer and use it in GitHub Desktop.
Save xjamundx/ffafa67925db6d7ffc4c86a7fb2bcb45 to your computer and use it in GitHub Desktop.

Trying Flow on my node.js code…Let’s see how this goes

Finding the getting started guide:

  1. google flow
  2. click “getflow.com”
  3. was something else
  4. google flow javascript
  5. found flowtype.org
  6. Click “get started” and landed here https://flowtype.org/docs/getting-started.html#_

Following the guide:

First thoughts, okay this is for a new project, but I’m not making a new project, so adapting to my life. In this case we’ll try with my node.js project.

  1. touch .flowconfig
  2. npm install --save-dev flow-bin
  3. Add // @flow to my index.js file // seems gross to me, but doing it anyway
  4. Typing npm run-script flow thought maybe it would work, but it doesn’t. get “missing script: flow”
  5. Typing ./node_modules/.bin/flow instead (not from docs)
  6. lots of weird startup stuff…. “flow is still initializing”
  7. “Found 43 errors”
node_modules/findup/test/fixture/f/e/d/c/b/a/top.json:1
  1: 
     ^ Unexpected end of input

node_modules/findup/test/fixture/f/e/d/c/config.json:1
  1: 
     ^ Unexpected end of input
.... // goes on an on

Errors some pretty unrelated to anything I'm doing. Giving up.

Will Try Some More Things....

  1. google the error messages
  2. Found facebook/flow#1420 (comment) trying it
  3. "Found 49 errors" > 6 more errors than last time!!?!?
  4. Okay there's a not about the main field with that regex, trying another regex .*/node_modules/.*/\(lib\|test\).*\.json$
  5. "Found 36 errors" > hey progress
  6. Let's look at the last two
node_modules/react/node_modules/fbjs/lib/Deferred.js.flow:60
 60:     Promise.prototype.done.apply(this._promise, arguments);
                           ^^^^ property `done`. Property not found in
497: declare class Promise<+R> {
     ^ Promise. See lib: /private/tmp/flow/flowlib_22c9017e/core.js:497

node_modules/react/node_modules/fbjs/lib/shallowEqual.js.flow:29
 29:     return x !== 0 || 1 / (x: $FlowIssue) === 1 / (y: $FlowIssue);
  1. Seems like facebook is breaking flow. Pretty weird.
  2. Googling it.
  3. Found facebookarchive/draft-js#305
  4. @zpao says we need to update fbjs. I don't even use that dep directly, so I don't agree.
  5. Getting annoyed again, but running npm ls fbjs
p2pnodeweb@1.29.0 /Users/jamuferguson/dev/paypal/web
├─┬ flux@2.1.1
│ └─┬ fbemitter@2.0.2
│   └── fbjs@0.7.2 
└─┬ react@15.3.1
  └── fbjs@0.8.4 
  1. grep 'version":' ./node_modules/fbjs/package.json
  2. "0.1.0-alpha.7"
  3. Hmm, seems old npm i fbjs
  4. grep 'version":' ./node_modules/fbjs/package.json
  5. "version": "0.8.4"
  6. seems better, let's
  7. ./node_modules/.bin/flow
  8. "Found 38 Errors" > Okay ,that's 2 more than before" and more JSON errors in there

giving up again, for now

@xjamundx
Copy link
Author

xjamundx commented Aug 30, 2016

Now seeing a ton of:

118:            return next()
                ^^^^^^^^^^^^^ async return. Missing annotation

I assume the problem here is that it's doesn't like that we use this shortcut to return and call next() on the same line, because technically it will return the results of the next() function (which is usualy nothing).

Maybe adding : void to the function declaration will help?

Or maybe I'll have to do:

next()
return

@xjamundx
Copy link
Author

Nothing seems to work.

Here's the results of trying return void next()

118:            return void next()
                ^^^^^^^^^^^^^^^^^^ async return. Missing annotation

Here's the results of putting them on separate lines:

119:            return
                ^^^^^^ async return. Missing annotation

@xjamundx
Copy link
Author

And async function always returns a Promise, so maybe it's getting confused? Or maybe there's just some special way that it needs to be annotated.

@xjamundx
Copy link
Author

xjamundx commented Aug 30, 2016

Okay got it. While I think it should be built-in, adding this to the end of the functional declaration seems to work

: Promise<void>

So our async middleware looks like this:

async function middleware(req: ExpressReq, res: any, next: function): Promise<void> {

@xjamundx
Copy link
Author

xjamundx commented Aug 30, 2016

Now running into this issue:

 const [contacts, fancyList: CustomType[] = [], randomThing = ''] = results
                                         ^^^^^^^^^^^^^^^^^^^ assignment of identifier `randomThing`. Could not resolve name

But I think it will be sorted out by:
facebook/flow#2239

@xjamundx
Copy link
Author

Was still getting these errors

node_modules/fbjs/lib/Deferred.js.flow:60
 60:     Promise.prototype.done.apply(this._promise, arguments);
                           ^^^^ property `done`. Property not found in
[libs]
497: declare class Promise<+R> {
     ^ Promise. See lib: /private/tmp/flow/flowlib_25b721b0/core.js:497

node_modules/react/node_modules/fbjs/lib/Deferred.js.flow:60
 60:     Promise.prototype.done.apply(this._promise, arguments);
                           ^^^^ property `done`. Property not found in
497: declare class Promise<+R> {
     ^ Promise. See lib: /private/tmp/flow/flowlib_25b721b0/core.js:497

So I added the following to the [ignore]:

.*/node_modules/.*fbjs/*

@xjamundx
Copy link
Author

Now trying to get babel to work. Seems kind of busted. Lots of these errors.

express-session deprecated undefined saveUninitialized option; provide saveUninitialized option node_modules/kraken-js/node_modules/meddleware/index.js:105:20
express-session deprecated req.secret; provide secret option node_modules/kraken-js/node_modules/meddleware/index.js:105:20
Tue, 30 Aug 2016 04:34:14 GMT unhandledRejection
SyntaxError: /Users/jamuferguson/dev/paypal/code/controllers/app/controller.js: Unexpected token (110:76)
  109 |  */
> 110 | export async function middleware(req: ExpressReq, res: any, next: function): Promise<void> {
      |                                                                             ^
  111 | 
    at Parser.pp.raise (/Users/jamuferguson/dev/paypal/code/node_modules/babylon/lib/parser/location.js:22:13)
    at Parser.pp.unexpected (/Users/jamuferguson/dev/paypal/code/node_modules/babylon/lib/parser/util.js:91:8)
    at Parser.pp.flowParsePrimaryType (/Users/jamuferguson/dev/paypal/code/node_modules/babylon/lib/plugins/flow.js:1101:8)

@xjamundx
Copy link
Author

xjamundx commented Aug 30, 2016

I guess we need to do this instead?

Replace next: function with next: Function

Weird that flow didn't mind the issue. Not actually seeing how to annotate functions very clearly. Looking here:
https://flowtype.org/docs/functions.html

@eliranmal
Copy link

this is totally fascinating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment