Skip to content

Commit

Permalink
New: Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyavolodin committed Jul 11, 2019
1 parent f1fe954 commit 7c598c2
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions designs/2019-suggestions/README.md
@@ -0,0 +1,59 @@
- Start Date: 2019-07-04
- RFC PR: https://github.com/eslint/rfcs/pull/30
- Authors: Ilya Volodin, Dan Abramov

# Code Suggestions

## 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.

## Motivation

This has been suggested multiple times before, but the main driving force behind this proposal is ability to provide user with options on how to automatically refactor/fix their code, without a possibility of "stealthily" breaking it. Examples of such suggestions include ability to suggest adding `import` statement if certain code is added to a file by the user, ability to provide refactoring suggestions like extracting repeated value into a variable, reformatting code to extract repeated statements into reusable function, etc. I do not envision ESLint team to initially create any rules that would provide suggestions, so this would be a feature that would enable plugin authors to do so, but it might be something that ESLint team will eventually do. This feature is geared towards code editor integrations, as such, it will only be available via NodeJS API.

## Detailed Design

* Modify `context.report` function parameter to include new property called `suggest` of type array of functions. Each function will be able to modify the code of the file to implement suggestion. Syntax is going to be exactly the same as for `fix` functions. Example of new signature below:
```js
context.report({
node,
loc,
messageId: 'messageId',
fix(fixer) { },
suggest: [
(fixer) => { },
(fixer) => { }
]
});
```
* Change `_verifyWithoutProcessors` and `_verifyWithProcessor` functions in linter.js to modify output of those functions to include `suggestions` array returned by the rule.
* Modify output of `CLIEngine` `executeOnFiles` and `executeOnText` to include `suggestions` array returned by the rule.
* Introduce a new `CLIEngine` public function `outputSuggestion(errorReport, suggestion)` that would apply selected suggestion (second argument) to the file that the errorReport was created for and save it to disk.

## Documentation

This would need to be documented as part of the public API.
* [Node.js API](https://eslint.org/docs/developer-guide/nodejs-api) page needs to be updated with corresponding changes to `linter` and `CLIEngine`.
* [Working with Rules](https://eslint.org/docs/developer-guide/working-with-rules) page needs to be updated to show examples of returning suggestions.

## Drawbacks

This might potentially be a bit confusing, since we do not have many functions that we provide through API only and not through CLI.

## Backwards Compatibility Analysis

Since `suggestions` property in `context.report` object is optional, this should be fully backwards compatible.

## Alternatives

This functionality is basically the same as existing `autofix` functionality already included in ESLint. The only difference is there might be multiple suggestions per error and they are not applied automatically by ESLint.

## Open Questions

Should we consider instead providing new type of `rules` that would not report any issues, but only provide suggestions?

## Related Discussions

https://github.com/eslint/eslint/issues/7873
https://github.com/eslint/eslint/issues/6733#issuecomment-234656842

0 comments on commit 7c598c2

Please sign in to comment.