-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
InternalAffairs/RedundantSourceRange
cop
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
Showing
17 changed files
with
80 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
lib/rubocop/cop/internal_affairs/redundant_source_range.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
spec/rubocop/cop/internal_affairs/redundant_source_range_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |