class RuboCop::Cop::Layout::HeredocArgumentClosingParenthesis

Checks for the placement of the closing parenthesis in a method call that passes a HEREDOC string as an argument. It should be placed at the end of the line containing the opening HEREDOC tag.

@example

# bad

   foo(<<-SQL
     bar
   SQL
   )

   foo(<<-SQL, 123, <<-NOSQL,
     bar
   SQL
     baz
   NOSQL
   )

   foo(
     bar(<<-SQL
       baz
     SQL
     ),
     123,
   )

# good

   foo(<<-SQL)
     bar
   SQL

   foo(<<-SQL, 123, <<-NOSQL)
     bar
   SQL
     baz
   NOSQL

   foo(
     bar(<<-SQL),
       baz
     SQL
     123,
   )

Constants

MSG

Public Class Methods

autocorrect_incompatible_with() click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 60
def self.autocorrect_incompatible_with
  [Style::TrailingCommaInArguments]
end

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 64
def on_send(node)
  heredoc_arg = extract_heredoc_argument(node)
  return unless heredoc_arg

  outermost_send = outermost_send_on_same_line(heredoc_arg)
  return unless outermost_send
  return unless outermost_send.loc.end
  return unless heredoc_arg.first_line != outermost_send.loc.end.line
  return if subsequent_closing_parentheses_in_same_line?(outermost_send)
  return if exist_argument_between_heredoc_end_and_closing_parentheses?(node)

  add_offense(outermost_send.loc.end) do |corrector|
    autocorrect(corrector, outermost_send)
  end
end

Private Instance Methods

add_correct_closing_paren(node, corrector) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 179
def add_correct_closing_paren(node, corrector)
  corrector.insert_after(node.arguments.last, ')')
end
add_correct_external_trailing_comma(node, corrector) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 265
def add_correct_external_trailing_comma(node, corrector)
  return unless external_trailing_comma?(node)

  corrector.insert_after(node.arguments.last, ',')
end
autocorrect(corrector, node) click to toggle source

Autocorrection note:

Commas are a bit tricky to handle when the method call is embedded in another expression. Here’s an example:

[

first_array_value,
foo(<<-SQL, 123, 456,
  SELECT * FROM db
SQL
),
third_array_value,

]

The “internal” trailing comma is after ‘456`. The “external” trailing comma is after `)`.

To autocorrect, we remove the latter, and move the former up:

[

first_array_value,
foo(<<-SQL, 123, 456),
  SELECT * FROM db
SQL
third_array_value,

]

# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 108
def autocorrect(corrector, node)
  fix_closing_parenthesis(node, corrector)

  remove_internal_trailing_comma(node, corrector) if internal_trailing_comma?(node)

  fix_external_trailing_comma(node, corrector) if external_trailing_comma?(node)
end
exist_argument_between_heredoc_end_and_closing_parentheses?(node) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 217
def exist_argument_between_heredoc_end_and_closing_parentheses?(node)
  return false unless (heredoc_end = find_most_bottom_of_heredoc_end(node.arguments))

  heredoc_end < node.loc.end.begin_pos &&
    range_between(heredoc_end, node.loc.end.begin_pos).source.strip != ''
end
external_trailing_comma?(node) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 283
def external_trailing_comma?(node)
  !external_trailing_comma_offset_from_loc_end(node).nil?
end
external_trailing_comma_offset_from_loc_end(node) click to toggle source

Returns nil if no trailing external comma.

# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 288
def external_trailing_comma_offset_from_loc_end(node)
  end_pos = node.source_range.end_pos
  offset = 0
  limit = 20
  offset += 1 while offset < limit && space?(end_pos + offset)
  char = processed_source.buffer.source[end_pos + offset]
  return unless char == ','

  offset + 1 # Add one to include the comma.
end
extract_heredoc(node) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 138
def extract_heredoc(node)
  return node if heredoc_node?(node)
  return node.receiver if single_line_send_with_heredoc_receiver?(node)

  return unless node.hash_type?

  node.values.find do |v|
    heredoc = extract_heredoc(v)
    return heredoc if heredoc
  end
end
extract_heredoc_argument(node) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 134
def extract_heredoc_argument(node)
  node.arguments.find { |arg_node| extract_heredoc(arg_node) }
end
find_most_bottom_of_heredoc_end(arguments) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 224
def find_most_bottom_of_heredoc_end(arguments)
  arguments.map do |argument|
    argument.loc.heredoc_end.end_pos if argument.loc.respond_to?(:heredoc_end)
  end.compact.max
end
fix_closing_parenthesis(node, corrector) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 174
def fix_closing_parenthesis(node, corrector)
  remove_incorrect_closing_paren(node, corrector)
  add_correct_closing_paren(node, corrector)
end
fix_external_trailing_comma(node, corrector) click to toggle source

External trailing comma helpers.

# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 260
def fix_external_trailing_comma(node, corrector)
  remove_incorrect_external_trailing_comma(node, corrector)
  add_correct_external_trailing_comma(node, corrector)
end
heredoc_node?(node) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 150
def heredoc_node?(node)
  node.respond_to?(:heredoc?) && node.heredoc?
end
incorrect_parenthesis_removal_begin(node) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 192
def incorrect_parenthesis_removal_begin(node)
  end_pos = node.source_range.end_pos
  if safe_to_remove_line_containing_closing_paren?(node)
    last_line_length = node.source.scan(/\n(.*)$/).last[0].size
    end_pos - last_line_length - 1 # Add one for the line break itself.
  else
    end_pos - 1 # Just the `)` at the end of the string
  end
end
incorrect_parenthesis_removal_end(node) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 208
def incorrect_parenthesis_removal_end(node)
  end_pos = node.source_range.end_pos
  if processed_source.buffer.source[end_pos] == ','
    end_pos + 1
  else
    end_pos
  end
end
internal_trailing_comma?(node) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 238
def internal_trailing_comma?(node)
  !internal_trailing_comma_offset_from_last_arg(node).nil?
end
internal_trailing_comma_offset_from_last_arg(node) click to toggle source

Returns nil if no trailing internal comma.

# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 243
def internal_trailing_comma_offset_from_last_arg(node)
  source_after_last_arg = range_between(
    node.children.last.source_range.end_pos,
    node.loc.end.begin_pos
  ).source

  first_comma_offset = source_after_last_arg.index(',')
  first_new_line_offset = source_after_last_arg.index("\n")
  return if first_comma_offset.nil?
  return if first_new_line_offset.nil?
  return if first_comma_offset > first_new_line_offset

  first_comma_offset + 1
end
outermost_send_on_same_line(heredoc) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 116
def outermost_send_on_same_line(heredoc)
  previous = heredoc
  current = previous.parent
  until send_missing_closing_parens?(current, previous, heredoc)
    previous = current
    current = current.parent
    return unless previous && current
  end
  current
end
remove_incorrect_closing_paren(node, corrector) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 183
def remove_incorrect_closing_paren(node, corrector)
  corrector.remove(
    range_between(
      incorrect_parenthesis_removal_begin(node),
      incorrect_parenthesis_removal_end(node)
    )
  )
end
remove_incorrect_external_trailing_comma(node, corrector) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 271
def remove_incorrect_external_trailing_comma(node, corrector)
  end_pos = node.source_range.end_pos
  return unless external_trailing_comma?(node)

  corrector.remove(
    range_between(
      end_pos,
      end_pos + external_trailing_comma_offset_from_loc_end(node)
    )
  )
end
remove_internal_trailing_comma(node, corrector) click to toggle source

Internal trailing comma helpers.

# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 232
def remove_internal_trailing_comma(node, corrector)
  offset = internal_trailing_comma_offset_from_last_arg(node)
  last_arg_end_pos = node.children.last.source_range.end_pos
  corrector.remove(range_between(last_arg_end_pos, last_arg_end_pos + offset))
end
safe_to_remove_line_containing_closing_paren?(node) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 202
def safe_to_remove_line_containing_closing_paren?(node)
  last_line = processed_source[node.loc.end.line - 1]
  # Safe to remove if last line only contains `)`, `,`, and whitespace.
  last_line.match?(/^ *\) {0,20},{0,1} *$/)
end
send_missing_closing_parens?(parent, child, heredoc) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 127
def send_missing_closing_parens?(parent, child, heredoc)
  parent&.call_type? &&
    parent.arguments.include?(child) &&
    parent.loc.begin &&
    parent.loc.end.line != heredoc.last_line
end
single_line_send_with_heredoc_receiver?(node) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 154
def single_line_send_with_heredoc_receiver?(node)
  return false unless node.send_type?
  return false unless heredoc_node?(node.receiver)

  node.receiver.location.heredoc_end.end_pos > node.source_range.end_pos
end
space?(pos) click to toggle source
# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 299
def space?(pos)
  processed_source.buffer.source[pos] == ' '
end
subsequent_closing_parentheses_in_same_line?(outermost_send) click to toggle source

Closing parenthesis helpers.

# File lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb, line 163
def subsequent_closing_parentheses_in_same_line?(outermost_send)
  last_arg_of_outer_send = outermost_send.last_argument
  return false unless last_arg_of_outer_send&.loc.respond_to?(:end) &&
                      (end_of_last_arg_of_outer_send = last_arg_of_outer_send.loc.end)

  end_of_outer_send = outermost_send.loc.end

  same_line?(end_of_outer_send, end_of_last_arg_of_outer_send) &&
    end_of_outer_send.column == end_of_last_arg_of_outer_send.column + 1
end