Skip to content

Commit e67e523

Browse files
committed
chore(no-sync): remove @typescript-eslint/utils
- Removes the dependency on `typescript`, a peer dependency of `@typescript-eslint/utils`
1 parent 42464ab commit e67e523

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

lib/util/get-parser-services.js

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,59 @@
11
"use strict"
22

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 - The parser to check.
13+
* @returns {boolean} `true` if the parser seems to be `@typescript-eslint/parser`, `false` otherwise.
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 - The parser name to mention in the error message.
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+
}
631

732
/**
833
* Get the TypeScript parser services.
9-
* If TypeScript isn't present, returns `null`.
34+
* - Partial implementation from `@typescript-eslint/utils`. @see https://github.com/typescript-eslint/typescript-eslint/blob/3e545207f0e34611f528128fc699b25091bc40b3/packages/utils/src/eslint-utils/getParserServices.ts
1035
*
11-
* @param {import('eslint').Rule.RuleContext} context - rule context
12-
* @returns {import('@typescript-eslint/parser').ParserServices | null}
36+
* @param {import('eslint').Rule.RuleContext} context - Rule context.
37+
* @returns {import('@typescript-eslint/parser').ParserServices | null} Parser services if TypeScript is being used, `null` otherwise.
38+
* @throws {Error} If parser services are present but incomplete.
1339
*/
1440
module.exports = function getParserServices(context) {
15-
// Not using tseslint parser?
41+
const parserServices = context.sourceCode.parserServices
42+
1643
if (
17-
context.sourceCode.parserServices?.esTreeNodeToTSNodeMap == null ||
18-
context.sourceCode.parserServices.tsNodeToESTreeNodeMap == null
44+
!parserServices ||
45+
parserServices.esTreeNodeToTSNodeMap === null ||
46+
parserServices.tsNodeToESTreeNodeMap === null
1947
) {
48+
// Not using TypeScript parser, or no type info: return null for legacy/JS support.
2049
return null
2150
}
2251

23-
return getParserServicesFromTsEslint(/** @type {any} */ (context), true)
52+
if (parserServices.program === null) {
53+
const parser =
54+
context.parserPath || context.languageOptions?.parser?.meta?.name
55+
throwError(parser)
56+
}
57+
58+
return parserServices
2459
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
},
1919
"dependencies": {
2020
"@eslint-community/eslint-utils": "^4.5.0",
21-
"@typescript-eslint/utils": "^8.26.1",
2221
"enhanced-resolve": "^5.17.1",
2322
"eslint-plugin-es-x": "^7.8.0",
2423
"get-tsconfig": "^4.8.1",
@@ -122,4 +121,4 @@
122121
"imports": {
123122
"#test-helpers": "./tests/test-helpers.js"
124123
}
125-
}
124+
}

0 commit comments

Comments
 (0)