Skip to content

Commit

Permalink
Minor cleanup of Babel types
Browse files Browse the repository at this point in the history
Summary:
The Babel types are a mess. This diff cleans up the definitions (a little bit, there is tons more work to be done to make them actually good), removes some FlowFixMe's, and fixes the definition for the template function.

Note: I am doing this to unblock a diff and I couldn't keep working until I cleaned up the file a little bit. I know it is not perfect but I'm not looking to spend a lot of time on it right now.

Reviewed By: rubennorte

Differential Revision: D21089593

fbshipit-source-id: e1498ad7a45579f76e3ef40026eff3ff6d48e234
  • Loading branch information
cpojer authored and facebook-github-bot committed Apr 17, 2020
1 parent 95ee7bb commit ccba71d
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 164 deletions.
155 changes: 30 additions & 125 deletions flow-typed/babel.js.flow
Expand Up @@ -18,33 +18,6 @@ type _BabelSourceMap = {|
+version: number,
|};

// based on babylon v6.13.1
type BabylonOptions = {
allowImportExportEverywhere?: boolean,
allowReturnOutsideFunction?: boolean,
allowSuperOutsideMethod?: boolean,
sourceType?: 'script' | 'module',
sourceFilename?: 'string',
plugins?: Array<
| 'asyncGenerators'
| 'classProperties'
| 'decorators'
| 'doExpressions'
| 'dynamicImport'
| 'exportExtensions'
| 'flow'
| 'functionBind'
| 'functionSent'
| 'jsx'
| 'nullishCoalescingOperator'
| 'objectRestSpread'
| 'optionalChaining'
| 'optionalCatchBinding',
>,
...
};

// based on babel-generator v6.18.0
type GeneratorOptions = {
auxiliaryCommentBefore?: string,
auxiliaryCommentAfter?: string,
Expand All @@ -67,46 +40,6 @@ type GeneratorOptions = {
type InlinePlugin = string | {...} | (() => {...});
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,
presets?: _Plugins,
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,
...
};

type _TransformOptions = __TransformOptions & {
env?: {[key: string]: __TransformOptions, ...},
...
};

type BabelNode = $FlowFixMe;
type BabelPath = $FlowFixMe;

Expand All @@ -117,7 +50,6 @@ type TransformResult = {
map: ?_BabelSourceMap,
...
};
// https://github.com/babel/babel/blob/master/packages/babel-generator/src/buffer.js#L42
export type GeneratorResult = {
code: string,
map: ?_BabelSourceMap,
Expand All @@ -126,7 +58,6 @@ export type GeneratorResult = {
};
type VisitFn = <State>(path: Object, state: State) => any;

type BabelTypes = {[key: string]: Function, ...};
type BabelVisitor = {...};

type _BabelSourceMapSegment = {
Expand All @@ -137,13 +68,7 @@ type _BabelSourceMapSegment = {
...
};

// #############################################
// ############ Babel 7 #################
// #############################################
// (Mostly compatible with Babel 6...)

// https://github.com/babel/babel/tree/master/packages/babylon#options
type Babylon7Options = {|
type BabelParserOptions = {|
allowImportExportEverywhere?: boolean,
allowReturnOutsideFunction?: boolean,
allowSuperOutsideMethod?: mixed, // "TODO" okay. Maybe don't use this yet.
Expand All @@ -156,7 +81,6 @@ type Babylon7Options = {|
tokens?: boolean,
|};

// https://github.com/babel/babel/tree/master/packages/babel-core#options
type BabelCoreOptions = {|
ast?: boolean,
auxiliaryCommentAfter?: ?string,
Expand Down Expand Up @@ -188,7 +112,7 @@ type BabelCoreOptions = {|
moduleIds?: ?{[string]: string, ...},
moduleRoot?: string,
only?: ?(string | Array<string> | RegExp | Array<string | RegExp>),
parserOpts?: ?Babylon7Options,
parserOpts?: ?BabelParserOptions,
plugins?: ?_Plugins,
presets?: Array<string>,
retainLines?: boolean,
Expand All @@ -206,8 +130,13 @@ type BabelCoreOptions = {|
) => void,
|};

// https://github.com/babel/babel/tree/master/packages/babel-template
type TemplateOptions = Babylon7Options & {|
type BabelSourceLocation = {|
+start: {|+line: number, +column: number|},
+end: {|+line: number, +column: number|},
+identifierName?: string,
|};

type TemplateOptions = BabelParserOptions & {|
// note: template defaults for these options;
// allowReturnOutsideFunction = true
// allowSuperOutsideMethod = true
Expand All @@ -217,36 +146,14 @@ type TemplateOptions = Babylon7Options & {|
preserveComments?: boolean,
|};

type Templatable = ({[tokenName: string]: mixed, ...}) => BabelNode;

type TemplateFunc = (tpl: string, options?: TemplateOptions) => Templatable;

type BabelSourceLocation = {|
+start: {|+line: number, +column: number|},
+end: {|+line: number, +column: number|},
+identifierName?: string,
|};

// 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
ast: BabelNode,
|};

// 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,
Expand All @@ -265,22 +172,15 @@ export type TransformFromAstSync = (

type ParseCallback = (Error => void) | ((void, BabelNode) => void);

// Use the sync versions if you expect a result
type void_DO_NOT_USE_SYNC = void;

// https://github.com/babel/babel/tree/master/packages/babel-core
declare module '@babel/core' {
declare type Ast = BabelNode;
declare type BabelSourceMap = _BabelSourceMap;
declare type Plugins = _Plugins;
declare type TransformResult = Transform7Result;
declare type BabelCoreOptions = BabelCoreOptions;

// use @babel/types instead!
declare var types: BabelTypes;
// use @babel/traverse instead!
declare var traverse: TraverseFunc;

declare var version: string;

// Note: DO NOT USE WITHOUT CALLBACK. Use transformSync instead.
Expand Down Expand Up @@ -320,7 +220,6 @@ declare module '@babel/core' {
): BabelNode;
}

// https://github.com/babel/babel/tree/master/packages/babel-generator
declare module '@babel/generator' {
declare export type BabelSourceMapSegment = _BabelSourceMapSegment;
declare export default (
Expand All @@ -335,26 +234,32 @@ declare module '@babel/generator' {
) => 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 type TemplateArgs = {[tokenName: string]: mixed, ...};
declare type Templatable = (args?: TemplateArgs) => BabelNode;
declare export default (
template: string,
options?: TemplateOptions,
) => Templatable;
declare export function expression(
template: string,
options?: TemplateOptions,
): Templatable;
}

declare module '@babel/types' {
// TODO we should make this types thing explicit (see pckg babel-flow-types)
declare module.exports: BabelTypes;
declare export type Types = {[key: string]: Function, ...};
declare module.exports: Types;
}

declare module '@babel/traverse' {
declare type Path = BabelPath;
declare export type Path = BabelPath;

declare module.exports: TraverseFunc;
declare export default (
ast: BabelNode | Array<BabelNode>,
visitor: BabelVisitor,
scope?: ?Object,
state?: ?Object,
parentPath?: ?Object,
) => void;
}
Expand Up @@ -10,7 +10,7 @@

'use strict';

import typeof {types as BabelTypes} from '@babel/core';
import type {Types} from '@babel/types';

export type Visitors = {|
visitor: {|
Expand All @@ -24,7 +24,7 @@ export type Visitors = {|
|},
|};

function constantFoldingPlugin(context: {types: BabelTypes, ...}): Visitors {
function constantFoldingPlugin(context: {types: Types, ...}): Visitors {
const t = context.types;

const evaluate = function(path: Object) {
Expand Down
6 changes: 2 additions & 4 deletions packages/metro-transform-plugins/src/import-export-plugin.js
Expand Up @@ -10,10 +10,8 @@

'use strict';

/* $FlowFixMe(>=0.99.0 site=react_native_fb) This comment suppresses an error
* found when Flow v0.99 was deployed. To see the error, delete this comment
* and run Flow. */
const template = require('@babel/template').default;
const {expression} = require('@babel/template');

import type {Ast} from '@babel/core';
import type {Path} from '@babel/traverse';
Expand Down Expand Up @@ -107,7 +105,7 @@ const esModuleExportTemplate = template(`
/**
* Resolution template in case it is requested.
*/
const resolveTemplate = template.expression(`
const resolveTemplate = expression(`
require.resolve(NODE)
`);

Expand Down
4 changes: 2 additions & 2 deletions packages/metro-transform-plugins/src/index.js
Expand Up @@ -16,11 +16,11 @@ import type {
Visitors as InlinePluginVisitors,
Options as InlinePluginOptions,
} from './inline-plugin';
import typeof {types as BabelTypes} from '@babel/core';
import type {Ast} from '@babel/core';
import type {Types} from '@babel/types';

type BabelPlugin<VisitorT, OptionsT> = (
context: {types: BabelTypes, ...},
context: {types: Types, ...},
options: OptionsT,
) => VisitorT;

Expand Down
4 changes: 2 additions & 2 deletions packages/metro-transform-plugins/src/inline-plugin.js
Expand Up @@ -12,9 +12,9 @@

const createInlinePlatformChecks = require('./utils/createInlinePlatformChecks');

import typeof {types as BabelTypes} from '@babel/core';
import type {Ast} from '@babel/core';
import type {Path} from '@babel/traverse';
import type {Types} from '@babel/types';

export type Options = {
dev: boolean,
Expand Down Expand Up @@ -42,7 +42,7 @@ const processId = {name: 'process'};
const dev = {name: '__DEV__'};

function inlinePlugin(
{types: t}: {types: BabelTypes, ...},
{types: t}: {types: Types, ...},
options: Options,
): Visitors {
const {isPlatformNode, isPlatformSelectNode} = createInlinePlatformChecks(
Expand Down
Expand Up @@ -10,9 +10,6 @@

'use strict';

/* $FlowFixMe(>=0.99.0 site=react_native_fb) This comment suppresses an error
* found when Flow v0.99 was deployed. To see the error, delete this comment
* and run Flow. */
const traverse = require('@babel/traverse').default;

import type {Ast} from '@babel/core';
Expand Down
Expand Up @@ -10,12 +10,12 @@

'use strict';

import typeof {types as BabelTypes} from '@babel/core';
import type {Types} from '@babel/types';

const importMap = new Map([['ReactNative', 'react-native']]);

function createInlinePlatformChecks(
t: BabelTypes,
t: Types,
requireName: string = 'require',
): {|
isPlatformNode: (node: any, scope: any, isWrappedModule: boolean) => boolean,
Expand Down
3 changes: 0 additions & 3 deletions packages/metro/src/Bundler/util.js
Expand Up @@ -10,9 +10,6 @@

'use strict';

/* $FlowFixMe(>=0.99.0 site=react_native_fb) This comment suppresses an error
* found when Flow v0.99 was deployed. To see the error, delete this comment
* and run Flow. */
const template = require('@babel/template').default;
const babelTypes = require('@babel/types');
const babylon = require('@babel/parser');
Expand Down
Expand Up @@ -10,8 +10,8 @@

'use strict';

import typeof {types as BabelTypes} from '@babel/core';
import type {Path} from '@babel/traverse';
import type {Types} from '@babel/types';

type State = {|
opts: {|
Expand All @@ -22,7 +22,7 @@ type State = {|
function reverseDependencyMapReferences({
types: t,
}: {
types: BabelTypes,
types: Types,
...
}): {|visitor: {|CallExpression: (path: Path, state: State) => void|}|} {
return {
Expand Down

0 comments on commit ccba71d

Please sign in to comment.