-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
getParserServices.ts
31 lines (28 loc) · 966 Bytes
/
getParserServices.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { ParserServices, TSESLint } from '../';
const ERROR_MESSAGE =
'You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.';
type RequiredParserServices = {
[k in keyof ParserServices]: Exclude<ParserServices[k], undefined>;
};
/**
* Try to retrieve typescript parser service from context
*/
export function getParserServices<
TMessageIds extends string,
TOptions extends unknown[]
>(
context: TSESLint.RuleContext<TMessageIds, TOptions>,
): RequiredParserServices {
if (
!context.parserServices ||
!context.parserServices.program ||
!context.parserServices.esTreeNodeToTSNodeMap
) {
/**
* The user needs to have configured "project" in their parserOptions
* for @typescript-eslint/parser
*/
throw new Error(ERROR_MESSAGE);
}
return context.parserServices as RequiredParserServices;
}