Skip to content

Commit

Permalink
Use Node#left_sibling(s) and Node#right_sibling(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
koic committed Jul 9, 2021
1 parent 4a3519b commit 2e73f21
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
10 changes: 1 addition & 9 deletions lib/rubocop/cop/rails/active_record_callbacks_order.rb
Expand Up @@ -71,7 +71,7 @@ def on_class(class_node)

# Autocorrect by swapping between two nodes autocorrecting them
def autocorrect(corrector, node)
previous = left_siblings_of(node).reverse_each.find do |sibling|
previous = node.left_siblings.reverse_each.find do |sibling|
callback?(sibling)
end

Expand All @@ -96,14 +96,6 @@ def callback?(node)
node.send_type? && CALLBACKS_ORDER_MAP.key?(node.method_name)
end

def left_siblings_of(node)
siblings_of(node)[0, node.sibling_index]
end

def siblings_of(node)
node.parent.children
end

def source_range_with_comment(node)
begin_pos = begin_pos_with_comment(node)
end_pos = end_position_for(node)
Expand Down
12 changes: 2 additions & 10 deletions lib/rubocop/cop/rails/redundant_allow_nil.rb
Expand Up @@ -56,8 +56,8 @@ def on_send(node)

def register_offense(allow_nil, message)
add_offense(allow_nil, message: message) do |corrector|
prv_sib = previous_sibling(allow_nil)
nxt_sib = next_sibling(allow_nil)
prv_sib = allow_nil.left_sibling
nxt_sib = allow_nil.right_sibling

if nxt_sib
corrector.remove(range_between(node_beg(allow_nil), node_beg(nxt_sib)))
Expand Down Expand Up @@ -88,14 +88,6 @@ def find_allow_nil_and_allow_blank(node)
nil
end

def previous_sibling(node)
node.parent.children[node.sibling_index - 1]
end

def next_sibling(node)
node.parent.children[node.sibling_index + 1]
end

def node_beg(node)
node.loc.expression.begin_pos
end
Expand Down

0 comments on commit 2e73f21

Please sign in to comment.