Skip to content

Commit 5b3c683

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 5b3c683

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

lib/util/get-parser-services.js

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,65 @@
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
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+
}
631

732
/**
833
* Get the TypeScript parser services.
934
* 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
1038
*
1139
* @param {import('eslint').Rule.RuleContext} context - rule context
40+
* @param {boolean} [allowWithoutFullTypeInformation=false]
1241
* @returns {import('@typescript-eslint/parser').ParserServices | null}
1342
*/
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+
1649
if (
17-
context.sourceCode.parserServices?.esTreeNodeToTSNodeMap == null ||
18-
context.sourceCode.parserServices.tsNodeToESTreeNodeMap == null
50+
!parserServices ||
51+
parserServices.esTreeNodeToTSNodeMap == null ||
52+
parserServices.tsNodeToESTreeNodeMap == null
1953
) {
54+
// Not using TypeScript parser, or no type info: return null for legacy/JS support
2055
return null
2156
}
2257

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
2465
}

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)