Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #6710] Fix Naming/MemoizedInstanceVariableName on method starts with underscore #6711

Merged
merged 1 commit into from Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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