Skip to content

Commit

Permalink
Fixed method_used in analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhamo1107 committed Aug 17, 2024
1 parent 76ae513 commit 411be74
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@

# rspec failure tracking
.rspec_status
*.iml
*.iml
*.gem
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ Style/StringLiteralsInInterpolation:

Lint/ScriptPermission:
Enabled: false

Metrics/MethodLength:
Max: 20
30 changes: 24 additions & 6 deletions lib/un_used_methods/analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ def find_in_directory(directory)
Dir.glob("#{directory}/**/*.rb").each do |file|
methods = extract_methods(file)
methods.each do |method|
un_used_methods << "#{file}: #{method}" unless method_used?(method)
if method_used?(method)
un_used_methods << "#{file}: #{method}"
p un_used_methods
end
end
end

Expand All @@ -50,13 +53,28 @@ def extract_methods(file)
end

def method_used?(method)
method_pattern = /#{method}/
files = Dir.glob("app/**/*.{rb}")

files.any? do |file|
puts "Checking if method '#{method}' is used..."
# Patterns to detect method calls
method_call_pattern = /#{method}\s*\(/
# Search directories for relevant file types
files = Dir.glob("app/**/*.{rb,html,erb,haml,slim,js,jsx,ts,tsx}") + Dir.glob("lib/**/*.{rb}")
method_defined = false
method_used = false
files.each do |file|
content = File.read(file)
content.match?(method_pattern)
# Check for method definition
method_definition_pattern = /def\s+#{method}\b/
if content =~ method_definition_pattern
method_defined = true
puts "Method '#{method}' defined in file: #{file}"
end
# Check for method usage
if content =~ method_call_pattern
method_used = true
puts "Method '#{method}' called in file: #{file}"
end
end
method_defined && !method_used
end

def report_un_used_methods(unused_methods)
Expand Down

0 comments on commit 411be74

Please sign in to comment.