From ccba71d12663b53190005feb7c0af953d24e8f7e Mon Sep 17 00:00:00 2001 From: Christoph Nakazawa Date: Fri, 17 Apr 2020 09:16:53 -0700 Subject: [PATCH] Minor cleanup of Babel types 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 --- flow-typed/babel.js.flow | 155 ++++-------------- .../src/constant-folding-plugin.js | 4 +- .../src/import-export-plugin.js | 6 +- packages/metro-transform-plugins/src/index.js | 4 +- .../src/inline-plugin.js | 4 +- .../src/normalizePseudoGlobals.js | 3 - .../src/utils/createInlinePlatformChecks.js | 4 +- packages/metro/src/Bundler/util.js | 3 - .../reverse-dependency-map-references.js | 4 +- .../src/ModuleGraph/worker/JsFileWrapping.js | 7 - .../ModuleGraph/worker/collectDependencies.js | 6 - .../ModuleGraph/worker/generateImportNames.js | 3 - .../worker/optimizeDependencies.js | 3 - 13 files changed, 42 insertions(+), 164 deletions(-) diff --git a/flow-typed/babel.js.flow b/flow-typed/babel.js.flow index ac44dc0bdc..af8ebb3422 100644 --- a/flow-typed/babel.js.flow +++ b/flow-typed/babel.js.flow @@ -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, @@ -67,46 +40,6 @@ type GeneratorOptions = { type InlinePlugin = string | {...} | (() => {...}); type _Plugins = Array; -// 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, - ignore?: string | RegExp | Array, - 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; @@ -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, @@ -126,7 +58,6 @@ export type GeneratorResult = { }; type VisitFn = (path: Object, state: State) => any; -type BabelTypes = {[key: string]: Function, ...}; type BabelVisitor = {...}; type _BabelSourceMapSegment = { @@ -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. @@ -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, @@ -188,7 +112,7 @@ type BabelCoreOptions = {| moduleIds?: ?{[string]: string, ...}, moduleRoot?: string, only?: ?(string | Array | RegExp | Array), - parserOpts?: ?Babylon7Options, + parserOpts?: ?BabelParserOptions, plugins?: ?_Plugins, presets?: Array, retainLines?: boolean, @@ -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 @@ -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, - visitor: BabelVisitor, - scope?: ?Object, - state?: ?Object, - parentPath?: ?Object, -) => void; - export type TransformSync = ( code: string, options: BabelCoreOptions, @@ -265,10 +172,8 @@ 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; @@ -276,11 +181,6 @@ declare module '@babel/core' { 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. @@ -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 ( @@ -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, + visitor: BabelVisitor, + scope?: ?Object, + state?: ?Object, + parentPath?: ?Object, + ) => void; } diff --git a/packages/metro-transform-plugins/src/constant-folding-plugin.js b/packages/metro-transform-plugins/src/constant-folding-plugin.js index 8509f37d4d..f5af1bd184 100644 --- a/packages/metro-transform-plugins/src/constant-folding-plugin.js +++ b/packages/metro-transform-plugins/src/constant-folding-plugin.js @@ -10,7 +10,7 @@ 'use strict'; -import typeof {types as BabelTypes} from '@babel/core'; +import type {Types} from '@babel/types'; export type Visitors = {| visitor: {| @@ -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) { diff --git a/packages/metro-transform-plugins/src/import-export-plugin.js b/packages/metro-transform-plugins/src/import-export-plugin.js index 4ba2f1fd5c..f22850e824 100644 --- a/packages/metro-transform-plugins/src/import-export-plugin.js +++ b/packages/metro-transform-plugins/src/import-export-plugin.js @@ -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'; @@ -107,7 +105,7 @@ const esModuleExportTemplate = template(` /** * Resolution template in case it is requested. */ -const resolveTemplate = template.expression(` +const resolveTemplate = expression(` require.resolve(NODE) `); diff --git a/packages/metro-transform-plugins/src/index.js b/packages/metro-transform-plugins/src/index.js index f44f881e65..822816ed8f 100644 --- a/packages/metro-transform-plugins/src/index.js +++ b/packages/metro-transform-plugins/src/index.js @@ -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 = ( - context: {types: BabelTypes, ...}, + context: {types: Types, ...}, options: OptionsT, ) => VisitorT; diff --git a/packages/metro-transform-plugins/src/inline-plugin.js b/packages/metro-transform-plugins/src/inline-plugin.js index 052b3d0245..cb6eb240c3 100644 --- a/packages/metro-transform-plugins/src/inline-plugin.js +++ b/packages/metro-transform-plugins/src/inline-plugin.js @@ -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, @@ -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( diff --git a/packages/metro-transform-plugins/src/normalizePseudoGlobals.js b/packages/metro-transform-plugins/src/normalizePseudoGlobals.js index 120ecbfd75..61ed34a320 100644 --- a/packages/metro-transform-plugins/src/normalizePseudoGlobals.js +++ b/packages/metro-transform-plugins/src/normalizePseudoGlobals.js @@ -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'; diff --git a/packages/metro-transform-plugins/src/utils/createInlinePlatformChecks.js b/packages/metro-transform-plugins/src/utils/createInlinePlatformChecks.js index 6c8c855c92..5418be4ab7 100644 --- a/packages/metro-transform-plugins/src/utils/createInlinePlatformChecks.js +++ b/packages/metro-transform-plugins/src/utils/createInlinePlatformChecks.js @@ -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, diff --git a/packages/metro/src/Bundler/util.js b/packages/metro/src/Bundler/util.js index dfa0a71641..d14e92b561 100644 --- a/packages/metro/src/Bundler/util.js +++ b/packages/metro/src/Bundler/util.js @@ -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'); diff --git a/packages/metro/src/ModuleGraph/output/reverse-dependency-map-references.js b/packages/metro/src/ModuleGraph/output/reverse-dependency-map-references.js index 904ec0ea4c..31b2dedf4f 100644 --- a/packages/metro/src/ModuleGraph/output/reverse-dependency-map-references.js +++ b/packages/metro/src/ModuleGraph/output/reverse-dependency-map-references.js @@ -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: {| @@ -22,7 +22,7 @@ type State = {| function reverseDependencyMapReferences({ types: t, }: { - types: BabelTypes, + types: Types, ... }): {|visitor: {|CallExpression: (path: Path, state: State) => void|}|} { return { diff --git a/packages/metro/src/ModuleGraph/worker/JsFileWrapping.js b/packages/metro/src/ModuleGraph/worker/JsFileWrapping.js index fcb18f0f51..6270ef2a1d 100644 --- a/packages/metro/src/ModuleGraph/worker/JsFileWrapping.js +++ b/packages/metro/src/ModuleGraph/worker/JsFileWrapping.js @@ -12,14 +12,7 @@ /* eslint-disable lint/no-unclear-flowtypes */ const t = require('@babel/types'); -/* $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; - -/* $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; const WRAP_NAME = '$$_REQUIRE'; // note: babel will prefix this with _ diff --git a/packages/metro/src/ModuleGraph/worker/collectDependencies.js b/packages/metro/src/ModuleGraph/worker/collectDependencies.js index 6dc2e91f5f..027e20c9c6 100644 --- a/packages/metro/src/ModuleGraph/worker/collectDependencies.js +++ b/packages/metro/src/ModuleGraph/worker/collectDependencies.js @@ -13,13 +13,7 @@ const nullthrows = require('nullthrows'); const generate = require('@babel/generator').default; -/* $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; -/* $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; const types = require('@babel/types'); diff --git a/packages/metro/src/ModuleGraph/worker/generateImportNames.js b/packages/metro/src/ModuleGraph/worker/generateImportNames.js index 76746d564c..cd15341381 100644 --- a/packages/metro/src/ModuleGraph/worker/generateImportNames.js +++ b/packages/metro/src/ModuleGraph/worker/generateImportNames.js @@ -11,9 +11,6 @@ 'use strict'; const nullthrows = require('nullthrows'); -/* $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'; diff --git a/packages/metro/src/ModuleGraph/worker/optimizeDependencies.js b/packages/metro/src/ModuleGraph/worker/optimizeDependencies.js index 35ec802c84..99faaab591 100644 --- a/packages/metro/src/ModuleGraph/worker/optimizeDependencies.js +++ b/packages/metro/src/ModuleGraph/worker/optimizeDependencies.js @@ -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; const babelGenerate = require('@babel/generator').default;