Skip to content
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

New: Suggestions #30

Merged
merged 1 commit into from Aug 9, 2019
Merged

New: Suggestions #30

merged 1 commit into from Aug 9, 2019

Conversation

ilyavolodin
Copy link
Member

Summary

This RTC proposes introducing a new mechanism for providing users with suggestions. It's very similar to fixes mechanism that already exists in ESLint, but will provide ability to create multiple suggestions per rule, suggestions will not be automatically applied by using --fix flag, and will require user interaction. This feature will only be exposed through the ESLint API, and will not be available on the CLI.

Related Issues

eslint/eslint#7873
eslint/eslint#6733 (comment)

Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your proposal.

I failed to read how this RFC changes public APIs. Would you elaborate about it?

designs/2019-suggestions/README.md Outdated Show resolved Hide resolved
designs/2019-suggestions/README.md Outdated Show resolved Hide resolved
designs/2019-suggestions/README.md Outdated Show resolved Hide resolved
designs/2019-suggestions/README.md Outdated Show resolved Hide resolved
designs/2019-suggestions/README.md Outdated Show resolved Hide resolved
designs/2019-suggestions/README.md Show resolved Hide resolved
@mysticatea mysticatea added feature Initial Commenting This RFC is in the initial feedback stage and removed triage labels Jul 9, 2019
@platinumazure
Copy link
Member

Thanks for the RFC!

I guess my main question is... Why do suggestions need to be separate from autofixes? Could we just add an attribute to autofixes which says whether it's a suggestion or meant to be a safe autofix? What value do suggestions add that autofix does not or cannot provide via an enhancement to autofix?

@ilyavolodin
Copy link
Member Author

There may be many suggestions per rule error, none of them should be applied automatically at any point. That behavior feels different enough to me to make it into it's own thing, and not try to piggy back on autofixes.

@ilyavolodin
Copy link
Member Author

@mysticatea I updated PR with your suggestions and some additional changes. Please take a look when you have a chance. CC: @gaearon

@not-an-aardvark
Copy link
Member

not-an-aardvark commented Jul 11, 2019

Could you give an example of a case where there would be many suggestions for a rule error?

Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the update.

I have more some suggestions.

  • About how editor integrators use the suggestions properties.
  • About processors which don't support autofix.

designs/2019-suggestions/README.md Outdated Show resolved Hide resolved
designs/2019-suggestions/README.md Outdated Show resolved Hide resolved
designs/2019-suggestions/README.md Outdated Show resolved Hide resolved
designs/2019-suggestions/README.md Outdated Show resolved Hide resolved
designs/2019-suggestions/README.md Outdated Show resolved Hide resolved
designs/2019-suggestions/README.md Outdated Show resolved Hide resolved
designs/2019-suggestions/README.md Show resolved Hide resolved
@mysticatea
Copy link
Member

I have one example, no-useless-escape rule. We have given up fixing the errors of no-useless-escape rule because it can be two cases. We can use suggestions for that.

  • Remove this \.
  • Use \\ instead.

@platinumazure
Copy link
Member

Why not just allow autofixes to include a new Boolean property to say if it's safe to apply as an autofix or not?

It seems the mechanism for describing what needs to change, at least, is exactly the same as autofix, so I feel we should reuse that if we can. I think the difference is we would be able to have more suggestions/fixes (such as no-useless-escape, sort-keys, etc.) but only specific fixes are applied automatically. Or we could have multiple levels of fixes based on how "safe" we think they are, and autofix only applies the safest fixes.

@mysticatea
Copy link
Member

@platinumazure How do you think to handle multiple suggestions on fix?

@platinumazure
Copy link
Member

@platinumazure How do you think to handle multiple suggestions on fix?

The same way as currently: Treat them as a combined fix for collision purposes, don't apply the fix automatically if it does collide. In the case of suggestions (which I am interpreting as fixes not applied automatically in my proposal), then the suggestion isn't applied anyway. It would be up to the integration to decide how to apply the fixes.

I could see us considering some quality-of-life options, such as not showing suggestions if autofixes conflict, not retrieving suggestions until all fixes are applied, etc., but I don't have anything specific in mind.

@mysticatea
Copy link
Member

@platinumazure No, we cannot combine two suggestions because the two are different solutions for the same error. For example,

new RegExp("*\.js", "u")
//           ^^
//           ├ Remove the '\'. (no-useless-escape)
//           └ Replace the '\' by '\\'. (no-useless-escape)

no-useless-escape provides two solutions for a \, then people will choose the correct solution on their editor integration.

@platinumazure
Copy link
Member

platinumazure commented Jul 15, 2019

Oh, that's a key insight. Okay, I agree, that changes things.

Maybe we could require that every report have either: Suggestions (array), fix (single fixable object), or neither. And suggestions shouldn't be automatically applied. I think now I'm on the same page as @ilyavolodin (more or less).

@ilyavolodin
Copy link
Member Author

@platinumazure Sorry about not providing concrete example. I was struggling to come up with simple enough example of suggestion that would be visually easy to understand. I think one problem that I see with suggestions is that they might be taken too far, as in suggest refactoring of a large chunk of code. I think that's outside of the responsibilities of ESLint.
But another example might be a custom rule that somebody might write that would suggest replacing callback pattern with either promises or async/await. Neither way is safe to do automatically, and neither is a "correct" solution, but a user can choose what he prefers. One more example might be a custom rule that sees _.clone() and suggests to add import of either lodash or underscore, or maybe even suggest replacing it completely with Object.assign in some cases. Again, user has to decide which one he wants to apply.
Fixes are basically yes/no question. And suggestions are multiple choice (in most cases).

@ilyavolodin
Copy link
Member Author

@mysticatea Updated again to incorporate your suggestions. Thank you very much for those, they are very helpful!

Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a minor suggestion. Thank you!

designs/2019-suggestions/README.md Outdated Show resolved Hide resolved
@ilyavolodin ilyavolodin added Final Commenting This RFC is in the final week of commenting and removed Initial Commenting This RFC is in the initial feedback stage labels Aug 2, 2019
@ilyavolodin
Copy link
Member Author

Moving this into final commenting stage. @eslint/eslint-tsc please take a look.

@gaearon
Copy link

gaearon commented Aug 2, 2019

@ilyavolodin Thank you so much for driving this!

If there’s anything I can clarify about our use case for this (for React Hooks) I’d be happy to provide details. (I linked to a few issues which you can see above.)

@platinumazure
Copy link
Member

I'm hoping to review this in the next day or two. Any chance we could wait to merge until, say, Wednesday? (If I don't review by then, just go ahead and merge.)

Sorry, I've been traveling and forgot about this RFC.

@ilyavolodin
Copy link
Member Author

Going to merge this in, as it doesn't seem like there are any objections.

@ilyavolodin ilyavolodin merged commit 62384fc into master Aug 9, 2019
@ilyavolodin ilyavolodin deleted the suggestions branch August 9, 2019 17:19
@wdoug
Copy link

wdoug commented Sep 18, 2019

I see that the process for getting an RCF approved here is entirely independent of the actual implementation. @ilyavolodin, are you planning on working on the implementation of this feature, or, if not, do you know if anyone is planning on working on it?

@gaearon
Copy link

gaearon commented Sep 24, 2019

I don't think anyone is working on it. Would you like to? It would be an amazing contribution.

@ilyavolodin
Copy link
Member Author

@wdoug Unfortunately, I don't have time to work on this right now. So if you would like to give it a try, that would be great!

@wdoug
Copy link

wdoug commented Oct 3, 2019

I would like to help if I can. I'll see if I can get familiar enough with the codebase to get a pull request up in my free time. If you end up having time to work on it before I get something up @ilyavolodin just let me know.

@gaearon
Copy link

gaearon commented Oct 3, 2019

@wdoug Thanks for looking into it!

@wdoug
Copy link

wdoug commented Oct 7, 2019

@ilyavolodin, if you get a chance to check it out, I opened a PR for this here that could use some feedback and verification that I understood the RFC correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Final Commenting This RFC is in the final week of commenting
Projects
None yet
6 participants