Skip to content

Commit

Permalink
Merge pull request #10169 from dvandersluis/same-line
Browse files Browse the repository at this point in the history
Add more cases to `InternalAffairs/LocationLineEqualityComparison`
  • Loading branch information
koic committed Oct 8, 2021
2 parents a255b80 + 976f71d commit 8c98603
Show file tree
Hide file tree
Showing 24 changed files with 89 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module InternalAffairs
# # bad
# node.loc.line == node.parent.loc.line
#
# # bad
# node.loc.first_line == node.parent.loc.first_line
#
# # good
# same_line?(node, node.parent)
#
Expand All @@ -17,26 +20,40 @@ class LocationLineEqualityComparison < Base

MSG = 'Use `%<preferred>s`.'

# @!method line_send(node)
def_node_matcher :line_send, <<~PATTERN
{
(send (send _ {:loc :source_range}) {:line :first_line})
(send _ :first_line)
}
PATTERN

# @!method location_line_equality_comparison?(node)
def_node_matcher :location_line_equality_comparison?, <<~PATTERN
(send
(send (send _ :loc) :line) :==
(send (send _ :loc) :line))
(send #line_send :== #line_send)
PATTERN

def on_send(node)
return unless location_line_equality_comparison?(node)

lhs, _op, rhs = *node

lhs_receiver = lhs.receiver.receiver.source
rhs_receiver = rhs.receiver.receiver.source
lhs_receiver = extract_receiver(lhs)
rhs_receiver = extract_receiver(rhs)
preferred = "same_line?(#{lhs_receiver}, #{rhs_receiver})"

add_offense(node, message: format(MSG, preferred: preferred)) do |corrector|
corrector.replace(node, preferred)
end
end

private

def extract_receiver(node)
receiver = node.receiver
receiver = receiver.receiver if receiver.method?(:loc) || receiver.method?(:source_range)
receiver.source
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/assignment_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AssignmentIndentation < Base
def check_assignment(node, rhs)
return unless rhs
return unless node.loc.operator
return if node.loc.operator.line == rhs.first_line
return if same_line?(node.loc.operator, rhs)

base = display_column(leftmost_multiple_assignment(node).source_range)
check_alignment([rhs], base + configured_indentation_width)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/empty_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def investigate(comments)

def autocorrect(corrector, node)
previous_token = previous_token(node)
range = if previous_token && node.loc.line == previous_token.line
range = if previous_token && same_line?(node, previous_token)
range_with_surrounding_space(range: node.loc.expression, newlines: false)
else
range_by_whole_lines(node.loc.expression, include_final_newline: true)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/first_array_element_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def check(array_node, left_parenthesis)
left_bracket = array_node.loc.begin
first_elem = array_node.values.first
if first_elem
return if first_elem.source_range.line == left_bracket.line
return if same_line?(first_elem, left_bracket)

check_first(first_elem, left_bracket, left_parenthesis, 0)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/first_hash_element_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def check(hash_node, left_parenthesis)
first_pair = hash_node.pairs.first

if first_pair
return if first_pair.first_line == left_brace.line
return if same_line?(first_pair, left_brace)

if separator_style?(first_pair)
check_based_on_longest_key(hash_node, left_brace, left_parenthesis)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/first_parameter_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def check(def_node)
left_parenthesis = def_node.arguments.loc.begin
first_elem = def_node.arguments.first
return unless first_elem
return if first_elem.source_range.line == left_parenthesis.line
return if same_line?(first_elem, left_parenthesis)

check_first(first_elem, left_parenthesis, nil, 0)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/hash_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def autocorrect_incompatible_with_other_cops?(node)

parent_loc = node.parent.loc
selector = parent_loc.selector || parent_loc.expression
selector.line == node.pairs.first.loc.line
same_line?(selector, node.pairs.first)
end

def reset!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def subsequent_closing_parentheses_in_same_line?(outermost_send)

end_of_outer_send = outermost_send.loc.end

end_of_outer_send.line == end_of_last_arg_of_outer_send.line &&
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

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/indentation_width.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def skip_check?(base_loc, body_node)
return true unless body_node

# Don't check if expression is on same line as "then" keyword, etc.
return true if body_node.loc.line == base_loc.line
return true if same_line?(body_node, base_loc)

return true if starts_with_access_modifier?(body_node)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/line_length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def breakable_range_after_semicolon(semicolon_token)
range = semicolon_token.pos
end_pos = range.end_pos
next_range = range_between(end_pos, end_pos + 1)
return nil unless next_range.line == range.line
return nil unless same_line?(next_range, range)

next_char = next_range.source
return nil if /[\r\n]/.match?(next_char)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/multiline_assignment_layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def check_by_enforced_style(node, rhs)
end

def check_new_line_offense(node, rhs)
return unless node.loc.operator.line == rhs.first_line
return unless same_line?(node.loc.operator, rhs)

add_offense(node, message: NEW_LINE_OFFENSE) do |corrector|
corrector.insert_after(node.loc.operator, "\n")
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/layout/multiline_block_layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def on_block(node)
add_offense_for_expression(node, node.arguments, ARG_MSG)
end

return unless node.body && node.loc.begin.line == node.body.first_line
return unless node.body && same_line?(node.loc.begin, node.body)

add_offense_for_expression(node, node.body, MSG)
end
Expand Down Expand Up @@ -110,7 +110,7 @@ def autocorrect(corrector, node)

expr_before_body ||= node.loc.begin

return unless expr_before_body.line == node.body.first_line
return unless same_line?(expr_before_body, node.body)

autocorrect_body(corrector, node, node.body)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def relevant_node?(send_node)
def right_hand_side(send_node)
dot = send_node.loc.dot
selector = send_node.loc.selector
if send_node.dot? && selector && dot.line == selector.line
if send_node.dot? && selector && same_line?(dot, selector)
dot.join(selector)
elsif selector
selector
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/rescue_ensure_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def check(node)
alignment_loc = alignment_location(alignment_node)
kw_loc = node.loc.keyword

return if alignment_loc.column == kw_loc.column || alignment_loc.line == kw_loc.line
return if alignment_loc.column == kw_loc.column || same_line?(alignment_loc, kw_loc)

add_offense(
kw_loc, message: format_message(alignment_node, alignment_loc, kw_loc)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/space_before_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SpaceBeforeComment < Base
def on_new_investigation
processed_source.sorted_tokens.each_cons(2) do |token1, token2|
next unless token2.comment?
next unless token1.line == token2.line
next unless same_line?(token1, token2)
next unless token1.pos.end == token2.pos.begin

range = token2.pos
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/else_layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def check_else(node)
first_else = else_branch.begin_type? ? else_branch.children.first : else_branch

return unless first_else
return unless first_else.source_range.line == node.loc.else.line
return unless same_line?(first_else, node.loc.else)

add_offense(first_else) { |corrector| autocorrect(corrector, node, first_else) }
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/mixin/end_keyword_alignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def check_end_kw_alignment(node, align_ranges)

def matching_ranges(end_loc, align_ranges)
align_ranges.select do |_, range|
range.line == end_loc.line ||
column_offset_between(range, end_loc).zero?
same_line?(range, end_loc) || column_offset_between(range, end_loc).zero?
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/multiline_element_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def each_argument_node(node, type)
node.arguments.each do |arg|
on_node(type, arg, :send) do |type_node|
left_brace = type_node.loc.begin
if left_brace && left_brace.line == left_parenthesis.line
if left_brace && same_line?(left_brace, left_parenthesis)
yield type_node, left_parenthesis
ignore_node(type_node)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def children(node)
# This method depends on the fact that we have guarded
# against implicit and empty literals.
def opening_brace_on_same_line?(node)
node.loc.begin.line == children(node).first.first_line
same_line?(node.loc.begin, children(node).first)
end

# This method depends on the fact that we have guarded
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/space_after_punctuation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def each_missing_space(tokens)
end

def space_missing?(token1, token2)
token1.line == token2.line && token2.column == token1.column + offset
same_line?(token1, token2) && token2.column == token1.column + offset
end

def space_required_before?(token)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/space_before_punctuation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def each_missing_space(tokens)
end

def space_missing?(token1, token2)
token1.line == token2.line && token2.begin_pos > token1.end_pos
same_line?(token1, token2) && token2.begin_pos > token1.end_pos
end

def space_required_after?(token)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/trailing_body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def trailing_body?(node)
end

def body_on_first_line?(node, body)
node.source_range.first_line == body.source_range.first_line
same_line?(node, body)
end

def first_part_of(body)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/line_end_concatenation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def check_token_set(index)

return unless eligible_token_set?(predecessor, operator, successor)

return if operator.line == successor.line
return if same_line?(operator, successor)

next_successor = token_after_last_string(successor, index)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,50 @@
RUBY
end

it 'registers and corrects an offense when comparing `#loc.source_range` with LHS and RHS' do
expect_offense(<<~RUBY)
node.source_range.line == node.parent.source_range.line
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `same_line?(node, node.parent)`.
RUBY

expect_correction(<<~RUBY)
same_line?(node, node.parent)
RUBY
end

it 'registers an offense and corrects when using `loc.first_line`' do
expect_offense(<<~RUBY)
node.loc.line == node.parent.loc.first_line
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `same_line?(node, node.parent)`.
RUBY

expect_correction(<<~RUBY)
same_line?(node, node.parent)
RUBY
end

it 'registers an offense and corrects when using `source_range.first_line`' do
expect_offense(<<~RUBY)
node.source_range.line == node.parent.source_range.first_line
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `same_line?(node, node.parent)`.
RUBY

expect_correction(<<~RUBY)
same_line?(node, node.parent)
RUBY
end

it 'registers an offense and corrects when using `first_line`' do
expect_offense(<<~RUBY)
node.loc.line == node.parent.first_line
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `same_line?(node, node.parent)`.
RUBY

expect_correction(<<~RUBY)
same_line?(node, node.parent)
RUBY
end

it 'does not register an offense when using `same_line?`' do
expect_no_offenses(<<~RUBY)
same_line?(node, node.parent)
Expand Down

0 comments on commit 8c98603

Please sign in to comment.