sevencop 0.6.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +0 -7
- data/CHANGELOG.md +18 -0
- data/Gemfile.lock +13 -11
- data/README.md +23 -1
- data/config/default.yml +14 -0
- data/lib/rubocop/cop/sevencop/hash_literal_order.rb +19 -14
- data/lib/rubocop/cop/sevencop/to_s_with_argument.rb +39 -0
- data/lib/rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity.rb +1 -1
- data/lib/rubocop/cop/sevencop/where_not.rb +45 -0
- data/lib/sevencop/version.rb +1 -1
- data/lib/sevencop.rb +2 -0
- 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: 021c0c8e909037a665540671725553ec8a0a071aea513ea2ad39e62a104141cc
|
4
|
+
data.tar.gz: af3cbb3aaae938b0fa209995ca76dd65622aed3a4039d48a91dd4eaada3108eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df542292fe608cc072d4770263cbcaa3df4ad040d3b59c93362b3188ac54ab56285a36199451a95476927b85cbb92d67c164d3cf5b184834c87a78d3745ef343
|
7
|
+
data.tar.gz: 7289c90669cbe86f3eb025b71d2083fd60b1993ba8e5035bea7f06aecb2f1ccd8c274e53b29daea81358ac5d641a5be325189cda929035fae8b0dd127a4b360a
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,24 @@
|
|
2
2
|
|
3
3
|
## Unreleased
|
4
4
|
|
5
|
+
## 0.8.0 - 2022-07-20
|
6
|
+
|
7
|
+
### Added
|
8
|
+
|
9
|
+
- Add `Sevencop/ToSWithArgument` cop.
|
10
|
+
|
11
|
+
## 0.7.0 - 2022-07-15
|
12
|
+
|
13
|
+
### Added
|
14
|
+
|
15
|
+
- Add `Sevencop/WhereNot` cop.
|
16
|
+
|
17
|
+
## 0.6.1 - 2022-07-04
|
18
|
+
|
19
|
+
### Fixed
|
20
|
+
|
21
|
+
- Fix `Sevencop/HashLiteralOrder` on surrounding-space-less Hash.
|
22
|
+
|
5
23
|
## 0.6.0 - 2022-07-04
|
6
24
|
|
7
25
|
### Added
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sevencop (0.
|
4
|
+
sevencop (0.8.0)
|
5
5
|
rubocop
|
6
6
|
|
7
7
|
GEM
|
@@ -9,12 +9,13 @@ GEM
|
|
9
9
|
specs:
|
10
10
|
ast (2.4.2)
|
11
11
|
diff-lcs (1.5.0)
|
12
|
-
|
13
|
-
|
12
|
+
json (2.6.2)
|
13
|
+
parallel (1.22.1)
|
14
|
+
parser (3.1.2.0)
|
14
15
|
ast (~> 2.4.1)
|
15
16
|
rainbow (3.1.1)
|
16
17
|
rake (13.0.6)
|
17
|
-
regexp_parser (2.
|
18
|
+
regexp_parser (2.5.0)
|
18
19
|
rexml (3.2.5)
|
19
20
|
rspec (3.11.0)
|
20
21
|
rspec-core (~> 3.11.0)
|
@@ -25,23 +26,24 @@ GEM
|
|
25
26
|
rspec-expectations (3.11.0)
|
26
27
|
diff-lcs (>= 1.2.0, < 2.0)
|
27
28
|
rspec-support (~> 3.11.0)
|
28
|
-
rspec-mocks (3.11.
|
29
|
+
rspec-mocks (3.11.1)
|
29
30
|
diff-lcs (>= 1.2.0, < 2.0)
|
30
31
|
rspec-support (~> 3.11.0)
|
31
32
|
rspec-support (3.11.0)
|
32
|
-
rubocop (1.
|
33
|
+
rubocop (1.31.2)
|
34
|
+
json (~> 2.3)
|
33
35
|
parallel (~> 1.10)
|
34
36
|
parser (>= 3.1.0.0)
|
35
37
|
rainbow (>= 2.2.2, < 4.0)
|
36
38
|
regexp_parser (>= 1.8, < 3.0)
|
37
|
-
rexml
|
38
|
-
rubocop-ast (>= 1.
|
39
|
+
rexml (>= 3.2.5, < 4.0)
|
40
|
+
rubocop-ast (>= 1.18.0, < 2.0)
|
39
41
|
ruby-progressbar (~> 1.7)
|
40
42
|
unicode-display_width (>= 1.4.0, < 3.0)
|
41
|
-
rubocop-ast (1.
|
43
|
+
rubocop-ast (1.19.1)
|
42
44
|
parser (>= 3.1.1.0)
|
43
45
|
ruby-progressbar (1.11.0)
|
44
|
-
unicode-display_width (2.
|
46
|
+
unicode-display_width (2.2.0)
|
45
47
|
|
46
48
|
PLATFORMS
|
47
49
|
x86_64-linux
|
@@ -53,4 +55,4 @@ DEPENDENCIES
|
|
53
55
|
sevencop!
|
54
56
|
|
55
57
|
BUNDLED WITH
|
56
|
-
2.3.
|
58
|
+
2.3.17
|
data/README.md
CHANGED
@@ -27,12 +27,22 @@ gem install sevencop
|
|
27
27
|
|
28
28
|
## Usage
|
29
29
|
|
30
|
+
Require sevencop from your .rubocop.yml:
|
31
|
+
|
30
32
|
```yaml
|
31
33
|
# .rubocop.yml
|
32
34
|
require:
|
33
35
|
- sevencop
|
34
36
|
```
|
35
37
|
|
38
|
+
then enable the cops you want to use:
|
39
|
+
|
40
|
+
```yaml
|
41
|
+
# .rubocop.yml
|
42
|
+
Sevencop/BelongsToOptional:
|
43
|
+
Enabled: true
|
44
|
+
```
|
45
|
+
|
36
46
|
## Cops
|
37
47
|
|
38
48
|
All cops are `Enabled: false` by default.
|
@@ -59,7 +69,7 @@ This is useful for migration of `config.active_record.belongs_to_required_by_def
|
|
59
69
|
|
60
70
|
### Sevencop/HashLiteralOrder
|
61
71
|
|
62
|
-
Sort Hash literal by key.
|
72
|
+
Sort Hash literal entries by key.
|
63
73
|
|
64
74
|
```ruby
|
65
75
|
# bad
|
@@ -135,3 +145,15 @@ validates :name, uniqueness: { allow_nil: true, scope: :user_id, case_sensitive:
|
|
135
145
|
```
|
136
146
|
|
137
147
|
Useful to keep the same behavior between Rails 6.0 and 6.1 where case insensitive collation is used in MySQL.
|
148
|
+
|
149
|
+
### Sevencop/WhereNot
|
150
|
+
|
151
|
+
Identifies passing multi-elements Hash literal to `where.not`.
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
# bad
|
155
|
+
where.not(key1: value1, key2: value2)
|
156
|
+
|
157
|
+
# good
|
158
|
+
where.not(key1: value1).where.not(key2: value2)
|
159
|
+
```
|
data/config/default.yml
CHANGED
@@ -25,9 +25,23 @@ Sevencop/RedundantExistenceCheck:
|
|
25
25
|
Safe: false
|
26
26
|
VersionAdded: '0.1'
|
27
27
|
|
28
|
+
Sevencop/ToSWithArgument:
|
29
|
+
Description: |
|
30
|
+
Identifies passing any argument to `#to_s`.
|
31
|
+
Enabled: false
|
32
|
+
Safe: false
|
33
|
+
VersionAdded: '0.8'
|
34
|
+
|
28
35
|
Sevencop/UniquenessValidatorExplicitCaseSensitivity:
|
29
36
|
Description: |
|
30
37
|
Specify :case_sensitivity option on use of UniquenessValidator.
|
31
38
|
Enabled: false
|
32
39
|
Safe: false
|
33
40
|
VersionAdded: '0.3'
|
41
|
+
|
42
|
+
Sevencop/WhereNot:
|
43
|
+
Description: |
|
44
|
+
Identifies passing multi-elements Hash literal to `where.not`.
|
45
|
+
Enabled: false
|
46
|
+
Safe: false
|
47
|
+
VersionAdded: '0.7'
|
@@ -54,22 +54,15 @@ module RuboCop
|
|
54
54
|
# @param [RuboCop::AST::HashNode] node
|
55
55
|
# @return [String]
|
56
56
|
def autocorrect(node)
|
57
|
-
whitespace = whitespace_leading(node)
|
58
57
|
[
|
59
58
|
'{',
|
60
|
-
|
61
|
-
sort(node.pairs).map(&:source).join(",#{
|
62
|
-
|
59
|
+
whitespace_leading(node),
|
60
|
+
sort(node.pairs).map(&:source).join(",#{whitespace_between(node)}"),
|
61
|
+
whitespace_trailing(node),
|
63
62
|
'}'
|
64
63
|
].join
|
65
64
|
end
|
66
65
|
|
67
|
-
# @param [RuboCop::AST::HashNode] node
|
68
|
-
# @return [Boolean]
|
69
|
-
def multi_line?(node)
|
70
|
-
node.source.include?("\n")
|
71
|
-
end
|
72
|
-
|
73
66
|
# @param [Array<RuboCop::AST::PairNode>] pairs
|
74
67
|
# @return [Array<RuboCop::AST::PairNode>]
|
75
68
|
def sort(pairs)
|
@@ -86,15 +79,27 @@ module RuboCop
|
|
86
79
|
|
87
80
|
# @param [RuboCop::AST::HashNode] node
|
88
81
|
# @return [String]
|
89
|
-
# { a: 1,
|
90
|
-
#
|
91
|
-
def
|
82
|
+
# { a: 1, b: 1 }
|
83
|
+
# ^^^
|
84
|
+
def whitespace_between(node)
|
85
|
+
if node.pairs.length >= 2
|
86
|
+
node.source[node.pairs[0].location.expression.end_pos + 1...node.pairs[1].location.expression.begin_pos]
|
87
|
+
else
|
88
|
+
' '
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# @param [RuboCop::AST::HashNode] node
|
93
|
+
# @return [String]
|
94
|
+
# { a: 1, b: 1 }
|
95
|
+
# ^^
|
96
|
+
def whitespace_trailing(node)
|
92
97
|
node.source[node.pairs[-1].location.expression.end_pos...node.location.end.begin_pos]
|
93
98
|
end
|
94
99
|
|
95
100
|
# @param [RuboCop::AST::HashNode] node
|
96
101
|
# @return [String]
|
97
|
-
# { a: 1,
|
102
|
+
# { a: 1, b: 1 }
|
98
103
|
# ^^^^
|
99
104
|
def whitespace_leading(node)
|
100
105
|
node.source[node.location.begin.end_pos...node.pairs[0].location.expression.begin_pos]
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Sevencop
|
6
|
+
# Identifies passing any argument to `#to_s`.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# to_s(:delimited)
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# to_formatted_s(:delimited)
|
15
|
+
#
|
16
|
+
class ToSWithArgument < Base
|
17
|
+
extend AutoCorrector
|
18
|
+
|
19
|
+
MSG = 'Use `to_formatted_s(...)` instead of `to_s(...)`.'
|
20
|
+
|
21
|
+
def_node_matcher :to_s_with_any_argument?, <<~PATTERN
|
22
|
+
(call _ :to_s _+)
|
23
|
+
PATTERN
|
24
|
+
|
25
|
+
def on_send(node)
|
26
|
+
return unless to_s_with_any_argument?(node)
|
27
|
+
|
28
|
+
add_offense(node) do |rewriter|
|
29
|
+
rewriter.replace(
|
30
|
+
node.loc.selector,
|
31
|
+
'to_formatted_s'
|
32
|
+
)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
alias on_csend on_send
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -4,7 +4,7 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module Sevencop
|
6
6
|
# Identifies use of UniquenessValidator without :case_sensitive option.
|
7
|
-
#
|
7
|
+
# Useful to keep the same behavior between Rails 6.0 and 6.1 where case insensitive collation is used in MySQL.
|
8
8
|
#
|
9
9
|
# @safety
|
10
10
|
# This cop is unsafe because it can register a false positive.
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Sevencop
|
6
|
+
# Identifies passing multi-elements Hash literal to `where.not`.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# where.not(key1: value1, key2: value2)
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# where.not(key1: value1).where.not(key2: value2)
|
15
|
+
#
|
16
|
+
class WhereNot < Base
|
17
|
+
extend AutoCorrector
|
18
|
+
|
19
|
+
MSG = 'Use `where.not(key1: value1).where.not(key2: value2)` style.'
|
20
|
+
|
21
|
+
def_node_matcher :where_not_with_multiple_elements_hash?, <<~PATTERN
|
22
|
+
(send
|
23
|
+
(send _ :where) :not
|
24
|
+
({ hash | kwargs }
|
25
|
+
(pair _ _)
|
26
|
+
(pair _ _)+))
|
27
|
+
PATTERN
|
28
|
+
|
29
|
+
def on_send(node)
|
30
|
+
return unless where_not_with_multiple_elements_hash?(node)
|
31
|
+
|
32
|
+
add_offense(node) do |corrector|
|
33
|
+
pairs = node.children[2].children
|
34
|
+
last_end_pos = pairs[0].location.expression.end_pos
|
35
|
+
pairs[1..].each do |pair|
|
36
|
+
corrector.remove(pair.location.expression.with(begin_pos: last_end_pos))
|
37
|
+
last_end_pos = pair.location.expression.end_pos
|
38
|
+
corrector.insert_after(node.location.expression, ".where.not(#{pair.source})")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/sevencop/version.rb
CHANGED
data/lib/sevencop.rb
CHANGED
@@ -10,7 +10,9 @@ require_relative 'rubocop/cop/sevencop/belongs_to_optional'
|
|
10
10
|
require_relative 'rubocop/cop/sevencop/hash_literal_order'
|
11
11
|
require_relative 'rubocop/cop/sevencop/order_field'
|
12
12
|
require_relative 'rubocop/cop/sevencop/redundant_existence_check'
|
13
|
+
require_relative 'rubocop/cop/sevencop/to_s_with_argument'
|
13
14
|
require_relative 'rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity'
|
15
|
+
require_relative 'rubocop/cop/sevencop/where_not'
|
14
16
|
|
15
17
|
module Sevencop
|
16
18
|
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.8.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-07-
|
11
|
+
date: 2022-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -45,7 +45,9 @@ files:
|
|
45
45
|
- lib/rubocop/cop/sevencop/hash_literal_order.rb
|
46
46
|
- lib/rubocop/cop/sevencop/order_field.rb
|
47
47
|
- lib/rubocop/cop/sevencop/redundant_existence_check.rb
|
48
|
+
- lib/rubocop/cop/sevencop/to_s_with_argument.rb
|
48
49
|
- lib/rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity.rb
|
50
|
+
- lib/rubocop/cop/sevencop/where_not.rb
|
49
51
|
- lib/sevencop.rb
|
50
52
|
- lib/sevencop/inject.rb
|
51
53
|
- lib/sevencop/version.rb
|