sevencop 0.5.1 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db281c40f5207dfc9759c52c60d4e2799d772e8d612364c4afa9b72eb935c90a
4
- data.tar.gz: f84b8ca1a7d8cdb598c170b9c04b98b2358750ced29a8d3b3fb011b8a629449c
3
+ metadata.gz: 58fe0254e47cc59376f53d38640a163d0ba80dfba6f3f41998350b1b049010fd
4
+ data.tar.gz: 969301c9b0ef1af7ab3170c63fd72f5f8d6b095de2c84f2818917a003afe59cb
5
5
  SHA512:
6
- metadata.gz: 4597d82f8b5dd02a575d4720c10f495e6ebf16bfe47ef88c9557e5cb474c83bc35505861e2a1e5fdbc3b1d062fac6a06d3cfe94a05593ee9638a0aecf1034125
7
- data.tar.gz: 3e9c4206cec1f67a0ab9176814f4405fe6d7fbe83c8571d0759788206b67569a8ad144f0cb934dbc2ddf30df29d529906f7160c5b029fbc13a60464229f2a8d6
6
+ metadata.gz: be7586aef6236eab74b74494893ed7af665f01c4879ea36f63d6695250170174175859c439e7a6dd39775095b1ae210fa80a8d4e2e4f8a3c177e6ccc060c9c81
7
+ data.tar.gz: 7003edbafb17736c0e0bdfaaf65c1e1340fb45b684b31b1d573d6977f19e8c192700fb8bb4b57d0003f32753d2438110b6de937da42832a06641118c6e2b0c08
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.6.0 - 2022-07-04
6
+
7
+ ### Added
8
+
9
+ - Add `Sevencop/HashLiteralOrder` cop.
10
+
11
+ ### Removed
12
+
13
+ - Remove unnecessary method names check on `Sevencop/OrderField`.
14
+
5
15
  ## 0.5.1 - 2022-06-28
6
16
 
7
17
  ### Fixed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sevencop (0.5.1)
4
+ sevencop (0.6.0)
5
5
  rubocop
6
6
 
7
7
  GEM
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'
@@ -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
- _ 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.1'
4
+ VERSION = '0.6.0'
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.1
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-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