rubocop-petal 0.3.1 → 0.5.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 +4 -4
- data/CHANGELOG.md +13 -1
- data/Gemfile.lock +2 -2
- data/config/default.yml +15 -0
- data/lib/rubocop/cop/rails/validate_uniqueness_case.rb +89 -0
- data/lib/rubocop/cop/rspec/stub_products.rb +51 -0
- data/lib/rubocop/petal/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa2179fb3df37d962557546e94f204ee9a8789d2850cda31a53e7375b47edb50
|
4
|
+
data.tar.gz: dfeedd44694e75eab2c78f7205d93701a356378d147d6dca9e5adf68bf1e805f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df124d64a0b69c1311c86e47393fd94379f89fac39d5f6752ec8af6f97df783764fcf581de9c5e11d79d5245bba37f8f07464d29d86f102c9b07056c610ffb8c
|
7
|
+
data.tar.gz: dec1a550aa7fbfdfeae58457bd668d671b21a0db0933685d253c18408b913bde81ccbf0e7fbb6ab1b5966741a6d4b3590956fc2641a11a0fda40d55c9980bc9a
|
data/CHANGELOG.md
CHANGED
@@ -2,9 +2,21 @@
|
|
2
2
|
|
3
3
|
# main
|
4
4
|
|
5
|
+
#v0.5.0
|
6
|
+
|
7
|
+
* Added cop `Rails/ValidateUniquenessCase` ([#20](https://github.com/petalmd/rubocop-petal/pull/20))
|
8
|
+
|
9
|
+
#v0.4.1
|
10
|
+
|
11
|
+
* Fix typo default config SafeAutoCorrect RSpec/StubProducts ([#19](https://github.com/petalmd/rubocop-petal/pull/19))
|
12
|
+
|
13
|
+
#v0.4.0
|
14
|
+
|
15
|
+
* Added cop `RSpec/StubProducts` ([#18](https://github.com/petalmd/rubocop-petal/pull/18))
|
16
|
+
|
5
17
|
# v0.3.1
|
6
18
|
|
7
|
-
Correct cop name `Migration/SchemaStatementsMethods` in config ([#17](https://github.com/petalmd/rubocop-petal/pull/17))
|
19
|
+
* Correct cop name `Migration/SchemaStatementsMethods` in config ([#17](https://github.com/petalmd/rubocop-petal/pull/17))
|
8
20
|
|
9
21
|
# v0.3.0
|
10
22
|
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rubocop-petal (0.
|
4
|
+
rubocop-petal (0.5.0)
|
5
5
|
rubocop (>= 1.7.0, < 2.0)
|
6
6
|
rubocop-rails (~> 2.10)
|
7
7
|
|
@@ -51,7 +51,7 @@ GEM
|
|
51
51
|
unicode-display_width (>= 1.4.0, < 3.0)
|
52
52
|
rubocop-ast (1.15.0)
|
53
53
|
parser (>= 3.0.1.1)
|
54
|
-
rubocop-rails (2.
|
54
|
+
rubocop-rails (2.14.2)
|
55
55
|
activesupport (>= 4.2.0)
|
56
56
|
rack (>= 1.1)
|
57
57
|
rubocop (>= 1.7.0, < 2.0)
|
data/config/default.yml
CHANGED
@@ -30,6 +30,13 @@ RSpec/CreateListMax:
|
|
30
30
|
Include:
|
31
31
|
- spec/**/*
|
32
32
|
|
33
|
+
RSpec/StubProducts:
|
34
|
+
Description: 'Suggest to use stub_products instead of veil/unveil_product.'
|
35
|
+
Enabled: true
|
36
|
+
SafeAutoCorrect: false
|
37
|
+
Include:
|
38
|
+
- spec/**/*
|
39
|
+
|
33
40
|
Grape/HelpersIncludeModule:
|
34
41
|
Description: 'Prevent using helpers with block to include module'
|
35
42
|
Enabled: true
|
@@ -63,3 +70,11 @@ Rails/TableName:
|
|
63
70
|
Chewy/ResetOnType:
|
64
71
|
Description: 'Prevent using reset! methods on Chewy type class'
|
65
72
|
Enabled: true
|
73
|
+
|
74
|
+
Rails/ValidateUniquenessCase:
|
75
|
+
Description: 'Enforce setting uniqueness case sensitive option.'
|
76
|
+
Enabled: true
|
77
|
+
SafeAutoCorrect: false
|
78
|
+
StyleGuide: 'https://guides.rubyonrails.org/6_1_release_notes.html#active-record-notable-changes'
|
79
|
+
Include:
|
80
|
+
- app/models/**/*
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Enforces to specify a case sensitive or case insensitive option
|
7
|
+
# to the uniqueness validation.
|
8
|
+
#
|
9
|
+
# The default value in Rails 5 is `case_sensitive: true`. Which does not
|
10
|
+
# reflect MySQL's default behavior. In Rails 6.1 the default value will be
|
11
|
+
# `case_sensitive: false` to reflect MySQL's default behavior
|
12
|
+
# (https://guides.rubyonrails.org/6_1_release_notes.html#active-record-notable-changes).
|
13
|
+
#
|
14
|
+
# # bad
|
15
|
+
# validates :name, uniqueness: true
|
16
|
+
# validates :name, uniqueness: { scope: :user_id }
|
17
|
+
#
|
18
|
+
# # good
|
19
|
+
# validates :name, uniqueness: { case_sensitive: true }
|
20
|
+
# validates :name, uniqueness: { scope: :user_id, case_sensitive: false }
|
21
|
+
#
|
22
|
+
class ValidateUniquenessCase < Base
|
23
|
+
extend AutoCorrector
|
24
|
+
|
25
|
+
# Don't force to add the `case_sensitive` option when
|
26
|
+
# the default value is already `false` in Rails 6.
|
27
|
+
MAXIMUM_RAILS_VERSION = 6.1
|
28
|
+
MSG = 'Pass `case_sensitive: true|false` to uniqueness options.'
|
29
|
+
RESTRICT_ON_SEND = %i[validates].freeze
|
30
|
+
|
31
|
+
def_node_search :uniqueness?, <<~PATTERN
|
32
|
+
(sym :uniqueness)
|
33
|
+
PATTERN
|
34
|
+
|
35
|
+
def_node_matcher :have_case_sensitive_options?, <<~PATTERN
|
36
|
+
(pair (sym :case_sensitive) ${true false})
|
37
|
+
PATTERN
|
38
|
+
|
39
|
+
def on_send(node)
|
40
|
+
return unless support_target_rails_version?
|
41
|
+
|
42
|
+
uniqueness_options = match_uniqueness_options(node)
|
43
|
+
return unless uniqueness_options
|
44
|
+
|
45
|
+
uniqueness_child_options = uniqueness_options.children.last
|
46
|
+
|
47
|
+
# When it's just `uniqueness: true`
|
48
|
+
if uniqueness_child_options.boolean_type?
|
49
|
+
boolean_uniqueness(uniqueness_options)
|
50
|
+
return
|
51
|
+
end
|
52
|
+
|
53
|
+
case_sensitive_options = uniqueness_child_options.pairs.detect { |c| have_case_sensitive_options?(c) }
|
54
|
+
hash_uniqueness_offense(uniqueness_options, case_sensitive_options)
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def match_uniqueness_options(node)
|
60
|
+
node.children.last.children.detect do |c|
|
61
|
+
# Skip if not a hash
|
62
|
+
next if c.is_a?(Symbol)
|
63
|
+
|
64
|
+
uniqueness?(c)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def boolean_uniqueness(node)
|
69
|
+
# When it's just `uniqueness: true`
|
70
|
+
add_offense(node) do |corrector|
|
71
|
+
corrector.replace(node, 'uniqueness: { case_sensitive: false }')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def hash_uniqueness_offense(node, sensitive_options)
|
76
|
+
return if sensitive_options
|
77
|
+
|
78
|
+
add_offense(node) do |corrector|
|
79
|
+
corrector.insert_after(node.loc.expression.adjust(end_pos: -2), ', case_sensitive: false')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def support_target_rails_version?
|
84
|
+
target_rails_version < MAXIMUM_RAILS_VERSION
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module RSpec
|
6
|
+
# Suggest to use stub_products instead of veil/unveil_product
|
7
|
+
# which queries the database and can cause flaky tests.
|
8
|
+
#
|
9
|
+
# # bad
|
10
|
+
# unveil_product('MY_PRODUCT')
|
11
|
+
# veil_product('MY_PRODUCT')
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# stub_products('MY_PRODUCT' => true)
|
15
|
+
# stub_products('MY_PRODUCT' => false)
|
16
|
+
# stub_products('MY_PRODUCT' => group)
|
17
|
+
# stub_products('MY_PRODUCT' => [group1, group2])
|
18
|
+
# stub_products(MY_PRODUCT: true)
|
19
|
+
#
|
20
|
+
class StubProducts < Base
|
21
|
+
extend AutoCorrector
|
22
|
+
|
23
|
+
MSG = 'Use `stub_products` instead of veil/unveil_product.'
|
24
|
+
|
25
|
+
def_node_search :veil_product?, <<~PATTERN
|
26
|
+
(send nil? :veil_product _)
|
27
|
+
PATTERN
|
28
|
+
|
29
|
+
def_node_search :unveil_product?, <<~PATTERN
|
30
|
+
(send nil? :unveil_product _)
|
31
|
+
PATTERN
|
32
|
+
|
33
|
+
def on_send(node)
|
34
|
+
return unless veil_product?(node) || unveil_product?(node)
|
35
|
+
|
36
|
+
add_offense(node) do |corrector|
|
37
|
+
if (match = /^\S*\s+(\S+)|\(([^)]+)\)/.match(node.source))
|
38
|
+
match1, match2 = match.captures
|
39
|
+
product_code = match1 || match2
|
40
|
+
|
41
|
+
product_is_available = !veil_product?(node)
|
42
|
+
subst = "stub_products(#{product_code} => #{product_is_available})"
|
43
|
+
|
44
|
+
corrector.replace(node, subst)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
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: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean-Francis Bastien
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -75,8 +75,10 @@ files:
|
|
75
75
|
- lib/rubocop/cop/rails/enum_prefix.rb
|
76
76
|
- lib/rubocop/cop/rails/risky_activerecord_invocation.rb
|
77
77
|
- lib/rubocop/cop/rails/table_name.rb
|
78
|
+
- lib/rubocop/cop/rails/validate_uniqueness_case.rb
|
78
79
|
- lib/rubocop/cop/rspec/authenticated_as.rb
|
79
80
|
- lib/rubocop/cop/rspec/create_list_max.rb
|
81
|
+
- lib/rubocop/cop/rspec/stub_products.rb
|
80
82
|
- lib/rubocop/petal.rb
|
81
83
|
- lib/rubocop/petal/inject.rb
|
82
84
|
- lib/rubocop/petal/version.rb
|