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
Bug encountered with Layout/RescueEnsureAlignment
Cop and a memoized method
#6254
Comments
The # Session-I
$ rubocop test.rb -a
Inspecting 1 file
C
Offenses:
test.rb:8:5: C: [Corrected]Layout/RescueEnsureAlignment: rescue at 8, 4 is not aligned with begin at 6, 13.
rescue StandardError
^^^^^^
1 file inspected, 1 offense detected, 1 offense corrected # Session-II
$ rubocop test.rb -a
Inspecting 1 file
C
Offenses:
test.rb:8:5: C: [Corrected]Layout/RescueEnsureAlignment: rescue at 8, 4 is not aligned with begin at 6, 13.
rescue StandardError
^^^^^^
1 file inspected, 1 offense detected, 1 offense corrected |
Why do you think this is a bug? All layout cops default to layout like:
(that's alignment with the keyword) Most cops are configurable and allow you to configure fixed indent with respect to the first line as well (which seems to be what you want). P.S. The markdown rendering is retarded and I can't get it to show what I want. |
@bbatsov I understand that you're recommending the following # frozen_string_literal: true
# Dummy Class
class Foo
def bar
@bar ||= begin
expensive_method
rescue StandardError
fall_back
end
end
end but IMO, that just reduces the readability, IMO..
I could not find any docs on how to configure this cop..
Because to me, the existing code is more readable / visually pleasing: class Foo
def bar
@bar ||= begin
expensive_method
rescue StandardError
fall_back
end
end
end I would've even expected Rubocop to recommend aligning with the |
Either ways, the |
Barring all personal opinion on what is readable and not, I labelled this as a feature request to add a configuration option to make indentation respect the assignment LHS like so: @bar ||= begin
expensive_method
rescue StandardError
fall_back
end when used as RHS of an assignment. I added a separate bug report for the auto-correct problem in #6258. |
@Drenmi Thank you for getting the ball rolling on the two issues.. |
This feature request is similar to how foo = if bar
2
else
3
end On its own, I believe |
I apologize for introducing this new bug. It was introduced while fixing another bug, #6246. I switched the check for alignment to be based on the keyword rather than I agree with @Drenmi and @bquorning on handling this with new configuration. |
Barely related, this cop seems oddly opinionated when using rescue with blocks:
triggers
I would expect to align |
Old implementation depended on He doesn't break anything, because Ryan attached
Today I am introducing fix #6328 for inline access modifiers. It is not related with this issue, but I read this code and can say something about it. I see you want to receive
We need to implement something like What do you think, how to implement such method? Some special nodes like We need here any old project developer to ask how to provide baseline method. I think it is not a trivial change. |
We can compare the following code.
I can offer algorithm:
What do you think about it? I don't know whether we should implement it as a mixin for cops or as a method for |
[Fix #6254] Fix RescueEnsureAlignment for non-local assignments
@Drenmi @ashmaroli is it just me, or does the example from the original comment still fail even after the fix? # test.rb
class Foo
def bar
@bar ||= begin
expensive_method
rescue StandardError
fall_back
end
end
end
|
@oehlschl The fix has not released yet. To test, you'll have to point your Gemfile to the # Gemfile
gem 'rubocop', git: 'https://github.com/rubocop-hq/rubocop.git', branch: 'master'
|
Thanks @ashmaroli! Should've checked that before asking. I suspected as much, though I was confused by the changelog, since it looked to me like it went into 0.63.0. Anyway, thanks again all. |
@ashmaroli @Drenmi not trying to be dense, but it looks like the syntax above still fails in master? I opened a spec-only PR in #6771. |
@oehlschl I agree with you. My test case is failing with What's worse is that the autocorrect result is not correct as well: Original script# frozen_string_literal: true
# Dummy Class
class Foo
def bar
@bar ||= begin
expensive_method
rescue StandardError
fall_back
end
end
end Running RuboCoprubocop -V
# => 0.64.0 (using Parser 2.6.0.0, running on ruby 2.4.4 x64-mingw32)
rubocop test.rb --auto-correct
Inspecting 1 file
C
Offenses:
test.rb:8:5: C: [Corrected] Layout/RescueEnsureAlignment: rescue at 8, 4 is not aligned with begin at 6, 13.
rescue StandardError
^^^^^^
test.rb:9:7: C: [Corrected] Layout/IndentationWidth: Use 2 (not -7) spaces for indentation.
fall_back
^^^^^^^
1 file inspected, 2 offenses detected, 2 offenses corrected Result# frozen_string_literal: true
# Dummy Class
class Foo
def bar
@bar ||= begin
expensive_method
rescue StandardError
fall_back
end
end
end |
@Drenmi I would like this ticket to be re-opened based on above evidence. |
What configuration options are looking at here?
There's room for a combinatorial explosion of options here (rescue,begin,def,class, etc.), but I don't think anything complicated will be worth maintaining. I'm looking into fixing this issue. |
Expected behavior
Expected RuboCop to not flag the method.
Actual behavior
Steps to reproduce the problem
test.rb
:rubocop test.rb
RuboCop version
Include the output of
rubocop -V
orbundle exec rubocop -V
if using Bundler. Here's an example:Inference
Rubocop recommending to place
rescue
at position 13 in this example is a distasteful 🐛/cc @rrosenblum
The text was updated successfully, but these errors were encountered: