rubocop-petal 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99a53e19d1caafc491f963ad3a723fb1b06f9e15fc9d0914db50287c68af1dd6
4
- data.tar.gz: c1c37cbc85d3c4e56bdd2fffa928f8ce7f16db9afa5541014275a519d75146a5
3
+ metadata.gz: cdc12661793d2fbaa5d5634b86c23eb3e249a884ec4902406d66d969565fccd7
4
+ data.tar.gz: 8b1b59059300e38b056fab5b4c1d342ae3bf61dcd0db1204f140586c15dc889a
5
5
  SHA512:
6
- metadata.gz: 862c765c3e4c638372c04b441d569d25b260f0333e887cb25b76064ac82214173e4367b0225e4023b9ee4de9f804bb57b224bc524fb4beec46b9eff4bde8546d
7
- data.tar.gz: 263fe7fd0120e188d681dd68f7f44533e01c32d0a71596bc064781628175c7a40f0acea5cb22ec00707a8a67917fdb1dda1ee6811fea92ebf1047887346da20a
6
+ metadata.gz: 5c1494de93b6053ecac07431001234072212e9f62f9130b3dcc6ccefd3218cb3b454e5ace6de2c1e0d6d6aa91adf651d5454ebcfbca346b707579d45e855e77f
7
+ data.tar.gz: adb3d03f9419b9629ae7db9b7b1b4e811ce9d27a6a3b209f6ad6feaa4ca7edce47da7c698d3e9074cf25c3d452f022a2abebf0c80bb9b1b9764178614a571133
data/config/default.yml CHANGED
@@ -4,6 +4,10 @@ Chewy/FieldType:
4
4
  Include:
5
5
  - app/chewy/**/*
6
6
 
7
+ Chewy/UpdateIndexArgument:
8
+ Description: 'Prevent single expression in update_index block.'
9
+ Enabled: true
10
+
7
11
  Grape/PreferNamespace:
8
12
  Description: 'Prevent usage of namespace aliases.'
9
13
  Enabled: true
@@ -146,6 +150,10 @@ Sidekiq/NoNilReturn:
146
150
  Include:
147
151
  - app/workers/**/*
148
152
 
153
+ Sidekiq/PerformInline:
154
+ Description: 'Suggest to use `perform_inline` instead of `new.perform` for Sidekiq workers.'
155
+ Enabled: true
156
+
149
157
  Sidekiq/SymbolArgument:
150
158
  Description: "Prevent passing keywords arguments in worker's perform method"
151
159
  Enabled: true
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Chewy
6
+ # This cop checks for the use of block with single expression in `update_index` method
7
+ # and suggests to use method symbol as second argument instead of block.
8
+ #
9
+ # # bad
10
+ # update_index('index_name') { self }
11
+ #
12
+ # # good
13
+ # update_index('index_name', :self)
14
+ #
15
+ class UpdateIndexArgument < Base
16
+ extend AutoCorrector
17
+
18
+ MSG = 'Use method symbol as second argument instead of block.'
19
+
20
+ RESTRICT_ON_SEND = %i[update_index].freeze
21
+
22
+ # @!method block_with_single_expression?(node)
23
+ def_node_matcher :block_with_single_expression?, <<~PATTERN
24
+ (block
25
+ (send nil? :update_index (str _) $...)
26
+ (args)
27
+ $...)
28
+ PATTERN
29
+
30
+ def on_block(node)
31
+ block_with_single_expression?(node) do |existing_args, method_name|
32
+ return if method_name.last.children.compact.size > 1
33
+
34
+ method_name = method_name.first.source
35
+ add_offense(node.loc.expression) do |corrector|
36
+ corrector.replace(node.loc.expression, corrected_code(node, existing_args, method_name))
37
+ end
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def corrected_code(node, existing_args, method_name)
44
+ method_call = node.children[0]
45
+ index_name = method_call.children[2].source
46
+
47
+ if existing_args.any?
48
+ "update_index(#{index_name}, :#{method_name}, #{existing_args.map(&:source).join(', ')})"
49
+ else
50
+ "update_index(#{index_name}, :#{method_name})"
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sidekiq
6
+ # Suggest to use `perform_inline` instead of `new.perform` for Sidekiq workers.
7
+ #
8
+ # @bad
9
+ # MyWorker.new.perform
10
+ #
11
+ # @good
12
+ # MyWorker.perform_inline
13
+ #
14
+ class PerformInline < Base
15
+ extend AutoCorrector
16
+ MSG = 'Use `perform_inline` instead of `new.perform`'
17
+
18
+ RESTRICT_ON_SEND = %i[perform].freeze
19
+
20
+ # @!method new_perform?(node)
21
+ def_node_matcher :new_perform?, <<~PATTERN
22
+ (send (send _ :new) :perform ...)
23
+ PATTERN
24
+
25
+ def on_send(node)
26
+ return unless new_perform?(node)
27
+
28
+ new_perform_node = node.source_range.with(
29
+ begin_pos: node.receiver.receiver.source_range.end_pos + 1,
30
+ end_pos: node.loc.selector.end_pos
31
+ )
32
+
33
+ add_offense(new_perform_node) do |corrector|
34
+ corrector.replace(new_perform_node, 'perform_inline')
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Petal
5
- VERSION = '1.4.0'
5
+ VERSION = '1.5.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-petal
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean-Francis Bastien
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-14 00:00:00.000000000 Z
11
+ date: 2024-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -96,6 +96,7 @@ files:
96
96
  - lib/rubocop-petal.rb
97
97
  - lib/rubocop/cop/chewy/field_type.rb
98
98
  - lib/rubocop/cop/chewy/reset_on_type.rb
99
+ - lib/rubocop/cop/chewy/update_index_argument.rb
99
100
  - lib/rubocop/cop/grape/helpers_include_module.rb
100
101
  - lib/rubocop/cop/grape/prefer_namespace.rb
101
102
  - lib/rubocop/cop/grape/unnecessary_namespace.rb
@@ -126,6 +127,7 @@ files:
126
127
  - lib/rubocop/cop/sidekiq/helpers.rb
127
128
  - lib/rubocop/cop/sidekiq/keyword_arguments.rb
128
129
  - lib/rubocop/cop/sidekiq/no_nil_return.rb
130
+ - lib/rubocop/cop/sidekiq/perform_inline.rb
129
131
  - lib/rubocop/cop/sidekiq/symbol_argument.rb
130
132
  - lib/rubocop/petal.rb
131
133
  - lib/rubocop/petal/inject.rb