gravedigger 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 000db92231504a24d3ab6d87569148eafaa6d355af6192ebbb8efa0f8c881392
4
- data.tar.gz: c4242466fcd30cc7a49a429b9e897732e09f441c06c8fc654bb68a12f53e8f1e
3
+ metadata.gz: 9c759391fd2e7994d2817a851ceb048db9ae1fa44e6871bc452c87910acd7b00
4
+ data.tar.gz: 38c08172baa3f34fe9ec5273d7d3748b295650aca62b6a6250169e73d1b7fed6
5
5
  SHA512:
6
- metadata.gz: 545d8998fa99660778cc4686699c8d86d8b0ae6f6db2164f6bf9d7f4917c720db9ee98daeb12383253f3014317592bc1ecca2a5fb9c41211244fedab5c400190
7
- data.tar.gz: f68a4188b3cad386ea55a7da0d45f7603516682f086b3f7243109a0a0968f9664b61f87b8adfac5d14ab44cb0908243472768c2082af9a49397c15825993af97
6
+ metadata.gz: 0db1fea6bb33cc4526feca9635043a2587266a6eb69c514252834d8caa9fbc8ff88997b5c3488ef3797e171839b42a5f69258c0ce03aca21f3fc493da69a98f0
7
+ data.tar.gz: bee13da0788e1224b5422d10fb9b6608a313787fa8109725a9544962b80a433d65873e9cf5eacdbd1eda43c480c0803ed8eaf4bf7c05f3d23751e4386b31391b
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gravedigger (0.1.1)
4
+ gravedigger (0.1.2)
5
5
  thor
6
6
 
7
7
  GEM
@@ -9,12 +9,15 @@ module Gravedigger
9
9
  desc "dig", "Find unused code in your Rails project"
10
10
 
11
11
  def dig
12
- method_definitions, get_definitions_errors = Gravedigger::DefinitionFinder.get_definitions
13
- output, method_search_errors = Gravedigger::UsageSearcher.search_methods(method_definitions)
12
+ files_to_search = Dir[File.join(Dir.pwd,"/{app,lib,config}/**/*.{erb,haml,rb}")]
13
+ method_definitions, variable_definitions, definition_errors = Gravedigger::DefinitionFinder.get_definitions(files_to_search)
14
14
 
15
- errors = get_definitions_errors.concat method_search_errors
15
+ unused_methods, method_search_errors = Gravedigger::UsageSearcher.search_methods(method_definitions, files_to_search)
16
+ unused_variables, variable_search_errors = Gravedigger::UsageSearcher.search_variables(variable_definitions, files_to_search)
16
17
 
17
- Gravedigger::OutputPrinter.print_output(output, errors)
18
+ errors = definition_errors + method_search_errors + variable_search_errors
19
+
20
+ Gravedigger::OutputPrinter.print_output(unused_methods, unused_variables, errors)
18
21
  end
19
22
  end
20
23
  end
@@ -2,26 +2,29 @@
2
2
 
3
3
  module Gravedigger
4
4
  module DefinitionFinder
5
- def self.get_definitions
6
- all_file_names = Dir[File.join(Dir.pwd,"/{app,lib,config}/**/*.{erb,haml,rb}")]
5
+ def self.get_definitions(files_to_search)
7
6
  method_definitions = {}
7
+ variable_definitions = {}
8
8
  errors = []
9
9
 
10
- all_file_names.each do |fileName|
10
+ files_to_search.each do |fileName|
11
11
  begin
12
- File.readlines(fileName).each_with_index do |line,index|
13
- next unless line
14
- method_name = find_method_definitions(line)
15
- location = "[#{fileName}:#{index+1}]"
12
+ File.readlines(fileName).each_with_index do |line,index|
13
+ next unless line
14
+ method_name = find_method_definitions(line)
15
+ variable_name = find_variable_definitions(line)
16
16
 
17
- method_definitions[method_name] = location if method_name
18
- end
17
+ location = "[#{fileName}:#{index+1}]"
18
+
19
+ method_definitions[method_name] = location if method_name
20
+ variable_definitions[variable_name] = location if variable_name
21
+ end
19
22
  rescue StandardError => error
20
23
  errors << "Error while reading file: #{fileName} \n #{error.message}"
21
24
  end
22
25
  end
23
26
 
24
- return method_definitions, errors
27
+ return method_definitions, variable_definitions, errors
25
28
  end
26
29
 
27
30
  def self.find_method_definitions(line)
@@ -32,5 +35,12 @@ module Gravedigger
32
35
  end
33
36
  method_name
34
37
  end
38
+
39
+ def self.find_variable_definitions(line)
40
+ result = line.scan(/^[ ]+([A-Za-z0-9_\!\@]*)[ ]*\=[^\=\>]/)
41
+ return nil if result.empty?
42
+ result[0][0].sub!(/^@/, '')
43
+ result[0][0]
44
+ end
35
45
  end
36
46
  end
@@ -3,18 +3,27 @@
3
3
  module Gravedigger
4
4
  module OutputPrinter
5
5
  NEW_LINE = "\n"
6
- def self.print_output(output, error_messages = [])
7
- print_scan_results(output.length, error_messages.length)
6
+ def self.print_output(unused_methods, unused_variables, error_messages = [])
7
+ print_scan_results(unused_methods.length, unused_variables.length, error_messages.length)
8
8
 
9
- if output.any?
9
+ if unused_methods.any?
10
10
  puts NEW_LINE
11
11
  puts "Unused methods in your project:"
12
12
  puts NEW_LINE
13
- output.each do |method_name, location|
13
+ unused_methods.each do |method_name, location|
14
14
  puts "#{method_name}: #{location}"
15
15
  end
16
16
  end
17
17
 
18
+ if unused_variables.any?
19
+ puts NEW_LINE
20
+ puts "Unused variables in your project:"
21
+ puts NEW_LINE
22
+ unused_variables.each do |variable_name, location|
23
+ puts "#{variable_name}: #{location}"
24
+ end
25
+ end
26
+
18
27
  if error_messages.any?
19
28
  puts NEW_LINE
20
29
  puts "Errors encountered while processing:"
@@ -25,10 +34,11 @@ module Gravedigger
25
34
  end
26
35
  end
27
36
 
28
- def self.print_scan_results(output_length, error_messages_length)
37
+ def self.print_scan_results(unused_methods_length, unused_variables_length, error_messages_length)
29
38
  puts "="*80 + NEW_LINE
30
39
  puts "Scanning completed."
31
- puts print_with_green_color(output_length) + " unused method definitions found."
40
+ puts print_with_green_color(unused_methods_length) + " unused method definitions found."
41
+ puts print_with_green_color(unused_variables_length) + " unused variable definitions found."
32
42
  puts print_with_red_color(error_messages_length) + " errors encountered."
33
43
  puts "="*80 +NEW_LINE
34
44
  end
@@ -2,10 +2,9 @@
2
2
 
3
3
  module Gravedigger
4
4
  module UsageSearcher
5
- def self.search_methods(method_definitions)
6
- all_file_names = Dir[File.join(Dir.pwd,"/{app,lib,config}/**/*.{erb,haml,rb}")]
5
+ def self.search_methods(method_definitions, files_to_search)
7
6
  errors = []
8
- all_file_names.each do |fileName|
7
+ files_to_search.each do |fileName|
9
8
  begin
10
9
  File.open(fileName).each_line do |line|
11
10
  next unless line
@@ -15,12 +14,44 @@ module Gravedigger
15
14
  end
16
15
  end
17
16
  rescue StandardError => error
18
- errors << "Error while searching method: #{method_name} in file: #{fileName}\n"\
17
+ errors << "Error while searching method in file: #{fileName}\n"\
19
18
  "#{error.message}"
20
19
  end
21
20
  end
22
21
 
23
22
  return method_definitions, errors
24
23
  end
24
+
25
+ def self.search_variables(variable_definitions, files_to_search)
26
+ errors = []
27
+ files_to_search.each do |fileName|
28
+ begin
29
+ File.readlines(fileName).each_with_index do |line,index|
30
+ next unless line
31
+ variable_usage_matchs = line.scan(/[ \(\{\[\=\,\+\*\-\/\:]+([A-Za-z0-9\_\-\?\!\@]+)/)
32
+ variable_usage_matchs.each do |variable_usage_match|
33
+ next unless variable_usage_match
34
+ variable_usage = extract_variable_usage(variable_usage_match)
35
+ if variable_definitions[variable_usage] != "[#{fileName}:#{index+1}]"
36
+ variable_definitions.delete(variable_usage)
37
+ end
38
+
39
+ end
40
+ end
41
+ rescue StandardError => error
42
+ errors << "Error while searching variable in file: #{fileName}\n"\
43
+ "#{error.message}"
44
+ end
45
+ end
46
+
47
+ return variable_definitions, errors
48
+ end
49
+
50
+ def self.extract_variable_usage(variable_usage_match)
51
+ variable_usage = variable_usage_match.first
52
+ variable_usage.sub!(/[@!]/, '') # remove ! and @ characters from string
53
+
54
+ variable_usage
55
+ end
25
56
  end
26
57
  end
@@ -1,3 +1,3 @@
1
1
  module Gravedigger
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gravedigger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oğuzhan Göller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-14 00:00:00.000000000 Z
11
+ date: 2020-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler