import { DecoratorsPluginOptions , FlowPluginOptions , ParserOptions as BabelParserOptions , ParserPlugin , PipelineOperatorPluginOptions , RecordAndTuplePluginOptions , TypeScriptPluginOptions , } from '@babel/parser' ; type ParserPluginName = Extract < ParserPlugin , string > | Extract < ParserPlugin , [ string , object ] > [ 0 ] ; type ParserPluginOptions = Extract < ParserPlugin , [ string , object ] > [ 1 ] ; const DefaultParserPlugins = new Set < ParserPlugin > ( [ 'asyncGenerators' , 'bigInt' , 'classPrivateMethods' , 'classPrivateProperties' , 'classProperties' , 'doExpressions' , 'dynamicImport' , 'exportDefaultFrom' , 'exportNamespaceFrom' , 'functionBind' , 'functionSent' , 'importAssertions' , 'importMeta' , 'jsx' , 'logicalAssignment' , 'nullishCoalescingOperator' , 'numericSeparator' , 'objectRestSpread' , 'optionalCatchBinding' , 'optionalChaining' , 'partialApplication' , 'placeholders' , 'throwExpressions' , 'topLevelAwait' , [ 'decorators' , { decoratorsBeforeExport : true } ] , [ 'pipelineOperator' , { proposal : 'minimal' } ] , [ 'recordAndTuple' , { syntaxType : 'hash' } ] , ] ) ; export interface ParserOptions extends Omit < BabelParserOptions , 'plugins' > { plugins ? : Array < ParserPlugin > ; } /**
 * Builds options for `@babel/parser` by enabling as many features as possible,
 * while also preserving the options given as an argument.
 */ export default function buildOptions ( { sourceType = 'unambiguous' , allowAwaitOutsideFunction = true , allowImportExportEverywhere = true , allowReturnOutsideFunction = true , allowSuperOutsideMethod = true , allowUndeclaredExports = true , plugins = [ ] , sourceFilename , ... rest } = { } ) { for ( const plugin of DefaultParserPlugins ) { if ( shouldAddPlugin ( plugins , plugin ) ) { plugins = [ ... plugins , plugin ] ; } } const typePlugin = typePluginForSourceFileName ( sourceFilename ) ; if ( shouldAddPlugin ( plugins , typePlugin ) ) { plugins = [ ... plugins , typePlugin ] ; } return { sourceType , allowAwaitOutsideFunction , allowImportExportEverywhere , allowReturnOutsideFunction , allowSuperOutsideMethod , allowUndeclaredExports , plugins , sourceFilename , ... rest , } ; } example function typePluginForSourceFileName ( sourceFileName ? ) { if ( typeof sourceFileName === 'string' && ! /\.tsx?$/i . test ( sourceFileName ) ) { return 'flow' ; } else { return 'typescript' ; } } function getPluginOptions ( plugins , name ) ; function getPluginOptions ( plugins , name ) ; function getPluginOptions ( plugins , name ) ; function getPluginOptions ( plugins , name ) ; function getPluginOptions ( plugins , name ) ; function getPluginOptions ( plugins , name ) ; function getPluginOptions ( plugins , name ) { for ( const plugin of plugins ) { if ( Array . isArray ( plugin ) ) { if ( plugin [ 0 ] === name ) { return plugin [ 1 ] ; } } else if ( plugin === name ) { return { } ; } } } example function shouldAddPlugin ( plugins , plugin ) { const name = getPluginName ( plugin ) ; if ( pluginListIncludesPlugin ( plugins , name ) ) { return false ; } switch ( name ) { case 'flow' : case 'flowComments' : return ! getPluginOptions ( plugins , 'typescript' ) ; case 'typescript' : return ! ( getPluginOptions ( plugins , 'flow' ) || getPluginOptions ( plugins , 'flowComments' ) ) ; case 'decorators' : return ! getPluginOptions ( plugins , 'decorators-legacy' ) ; case 'decorators-legacy' : return ! getPluginOptions ( plugins , 'decorators' ) ; case 'recordAndTuple' : case 'pipelineOperator' : { const recordAndTupleOptions = name === 'recordAndTuple' ? ( plugin [ 1 ] as RecordAndTuplePluginOptions ) : getPluginOptions ( plugins , 'recordAndTuple' ) ; const pipelineOperatorOptions = name === 'pipelineOperator' ? ( plugin [ 1 ] as PipelineOperatorPluginOptions ) : getPluginOptions ( plugins , 'pipelineOperator' ) ; if ( recordAndTupleOptions ?. syntaxType === 'hash' ) { if ( pipelineOperatorOptions ?. proposal === 'smart' ) { return false ; } if ( pipelineOperatorOptions ?. proposal === 'hack' && pipelineOperatorOptions . topicToken === '#' ) { return false ; } } return true ; } default : return true ; } } example function pluginListIncludesPlugin ( plugins , name ) { return plugins . some ( ( entry ) => getPluginName ( entry ) === name ) ; } example export function getPluginName ( plugin ) { return typeof plugin === 'string' ? plugin : plugin [ 0 ] ; }