@@ -15,6 +15,30 @@ function isEmpty(obj: any): boolean {
1515
1616type BabelParser = Pick < typeof defaultBabelParser , 'parse' | 'parseExpression' >
1717
18+ function mergePlugins (
19+ a : ParserPlugin [ ] | undefined ,
20+ b : ParserPlugin [ ] | undefined
21+ ) : ParserPlugin [ ] | undefined {
22+ if ( ! b ) return a
23+ if ( ! a ) return b
24+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25+ const map : Map < string , any > = new Map (
26+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
27+ a . map ( ( p : ParserPlugin ) : [ string , any ] =>
28+ Array . isArray ( p ) ? p : [ p , undefined ]
29+ )
30+ )
31+ for ( const p of b ) {
32+ if ( Array . isArray ( p ) ) map . set ( p [ 0 ] , { ...map . get ( p [ 0 ] ) , ...p [ 1 ] } )
33+ else if ( ! map . has ( p ) ) map . set ( p , map . get ( p ) )
34+ }
35+ return [ ...map . entries ( ) ] . map (
36+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37+ ( e : [ string , any ] ) => ( e [ 1 ] === undefined ? e [ 0 ] : e )
38+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
39+ ) as any
40+ }
41+
1842export class Parser {
1943 readonly babelParser : BabelParser
2044 readonly parserOpts : ParserOptions
@@ -37,7 +61,11 @@ export class Parser {
3761 }
3862
3963 bindParserOpts ( parserOpts : ParserOptions ) : Parser {
40- return new Parser ( this . babelParser , { ...this . parserOpts , ...parserOpts } )
64+ return new Parser ( this . babelParser , {
65+ ...this . parserOpts ,
66+ ...parserOpts ,
67+ plugins : mergePlugins ( this . parserOpts . plugins , parserOpts . plugins ) ,
68+ } )
4169 }
4270}
4371
@@ -141,10 +169,7 @@ function createParserFromConfig(babelParser: BabelParser, config: any): Parser {
141169 return new Parser ( babelParser , opts . parserOpts )
142170}
143171
144- export function getParserSync (
145- file : string ,
146- options ?: Omit < ParserOptions , 'plugins' >
147- ) : Parser {
172+ export function getParserSync ( file : string , options ?: ParserOptions ) : Parser {
148173 let result
149174 const parentDir = Path . dirname ( file )
150175 const extname = Path . extname ( file )
@@ -185,7 +210,7 @@ export function getParserSync(
185210
186211export async function getParserAsync (
187212 file : string ,
188- options ?: Omit < ParserOptions , 'plugins' >
213+ options ?: ParserOptions
189214) : Promise < Parser > {
190215 let promise
191216 if ( / \. t s $ / . test ( file ) ) promise = Promise . resolve ( tsParser )
@@ -241,7 +266,7 @@ export function parseSync(
241266 {
242267 encoding = 'utf8' ,
243268 ...options
244- } : { encoding ?: BufferEncoding } & Omit < ParserOptions , 'plugins' > = { }
269+ } : { encoding ?: BufferEncoding } & ParserOptions = { }
245270) : t . File {
246271 const parser = getParserSync ( file , options )
247272 return parser . parse ( readFileSync ( file , encoding ) )
@@ -252,7 +277,7 @@ export async function parseAsync(
252277 {
253278 encoding = 'utf8' ,
254279 ...options
255- } : { encoding ?: BufferEncoding } & Omit < ParserOptions , 'plugins' > = { }
280+ } : { encoding ?: BufferEncoding } & ParserOptions = { }
256281) : Promise < t . File > {
257282 const parser = await getParserAsync ( file , options )
258283 return parser . parse ( await readFile ( file , encoding ) )
0 commit comments