From a688ca2c42d6508314ec981b00ec6ffdbea9216f Mon Sep 17 00:00:00 2001 From: Masataka Pocke Kuwabara Date: Sat, 26 Jan 2019 14:28:03 +0900 Subject: [PATCH] [Fix #6710] Fix `Naming/MemoizedInstanceVariableName` on method starts with underscore --- CHANGELOG.md | 1 + .../naming/memoized_instance_variable_name.rb | 17 ++++++++++------- .../memoized_instance_variable_name_spec.rb | 1 - 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 365261eec20..fd808261ac3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * [#6689](https://github.com/rubocop-hq/rubocop/pull/6689): Support more complex argument patterns on `Rails/Validation` auto-correction. ([@r7kamura][]) * [#6668](https://github.com/rubocop-hq/rubocop/issues/6668): Fix autocorrection for `Style/UnneededCondition` when conditional has the `unless` form. ([@mvz][]) * [#6382](https://github.com/rubocop-hq/rubocop/issues/6382): Fix `Layout/IndentationWidth` with `Layout/EndAlignment` set to start_of_line. ([@dischorde][], [@siegfault][], [@mhelmetag][]) +* [#6710](https://github.com/rubocop-hq/rubocop/issues/6710): Fix `Naming/MemoizedInstanceVariableName` on method starts with underscore. ([@pocke][]) ## 0.63.1 (2019-01-22) diff --git a/lib/rubocop/cop/naming/memoized_instance_variable_name.rb b/lib/rubocop/cop/naming/memoized_instance_variable_name.rb index e4a145e01c4..2b69fb96f58 100644 --- a/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +++ b/lib/rubocop/cop/naming/memoized_instance_variable_name.rb @@ -132,9 +132,7 @@ def matches?(method_name, ivar_assign) variable = ivar_assign.children.first variable_name = variable.to_s.sub('@', '') - return false unless valid_leading_underscore?(variable_name) - - variable_name.sub(/\A_/, '') == method_name.sub(/\A_/, '') + variable_name_candidates(method_name).include?(variable_name) end def message(variable) @@ -152,14 +150,19 @@ def suggested_var(method_name) style == :required ? "_#{suggestion}" : suggestion end - def valid_leading_underscore?(variable_name) + def variable_name_candidates(method_name) + no_underscore = method_name.sub(/\A_/, '') + with_underscore = "_#{method_name}" case style when :required - variable_name.start_with?('_') + [with_underscore, + method_name.start_with?('_') ? method_name : nil].compact when :disallowed - !variable_name.start_with?('_') + [method_name, no_underscore] + when :optional + [method_name, with_underscore, no_underscore] else - true + raise 'Unreachable' end end end 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 2b6f1cb5871..f1c3c6f748c 100644 --- a/spec/rubocop/cop/naming/memoized_instance_variable_name_spec.rb +++ b/spec/rubocop/cop/naming/memoized_instance_variable_name_spec.rb @@ -104,7 +104,6 @@ def _foo end it 'does not register an offense with a leading `_` for both names' do - pending expect_no_offenses(<<-RUBY.strip_indent) def _foo @_foo ||= :foo