Skip to content

Commit

Permalink
Merge pull request #1586 from r7kamura/feature/internal-affairs
Browse files Browse the repository at this point in the history
Auto-correct `InternalAffairs/LocationExpression` offenses
  • Loading branch information
pirj authored Mar 4, 2023
2 parents f6e995c + afd1929 commit d114b99
Show file tree
Hide file tree
Showing 17 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions lib/rubocop/cop/rspec/be_nil.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ def check_be_style(node)
return unless be_nil_matcher?(node)

add_offense(node, message: BE_MSG) do |corrector|
corrector.replace(node.loc.expression, 'be(nil)')
corrector.replace(node.source_range, 'be(nil)')
end
end

def check_be_nil_style(node)
return unless nil_value_expectation?(node)

add_offense(node, message: BE_NIL_MSG) do |corrector|
corrector.replace(node.loc.expression, 'be_nil')
corrector.replace(node.source_range, 'be_nil')
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/rspec/change_by_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def on_send(node)
private

def check_offense(node)
expression = node.loc.expression
expression = node.source_range
if compound_expectations?(node)
add_offense(expression, message: message_compound) do |corrector|
autocorrect_compound(corrector, node)
Expand All @@ -117,7 +117,7 @@ def compound_expectations?(node)

def autocorrect(corrector, node)
corrector.replace(node.parent.loc.selector, 'not_to')
range = node.loc.dot.with(end_pos: node.loc.expression.end_pos)
range = node.loc.dot.with(end_pos: node.source_range.end_pos)
corrector.remove(range)
end

Expand All @@ -126,7 +126,7 @@ def autocorrect_compound(corrector, node)

change_nodes(node) do |change_node|
corrector.replace(change_node.loc.selector, negated_matcher)
range = node.loc.dot.with(end_pos: node.loc.expression.end_pos)
range = node.loc.dot.with(end_pos: node.source_range.end_pos)
corrector.remove(range)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/duplicated_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def autocorrect(corrector, node)
corrector.remove(
range_with_surrounding_comma(
range_with_surrounding_space(
node.location.expression,
node.source_range,
side: :left
),
:left
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/empty_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def examples_in_branches?(condition_node)

def removed_range(node)
range_by_whole_lines(
node.location.expression,
node.source_range,
include_final_newline: true
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/empty_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
empty_hook?(node) do |hook|
add_offense(hook) do |corrector|
corrector.remove(
range_with_surrounding_space(node.loc.expression, side: :left)
range_with_surrounding_space(node.source_range, side: :left)
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/example_wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def add_wording_offense(node, message)
end

def docstring(node)
expr = node.loc.expression
expr = node.source_range

Parser::Source::Range.new(
expr.source_buffer,
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/excessive_docstring_spacing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def add_whitespace_offense(node, text)
end

def docstring(node)
expr = node.loc.expression
expr = node.source_range

Parser::Source::Range.new(
expr.source_buffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def autocorrect_without_parens(corrector, node)
left_braces, right_braces = braces(node)

argument = node.first_argument
expression = argument.location.expression
expression = argument.source_range
corrector.insert_before(expression, left_braces)
corrector.insert_after(expression, right_braces)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/rspec/factory_bot/syntax_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def on_send(node)

def crime_scene(node)
range_between(
node.loc.expression.begin_pos,
node.source_range.begin_pos,
node.loc.selector.end_pos
)
end

def offense(node)
range_between(
node.loc.expression.begin_pos,
node.source_range.begin_pos,
node.loc.selector.begin_pos
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/focus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def focus_metadata(node, &block)

def with_surrounding(focus)
range_with_space =
range_with_surrounding_space(focus.loc.expression, side: :left)
range_with_surrounding_space(focus.source_range, side: :left)

range_with_surrounding_comma(range_with_space, :left)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/rspec/mixin/location_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module LocationHelp
# ^^^^^
def arguments_with_whitespace(node)
node.loc.selector.end.with(
end_pos: node.loc.expression.end_pos
end_pos: node.source_range.end_pos
)
end

Expand All @@ -27,8 +27,8 @@ def block_with_whitespace(node)
return unless (parent = node.parent)
return unless parent.block_type?

node.loc.expression.end.with(
end_pos: parent.loc.expression.end_pos
node.source_range.end.with(
end_pos: parent.source_range.end_pos
)
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/rubocop/cop/rspec/predicate_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def to_predicate_matcher(name)

def remove_predicate(corrector, predicate)
range = predicate.loc.dot.with(
end_pos: predicate.loc.expression.end_pos
end_pos: predicate.source_range.end_pos
)

corrector.remove(range)
Expand All @@ -99,7 +99,7 @@ def rewrite_matcher(corrector, predicate, matcher)
block = block_loc ? block_loc.source : ''

corrector.replace(
matcher.loc.expression,
matcher.source_range,
to_predicate_matcher(predicate.method_name) + args + block
)
end
Expand Down Expand Up @@ -214,7 +214,7 @@ def message_explicit(matcher)

def corrector_explicit(corrector, to_node, actual, matcher, block_child)
replacement_matcher = replacement_matcher(to_node)
corrector.replace(matcher.loc.expression, replacement_matcher)
corrector.replace(matcher.source_range, replacement_matcher)
move_predicate(corrector, actual, matcher, block_child)
corrector.replace(to_node.loc.selector, 'to')
end
Expand All @@ -226,7 +226,7 @@ def move_predicate(corrector, actual, matcher, block_child)
block = block_loc ? block_loc.source : ''

corrector.remove(block_loc) if block_loc
corrector.insert_after(actual.loc.expression,
corrector.insert_after(actual.source_range,
".#{predicate}" + args + block)
end

Expand Down
8 changes: 4 additions & 4 deletions lib/rubocop/cop/rspec/rails/inferred_spec_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ def autocorrect(corrector, node)
# @return [Parser::Source::Range]
def remove_range(node)
if node.left_sibling
node.loc.expression.with(
begin_pos: node.left_sibling.loc.expression.end_pos
node.source_range.with(
begin_pos: node.left_sibling.source_range.end_pos
)
elsif node.right_sibling
node.loc.expression.with(
end_pos: node.right_sibling.loc.expression.begin_pos
node.source_range.with(
end_pos: node.right_sibling.source_range.begin_pos
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/receive_counts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def matcher_for(method, count)

def range(node, offending_node)
offending_node.loc.dot.with(
end_pos: node.loc.expression.end_pos
end_pos: node.source_range.end_pos
)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/rspec/sort_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def crime_scene(symbols, pairs)
metadata = symbols + pairs

range_between(
metadata.first.loc.expression.begin_pos,
metadata.last.loc.expression.end_pos
metadata.first.source_range.begin_pos,
metadata.last.source_range.end_pos
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/variable_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def on_send(node)
return if variable.dstr_type? || variable.dsym_type?
return if matches_allowed_pattern?(variable.value)

check_name(node, variable.value, variable.loc.expression)
check_name(node, variable.value, variable.source_range)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/verified_double_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def on_send(node)
break correct_style_detected unless opposing_style?(class_reference)

message = format(MSG, style: style)
expression = class_reference.loc.expression
expression = class_reference.source_range

add_offense(expression, message: message) do |corrector|
violation = class_reference.children.last.to_s
Expand Down

0 comments on commit d114b99

Please sign in to comment.