1
- import type { ESLintExtendedProgram , ESLintProgram } from "." ;
2
1
import { 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 ;
14
10
15
- /** Get parser name */
16
- export function getParserName (
11
+ /** Get parser for script lang */
12
+ export function getParserForLang (
17
13
attrs : Record < string , string | undefined > ,
18
- parser : any
19
- ) : string {
14
+ parser : UserOptionParser
15
+ ) : string | ParserObject {
20
16
if ( parser ) {
21
- if ( typeof parser === "string" && parser !== "espree" ) {
17
+ if ( typeof parser === "string" || isParserObject ( parser ) ) {
22
18
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 ;
27
24
}
28
25
}
29
26
}
@@ -33,12 +30,15 @@ export function getParserName(
33
30
/** Get parser */
34
31
export function getParser (
35
32
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" ) {
40
40
// eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
41
- return require ( name ) ;
41
+ return require ( parserValue ) ;
42
42
}
43
43
return getEspree ( ) ;
44
44
}
0 commit comments