Skip to content

Commit

Permalink
Add new InternalAffairs/RedundantSourceRange cop
Browse files Browse the repository at this point in the history
This PR adds new `InternalAffairs/RedundantSourceRange` cop

It checks for redundant `source_range`.

```ruby
# bad
node.source_range.source

# good
node.source
```

This PR also adds `RuboCop::Ext::Comment#source` so that `Parser::Source::Comment`
can be treated like `Node#source`.
  • Loading branch information
koic authored and bbatsov committed Mar 1, 2023
1 parent eec53fa commit 528f397
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/bundler/gem_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def preceding_lines(node)
end

def preceding_comment?(node1, node2)
node1 && node2 && precede?(node2, node1) && comment_line?(node2.source_range.source)
node1 && node2 && precede?(node2, node1) && comment_line?(node2.source)
end

def ignored_gem?(node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/correctors/line_break_corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def break_line_before(range:, node:, corrector:, configured_width:,
def move_comment(eol_comment:, node:, corrector:)
return unless eol_comment

text = eol_comment.source_range.source
text = eol_comment.source
corrector.insert_before(node, "#{text}\n#{' ' * node.loc.keyword.column}")
corrector.remove(eol_comment)
end
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/internal_affairs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require_relative 'internal_affairs/redundant_location_argument'
require_relative 'internal_affairs/redundant_message_argument'
require_relative 'internal_affairs/redundant_method_dispatch_node'
require_relative 'internal_affairs/redundant_source_range'
require_relative 'internal_affairs/single_line_comparison'
require_relative 'internal_affairs/style_detected_api_use'
require_relative 'internal_affairs/undefined_config'
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/internal_affairs/cop_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def suggestion_for_message(suggestion, match_data)
end

def first_comment_line(node)
node.source_range.source.lines.find { |line| comment_line?(line) }
node.source.lines.find { |line| comment_line?(line) }
end

def comment_body(comment_line)
Expand All @@ -86,7 +86,7 @@ def comment_body(comment_line)

def comment_index(node, comment_line)
body = comment_body(comment_line)
node.source_range.source.index(body)
node.source.index(body)
end

def relevant_file?(file)
Expand Down
39 changes: 39 additions & 0 deletions lib/rubocop/cop/internal_affairs/redundant_source_range.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module RuboCop
module Cop
module InternalAffairs
# Checks for redundant `source_range`.
#
# @example
#
# # bad
# node.source_range.source
#
# # good
# node.source
#
class RedundantSourceRange < Base
extend AutoCorrector

MSG = 'Remove the redundant `source_range`.'
RESTRICT_ON_SEND = %i[source].freeze

# @!method redundant_source_range(node)
def_node_matcher :redundant_source_range, <<~PATTERN
(send $(send _ :source_range) :source)
PATTERN

def on_send(node)
return unless (source_range = redundant_source_range(node))

selector = source_range.loc.selector

add_offense(selector) do |corrector|
corrector.remove(source_range.loc.dot.join(selector))
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/heredoc_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def adjust_squiggly(corrector, node)
end

def adjust_minus(corrector, node)
heredoc_beginning = node.source_range.source
heredoc_beginning = node.source
corrected = heredoc_beginning.sub(/<<-?/, '<<~')
corrector.replace(node, corrected)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/trailing_whitespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def whitespace_only?(range)
end

def static?(heredoc)
heredoc.source_range.source.end_with? "'"
heredoc.source.end_with? "'"
end

def skip_heredoc?
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/empty_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def allow_comment?(node)
return false unless processed_source.contains_comment?(node.source_range)

line_comment = processed_source.comment_at_line(node.source_range.line)
!line_comment || !comment_disables_cop?(line_comment.source_range.source)
!line_comment || !comment_disables_cop?(line_comment.source)
end

def allow_empty_lambdas?
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/lint/redundant_cop_enable_directive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def cop_name_indention(comment, name)
end

def range_with_comma(comment, name)
source = comment.source_range.source
source = comment.source

begin_pos = cop_name_indention(comment, name)
end_pos = begin_pos + name.size
Expand All @@ -94,7 +94,7 @@ def range_with_comma(comment, name)

def range_to_remove(begin_pos, end_pos, comment)
start = comment_start(comment)
source = comment.source_range.source
source = comment.source

if source[begin_pos - 1] == ','
range_with_comma_before(start, begin_pos, end_pos)
Expand All @@ -112,7 +112,7 @@ def range_with_comma_before(start, begin_pos, end_pos)
# If the list of cops is comma-separated, but without a empty space after the comma,
# we should **not** remove the prepending empty space, thus begin_pos += 1
def range_with_comma_after(comment, start, begin_pos, end_pos)
begin_pos += 1 if comment.source_range.source[end_pos + 1] != ' '
begin_pos += 1 if comment.source[end_pos + 1] != ' '

range_between(start + begin_pos, start + end_pos + 1)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/documentation_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def documentation_comment?(node)
# The args node1 & node2 may represent a RuboCop::AST::Node
# or a Parser::Source::Comment. Both respond to #loc.
def preceding_comment?(node1, node2)
node1 && node2 && precede?(node2, node1) && comment_line?(node2.source_range.source)
node1 && node2 && precede?(node2, node1) && comment_line?(node2.source)
end

# The args node1 & node2 may represent a RuboCop::AST::Node
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/hash_transform_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def set_new_arg_name(transformed_argname, corrector)
end

def set_new_body_expression(transforming_body_expr, corrector)
body_source = transforming_body_expr.source_range.source
body_source = transforming_body_expr.source
if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
body_source = "{ #{body_source} }"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/mixin/statement_modifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def first_line_comment(node)
comment = processed_source.find_comment { |c| same_line?(c, node) }
return unless comment

comment_source = comment.source_range.source
comment_source = comment.source
comment_source unless comment_disables_cop?(comment_source)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/multiline_ternary_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def replacement(node)

def comments_in_condition(node)
comments_in_range(node).map do |comment|
"#{comment.source_range.source}\n"
"#{comment.source}\n"
end.join
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/redundant_interpolation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def embedded_in_percent_array?(node)
end

def autocorrect_variable_interpolation(corrector, embedded_node, node)
replacement = "#{embedded_node.source_range.source}.to_s"
replacement = "#{embedded_node.source}.to_s"

corrector.replace(node, replacement)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/redundant_string_escape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def percent_array_literal?(node)

def heredoc_with_disabled_interpolation?(node)
if heredoc?(node)
node.source_range.source.end_with?("'")
node.source.end_with?("'")
elsif node.parent&.dstr_type?
heredoc_with_disabled_interpolation?(node.parent)
else
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/ext/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module RuboCop
module Ext
# Extensions to `Parser::Source::Comment`.
module Comment
def source
loc.expression.source
end

def source_range
loc.expression
end
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/internal_affairs/redundant_source_range_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::InternalAffairs::RedundantSourceRange, :config do
it 'registers an offense when using `node.source_range.source`' do
expect_offense(<<~RUBY)
node.source_range.source
^^^^^^^^^^^^ Remove the redundant `source_range`.
RUBY

expect_correction(<<~RUBY)
node.source
RUBY
end

it 'does not register an offense when using `node.source`' do
expect_no_offenses(<<~RUBY)
node.source
RUBY
end
end

0 comments on commit 528f397

Please sign in to comment.