class RuboCop::Cop::Layout::FirstArrayElementIndentation
]
:completely_different
and_now_for_something = [
# good
]
:completely_different
and_now_for_something = [
# bad
# brackets are indented to the same position.
# The ‘align_brackets` style enforces that the opening and closing
@example EnforcedStyle: align_brackets
])
:no_difference
and_in_a_method_call([
]
:value
array = [
# good
])
:its_like_this
but_in_a_method_call([
]
:value
array = [
# bad
# defined inside a method call.
# separate lines is indented the same as an array literal which is not
# literal where the opening bracket and the first element are on
# The `consistent` style enforces that the first element in an array
@example EnforcedStyle: consistent
])
:its_like_this
but_in_a_method_call([
]
:value
array = [
# good
])
:no_difference
and_in_a_method_call([
]
:value
array = [
# bad
# than the position inside the opening parenthesis.
# element are on separate lines is indented one step (two spaces) more
# element in an array literal where the opening bracket and first
# The `special_inside_parentheses` style enforces that the first
@example EnforcedStyle: special_inside_parentheses (default)
styles are ’consistent’ and ‘align_brackets’. Here are examples:
This default style is called ‘special_inside_parentheses’. Alternative
more than the start of the line where the opening square bracket is.
Other array literals shall have their first element indented one step
position inside the opening parenthesis.
their first element indented one step (two spaces) more than the
same line as the opening parenthesis of the method call, shall have
parentheses, and where the opening square bracket of the array is on the
By default, array literals that are arguments in a method call with
`EnforcedStyle: with_fixed_indentation` is specified for ‘Layout/ArrayAlignment`.
This cop will respect `Layout/ArrayAlignment` and will not work when
The other elements’ indentations are handled by ‘Layout/ArrayAlignment` cop.
where the opening bracket and the first element are on separate lines.
Checks the indentation of the first element in an array literal
def autocorrect(corrector, node)
def autocorrect(corrector, node) AlignmentCorrector.correct(corrector, processed_source, node, @column_delta) end
def base_description(indent_base_type)
def base_description(indent_base_type) case indent_base_type when :left_brace_or_bracket 'the position of the opening bracket' when :first_column_after_left_parenthesis 'the first position after the preceding left parenthesis' when :parent_hash_key 'the parent hash key' else 'the start of the line where the left square bracket is' end end
def brace_alignment_style
def brace_alignment_style :align_brackets end
def check(array_node, left_parenthesis)
def check(array_node, left_parenthesis) return if ignored_node?(array_node) left_bracket = array_node.loc.begin first_elem = array_node.values.first if first_elem return if same_line?(first_elem, left_bracket) check_first(first_elem, left_bracket, left_parenthesis, 0) end check_right_bracket(array_node.loc.end, first_elem, left_bracket, left_parenthesis) end
def check_right_bracket(right_bracket, first_elem, left_bracket, left_parenthesis)
def check_right_bracket(right_bracket, first_elem, left_bracket, left_parenthesis) # if the right bracket is on the same line as the last value, accept return if /\S/.match?(right_bracket.source_line[0...right_bracket.column]) expected_column, indent_base_type = indent_base(left_bracket, first_elem, left_parenthesis) @column_delta = expected_column - right_bracket.column return if @column_delta.zero? msg = message_for_right_bracket(indent_base_type) add_offense(right_bracket, message: msg) do |corrector| autocorrect(corrector, right_bracket) end end
def enforce_first_argument_with_fixed_indentation?
def enforce_first_argument_with_fixed_indentation? argument_alignment_config = config.for_enabled_cop('Layout/ArrayAlignment') argument_alignment_config['EnforcedStyle'] == 'with_fixed_indentation' end
def message(base_description)
def message(base_description) format( MSG, configured_indentation_width: configured_indentation_width, base_description: base_description ) end
def message_for_right_bracket(indent_base_type)
def message_for_right_bracket(indent_base_type) case indent_base_type when :left_brace_or_bracket 'Indent the right bracket the same as the left bracket.' when :first_column_after_left_parenthesis 'Indent the right bracket the same as the first position ' \ 'after the preceding left parenthesis.' when :parent_hash_key 'Indent the right bracket the same as the parent hash key.' \ else 'Indent the right bracket the same as the start of the line ' \ 'where the left bracket is.' end end
def on_array(node)
def on_array(node) return if style != :consistent && enforce_first_argument_with_fixed_indentation? check(node, nil) if node.loc.begin end
def on_send(node)
def on_send(node) return if style != :consistent && enforce_first_argument_with_fixed_indentation? each_argument_node(node, :array) do |array_node, left_parenthesis| check(array_node, left_parenthesis) end end