class RuboCop::Cop::Style::RedundantLineContinuation


(argument)
some_method <br># it is not redundant.
# also good - backslash with newline between the method name and its arguments,
- 3
+ 2 <br>1 <br># it is not redundant
# also good - backslash at the line following the newline begins with a + or -,
baz)
foo(bar, # <br># also good - backslash at the end of a comment is not redundant
’baz’)
foo(‘bar’ <br># also good - backslash in string concatenation is not redundant
baz)
foo(bar,
# good
baz)
foo(bar, <br># bad
bar}
{foo:
bar]
[foo,
# good
bar}
{foo: <br>bar]
[foo, <br># bad
.baz
&.bar
foo
bar
foo.
# good
.baz
&.bar <br>foo <br>bar
foo. <br># bad
@example
for string concatenation is not redundant and is not considered an offense.
However, a backslash at the end of a comment or
does not result in a syntax error.
This cop marks a line continuation as redundant if removing the backslash
Check for redundant line continuation.

def argument_is_method?(node)

def argument_is_method?(node)
  return false unless node.send_type?
  return false unless (first_argument = node.first_argument)
  method_call_with_arguments?(first_argument)
end

def argument_newline?(node)

def argument_newline?(node)
  node = node.children.first if node.root? && node.begin_type?
  if argument_is_method?(node)
    argument_newline?(node.first_argument)
  else
    return false unless method_call_with_arguments?(node)
    node.loc.selector.line != node.first_argument.loc.line
  end
end

def ends_with_backslash_without_comment?(source_line)

def ends_with_backslash_without_comment?(source_line)
  source_line.gsub(/#.+/, '').end_with?('\\')
end

def find_node_for_line(line)

def find_node_for_line(line)
  processed_source.ast.each_node do |node|
    return node if same_line?(node, line)
  end
end

def inside_string_literal?(range, token)

def inside_string_literal?(range, token)
  ALLOWED_STRING_TOKENS.include?(token.type) && token.pos.overlaps?(range)
end

def inside_string_literal_or_method_with_argument?(range)

def inside_string_literal_or_method_with_argument?(range)
  processed_source.tokens.each_cons(2).any? do |token, next_token|
    inside_string_literal?(range, token) || method_with_argument?(token, next_token)
  end
end

def method_call_with_arguments?(node)

def method_call_with_arguments?(node)
  node.call_type? && !node.arguments.empty?
end

def method_with_argument?(current_token, next_token)

argument
do_something \

A method call without parentheses such as the following cannot remove `\`:
def method_with_argument?(current_token, next_token)
  current_token.type == :tIDENTIFIER && ARGUMENT_TYPES.include?(next_token.type)
end

def on_new_investigation

def on_new_investigation
  return unless processed_source.ast
  each_match_range(processed_source.ast.source_range, /(\\\n)/) do |range|
    next if require_line_continuation?(range)
    next unless redundant_line_continuation?(range)
    add_offense(range) do |corrector|
      corrector.remove_leading(range, 1)
    end
  end
end

def redundant_line_continuation?(range)

def redundant_line_continuation?(range)
  return true unless (node = find_node_for_line(range.line))
  return false if argument_newline?(node)
  source = node.parent ? node.parent.source : node.source
  parse(source.gsub(/\\\n/, "\n")).valid_syntax?
end

def require_line_continuation?(range)

def require_line_continuation?(range)
  !ends_with_backslash_without_comment?(range.source_line) ||
    string_concatenation?(range.source_line) ||
    start_with_arithmetic_operator?(processed_source[range.line]) ||
    inside_string_literal_or_method_with_argument?(range)
end

def same_line?(node, line)

def same_line?(node, line)
  return unless (source_range = node.source_range)
  if node.is_a?(AST::StrNode)
    if node.heredoc?
      (node.loc.heredoc_body.line..node.loc.heredoc_body.last_line).cover?(line)
    else
      (source_range.line..source_range.last_line).cover?(line)
    end
  else
    source_range.line == line
  end
end

def start_with_arithmetic_operator?(source_line)

def start_with_arithmetic_operator?(source_line)
  %r{\A\s*[+\-*/%]}.match?(source_line)
end

def string_concatenation?(source_line)

def string_concatenation?(source_line)
  /["']\s*\\\z/.match?(source_line)
end