sevencop 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile.lock +1 -1
- data/README.md +20 -0
- data/config/default.yml +12 -3
- data/lib/rubocop/cop/sevencop/hash_literal_order.rb +105 -0
- data/lib/rubocop/cop/sevencop/order_field.rb +1 -3
- 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: 58fe0254e47cc59376f53d38640a163d0ba80dfba6f3f41998350b1b049010fd
|
4
|
+
data.tar.gz: 969301c9b0ef1af7ab3170c63fd72f5f8d6b095de2c84f2818917a003afe59cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be7586aef6236eab74b74494893ed7af665f01c4879ea36f63d6695250170174175859c439e7a6dd39775095b1ae210fa80a8d4e2e4f8a3c177e6ccc060c9c81
|
7
|
+
data.tar.gz: 7003edbafb17736c0e0bdfaaf65c1e1340fb45b684b31b1d573d6977f19e8c192700fb8bb4b57d0003f32753d2438110b6de937da42832a06641118c6e2b0c08
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -57,6 +57,26 @@ belongs_to :group, options
|
|
57
57
|
|
58
58
|
This is useful for migration of `config.active_record.belongs_to_required_by_default`.
|
59
59
|
|
60
|
+
### Sevencop/HashLiteralOrder
|
61
|
+
|
62
|
+
Sort Hash literal by key.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
# bad
|
66
|
+
{
|
67
|
+
b: 1,
|
68
|
+
a: 1,
|
69
|
+
c: 1
|
70
|
+
}
|
71
|
+
|
72
|
+
# good
|
73
|
+
{
|
74
|
+
a: 1,
|
75
|
+
b: 1,
|
76
|
+
c: 1
|
77
|
+
}
|
78
|
+
```
|
79
|
+
|
60
80
|
### Sevencop/OrderField
|
61
81
|
|
62
82
|
Identifies a String including "field" is passed to `order` or `reorder`.
|
data/config/default.yml
CHANGED
@@ -5,20 +5,29 @@ Sevencop/BelongsToOptional:
|
|
5
5
|
Safe: false
|
6
6
|
VersionAdded: '0.5'
|
7
7
|
|
8
|
+
Sevencop/HashLiteralOrder:
|
9
|
+
Description: |
|
10
|
+
Sort Hash literal entries by key.
|
11
|
+
Enabled: false
|
12
|
+
VersionAdded: '0.6'
|
13
|
+
|
8
14
|
Sevencop/OrderField:
|
9
|
-
Description:
|
15
|
+
Description: |
|
16
|
+
Wrap safe SQL String by `Arel.sql`.
|
10
17
|
Enabled: false
|
11
18
|
Safe: false
|
12
19
|
VersionAdded: '0.4'
|
13
20
|
|
14
21
|
Sevencop/RedundantExistenceCheck:
|
15
|
-
Description:
|
22
|
+
Description: |
|
23
|
+
Avoid redundant existent check before file operation.
|
16
24
|
Enabled: false
|
17
25
|
Safe: false
|
18
26
|
VersionAdded: '0.1'
|
19
27
|
|
20
28
|
Sevencop/UniquenessValidatorExplicitCaseSensitivity:
|
21
|
-
Description:
|
29
|
+
Description: |
|
30
|
+
Specify :case_sensitivity option on use of UniquenessValidator.
|
22
31
|
Enabled: false
|
23
32
|
Safe: false
|
24
33
|
VersionAdded: '0.3'
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Sevencop
|
6
|
+
# Sort Hash literal entries by key.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
# {
|
12
|
+
# b: 1,
|
13
|
+
# a: 1,
|
14
|
+
# c: 1
|
15
|
+
# }
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
# {
|
19
|
+
# a: 1,
|
20
|
+
# b: 1,
|
21
|
+
# c: 1
|
22
|
+
# }
|
23
|
+
#
|
24
|
+
class HashLiteralOrder < Base
|
25
|
+
extend AutoCorrector
|
26
|
+
|
27
|
+
MSG = 'Sort Hash literal entries by key.'
|
28
|
+
|
29
|
+
def_node_matcher :hash_literal?, <<~PATTERN
|
30
|
+
(hash
|
31
|
+
(pair
|
32
|
+
{sym | str}
|
33
|
+
_
|
34
|
+
)+
|
35
|
+
)
|
36
|
+
PATTERN
|
37
|
+
|
38
|
+
# @param [RuboCop::AST::HashNode] node
|
39
|
+
def on_hash(node)
|
40
|
+
return unless hash_literal?(node)
|
41
|
+
|
42
|
+
return if sorted?(node)
|
43
|
+
|
44
|
+
add_offense(node) do |corrector|
|
45
|
+
corrector.replace(
|
46
|
+
node,
|
47
|
+
autocorrect(node)
|
48
|
+
)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
# @param [RuboCop::AST::HashNode] node
|
55
|
+
# @return [String]
|
56
|
+
def autocorrect(node)
|
57
|
+
whitespace = whitespace_leading(node)
|
58
|
+
[
|
59
|
+
'{',
|
60
|
+
whitespace,
|
61
|
+
sort(node.pairs).map(&:source).join(",#{whitespace}"),
|
62
|
+
whitespace_ending(node),
|
63
|
+
'}'
|
64
|
+
].join
|
65
|
+
end
|
66
|
+
|
67
|
+
# @param [RuboCop::AST::HashNode] node
|
68
|
+
# @return [Boolean]
|
69
|
+
def multi_line?(node)
|
70
|
+
node.source.include?("\n")
|
71
|
+
end
|
72
|
+
|
73
|
+
# @param [Array<RuboCop::AST::PairNode>] pairs
|
74
|
+
# @return [Array<RuboCop::AST::PairNode>]
|
75
|
+
def sort(pairs)
|
76
|
+
pairs.sort_by do |pair|
|
77
|
+
pair.key.value
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# @param [RuboCop::AST::HashNode] node
|
82
|
+
# @return [Boolean]
|
83
|
+
def sorted?(node)
|
84
|
+
node.pairs.map(&:source) == sort(node.pairs).map(&:source)
|
85
|
+
end
|
86
|
+
|
87
|
+
# @param [RuboCop::AST::HashNode] node
|
88
|
+
# @return [String]
|
89
|
+
# { a: 1, b: 1 }
|
90
|
+
# ^^
|
91
|
+
def whitespace_ending(node)
|
92
|
+
node.source[node.pairs[-1].location.expression.end_pos...node.location.end.begin_pos]
|
93
|
+
end
|
94
|
+
|
95
|
+
# @param [RuboCop::AST::HashNode] node
|
96
|
+
# @return [String]
|
97
|
+
# { a: 1, b: 1 }
|
98
|
+
# ^^^^
|
99
|
+
def whitespace_leading(node)
|
100
|
+
node.source[node.location.begin.end_pos...node.pairs[0].location.expression.begin_pos]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -32,11 +32,9 @@ module RuboCop
|
|
32
32
|
reorder
|
33
33
|
].freeze
|
34
34
|
|
35
|
-
ORDER_METHOD_NAMES = RESTRICT_ON_SEND.to_set.freeze
|
36
|
-
|
37
35
|
def_node_matcher :order_with_field?, <<~PATTERN
|
38
36
|
(send
|
39
|
-
_
|
37
|
+
_ _
|
40
38
|
{
|
41
39
|
(str /field\(.+\)/) |
|
42
40
|
(dstr <(str /field\(.+\)/) ...>)
|
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/belongs_to_optional'
|
10
|
+
require_relative 'rubocop/cop/sevencop/hash_literal_order'
|
10
11
|
require_relative 'rubocop/cop/sevencop/order_field'
|
11
12
|
require_relative 'rubocop/cop/sevencop/redundant_existence_check'
|
12
13
|
require_relative 'rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity'
|
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.6.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-
|
11
|
+
date: 2022-07-03 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/belongs_to_optional.rb
|
45
|
+
- lib/rubocop/cop/sevencop/hash_literal_order.rb
|
45
46
|
- lib/rubocop/cop/sevencop/order_field.rb
|
46
47
|
- lib/rubocop/cop/sevencop/redundant_existence_check.rb
|
47
48
|
- lib/rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity.rb
|