1- import type { ESLintExtendedProgram , ESLintProgram } from "." ;
21import { getEspree } from "./espree" ;
3- /**
4- * The interface of a result of ESLint custom parser.
5- */
6- export type ESLintCustomParserResult = ESLintProgram | ESLintExtendedProgram ;
7- /**
8- * The interface of ESLint custom parsers.
9- */
10- export interface ESLintCustomParser {
11- parse ( code : string , options : any ) : ESLintCustomParserResult ;
12- parseForESLint ?( code : string , options : any ) : ESLintCustomParserResult ;
13- }
2+ import type { ParserObject } from "./parser-object" ;
3+ import { isParserObject } from "./parser-object" ;
4+
5+ type UserOptionParser =
6+ | string
7+ | ParserObject
8+ | Record < string , string | ParserObject | undefined >
9+ | undefined ;
1410
15- /** Get parser name */
16- export function getParserName (
11+ /** Get parser for script lang */
12+ export function getParserForLang (
1713 attrs : Record < string , string | undefined > ,
18- parser : any
19- ) : string {
14+ parser : UserOptionParser
15+ ) : string | ParserObject {
2016 if ( parser ) {
21- if ( typeof parser === "string" && parser !== "espree" ) {
17+ if ( typeof parser === "string" || isParserObject ( parser ) ) {
2218 return parser ;
23- } else if ( typeof parser === "object" ) {
24- const name = parser [ attrs . lang || "js" ] ;
25- if ( typeof name === "string" ) {
26- return getParserName ( attrs , name ) ;
19+ }
20+ if ( typeof parser === "object" ) {
21+ const value = parser [ attrs . lang || "js" ] ;
22+ if ( typeof value === "string" || isParserObject ( value ) ) {
23+ return value ;
2724 }
2825 }
2926 }
@@ -33,12 +30,15 @@ export function getParserName(
3330/** Get parser */
3431export function getParser (
3532 attrs : Record < string , string | undefined > ,
36- parser : any
37- ) : ESLintCustomParser {
38- const name = getParserName ( attrs , parser ) ;
39- if ( name !== "espree" ) {
33+ parser : UserOptionParser
34+ ) : ParserObject {
35+ const parserValue = getParserForLang ( attrs , parser ) ;
36+ if ( isParserObject ( parserValue ) ) {
37+ return parserValue ;
38+ }
39+ if ( parserValue !== "espree" ) {
4040 // eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
41- return require ( name ) ;
41+ return require ( parserValue ) ;
4242 }
4343 return getEspree ( ) ;
4444}
0 commit comments