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 cops with Rails compatibility issue dependant on Rails cops being disabled #2862

Merged
merged 1 commit into from
Mar 22, 2016

Conversation

rrosenblum
Copy link
Contributor

It has been a while since this has come up, but it is still a known issue. Since we cannot determine if the method comes from Ruby or Rails, the next best thing is to determine if Rails is going to be in used and not run these cops if it is. Short of scanning the Gemfile for a rails entry, this seemed like the easiest and most self contained approach. If anyone has an alternative idea, I am open to suggestions.

@rrosenblum rrosenblum force-pushed the disable_rails_conflicting_cops branch 2 times, most recently from 436bf95 to fbdae39 Compare February 18, 2016 14:21
@@ -61,6 +78,14 @@ def autocorrect(node)

private

def should_run?
if config[RAILS] && config[RAILS][ENABLED]
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about the -R/--rails command line option?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had initially forgotten about the command line flags. It turns out that I will not have to do anything special to handle the command line flags. Runner#inspect_file calls enable_rails_cops if one of the command line flags is passed in and sets the config['Rails']['Enabled'] flag.

def inspect_file(processed_source)
  config = @config_store.for(processed_source.path)
  enable_rails_cops(config) if @options[:rails]
  team = Cop::Team.new(mobilized_cop_classes(config), config, @options)
  offenses = team.inspect_file(processed_source)
  @errors.concat(team.errors)
  @warnings.concat(team.warnings)
  [offenses, team.updated_source_file?]
end

def enable_rails_cops(config)
  config['Rails'] ||= {}
  config['Rails']['Enabled'] = true
end

@alexdowad
Copy link
Contributor

It's been stated many times that we want to remove the Rails cops from RC and make them a separate gem. What will happen to this code when that is done?

@rrosenblum rrosenblum force-pushed the disable_rails_conflicting_cops branch from fbdae39 to f85baea Compare February 24, 2016 02:40
@rrosenblum
Copy link
Contributor Author

@alexdowad, I think that this will work just fine when the Rails cops are moved out to a different gem. Both gems would make use of the .rubocop.yml configuration. RuboCop can read any arbitrary configuration that exists in that file. The command line options may not work as smoothly, but I am not sure. Worst case scenario, we can change the check to look for if rubocop-rails is required or something similar.

@rrosenblum rrosenblum force-pushed the disable_rails_conflicting_cops branch 2 times, most recently from bf4d9c0 to 648d0e5 Compare February 25, 2016 13:35
@rrosenblum rrosenblum force-pushed the disable_rails_conflicting_cops branch from 648d0e5 to 22b3701 Compare March 5, 2016 06:45
@bbatsov
Copy link
Collaborator

bbatsov commented Mar 5, 2016

Btw, you can be using ActiveSupport or similar gems even without Rails, so while this might solve some cases of the problem, it definitely won't solve all of them.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 5, 2016

But my real concern is having something Rails-specific in the code, when long-term I want to move the Rails functionality out of it.

@rrosenblum
Copy link
Contributor Author

Btw, you can be using ActiveSupport or similar gems even without Rails

I am aware that this wouldn't solve all cases, but the hope was that it would at least help some.

I can understand not wanting to add Rails specific code to base project. Once the Rails cops are refactored out, we should be able to disable the cops or the auto-correct from inside that project.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 19, 2016

OK, I guess it's ok to add something like this for the time being. Still, I'd like for the comments and descriptions to be more specific, as ActiveSupport/Record are just two pieces of Rails.

@@ -1173,6 +1173,11 @@ Performance/Count:
Use `count` instead of `select...size`, `reject...size`,
`select...count`, `reject...count`, `select...length`,
and `reject...length`.
# This cop has known compatibility issues with Rails. Rails' `count` ignores
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here there should be a reference to ActiveRecord.

@rrosenblum
Copy link
Contributor Author

I'd like for the comments and descriptions to be more specific, as ActiveSupport/Record are just two pieces of Rails.

Will do. I left it generically to Rails initially because the same distinction isn't make for Rails cops.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 19, 2016

Will do. I left it generically to Rails initially because the same distinction isn't make for Rails cops.
Show all checks

There's definitely room for improvement regarding their names. :-)

@rrosenblum rrosenblum force-pushed the disable_rails_conflicting_cops branch 2 times, most recently from 69fd6a5 to 6578e62 Compare March 20, 2016 15:41
@@ -75,6 +75,10 @@
* [#2923](https://github.com/bbatsov/rubocop/issues/2923): `Style/FileName` considers file names which contain a ? or ! character to still be "snake case". ([@alexdowad][])
* [#2879](https://github.com/bbatsov/rubocop/issues/2879): When autocorrecting, `Lint/UnusedMethodArgument` removes unused block arguments rather than simply prefixing them with an underscore. ([@alexdowad][])

### Changes
Copy link
Collaborator

Choose a reason for hiding this comment

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

This section isn't at the right location.

@rrosenblum rrosenblum force-pushed the disable_rails_conflicting_cops branch from 6578e62 to 934df83 Compare March 22, 2016 21:48
@rrosenblum
Copy link
Contributor Author

Updated and rebased.

bbatsov added a commit that referenced this pull request Mar 22, 2016
Make cops with Rails compatibility issue dependant on Rails cops being disabled
@bbatsov bbatsov merged commit 674dbd1 into rubocop:master Mar 22, 2016
@rrosenblum rrosenblum deleted the disable_rails_conflicting_cops branch March 23, 2016 15:14
@sferik
Copy link
Contributor

sferik commented Mar 28, 2016

For what it’s worth, I’ve submitted a patch to Rails that would make auto-correcting the Performance/Count cop safe again. What is the issue with Performance/Detect? Could we do the same thing there?

@rrosenblum
Copy link
Contributor Author

@sferik thanks for putting in a PR to Rails. That has been on my radar for a while. It has been a little while since I worked with Performance/Detect, if I remember correctly, Rails does not implement detect. Similar to count, I also don't think that ActiveRecord#find is implemented to take a block.

@sferik
Copy link
Contributor

sferik commented Mar 29, 2016

@rrosenblum By my reading of the code, ActiveRecord::Relation#find does indeed take a block and has since Rails 3.0.0.

@rrosenblum
Copy link
Contributor Author

I remembered the issue. ActiveRecord partially supports find with a block.

User.where(username: 'username').select { |u| u.active }.first can be converted to User.where(username: 'username').find { |u| u.active }. Unfortunately, User.select { |u| u.username == 'username' }.first cannot be converted to User.find { |u| u.username == 'username' }. This results in ActiveRecord::RecordNotFound: Couldn't find User without an ID.

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.

6 participants