Skip to content

Commit

Permalink
Merge pull request #1569 from ydah/fix/1149
Browse files Browse the repository at this point in the history
 Fix a false positive for `RSpec/VariableName` and `RSpec/VariableDefinition` when inside non-spec code
  • Loading branch information
bquorning authored Jan 23, 2023
2 parents 547bdd7 + 0ed8589 commit 0868629
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 60 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Fix a false positive for `RSpec/ContextWording` when context is interpolated string literal or execute string. ([@ydah])
- Fix a false positive for `RSpec/DescribeMethod` when multi-line describe without `#` and `.` at the beginning. ([@ydah], [@pirj])
- Fix a false positive for `RSpec/VariableName` when inside non-spec code. ([@ydah])
- Fix a false positive for `RSpec/VariableDefinition` when inside non-spec code. ([@ydah])
- Add new `RSpec/PendingBlockInsideExample` cop. ([@ydah])

## 2.18.1 (2023-01-19)
Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/cop/rspec/variable_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ class VariableDefinition < Base
extend AutoCorrector
include ConfigurableEnforcedStyle
include Variable
include InsideExampleGroup

MSG = 'Use %<style>s for variable names.'

def on_send(node)
return unless inside_example_group?(node)

variable_definition?(node) do |variable|
next unless style_violation?(variable)

Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/cop/rspec/variable_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ class VariableName < Base
include ConfigurableNaming
include AllowedPattern
include Variable
include InsideExampleGroup

MSG = 'Use %<style>s for variable names.'

def on_send(node)
return unless inside_example_group?(node)

variable_definition?(node) do |variable|
return if variable.dstr_type? || variable.dsym_type?
return if matches_allowed_pattern?(variable.value)
Expand Down
60 changes: 46 additions & 14 deletions spec/rubocop/cop/rspec/variable_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,47 @@

it 'registers an offense for string name' do
expect_offense(<<~RUBY)
let('user_name') { 'Adam' }
^^^^^^^^^^^ Use symbols for variable names.
RSpec.describe Foo do
let('user_name') { 'Adam' }
^^^^^^^^^^^ Use symbols for variable names.
end
RUBY

expect_correction(<<~RUBY)
let(:user_name) { 'Adam' }
RSpec.describe Foo do
let(:user_name) { 'Adam' }
end
RUBY
end

it 'does not register offense for interpolated string' do
expect_no_offenses(<<~'RUBY')
let("user-#{id}") { 'Adam' }
RSpec.describe Foo do
let("user-#{id}") { 'Adam' }
end
RUBY
end

it 'does not register offense for multiline string' do
expect_no_offenses(<<~'RUBY')
let("user" \
"-foo") { 'Adam' }
RSpec.describe Foo do
let("user" \
"-foo") { 'Adam' }
end
RUBY
end

it 'does not register offense for symbol names' do
expect_no_offenses(<<~RUBY)
let(:user_name) { 'Adam' }
RSpec.describe Foo do
let(:user_name) { 'Adam' }
end
RUBY
end

it 'does not register offense when not inside spec group' do
expect_no_offenses(<<~RUBY)
let('user_name') { 'Adam' }
RUBY
end
end
Expand All @@ -40,29 +56,45 @@

it 'registers an offense for symbol name' do
expect_offense(<<~RUBY)
let(:user_name) { 'Adam' }
^^^^^^^^^^ Use strings for variable names.
RSpec.describe Foo do
let(:user_name) { 'Adam' }
^^^^^^^^^^ Use strings for variable names.
end
RUBY

expect_correction(<<~RUBY)
let("user_name") { 'Adam' }
RSpec.describe Foo do
let("user_name") { 'Adam' }
end
RUBY
end

it 'registers an offense for interpolated symbol' do
expect_offense(<<~'RUBY')
let(:"user-#{id}") { 'Adam' }
^^^^^^^^^^^^^ Use strings for variable names.
RSpec.describe Foo do
let(:"user-#{id}") { 'Adam' }
^^^^^^^^^^^^^ Use strings for variable names.
end
RUBY

expect_correction(<<~'RUBY')
let("user-#{id}") { 'Adam' }
RSpec.describe Foo do
let("user-#{id}") { 'Adam' }
end
RUBY
end

it 'does not register offense for string names' do
expect_no_offenses(<<~RUBY)
let('user_name') { 'Adam' }
RSpec.describe Foo do
let('user_name') { 'Adam' }
end
RUBY
end

it 'does not register offense when not inside spec group' do
expect_no_offenses(<<~RUBY)
let(:user_name) { 'Adam' }
RUBY
end
end
Expand Down
Loading

0 comments on commit 0868629

Please sign in to comment.