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

Always correct binding to multiline #78

Merged
merged 3 commits into from
Oct 15, 2021

Conversation

vinistock
Copy link
Member

@vinistock vinistock commented Oct 5, 2021

Trying to auto-correct callback conditionals can produce incorrect code if the method used inside is private. E.g.:

class Article
  # If we simply add `T.bind(self, Article).valid?` here, we get an error because valid? is private
  before_create :do_something, if: -> { valid? }

  private

  def valid?
    true
  end
end

Therefore, the only way of safely making these corrections is by always correcting to multi-line.

I also had to fix a documentation example which was breaking rake, so that I could re-generate the documentation.

Note
We had discussed changing the purpose of this cop to add the T.bind into any arbitrary method that we could specify via configuration. However, the place we need to insert the T.bind is different for certain types of methods.

E.g.:

# in this case, the block is the actual block argument
setup do
  T.bind(self, MyTest)
end

# in this case, the block is captured inside a hash options parameter and the actual block is a completely different argument
before_action if: -> { should_do_it? } do
end

If you have any ideas of how we could make this configurable and generic, please let me know. But my suggestion would be to have a different cop for regular blocks and this one remains for callbacks only.

@vinistock vinistock requested a review from a team October 5, 2021 20:56
@vinistock vinistock merged commit 076602f into master Oct 15, 2021
@vinistock vinistock deleted the always_correct_binding_to_multiline branch October 15, 2021 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants