un_used_methods 0.1.1 → 0.1.3
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 +70 -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: 165752d3ad48f228b8dc3314fb55c847df1669e46779fab24cd7ae22ee79c4fb
|
4
|
+
data.tar.gz: 17a1e0d278cbe0af632f040851401e7c62d7c08b262639ed522e6615ab128121
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d412e91fc1ff54838afcc323a46ac6156f5427abc167a9bf7201d563f0ea4be8c1d1d7b1d8e7c7737c38116cb9b431557855784253b9f99f6f3828877ece2f2
|
7
|
+
data.tar.gz: 734cdc3cd74a4f26d3470231942e7a9a97b18475aee25ecd344cdce55d75ff2c50fa875c6a66ff1d1017ae218b444fad0bf7473d7062cdc8eaeaea5df18f15ef
|
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,77 @@ 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 if the method is used in callbacks like `before_action` or `after_action`
|
58
|
+
return true if method_used_in_callback?(method, files)
|
59
|
+
|
60
|
+
# Check method usage within its own file
|
61
|
+
method_called_in_own_file?(definition_file, method_call_patterns)
|
62
|
+
end
|
63
|
+
|
64
|
+
def build_method_call_patterns(method)
|
65
|
+
[
|
66
|
+
/(\.|^|\s)#{method}\s*\(/, # Matches method calls with parameters
|
67
|
+
/(\.|^|\s)#{method}\b(?!\()/, # Matches method calls without parameters
|
68
|
+
/(\.|^|\s):#{method}\b/, # Matches method references as symbols (e.g., :method_name)
|
69
|
+
/\b#{method}\b/ # Matches method as a standalone word, e.g., when passed as an argument
|
70
|
+
]
|
71
|
+
end
|
72
|
+
|
73
|
+
def file_contains_method_call?(files, definition_file, patterns)
|
74
|
+
files.any? do |file|
|
75
|
+
next if file == definition_file
|
76
|
+
|
77
|
+
content = read_file(file)
|
78
|
+
patterns.any? { |pattern| content.match?(pattern) }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def method_called_in_own_file?(definition_file, patterns)
|
83
|
+
content = read_file(definition_file)
|
84
|
+
patterns.any? { |pattern| content.scan(pattern).count > 1 }
|
85
|
+
end
|
86
|
+
|
87
|
+
def method_used_in_callback?(method, files)
|
88
|
+
# Create a dynamic regex pattern to match any Rails callback with the given method
|
89
|
+
callback_pattern = /\b(before|after|around|validate|commit|save|create|update|destroy)\w*\s*:#{method}\b/
|
90
|
+
|
91
|
+
files.any? do |file|
|
92
|
+
content = read_file(file)
|
93
|
+
content.match?(callback_pattern)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def read_file(file)
|
98
|
+
content = File.read(file)
|
99
|
+
strip_comments(content, file)
|
100
|
+
end
|
101
|
+
|
102
|
+
def strip_comments(content, file)
|
103
|
+
case File.extname(file)
|
104
|
+
when ".rb"
|
105
|
+
# Remove Ruby comments and strings
|
106
|
+
content.gsub(/#.*$/, "").gsub(/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/, "")
|
107
|
+
when ".erb", ".html", ".haml", ".slim"
|
108
|
+
# Remove HTML, ERB, HAML, SLIM comments
|
109
|
+
content.gsub(/<!--.*?-->/m, "").gsub(/<%#.*?%>/m, "")
|
110
|
+
when ".js", ".jsx", ".ts", ".tsx"
|
111
|
+
# Remove JavaScript/TypeScript comments
|
112
|
+
content.gsub(%r{//.*$}, "").gsub(%r{/\*.*?\*/}m, "")
|
113
|
+
else
|
114
|
+
content
|
70
115
|
end
|
71
|
-
!method_used
|
72
116
|
end
|
73
117
|
|
74
118
|
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.3
|
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-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|