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 #4563] Style/TrailingUnderscoreVariable cop leaves an unclosed parentheses #4568

Conversation

smakagon
Copy link
Contributor

This fixes autocorrect issue with Style/TrailingUnderscoreVariable.
Without this fix cop would autocorrect this code:

(foo, _) = [1, 2, 3]

To this:

(foo, = [1, 2, 3]

With the fix it autocorrects this code:

(foo, _) = [1, 2, 3]

To the following:

(foo,) = [1, 2, 3]

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

@smakagon smakagon force-pushed the 4563_style_underscore_variable_cop_leaves_unclosed_parentheses branch from c059e21 to f2d4323 Compare June 30, 2017 20:11
expect(new_source).to eq('(a, b,) = foo()')
end

it 'removes parentheses when every assignment is to `_`' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change the description of this test case and the next to mention that it removes the entire assignment, not just the parentheses. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, makes sense, I'll change that.

)
end

def unused_variable_only?(offense, variables)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method name should be pluralized as #unused_variables_only?? 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will rename.

@Drenmi
Copy link
Collaborator

Drenmi commented Jul 1, 2017

Changes look good! I was a bit surprised by this cop's reliance on ranges, though, instead of working directly with the child nodes of (mlhs). WDYT @smakagon?

@smakagon
Copy link
Contributor Author

smakagon commented Jul 1, 2017

@Drenmi I was surprised too. This cop looked pretty simple to me, but when I started to look into implementation I figured out that it's not that easy.

I think it was implemented basing on ranges because we have to deal with commas and spaces (and parentheses now), but in general I feel like there is a room for improvement.
Code I added is not the best one I ever wrote too, though.

I was focused mostly on the fix for autocorrect. Do you think we should refactor it?

@smakagon smakagon changed the title [FIX #4563] Style/TrailingUnderscoreVariable cop leaves an unclosed parenthesis [FIX #4563] Style/TrailingUnderscoreVariable cop leaves an unclosed parentheses Jul 1, 2017
@smakagon smakagon force-pushed the 4563_style_underscore_variable_cop_leaves_unclosed_parentheses branch from f2d4323 to f8b7b89 Compare July 1, 2017 13:23
@Drenmi
Copy link
Collaborator

Drenmi commented Jul 2, 2017

@smakagon: I think the PR is great. Short and focused. I'd say no need to refactor. Was just curious why the original implementer used this approach. 🙂

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 2, 2017

It might be nice to still refactor this cop down the road.

@bbatsov bbatsov merged commit 122b993 into rubocop:master Jul 2, 2017
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

Successfully merging this pull request may close these issues.

3 participants