lib/rubocop/cop/style/align_hash.rb in rubocop-0.26.1 vs lib/rubocop/cop/style/align_hash.rb in rubocop-0.27.0
- old
+ new
@@ -39,11 +39,11 @@
key_delta = key_delta(first_pair, current_pair)
current_separator = current_pair.loc.operator
separator_delta = separator_delta(first_pair, current_separator,
key_delta)
value_delta = value_delta(first_pair, current_pair) -
- key_delta - separator_delta
+ key_delta - separator_delta
{ key: key_delta, separator: separator_delta, value: value_delta }
end
private
@@ -104,11 +104,12 @@
def value_delta(first_pair, current_pair)
first_key, _ = *first_pair
_, current_value = *current_pair
correct_value_column = first_key.loc.column +
- spaced_separator(current_pair).length + @max_key_width
+ spaced_separator(current_pair).length +
+ @max_key_width
correct_value_column - current_value.loc.column
end
def spaced_separator(node)
node.loc.operator.is?('=>') ? ' => ' : ': '
@@ -146,11 +147,12 @@
MSG = 'Align the elements of a hash literal if they span more than ' \
'one line.'
def on_send(node)
return unless (last_child = node.children.last) &&
- hash?(last_child) && ignore_last_argument_hash?(last_child)
+ hash?(last_child) &&
+ ignore_last_argument_hash?(last_child)
ignore_node(last_child)
end
def on_hash(node)
@@ -160,30 +162,33 @@
@alignment_for_hash_rockets ||=
new_alignment('EnforcedHashRocketStyle')
@alignment_for_colons ||= new_alignment('EnforcedColonStyle')
- first_pair = node.children.first
-
unless @alignment_for_hash_rockets.checkable_layout(node) &&
- @alignment_for_colons.checkable_layout(node)
+ @alignment_for_colons.checkable_layout(node)
return
end
+ check_pairs(node)
+ end
+
+ private
+
+ def check_pairs(node)
+ first_pair = node.children.first
@column_deltas = alignment_for(first_pair)
- .deltas_for_first_pair(first_pair, node)
+ .deltas_for_first_pair(first_pair, node)
add_offense(first_pair, :expression) unless good_alignment?
node.children.each_cons(2) do |prev, current|
@column_deltas = alignment_for(current).deltas(first_pair, prev,
current)
add_offense(current, :expression) unless good_alignment?
end
end
- private
-
def ignore_last_argument_hash?(node)
case cop_config['EnforcedLastArgumentHashStyle']
when 'always_inspect' then false
when 'always_ignore' then true
when 'ignore_explicit' then explicit_hash?(node)
@@ -197,15 +202,14 @@
def explicit_hash?(node)
node.loc.begin
end
- # Returns true if the hash spans multiple lines, and each key-value
- # pair following the first is on a new line.
+ # Returns true if the hash spans multiple lines
def multiline?(node)
return false unless node.loc.expression.source.include?("\n")
- return false if node.children[1..-1].find do |child|
+ return false if node.children[1..-1].all? do |child|
!begins_its_line?(child.loc.expression)
end
true
end