Skip to content

Commit

Permalink
Merge pull request #986 from rubocop-hq/variable-definition-and-inter…
Browse files Browse the repository at this point in the history
…polated-strings

Allow dstr even when `symbol` style is configured
  • Loading branch information
pirj committed Aug 3, 2020
2 parents a1bafa7 + 2f7b2b2 commit 495ec0e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Add a new base cop class `::RuboCop::Cop::RSpec::Base`. The old base class `::RuboCop::Cop::RSpec::Cop` is deprecated, and will be removed in the next major release. ([@bquorning][])
* Add support for subject detection after includes and example groups in `RSpec/LeadingSubject`. ([@pirj][])
* Ignore trailing punctuation in context description prefix. ([@elliterate][])

* Relax `RSpec/VariableDefinition` cop so interpolated and multiline strings are accepted even when configured to enforce the `symbol` style. ([@bquorning][])
* Fix `RSpec/EmptyExampleGroup` to flag example groups with examples in invalid scopes. ([@mlarraz][])
* Fix `RSpec/EmptyExampleGroup` to ignore examples groups with examples defined inside iterators. ([@pirj][])

Expand Down
10 changes: 5 additions & 5 deletions lib/rubocop/cop/rspec/variable_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ module RSpec
#
# @example EnforcedStyle: symbols (default)
# # bad
# let('user_name') { 'Adam' }
# subject('user') { create_user }
# let('user_name') { 'Adam' }
#
# # good
# let(:user_name) { 'Adam' }
# subject(:user) { create_user }
# let(:user_name) { 'Adam' }
#
# @example EnforcedStyle: strings
# # bad
# let(:user_name) { 'Adam' }
# subject(:user) { create_user }
# let(:user_name) { 'Adam' }
#
# # good
# let('user_name') { 'Adam' }
# subject('user') { create_user }
# let('user_name') { 'Adam' }
class VariableDefinition < Base
include ConfigurableEnforcedStyle
include RuboCop::RSpec::Variable
Expand All @@ -44,7 +44,7 @@ def style_violation?(variable)
end

def string?(node)
node.str_type? || node.dstr_type?
node.str_type?
end

def symbol?(node)
Expand Down
8 changes: 4 additions & 4 deletions manual/cops_rspec.md
Original file line number Diff line number Diff line change
Expand Up @@ -3042,23 +3042,23 @@ Checks that memoized helpers names are symbols or strings.

```ruby
# bad
let('user_name') { 'Adam' }
subject('user') { create_user }
let('user_name') { 'Adam' }

# good
let(:user_name) { 'Adam' }
subject(:user) { create_user }
let(:user_name) { 'Adam' }
```
#### EnforcedStyle: strings

```ruby
# bad
let(:user_name) { 'Adam' }
subject(:user) { create_user }
let(:user_name) { 'Adam' }

# good
let('user_name') { 'Adam' }
subject('user') { create_user }
let('user_name') { 'Adam' }
```

### Configurable attributes
Expand Down
12 changes: 5 additions & 7 deletions spec/rubocop/cop/rspec/variable_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@
RUBY
end

it 'registers an offense for interpolated string' do
expect_offense(<<~'RUBY')
it 'does not register offense for interpolated string' do
expect_no_offenses(<<~'RUBY')
let("user-#{id}") { 'Adam' }
^^^^^^^^^^^^ Use symbols for variable names.
RUBY
end

it 'registers an offense for multiline string' do
expect_offense(<<~'RUBY')
let("user"\
^^^^^^^ Use symbols for variable names.
it 'does not register offense for multiline string' do
expect_no_offenses(<<~'RUBY')
let("user" \
"-foo") { 'Adam' }
RUBY
end
Expand Down

0 comments on commit 495ec0e

Please sign in to comment.