rubocop-legion 0.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 761ab5c8fe6b85b1df48dddace038f224e7a1a65289c8f06a4c8e37543a284f3
4
- data.tar.gz: 20316578e3b32c13a62b1524167523ce2f9105557d4bf4301b1f146108cbf05c
3
+ metadata.gz: 1d2c3a1967dcc9bd66a2c806d2b471b59594d51ae9fec7c2c3eaad9946898932
4
+ data.tar.gz: cdd7fcb62b6b0ff1eb17d595aa6a7b76259fdefa7ae5ecd6d6b1306f98e817c1
5
5
  SHA512:
6
- metadata.gz: ca163ec06479b86169f5fa4016950c85b9d7ffdb2d7636fbd7f2a5afaf93cf98ad488ff23acee8c77b6ce505c09e8ac6928592b7d16a7a088f8edeeed11a97e0
7
- data.tar.gz: 51ec78f8b560249fb871e185e0989f3f9af27bfc40b637adc46b9c6d20c4f10a376a60aa7ad397f08f89b10e10267de3e815a98bc9faf7dddd0903e55ffe0a8f
6
+ metadata.gz: 0515bb7d5ea06e80069a937b9899e23bf281366660d718cd707460b0ed63a9955ed4ff531f6e592c9b203ea090bda1ba620d5d80fbb4b758dab3b91f9190dc71
7
+ data.tar.gz: 673910f539a627bd4697e1bfd86ee8c8b0f9644d257090fafcad43907bd080af80be71e961340e53be6077251dd8dd302b957fcb8d74a4418be228b3f6fd175c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
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
+
3
8
  ## [0.1.1] - 2026-03-29
4
9
 
5
10
  ### Fixed
data/config/default.yml CHANGED
@@ -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:
@@ -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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Legion
5
- VERSION = '0.1.1'
5
+ VERSION = '0.1.2'
6
6
  end
7
7
  end
@@ -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.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