Skip to content

Commit

Permalink
[Fix rubocop#4704] Move Lint/EndAlignment to Layout
Browse files Browse the repository at this point in the history
Oftentimes, a misaligned `end` on module/class definition is just a
whitespace/indentation error. Having it fixed together with the other `Layout`
cops by e.g. `rubocop --auto-correct --only Layout` is very convenient.

But since it *may* be something more serious than just a style issue, cop
violations are still of severity `warning`.
  • Loading branch information
bquorning committed Jan 6, 2018
1 parent 8c545d1 commit 89c43bf
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 109 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* [#5240](https://github.com/bbatsov/rubocop/pull/5240): Make `Style/StringHashKeys` to accepts environment variables. ([@pocke][])
* [#5395](https://github.com/bbatsov/rubocop/pull/5395): Always exit 2 when specified configuration file does not exist. ([@pocke][])
* [#5402](https://github.com/bbatsov/rubocop/pull/5402): Remove undefined `ActiveSupport::TimeZone#strftime` method from defined dangerous methods of `Rails/TimeZone` cop. ([@koic][])
* [#4704](https://github.com/bbatsov/rubocop/issues/4704): Move `Lint/EndAlignment` to the `Layout` namespace. ([@bquorning][])

## 0.52.1 (2017-12-27)

Expand Down
33 changes: 17 additions & 16 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,23 @@ Layout/EmptyLinesAroundModuleBody:
- empty_lines_special
- no_empty_lines

# Align ends correctly.
Layout/EndAlignment:
# The value `keyword` means that `end` should be aligned with the matching
# keyword (`if`, `while`, etc.).
# The value `variable` means that in assignments, `end` should be aligned
# with the start of the variable on the left hand side of `=`. In all other
# situations, `end` should still be aligned with the keyword.
# The value `start_of_line` means that `end` should be aligned with the start
# of the line which the matching keyword appears on.
EnforcedStyleAlignWith: keyword
SupportedStylesAlignWith:
- keyword
- variable
- start_of_line
AutoCorrect: false
Severity: warning

Layout/EndOfLine:
# The `native` style means that CR+LF (Carriage Return + Line Feed) is
# enforced on Windows, and LF is enforced on other platforms. The other styles
Expand Down Expand Up @@ -1510,22 +1527,6 @@ Lint/DefEndAlignment:
- def
AutoCorrect: false

# Align ends correctly.
Lint/EndAlignment:
# The value `keyword` means that `end` should be aligned with the matching
# keyword (`if`, `while`, etc.).
# The value `variable` means that in assignments, `end` should be aligned
# with the start of the variable on the left hand side of `=`. In all other
# situations, `end` should still be aligned with the keyword.
# The value `start_of_line` means that `end` should be aligned with the start
# of the line which the matching keyword appears on.
EnforcedStyleAlignWith: keyword
SupportedStylesAlignWith:
- keyword
- variable
- start_of_line
AutoCorrect: false

Lint/InheritException:
# The default base class in favour of `Exception`.
EnforcedStyle: runtime_error
Expand Down
8 changes: 4 additions & 4 deletions config/enabled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ Layout/EmptyLinesAroundModuleBody:
StyleGuide: '#empty-lines-around-bodies'
Enabled: true

Layout/EndAlignment:
Description: 'Align ends correctly.'
Enabled: true

Layout/EndOfLine:
Description: 'Use Unix-style line endings.'
StyleGuide: '#crlf'
Expand Down Expand Up @@ -496,10 +500,6 @@ Lint/EmptyWhen:
Description: 'Checks for `when` branches with empty bodies.'
Enabled: true

Lint/EndAlignment:
Description: 'Align ends correctly.'
Enabled: true

Lint/EndInMethod:
Description: 'END blocks should not be placed inside method definitions.'
Enabled: true
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
require_relative 'rubocop/cop/layout/empty_lines_around_method_body'
require_relative 'rubocop/cop/layout/empty_lines_around_module_body'
require_relative 'rubocop/cop/layout/empty_lines'
require_relative 'rubocop/cop/layout/end_alignment'
require_relative 'rubocop/cop/layout/end_of_line'
require_relative 'rubocop/cop/layout/extra_spacing'
require_relative 'rubocop/cop/layout/first_array_element_line_break'
Expand Down Expand Up @@ -257,7 +258,6 @@
require_relative 'rubocop/cop/lint/empty_expression'
require_relative 'rubocop/cop/lint/empty_interpolation'
require_relative 'rubocop/cop/lint/empty_when'
require_relative 'rubocop/cop/lint/end_alignment'
require_relative 'rubocop/cop/lint/end_in_method'
require_relative 'rubocop/cop/lint/ensure_return'
require_relative 'rubocop/cop/lint/float_out_of_range'
Expand Down
11 changes: 10 additions & 1 deletion lib/rubocop/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ class Config
'`Naming/VariableName`.',
'Style/VariableNumber' =>
'The `Style/VariableNumber` cop has been renamed to ' \
'`Naming/VariableNumber`.'
'`Naming/VariableNumber`.',
'Lint/EndAlignment' =>
'The `Lint/EndAlignment` cop has been renamed to ' \
'`Layout/EndAlignment`.'
}.freeze

OBSOLETE_PARAMETERS = [
Expand Down Expand Up @@ -169,6 +172,12 @@ class Config
alternative: '`AlignWith` has been renamed to ' \
'`EnforcedStyleAlignWith`'
},
{
cop: 'Layout/EndAlignment',
parameter: 'AlignWith',
alternative: '`AlignWith` has been renamed to ' \
'`EnforcedStyleAlignWith`'
},
{
cop: 'Lint/DefEndAlignment',
parameter: 'AlignWith',
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/else_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def check_assignment(node, rhs)
rhs = first_part_of_call_chain(rhs)
return unless rhs

end_config = config.for_cop('Lint/EndAlignment')
end_config = config.for_cop('Layout/EndAlignment')
style = end_config['EnforcedStyleAlignWith'] || 'keyword'
base = variable_alignment?(node.loc, rhs, style.to_sym) ? node : rhs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module RuboCop
module Cop
module Lint
module Layout
# This cop checks whether the end keywords are aligned properly.
#
# Three modes are supported through the `EnforcedStyleAlignWith`
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/indentation_width.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def check_assignment(node, rhs)
rhs = first_part_of_call_chain(rhs)
return unless rhs

end_config = config.for_cop('Lint/EndAlignment')
end_config = config.for_cop('Layout/EndAlignment')
style = end_config['EnforcedStyleAlignWith'] || 'keyword'
base = variable_alignment?(node.loc, rhs, style.to_sym) ? node : rhs

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/conditional_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module ConditionalAssignmentHelper
extend NodePattern::Macros

EQUAL = '='.freeze
END_ALIGNMENT = 'Lint/EndAlignment'.freeze
END_ALIGNMENT = 'Layout/EndAlignment'.freeze
ALIGN_WITH = 'EnforcedStyleAlignWith'.freeze
KEYWORD = 'keyword'.freeze

Expand Down
2 changes: 1 addition & 1 deletion manual/basic_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test.rb:2:3: C: Style/GuardClause: Use a guard clause instead of wrapping the co
test.rb:2:3: C: Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
if something
^^
test.rb:4:5: W: Lint/EndAlignment: end at 4, 4 is not aligned with if at 2, 2.
test.rb:4:5: W: Layout/EndAlignment: end at 4, 4 is not aligned with if at 2, 2.
end
^^^
Expand Down
2 changes: 1 addition & 1 deletion manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ In the following section you find all available cops:
* [Layout/EmptyLinesAroundExceptionHandlingKeywords](cops_layout.md#layoutemptylinesaroundexceptionhandlingkeywords)
* [Layout/EmptyLinesAroundMethodBody](cops_layout.md#layoutemptylinesaroundmethodbody)
* [Layout/EmptyLinesAroundModuleBody](cops_layout.md#layoutemptylinesaroundmodulebody)
* [Layout/EndAlignment](cops_layout.md#layoutendalignment)
* [Layout/EndOfLine](cops_layout.md#layoutendofline)
* [Layout/ExtraSpacing](cops_layout.md#layoutextraspacing)
* [Layout/FirstArrayElementLineBreak](cops_layout.md#layoutfirstarrayelementlinebreak)
Expand Down Expand Up @@ -201,7 +202,6 @@ In the following section you find all available cops:
* [Lint/EmptyExpression](cops_lint.md#lintemptyexpression)
* [Lint/EmptyInterpolation](cops_lint.md#lintemptyinterpolation)
* [Lint/EmptyWhen](cops_lint.md#lintemptywhen)
* [Lint/EndAlignment](cops_lint.md#lintendalignment)
* [Lint/EndInMethod](cops_lint.md#lintendinmethod)
* [Lint/EnsureReturn](cops_lint.md#lintensurereturn)
* [Lint/FloatOutOfRange](cops_lint.md#lintfloatoutofrange)
Expand Down
70 changes: 70 additions & 0 deletions manual/cops_layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,76 @@ EnforcedStyle | `no_empty_lines` | `empty_lines`, `empty_lines_except_namespace`

* [https://github.com/bbatsov/ruby-style-guide#empty-lines-around-bodies](https://github.com/bbatsov/ruby-style-guide#empty-lines-around-bodies)

## Layout/EndAlignment

Enabled by default | Supports autocorrection
--- | ---
Enabled | Yes

This cop checks whether the end keywords are aligned properly.

Three modes are supported through the `EnforcedStyleAlignWith`
configuration parameter:

If it's set to `keyword` (which is the default), the `end`
shall be aligned with the start of the keyword (if, class, etc.).

If it's set to `variable` the `end` shall be aligned with the
left-hand-side of the variable assignment, if there is one.

If it's set to `start_of_line`, the `end` shall be aligned with the
start of the line where the matching keyword appears.

### Examples

#### EnforcedStyleAlignWith: keyword (default)

```ruby
# bad

variable = if true
end

# good

variable = if true
end
```
#### EnforcedStyleAlignWith: variable

```ruby
# bad

variable = if true
end

# good

variable = if true
end
```
#### EnforcedStyleAlignWith: start_of_line

```ruby
# bad

variable = if true
end

# good

puts(if true
end)
```

### Configurable attributes

Name | Default value | Configurable values
--- | --- | ---
EnforcedStyleAlignWith | `keyword` | `keyword`, `variable`, `start_of_line`
AutoCorrect | `false` | Boolean
Severity | `warning` | String

## Layout/EndOfLine

Enabled by default | Supports autocorrection
Expand Down
69 changes: 0 additions & 69 deletions manual/cops_lint.md
Original file line number Diff line number Diff line change
Expand Up @@ -720,75 +720,6 @@ when baz then 2
end
```

## Lint/EndAlignment

Enabled by default | Supports autocorrection
--- | ---
Enabled | Yes

This cop checks whether the end keywords are aligned properly.

Three modes are supported through the `EnforcedStyleAlignWith`
configuration parameter:

If it's set to `keyword` (which is the default), the `end`
shall be aligned with the start of the keyword (if, class, etc.).

If it's set to `variable` the `end` shall be aligned with the
left-hand-side of the variable assignment, if there is one.

If it's set to `start_of_line`, the `end` shall be aligned with the
start of the line where the matching keyword appears.

### Examples

#### EnforcedStyleAlignWith: keyword (default)

```ruby
# bad

variable = if true
end

# good

variable = if true
end
```
#### EnforcedStyleAlignWith: variable

```ruby
# bad

variable = if true
end

# good

variable = if true
end
```
#### EnforcedStyleAlignWith: start_of_line

```ruby
# bad

variable = if true
end

# good

puts(if true
end)
```

### Configurable attributes

Name | Default value | Configurable values
--- | --- | ---
EnforcedStyleAlignWith | `keyword` | `keyword`, `variable`, `start_of_line`
AutoCorrect | `false` | Boolean

## Lint/EndInMethod

Enabled by default | Supports autocorrection
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/cli/cli_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class SomeCop < Cop
"\ty = 3",
' end'])
create_file('.rubocop.yml', <<-YAML.strip_indent)
Lint/EndAlignment:
Layout/EndAlignment:
Enabled: false
YAML
expect(cli.run(['--format', 'simple',
Expand Down Expand Up @@ -921,7 +921,7 @@ def badName
'Another good alternative is the usage of control flow &&/||.',
' if something',
' ^^',
'example3.rb:4:5: W: Lint/EndAlignment: ' \
'example3.rb:4:5: W: Layout/EndAlignment: ' \
'end at 4, 4 is not aligned with if at 2, 2.',
' end',
' ^^^',
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/layout/else_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
subject(:cop) { described_class.new(config) }

let(:config) do
RuboCop::Config.new('Lint/EndAlignment' => end_alignment_config)
RuboCop::Config.new('Layout/EndAlignment' => end_alignment_config)
end
let(:end_alignment_config) do
{ 'Enabled' => true, 'EnforcedStyleAlignWith' => 'variable' }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Lint::EndAlignment, :config do
RSpec.describe RuboCop::Cop::Layout::EndAlignment, :config do
subject(:cop) { described_class.new(config) }

let(:cop_config) do
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/layout/indentation_width_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:config) do
RuboCop::Config.new('Layout/IndentationWidth' => cop_config,
'Layout/IndentationConsistency' => consistency_config,
'Lint/EndAlignment' => end_alignment_config,
'Layout/EndAlignment' => end_alignment_config,
'Lint/DefEndAlignment' => def_end_alignment_config)
end
let(:consistency_config) { { 'EnforcedStyle' => 'normal' } }
Expand Down
Loading

0 comments on commit 89c43bf

Please sign in to comment.