rubocop-fluentd 0.1.1 → 0.2.0

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: 56610f49bc89a80c7cfc74091dbf9e0e5af8a2c7de39698bcf0b9464357d4682
4
- data.tar.gz: b6fa1274a3afda983e6e5f3c7552476f664521a2e163343b0299178e46ab487d
3
+ metadata.gz: 36fbca65669f2ece9eebbf4b11704127822b9fe495cdd613eaabb1bd54a0ed34
4
+ data.tar.gz: 26785e17f1b748804fec642f24746cbdd880542eb2e73e6c1f0fa7fe68d69831
5
5
  SHA512:
6
- metadata.gz: 9f938ec1fec15a0c9d0f7aac29857a76853831f6b6bc19b1c2bf73846fbe6ade79ca003797b34a0d9538831f1cf2764d235036dd4d33e562dbfcc63a76b40240
7
- data.tar.gz: f1131bc32c16d6ccaa783b8d40641735bcd7ce7ec3462a2e111212a4783afe02624e0ebfcb599b907d8a9ade2bfe4cf2a9d4ea9ed5d82b29f226d26853c5afeb
6
+ metadata.gz: 3e7dea98385b42b570a66f60358813f02bd1dad063c001aedd7bf53fd782c47e2a5be199dcec79c9c79fb2dc8383fcb69e695d4746c22c99ad64e18825a4cab0
7
+ data.tar.gz: 5cc4b01eff6ec31f8f1a623c1331956b65f72aa17ae2fc4d6d311f2e2971af35da027f0fe27edafa1cda9339d0cd1809ace43afc6435854e80dc90728436b028
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## 0.2.0 - 2025-07-10
4
+
5
+ * Support autocorrect for the following custom cop
6
+ - Lint/FluentdPluginConfigParamDefaultTime
7
+ - Lint/FluentdPluginLogScope
8
+
3
9
  ## 0.1.1 - 2025-07-09
4
10
 
5
11
  * Add Lint/FluentdPluginConfigParamDefaultTime
@@ -16,6 +16,7 @@ module RuboCop
16
16
  #
17
17
  class FluentdPluginConfigParamDefaultTime < Base
18
18
  include IgnoredNode
19
+ extend AutoCorrector
19
20
 
20
21
  RESTRICT_ON_SEND = %i[config_param].freeze
21
22
 
@@ -27,13 +28,44 @@ module RuboCop
27
28
  def on_send(node)
28
29
  return unless config_param_default_string_time?(node)
29
30
 
30
- symbol = node.children[2]
31
- parameter = symbol.value
31
+ parameter = config_param_variable(node)
32
+ default = config_param_default(node)
32
33
  message = "The value of :#{parameter} must be `integer` or `float` for default time value."
33
- add_offense(node, message: message)
34
+
35
+ expression = config_param_default_string_time?(node)
36
+ add_offense(node, message: message) do |corrector|
37
+ seconds = timestr_to_seconds(default)
38
+ source_code = "config_param :#{parameter}, :time, :default => #{seconds}"
39
+ corrector.replace(node, source_code)
40
+ end
34
41
  ignore_node(node)
35
42
  end
36
- alias on_csend on_send
43
+
44
+ def config_param_variable(node)
45
+ symbol = node.children[2]
46
+ symbol.value
47
+ end
48
+
49
+ def config_param_default(node)
50
+ pair = node.children[4].children.first
51
+ str = pair.children.last
52
+ str.value
53
+ end
54
+
55
+ def timestr_to_seconds(literal)
56
+ value = if literal.end_with?("d")
57
+ literal.delete("d").to_i * 60 * 60 * 24
58
+ elsif literal.end_with?("h")
59
+ literal.delete("h").to_i * 60 * 60
60
+ elsif literal.end_with?("m")
61
+ literal.delete("m").to_i * 60
62
+ elsif literal.end_with?("s")
63
+ literal.delete("s").to_i
64
+ else
65
+ literal.to_i
66
+ end
67
+ value
68
+ end
37
69
  end
38
70
  end
39
71
  end
@@ -34,6 +34,7 @@ module RuboCop
34
34
  #
35
35
  class FluentdPluginLogScope < Base
36
36
  include IgnoredNode
37
+ extend AutoCorrector
37
38
 
38
39
  MSG = 'Use plugin scope `log` instead of global scope `$log`.'
39
40
 
@@ -43,36 +44,57 @@ module RuboCop
43
44
 
44
45
  # @!method global_log_method?(node)
45
46
  def_node_matcher :global_reciever_method?, <<~PATTERN
46
- (send gvar ...)
47
+ (send gvar $_ $(...))
47
48
  PATTERN
48
49
 
49
50
  # @!method global_reciever_block_method?(node)
50
51
  def_node_matcher :global_reciever_block_method?, <<~PATTERN
51
- (block (send gvar ...) ...)
52
+ (block (send gvar $_) _ $(...))
52
53
  PATTERN
53
54
 
54
55
  def on_send(node)
55
56
  return if part_of_ignored_node?(node)
56
- return unless global_reciever_method?(node)
57
+ expression = global_reciever_method?(node)
58
+ return unless expression
57
59
 
58
60
  # $log.method(...)
59
61
  if send_global_log_node?(node)
60
- add_offense(node)
62
+ add_offense(node) do |corrector|
63
+ method = expression.first
64
+ literal = expression.last
65
+ source_code = "log.#{method} { #{literal.source} }"
66
+ # $log.xxx => log.xxx
67
+ corrector.replace(node, source_code)
68
+ end
61
69
  end
62
70
  end
63
71
 
64
72
  def on_block(node)
65
- return unless global_reciever_block_method?(node)
73
+ expression = global_reciever_block_method?(node)
74
+ return unless expression
66
75
 
67
76
  # $log.method { ... }
68
77
  send_node = node.children.first
69
78
  if send_global_log_node?(send_node)
70
- add_offense(node)
79
+ add_offense(node) do |corrector|
80
+ source_code = "log.#{block_log_level_method(node)}"
81
+ # $log.xxx => log.xxx
82
+ corrector.replace(node.children.first, source_code)
83
+ end
71
84
  # mark do not match on_send further more
72
85
  ignore_node(node)
73
86
  end
74
87
  end
75
88
 
89
+ def log_level_method(node)
90
+ node.children[1]
91
+ end
92
+
93
+ def block_log_level_method(node)
94
+ send_node = node.children.first
95
+ send_node.children.last
96
+ end
97
+
76
98
  def send_global_log_node?(node)
77
99
  node.class == RuboCop::AST::SendNode and
78
100
  global_log_reciever?(node.children.first)
@@ -81,9 +103,6 @@ module RuboCop
81
103
  def global_log_reciever?(node)
82
104
  node.name == :$log
83
105
  end
84
-
85
- alias on_csend on_send
86
- alias on_cblock on_block
87
106
  end
88
107
  end
89
108
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Fluentd
5
- VERSION = "0.1.1"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-fluentd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kentaro Hayashi
@@ -9,6 +9,20 @@ bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rubocop-performance
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.25'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.25'
12
26
  - !ruby/object:Gem::Dependency
13
27
  name: lint_roller
14
28
  requirement: !ruby/object:Gem::Requirement