un_used_methods 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb16037d95822bc46032688ce2f4946b51bdcb036f6d10ac3e99bad7e55e841f
4
- data.tar.gz: f09b4e02a470acc103637647c5518a36ec60df67afa3a7a5a08bc191b4fc24e5
3
+ metadata.gz: 03e7bb25efe13d600ecc3078919a2d63dc91f8134b9439541f464657577a21a2
4
+ data.tar.gz: fd5f60af074afadfa114e86b7bb556c0fb6831fa0d79bb8f5763736ba9292f98
5
5
  SHA512:
6
- metadata.gz: 010c199699355f6eb81141e82377a193a409c98156ead13417867a71a9384622da243d74eee859d12539e8c8fe12f5cfcbe03261326a73c3ab3e316ff64f0510
7
- data.tar.gz: 9120c81b4cebcadaa300622c6d7c7fe372bb8776f84a7ab8fee16eed814810250a4f0f1468b6be11bf69be591afdc3efee4f079804e759bbcb41daa63d4431ee
6
+ metadata.gz: 188d19d09c7b1112e646a9889ebb35e2f2b42f9d88e9d4469722f5d37e93d54bd84bcdd4511e3eaa51e43343095c6a35e35c966279c48ad3d6dbeaf82ce511e1
7
+ data.tar.gz: c8f0ced1ae32c8e485d94e87ae82beab821f4faae37bbacce056c29b9dc9d3d9f8e218a4467eb827854ff057e116eefbe0f7726be46eed52f87a944c835281a4
@@ -35,4 +35,45 @@
35
35
  <orderEntry type="library" scope="PROVIDED" name="thor (v1.3.1, ruby-3.1.6-p260 (2)) [gem]" level="application" />
36
36
  <orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v2.5.0, ruby-3.1.6-p260 (2)) [gem]" level="application" />
37
37
  </component>
38
+ <component name="RakeTasksCache-v2">
39
+ <option name="myRootTask">
40
+ <RakeTaskImpl id="rake">
41
+ <subtasks>
42
+ <RakeTaskImpl description="Build un_used_methods-0.1.1.gem into the pkg directory" fullCommand="build" id="build" />
43
+ <RakeTaskImpl id="build">
44
+ <subtasks>
45
+ <RakeTaskImpl description="Generate SHA512 checksum of un_used_methods-0.1.1.gem into the checksums directory" fullCommand="build:checksum" id="checksum" />
46
+ </subtasks>
47
+ </RakeTaskImpl>
48
+ <RakeTaskImpl description="Remove any temporary products" fullCommand="clean" id="clean" />
49
+ <RakeTaskImpl description="Remove any generated files" fullCommand="clobber" id="clobber" />
50
+ <RakeTaskImpl description="Build and install un_used_methods-0.1.1.gem into system gems" fullCommand="install" id="install" />
51
+ <RakeTaskImpl id="install">
52
+ <subtasks>
53
+ <RakeTaskImpl description="Build and install un_used_methods-0.1.1.gem into system gems without network access" fullCommand="install:local" id="local" />
54
+ </subtasks>
55
+ </RakeTaskImpl>
56
+ <RakeTaskImpl description="Create tag v0.1.1 and build and push un_used_methods-0.1.1.gem to https://rubygems.org" fullCommand="release[remote]" id="release[remote]" />
57
+ <RakeTaskImpl description="Run RuboCop" fullCommand="rubocop" id="rubocop" />
58
+ <RakeTaskImpl id="rubocop">
59
+ <subtasks>
60
+ <RakeTaskImpl description="Autocorrect RuboCop offenses (only when it's safe)" fullCommand="rubocop:autocorrect" id="autocorrect" />
61
+ <RakeTaskImpl description="Autocorrect RuboCop offenses (safe and unsafe)" fullCommand="rubocop:autocorrect_all" id="autocorrect_all" />
62
+ <RakeTaskImpl description="" fullCommand="rubocop:auto_correct" id="auto_correct" />
63
+ </subtasks>
64
+ </RakeTaskImpl>
65
+ <RakeTaskImpl description="Run RSpec code examples" fullCommand="spec" id="spec" />
66
+ <RakeTaskImpl description="" fullCommand="default" id="default" />
67
+ <RakeTaskImpl description="" fullCommand="release" id="release" />
68
+ <RakeTaskImpl id="release">
69
+ <subtasks>
70
+ <RakeTaskImpl description="" fullCommand="release:guard_clean" id="guard_clean" />
71
+ <RakeTaskImpl description="" fullCommand="release:rubygem_push" id="rubygem_push" />
72
+ <RakeTaskImpl description="" fullCommand="release:source_control_push" id="source_control_push" />
73
+ </subtasks>
74
+ </RakeTaskImpl>
75
+ </subtasks>
76
+ </RakeTaskImpl>
77
+ </option>
78
+ </component>
38
79
  </module>
data/README.md CHANGED
@@ -1,39 +1,69 @@
1
- # UnUsedMethods
2
-
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/un_used_methods`. To experiment with that code, run `bin/console` for an interactive prompt.
6
-
7
- ## Installation
8
-
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
-
11
- Install the gem and add to the application's Gemfile by executing:
12
-
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
-
15
- If bundler is not being used to manage dependencies, install the gem by executing:
16
-
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
-
19
- ## Usage
20
-
21
- TODO: Write usage instructions here
22
-
23
- ## Development
24
-
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
-
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
-
29
- ## Contributing
30
-
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/un_used_methods. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/un_used_methods/blob/master/CODE_OF_CONDUCT.md).
32
-
33
- ## License
34
-
35
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
-
37
- ## Code of Conduct
38
-
39
- Everyone interacting in the UnUsedMethods project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/un_used_methods/blob/master/CODE_OF_CONDUCT.md).
1
+ # UnUsedMethods Gem
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/un_used_methods.svg)](https://badge.fury.io/rb/un_used_methods)
4
+
5
+ The `un_used_methods` gem is designed to help you identify and clean up unused methods in your Ruby on Rails application. In a large codebase, it can be challenging to track which methods are actively used and which are obsolete. The `un_used_methods` gem scans your application's codebase and identifies methods that are defined but not used anywhere else in your project. This helps you spot and remove unnecessary code, improving maintainability and performance.
6
+
7
+
8
+ ## Features
9
+
10
+ - Scans for method definitions across various directories (models, controllers, helpers, views, and more).
11
+ - Checks for method usage in code files and view templates.
12
+ - Reports methods that are defined but not used in the project.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's `Gemfile`:
17
+
18
+ ```ruby
19
+ gem 'un_used_methods'
20
+ ```
21
+
22
+ Then run:
23
+
24
+ ```ruby
25
+ bundle install
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ To use the gem, run the following command:
31
+
32
+ ```ruby
33
+ bundle exec un_used_methods find_unused
34
+ ```
35
+
36
+ This command will analyze your codebase and print out any unused methods it finds. The gem scans through Ruby files, HTML/ERB templates, and other relevant file types to ensure comprehensive coverage.
37
+
38
+ ## Example Output
39
+
40
+ ```plaintext
41
+ Method 'test' called in file: app/models/test.rb
42
+ Method 'another_method' not called anywhere in the project
43
+
44
+ Unused Methods found in your model, controller and helper directories:
45
+ app/models/test.rb: another_method
46
+ ```
47
+
48
+ ## Configuration
49
+
50
+ The gem uses default settings to scan common directories and file types. If you need to customize the directories or file types to scan, you can modify the gem's configuration within the codebase.
51
+
52
+ ## Development
53
+
54
+ To contribute to the development of this gem:
55
+
56
+ 1. Fork the repository.
57
+ 2. Create a new branch (`git checkout -b feature/your-feature`).
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`).
59
+ 4. Push to the branch (`git push origin feature/your-feature`).
60
+ 5. Create a new Pull Request.
61
+ 6. Write RSpec test cases for the features you added.
62
+
63
+ ## License
64
+
65
+ This gem is available as open source under the terms of the MIT License.
66
+
67
+ ## Acknowledgements
68
+
69
+ This gem leverages concepts from static code analysis and method usage detection to enhance code quality.
@@ -19,14 +19,10 @@ module UnUsedMethods
19
19
  def find_un_used_methods
20
20
  un_used_methods = []
21
21
 
22
- # Analyze Models
23
- un_used_methods += find_in_directory("app/models")
24
-
25
- # Analyze Controllers
26
- un_used_methods += find_in_directory("app/controllers")
27
-
28
- # Analyze Helpers
29
- un_used_methods += find_in_directory("app/helpers")
22
+ # Analyze Models, Controllers, and Helpers
23
+ ["app/models", "app/controllers", "app/helpers"].each do |directory|
24
+ un_used_methods += find_in_directory(directory)
25
+ end
30
26
 
31
27
  un_used_methods
32
28
  end
@@ -37,7 +33,7 @@ module UnUsedMethods
37
33
  Dir.glob("#{directory}/**/*.rb").each do |file|
38
34
  methods = extract_methods(file)
39
35
  methods.each do |method|
40
- un_used_methods << "#{file}: #{method}" if method_used?(method, file)
36
+ un_used_methods << "#{file}: #{method}" unless method_used?(method, file)
41
37
  end
42
38
  end
43
39
 
@@ -46,29 +42,62 @@ module UnUsedMethods
46
42
 
47
43
  def extract_methods(file)
48
44
  content = File.read(file)
45
+ content = strip_comments(content, file)
49
46
  content.scan(/def (\w+)/).flatten
50
47
  end
51
48
 
52
49
  def method_used?(method, definition_file)
53
- # Patterns to detect method calls with and without parentheses
54
- method_call_pattern = /(\.|^|\s)#{method}\s*(\(|$)/
55
- # Search directories for relevant file types
50
+ method_call_patterns = build_method_call_patterns(method)
51
+
56
52
  files = Dir.glob("app/**/*.{rb,html,erb,haml,slim,js,jsx,ts,tsx}") + Dir.glob("lib/**/*.{rb}")
57
- method_used = false
58
- files.each do |file|
59
- content = File.read(file)
60
- # Check for method usage (skip the method's own definition line)
61
- if content =~ method_call_pattern && file != definition_file
62
- method_used = true
63
- p "Method '#{method}' called in file: #{file}"
64
- break
65
- elsif content.scan(method_call_pattern).count > 1 && file == definition_file
66
- method_used = true
67
- p "Method '#{method}' called in its own file: #{file}"
68
- break
69
- end
53
+
54
+ # Check if method is used in any file other than its own definition file
55
+ return true if file_contains_method_call?(files, definition_file, method_call_patterns)
56
+
57
+ # Check method usage within its own file
58
+ method_called_in_own_file?(definition_file, method_call_patterns)
59
+ end
60
+
61
+ def build_method_call_patterns(method)
62
+ [
63
+ /(\.|^|\s)#{method}\s*\(/, # Matches method calls with parameters
64
+ /(\.|^|\s)#{method}\b(?!\()/ # Matches method calls without parameters
65
+ ]
66
+ end
67
+
68
+ def file_contains_method_call?(files, definition_file, patterns)
69
+ files.any? do |file|
70
+ next if file == definition_file
71
+
72
+ content = read_file(file)
73
+ patterns.any? { |pattern| content.match?(pattern) }
74
+ end
75
+ end
76
+
77
+ def method_called_in_own_file?(definition_file, patterns)
78
+ content = read_file(definition_file)
79
+ patterns.any? { |pattern| content.scan(pattern).count > 1 }
80
+ end
81
+
82
+ def read_file(file)
83
+ content = File.read(file)
84
+ strip_comments(content, file)
85
+ end
86
+
87
+ def strip_comments(content, file)
88
+ case File.extname(file)
89
+ when ".rb"
90
+ # Remove Ruby comments
91
+ content.gsub(/#.*$/, "")
92
+ when ".erb", ".html", ".haml", ".slim"
93
+ # Remove HTML, ERB, HAML, SLIM comments
94
+ content.gsub(/<!--.*?-->/m, "").gsub(/<%#.*?%>/m, "")
95
+ when ".js", ".jsx", ".ts", ".tsx"
96
+ # Remove JavaScript/TypeScript comments
97
+ content.gsub(%r{//.*$}, "").gsub(%r{/\*.*?\*/}m, "")
98
+ else
99
+ content
70
100
  end
71
- !method_used
72
101
  end
73
102
 
74
103
  def report_un_used_methods(unused_methods)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UnUsedMethods
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: un_used_methods
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
  - Dhamo1107
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-17 00:00:00.000000000 Z
11
+ date: 2024-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake