Skip to content

Commit

Permalink
Add new InternalAffairs/ProcessedSourceBufferName cop
Browse files Browse the repository at this point in the history
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
koic authored and bbatsov committed Feb 22, 2023
1 parent d7ffaca commit d16167e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/rubocop/cop/internal_affairs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require_relative 'internal_affairs/node_type_predicate'
require_relative 'internal_affairs/numblock_handler'
require_relative 'internal_affairs/offense_location_keyword'
require_relative 'internal_affairs/processed_source_buffer_name'
require_relative 'internal_affairs/redundant_context_config_parameter'
require_relative 'internal_affairs/redundant_described_class_as_subject'
require_relative 'internal_affairs/redundant_let_rubocop_config_new'
Expand Down
42 changes: 42 additions & 0 deletions lib/rubocop/cop/internal_affairs/processed_source_buffer_name.rb
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
2 changes: 1 addition & 1 deletion lib/rubocop/cop/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def autocorrect(processed_source, report, original:, offset:)
# holds source read in from stdin, when --stdin option is used
@options[:stdin] = new_source
else
filename = processed_source.buffer.name
filename = processed_source.file_path
File.write(filename, new_source)
end
@updated_source_file = true
Expand Down
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

0 comments on commit d16167e

Please sign in to comment.