Skip to content

Commit

Permalink
add flow
Browse files Browse the repository at this point in the history
Summary: This adds flow types to new code written for the Buck/Packager integration.

Reviewed By: cpojer

Differential Revision: D4175156

fbshipit-source-id: 38c3d2c9176c7b3cf22b8baed7d445a75d033d04
  • Loading branch information
davidaurelio authored and Facebook Github Bot committed Nov 17, 2016
1 parent 242bc69 commit c8b463f
Show file tree
Hide file tree
Showing 11 changed files with 530 additions and 331 deletions.
6 changes: 3 additions & 3 deletions flow/babel.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type GeneratorOptions = {
sourceFileName?: string,
};

type InlinePlugin = [() => {}, {}];
type InlinePlugin = [string | {} | () => {}, any];

// based on https://babeljs.io/docs/usage/options/ -- 2016-11-11
type _TransformOptions = {
Expand Down Expand Up @@ -89,7 +89,7 @@ type TransformOptions =
_TransformOptions & {env?: {[key: string]: TransformOptions}};
declare class _Ast {};
type TransformResult = {
ast: ?_Ast,
ast: _Ast,
code: ?string,
map: ?_SourceMap,
};
Expand Down Expand Up @@ -122,5 +122,5 @@ declare module 'babel-generator' {
declare function exports(
ast: _Ast,
options?: GeneratorOptions,
): {ast: Object, code: string, map: Object};
): TransformResult;
}
5 changes: 3 additions & 2 deletions packager/react-packager/src/ModuleGraph/Graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
*/
'use strict';

const invariant = require('fbjs/lib/invariant');
const memoize = require('async/memoize');
const queue = require('async/queue');
const seq = require('async/seq');
const invariant = require('fbjs/lib/invariant');

import type {GraphFn, LoadFn, ResolveFn, File, Module} from './types.flow';

Expand All @@ -28,7 +28,8 @@ exports.create = function create(resolve: ResolveFn, load: LoadFn): GraphFn {

if (typeof platform !== 'string') {
log.error('`Graph`, called without a platform');
return callback(Error('The target platform has to be passed'));
callback(Error('The target platform has to be passed'));
return;
}

const modules: Map<string | null, Module> = new Map();
Expand Down
72 changes: 45 additions & 27 deletions packager/react-packager/src/ModuleGraph/types.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,80 +12,98 @@

import type {Console} from 'console';

type callback<T> = (error: ?Error, result?: T) => any;
type callback2<T, T1> = (error: ?Error, a?: T, b?: T1) => any;
export type Callback<A = void, B = void>
= ((error: Error) => mixed)
& ((error: null | void, a: A, b: B) => mixed);

type ResolveOptions = {
log?: Console,
};

type LoadOptions = {
type LoadOptions = {|
log?: Console,
optimize?: boolean,
platform?: string,
};
|};

type GraphOptions = {
type GraphOptions = {|
cwd?: string,
log?: Console,
optimize?: boolean,
skip?: Set<string>,
};
|};

type Dependency = {
type Dependency = {|
id: string,
path: string,
};
|};

export type File = {
path: string,
code?: string,
export type File = {|
ast: Object,
};
code?: string,
path: string,
|};

export type Module = {
file: File,
export type Module = {|
dependencies: Array<Dependency>,
};
file: File,
|};

export type GraphFn = (
entryPoints: Iterable<string>,
platform: string,
options?: GraphOptions,
callback?: callback<Array<Module>>,
callback?: Callback<Array<Module>>,
) => void;

export type ResolveFn = (
id: string,
source: string,
platform: string,
options?: ResolveOptions,
callback: callback<string>,
callback: Callback<string>,
) => void;

export type LoadFn = (
file: string,
options: LoadOptions,
callback: callback2<File, Array<string>>,
callback: Callback<File, Array<string>>,
) => void;

type TransformResult = {
export type TransformResult = {|
code: string,
dependencies: Array<string>,
dependencyMapName?: string,
map: ?Object,
dependencies: Array<String>,
};
|};

export type TransformedFile = {
file: string,
code: string,
transformed: {[variant: string]: TransformResult},
file: string,
hasteID: ?string,
isPolyfill: boolean,
package?: PackageData,
transformed: {[variant: string]: TransformResult},
};

export type PackageData = {
name?: string,
main?: string,
export type PackageData = {|
browser?: Object | string,
main?: string,
name?: string,
'react-native'?: Object | string,
};
|};

export type TransformFnResult = {|
ast: Object,
map?: Object,
|};

export type TransformFn = (
data: {|
filename: string,
options?: Object,
plugins?: Array<string | Object | [string | Object, any]>,
sourceCode: string,
|},
callback: Callback<TransformFnResult>
) => void;
Loading

0 comments on commit c8b463f

Please sign in to comment.