class RuboCop::Cop::Layout::ArgumentAlignment
key: value
:baz,
foo :bar,
# bad
key: value
:baz,
foo :bar,
# good
@example EnforcedStyle: with_fixed_indentation
)
key: value
:baz,
:bar,
foo(
key: value
:baz,
foo :bar,
# bad
)
key: value
:baz,
:bar,
foo(
key: value
:baz,
foo :bar,
# good
@example EnforcedStyle: with_first_argument (default)
definition are aligned.
Here we check if the arguments on a multi-line method
def arguments_or_first_arg_pairs(node)
def arguments_or_first_arg_pairs(node) first_arg = node.first_argument if first_arg.hash_type? && !first_arg.braces? first_arg.pairs else node.arguments end end
def arguments_with_last_arg_pairs(node)
def arguments_with_last_arg_pairs(node) items = node.arguments[0..-2] last_arg = node.arguments.last if last_arg.hash_type? && !last_arg.braces? items += last_arg.pairs else items << last_arg end items end
def autocorrect(corrector, node)
def autocorrect(corrector, node) AlignmentCorrector.correct(corrector, processed_source, node, column_delta) end
def autocorrect_incompatible_with_other_cops?
def autocorrect_incompatible_with_other_cops? with_first_argument_style? && enforce_hash_argument_with_separator? end
def base_column(node, first_argument)
def base_column(node, first_argument) if fixed_indentation? || first_argument.nil? 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(first_argument.source_range) end end
def enforce_hash_argument_with_separator?
def enforce_hash_argument_with_separator? return false unless hash_argument_config['Enabled'] RuboCop::Cop::Layout::HashAlignment::SEPARATOR_ALIGNMENT_STYLES.any? do |style| hash_argument_config[style]&.include?('separator') end end
def fixed_indentation?
def fixed_indentation? cop_config['EnforcedStyle'] == 'with_fixed_indentation' end
def flattened_arguments(node)
def flattened_arguments(node) if fixed_indentation? arguments_with_last_arg_pairs(node) else arguments_or_first_arg_pairs(node) end end
def hash_argument_config
def hash_argument_config config.for_cop('Layout/HashAlignment') end
def message(_node)
def message(_node) fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_PARAMS_MSG end
def multiple_arguments?(node)
def multiple_arguments?(node) return true if node.arguments.size >= 2 first_argument = node.first_argument first_argument&.hash_type? && first_argument.pairs.count >= 2 end
def on_send(node)
def on_send(node) return if !multiple_arguments?(node) || (node.send_type? && node.method?(:[]=)) || autocorrect_incompatible_with_other_cops? items = flattened_arguments(node) check_alignment(items, base_column(node, items.first)) end
def target_method_lineno(node)
def target_method_lineno(node) if node.loc.selector node.loc.selector.line else # l.(1) has no selector, so we use the opening parenthesis instead node.loc.begin.line end end
def with_first_argument_style?
def with_first_argument_style? cop_config['EnforcedStyle'] == 'with_first_argument' end