From d986aac32bbddef4b649a352f1114ad9f697cf89 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Wed, 31 Jan 2018 08:11:48 -0800 Subject: [PATCH] Add flow types for Babel 7 (but not the library, these are only declares) Reviewed By: davidaurelio Differential Revision: D6834294 fbshipit-source-id: ffe66a6e7b683a4ed6361622049206869c635b71 --- flow-typed/babel.js.flow | 192 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 186 insertions(+), 6 deletions(-) diff --git a/flow-typed/babel.js.flow b/flow-typed/babel.js.flow index e33606b3..a39d9cd3 100644 --- a/flow-typed/babel.js.flow +++ b/flow-typed/babel.js.flow @@ -118,6 +118,8 @@ type GeneratorResult = { }; type VisitFn = (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( ast: Ast, - visitor: { - [key: string]: - | VisitFn - | {enter?: VisitFn, exit?: VisitFn}, - }, + 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, // "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 | RegExp | Array), + inputSourceMap?: ?BabelSourceMap, + minified?: boolean, + moduleId?: ?string, + moduleIds?: ?{[string]: string}, + moduleRoot?: string, + only?: ?(string | Array | RegExp | Array), + parserOpts?: ?Babylon7Options, + plugins?: Array, + presets?: Array, + 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, + 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, + 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; +}