un_used_methods 0.1.2 → 0.1.3

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: 03e7bb25efe13d600ecc3078919a2d63dc91f8134b9439541f464657577a21a2
4
- data.tar.gz: fd5f60af074afadfa114e86b7bb556c0fb6831fa0d79bb8f5763736ba9292f98
3
+ metadata.gz: 165752d3ad48f228b8dc3314fb55c847df1669e46779fab24cd7ae22ee79c4fb
4
+ data.tar.gz: 17a1e0d278cbe0af632f040851401e7c62d7c08b262639ed522e6615ab128121
5
5
  SHA512:
6
- metadata.gz: 188d19d09c7b1112e646a9889ebb35e2f2b42f9d88e9d4469722f5d37e93d54bd84bcdd4511e3eaa51e43343095c6a35e35c966279c48ad3d6dbeaf82ce511e1
7
- data.tar.gz: c8f0ced1ae32c8e485d94e87ae82beab821f4faae37bbacce056c29b9dc9d3d9f8e218a4467eb827854ff057e116eefbe0f7726be46eed52f87a944c835281a4
6
+ metadata.gz: 9d412e91fc1ff54838afcc323a46ac6156f5427abc167a9bf7201d563f0ea4be8c1d1d7b1d8e7c7737c38116cb9b431557855784253b9f99f6f3828877ece2f2
7
+ data.tar.gz: 734cdc3cd74a4f26d3470231942e7a9a97b18475aee25ecd344cdce55d75ff2c50fa875c6a66ff1d1017ae218b444fad0bf7473d7062cdc8eaeaea5df18f15ef
@@ -54,6 +54,9 @@ module UnUsedMethods
54
54
  # Check if method is used in any file other than its own definition file
55
55
  return true if file_contains_method_call?(files, definition_file, method_call_patterns)
56
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
+
57
60
  # Check method usage within its own file
58
61
  method_called_in_own_file?(definition_file, method_call_patterns)
59
62
  end
@@ -61,7 +64,9 @@ module UnUsedMethods
61
64
  def build_method_call_patterns(method)
62
65
  [
63
66
  /(\.|^|\s)#{method}\s*\(/, # Matches method calls with parameters
64
- /(\.|^|\s)#{method}\b(?!\()/ # Matches method calls without 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
65
70
  ]
66
71
  end
67
72
 
@@ -79,6 +84,16 @@ module UnUsedMethods
79
84
  patterns.any? { |pattern| content.scan(pattern).count > 1 }
80
85
  end
81
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
+
82
97
  def read_file(file)
83
98
  content = File.read(file)
84
99
  strip_comments(content, file)
@@ -87,8 +102,8 @@ module UnUsedMethods
87
102
  def strip_comments(content, file)
88
103
  case File.extname(file)
89
104
  when ".rb"
90
- # Remove Ruby comments
91
- content.gsub(/#.*$/, "")
105
+ # Remove Ruby comments and strings
106
+ content.gsub(/#.*$/, "").gsub(/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'/, "")
92
107
  when ".erb", ".html", ".haml", ".slim"
93
108
  # Remove HTML, ERB, HAML, SLIM comments
94
109
  content.gsub(/<!--.*?-->/m, "").gsub(/<%#.*?%>/m, "")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UnUsedMethods
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
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.2
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-21 00:00:00.000000000 Z
11
+ date: 2024-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake