-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: getParserServices should throw informative error when used with non-typescript-eslint parser #6814
Comments
I cannot reproduce this. I tried installing v6 of the parser and the plugin, then running
For interest's sake I did a
Which is what I'd expect for our v6 changes. In terms of the
So it returns without error.
If it's an empty object then it's likely it was not provided by our parser - our parser has returned a complete object with properties for |
Ah, I should have made it more clear. This issue occurs with Babel parser (I use Babel parser when testing my plugin in environments without TypeScript). Should I add a check to my plugin to test what parser is bring used before calling this function? Or maybe this function could perform that check? |
The general problem is that if a user uses babel as their parser then lint rules can subtly break in many ways - the babel AST has many differences compared to our AST. People can happily use babel as their parser for JS code - but not for TS code. We can consider handing this case in case the user wants to use a 3rd party rule on pure JS code parsed by another parser (though TBH they should still just use our parser everywhere). The issue is that we can't provide the maps at all in this case. |
Maybe |
Perhaps a 3rd boolean param to explicitly opt-in to other parsers? |
Not sure if related, transient issue in WebStorm with same stacktrace: #6575 (comment) |
Agreed that silently passing when things should break would be confusing. I'd like to see it switched to throwing an error by default honestly. If you're trying to get type info in a rule and it's a babel AST, you probably wouldn't want the rule to run at all. Maybe we can switch the existing second boolean param to a type union of boolean and an options object? Thoughts? |
The usecase for "I want to support TS code as well as JS code and I want to support other parsers for JS and TS parser for TS and if and only if there are types then I want to consume the types" is pretty niche. Most of the time it is instead "I want to support TS parser and consume types" or "I want to support any parser". This is definitely a very, very rare case - which is why this is the first report of it. We already advise against progressive enhancements with type information due to the weirdness it introduces by potentially hiding configuration problems - eg "oops I borked my config and this file wasn't type-aware parsed" should blow up - rather than having a rule silently turn features off and false-negativing. I guess I would need to better understand the usecase for such flexibility before we consider this. |
Coming back to this: I do think we should throw an error if someone is using typed linting with a parser other than Babel. That's not a use case that's likely to work. The real issue, then, is that the error should probably be more informative than the current: 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. Accepting PRs to add an error message that explicitly says... something around the lines of the parser needing to be ours. I haven't put much thought into wording 🙂 |
getParserServices(context, true)
throwing error
We might want to have #8150 supercede this. Rules can't make much of a decision on this themselves. |
Before You File a Bug Report Please Confirm You Have Done The Following...
Relevant Package
utils
Playground Link
No response
Repro Code
ESLint Config
tsconfig
Expected Result
typeof
parserServices
should beParserServicesWithoutTypeInformation
Actual Result
An error is thrown:
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.
Additional Info
This check is testing for
null
orundefined
but mycontext.parserServices
is an empty object and thus the error is thrown.https://github.com/typescript-eslint/typescript-eslint/blob/v6/packages/utils/src/eslint-utils/getParserServices.ts#L35-L39
Edit: Looks like
parserServices
isn't actually set directly on thecontext
object but is instead being inherited through its prototype chain.Versions
@typescript-eslint/eslint-plugin
6.0.0-alpha.106
@typescript-eslint/parser
6.0.0-alpha.106
@typescript-eslint/scope-manager
6.0.0-alpha.106
@typescript-eslint/typescript-estree
6.0.0-alpha.106
@typescript-eslint/type-utils
6.0.0-alpha.106
@typescript-eslint/utils
6.0.0-alpha.106
TypeScript
5.0.3
ESLint
8.36.0
node
18.15.0
The text was updated successfully, but these errors were encountered: