sevencop 0.5.0 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 319d9ad3cd53f54122dd36a4d53d4ba2b1e496831c3c26022b2a8494f5a95fdc
4
- data.tar.gz: b54dd12e0177a48827cb87f35fa6af902e256f996c96a990933c08c22e8b91f5
3
+ metadata.gz: f744cb31d07cadbb924e1ce690b85ee2c771ff9264333c153fcc52244d596dd6
4
+ data.tar.gz: c594faf493963d8f188559ca1f77b7b8570c05b5936356d6fc259de2417200a7
5
5
  SHA512:
6
- metadata.gz: 07d68c578fa6807867b0caa213706484ff8c04475e1f17ce0f3121a37307f0cb48962a1ee7c1782dcbaedbff34748a075a41522e8d9c79dcdf24e9d4b8aa581a
7
- data.tar.gz: e47791134b21b41a4d9fcae6503db43cefb2d377391b7e3c5a7537bd5944fecd444164931df15035d628b939ce6fe5e6fac50694aef76c856c66c3a97763b04a
6
+ metadata.gz: ee20da1638b96c6256a257b6602afcc90a22b33c4988fd8a44b50d5f93b7ab311e7ce052f3aeef4dd0b08c40028cb1c371184a3569e8656086a96b685b62d4d9
7
+ data.tar.gz: 266bdf4e223ce62e269da6e850b22491d8b1dc052a8f686b972c895a86d29d067b4105e1716c78c9b5f7c2cd231b567ac1b26e58cac656f45843f2dabf5b432f
data/CHANGELOG.md CHANGED
@@ -2,6 +2,28 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.6.1 - 2022-07-04
6
+
7
+ ### Fixed
8
+
9
+ - Fix `Sevencop/HashLiteralOrder` on surrounding-space-less Hash.
10
+
11
+ ## 0.6.0 - 2022-07-04
12
+
13
+ ### Added
14
+
15
+ - Add `Sevencop/HashLiteralOrder` cop.
16
+
17
+ ### Removed
18
+
19
+ - Remove unnecessary method names check on `Sevencop/OrderField`.
20
+
21
+ ## 0.5.1 - 2022-06-28
22
+
23
+ ### Fixed
24
+
25
+ - Fix `Sevencop/BelongsToOptional` scope bug.
26
+
5
27
  ## 0.5.0 - 2022-06-27
6
28
 
7
29
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sevencop (0.5.0)
4
+ sevencop (0.6.1)
5
5
  rubocop
6
6
 
7
7
  GEM
@@ -53,4 +53,4 @@ DEPENDENCIES
53
53
  sevencop!
54
54
 
55
55
  BUNDLED WITH
56
- 2.3.6
56
+ 2.3.16
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: Wrap safe SQL String by `Arel.sql`.
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: Avoid redundant existent check before file operation.
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: Specify :case_sensitivity option on use of UniquenessValidator.
29
+ Description: |
30
+ Specify :case_sensitivity option on use of UniquenessValidator.
22
31
  Enabled: false
23
32
  Safe: false
24
33
  VersionAdded: '0.3'
@@ -29,15 +29,15 @@ module RuboCop
29
29
  ].freeze
30
30
 
31
31
  def_node_matcher :without_options?, <<~PATTERN
32
- (send _ _ _)
32
+ (send _ _ _ (block ...)?)
33
33
  PATTERN
34
34
 
35
35
  def_node_matcher :with_hash_options?, <<~PATTERN
36
- (send _ _ _ (hash ...))
36
+ (send _ _ _ (block ...)? (hash ...))
37
37
  PATTERN
38
38
 
39
39
  def_node_matcher :with_optional?, <<~PATTERN
40
- (send _ _ _ (hash <(pair (sym :optional) _) ...>))
40
+ (send _ _ _ (block ...)? (hash <(pair (sym :optional) _) ...>))
41
41
  PATTERN
42
42
 
43
43
  # @param [RuboCop::AST::SendNode] node
@@ -45,11 +45,7 @@ module RuboCop
45
45
  return unless without_options?(node) || (with_hash_options?(node) && !with_optional?(node))
46
46
 
47
47
  add_offense(node) do |corrector|
48
- if node.arguments[-1].hash_type?
49
- corrector.insert_after(node.arguments[-1], ', optional: true')
50
- elsif node.arguments.length == 1
51
- corrector.insert_after(node.arguments.first, ', optional: true')
52
- end
48
+ corrector.insert_after(node.arguments[-1], ', optional: true')
53
49
  end
54
50
  end
55
51
  end
@@ -0,0 +1,116 @@
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
+ [
58
+ '{',
59
+ whitespace_leading(node),
60
+ sort(node.pairs).map(&:source).join(",#{whitespace_between(node)}"),
61
+ whitespace_trailing(node),
62
+ '}'
63
+ ].join
64
+ end
65
+
66
+ # @param [RuboCop::AST::HashNode] node
67
+ # @return [Boolean]
68
+ def multi_line?(node)
69
+ node.source.include?("\n")
70
+ end
71
+
72
+ # @param [Array<RuboCop::AST::PairNode>] pairs
73
+ # @return [Array<RuboCop::AST::PairNode>]
74
+ def sort(pairs)
75
+ pairs.sort_by do |pair|
76
+ pair.key.value
77
+ end
78
+ end
79
+
80
+ # @param [RuboCop::AST::HashNode] node
81
+ # @return [Boolean]
82
+ def sorted?(node)
83
+ node.pairs.map(&:source) == sort(node.pairs).map(&:source)
84
+ end
85
+
86
+ # @param [RuboCop::AST::HashNode] node
87
+ # @return [String]
88
+ # { a: 1, b: 1 }
89
+ # ^^^
90
+ def whitespace_between(node)
91
+ if node.pairs.length >= 2
92
+ node.source[node.pairs[0].location.expression.end_pos + 1...node.pairs[1].location.expression.begin_pos]
93
+ else
94
+ ' '
95
+ end
96
+ end
97
+
98
+ # @param [RuboCop::AST::HashNode] node
99
+ # @return [String]
100
+ # { a: 1, b: 1 }
101
+ # ^^
102
+ def whitespace_trailing(node)
103
+ node.source[node.pairs[-1].location.expression.end_pos...node.location.end.begin_pos]
104
+ end
105
+
106
+ # @param [RuboCop::AST::HashNode] node
107
+ # @return [String]
108
+ # { a: 1, b: 1 }
109
+ # ^^^^
110
+ def whitespace_leading(node)
111
+ node.source[node.location.begin.end_pos...node.pairs[0].location.expression.begin_pos]
112
+ end
113
+ end
114
+ end
115
+ end
116
+ 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
- _ ORDER_METHOD_NAMES
37
+ _ _
40
38
  {
41
39
  (str /field\(.+\)/) |
42
40
  (dstr <(str /field\(.+\)/) ...>)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sevencop
4
- VERSION = '0.5.0'
4
+ VERSION = '0.6.1'
5
5
  end
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.5.0
4
+ version: 0.6.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-27 00:00:00.000000000 Z
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
@@ -72,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
73
  - !ruby/object:Gem::Version
73
74
  version: '0'
74
75
  requirements: []
75
- rubygems_version: 3.2.32
76
+ rubygems_version: 3.3.7
76
77
  signing_key:
77
78
  specification_version: 4
78
79
  summary: Custom cops for RuboCop.