class RuboCop::Cop::Layout::HashAlignment
bar: 2})
do_something({foo: 1,
# good
bar: 2)
do_something(foo: 1,
# bad
# Ignore only explicit hashes.
@example EnforcedLastArgumentHashStyle: ignore_explicit
bar: 2)
do_something(foo: 1,
# good
bar: 2})
do_something({foo: 1,
# bad
# Ignore only implicit hashes.
@example EnforcedLastArgumentHashStyle: ignore_implicit
bar: 2})
do_something({foo: 1,
# good
bar: 2)
do_something(foo: 1,
# good
# Ignore both implicit and explicit hashes.
@example EnforcedLastArgumentHashStyle: always_ignore
})
bar: 2
foo: 1,
do_something({
# good
bar: 2})
do_something({foo: 1,
# good
)
bar: 2
foo: 1,
do_something(
# good
bar: 2)
do_something(foo: 1,
# good
bar: 2})
do_something({foo: 1,
# bad
bar: 2)
do_something(foo: 1,
# bad
# Inspect both implicit and explicit hashes.
@example EnforcedLastArgumentHashStyle: always_inspect (default)
}
ba: baz
foo: bar,
{
# good
}
ba: baz
foo: bar,
{
# bad
@example EnforcedColonStyle: table
}
ba: baz
foo: bar,
{
# good
}
ba: baz
foo: bar,
{
# bad
@example EnforcedColonStyle: separator
}
ba: baz
foo: bar,
{
# good
}
ba: baz
foo: bar,
{
}
ba: baz
foo: bar,
{
# bad
@example EnforcedColonStyle: key (default)
}
:ba => baz
:foo => bar,
{
# good
}
:ba => baz
:foo => bar,
{
# bad
@example EnforcedHashRocketStyle: table
}
:ba => baz
:foo => bar,
{
# good
}
:ba => baz
:foo => bar,
{
}
:ba => baz
:foo => bar,
{
# bad
@example EnforcedHashRocketStyle: separator
}
:ba => baz
:foo => bar,
{
# good
}
:ba => baz
:foo => bar,
{
}
:ba => baz
:foo => bar,
{
# bad
@example EnforcedHashRocketStyle: key (default)
passing a list of styles to EnforcedStyles.
Alternatively you can specify multiple allowed styles. That’s done by
* ignore_implicit (without curly braces)
* always_ignore
* always_inspect
can also be configured. The options are:
The treatment of hashes passed as the last argument to a method call
* table (left align keys, hash rockets, and values)
* separator (align hash rockets and colons, right align keys)
* key (left align keys, one space before hash rockets and values)
options are:
literal are aligned according to configuration. The configuration
Check that the keys, separators, and values of a multi-line hash
def add_offenses
def add_offenses kwsplat_offenses = offenses_by.delete(KeywordSplatAlignment) register_offenses_with_format(kwsplat_offenses, KeywordSplatAlignment) format, offenses = offenses_by.min_by { |_, v| v.length } register_offenses_with_format(offenses, format) end
def adjust(corrector, delta, range)
def adjust(corrector, delta, range) if delta.positive? corrector.insert_before(range, ' ' * delta) elsif delta.negative? range = range_between(range.begin_pos - delta.abs, range.begin_pos) corrector.remove(range) end end
def alignment_for(pair)
def alignment_for(pair) if pair.kwsplat_type? [KeywordSplatAlignment.new] elsif pair.hash_rocket? alignment_for_hash_rockets else alignment_for_colons end end
def alignment_for_colons
def alignment_for_colons @alignment_for_colons ||= new_alignment('EnforcedColonStyle') end
def alignment_for_hash_rockets
def alignment_for_hash_rockets @alignment_for_hash_rockets ||= new_alignment('EnforcedHashRocketStyle') end
def argument_alignment_config
def argument_alignment_config config.for_cop('Layout/ArgumentAlignment') end
def autocorrect_incompatible_with_other_cops?(node)
def autocorrect_incompatible_with_other_cops?(node) return false unless enforce_first_argument_with_fixed_indentation? && node.pairs.any? && node.parent&.call_type? parent_loc = node.parent.loc selector = parent_loc.selector || parent_loc.expression same_line?(selector, node.pairs.first) end
def check_delta(delta, node:, alignment:)
def check_delta(delta, node:, alignment:) offenses_by[alignment.class] ||= [] return if good_alignment? delta column_deltas[alignment.class][node] = delta offenses_by[alignment.class].push(node) end
def check_pairs(node)
def check_pairs(node) first_pair = node.pairs.first reset! alignment_for(first_pair).each do |alignment| delta = alignment.deltas_for_first_pair(first_pair, node) check_delta delta, node: first_pair, alignment: alignment end node.children.each do |current| alignment_for(current).each do |alignment| delta = alignment.deltas(first_pair, current) check_delta delta, node: current, alignment: alignment end end add_offenses end
def correct_key_value(corrector, delta, key, value, separator)
def correct_key_value(corrector, delta, key, value, separator) # We can't use the instance variable inside the lambda. That would # just give each lambda the same reference and they would all get the # last value of each. Some local variables fix the problem. separator_delta = delta[:separator] || 0 value_delta = delta[:value] || 0 key_delta = delta[:key] || 0 key_column = key.column key_delta = -key_column if key_delta < -key_column adjust(corrector, key_delta, key) adjust(corrector, separator_delta, separator) adjust(corrector, value_delta, value) end
def correct_no_value(corrector, key_delta, key)
def correct_no_value(corrector, key_delta, key) adjust(corrector, key_delta, key) end
def correct_node(corrector, node, delta)
def correct_node(corrector, node, delta) # We can't use the instance variable inside the lambda. That would # just give each lambda the same reference and they would all get the # last value of each. A local variable fixes the problem. if node.value && node.respond_to?(:value_omission?) && !node.value_omission? correct_key_value(corrector, delta, node.key.source_range, node.value.source_range, node.loc.operator) else delta_value = delta[:key] || 0 correct_no_value(corrector, delta_value, node.source_range) end end
def double_splat?(node)
def double_splat?(node) node.children.last.is_a?(Symbol) end
def enforce_first_argument_with_fixed_indentation?
def enforce_first_argument_with_fixed_indentation? return false unless argument_alignment_config['Enabled'] argument_alignment_config['EnforcedStyle'] == 'with_fixed_indentation' end
def good_alignment?(column_deltas)
def good_alignment?(column_deltas) column_deltas.values.all?(&:zero?) end
def ignore_hash_argument?(node)
def ignore_hash_argument?(node) case cop_config['EnforcedLastArgumentHashStyle'] when 'always_inspect' then false when 'always_ignore' then true when 'ignore_explicit' then node.braces? when 'ignore_implicit' then !node.braces? end end
def new_alignment(key)
def new_alignment(key) formats = cop_config[key] formats = [formats] if formats.is_a? String formats.uniq.map do |format| case format when 'key' KeyAlignment.new when 'table' TableAlignment.new when 'separator' SeparatorAlignment.new else raise "Unknown #{key}: #{formats}" end end end
def on_hash(node)
def on_hash(node) return if autocorrect_incompatible_with_other_cops?(node) || ignored_node?(node) || node.pairs.empty? || node.single_line? proc = ->(a) { a.checkable_layout?(node) } return unless alignment_for_hash_rockets.any?(proc) && alignment_for_colons.any?(proc) check_pairs(node) end
def on_send(node)
def on_send(node) return if double_splat?(node) return unless node.arguments? last_argument = node.last_argument return unless last_argument.hash_type? && ignore_hash_argument?(last_argument) ignore_node(last_argument) end
def register_offenses_with_format(offenses, format)
def register_offenses_with_format(offenses, format) (offenses || []).each do |offense| add_offense(offense, message: MESSAGES[format]) do |corrector| delta = column_deltas[alignment_for(offense).first.class][offense] correct_node(corrector, offense, delta) unless delta.nil? end end end
def reset!
def reset! self.offenses_by = {} self.column_deltas = Hash.new { |hash, key| hash[key] = {} } end