|
1 | 1 | "use strict"
|
2 | 2 |
|
3 |
| -const { |
4 |
| - getParserServices: getParserServicesFromTsEslint, |
5 |
| -} = require("@typescript-eslint/utils/eslint-utils") |
| 3 | +const ERROR_MESSAGE_REQUIRES_PARSER_SERVICES = |
| 4 | + "You have used a rule which requires type information, but don't have parserOptions set to generate type information for this file. See https://typescript-eslint.io/getting-started/typed-linting for enabling linting with type information." |
| 5 | +const ERROR_MESSAGE_UNKNOWN_PARSER = |
| 6 | + 'Note: detected a parser other than @typescript-eslint/parser. Make sure the parser is configured to forward "parserOptions.project" to @typescript-eslint/parser.' |
| 7 | + |
| 8 | +/** |
| 9 | + * Checks if the parser seems to be `@typescript-eslint/parser`. |
| 10 | + * - Implementation from `@typescript-eslint/utils`. @see https://github.com/typescript-eslint/typescript-eslint/blob/3e545207f0e34611f528128fc699b25091bc40b3/packages/utils/src/eslint-utils/parserSeemsToBeTSESLint.ts |
| 11 | + * |
| 12 | + * @param {string | undefined} parser |
| 13 | + * @returns {boolean} |
| 14 | + */ |
| 15 | +function parserSeemsToBeTSESLint(parser) { |
| 16 | + return !!parser && /(?:typescript-eslint|\.\.)[\w/\\]*parser/.test(parser) |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Throws a detailed error if parser services are not available. |
| 21 | + * @param {string | undefined} parser |
| 22 | + */ |
| 23 | +function throwError(parser) { |
| 24 | + const messages = [ |
| 25 | + ERROR_MESSAGE_REQUIRES_PARSER_SERVICES, |
| 26 | + `Parser: ${parser || "(unknown)"}`, |
| 27 | + !parserSeemsToBeTSESLint(parser) && ERROR_MESSAGE_UNKNOWN_PARSER, |
| 28 | + ].filter(Boolean) |
| 29 | + throw new Error(messages.join("\n")) |
| 30 | +} |
6 | 31 |
|
7 | 32 | /**
|
8 | 33 | * Get the TypeScript parser services.
|
9 | 34 | * If TypeScript isn't present, returns `null`.
|
| 35 | + * Throws if parser services are present but incomplete. |
| 36 | + * |
| 37 | + * Partial implementation from `@typescript-eslint/utils` @see https://github.com/typescript-eslint/typescript-eslint/blob/3e545207f0e34611f528128fc699b25091bc40b3/packages/utils/src/eslint-utils/getParserServices.ts |
10 | 38 | *
|
11 | 39 | * @param {import('eslint').Rule.RuleContext} context - rule context
|
| 40 | + * @param {boolean} [allowWithoutFullTypeInformation=false] |
12 | 41 | * @returns {import('@typescript-eslint/parser').ParserServices | null}
|
13 | 42 | */
|
14 |
| -module.exports = function getParserServices(context) { |
15 |
| - // Not using tseslint parser? |
| 43 | +module.exports = function getParserServices( |
| 44 | + context, |
| 45 | + allowWithoutFullTypeInformation = false |
| 46 | +) { |
| 47 | + const parserServices = context.sourceCode.parserServices |
| 48 | + |
16 | 49 | if (
|
17 |
| - context.sourceCode.parserServices?.esTreeNodeToTSNodeMap == null || |
18 |
| - context.sourceCode.parserServices.tsNodeToESTreeNodeMap == null |
| 50 | + !parserServices || |
| 51 | + parserServices.esTreeNodeToTSNodeMap == null || |
| 52 | + parserServices.tsNodeToESTreeNodeMap == null |
19 | 53 | ) {
|
| 54 | + // Not using TypeScript parser, or no type info: return null for legacy/JS support |
20 | 55 | return null
|
21 | 56 | }
|
22 | 57 |
|
23 |
| - return getParserServicesFromTsEslint(/** @type {any} */ (context), true) |
| 58 | + if (parserServices.program == null && !allowWithoutFullTypeInformation) { |
| 59 | + const parser = |
| 60 | + context.parserPath || context.languageOptions?.parser?.meta?.name |
| 61 | + throwError(parser) |
| 62 | + } |
| 63 | + |
| 64 | + return parserServices |
24 | 65 | }
|
0 commit comments