Skip to content

Commit

Permalink
Merge pull request #17 from ydah/develop
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
ydah committed Feb 18, 2022
2 parents d6126da + fb4e9ad commit 354f417
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 48 deletions.
32 changes: 17 additions & 15 deletions lib/rubocop/safe_todo_searcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@

module Rubocop
module SafeTodoSearcher
class Error < StandardError; end
class << self
def search
File.exist?(".rubocop_todo.yml") ? parse : "rubocop_todo.yml does not exist"
end

def self.search
File.exist?(".rubocop_todo.yml") ? parse : "rubocop_todo.yml does not exist"
end
private

def self.parse
res = +""
File.open(".rubocop_todo.yml", "r") { |f| YAML.safe_load(f) }.each_key do |key|
res << "#{key}\n" if support_autocorrect?(key)
def parse
res = +""
File.open(".rubocop_todo.yml", "r") { |f| YAML.safe_load(f) }.each_key do |key|
res << "#{key}\n" if support_autocorrect?(key)
end
res
end
res
end

def self.support_autocorrect?(key)
cop = Object.const_get "RuboCop::Cop::#{key.gsub(%r{/}, "::")}"
cop.support_autocorrect? && cop.new(RuboCop::ConfigLoader.default_configuration).safe_autocorrect?
rescue NameError
false
def support_autocorrect?(key)
cop = Object.const_get "RuboCop::Cop::#{key.gsub(%r{/}, "::")}"
cop.support_autocorrect? && cop.new(RuboCop::ConfigLoader.default_configuration).safe_autocorrect?
rescue NameError
false
end
end
end
end
33 changes: 0 additions & 33 deletions spec/rubocop/safe_todo_searcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,4 @@
expect(Rubocop::SafeTodoSearcher::VERSION).not_to be nil
end
end

describe "support_autocorrect?" do
context "auto-correct is supported" do
it "returns true" do
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Bundler/OrderedGems")).to be true
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Gemspec/DateAssignment")).to be true
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Layout/AccessModifierIndentation")).to be true
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Lint/AmbiguousOperatorPrecedence")).to be true
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Migration/DepartmentName")).to be true
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Naming/BinaryOperatorParameterName")).to be true
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Style/AccessorGrouping")).to be true
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Rails/ActionFilter")).to be true
end
end

context "auto-correct is not supported" do
it "returns false" do
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Bundler/DuplicatedGem")).to be false
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Layout/EndOfLine")).to be false
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Lint/AmbiguousAssignment")).to be false
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Naming/AccessorMethodName")).to be false
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Security/Eval")).to be false
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Style/AccessModifierDeclarations")).to be false
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Rails/AfterCommitOverride")).to be false
end
end

context "nonexistent cops" do
it "returns false" do
expect(Rubocop::SafeTodoSearcher.support_autocorrect?("Invalid/NonExistentCops")).to be false
end
end
end
end

0 comments on commit 354f417

Please sign in to comment.