forked from rubocop/rubocop
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes rubocop#8582. This PR adds `Layout/BeginEndAlignment` cop to solve the issue. The following is an example: ```ruby % cat example.rb foo = bar rescue "{}" ``` ## Before The alignment was misaligned because `begin` was not awake. ```ruby % rubocop -a example.rb (snip) % cat example.rb foo = begin bar rescue StandardError '{}' end ``` ## After Awakened by `begin`, so aligned. ```ruby % rubocop -a example.rb (snip) % cat example.rb foo = begin bar rescue StandardError '{}' end ``` I was first planning to add `begin` processing to `Layout/EndAlignment` cop. However, `Layout/EndAlignment` cop with `EnforcedStyleAlignWith: keyword (default)` was not preferred code in the following: ```ruby longlonglonglonglonglonglonglonglong ||= begin do_something end ``` This style has been found to have very long line length with RuboCop's own code. Therefore, since this PR would like to use `EnforcedStyleAlignWith: start_of_line` by default against `foo ||= begin`, this PR added the new cop. ```ruby # EnforcedStyleAlignWith: start_of_line (default) longlonglonglonglonglonglonglonglong ||= begin do_something end ``` This solution allows configuration with minimal impact on existing code. It is configurable if user want to align to `begin`. ```ruby # EnforcedStyleAlignWith: begin longlonglonglonglonglonglonglonglong ||= begin do_something end ``` This PR also makes `Layout/RescueEnsureAlignment` cop aware of new `Layout/BeginEndAlignment` cop's config to align `rescue` indents.
- Loading branch information
Showing
14 changed files
with
341 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Layout | ||
# This cop checks whether the end keyword of `begin` is aligned properly. | ||
# | ||
# Two modes are supported through the `EnforcedStyleAlignWith` configuration | ||
# parameter. If it's set to `start_of_line` (which is the default), the | ||
# `end` shall be aligned with the start of the line where the `begin` | ||
# keyword is. If it's set to `begin`, the `end` shall be aligned with the | ||
# `begin` keyword. | ||
# | ||
# `Layout/EndAlignment` cop aligns with keywords (e.g. `if`, `while`, `case`) | ||
# by default. On the other hand, `||= begin` that this cop targets tends to | ||
# align with the start of the line, it defaults to `EnforcedStyleAlignWith: start_of_line`. | ||
# These style can be configured by each cop. | ||
# | ||
# @example EnforcedStyleAlignWith: start_of_line (default) | ||
# # bad | ||
# foo ||= begin | ||
# do_something | ||
# end | ||
# | ||
# # good | ||
# foo ||= begin | ||
# do_something | ||
# end | ||
# | ||
# @example EnforcedStyleAlignWith: begin | ||
# # bad | ||
# foo ||= begin | ||
# do_something | ||
# end | ||
# | ||
# # good | ||
# foo ||= begin | ||
# do_something | ||
# end | ||
# | ||
class BeginEndAlignment < Base | ||
include EndKeywordAlignment | ||
include RangeHelp | ||
extend AutoCorrector | ||
|
||
MSG = '`end` at %d, %d is not aligned with `%s` at %d, %d.' | ||
|
||
def on_kwbegin(node) | ||
check_begin_alignment(node) | ||
end | ||
|
||
private | ||
|
||
def check_begin_alignment(node) | ||
align_with = { | ||
begin: node.loc.begin, | ||
start_of_line: start_line_range(node) | ||
} | ||
check_end_kw_alignment(node, align_with) | ||
end | ||
|
||
def autocorrect(corrector, node) | ||
AlignmentCorrector.align_end(corrector, processed_source, node, alignment_node(node)) | ||
end | ||
|
||
def alignment_node(node) | ||
case style | ||
when :begin | ||
node | ||
else | ||
start_line_range(node) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Layout::BeginEndAlignment, :config do | ||
let(:cop_config) do | ||
{ 'EnforcedStyleAlignWith' => 'begin' } | ||
end | ||
|
||
include_examples 'aligned', 'begin', '', 'end' | ||
|
||
include_examples 'misaligned', <<~RUBY, false | ||
begin | ||
end | ||
^^^ `end` at 2, 2 is not aligned with `begin` at 1, 0. | ||
RUBY | ||
|
||
include_examples 'aligned', 'puts 1; begin', '', ' end' | ||
|
||
context 'when EnforcedStyleAlignWith is start_of_line' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyleAlignWith' => 'start_of_line' } | ||
end | ||
|
||
include_examples 'aligned', 'puts 1; begin', '', 'end' | ||
|
||
include_examples 'misaligned', <<~RUBY, false | ||
begin | ||
end | ||
^^^ `end` at 2, 2 is not aligned with `begin` at 1, 0. | ||
RUBY | ||
|
||
include_examples 'misaligned', <<~RUBY, :begin | ||
var << begin | ||
end | ||
^^^ `end` at 2, 7 is not aligned with `var << begin` at 1, 0. | ||
RUBY | ||
|
||
include_examples 'aligned', 'var = begin', '', 'end' | ||
end | ||
end |
Oops, something went wrong.