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 #6349] MemoizedInstanceVariableName allows underscored method name #6587

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/rubocop/cop/naming/memoized_instance_variable_name.rb
Original file line number Diff line number Diff line change
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_/, '')
valid_variable_name?(method_name, variable_name)
end

def message(variable)
Expand All @@ -152,14 +150,15 @@ def suggested_var(method_name)
style == :required ? "_#{suggestion}" : suggestion
end

def valid_leading_underscore?(variable_name)
def valid_variable_name?(method_name, variable_name)
clean_method_name = method_name.sub(/\A_/, '')
case style
when :required
variable_name.start_with?('_')
variable_name == '_' + clean_method_name
when :disallowed
!variable_name.start_with?('_')
[clean_method_name, method_name].include?(variable_name)
else
true
variable_name.sub(/\A_/, '') == clean_method_name
end
end
end
Expand Down
Original file line number Diff line number Diff line change
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