Skip to content

Commit

Permalink
[Fix rubocop#5670] Suggest valid memoized instance variable for bang …
Browse files Browse the repository at this point in the history
…method

See rubocop#5670 and rubocop#5648
  • Loading branch information
pocke committed Mar 14, 2018
1 parent 841569b commit d09e400
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

* [#5642](https://github.com/bbatsov/rubocop/pull/5642): Fix Style/Documentation :nodoc: for compact-style nested modules/classes. ([@ojab][])
* [#5648](https://github.com/bbatsov/rubocop/issues/5648): Suggest valid memoized instance variable for predicate method. ([@satyap][])
* [#5670](https://github.com/bbatsov/rubocop/issues/5670): Suggest valid memoized instance variable for bang method. ([@pocke][])
* [#5623](https://github.com/bbatsov/rubocop/pull/5623): Fix `Bundler/OrderedGems` when a group includes duplicate gems. ([@colorbox][])
* [#5633](https://github.com/bbatsov/rubocop/pull/5633): Fix broken `--fail-fast`. ([@mmyoji][])
* [#5630](https://github.com/bbatsov/rubocop/issues/5630): Fix false positive for `Style/FormatStringToken` when using placeholder arguments in `format` method. ([@koic][])
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/naming/memoized_instance_variable_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def on_def(node)
msg = format(
MSG,
var: ivar_assign.children.first.to_s,
suggested_var: method_name.to_s.chomp('?'),
suggested_var: method_name.to_s.delete('!?'),
method: method_name
)
add_offense(node, location: ivar_assign.source_range, message: msg)
Expand All @@ -66,7 +66,7 @@ def on_def(node)

def matches?(method_name, ivar_assign)
return true if ivar_assign.nil? || method_name == :initialize
method_name = method_name.to_s.sub('?', '')
method_name = method_name.to_s.delete('!?')
variable = ivar_assign.children.first
variable_name = variable.to_s.sub('@', '')
variable_name == method_name
Expand Down
24 changes: 21 additions & 3 deletions spec/rubocop/cop/naming/memoized_instance_variable_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ def foo
end
RUBY
end
end

context 'memoized variable after other code does not match method name' do
it 'registers an offense' do
it 'registers an offense for a predicate method' do
expect_offense(<<-RUBY.strip_indent)
def foo?
helper_variable = something_we_need_to_calculate_foo
Expand All @@ -71,6 +69,16 @@ def foo?
end
RUBY
end

it 'registers an offense for a bang method' do
expect_offense(<<-RUBY.strip_indent)
def foo!
helper_variable = something_we_need_to_calculate_foo
@bar ||= calculate_expensive_thing(helper_variable)
^^^^ Memoized variable `@bar` does not match method name `foo!`. Use `@foo` instead.
end
RUBY
end
end

context 'memoized variable matches method name' do
Expand Down Expand Up @@ -124,6 +132,16 @@ def a?
end
end

context 'memoized variable matches bang method name' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
def a!
@a ||= :foo
end
RUBY
end
end

context 'code follows memoized variable assignment' do
it 'does not register an offense' do
expect_no_offenses(<<-RUBY.strip_indent)
Expand Down

0 comments on commit d09e400

Please sign in to comment.