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

Add flow type annotations. #51

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[ignore]
.*/node_modules/documentation/*

[libs]

[include]

[options]
Copy link
Member

Choose a reason for hiding this comment

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

@vmx guessing that these .flowconfig files will be removed in your effort to add flow directly to aegir, correct?

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 don't believe that would be possible given that flow uses .flowconfig to detect the root what needs to be type-checked.

Copy link
Member

Choose a reason for hiding this comment

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

Huh, is there no way for us to specify where the config file is located? Feels very suboptimal if we have to add this to every repository...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Huh, is there no way for us to specify where the config file is located? Feels very suboptimal if we have to add this to every repository...

As far as I know it's still the case (at least there is no documented parameter to a flow command). Please note that it's not as much of an issue about specifying a config (.flowconfig could be a blank file) but rather about not being able to specify a root from which to type-check.

Copy link
Member

Choose a reason for hiding this comment

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

@victorbjelkholm I hope to have it go away. @garrensmith will have a look.

Choose a reason for hiding this comment

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

Reading here, it doesn't seem like we can remove the flowconfig file. It should be a very simple file for each project. I don't think it would be to much of an issue having it in each repo. We could maybe look at having AEgir generate a generic .flowconfig file for a repo.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"lodash.filter": "^4.6.0",
"lodash.map": "^4.6.0",
"varint": "^5.0.0",
"xtend": "^4.0.1"
"xtend": "^4.0.1",
"callback.flow": "^1.0.0"
},
"devDependencies": {
"aegir": "^12.3.0",
Expand Down
32 changes: 32 additions & 0 deletions src/codec.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @flow

import type { Protocol } from "./protocols-table.js"

export type Name$Address = [string, string]
export type Code$Buffer = [number, Buffer]
export type { Protocol }
export class ParseError extends Error {}

declare export default {
stringToStringTuples(string): Name$Address[],
stringTuplesToString(Name$Address[]): string,
tuplesToStringTuples(Code$Buffer[]): Name$Address[],
stringTuplesToTuples(Name$Address[]): Code$Buffer[],

bufferToTuples(Buffer): Code$Buffer[],
tuplesToBuffer(Code$Buffer[]): Buffer,

bufferToString(Buffer): string,
stringToBuffer(string): Buffer,

fromString(string): Buffer,
fromBuffer(Buffer): Buffer,
validateBuffer(mixed): void,
isValidBuffer(mixed): boolean,
cleanPath(string): string,

ParseError(string): ParseError,
protoFromTuple(Name$Address | Code$Buffer): Protocol,

sizeForAddr(Protocol, Buffer): number
}
13 changes: 13 additions & 0 deletions src/convert.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @flow

import type { Protocol, Code } from "./protocols-table"
export type { Protocol, Code }

declare export default {
// So this just flips the type it seems, I wish there were two different named
// functions instead.
(Protocol, Buffer): string,
(Protocol, string): Buffer,
toString(Protocol, Buffer): string,
toBuffer(Protocol, string): Buffer
}
49 changes: 49 additions & 0 deletions src/index.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// @flow

import type { Callback } from "callback.flow"
import type { Protocol, Code } from "./protocols-table"
import protocols from "./protocols-table"
export opaque type String: string = string
export type { Protocol, Code, Callback }

export type Options = {
family: string,
host: string,
transport: string,
port: string
}

export type NodeAddress = {
family: string,
address: string,
port: string
}

export interface Multiaddr {
buffer: Buffer;
toString(): String;
toOptions(): Options;
inspect(): string;
protos(): Protocol[];
protoCodes(): Code[];
protoNames(): string[];
tuples(): Array<[Code, Buffer]>;
stringTuples(): Array<[Code, string | number]>;
encapsulate(string | Buffer | Multiaddr): Multiaddr;
decapsulate(string | Buffer | Multiaddr): Multiaddr;
getPeerId(): ?string;
equals(Multiaddr): boolean;
nodeAddress(): NodeAddress;
isThinWaistAddress(Multiaddr): boolean;
fromStupidString(string): empty;
}

declare export default {
Buffer: typeof Buffer,
protocols: typeof protocols,
(string | Buffer | Multiaddr): Multiaddr,
fromNodeAddress(NodeAddress, transport: string): Multiaddr,
isMultiaddr(mixed): boolean,
isName(mixed): boolean,
resolve(mixed, Callback<Error, void>): void
}
21 changes: 21 additions & 0 deletions src/protocols-table.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @flow

export opaque type Code: number = number
export type Size = number

export type Protocol = {
code: Code,
size: Size,
name: string,
resolvable: boolean
}

declare export default {
(string | number): Protocol,
lengthPrefixedVarSize: number,
V: number,
table: Array<[number, number, string]>,
names: { [string]: Protocol },
codes: { [number]: Protocol },
object(Code, Size, string, boolean): Protocol
}