Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Node#loc_is? for easier test of locations #345

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/rubocop/ast/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def operator_keyword?
end

def parenthesized_call?
loc.respond_to?(:begin) && loc.begin&.is?('(')
loc_is?(:begin, '(')
end

def call_type?
Expand Down Expand Up @@ -534,6 +534,17 @@ def guard_clause?
node.match_guard_clause?
end

# Shortcut to safely test a particular location, even if
# this location does not exist or is `nil`
def loc_is?(which_loc, str)
return false unless loc.respond_to?(which_loc)

location = loc.public_send(which_loc)
return false unless location

location.is?(str)
end

# @!method match_guard_clause?(node = self)
def_node_matcher :match_guard_clause?, <<~PATTERN
[${(send nil? {:raise :fail} ...) return break next} single_line?]
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/array_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def each_value(&block)
#
# @return [Boolean] whether the array is enclosed in square brackets
def square_brackets?
loc.begin&.is?('[')
loc_is?(:begin, '[')
end

# Checks whether the `array` literal is delimited by percent brackets.
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/for_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def keyword
#
# @return [Boolean] whether the `for` node has a `do` keyword
def do?
loc.begin&.is?('do')
loc_is?(:begin, 'do')
end

# Checks whether this node body is a void context.
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/hash_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def mixed_delimiters?
#
# @return [Boolean] whether the `hash` literal is enclosed in braces
def braces?
loc.end&.is?('}')
loc_is?(:end, '}')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/in_pattern_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def branch_index
#
# @return [Boolean] whether the `in` node has a `then` keyword
def then?
loc.begin&.is?('then')
loc_is?(:begin, 'then')
end

# Returns the body of the `in` node.
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/ast/node/mixin/method_dispatch_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,23 @@ def setter_method?
#
# @return [Boolean] whether the method was called with a connecting dot
def dot?
loc.respond_to?(:dot) && loc.dot&.is?('.')
loc_is?(:dot, '.')
end

# Checks whether the dispatched method uses a double colon to connect the
# receiver and the method name.
#
# @return [Boolean] whether the method was called with a connecting dot
def double_colon?
loc.respond_to?(:dot) && loc.dot&.is?('::')
loc_is?(:dot, '::')
end

# Checks whether the dispatched method uses a safe navigation operator to
# connect the receiver and the method name.
#
# @return [Boolean] whether the method was called with a connecting dot
def safe_navigation?
loc.respond_to?(:dot) && loc.dot&.is?('&.')
loc_is?(:dot, '&.')
end

# Checks whether the *explicit* receiver of this method dispatch is
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/mixin/parameterized_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module ParameterizedNode
# @return [Boolean] whether this node's arguments are
# wrapped in parentheses
def parenthesized?
loc.end&.is?(')')
loc_is?(:end, ')')
end

# A shorthand for getting the first argument of the node.
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/str_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class StrNode < Node
include BasicLiteralNode

def character_literal?
loc.respond_to?(:begin) && loc.begin&.is?('?')
loc_is?(:begin, '?')
end

def heredoc?
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/until_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def inverse_keyword
#
# @return [Boolean] whether the `until` node has a `do` keyword
def do?
loc.begin&.is?('do')
loc_is?(:begin, 'do')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/when_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def branch_index
#
# @return [Boolean] whether the `when` node has a `then` keyword
def then?
loc.begin&.is?('then')
loc_is?(:begin, 'then')
end

# Returns the body of the `when` node.
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/while_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def inverse_keyword
#
# @return [Boolean] whether the `until` node has a `do` keyword
def do?
loc.begin&.is?('do')
loc_is?(:begin, 'do')
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/ast/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1109,4 +1109,28 @@ class << expr
end
end
end

describe '#loc_is?' do
let(:src) { '%i[>> sym << sym2]' }

context 'when loc exists' do
let(:src) { ':sym' }

it 'returns true when loc matches argument' do
expect(node).to be_loc_is(:begin, ':')
end

it 'returns false when loc does not match argument' do
expect(node).not_to be_loc_is(:begin, '!')
end
end

it 'returns false when requested loc is `nil`' do
expect(node).not_to be_loc_is(:begin, ':')
end

it 'returns false when requested loc does not exist' do
expect(node).not_to be_loc_is(:foo, ':')
end
end
end
Loading