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
RuleTester: Cannot test failing suggestion fixer #13723
Comments
Thanks for the issue! By the LintMessage specification,
A bug fix would be to update The rule should check upfront whether a fix is possible, and then just not return the whole suggestion object if the fix cannot be applied. In tests, asserting that there were no suggestions can be done with However, returning If @DMartens just to verify, is that the expected behavior? |
I would not remove the suggestion from the suggestions, as the user might think that the suggestion is available but it is not presented to them. |
In this case, we have an invalid
Line 95 in c1974b3
VSCode integration also expects
So, this seems to be something that should be fixed in I think we should update |
I'd propose that we update report translator to remove the whole suggestion from the array of suggestions if the suggestion's That way, end-users shouldn't notice a difference, aside from not seeing crashes in the editor due to broken suggestions anymore. Rule developers will have an additional place where it can be decided to not provide the suggestion: the I believe the only downside is that some tests in plugins might have to be updated after this change, if such tests exist (it's possible, since the |
@mdjermanovic I’d much rather update RuleTester to throw when a suggestion doesn’t produce a fix, as I think that’s an error condition that may be easy to miss. I can’t think of a case where it’s not possible to know if a suggestion can be applied before creating the suggestion. |
RuleTester already throws if the actual number of suggestions doesn't match the expected number. If we remove the whole suggestion when Assuming that test cases for those rules have the
We often use eslint/lib/rules/operator-assignment.js Lines 116 to 131 in 551b1e9
It's always possible to extract the first part of the If we don't allow |
Ah ok, I was unaware that ESLint itself was returning null in some cases. While I still think that’s not a great thing to allow or encourage, I wouldn’t want to make a change to break existing behavior. With that all said, I’m 👍 to your proposal for removing the suggestion if fix() returns null. |
Tell us about your environment
Environment Info:
Node version: v14.12.0
npm version: v6.14.2
Local ESLint version: v7.10.0 (Currently used)
Global ESLint version: Not found
What did you do?
I am currently creating a custom rule with a suggestion fixer.
Sometimes the suggestion is not applicable and I return null directly:
Then if I test this in the rule tester like this:
I get the error:
TypeError: Cannot read property 'range' of null
.I investigated and the basic flow is:
I expect returning null from a fixer to be supported behaviour because it works if I convert the suggestion to a fix.
Are you willing to submit a pull request to fix this bug?
Yes
The text was updated successfully, but these errors were encountered: