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 +4 -4
- data/CHANGELOG.md +5 -0
- data/config/default.yml +6 -0
- data/lib/rubocop/cop/legion/helper_migration/logging_guard.rb +71 -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
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
|
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
|