Skip to content

Commit

Permalink
adds rubocop rule and test
Browse files Browse the repository at this point in the history
  • Loading branch information
zgoldman-r7 committed May 30, 2024
1 parent 2fb35f9 commit 0e88d90
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require:
- ./lib/rubocop/cop/lint/module_disclosure_date_format.rb
- ./lib/rubocop/cop/lint/module_disclosure_date_present.rb
- ./lib/rubocop/cop/lint/deprecated_gem_version.rb
- ./lib/rubocop/cop/lint/deprecated_file_exists.rb
- ./lib/rubocop/cop/lint/module_enforce_notes.rb
- ./lib/rubocop/cop/lint/detect_invalid_pack_directives.rb

Expand Down
46 changes: 46 additions & 0 deletions lib/rubocop/cop/lint/deprecated_file_exists.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

# frozen_string_literal: true

module RuboCop
module Cop
module Lint
class DeprecatedFileExists < Base
include RangeHelp
extend AutoCorrector

MSG = 'Use `File.exist?` instead of `File.exists?`.'

# @!method gem_version_const(node)
def_node_matcher :file_exists_const, <<~PATTERN
(send
(const {nil? cbase} :File) :exists?)
PATTERN

def_node_matcher :file_exists_const_cbase, <<~PATTERN
(send
(const
(cbase) :File) :exists?)
PATTERN

def on_send(node)
return unless file_exists_const(node)

add_offense(node, message: MSG) do |corrector|
autocorrect(corrector, node)
end
end

private

def autocorrect(corrector, node)
if file_exists_const_cbase(node)
corrector.replace(node, '::File.exist?')
else
corrector.replace(node, 'File.exist?')
end
end

end
end
end
end
31 changes: 31 additions & 0 deletions spec/rubocop/cop/lint/deprecated_file_exists_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'
require 'rubocop/cop/lint/deprecated_file_exists'

RSpec.describe RuboCop::Cop::Lint::DeprecatedFileExists do
subject(:cop) { described_class.new(config) }
let(:empty_rubocop_config) { { } }
let(:config) { RuboCop::Config.new(empty_rubocop_config) }

it 'corrects `File.exists?`' do
expect_offense(<<~RUBY)
File.exists?
^^^^^^^^^^^^ Use `File.exist?` instead of `File.exists?`.
RUBY

expect_correction(<<~RUBY)
File.exist?
RUBY
end


it 'corrects `::File.exists?`' do
expect_offense(<<~RUBY)
::File.exists?
^^^^^^^^^^^^^^ Use `File.exist?` instead of `File.exists?`.
RUBY

expect_correction(<<~RUBY)
::File.exist?
RUBY
end
end

0 comments on commit 0e88d90

Please sign in to comment.