rubocop-fluentd 0.1.0 → 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: d2f08b0f70066010f0a03161c19f17b8167f5a83189c81c4352314b202b06d0e
4
- data.tar.gz: 148332ce3ad1d4713096690b9e70ad346947905ec5de3e4386c55d921e816824
3
+ metadata.gz: 36fbca65669f2ece9eebbf4b11704127822b9fe495cdd613eaabb1bd54a0ed34
4
+ data.tar.gz: 26785e17f1b748804fec642f24746cbdd880542eb2e73e6c1f0fa7fe68d69831
5
5
  SHA512:
6
- metadata.gz: 6ff125adaa98eb0cb9427314816e222a48e513fe8285e15298fb839883902fa0349ea90be1dc055e35369d3b6b02a2c9df3d8459383ec20bfc470be2ace8a857
7
- data.tar.gz: 77f4e315f0584cd8cb62e00d0b53709a806a6c9323e500c2e3ad23f7c4d363beb4a1f1d3bffa655f27f30362893ebaf86c07af2da01557259afd00494f253321
6
+ metadata.gz: 3e7dea98385b42b570a66f60358813f02bd1dad063c001aedd7bf53fd782c47e2a5be199dcec79c9c79fb2dc8383fcb69e695d4746c22c99ad64e18825a4cab0
7
+ data.tar.gz: 5cc4b01eff6ec31f8f1a623c1331956b65f72aa17ae2fc4d6d311f2e2971af35da027f0fe27edafa1cda9339d0cd1809ace43afc6435854e80dc90728436b028
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
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
+
9
+ ## 0.1.1 - 2025-07-09
10
+
11
+ * Add Lint/FluentdPluginConfigParamDefaultTime
12
+
3
13
  ## 0.1.0 - 2025-07-07
4
14
 
5
15
  * Initial release
data/README.md CHANGED
@@ -23,6 +23,8 @@ Configure `.rubocop.yml` like this:
23
23
  ```yaml
24
24
  Lint/FluentdPluginLogScope:
25
25
  Enabled: true
26
+ Lint/FluentdPluginConfigParamDefaultTime:
27
+ Enabled: true
26
28
  Performance/FluentdPluginLogStringInterpolation:
27
29
  Enabled: true
28
30
  ```
data/config/default.yml CHANGED
@@ -1,5 +1,10 @@
1
1
  # Write it!
2
2
 
3
+ Lint/FluentdPluginConfigParamDefaultTime:
4
+ Description: 'Warn the default value of :time for config_param is string'
5
+ Enabled: true
6
+ VersionAdded: '0.1.1'
7
+
3
8
  Lint/FluentdPluginLogLevel:
4
9
  Description: 'Warn global scope `$log` was used in Fluentd plugin. The scope of plugin log level was supported since Fluentd 0.10.43'
5
10
  Enabled: true
@@ -1,3 +1,4 @@
1
1
  # frozen_string_literal: true
2
+ require_relative 'lint/plugin_config_param_time'
2
3
  require_relative 'lint/plugin_log_scope'
3
4
  require_relative 'performance/plugin_log_string_interpolation'
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Lint
6
+ #
7
+ # @example FluentdPluginConfigParamDefaultTime (default)
8
+ #
9
+ # # bad
10
+ # config_param :interval, :time, :default => '1h'
11
+ #
12
+ # # good
13
+ # config_param :interval, :time, :default => 3600
14
+ #
15
+ # See https://github.com/fluent/fluent-plugin-opensearch/pull/159
16
+ #
17
+ class FluentdPluginConfigParamDefaultTime < Base
18
+ include IgnoredNode
19
+ extend AutoCorrector
20
+
21
+ RESTRICT_ON_SEND = %i[config_param].freeze
22
+
23
+ # @!method config_param_default_time?(node)
24
+ def_node_matcher :config_param_default_string_time?, <<~PATTERN
25
+ (send nil? :config_param (sym _) (sym :time) (hash (pair (sym :default) (str _))))
26
+ PATTERN
27
+
28
+ def on_send(node)
29
+ return unless config_param_default_string_time?(node)
30
+
31
+ parameter = config_param_variable(node)
32
+ default = config_param_default(node)
33
+ message = "The value of :#{parameter} must be `integer` or `float` for default time value."
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
41
+ ignore_node(node)
42
+ end
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
69
+ end
70
+ end
71
+ end
72
+ 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.0"
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.0
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
@@ -53,6 +67,7 @@ files:
53
67
  - config/default.yml
54
68
  - lib/rubocop-fluentd.rb
55
69
  - lib/rubocop/cop/fluentd_cops.rb
70
+ - lib/rubocop/cop/lint/plugin_config_param_time.rb
56
71
  - lib/rubocop/cop/lint/plugin_log_scope.rb
57
72
  - lib/rubocop/cop/performance/plugin_log_string_interpolation.rb
58
73
  - lib/rubocop/fluentd.rb