Bug: [consistent-type-assertions] should not error when parser services are not available
Before You File a Bug Report Please Confirm You Have Done The Following...
- [X] I have tried restarting my IDE and the issue persists.
- [X] I have updated to the latest version of the packages.
- [X] I have searched for related issues and found none that matched my issue.
- [X] I have read the FAQ and my problem is not listed.
Issue Description
See https://github.com/typescript-eslint/typescript-eslint/issues/9855#issuecomment-2302246491:
The issue is that the rule is using the TS AST which comes from parser services.
The util shouldn't eagerly error in this case though, I don't think. It should lazily error when the rule accesses the services instead - I.e. Make the properties getters that throw.
Also we could probably fix the rule to not use the TS AST. I think we updated the utils to allow us to leverage the estree ast for operator precedence now?
My opinion: non-type-aware rules should never crash on a file for which they match no selectors.
I.e. Json or angular template files that don't use the same ast structure shouldn't crash in our rules like this.
In the reproduction for this issue (see Stackblitz link below), the rule is enabled on a JavaScript file parsed with Espree, not typescript-eslint's parser. Instead of doing nothing, the rule errors:
Error while loading rule '@typescript-eslint/consistent-type-assertions': 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.
Parser: (unknown)
Note: detected a parser other than @typescript-eslint/parser. Make sure the parser is configured to forward "parserOptions.project" to @typescript-eslint/parser.
Reproduction Repository Link
https://stackblitz.com/edit/vitejs-vite-fgbcrl?file=eslint.config.js
Repro Steps
npm installnpm run lint
Versions
{
"eslint": "9.9.0",
"typescript": "5.3.3",
"typescript-eslint": "8.2.0"
}