sevencop 0.2.0 → 0.3.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 +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +27 -2
- data/config/default.yml +6 -0
- data/lib/rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity.rb +92 -0
- data/lib/sevencop/version.rb +1 -1
- data/lib/sevencop.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 18546d00ceb4ff7f756715ecdbe89e7941a51855df542b4ad2a8a05843ebc46a
|
|
4
|
+
data.tar.gz: b968132f755f723217b47fd81126bb11242e0702b267fcc9ef49eac054c9c22a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d1e87a57863fe1881b6fc0c923213d9d563cdeba24c1ab92715bab58004efbdaf3b011319b1ddc3215b5b20f443a40c99cd515754312a0ba920ff601cd6eb7ab
|
|
7
|
+
data.tar.gz: ae482542aa0d259384cba3bfd04932ae1811b9ed686245ebd3de5f1e1fa7deac48a90fcea0b2f0276afcd9244846b7b59fd4ba1ceef846d1217b505b183877c4
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -44,11 +44,36 @@ Identifies redundant existent check before file operation.
|
|
|
44
44
|
FileUtils.mkdir(a) unless FileTest.exist?(a)
|
|
45
45
|
|
|
46
46
|
# good
|
|
47
|
-
FileUtils.
|
|
47
|
+
FileUtils.mkdir_p(a)
|
|
48
48
|
|
|
49
49
|
# bad
|
|
50
50
|
FileUtils.rm(a) if FileTest.exist?(a)
|
|
51
51
|
|
|
52
52
|
# good
|
|
53
|
-
FileUtils.
|
|
53
|
+
FileUtils.rm_f(a)
|
|
54
54
|
```
|
|
55
|
+
|
|
56
|
+
### `Sevencop/UniquenessValidatorExplicitCaseSensitivity`
|
|
57
|
+
|
|
58
|
+
Identifies use of UniquenessValidator without :case_sensitive option.
|
|
59
|
+
|
|
60
|
+
```ruby
|
|
61
|
+
# bad
|
|
62
|
+
validates :name, uniqueness: true
|
|
63
|
+
|
|
64
|
+
# good
|
|
65
|
+
validates :name, uniqueness: { case_sensitive: true }
|
|
66
|
+
|
|
67
|
+
# good
|
|
68
|
+
validates :name, uniqueness: { case_sensitive: false }
|
|
69
|
+
|
|
70
|
+
# bad
|
|
71
|
+
validates :name, uniqueness: { allow_nil: true, scope: :user_id }
|
|
72
|
+
|
|
73
|
+
# good
|
|
74
|
+
validates :name, uniqueness: { allow_nil: true, scope: :user_id, case_sensitive: true }
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Useful to keep the same behavior between Rails 6.0 and 6.1 where case insensitive collation is used in MySQL.
|
|
78
|
+
|
|
79
|
+
Note that this cop is `Enabled: false` by default.
|
data/config/default.yml
CHANGED
|
@@ -3,3 +3,9 @@ Sevencop/RedundantExistenceCheck:
|
|
|
3
3
|
Enabled: true
|
|
4
4
|
Safe: false
|
|
5
5
|
VersionAdded: '0.1'
|
|
6
|
+
|
|
7
|
+
Sevencop/UniquenessValidatorExplicitCaseSensitivity:
|
|
8
|
+
Description: Specify :case_sensitivity option on use of UniquenessValidator.
|
|
9
|
+
Enabled: false
|
|
10
|
+
Safe: false
|
|
11
|
+
VersionAdded: '0.3'
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Sevencop
|
|
6
|
+
# Identifies use of UniquenessValidator without :case_sensitive option.
|
|
7
|
+
# This is useful to keep the same behavior between Rails 6.0 and 6.1 where case insensitive collation is used in MySQL.
|
|
8
|
+
#
|
|
9
|
+
# @safety
|
|
10
|
+
# This cop is unsafe because it can register a false positive.
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
#
|
|
14
|
+
# # bad
|
|
15
|
+
# validates :name, uniqueness: true
|
|
16
|
+
#
|
|
17
|
+
# # good
|
|
18
|
+
# validates :name, uniqueness: { case_sensitive: true }
|
|
19
|
+
#
|
|
20
|
+
# # good
|
|
21
|
+
# validates :name, uniqueness: { case_sensitive: false }
|
|
22
|
+
#
|
|
23
|
+
# # bad
|
|
24
|
+
# validates :name, uniqueness: { allow_nil: true, scope: :user_id }
|
|
25
|
+
#
|
|
26
|
+
# # good
|
|
27
|
+
# validates :name, uniqueness: { allow_nil: true, scope: :user_id, case_sensitive: true }
|
|
28
|
+
#
|
|
29
|
+
class UniquenessValidatorExplicitCaseSensitivity < Base
|
|
30
|
+
extend AutoCorrector
|
|
31
|
+
|
|
32
|
+
MSG = 'Specify :case_sensitivity option on use of UniquenessValidator.'
|
|
33
|
+
|
|
34
|
+
def_node_matcher :validates_uniqueness?, <<~PATTERN
|
|
35
|
+
(send nil? :validates
|
|
36
|
+
_
|
|
37
|
+
(hash
|
|
38
|
+
<
|
|
39
|
+
(pair
|
|
40
|
+
(sym :uniqueness)
|
|
41
|
+
{true | (hash ...)}
|
|
42
|
+
)
|
|
43
|
+
>
|
|
44
|
+
...
|
|
45
|
+
)
|
|
46
|
+
)
|
|
47
|
+
PATTERN
|
|
48
|
+
|
|
49
|
+
def_node_matcher :validates_uniqueness_with_case_sensitivity?, <<~PATTERN
|
|
50
|
+
(send nil? :validates
|
|
51
|
+
_
|
|
52
|
+
(hash
|
|
53
|
+
<
|
|
54
|
+
(pair
|
|
55
|
+
(sym :uniqueness)
|
|
56
|
+
(hash <(pair (sym :case_sensitive) _) ...>)
|
|
57
|
+
)
|
|
58
|
+
...
|
|
59
|
+
>
|
|
60
|
+
)
|
|
61
|
+
)
|
|
62
|
+
PATTERN
|
|
63
|
+
|
|
64
|
+
# @param [RuboCop::AST::SendNode] node
|
|
65
|
+
def on_send(node)
|
|
66
|
+
return unless validates_uniqueness?(node) && !validates_uniqueness_with_case_sensitivity?(node)
|
|
67
|
+
|
|
68
|
+
add_offense(node) do |corrector|
|
|
69
|
+
uniqueness_value = find_uniqueness_value(node)
|
|
70
|
+
if uniqueness_value.true_type?
|
|
71
|
+
corrector.replace(
|
|
72
|
+
uniqueness_value.source_range,
|
|
73
|
+
'{ case_sensitive: true }'
|
|
74
|
+
)
|
|
75
|
+
else
|
|
76
|
+
corrector.insert_after(
|
|
77
|
+
uniqueness_value.pairs.last.source_range,
|
|
78
|
+
', case_sensitive: true'
|
|
79
|
+
)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# @param [RuboCop::AST::SendNode] node
|
|
85
|
+
# @param [RuboCop::AST::Node]
|
|
86
|
+
def find_uniqueness_value(node)
|
|
87
|
+
node.arguments[1].pairs.find { |pair| pair.key.value == :uniqueness }.value
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
data/lib/sevencop/version.rb
CHANGED
data/lib/sevencop.rb
CHANGED
|
@@ -7,6 +7,7 @@ require_relative 'sevencop/inject'
|
|
|
7
7
|
require_relative 'sevencop/version'
|
|
8
8
|
|
|
9
9
|
require_relative 'rubocop/cop/sevencop/redundant_existence_check'
|
|
10
|
+
require_relative 'rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity'
|
|
10
11
|
|
|
11
12
|
module Sevencop
|
|
12
13
|
PROJECT_ROOT = ::Pathname.new(__dir__).parent.expand_path.freeze
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sevencop
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ryo Nakamura
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2022-06-
|
|
11
|
+
date: 2022-06-17 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rubocop
|
|
@@ -42,6 +42,7 @@ files:
|
|
|
42
42
|
- Rakefile
|
|
43
43
|
- config/default.yml
|
|
44
44
|
- lib/rubocop/cop/sevencop/redundant_existence_check.rb
|
|
45
|
+
- lib/rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity.rb
|
|
45
46
|
- lib/sevencop.rb
|
|
46
47
|
- lib/sevencop/inject.rb
|
|
47
48
|
- lib/sevencop/version.rb
|