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 Layout/AlignHash when mixed Hash styles are used #7068

Merged

Conversation

rmm5t
Copy link
Contributor

@rmm5t rmm5t commented May 21, 2019

Fixes #7066. Otherwise, this happens when mixed hash styles are used:

      {
        'Content-Type' => 'application/json',
        Authorization: "Bearer #{@access_token}",
      }
An error occurred while Layout/AlignHash cop was inspecting /Users/rmcgeary/work/ironridge/lib/salesforce/client.rb:72:6.
nil can't be coerced into Integer
.../gems/rubocop-0.70.0/lib/rubocop/cop/mixin/hash_alignment.rb:113:in `+'
.../gems/rubocop-0.70.0/lib/rubocop/cop/mixin/hash_alignment.rb:113:in `value_delta'
.../gems/rubocop-0.70.0/lib/rubocop/cop/mixin/hash_alignment.rb:65:in `deltas'
.../gems/rubocop-0.70.0/lib/rubocop/cop/layout/align_hash.rb:239:in `block (2 levels) in check_pairs'
.../gems/rubocop-0.70.0/lib/rubocop/cop/layout/align_hash.rb:238:in `each'
.../gems/rubocop-0.70.0/lib/rubocop/cop/layout/align_hash.rb:238:in `block in check_pairs'
.../gems/rubocop-0.70.0/lib/rubocop/cop/layout/align_hash.rb:237:in `each'
.../gems/rubocop-0.70.0/lib/rubocop/cop/layout/align_hash.rb:237:in `check_pairs'
.../gems/rubocop-0.70.0/lib/rubocop/cop/layout/align_hash.rb:208:in `on_hash'

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).
  • 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.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@rmm5t rmm5t force-pushed the 7066-fix-layout-align-hash-mixed-hash-styles branch from c9c64a8 to ea22a4e Compare May 22, 2019 01:04
@@ -102,7 +102,7 @@ def key_delta(first_pair, current_pair)
end

def hash_rocket_delta(first_pair, current_pair)
first_pair.loc.column + max_key_width + 1 -
first_pair.loc.column + max_key_width.to_i + 1 -
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why isn't this a number in the first place? Probably we can convert it earlier and this would yield slightly better code.

Copy link
Contributor Author

@rmm5t rmm5t May 22, 2019

Choose a reason for hiding this comment

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

Fair question. I found this code to be a tad hard to follow. This value is initialized to nil as an attr_accessor, and its usage expects deltas_for_first_pair to have been called first, but it apparently wasn’t under the particular scenario that caused the error.

Are you asking for an initializer that initializes this value to zero?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To clarify, for now, I’m just trying to keep the bug fix as small as possible considering there might be other syntax scenarios that we haven’t considered yet for the new AlignHash features.

Copy link
Contributor

Choose a reason for hiding this comment

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

It’s probably related to AlignHash now being able to use a mix of e.g. TableAlignment and KeyAlignment, leading to – as you mention – TableAlignment#deltas_for_first_pair not being called before accessing max_key_width, and thus doing calculations with nil

I agree that defaulting to 0 would be a good approach here.

Copy link
Contributor Author

@rmm5t rmm5t May 22, 2019

Choose a reason for hiding this comment

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

Okay, I'll add a constructor to default the value to zero. Stand by.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested changes pushed.

@rmm5t rmm5t force-pushed the 7066-fix-layout-align-hash-mixed-hash-styles branch from d616788 to 9b073e6 Compare May 22, 2019 20:29
@bbatsov bbatsov merged commit 3384933 into rubocop:master May 23, 2019
@bbatsov
Copy link
Collaborator

bbatsov commented May 23, 2019

Thanks!

@rmm5t rmm5t deleted the 7066-fix-layout-align-hash-mixed-hash-styles branch May 23, 2019 13:36
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.

New Layout/AlignHash has a runtime error on mixed Hash style
4 participants