aarona
aarona

Reputation: 37303

RuboCop - Disabling Style/AndOr for my entire Rails project?

I'm playing around with Rubocop and there are some conventions I use that I'd prefer over the standard Rubocop follow. Consider the following:

if some_variable and another
  do_this
end

Rubocop complains that I should be using && instead of and here. I checked the documentation and the default setting for Style/AndOr is always. However, it does have setting (conditionals) that allows me to do this:

model.save! and return

...but I'd rather just turn this standard completely off for my Rails project. Rubocop does let me do this per instance like this:

# rubocop:disable Style/AndOr
if check_this and check_that
  # rubocop:enable Style/AndOr

  do_something
end

...but that seems really tedious so I'd prefer it if I could disable Style/AndOr altogether. Is this possible and if so how would I go about doing this?

Upvotes: 1

Views: 579

Answers (2)

David Bodow
David Bodow

Reputation: 717

Using the accepted answer from @Casper above you can disable any Rubocop style guideline you desire, but I'd also throw in the lateral thinking option of using Rubocop to gradually transition style in your existing code. Note that the reason && / || are defaults is because they are higher precedence than =, which makes most code more intuitive; in some sense, you might consider this a code smell prompting a change in your style guidelines.

If you're interested in migrating style, consider setting up your CI to lint only touched files; this way, you can avoid the headache of a full codebase lint and just do it on files you've touched. Then, you can use Rubocop's autocorrect to quickly fix most problems: bundle exec rake rubocop -a.

Upvotes: 2

Casper
Casper

Reputation: 34308

Create a dotfile named .rubocop.yml (note the dot):

Style/AndOr:
  Enabled: false

See more information here:
https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md

Upvotes: 6

Related Questions