rubocop-petal 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/default.yml +28 -0
- data/lib/rubocop/cop/chewy/field_type.rb +31 -0
- data/lib/rubocop/cop/migration/change_table_references.rb +7 -6
- data/lib/rubocop/cop/migration/standalone_add_reference.rb +32 -0
- data/lib/rubocop/cop/rails/destroy_all_bang.rb +30 -0
- data/lib/rubocop/cop/rails/enum_starting_value.rb +44 -0
- data/lib/rubocop/cop/sidekiq/no_nil_return.rb +28 -0
- data/lib/rubocop/petal/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 308ace8925b50301e05d8fec4a5a94846dbe2b6161196ef71555add216dbc177
|
4
|
+
data.tar.gz: 0dde2a25fde7f35d5d75346cb7cdb825fc451dbddba99056fb1c79782d575433
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dde8a2affe791c2ba4903e5d5a5c0360b477b4ef06668920d8d04eeb106526ccb9d79fc1bf3b8f992a03af80671ccab3d41a51518fee680e61556ae2a590f9a8
|
7
|
+
data.tar.gz: d636067ac2f064154c751c316ca7dc345a5ead4df64de07cc9948de8feb32da5d648ce4727cb0608ae70e70232e0444c39e45c7e52987d19709bb797c3795aea
|
data/config/default.yml
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
Chewy/FieldType:
|
2
|
+
Description: 'Assure that a type is defined for a Chewy field.'
|
3
|
+
Enabled: true
|
4
|
+
Include:
|
5
|
+
- app/chewy/**/*
|
6
|
+
|
1
7
|
Grape/PreferNamespace:
|
2
8
|
Description: 'Prevent usage of namespace aliases.'
|
3
9
|
Enabled: true
|
@@ -29,6 +35,12 @@ Migration/SchemaStatementsMethods:
|
|
29
35
|
Include:
|
30
36
|
- db/migrate/**
|
31
37
|
|
38
|
+
Migration/StandaloneAddReference:
|
39
|
+
Description: 'Prevent using `add_reference/belongs_to` outside of a change_table.'
|
40
|
+
Enabled: true
|
41
|
+
Include:
|
42
|
+
- db/migrate/**
|
43
|
+
|
32
44
|
RSpec/AuthenticatedAs:
|
33
45
|
Description: 'Suggest to use authenticated_as instead of legacy api_key.'
|
34
46
|
Enabled: true
|
@@ -68,6 +80,11 @@ Grape/UnnecessaryNamespace:
|
|
68
80
|
Include:
|
69
81
|
- app/api/**/*
|
70
82
|
|
83
|
+
Rails/DestroyAllBang:
|
84
|
+
Description: 'Prevent using `destroy_all` in favor of `each(&:destroy!)` to go along Rails/SaveBang cop.'
|
85
|
+
StyleGuide: https://rails.rubystyle.guide#save-bang
|
86
|
+
Enabled: true
|
87
|
+
|
71
88
|
Rails/EnumPrefix:
|
72
89
|
Description: 'Set prefix options when using enums.'
|
73
90
|
Enabled: true
|
@@ -75,6 +92,13 @@ Rails/EnumPrefix:
|
|
75
92
|
Include:
|
76
93
|
- app/models/**/*
|
77
94
|
|
95
|
+
Rails/EnumStartingValue:
|
96
|
+
Description: 'Prevent starting from zero with an enum.'
|
97
|
+
Enabled: true
|
98
|
+
StyleGuide: https://github.com/petalmd/rubocop-petal/issues/56
|
99
|
+
Include:
|
100
|
+
- app/models/**/*
|
101
|
+
|
78
102
|
Rails/RiskyActiverecordInvocation:
|
79
103
|
Description: 'Interpolation, use hash or parameterized syntax.'
|
80
104
|
Enabled: true
|
@@ -93,3 +117,7 @@ RSpec/JsonResponse:
|
|
93
117
|
Performance/Snif:
|
94
118
|
Description: 'Prevent snif in favor of detect'
|
95
119
|
Enabled: true
|
120
|
+
|
121
|
+
Sidekiq/NoNilReturn:
|
122
|
+
Description: 'Prevent early nil return in workers'
|
123
|
+
Enabled: false
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Chewy
|
6
|
+
# This cop force to specify a type for Chewy field
|
7
|
+
#
|
8
|
+
# # bad
|
9
|
+
# field :name
|
10
|
+
#
|
11
|
+
# # good
|
12
|
+
# field :name, type: 'text'
|
13
|
+
class FieldType < Base
|
14
|
+
MSG = 'Specify a `type` for Chewy field.'
|
15
|
+
|
16
|
+
RESTRICT_ON_SEND = %i[field].freeze
|
17
|
+
|
18
|
+
# @!method options_has_field?(node)
|
19
|
+
def_node_matcher :options_has_field?, <<~PATTERN
|
20
|
+
(send nil? :field (sym _)+ (hash <(pair (sym :type) {str sym}) ...>))
|
21
|
+
PATTERN
|
22
|
+
|
23
|
+
def on_send(node)
|
24
|
+
return if options_has_field?(node)
|
25
|
+
|
26
|
+
add_offense(node)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -21,22 +21,23 @@ module RuboCop
|
|
21
21
|
# t.foreign_key :users, column: :user_id
|
22
22
|
# end
|
23
23
|
class ChangeTableReferences < Base
|
24
|
-
MSG = 'Use a combination of `t.bigint`, `t.index` and `t.foreign_key` in a change_table.'
|
24
|
+
MSG = 'Use a combination of `t.bigint`, `t.index` and `t.foreign_key` in a change_table to add a reference.' \
|
25
|
+
'Or `t.remove_foreign_key`, `t.remove` to remove a reference.'
|
25
26
|
|
26
27
|
# @!method add_references_in_block?(node)
|
27
|
-
|
28
|
-
(send lvar /references|belongs_to/ ...)
|
28
|
+
def_node_matcher :add_references_in_block?, <<~PATTERN
|
29
|
+
(send lvar /references|belongs_to|remove_references|remove_belongs_to/ ...)
|
29
30
|
PATTERN
|
30
31
|
|
31
32
|
# @!method change_table?(node)
|
32
|
-
|
33
|
-
(send nil? :change_table ...)
|
33
|
+
def_node_matcher :change_table?, <<~PATTERN
|
34
|
+
(block (send nil? :change_table ...) ...)
|
34
35
|
PATTERN
|
35
36
|
|
36
37
|
def on_block(node)
|
37
38
|
return unless change_table?(node)
|
38
39
|
|
39
|
-
references_node = node.
|
40
|
+
references_node = node.child_nodes[2].each_node.detect { |n| add_references_in_block?(n) }
|
40
41
|
return unless references_node
|
41
42
|
|
42
43
|
arguments = references_node.child_nodes[1]
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Migration
|
6
|
+
# Prevent using `add_reference` and `remove_reference` outside of
|
7
|
+
# a `change_table` block. `add_reference` create multiples `ALTER TABLE`
|
8
|
+
# statements. Using `change_table` with `bulk: true` is more efficient.
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# add_reference :products, :user, foreign_key: true
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# change_table :products, bulk: true do |t|
|
15
|
+
# t.bigint :user_id, null: false
|
16
|
+
# t.index :user_id
|
17
|
+
# t.foreign_key :users, column: :user_id
|
18
|
+
# end
|
19
|
+
class StandaloneAddReference < Base
|
20
|
+
MSG = 'Modifying references must be done in a change_table block.'
|
21
|
+
|
22
|
+
RESTRICT_ON_SEND = %i[add_reference add_belongs_to remove_reference remove_belongs_to].freeze
|
23
|
+
|
24
|
+
def on_send(node)
|
25
|
+
reference_method = node.source_range.with(end_pos: node.child_nodes.first.source_range.begin_pos - 1)
|
26
|
+
|
27
|
+
add_offense(reference_method)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Cop to enforce the use of `each(&:destroy!)` over `destroy_all`.
|
7
|
+
# https://docs.rubocop.org/rubocop-rails/cops_rails.html#railssavebang
|
8
|
+
# https://github.com/rails/rails/pull/37782
|
9
|
+
# https://discuss.rubyonrails.org/t/proposal-add-destroy-all-method-to-activerecord-relation/80959
|
10
|
+
#
|
11
|
+
# # bad
|
12
|
+
# User.destroy_all
|
13
|
+
#
|
14
|
+
# # good
|
15
|
+
# User.each(&:destroy!)
|
16
|
+
class DestroyAllBang < Base
|
17
|
+
extend AutoCorrector
|
18
|
+
MSG = 'Use `each(&:destroy!)` instead of `destroy_all`.'
|
19
|
+
RESTRICT_ON_SEND = %i[destroy_all].freeze
|
20
|
+
|
21
|
+
def on_send(node)
|
22
|
+
destroy_all_range = node.loc.selector
|
23
|
+
add_offense(destroy_all_range) do |corrector|
|
24
|
+
corrector.replace(node.loc.selector, 'each(&:destroy!)')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Prevent the user to start from Zero with an enum.
|
7
|
+
# When using a wrong value like .where(state: :patato) let Rails + MySQL do a WHERE state = 0.
|
8
|
+
# It will match nothing since no record will have a 0 value.
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# enum my_enum: {apple: 0, bannana: 1}
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# enum my_enum: {apple: 1, banana: 2}
|
15
|
+
|
16
|
+
class EnumStartingValue < Base
|
17
|
+
MSG = 'Prefer starting from `1` instead of `0` with `enum`.'
|
18
|
+
|
19
|
+
def_node_matcher :enum?, <<~PATTERN
|
20
|
+
(send nil? :enum (hash ...))
|
21
|
+
PATTERN
|
22
|
+
|
23
|
+
def_node_matcher :enum_attributes, <<~PATTERN
|
24
|
+
(send nil? :enum (:hash (:pair (...)$(...) )...))
|
25
|
+
PATTERN
|
26
|
+
|
27
|
+
def on_send(node)
|
28
|
+
return unless enum? node
|
29
|
+
|
30
|
+
add_offense(node) if start_with_zero?(enum_attributes(node))
|
31
|
+
end
|
32
|
+
|
33
|
+
def start_with_zero?(node)
|
34
|
+
return unless node.type == :hash
|
35
|
+
|
36
|
+
node.children.any? do |child|
|
37
|
+
value = child.value
|
38
|
+
value.type == :int && value.value.zero?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Sidekiq
|
6
|
+
# Ensure workers avoid returning early
|
7
|
+
#
|
8
|
+
# # bad
|
9
|
+
# def perform
|
10
|
+
# return if condition
|
11
|
+
# ...
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# # good
|
15
|
+
# def perform
|
16
|
+
# # TDB, idea would be `raise SilentError if condition`
|
17
|
+
# end
|
18
|
+
|
19
|
+
class NoNilReturn < Base
|
20
|
+
MSG = 'Avoid using early nil return in workers.'
|
21
|
+
|
22
|
+
def on_return(node)
|
23
|
+
add_offense(node) if node.arguments.first&.nil_type? || node.arguments.empty?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
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
|
+
version: 1.2.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: 2023-
|
11
|
+
date: 2023-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- config/base.yml
|
81
81
|
- config/default.yml
|
82
82
|
- lib/rubocop-petal.rb
|
83
|
+
- lib/rubocop/cop/chewy/field_type.rb
|
83
84
|
- lib/rubocop/cop/chewy/reset_on_type.rb
|
84
85
|
- lib/rubocop/cop/grape/helpers_include_module.rb
|
85
86
|
- lib/rubocop/cop/grape/prefer_namespace.rb
|
@@ -88,15 +89,19 @@ files:
|
|
88
89
|
- lib/rubocop/cop/migration/change_table_references.rb
|
89
90
|
- lib/rubocop/cop/migration/foreign_key_option.rb
|
90
91
|
- lib/rubocop/cop/migration/schema_statements_methods.rb
|
92
|
+
- lib/rubocop/cop/migration/standalone_add_reference.rb
|
91
93
|
- lib/rubocop/cop/performance/snif.rb
|
92
94
|
- lib/rubocop/cop/petal_cops.rb
|
95
|
+
- lib/rubocop/cop/rails/destroy_all_bang.rb
|
93
96
|
- lib/rubocop/cop/rails/enum_prefix.rb
|
97
|
+
- lib/rubocop/cop/rails/enum_starting_value.rb
|
94
98
|
- lib/rubocop/cop/rails/risky_activerecord_invocation.rb
|
95
99
|
- lib/rubocop/cop/rspec/authenticated_as.rb
|
96
100
|
- lib/rubocop/cop/rspec/create_list_max.rb
|
97
101
|
- lib/rubocop/cop/rspec/json_response.rb
|
98
102
|
- lib/rubocop/cop/rspec/sidekiq_inline.rb
|
99
103
|
- lib/rubocop/cop/rspec/stub_products.rb
|
104
|
+
- lib/rubocop/cop/sidekiq/no_nil_return.rb
|
100
105
|
- lib/rubocop/petal.rb
|
101
106
|
- lib/rubocop/petal/inject.rb
|
102
107
|
- lib/rubocop/petal/version.rb
|