rubocop-legion 0.1.0 → 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 +4 -4
- data/CHANGELOG.md +17 -0
- data/config/default.yml +12 -2
- data/lib/rubocop/cop/legion/helper_migration/logging_guard.rb +71 -0
- data/lib/rubocop/cop/legion/rescue_logging/bare_rescue.rb +11 -0
- data/lib/rubocop/cop/legion/rescue_logging/no_capture.rb +12 -6
- data/lib/rubocop/cop/legion/rescue_logging/silent_capture.rb +2 -0
- data/lib/rubocop/legion/version.rb +1 -1
- data/lib/rubocop-legion.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1d2c3a1967dcc9bd66a2c806d2b471b59594d51ae9fec7c2c3eaad9946898932
|
|
4
|
+
data.tar.gz: cdd7fcb62b6b0ff1eb17d595aa6a7b76259fdefa7ae5ecd6d6b1306f98e817c1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0515bb7d5ea06e80069a937b9899e23bf281366660d718cd707460b0ed63a9955ed4ff531f6e592c9b203ea090bda1ba620d5d80fbb4b758dab3b91f9190dc71
|
|
7
|
+
data.tar.gz: 673910f539a627bd4697e1bfd86ee8c8b0f9644d257090fafcad43907bd080af80be71e961340e53be6077251dd8dd302b957fcb8d74a4418be228b3f6fd175c
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.1.2] - 2026-03-29
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- New cop `Legion/HelperMigration/LoggingGuard`: flags unnecessary `respond_to?(:log_warn)` etc. and `defined?(Legion::Logging)` guards
|
|
7
|
+
|
|
8
|
+
## [0.1.1] - 2026-03-29
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
- BareRescue auto-corrector no longer corrupts inline rescue modifiers (`foo rescue nil`)
|
|
12
|
+
- NoCapture removed auto-correct to prevent correction loop with Lint/UselessAssignment
|
|
13
|
+
- SilentCapture skips `_`-prefixed variables (Ruby unused variable convention)
|
|
14
|
+
|
|
15
|
+
### Changed
|
|
16
|
+
- HelperMigration cops scoped to `lib/legion/extensions/**/*.rb` (lex-* gems only)
|
|
17
|
+
- Extension cops scoped to `lib/legion/extensions/**/*.rb` with spec exclusion
|
|
18
|
+
- ApiStringKeys scoped to `lib/legion/extensions/**/*.rb` (Faraday responses use string keys)
|
|
19
|
+
|
|
3
20
|
## [0.1.0] - 2026-03-29
|
|
4
21
|
|
|
5
22
|
### Added
|
data/config/default.yml
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Legion/HelperMigration:
|
|
4
4
|
Enabled: true
|
|
5
5
|
Include:
|
|
6
|
-
- 'lib/**/*.rb'
|
|
6
|
+
- 'lib/legion/extensions/**/*.rb'
|
|
7
7
|
Exclude:
|
|
8
8
|
- 'spec/**/*'
|
|
9
9
|
|
|
@@ -43,6 +43,12 @@ Legion/HelperMigration/DirectCrypt:
|
|
|
43
43
|
Severity: warning
|
|
44
44
|
VersionAdded: '0.1'
|
|
45
45
|
|
|
46
|
+
Legion/HelperMigration/LoggingGuard:
|
|
47
|
+
Description: 'Remove unnecessary `respond_to?(:log_warn)` or `defined?(Legion::Logging)` guards.'
|
|
48
|
+
Enabled: true
|
|
49
|
+
Severity: convention
|
|
50
|
+
VersionAdded: '0.1.2'
|
|
51
|
+
|
|
46
52
|
# Legion/ConstantSafety — prevent namespace resolution bugs inside module Legion
|
|
47
53
|
|
|
48
54
|
Legion/ConstantSafety:
|
|
@@ -163,7 +169,7 @@ Legion/Framework/ApiStringKeys:
|
|
|
163
169
|
Severity: warning
|
|
164
170
|
VersionAdded: '0.1'
|
|
165
171
|
Include:
|
|
166
|
-
- 'lib/**/*.rb'
|
|
172
|
+
- 'lib/legion/extensions/**/*.rb'
|
|
167
173
|
Exclude:
|
|
168
174
|
- 'spec/**/*'
|
|
169
175
|
|
|
@@ -171,6 +177,10 @@ Legion/Framework/ApiStringKeys:
|
|
|
171
177
|
|
|
172
178
|
Legion/Extension:
|
|
173
179
|
Enabled: true
|
|
180
|
+
Include:
|
|
181
|
+
- 'lib/legion/extensions/**/*.rb'
|
|
182
|
+
Exclude:
|
|
183
|
+
- 'spec/**/*'
|
|
174
184
|
|
|
175
185
|
Legion/Extension/ActorSingularModule:
|
|
176
186
|
Description: 'Use `module Actor` (singular). Framework discovers actors in `Actor`, not `Actors`.'
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Legion
|
|
6
|
+
module HelperMigration
|
|
7
|
+
# Detects unnecessary guard checks around logging that are no longer
|
|
8
|
+
# needed. The `log` helper is always available in extensions via
|
|
9
|
+
# `Helpers::Lex`.
|
|
10
|
+
#
|
|
11
|
+
# Catches two patterns:
|
|
12
|
+
# 1. `respond_to?` checks for old logging methods or `log`
|
|
13
|
+
# 2. `defined?(Legion::Logging)` guards
|
|
14
|
+
#
|
|
15
|
+
# @example
|
|
16
|
+
# # bad
|
|
17
|
+
# log.warn(msg) if respond_to?(:log_warn, true)
|
|
18
|
+
# log.info(msg) if respond_to?(:log)
|
|
19
|
+
# Legion::Logging.info(msg) if defined?(Legion::Logging)
|
|
20
|
+
#
|
|
21
|
+
# # good
|
|
22
|
+
# log.warn(msg)
|
|
23
|
+
# log.info(msg)
|
|
24
|
+
class LoggingGuard < RuboCop::Cop::Base
|
|
25
|
+
MSG_RESPOND_TO = '`respond_to?(:%<method>s)` guard is unnecessary. ' \
|
|
26
|
+
'`log` is always available via `Helpers::Lex`.'
|
|
27
|
+
MSG_DEFINED = '`defined?(Legion::Logging)` guard is unnecessary. ' \
|
|
28
|
+
'`Legion::Logging` is always loaded in the framework.'
|
|
29
|
+
|
|
30
|
+
OLD_METHODS = %i[log log_info log_warn log_error log_debug log_fatal].to_set.freeze
|
|
31
|
+
|
|
32
|
+
RESTRICT_ON_SEND = %i[respond_to?].freeze
|
|
33
|
+
|
|
34
|
+
# respond_to?(:log_warn) or respond_to?(:log_warn, true)
|
|
35
|
+
# @!method old_logging_respond_to?(node)
|
|
36
|
+
def_node_matcher :old_logging_respond_to?, <<~PATTERN
|
|
37
|
+
(send nil? :respond_to? (sym $_) ...)
|
|
38
|
+
PATTERN
|
|
39
|
+
|
|
40
|
+
def on_send(node)
|
|
41
|
+
old_logging_respond_to?(node) do |method_name|
|
|
42
|
+
next unless OLD_METHODS.include?(method_name)
|
|
43
|
+
|
|
44
|
+
add_offense(node, message: format(MSG_RESPOND_TO, method: method_name))
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# defined? is a keyword, not a send — handle via on_defined? callback
|
|
49
|
+
def on_defined?(node)
|
|
50
|
+
return false unless legion_logging_defined?(node)
|
|
51
|
+
|
|
52
|
+
add_offense(node, message: MSG_DEFINED)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def legion_logging_defined?(node)
|
|
58
|
+
child = node.children.first
|
|
59
|
+
return false unless child&.const_type?
|
|
60
|
+
|
|
61
|
+
# Match Legion::Logging — (const (const nil :Legion) :Logging)
|
|
62
|
+
parent_const = child.children.first
|
|
63
|
+
parent_const&.const_type? &&
|
|
64
|
+
parent_const.children == [nil, :Legion] &&
|
|
65
|
+
child.children.last == :Logging
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -29,11 +29,22 @@ module RuboCop
|
|
|
29
29
|
|
|
30
30
|
def on_resbody(node)
|
|
31
31
|
return unless node.exceptions.empty? && node.exception_variable.nil?
|
|
32
|
+
return if rescue_modifier?(node)
|
|
32
33
|
|
|
33
34
|
add_offense(node, severity: :warning) do |corrector|
|
|
34
35
|
corrector.insert_after(node.loc.keyword, ' => e')
|
|
35
36
|
end
|
|
36
37
|
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def rescue_modifier?(node)
|
|
42
|
+
rescue_node = node.parent
|
|
43
|
+
return false unless rescue_node&.rescue_type?
|
|
44
|
+
|
|
45
|
+
body = rescue_node.children.first
|
|
46
|
+
body && body.source_range.line == node.loc.keyword.line
|
|
47
|
+
end
|
|
37
48
|
end
|
|
38
49
|
end
|
|
39
50
|
end
|
|
@@ -23,21 +23,27 @@ module RuboCop
|
|
|
23
23
|
# log.error(e.message)
|
|
24
24
|
# end
|
|
25
25
|
class NoCapture < RuboCop::Cop::Base
|
|
26
|
-
extend AutoCorrector
|
|
27
|
-
|
|
28
26
|
MSG = 'Exception class specified but not captured. ' \
|
|
29
27
|
'Use `rescue %<classes>s => e` and log the exception.'
|
|
30
28
|
|
|
31
29
|
def on_resbody(node)
|
|
32
30
|
return if node.exceptions.empty? || !node.exception_variable.nil?
|
|
31
|
+
return if rescue_modifier?(node)
|
|
33
32
|
|
|
34
33
|
classes = node.exceptions.map(&:source).join(', ')
|
|
35
34
|
message = format(MSG, classes: classes)
|
|
36
35
|
|
|
37
|
-
add_offense(node, message: message, severity: :convention)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
add_offense(node, message: message, severity: :convention)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def rescue_modifier?(node)
|
|
42
|
+
rescue_node = node.parent
|
|
43
|
+
return false unless rescue_node&.rescue_type?
|
|
44
|
+
|
|
45
|
+
body = rescue_node.children.first
|
|
46
|
+
body && body.source_range.line == node.loc.keyword.line
|
|
41
47
|
end
|
|
42
48
|
end
|
|
43
49
|
end
|
|
@@ -32,6 +32,8 @@ module RuboCop
|
|
|
32
32
|
return unless node.exception_variable
|
|
33
33
|
|
|
34
34
|
var_name = variable_name(node.exception_variable)
|
|
35
|
+
return if var_name.to_s.start_with?('_')
|
|
36
|
+
|
|
35
37
|
body = node.body
|
|
36
38
|
|
|
37
39
|
return if body && (references_variable?(body, var_name) || contains_raise?(body))
|
data/lib/rubocop-legion.rb
CHANGED
|
@@ -13,6 +13,7 @@ require 'rubocop/cop/legion/helper_migration/direct_json'
|
|
|
13
13
|
require 'rubocop/cop/legion/helper_migration/direct_cache'
|
|
14
14
|
require 'rubocop/cop/legion/helper_migration/direct_local_cache'
|
|
15
15
|
require 'rubocop/cop/legion/helper_migration/direct_crypt'
|
|
16
|
+
require 'rubocop/cop/legion/helper_migration/logging_guard'
|
|
16
17
|
|
|
17
18
|
# Legion/ConstantSafety
|
|
18
19
|
require 'rubocop/cop/legion/constant_safety/bare_data_define'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rubocop-legion
|
|
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
|
- Esity
|
|
@@ -110,6 +110,7 @@ files:
|
|
|
110
110
|
- lib/rubocop/cop/legion/helper_migration/direct_json.rb
|
|
111
111
|
- lib/rubocop/cop/legion/helper_migration/direct_local_cache.rb
|
|
112
112
|
- lib/rubocop/cop/legion/helper_migration/direct_logging.rb
|
|
113
|
+
- lib/rubocop/cop/legion/helper_migration/logging_guard.rb
|
|
113
114
|
- lib/rubocop/cop/legion/helper_migration/old_logging_methods.rb
|
|
114
115
|
- lib/rubocop/cop/legion/rescue_logging/bare_rescue.rb
|
|
115
116
|
- lib/rubocop/cop/legion/rescue_logging/no_capture.rb
|