rubocop-rspec icon indicating copy to clipboard operation
rubocop-rspec copied to clipboard

Cop idea: prefer no parentheses: expect(subject).to eq(true) not expect(subject).to(eq(true))

Open hubertjakubiak opened this issue 5 years ago • 3 comments

Idea for cop suggestion:

Prefer expect(subject).to eq(true) not expect(subject).to(eq(true)). The same when not_to so expect(subject).not_to eq(true) not expect(subject).not_to(eq(true)).

There's no single example where second approach is being used in https://github.com/rubocop-hq/rspec-style-guide.

hubertjakubiak avatar Apr 17 '19 11:04 hubertjakubiak

Great idea.

There's this in the Ruby Style Guide:

Always omit parentheses for Methods that are part of an internal DSL (e.g., Rake, Rails, RSpec): validates :name, presence: true

Maybe add a cop here, and eventually a generic part if it to RuboCop, so that rubocop-rails could benefit from it as well, by marking validates and other Rails-specific DSL methods as such to be avoided.

Can't think of legit exceptions to this rule, e.g. when parenthesis would make sense.

pirj avatar May 09 '19 20:05 pirj

Can't think of legit exceptions to this rule, e.g. when parenthesis would make sense.

We just have to be careful of to in other contexts, notably the second to with this change matcher

expect { man.you_cant_handle_the_truth! }.to change { man.the_truth? }.from(true).to(false)

dgollahon avatar Jun 18 '19 06:06 dgollahon

I actually fixed that with below custom config:

Style/MethodCallWithArgsParentheses:
  Enabled: true
  IgnoredMethods:
    # long list of other methods that are also better without parentheses
    - to
    - not_to

hubertjakubiak avatar Nov 12 '21 11:11 hubertjakubiak