Skip to content

Commit

Permalink
[Fix rubocop#6710] Fix Naming/MemoizedInstanceVariableName on metho…
Browse files Browse the repository at this point in the history
…d starts with underscore
  • Loading branch information
pocke committed Jan 26, 2019
1 parent 002aef5 commit a688ca2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down
17 changes: 10 additions & 7 deletions lib/rubocop/cop/naming/memoized_instance_variable_name.rb
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit a688ca2

Please sign in to comment.