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 +4 -4
- data/.idea/un_used_methods.iml +41 -0
- data/README.md +69 -39
- data/lib/un_used_methods/analyzer.rb +55 -26
- data/lib/un_used_methods/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03e7bb25efe13d600ecc3078919a2d63dc91f8134b9439541f464657577a21a2
|
4
|
+
data.tar.gz: fd5f60af074afadfa114e86b7bb556c0fb6831fa0d79bb8f5763736ba9292f98
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 188d19d09c7b1112e646a9889ebb35e2f2b42f9d88e9d4469722f5d37e93d54bd84bcdd4511e3eaa51e43343095c6a35e35c966279c48ad3d6dbeaf82ce511e1
|
7
|
+
data.tar.gz: c8f0ced1ae32c8e485d94e87ae82beab821f4faae37bbacce056c29b9dc9d3d9f8e218a4467eb827854ff057e116eefbe0f7726be46eed52f87a944c835281a4
|
data/.idea/un_used_methods.iml
CHANGED
@@ -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
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
-
|
24
|
-
|
25
|
-
|
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}"
|
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
|
-
|
54
|
-
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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)
|
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.
|
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-
|
11
|
+
date: 2024-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|