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

Style/VariableName doesn't seem to work with method parameters #2335

Closed
mateusmedeiros opened this issue Oct 21, 2015 · 3 comments
Closed

Style/VariableName doesn't seem to work with method parameters #2335

mateusmedeiros opened this issue Oct 21, 2015 · 3 comments

Comments

@mateusmedeiros
Copy link

I don't know if this is intended behaviour, but if it is, I think it should be changed (unless there's a specific cop for that, in which case I'd really like to know which it is, because I couldn't find any).

If I have the following method in a ruby script:

def downloadAnother(urlFileHash)
  variableWithCamelCase = 2
  urlFileHash.each do |url, file|
    puts "downloading #{url} to #{file}"
  end
end

Rubocop will report 3 offenses from this method:

C: Use snake_case for method names.
def downloadAnother(urlFileHash)
    ^^^^^^^^^^^^^^^
W: Useless assignment to variable - variableWithCamelCase.
  variableWithCamelCase = 2
  ^^^^^^^^^^^^^^^^^^^^^
C: Use snake_case for variable names.
  variableWithCamelCase = 2
  ^^^^^^^^^^^^^^^^^^^^^

But I was expecting Style/VariableName to also detect offenses in parameters names, like this:

C: Use snake_case for variable names.
def downloadAnother(urlFileHash)
                    ^^^^^^^^^^^

This is the original method I had for which rubocop is reporting no offense at all:
def download(urlFileHash)
  urlFileHash.each do |url, file|
    puts "Downloading #{url} to the file #{file}..."
    File.open(file, 'wb') do |saved_file|
      open(url, 'rb') do |read_file|
        saved_file.write(read_file.read)
      end
    end
  end
end

I installed rubocop straight from master and it still reports no offense.
Here is my rubocop -V:

0.34.2 (using Parser 2.2.3.0, running on ruby 2.2.3 x86_64-linux)

Thanks in advance.

@jonas054
Copy link
Collaborator

Makes sense. Let Style/VariableName check parameter names too.

@kjvarga
Copy link

kjvarga commented Aug 9, 2019

This still isn't catching bad variable names in arguments passed to a method, and it doesn't catch bad let variable definitions (thought I think that one is understandable). The problem there is that in a lot of specs we define the variable using let and then it gets passed to a method when it's used, but this rule isn't catching either of those scenarios, leaving a big blind spot. It does catch bad parameters in a method definition, and bad variable assignments.

@kjvarga
Copy link

kjvarga commented Aug 9, 2019

Created issue: #7275

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants