From d09e4008d96e2cea223b2c0f27739d182e9c1067 Mon Sep 17 00:00:00 2001 From: Masataka Pocke Kuwabara Date: Thu, 15 Mar 2018 00:38:01 +0900 Subject: [PATCH] [Fix #5670] Suggest valid memoized instance variable for bang method See #5670 and #5648 --- CHANGELOG.md | 1 + .../naming/memoized_instance_variable_name.rb | 4 ++-- .../memoized_instance_variable_name_spec.rb | 24 ++++++++++++++++--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a319bbce1db5..4cd3eb409c69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) diff --git a/lib/rubocop/cop/naming/memoized_instance_variable_name.rb b/lib/rubocop/cop/naming/memoized_instance_variable_name.rb index 905f64b28e7c..31d6f01ff021 100644 --- a/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +++ b/lib/rubocop/cop/naming/memoized_instance_variable_name.rb @@ -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) @@ -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 diff --git a/spec/rubocop/cop/naming/memoized_instance_variable_name_spec.rb b/spec/rubocop/cop/naming/memoized_instance_variable_name_spec.rb index f6e7c7d55649..839e646877f7 100644 --- a/spec/rubocop/cop/naming/memoized_instance_variable_name_spec.rb +++ b/spec/rubocop/cop/naming/memoized_instance_variable_name_spec.rb @@ -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 @@ -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 @@ -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)