Skip to content

Commit

Permalink
Merge pull request #11936 from FnControlRuby/dir_empty
Browse files Browse the repository at this point in the history
Improve error message for `Style/DirEmpty`
  • Loading branch information
koic committed Jun 7, 2023
2 parents e3706b5 + b2aad87 commit 13a39cc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
22 changes: 8 additions & 14 deletions lib/rubocop/cop/style/dir_empty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,34 @@ class DirEmpty < Base
extend AutoCorrector
extend TargetRubyVersion

MSG = 'Use `Dir.empty?(%<arg>s)` instead.'
RESTRICT_ON_SEND = %i[== > empty? none?].freeze
MSG = 'Use `%<replacement>s` instead.'
RESTRICT_ON_SEND = %i[== != > empty? none?].freeze

minimum_target_ruby_version 2.4

# @!method offensive?(node)
def_node_matcher :offensive?, <<~PATTERN
{
(send (send (send $(const {nil? cbase} :Dir) :entries $_) :size) {:== :>} (int 2))
(send (send (send $(const {nil? cbase} :Dir) :children $_) :size) {:== :>} (int 0))
(send (send (send (send $(const {nil? cbase} :Dir) :entries $_) :size) :!) {:== :>} (int 2))
(send (send (send (send $(const {nil? cbase} :Dir) :children $_) :size) :!) {:== :>} (int 0))
(send (send (send $(const {nil? cbase} :Dir) :entries $_) :size) {:== :!= :>} (int 2))
(send (send (send $(const {nil? cbase} :Dir) :children $_) :size) {:== :!= :>} (int 0))
(send (send $(const {nil? cbase} :Dir) :children $_) :empty?)
(send (send $(const {nil? cbase} :Dir) :each_child $_) :none?)
}
PATTERN

def on_send(node)
offensive?(node) do |const_node, arg_node|
add_offense(node, message: format(MSG, arg: arg_node.source)) do |corrector|
bang(node)
corrector.replace(node,
"#{bang(node)}#{const_node.source}.empty?(#{arg_node.source})")
replacement = "#{bang(node)}#{const_node.source}.empty?(#{arg_node.source})"
add_offense(node, message: format(MSG, replacement: replacement)) do |corrector|
corrector.replace(node, replacement)
end
end
end

private

def bang(node)
if (node.method?(:==) && node.child_nodes.first.method?(:!)) ||
(node.method?(:>) && !node.child_nodes.first.method?(:!))
'!'
end
'!' if %i[!= >].include? node.method_name
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/rubocop/cop/style/dir_empty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
RUBY
end

it 'registers an offense for `!Dir.entries.size == 2`' do
it 'registers an offense for `Dir.entries.size != 2`' do
expect_offense(<<~RUBY)
!Dir.entries('path/to/dir').size == 2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Dir.empty?('path/to/dir')` instead.
Dir.entries('path/to/dir').size != 2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!Dir.empty?('path/to/dir')` instead.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -27,7 +27,7 @@
it 'registers an offense for `Dir.entries.size > 2`' do
expect_offense(<<~RUBY)
Dir.entries('path/to/dir').size > 2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Dir.empty?('path/to/dir')` instead.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!Dir.empty?('path/to/dir')` instead.
RUBY

expect_correction(<<~RUBY)
Expand Down

0 comments on commit 13a39cc

Please sign in to comment.