Skip to content

Commit

Permalink
[Fix rubocop#7654] Support with_fixed_indentation for Layout/ArrayAli…
Browse files Browse the repository at this point in the history
…gnment

Fixes rubocop#7654.

Added support for with_fixed_indentation for Layout/ArrayAlignment as for Layout/ArgumentAlignment and Layout/ParameterAlignment in order to keep the same style for whole code.
  • Loading branch information
nikitasakau committed Mar 1, 2020
1 parent 44c4669 commit 591e271
Show file tree
Hide file tree
Showing 5 changed files with 415 additions and 127 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -29,6 +29,7 @@
* Add `BracesRequiredMethods` parameter to `Style/BlockDelimiters` to require braces for specific methods such as Sorbet's `sig`. ([@maxh][])
* [#7686](https://github.com/rubocop-hq/rubocop/pull/7686): Add new `JUnitFormatter` formatter based on `rubocop-junit-formatter` gem. ([@koic][])
* [#7715](https://github.com/rubocop-hq/rubocop/pull/7715): Add `Steepfile` to default `Include` list. ([@ybiquitous][])
* [#7654](https://github.com/rubocop-hq/rubocop/issues/7654): Support `with_fixed_indentation` option for `Layout/ArrayAlignment` cop. ([@nikitasakov][])

### Bug fixes

Expand Down Expand Up @@ -4378,3 +4379,4 @@
[@masarakki]: https://github.com/masarakki
[@djudd]: https://github.com/djudd
[@jemmaissroff]: https://github.com/jemmaissroff
[@nikitasakov]: https://github.com/nikitasakov
22 changes: 21 additions & 1 deletion config/default.yml
Expand Up @@ -255,10 +255,30 @@ Layout/ArrayAlignment:
Description: >-
Align the elements of an array literal if they span more than
one line.
StyleGuide: '#align-multiline-arrays'
StyleGuide: '#no-double-indent'
Enabled: true
VersionAdded: '0.49'
VersionChanged: '0.77'
# Alignment of elements of a multi-line array.
#
# The `with_first_parameter` style aligns the following lines along the same
# column as the first element.
#
# array = [1, 2, 3,
# 4, 5, 6]
#
# The `with_fixed_indentation` style aligns the following lines with one
# level of indentation relative to the start of the line with start of array.
#
# array = [1, 2, 3,
# 4, 5, 6]
EnforcedStyle: with_first_element
SupportedStyles:
- with_first_element
- with_fixed_indentation
# By default, the indentation width from Layout/IndentationWidth is used
# But it can be overridden by setting this parameter
IndentationWidth: ~

Layout/AssignmentIndentation:
Description: >-
Expand Down
63 changes: 53 additions & 10 deletions lib/rubocop/cop/layout/array_alignment.rb
Expand Up @@ -6,33 +6,76 @@ module Layout
# Here we check if the elements of a multi-line array literal are
# aligned.
#
# @example
# @example EnforcedStyle: with_first_element (default)
# # good
#
# array = [1, 2, 3,
# 4, 5, 6]
# array = ['run',
# 'forrest',
# 'run']
#
# # bad
# a = [1, 2, 3,
#
# array = [1, 2, 3,
# 4, 5, 6]
# array = ['run',
# 'forrest',
# 'run']
#
# @example EnforcedStyle: with_fixed_indentation
# # good
# a = [1, 2, 3,
# 4, 5, 6]
# a = ['run',
# 'forrest',
# 'run']
#
# array = [1, 2, 3,
# 4, 5, 6]
#
# # bad
#
# array = [1, 2, 3,
# 4, 5, 6]
class ArrayAlignment < Cop
include Alignment

MSG = 'Align the elements of an array literal if they span more ' \
'than one line.'
ALIGN_ELEMENTS_MSG = 'Align the elements of an array literal ' \
'if they span more than one line.'

FIXED_INDENT_MSG = 'Use one level of indentation for elements ' \
'following the first line of a multi-line array.'

def on_array(node)
check_alignment(node.children)
return if node.children.size < 2

check_alignment(node.children, base_column(node, node.children))
end

def autocorrect(node)
AlignmentCorrector.correct(processed_source, node, column_delta)
end

private

def message(_node)
fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_ELEMENTS_MSG
end

def fixed_indentation?
cop_config['EnforcedStyle'] == 'with_fixed_indentation'
end

def base_column(node, args)
if fixed_indentation?
lineno = target_method_lineno(node)
line = node.source_range.source_buffer.source_line(lineno)
indentation_of_line = /\S.*/.match(line).begin(0)
indentation_of_line + configured_indentation_width
else
display_column(args.first.source_range)
end
end

def target_method_lineno(node)
node.loc.line
end
end
end
end
Expand Down
38 changes: 31 additions & 7 deletions manual/cops_layout.md
Expand Up @@ -124,25 +124,49 @@ aligned.

### Examples

#### EnforcedStyle: with_first_element (default)

```ruby
# good

array = [1, 2, 3,
4, 5, 6]
array = ['run',
'forrest',
'run']

# bad
a = [1, 2, 3,

array = [1, 2, 3,
4, 5, 6]
array = ['run',
'forrest',
'run']
```
#### EnforcedStyle: with_fixed_indentation

```ruby
# good
a = [1, 2, 3,
4, 5, 6]
a = ['run',
'forrest',
'run']

array = [1, 2, 3,
4, 5, 6]

# bad

array = [1, 2, 3,
4, 5, 6]
```

### Configurable attributes

Name | Default value | Configurable values
--- | --- | ---
EnforcedStyle | `with_first_element` | `with_first_element`, `with_fixed_indentation`
IndentationWidth | `<none>` | Integer

### References

* [https://rubystyle.guide#align-multiline-arrays](https://rubystyle.guide#align-multiline-arrays)
* [https://rubystyle.guide#no-double-indent](https://rubystyle.guide#no-double-indent)

## Layout/AssignmentIndentation

Expand Down

0 comments on commit 591e271

Please sign in to comment.