class RuboCop::Cop::Layout::LineEndStringConcatenationIndentation
}
‘in two parts’
first: ‘a message’ <br>my_hash = {
‘y’
result = ‘x’ <br># good
}
‘in two parts’
first: ‘a message’ <br>my_hash = {
‘y’
result = ‘x’ <br># bad
@example EnforcedStyle: indented
}
‘in two parts’
first: ‘a message’ <br>my_hash = {
‘y’
puts ‘x’ <br># good
}
‘in two parts’
first: ‘a message’ <br>my_hash = {
‘y’
puts ‘x’ <br># bad
@example EnforcedStyle: aligned (default)
end
’z’
‘y’ <br>‘x’ <br>def some_method
# good
}
‘in two parts’
first: ‘a message’ <br>my_hash = {
end
’z’
‘y’ <br>‘x’ <br>def some_method
# bad
@example
more than the first line. Lines 3 and forward shall be aligned with line 2.
If ‘EnforcedStyle: indented` is set, it’s the second line that shall be indented one step
concatenated string parts shall be indented regardless of ‘EnforcedStyle` configuration.
first part. There are some exceptions, such as implicit return values, where the
If `EnforcedStyle: aligned` is set, the concatenated string parts shall be aligned with the
literal and a backslash.
Checks the indentation of the next line after a line that ends with a string
def add_offense_and_correction(node, message)
def add_offense_and_correction(node, message) add_offense(node, message: message) { |corrector| autocorrect(corrector, node) } end
def always_indented?(dstr_node)
def always_indented?(dstr_node) PARENT_TYPES_FOR_INDENTED.include?(dstr_node.parent&.type) end
def autocorrect(corrector, node)
def autocorrect(corrector, node) AlignmentCorrector.correct(corrector, processed_source, node, @column_delta) end
def base_column(child)
def base_column(child) grandparent = child.parent.parent if grandparent&.type == :pair grandparent.loc.column else child.source_range.source_line =~ /\S/ end end
def check_aligned(children, start_index)
def check_aligned(children, start_index) base_column = children[start_index - 1].loc.column children[start_index..].each do |child| @column_delta = base_column - child.loc.column add_offense_and_correction(child, MSG_ALIGN) if @column_delta != 0 base_column = child.loc.column end end
def check_indented(children)
def check_indented(children) @column_delta = base_column(children[0]) + configured_indentation_width - children[1].loc.column add_offense_and_correction(children[1], MSG_INDENT) if @column_delta != 0 end
def on_dstr(node)
def on_dstr(node) return unless strings_concatenated_with_backslash?(node) children = node.children return if children.empty? if style == :aligned && !always_indented?(node) check_aligned(children, 1) else check_indented(children) check_aligned(children, 2) end end
def strings_concatenated_with_backslash?(dstr_node)
def strings_concatenated_with_backslash?(dstr_node) dstr_node.multiline? && dstr_node.children.all? { |c| c.str_type? || c.dstr_type? } && dstr_node.children.none?(&:multiline?) end