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

Make Style/RedundantSelf aware of arguments of a block #4600

Merged
merged 1 commit into from
Aug 9, 2017

Conversation

Envek
Copy link
Contributor

@Envek Envek commented Jul 12, 2017

Currently, Rubocop complains about redundant self when using a block with argument name same with receiver method, like this:

  %w[draft preview moderation approved rejected].each do |state|
    define_method "#{state}?" do
      self.state == state
    end
  end

Autocorrection fixes this code to state == state effectively breaking it.

This PR removes offense for arguments of a block with names equal to receiver method name.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists). Not exists
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop). Not required

@Drenmi
Copy link
Collaborator

Drenmi commented Jul 12, 2017

Nice catch, and nice PR. 🙂 Could you please add another example to the documentation and run rake generate_cops_documentation?

@Envek Envek force-pushed the fix-redundant-self-for-blocks branch from f8d03b2 to 0b2c74f Compare July 12, 2017 09:48
@Envek
Copy link
Contributor Author

Envek commented Jul 12, 2017

I found that it doesn't handle nested blocks well (as in the first comment), so I changed implementation (and test) to deal with it.

Added example to documentation.

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 8, 2017

I'm curious how is this different than passing a param from a method? Aren't block params lvars just as well?

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 8, 2017

In particular I see that we already had this in the code:

def on_blockarg(node)
    on_argument(node)
end

@Envek Envek force-pushed the fix-redundant-self-for-blocks branch from 0b2c74f to 43c8c62 Compare August 8, 2017 08:09
@Envek
Copy link
Contributor Author

Envek commented Aug 8, 2017

The main problem is that previous add_scope method cleared local variables for each nested scope that is fine for methods, because they can't be nested, but blocks usually are nested deeply.

Rebased on top of current master.

@bbatsov bbatsov merged commit a61bcd3 into rubocop:master Aug 9, 2017
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