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

`RSpec/VariableName` & `RSpec/VariableDefinition` complain on non-rspec `subject` method

Open ojab opened this issue 4 years ago • 6 comments

# `email_spec.rb` or whatever
Mail.new do
  subject 'This is a test email'
end

which is mail gem DSL for creating new emails, leads to

$ rubocop -rrubocop-rspec -V
1.14.0 (using Parser 3.0.1.1, rubocop-ast 1.5.0, running on ruby 3.0.0 x86_64-linux)
  - rubocop-rspec 2.2.0
$ rubocop -rrubocop-rspec email_spec.rb
Inspecting 1 file
C

Offenses:

email_spec.rb:3:11: C: RSpec/VariableDefinition: Use symbols for variable names.
  subject 'This is a test email'
          ^^^^^^^^^^^^^^^^^^^^^^
email_spec.rb:3:11: C: RSpec/VariableName: Use snake_case for variable names.
  subject 'This is a test email'
          ^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 2 offenses detected

ojab avatar May 09 '21 10:05 ojab

Hey @ojab !

Indeed, this is problematic. mail testing doc. I'm thinking if limiting those cops inspection to only subject that is a defied directly in an example group block would result in false positives in some other DSL. I believe your current project is rich in examples of various RSpec DSLs. What do you think?

pirj avatar May 09 '21 18:05 pirj

This one i a new project, so not many specs yet, but you know that there is more projects here :)

I guess restricting subject checks to the example groups makes sense.

ojab avatar May 10 '21 13:05 ojab

I think we can lower the false positives by:

  • limiting the check to block and blockpass nodes

and/or

  • ignoring rspec DSL inside class/module/struct .new blocks (we can go for the extra mile to check if the defined class icludes the RSpec DSL, but that would be too edgy)

Darhazer avatar Aug 25 '21 09:08 Darhazer

limiting the check to block and blockpass nodes

Brilliant idea, that should do it. I couldn't quickly find if mail supports subject with a block, apparently it doesn't.

pirj avatar Aug 25 '21 13:08 pirj

I couldn't quickly find if mail supports subject with a block, apparently it doesn't.

It does, but only with explicit receiver, so should be fine

> Mail.new { |mail| mail.subject = 'foo' }.subject
=> "foo"

ojab avatar Aug 25 '21 14:08 ojab

@ojab Would you like to send a PR to fix this? #1224 might give some hints for the change.

pirj avatar Jun 16 '22 23:06 pirj

@ojab Ping

pirj avatar Oct 15 '22 22:10 pirj

I'd like to, but lack of capacity :crying_cat_face:

ojab avatar Oct 27 '22 19:10 ojab