From 411be74773b716968eea97f741ce0559041cc0db Mon Sep 17 00:00:00 2001 From: Dhamo1107 Date: Sat, 17 Aug 2024 12:48:51 +0530 Subject: [PATCH] Fixed method_used in analyzer --- .gitignore | 3 ++- .rubocop.yml | 3 +++ lib/un_used_methods/analyzer.rb | 30 ++++++++++++++++++++++++------ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index c51c99f..6e83d6f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ # rspec failure tracking .rspec_status -*.iml \ No newline at end of file +*.iml +*.gem \ No newline at end of file diff --git a/.rubocop.yml b/.rubocop.yml index b79c9ba..505678d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -9,3 +9,6 @@ Style/StringLiteralsInInterpolation: Lint/ScriptPermission: Enabled: false + +Metrics/MethodLength: + Max: 20 diff --git a/lib/un_used_methods/analyzer.rb b/lib/un_used_methods/analyzer.rb index 923771d..453a582 100644 --- a/lib/un_used_methods/analyzer.rb +++ b/lib/un_used_methods/analyzer.rb @@ -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 @@ -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)