sevencop 0.2.0 → 0.4.1
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 +23 -0
- data/Gemfile.lock +1 -1
- data/README.md +48 -3
- data/Rakefile +2 -2
- data/config/default.yml +12 -0
- data/lib/rubocop/cop/sevencop/order_field.rb +63 -0
- data/lib/rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity.rb +94 -0
- data/lib/sevencop/version.rb +1 -1
- data/lib/sevencop.rb +2 -0
- data/{rubocop-rails_deprecation.gemspec → sevencop.gemspec} +0 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef38866fd82173f9e717566ed4ddacaa43571eda5760cc72b37ec615256e32cc
|
4
|
+
data.tar.gz: cf4aa55ea75a1eeebfae8a1d4ac311e83611a54b365911d4bf8d244fafbc587d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 744673cf9ae424e870f5a7526a61c19f3b124597e83554c81967544cfdf617992bc2d46c464347cef5f654a5aca1654486511abb85afb0c3123719b6214411f6
|
7
|
+
data.tar.gz: 81873ee0905b64eb4aa51cf7d0f93e62a87d2fd616a8c18756fe8d1ac68f023e0e8c0d5b6a2e77847bdf482edee2036956afc15248975292c10f1965488009d5
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,29 @@
|
|
2
2
|
|
3
3
|
## Unreleased
|
4
4
|
|
5
|
+
## 0.4.1 - 2022-06-23
|
6
|
+
|
7
|
+
### Fixed
|
8
|
+
|
9
|
+
- Fix `Sevencop/OrderField` on dstr.
|
10
|
+
|
11
|
+
## 0.4.0 - 2022-06-18
|
12
|
+
|
13
|
+
### Added
|
14
|
+
|
15
|
+
- Add `Sevencop/OrderField` cop.
|
16
|
+
|
17
|
+
### Changed
|
18
|
+
|
19
|
+
- Improve performance of `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
|
20
|
+
- Improve offense location of `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
|
21
|
+
|
22
|
+
## 0.3.0 - 2022-06-18
|
23
|
+
|
24
|
+
### Added
|
25
|
+
|
26
|
+
- Add `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
|
27
|
+
|
5
28
|
## 0.2.0 - 2022-06-07
|
6
29
|
|
7
30
|
### Changed
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -35,7 +35,27 @@ require:
|
|
35
35
|
|
36
36
|
## Cops
|
37
37
|
|
38
|
-
###
|
38
|
+
### Sevencop/OrderField
|
39
|
+
|
40
|
+
Identifies a String including "field" is passed to `order` or `reorder`.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
# bad
|
44
|
+
articles.order('field(id, ?)', a)
|
45
|
+
|
46
|
+
# good
|
47
|
+
articles.order(Arel.sql('field(id, ?)'), a)
|
48
|
+
|
49
|
+
# bad
|
50
|
+
reorder('field(id, ?)', a)
|
51
|
+
|
52
|
+
# good
|
53
|
+
reorder(Arel.sql('field(id, ?)'), a)
|
54
|
+
```
|
55
|
+
|
56
|
+
`Enabled: false` by default.
|
57
|
+
|
58
|
+
### Sevencop/RedundantExistenceCheck
|
39
59
|
|
40
60
|
Identifies redundant existent check before file operation.
|
41
61
|
|
@@ -44,11 +64,36 @@ Identifies redundant existent check before file operation.
|
|
44
64
|
FileUtils.mkdir(a) unless FileTest.exist?(a)
|
45
65
|
|
46
66
|
# good
|
47
|
-
FileUtils.
|
67
|
+
FileUtils.mkdir_p(a)
|
48
68
|
|
49
69
|
# bad
|
50
70
|
FileUtils.rm(a) if FileTest.exist?(a)
|
51
71
|
|
52
72
|
# good
|
53
|
-
FileUtils.
|
73
|
+
FileUtils.rm_f(a)
|
54
74
|
```
|
75
|
+
|
76
|
+
### Sevencop/UniquenessValidatorExplicitCaseSensitivity
|
77
|
+
|
78
|
+
Identifies use of UniquenessValidator without :case_sensitive option.
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# bad
|
82
|
+
validates :name, uniqueness: true
|
83
|
+
|
84
|
+
# good
|
85
|
+
validates :name, uniqueness: { case_sensitive: true }
|
86
|
+
|
87
|
+
# good
|
88
|
+
validates :name, uniqueness: { case_sensitive: false }
|
89
|
+
|
90
|
+
# bad
|
91
|
+
validates :name, uniqueness: { allow_nil: true, scope: :user_id }
|
92
|
+
|
93
|
+
# good
|
94
|
+
validates :name, uniqueness: { allow_nil: true, scope: :user_id, case_sensitive: true }
|
95
|
+
```
|
96
|
+
|
97
|
+
Useful to keep the same behavior between Rails 6.0 and 6.1 where case insensitive collation is used in MySQL.
|
98
|
+
|
99
|
+
`Enabled: false` by default.
|
data/Rakefile
CHANGED
@@ -16,7 +16,7 @@ task :new_cop, [:cop] do |_task, args|
|
|
16
16
|
require 'rubocop'
|
17
17
|
|
18
18
|
cop_name = args.fetch(:cop) do
|
19
|
-
warn
|
19
|
+
warn "usage: bundle exec rake 'new_cop[Department/Name]'"
|
20
20
|
exit!
|
21
21
|
end
|
22
22
|
|
@@ -24,7 +24,7 @@ task :new_cop, [:cop] do |_task, args|
|
|
24
24
|
|
25
25
|
generator.write_source
|
26
26
|
generator.write_spec
|
27
|
-
generator.inject_require(root_file_path: 'lib/
|
27
|
+
generator.inject_require(root_file_path: 'lib/sevencop.rb')
|
28
28
|
generator.inject_config(config_file_path: 'config/default.yml')
|
29
29
|
|
30
30
|
puts generator.todo
|
data/config/default.yml
CHANGED
@@ -1,5 +1,17 @@
|
|
1
|
+
Sevencop/OrderField:
|
2
|
+
Description: Wrap safe SQL String by `Arel.sql`.
|
3
|
+
Enabled: false
|
4
|
+
Safe: false
|
5
|
+
VersionAdded: '0.4'
|
6
|
+
|
1
7
|
Sevencop/RedundantExistenceCheck:
|
2
8
|
Description: Avoid redundant existent check before file operation.
|
3
9
|
Enabled: true
|
4
10
|
Safe: false
|
5
11
|
VersionAdded: '0.1'
|
12
|
+
|
13
|
+
Sevencop/UniquenessValidatorExplicitCaseSensitivity:
|
14
|
+
Description: Specify :case_sensitivity option on use of UniquenessValidator.
|
15
|
+
Enabled: false
|
16
|
+
Safe: false
|
17
|
+
VersionAdded: '0.3'
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Sevencop
|
6
|
+
# Identifies a String including "field" is passed to `order` or `reorder`.
|
7
|
+
#
|
8
|
+
# @safety
|
9
|
+
# This cop is unsafe because it can register a false positive.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
#
|
13
|
+
# # bad
|
14
|
+
# articles.order('field(id, ?)', a)
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# articles.order(Arel.sql('field(id, ?)'), a)
|
18
|
+
#
|
19
|
+
# # bad
|
20
|
+
# reorder('field(id, ?)', a)
|
21
|
+
#
|
22
|
+
# # good
|
23
|
+
# reorder(Arel.sql('field(id, ?)'), a)
|
24
|
+
#
|
25
|
+
class OrderField < Base
|
26
|
+
extend AutoCorrector
|
27
|
+
|
28
|
+
MSG = 'Wrap safe SQL String by `Arel.sql`.'
|
29
|
+
|
30
|
+
RESTRICT_ON_SEND = %i[
|
31
|
+
order
|
32
|
+
reorder
|
33
|
+
].freeze
|
34
|
+
|
35
|
+
ORDER_METHOD_NAMES = RESTRICT_ON_SEND.to_set.freeze
|
36
|
+
|
37
|
+
def_node_matcher :order_with_field?, <<~PATTERN
|
38
|
+
(send
|
39
|
+
_ ORDER_METHOD_NAMES
|
40
|
+
{
|
41
|
+
(str /field\(.+\)/) |
|
42
|
+
(dstr <(str /field\(.+\)/) ...>)
|
43
|
+
}
|
44
|
+
...
|
45
|
+
)
|
46
|
+
PATTERN
|
47
|
+
|
48
|
+
# @param [RuboCop::AST::SendNode] node
|
49
|
+
def on_send(node)
|
50
|
+
return unless order_with_field?(node)
|
51
|
+
|
52
|
+
first_argument_node = node.arguments.first
|
53
|
+
add_offense(first_argument_node) do |corrector|
|
54
|
+
corrector.replace(
|
55
|
+
node.arguments.first,
|
56
|
+
"Arel.sql(#{first_argument_node.source})"
|
57
|
+
)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,94 @@
|
|
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
|
+
RESTRICT_ON_SEND = %i[validates].freeze
|
35
|
+
|
36
|
+
def_node_matcher :validates_uniqueness?, <<~PATTERN
|
37
|
+
(send nil? :validates
|
38
|
+
_
|
39
|
+
(hash
|
40
|
+
<
|
41
|
+
(pair
|
42
|
+
(sym :uniqueness)
|
43
|
+
{true | (hash ...)}
|
44
|
+
)
|
45
|
+
>
|
46
|
+
...
|
47
|
+
)
|
48
|
+
)
|
49
|
+
PATTERN
|
50
|
+
|
51
|
+
def_node_matcher :validates_uniqueness_with_case_sensitivity?, <<~PATTERN
|
52
|
+
(send nil? :validates
|
53
|
+
_
|
54
|
+
(hash
|
55
|
+
<
|
56
|
+
(pair
|
57
|
+
(sym :uniqueness)
|
58
|
+
(hash <(pair (sym :case_sensitive) _) ...>)
|
59
|
+
)
|
60
|
+
...
|
61
|
+
>
|
62
|
+
)
|
63
|
+
)
|
64
|
+
PATTERN
|
65
|
+
|
66
|
+
# @param [RuboCop::AST::SendNode] node
|
67
|
+
def on_send(node)
|
68
|
+
return unless validates_uniqueness?(node) && !validates_uniqueness_with_case_sensitivity?(node)
|
69
|
+
|
70
|
+
uniqueness_value = find_uniqueness_value(node)
|
71
|
+
add_offense(uniqueness_value) do |corrector|
|
72
|
+
if uniqueness_value.true_type?
|
73
|
+
corrector.replace(
|
74
|
+
uniqueness_value.source_range,
|
75
|
+
'{ case_sensitive: true }'
|
76
|
+
)
|
77
|
+
else
|
78
|
+
corrector.insert_after(
|
79
|
+
uniqueness_value.pairs.last.source_range,
|
80
|
+
', case_sensitive: true'
|
81
|
+
)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# @param [RuboCop::AST::SendNode] node
|
87
|
+
# @param [RuboCop::AST::Node]
|
88
|
+
def find_uniqueness_value(node)
|
89
|
+
node.arguments[1].pairs.find { |pair| pair.key.value == :uniqueness }.value
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
data/lib/sevencop/version.rb
CHANGED
data/lib/sevencop.rb
CHANGED
@@ -6,7 +6,9 @@ require 'yaml'
|
|
6
6
|
require_relative 'sevencop/inject'
|
7
7
|
require_relative 'sevencop/version'
|
8
8
|
|
9
|
+
require_relative 'rubocop/cop/sevencop/order_field'
|
9
10
|
require_relative 'rubocop/cop/sevencop/redundant_existence_check'
|
11
|
+
require_relative 'rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity'
|
10
12
|
|
11
13
|
module Sevencop
|
12
14
|
PROJECT_ROOT = ::Pathname.new(__dir__).parent.expand_path.freeze
|
File without changes
|
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.4.1
|
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-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -41,11 +41,13 @@ files:
|
|
41
41
|
- README.md
|
42
42
|
- Rakefile
|
43
43
|
- config/default.yml
|
44
|
+
- lib/rubocop/cop/sevencop/order_field.rb
|
44
45
|
- lib/rubocop/cop/sevencop/redundant_existence_check.rb
|
46
|
+
- lib/rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity.rb
|
45
47
|
- lib/sevencop.rb
|
46
48
|
- lib/sevencop/inject.rb
|
47
49
|
- lib/sevencop/version.rb
|
48
|
-
-
|
50
|
+
- sevencop.gemspec
|
49
51
|
homepage: https://github.com/r7kamura/sevencop
|
50
52
|
licenses:
|
51
53
|
- MIT
|
@@ -69,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
71
|
- !ruby/object:Gem::Version
|
70
72
|
version: '0'
|
71
73
|
requirements: []
|
72
|
-
rubygems_version: 3.
|
74
|
+
rubygems_version: 3.2.32
|
73
75
|
signing_key:
|
74
76
|
specification_version: 4
|
75
77
|
summary: Custom cops for RuboCop.
|