Add flow types for Babel 7 (but not the library, these are only declares)

Reviewed By: davidaurelio

Differential Revision: D6834294

fbshipit-source-id: ffe66a6e7b683a4ed6361622049206869c635b71
This commit is contained in:
Peter van der Zee 2018-01-31 08:11:48 -08:00 committed by Facebook Github Bot
parent 9ac44657bc
commit d986aac32b
1 changed files with 186 additions and 6 deletions

View File

@ -118,6 +118,8 @@ type GeneratorResult = {
};
type VisitFn = <State>(path: Object, state: State) => any;
type BabelTypes = {[key: string]: Function};
declare module 'babel-core' {
declare type Plugins = _Plugins;
declare type BabelSourceMap = _BabelSourceMap;
@ -129,16 +131,13 @@ declare module 'babel-core' {
): TransformResult;
declare function traverse<State>(
ast: Ast,
visitor: {
[key: string]:
| VisitFn<State>
| {enter?: VisitFn<State>, exit?: VisitFn<State>},
},
ast: BabelNode,
visitor: BabelVisitor,
scope?: ?Object,
state?: State,
parentPath?: ?Object,
): void;
declare var types: {[key: string]: Function};
declare var types: BabelTypes;
declare function transformFromAst(
ast: Ast,
code?: ?string,
@ -162,3 +161,184 @@ declare module 'babel-generator' {
code?: string | {[string]: string},
) => GeneratorResult
}
// #############################################
// ############ Babel 7 #################
// #############################################
// (Mostly compatible with Babel 6...)
// https://github.com/babel/babel/tree/master/packages/babylon#options
type Babylon7Options = {|
allowImportExportEverywhere?: boolean,
allowReturnOutsideFunction?: boolean,
allowSuperOutsideMethod?: mixed, // "TODO" okay. Maybe don't use this yet.
sourceType?: 'script' | 'module' | 'unambiguous',
sourceFilename?: string,
startLine?: number,
plugins?: Array<string>, // "plugins" here are built-in features to enable
strictMode?: boolean, // "TODO" ehh to consider this strict mode?
ranges?: boolean,
tokens?: boolean,
|};
// https://github.com/babel/babel/tree/master/packages/babel-core#options
type BabelCoreOptions = {|
ast?: boolean,
auxiliaryCommentAfter?: ?string,
auxiliaryCommentBefore?: ?string,
babelrc?: boolean,
envName?: string,
code?: boolean,
comments?: boolean,
compact?: 'auto' | boolean,
env?: {production: {[string]: mixed}, staging: {[string]: mixed}},
extends?: ?string,
filename?: string,
filenameRelative?: 'string',
generatorOpts?: ?GeneratorOptions,
getModuleId?: ?(string) => string | boolean,
highlightCode?: boolean,
ignore?: ?(string | Array<string> | RegExp | Array<string | RegExp>),
inputSourceMap?: ?BabelSourceMap,
minified?: boolean,
moduleId?: ?string,
moduleIds?: ?{[string]: string},
moduleRoot?: string,
only?: ?(string | Array<string> | RegExp | Array<string | RegExp>),
parserOpts?: ?Babylon7Options,
plugins?: Array<BabelPlugin>,
presets?: Array<string>,
retainLines?: boolean,
shouldPrintComment?: ?(string) => boolean,
sourceFileName?: string,
sourceMaps?: boolean | 'inline' | 'both',
sourceMapTarget?: string,
sourceRoot?: string,
sourceType?: 'module' | 'script' | 'unambiguous',
wrapPluginVisitorMethod?: ?(
pluginAlias: string,
visitorType: string,
callback: () => void,
) => void,
|};
type TemplateOptions = Babylon7Options & {|
placeholderWhitelist?: Set<string>,
placeholderPattern?: RegExp | false,
preserveComments?: boolean,
|};
type Templatable = ({|[tokenName: string]: string|}) => BabelNode;
type TemplateFunc = (tpl: string, options: TemplateOptions) => Templatable;
// note: ast is not optional, babel will throw if it would be empty:
// https://github.com/babel/babel/blob/98969b8a7335e831c069164f5c56132111847224/packages/babel-core/src/transform-ast.js#L52
// https://github.com/babel/babel/blob/98969b8a7335e831c069164f5c56132111847224/packages/babel-core/src/transform-ast-sync.js#L15
export type Transform7Result = {|
code: string,
map?: BabelSourceMap,
ast: BabelNode, // note: babel never allows falsy ast value here
|};
// Do NOT make this an optional type!
type TransformCallback = (Error => void) | ((void, Transform7Result) => void);
type TraverseFunc = (
ast: BabelNode | Array<BabelNode>,
visitor: BabelVisitor,
scope?: ?Object,
state?: ?Object,
parentPath?: ?Object,
) => void;
export type TransformSync = (
code: string,
options: BabelCoreOptions,
) => Transform7Result;
export type TransformFileSync = (
filename: string,
options: BabelCoreOptions,
) => Transform7Result;
export type TransformFromAstSync = (
ast: BabelNode,
code?: string,
options?: BabelCoreOptions,
) => Transform7Result;
// https://github.com/babel/babel/tree/master/packages/babel-core
declare module '@babel/core' {
declare type Ast = BabelNode;
declare type BabelSourceMap = _BabelSourceMap;
declare type TransformResult = Transform7Result;
// use @babel/types instead!
declare var types: BabelTypes;
// use @babel/traverse instead!
declare var traverse: TraverseFunc;
// Note: DO NOT USE WITHOUT CALLBACK. Use transformSync instead.
declare function transform(
code: string,
options: BabelCoreOptions,
callback: TransformCallback,
): void;
declare var transformSync: TransformSync;
// Note: DO NOT USE WITHOUT CALLBACK. Use transformFileSync instead.
declare function transformFile(
filename: string,
options: BabelCoreOptions,
callback: TransformCallback,
): void;
declare var transformFileSync: TransformFileSync;
// Note: DO NOT USE WITHOUT CALLBACK. Use transformFromAstSync instead.
declare function transformFromAst(
ast: BabelNode,
code?: string,
options?: BabelCoreOptions,
callback: TransformCallback,
): void;
declare var transformFromAstSync: TransformFromAstSync;
}
// https://github.com/babel/babel/tree/master/packages/babylon
declare module 'babylon7' {
declare function parse(code: string, options?: Babylon7Options): BabelNode;
declare function parseExpression(
code: string,
options?: Babylon7Options,
): BabelNode;
}
// https://github.com/babel/babel/tree/master/packages/babel-generator
declare module '@babel/generator' {
declare export default (
ast: BabelNode,
options?: GeneratorOptions,
code?: string | {|[filename: string]: string|},
) => GeneratorResult
}
// https://github.com/babel/babel/tree/master/packages/babel-template
declare module '@babel/template' {
declare module.exports: TemplateFunc;
// & {
// ast: (code: string) => Ast,
// smart: TemplateFunc,
// statement: TemplateFunc,
// statements: TemplateFunc,
// expression: TemplateFunc,
// program: TemplateFunc,
// };
}
declare module '@babel/types' {
// TODO we should make this types thing explicit (see pckg babel-flow-types)
declare module.exports: BabelTypes;
}
declare module '@babel/traverse' {
declare module.exports: TraverseFunc;
}