-
-
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/ProcessedSourceBufferName
cop
Follow up rubocop/rubocop-minitest#217 (comment). This PR adds new `InternalAffairs/ProcessedSourceBufferName` cop. It enforces the use of `processed_source.file_path` instead of `processed_source.buffer.name`. ```ruby # bad processed_source.buffer.name # good processed_source.file_path ```
- Loading branch information
Showing
4 changed files
with
77 additions
and
1 deletion.
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
42 changes: 42 additions & 0 deletions
42
lib/rubocop/cop/internal_affairs/processed_source_buffer_name.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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module InternalAffairs | ||
# Enforces the use of `processed_source.file_path` instead of `processed_source.buffer.name`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# processed_source.buffer.name | ||
# | ||
# # good | ||
# processed_source.file_path | ||
# | ||
class ProcessedSourceBufferName < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `file_path` instead.' | ||
|
||
RESTRICT_ON_SEND = %i[name].freeze | ||
|
||
# @!method processed_source_buffer_name?(node) | ||
def_node_matcher :processed_source_buffer_name?, <<~PATTERN | ||
(send | ||
(send | ||
{(lvar :processed_source) (send nil? :processed_source)} :buffer) :name) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless processed_source_buffer_name?(node) | ||
|
||
offense_range = node.children.first.loc.selector.begin.join(node.loc.expression.end) | ||
|
||
add_offense(offense_range) do |corrector| | ||
corrector.replace(offense_range, 'file_path') | ||
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
33 changes: 33 additions & 0 deletions
33
spec/rubocop/cop/internal_affairs/processed_source_buffer_name_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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::InternalAffairs::ProcessedSourceBufferName, :config do | ||
it 'registers an offense and corrects when using `processed_source.buffer.name` and `processed_source` is a method call' do | ||
expect_offense(<<~RUBY) | ||
processed_source.buffer.name | ||
^^^^^^^^^^^ Use `file_path` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
processed_source.file_path | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `processed_source.buffer.name` and `processed_source` is a variable' do | ||
expect_offense(<<~RUBY) | ||
processed_source = create_processed_source | ||
processed_source.buffer.name | ||
^^^^^^^^^^^ Use `file_path` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
processed_source = create_processed_source | ||
processed_source.file_path | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `processed_source.file_path`' do | ||
expect_no_offenses(<<~RUBY) | ||
processed_source.file_path | ||
RUBY | ||
end | ||
end |