2017-06-09 20:52:06 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2018-01-11 11:12:05 +00:00
|
|
|
*
|
|
|
|
* @flow
|
|
|
|
* @format
|
2017-06-09 20:52:06 +00:00
|
|
|
*/
|
|
|
|
|
2018-01-11 12:27:23 +00:00
|
|
|
type _BabelSourceMap = {
|
2017-06-09 20:52:06 +00:00
|
|
|
file?: string,
|
|
|
|
mappings: string,
|
|
|
|
names: Array<string>,
|
|
|
|
sourceRoot?: string,
|
|
|
|
sources: Array<string>,
|
|
|
|
sourcesContent?: Array<?string>,
|
|
|
|
version: number,
|
|
|
|
};
|
|
|
|
|
|
|
|
// based on babylon v6.13.1
|
|
|
|
type BabylonOptions = {
|
|
|
|
allowImportExportEverywhere?: boolean,
|
|
|
|
allowReturnOutsideFunction?: boolean,
|
|
|
|
allowSuperOutsideMethod?: boolean,
|
|
|
|
sourceType?: 'script' | 'module',
|
|
|
|
sourceFilename?: 'string',
|
|
|
|
plugins?: Array<
|
2018-01-11 11:12:05 +00:00
|
|
|
| 'jsx'
|
|
|
|
| 'flow'
|
|
|
|
| 'doExpressions'
|
|
|
|
| 'objectRestSpread'
|
|
|
|
| 'decorators'
|
|
|
|
| 'classProperties'
|
|
|
|
| 'exportExtensions'
|
|
|
|
| 'asyncGenerators'
|
|
|
|
| 'functionBind'
|
|
|
|
| 'functionSent'
|
|
|
|
| 'dynamicImport',
|
2017-06-09 20:52:06 +00:00
|
|
|
>,
|
|
|
|
};
|
|
|
|
|
|
|
|
// based on babel-generator v6.18.0
|
|
|
|
type GeneratorOptions = {
|
|
|
|
auxiliaryCommentBefore?: string,
|
|
|
|
auxiliaryCommentAfter?: string,
|
|
|
|
shouldPrintComment?: (comment: string) => boolean,
|
|
|
|
retainLines?: boolean,
|
|
|
|
retainFunctionParens?: boolean,
|
|
|
|
comments?: boolean,
|
|
|
|
compact?: boolean | 'auto',
|
|
|
|
minified?: boolean,
|
|
|
|
concise?: boolean,
|
|
|
|
quotes?: 'single' | 'double',
|
|
|
|
filename?: string,
|
|
|
|
sourceMaps?: boolean,
|
|
|
|
sourceMapTarget?: string,
|
|
|
|
sourceRoot?: string,
|
|
|
|
sourceFileName?: string,
|
|
|
|
};
|
|
|
|
|
2018-01-11 11:12:05 +00:00
|
|
|
type InlinePlugin = string | {} | (() => {});
|
2017-06-09 20:52:06 +00:00
|
|
|
type _Plugins = Array<string | Object | [InlinePlugin] | [InlinePlugin, mixed]>;
|
|
|
|
|
|
|
|
// based on https://babeljs.io/docs/usage/options/ -- 2016-11-11
|
|
|
|
type __TransformOptions = {
|
|
|
|
filename?: string,
|
|
|
|
filenameRelative?: string,
|
2017-06-14 16:25:55 +00:00
|
|
|
presets?: _Plugins,
|
2017-06-09 20:52:06 +00:00
|
|
|
plugins?: _Plugins,
|
|
|
|
parserOpts?: BabylonOptions,
|
|
|
|
generatorOpts?: GeneratorOptions,
|
|
|
|
highlightCode?: boolean,
|
|
|
|
only?: string | RegExp | Array<string | RegExp>,
|
|
|
|
ignore?: string | RegExp | Array<string | RegExp>,
|
|
|
|
auxiliaryCommentBefore?: boolean,
|
|
|
|
auxiliaryCommentAfter?: boolean,
|
|
|
|
sourceMaps?: boolean,
|
|
|
|
inputSourceMap?: ?Object,
|
|
|
|
sourceMapTarget?: string,
|
|
|
|
sourceFileName?: string,
|
|
|
|
sourceRoot?: string,
|
|
|
|
moduleRoot?: string,
|
|
|
|
moduleIds?: boolean,
|
|
|
|
moduleId?: string,
|
|
|
|
getModuleId?: (moduleName: string) => string,
|
|
|
|
resolveModuleSource?: (source: string, filename: string) => string,
|
|
|
|
code?: boolean,
|
|
|
|
babelrc?: boolean,
|
|
|
|
ast?: boolean,
|
|
|
|
compact?: boolean | 'auto',
|
|
|
|
minified?: boolean,
|
|
|
|
comments?: boolean,
|
|
|
|
shouldPrintComment?: (comment: string) => boolean,
|
|
|
|
retainLines?: boolean,
|
|
|
|
extends?: string,
|
|
|
|
};
|
|
|
|
|
2018-01-11 11:12:05 +00:00
|
|
|
type _TransformOptions = __TransformOptions & {
|
|
|
|
env?: {[key: string]: __TransformOptions},
|
|
|
|
};
|
2017-12-20 22:18:21 +00:00
|
|
|
|
2018-02-05 22:43:59 +00:00
|
|
|
type BabelNode = $FlowFixMe;
|
2017-12-20 22:18:21 +00:00
|
|
|
|
2017-06-09 20:52:06 +00:00
|
|
|
type TransformResult = {
|
2018-02-01 11:03:30 +00:00
|
|
|
ast: BabelNode,
|
2017-06-09 20:52:06 +00:00
|
|
|
code: ?string,
|
|
|
|
ignored: boolean,
|
2018-01-11 12:27:23 +00:00
|
|
|
map: ?_BabelSourceMap,
|
2017-06-09 20:52:06 +00:00
|
|
|
};
|
2018-01-09 11:58:54 +00:00
|
|
|
// https://github.com/babel/babel/blob/master/packages/babel-generator/src/buffer.js#L42
|
|
|
|
type GeneratorResult = {
|
|
|
|
code: string,
|
2018-01-11 12:27:23 +00:00
|
|
|
map: ?_BabelSourceMap,
|
2018-01-12 12:09:45 +00:00
|
|
|
rawMappings: ?Array<_BabelSourceMapSegment>,
|
2018-01-09 11:58:54 +00:00
|
|
|
};
|
2017-06-09 20:52:06 +00:00
|
|
|
type VisitFn = <State>(path: Object, state: State) => any;
|
|
|
|
|
2018-01-31 16:11:48 +00:00
|
|
|
type BabelTypes = {[key: string]: Function};
|
|
|
|
|
2017-06-09 20:52:06 +00:00
|
|
|
declare module 'babel-core' {
|
|
|
|
declare type Plugins = _Plugins;
|
2018-01-11 12:27:23 +00:00
|
|
|
declare type BabelSourceMap = _BabelSourceMap;
|
2018-02-01 11:03:30 +00:00
|
|
|
declare type Ast = BabelNode;
|
2017-06-09 20:52:06 +00:00
|
|
|
declare type TransformOptions = _TransformOptions;
|
2018-01-31 16:11:50 +00:00
|
|
|
declare var transform: TransformSync /*(
|
2017-06-09 20:52:06 +00:00
|
|
|
code: string,
|
|
|
|
options?: _TransformOptions,
|
2018-01-31 16:11:50 +00:00
|
|
|
): TransformResult*/;
|
2017-06-09 20:52:06 +00:00
|
|
|
declare function traverse<State>(
|
2018-01-31 16:11:48 +00:00
|
|
|
ast: BabelNode,
|
|
|
|
visitor: BabelVisitor,
|
2017-06-09 20:52:06 +00:00
|
|
|
scope?: ?Object,
|
|
|
|
state?: State,
|
|
|
|
parentPath?: ?Object,
|
|
|
|
): void;
|
2018-01-31 16:11:48 +00:00
|
|
|
declare var types: BabelTypes;
|
2018-01-31 16:11:50 +00:00
|
|
|
declare var transformFile: TransformFile;
|
|
|
|
declare var transformFromAst: TransformFromAst /*(
|
2018-02-01 11:03:30 +00:00
|
|
|
ast: BabelNode,
|
2017-06-09 20:52:06 +00:00
|
|
|
code?: ?string,
|
|
|
|
babelOptions?: _TransformOptions,
|
2018-01-31 16:11:50 +00:00
|
|
|
): TransformResult*/;
|
2017-06-09 20:52:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 12:09:45 +00:00
|
|
|
type _BabelSourceMapSegment = {
|
2017-06-09 20:52:06 +00:00
|
|
|
generated: {column: number, line: number},
|
|
|
|
name?: string,
|
|
|
|
original?: {column: number, line: number},
|
|
|
|
source?: string,
|
|
|
|
};
|
|
|
|
|
2018-01-09 11:58:54 +00:00
|
|
|
// https://github.com/babel/babel/tree/master/packages/babel-generator
|
2017-06-09 20:52:06 +00:00
|
|
|
declare module 'babel-generator' {
|
2018-01-12 12:09:45 +00:00
|
|
|
declare export type BabelSourceMapSegment = _BabelSourceMapSegment;
|
2018-01-10 11:06:38 +00:00
|
|
|
declare export default (
|
2018-02-01 11:03:30 +00:00
|
|
|
ast: BabelNode,
|
2017-06-09 20:52:06 +00:00
|
|
|
options?: GeneratorOptions,
|
2018-01-09 11:58:54 +00:00
|
|
|
code?: string | {[string]: string},
|
2018-01-11 11:12:05 +00:00
|
|
|
) => GeneratorResult
|
2017-06-09 20:52:06 +00:00
|
|
|
}
|
2018-01-31 16:11:48 +00:00
|
|
|
|
|
|
|
// #############################################
|
|
|
|
// ############ 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>),
|
2018-01-31 16:11:50 +00:00
|
|
|
inputSourceMap?: ?_BabelSourceMap,
|
2018-01-31 16:11:48 +00:00
|
|
|
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,
|
|
|
|
|};
|
|
|
|
|
2018-03-07 12:18:13 +00:00
|
|
|
// https://github.com/babel/babel/tree/master/packages/babel-template
|
2018-01-31 16:11:48 +00:00
|
|
|
type TemplateOptions = Babylon7Options & {|
|
2018-03-07 12:18:13 +00:00
|
|
|
// note: template defaults for these options;
|
|
|
|
// allowReturnOutsideFunction = true
|
|
|
|
// allowSuperOutsideMethod = true
|
|
|
|
// sourceType = module
|
2018-01-31 16:11:48 +00:00
|
|
|
placeholderWhitelist?: Set<string>,
|
|
|
|
placeholderPattern?: RegExp | false,
|
|
|
|
preserveComments?: boolean,
|
|
|
|
|};
|
|
|
|
|
2018-03-07 12:18:13 +00:00
|
|
|
type Templatable = ({[tokenName: string]: string}) => BabelNode;
|
2018-01-31 16:11:48 +00:00
|
|
|
|
2018-03-07 12:18:13 +00:00
|
|
|
type TemplateFunc = (tpl: string, options?: TemplateOptions) => Templatable;
|
2018-01-31 16:11:48 +00:00
|
|
|
|
|
|
|
// 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,
|
2018-01-31 16:11:51 +00:00
|
|
|
map?: _BabelSourceMap,
|
2018-01-31 16:11:48 +00:00
|
|
|
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;
|
|
|
|
|
2018-01-31 16:11:51 +00:00
|
|
|
// Use the sync versions if you expect a result
|
|
|
|
type void_DO_NOT_USE_SYNC = void;
|
|
|
|
|
2018-01-31 16:11:48 +00:00
|
|
|
// 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,
|
2018-01-31 16:11:51 +00:00
|
|
|
): void_DO_NOT_USE_SYNC;
|
2018-01-31 16:11:48 +00:00
|
|
|
declare var transformSync: TransformSync;
|
|
|
|
// Note: DO NOT USE WITHOUT CALLBACK. Use transformFileSync instead.
|
|
|
|
declare function transformFile(
|
|
|
|
filename: string,
|
|
|
|
options: BabelCoreOptions,
|
|
|
|
callback: TransformCallback,
|
2018-01-31 16:11:51 +00:00
|
|
|
): void_DO_NOT_USE_SYNC;
|
2018-01-31 16:11:48 +00:00
|
|
|
declare var transformFileSync: TransformFileSync;
|
|
|
|
// Note: DO NOT USE WITHOUT CALLBACK. Use transformFromAstSync instead.
|
|
|
|
declare function transformFromAst(
|
|
|
|
ast: BabelNode,
|
|
|
|
code?: string,
|
|
|
|
options?: BabelCoreOptions,
|
|
|
|
callback: TransformCallback,
|
2018-01-31 16:11:51 +00:00
|
|
|
): void_DO_NOT_USE_SYNC;
|
2018-01-31 16:11:48 +00:00
|
|
|
declare var transformFromAstSync: TransformFromAstSync;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/babel/babel/tree/master/packages/babylon
|
|
|
|
declare module 'babylon7' {
|
2018-01-31 16:11:50 +00:00
|
|
|
declare function parse(
|
|
|
|
code: string,
|
|
|
|
options?: Babylon7Options,
|
|
|
|
): BabelNode & {program: {body: BabelNode}};
|
2018-01-31 16:11:48 +00:00
|
|
|
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;
|
|
|
|
}
|