Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend flow-bin to support new Node.js methods?

Is there a way to workaround the bugs with flow-bin when the tool reports errors where in fact it is a bug with flow itself?

For example, using Flow v0.62 and Node.js Buffer#swap64 method:

Error: lib/index.js:88                                                                                           
 88:         int64buf.swap64() // turn into Little-Endian
                      ^^^^^^ property `swap64`. Property not found in                     
 88:         int64buf.swap64() // turn into Little-Endian
             ^^^^^^^^ Buffer

According to the documentation for Node, I am pretty sure that the method exists. I don't really want to fork the Flow project just to fix something so small.

Is there a workaround while I wait for the pull request to be accepted by Facebook?

like image 900
Evgeny Avatar asked Jan 02 '18 14:01

Evgeny


1 Answers

Found a solution that allows me to override the Node.js included declarations Flow uses internally.

Flow allows to use a $projectRoot/flow-typed folder to include custom declarations of types, which is most often used to include declarations of common npm libraries.

The same folder can also be used to override some of the types used by Flow internally for its Node.js standard library.

I had to create a file called $projectRoot/flow-typed/node.js and into this file copy whole sections from https://github.com/facebook/flow/blob/master/lib/node.js. Copying partial sections does not work since Flow does not support extending declared types at this time (https://github.com/facebook/flow/issues/396.)

For example, take a Transform stream and add objectMode type arguments to its _transform method. Looks like this -

declare class stream$Transform extends stream$Duplex {
  _transform(
    chunk: Buffer | string | Object,
    encoding: string,
    callback: (error: ?Error, data?: Buffer | string | Object) => void
  ): void;
  _flush(
    callback: (error: ?Error) => void
  ): void;
}

where every Buffer | String was replaced to include Object as well.

The /flow-typed/node.js file is managed in version control of the project. But once a newer version of Flow supports the extra syntax in Node.js, this declaration can be removed. Meanwhile, it solves errors in Flow that are caused by Flow lagging behind Node.js and its general incorrectness.

like image 123
Evgeny Avatar answered Nov 16 '22 20:11

Evgeny