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

Don't define #autocorrect in mixins #3558

Closed
Drenmi opened this issue Sep 30, 2016 · 5 comments
Closed

Don't define #autocorrect in mixins #3558

Drenmi opened this issue Sep 30, 2016 · 5 comments

Comments

@Drenmi
Copy link
Collaborator

Drenmi commented Sep 30, 2016

I want to argue that we shouldn't define the #autocorrect method in mixins used by cops.

What's wrong with it?

  1. If I want to use a mixin in a cop that shouldn't implement auto-correct, I need to add a no-op #autocorrect definition to the cop. This is pretty icky.
  2. If I want to use two mixins, both implementing #autocorrect in a cop, which method is finally defined depends on the order of the mixins. This is very fragile.
  3. There might be more than one #autocorrect that makes sense for a mixin, depending on the context it is being used.
  4. There is no easy way to see if, and if so which, auto-correct is defined from within a Cop body.

What should we do instead?

We already have a suitable abstraction in the callable objects that #autocorrect return. A mixin could simply make any number of correctors available for the Cop to return from it's own #autocorrect method.

Another option could be to have the mixin provide lower level methods that the cop can use to assemble it's own #autocorrect. The Parentheses mixin could provide #remove_parentheses and #add_parentheses, for example.

Which mixins are currently implementing it?

  • AutocorrectAlignment
  • EmptyLinesAroundBody
  • FirstElementLineBreak
  • MultilineLiteralBraceLayout
  • Parentheses
  • SpaceAfterPunctuation
  • SpaceBeforePunctuation
  • SpaceInside
  • StringLiteralsHelp
  • TrailingComma
  • UnusedArgument
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 30, 2016

I agree. We should change this.

On Friday, 30 September 2016, Ted Johansson notifications@github.com
wrote:

I want to argue that we shouldn't define the #autocorrect method in
mixins used by cops.
What's wrong with it?

If I want to use a mixin in a cop that shouldn't implement
auto-correct, I need to add a no-op #autocorrect definition to the
cop. This is pretty icky.
2.

If I want to use two mixins, both implementing #autocorrect in a cop,
which method is finally defined depends on the order of the mixins. This is
very fragile.
3.

There might be more than one #autocorrect that makes sense for a
mixin, depending on the context it is being used.
4.

There is no easy way to see if, and if so which, auto-correct is
defined from within a Cop body.

What should we do instead?

We already have a suitable abstraction in the callable objects that
#autocorrect return. A mixin could simply make any number of correctors
available for the Cop to return from it's own #autocorrect method.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#3558, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGVyuByzs6j-wdGBdU-95dGTofJ-wycks5qvSJggaJpZM4KLLE9
.

@garettarrowood
Copy link
Contributor

garettarrowood commented Dec 13, 2017

I'm a fan of refactoring and these are great points. After working on bug reports from the new release, I'd be happy to tackle this.

@Drenmi
Copy link
Collaborator Author

Drenmi commented Dec 14, 2017

@garettarrowood: I've been thinking about this quite a bit. Since the #autocorrect method just needs to return an object that responds to #call, it is simple to extract common correctors into classes, and keep them in their own directory.

@garettarrowood
Copy link
Contributor

garettarrowood commented Dec 14, 2017

So, if I understand correctly, this would mean a new set of modules called correctors. All "corrector" methods/helpers would be moved out of mixins/* and into something like cop/correctors/space_correctors.rb. I'd be in great favor of a filename/classname convention with these so it is clear inside the cop class. This would be similar to Rails conventions for helpers, presenters, jobs, controllers, etc. filenames.

Example:

class SpaceInsideArrayLiteralBrackets < Cop
  include SurroundingSpace
  include SpaceCorrectors
  include ConfigurableEnforcedStyle

  ...
end

Is this what you had in mind?

@Drenmi
Copy link
Collaborator Author

Drenmi commented Dec 14, 2017

@garettarrowood: Pretty much. Except I see no reason to mix the corrector methods into the cop. The Cop class is already pretty overloaded, and correction is a concern that can be delegated to the corrector. I.e. composition over inheritance. 🙂

garettarrowood added a commit to garettarrowood/rubocop that referenced this issue Dec 17, 2017
garettarrowood added a commit to garettarrowood/rubocop that referenced this issue Dec 17, 2017
garettarrowood added a commit to garettarrowood/rubocop that referenced this issue Dec 17, 2017
garettarrowood added a commit to garettarrowood/rubocop that referenced this issue Dec 17, 2017
garettarrowood added a commit to garettarrowood/rubocop that referenced this issue Dec 17, 2017
garettarrowood added a commit to garettarrowood/rubocop that referenced this issue Dec 17, 2017
garettarrowood added a commit to garettarrowood/rubocop that referenced this issue Dec 17, 2017
garettarrowood added a commit to garettarrowood/rubocop that referenced this issue Dec 17, 2017
garettarrowood added a commit to garettarrowood/rubocop that referenced this issue Dec 17, 2017
garettarrowood added a commit to garettarrowood/rubocop that referenced this issue Dec 17, 2017
garettarrowood added a commit to garettarrowood/rubocop that referenced this issue Dec 17, 2017
garettarrowood added a commit to garettarrowood/rubocop that referenced this issue Dec 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants