sevencop 0.8.0 → 0.9.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: 021c0c8e909037a665540671725553ec8a0a071aea513ea2ad39e62a104141cc
4
- data.tar.gz: af3cbb3aaae938b0fa209995ca76dd65622aed3a4039d48a91dd4eaada3108eb
3
+ metadata.gz: 405e633edd8f28955ac15fa662affb4ff70e0296d45dbdc43c75426fa51c7280
4
+ data.tar.gz: c943dd42168df6e02e9630ed7eb80050da6d81ed1ea8ac333d19e933b9966a8e
5
5
  SHA512:
6
- metadata.gz: df542292fe608cc072d4770263cbcaa3df4ad040d3b59c93362b3188ac54ab56285a36199451a95476927b85cbb92d67c164d3cf5b184834c87a78d3745ef343
7
- data.tar.gz: 7289c90669cbe86f3eb025b71d2083fd60b1993ba8e5035bea7f06aecb2f1ccd8c274e53b29daea81358ac5d641a5be325189cda929035fae8b0dd127a4b360a
6
+ metadata.gz: 8ff7ec748e6078d83c4e5215ade19f42eadffeb46a1fdcdb653712d8a8f8834bfabebbb9d21253a864384fec30c06e19bd7e120fd58c2d334ca362fbf0b985ba
7
+ data.tar.gz: a60a3173ab3b6010563f80860ad6b591e39ba8260b2fe05ba6f0b5230d2e69d9d2441e29d4d2fb2fd5f53e5d32fe27107357a75b9d2e9db3955e63104e1dc6ca
data/.rubocop.yml CHANGED
@@ -1,3 +1,6 @@
1
+ require:
2
+ - rubocop/cop/internal_affairs
3
+
1
4
  AllCops:
2
5
  NewCops: enable
3
6
  SuggestExtensions: false
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --tag safety:"Cop Safety Information"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sevencop (0.8.0)
4
+ sevencop (0.9.1)
5
5
  rubocop
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -87,6 +87,24 @@ Sort Hash literal entries by key.
87
87
  }
88
88
  ```
89
89
 
90
+ ### Sevencop/InferredSpecType
91
+
92
+ Identifies redundant spec type.
93
+
94
+ ```ruby
95
+ # bad
96
+ # spec/models/user_spec.rb
97
+ RSpec.describe User, type: :model
98
+
99
+ # good
100
+ # spec/models/user_spec.rb
101
+ RSpec.describe User
102
+
103
+ # good
104
+ # spec/models/user_spec.rb
105
+ RSpec.describe User, type: :request
106
+ ```
107
+
90
108
  ### Sevencop/OrderField
91
109
 
92
110
  Identifies a String including "field" is passed to `order` or `reorder`.
@@ -28,14 +28,17 @@ module RuboCop
28
28
  belongs_to
29
29
  ].freeze
30
30
 
31
+ # @!method without_options?(node)
31
32
  def_node_matcher :without_options?, <<~PATTERN
32
33
  (send _ _ _ (block ...)?)
33
34
  PATTERN
34
35
 
36
+ # @!method with_hash_options?(node)
35
37
  def_node_matcher :with_hash_options?, <<~PATTERN
36
38
  (send _ _ _ (block ...)? (hash ...))
37
39
  PATTERN
38
40
 
41
+ # @!method with_optional?(node)
39
42
  def_node_matcher :with_optional?, <<~PATTERN
40
43
  (send _ _ _ (block ...)? (hash <(pair (sym :optional) _) ...>))
41
44
  PATTERN
@@ -26,6 +26,7 @@ module RuboCop
26
26
 
27
27
  MSG = 'Sort Hash literal entries by key.'
28
28
 
29
+ # @!method hash_literal?(node)
29
30
  def_node_matcher :hash_literal?, <<~PATTERN
30
31
  (hash
31
32
  (pair
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sevencop
6
+ # Identifies redundant spec type.
7
+ # This is automatically set by `config.infer_spec_type_from_file_location!`.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # # spec/models/user_spec.rb
13
+ # RSpec.describe User, type: :model
14
+ #
15
+ # # good
16
+ # # spec/models/user_spec.rb
17
+ # RSpec.describe User
18
+ #
19
+ # # good
20
+ # # spec/models/user_spec.rb
21
+ # RSpec.describe User, type: :request
22
+ #
23
+ class InferredSpecType < Base
24
+ extend AutoCorrector
25
+
26
+ # @return [Array<Hash>]
27
+ INFERENCES = [
28
+ { prefix: 'spec/channels/', type: :channel },
29
+ { prefix: 'spec/controllers/', type: :controller },
30
+ { prefix: 'spec/features/', type: :feature },
31
+ { prefix: 'spec/generator/', type: :generator },
32
+ { prefix: 'spec/helpers/', type: :helper },
33
+ { prefix: 'spec/jobs/', type: :job },
34
+ { prefix: 'spec/mailboxes/', type: :mailbox },
35
+ { prefix: 'spec/mailers/', type: :mailer },
36
+ { prefix: 'spec/models/', type: :model },
37
+ { prefix: 'spec/requests/', type: :request },
38
+ { prefix: 'spec/integration/', type: :request },
39
+ { prefix: 'spec/api/', type: :request },
40
+ { prefix: 'spec/routing/', type: :routing },
41
+ { prefix: 'spec/system/', type: :system },
42
+ { prefix: 'spec/views/', type: :view }
43
+ ].freeze
44
+
45
+ MSG = 'Remove redundant spec type.'
46
+
47
+ RESTRICT_ON_SEND = %i[
48
+ describe
49
+ ].freeze
50
+
51
+ # @param [RuboCop::AST::SendNode] node
52
+ def on_send(node)
53
+ pair_node = describe_with_type(node)
54
+ return unless pair_node
55
+ return unless inferred_type?(pair_node)
56
+
57
+ removable_node = detect_removable_node(pair_node)
58
+ add_offense(removable_node) do |corrector|
59
+ autocorrect(corrector, removable_node)
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ # @!method describe_with_type(node)
66
+ # @param [RuboCop::AST::SendNode] node
67
+ # @return [RuboCop::AST::PairNode, nil]
68
+ def_node_matcher :describe_with_type, <<~PATTERN
69
+ (send
70
+ { (const nil? :RSpec) | nil? }
71
+ _
72
+ _
73
+ _*
74
+ ({ hash | kwargs }
75
+ (pair ...)*
76
+ $(pair (sym :type) (sym _))
77
+ (pair ...)*
78
+ )
79
+ )
80
+ PATTERN
81
+
82
+ # @param [RuboCop::AST::Corrector] corrector
83
+ # @param [RuboCop::AST::Node] node
84
+ def autocorrect(corrector, node)
85
+ corrector.remove(
86
+ node.location.expression.with(
87
+ begin_pos: node.left_sibling.location.expression.end_pos
88
+ )
89
+ )
90
+ end
91
+
92
+ # @param [RuboCop::AST::PairNode] node
93
+ # @return [RuboCop::AST::Node]
94
+ def detect_removable_node(node)
95
+ if node.parent.pairs.size == 1
96
+ node.parent
97
+ else
98
+ node
99
+ end
100
+ end
101
+
102
+ # @return [String]
103
+ def file_path
104
+ processed_source.file_path
105
+ end
106
+
107
+ # @param [RuboCop::AST::PairNode] node
108
+ # @return [Boolean]
109
+ def inferred_type?(node)
110
+ inferred_type_from_file_path.inspect == node.value.source
111
+ end
112
+
113
+ # @return [Symbol, nil]
114
+ def inferred_type_from_file_path
115
+ INFERENCES.find do |inference|
116
+ break inference[:type] if file_path.include?(inference[:prefix])
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
@@ -32,12 +32,13 @@ module RuboCop
32
32
  reorder
33
33
  ].freeze
34
34
 
35
+ # @!method order_with_field?(node)
35
36
  def_node_matcher :order_with_field?, <<~PATTERN
36
37
  (send
37
38
  _ _
38
39
  {
39
- (str /field\(.+\)/) |
40
- (dstr <(str /field\(.+\)/) ...>)
40
+ (str /field\(.+\)/i) |
41
+ (dstr <(str /field\(.+\)/i) ...>)
41
42
  }
42
43
  ...
43
44
  )
@@ -84,6 +84,7 @@ module RuboCop
84
84
 
85
85
  MSG = 'Avoid redundant existent check before file operation.'
86
86
 
87
+ # @!method make_unless_exist?(node)
87
88
  def_node_matcher :make_unless_exist?, <<~PATTERN
88
89
  (if
89
90
  (send (const nil? CLASS_NAMES_FOR_EXIST) METHOD_NAMES_FOR_EXIST _)
@@ -92,6 +93,7 @@ module RuboCop
92
93
  )
93
94
  PATTERN
94
95
 
96
+ # @!method remove_if_exist?(node)
95
97
  def_node_matcher :remove_if_exist?, <<~PATTERN
96
98
  (if
97
99
  (send (const nil? CLASS_NAMES_FOR_EXIST) METHOD_NAMES_FOR_EXIST _)
@@ -18,6 +18,9 @@ module RuboCop
18
18
 
19
19
  MSG = 'Use `to_formatted_s(...)` instead of `to_s(...)`.'
20
20
 
21
+ RESTRICT_ON_SEND = %i[to_s].freeze
22
+
23
+ # @!method to_s_with_any_argument?(node)
21
24
  def_node_matcher :to_s_with_any_argument?, <<~PATTERN
22
25
  (call _ :to_s _+)
23
26
  PATTERN
@@ -33,6 +33,7 @@ module RuboCop
33
33
 
34
34
  RESTRICT_ON_SEND = %i[validates].freeze
35
35
 
36
+ # @!method validates_uniqueness?(node)
36
37
  def_node_matcher :validates_uniqueness?, <<~PATTERN
37
38
  (send nil? :validates
38
39
  _
@@ -48,6 +49,7 @@ module RuboCop
48
49
  )
49
50
  PATTERN
50
51
 
52
+ # @!method validates_uniqueness_with_case_sensitivity?(node)
51
53
  def_node_matcher :validates_uniqueness_with_case_sensitivity?, <<~PATTERN
52
54
  (send nil? :validates
53
55
  _
@@ -84,7 +86,6 @@ module RuboCop
84
86
  end
85
87
 
86
88
  # @param [RuboCop::AST::SendNode] node
87
- # @param [RuboCop::AST::Node]
88
89
  def find_uniqueness_value(node)
89
90
  node.arguments[1].pairs.find { |pair| pair.key.value == :uniqueness }.value
90
91
  end
@@ -18,6 +18,7 @@ module RuboCop
18
18
 
19
19
  MSG = 'Use `where.not(key1: value1).where.not(key2: value2)` style.'
20
20
 
21
+ # @!method where_not_with_multiple_elements_hash?(node)
21
22
  def_node_matcher :where_not_with_multiple_elements_hash?, <<~PATTERN
22
23
  (send
23
24
  (send _ :where) :not
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sevencop
4
- VERSION = '0.8.0'
4
+ VERSION = '0.9.1'
5
5
  end
data/lib/sevencop.rb CHANGED
@@ -8,6 +8,7 @@ require_relative 'sevencop/version'
8
8
 
9
9
  require_relative 'rubocop/cop/sevencop/belongs_to_optional'
10
10
  require_relative 'rubocop/cop/sevencop/hash_literal_order'
11
+ require_relative 'rubocop/cop/sevencop/inferred_spec_type'
11
12
  require_relative 'rubocop/cop/sevencop/order_field'
12
13
  require_relative 'rubocop/cop/sevencop/redundant_existence_check'
13
14
  require_relative 'rubocop/cop/sevencop/to_s_with_argument'
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.8.0
4
+ version: 0.9.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-07-20 00:00:00.000000000 Z
11
+ date: 2022-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -33,7 +33,7 @@ extra_rdoc_files: []
33
33
  files:
34
34
  - ".rspec"
35
35
  - ".rubocop.yml"
36
- - CHANGELOG.md
36
+ - ".yardopts"
37
37
  - CODE_OF_CONDUCT.md
38
38
  - Gemfile
39
39
  - Gemfile.lock
@@ -43,6 +43,7 @@ files:
43
43
  - config/default.yml
44
44
  - lib/rubocop/cop/sevencop/belongs_to_optional.rb
45
45
  - lib/rubocop/cop/sevencop/hash_literal_order.rb
46
+ - lib/rubocop/cop/sevencop/inferred_spec_type.rb
46
47
  - lib/rubocop/cop/sevencop/order_field.rb
47
48
  - lib/rubocop/cop/sevencop/redundant_existence_check.rb
48
49
  - lib/rubocop/cop/sevencop/to_s_with_argument.rb
data/CHANGELOG.md DELETED
@@ -1,87 +0,0 @@
1
- # Changelog
2
-
3
- ## Unreleased
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
-
23
- ## 0.6.0 - 2022-07-04
24
-
25
- ### Added
26
-
27
- - Add `Sevencop/HashLiteralOrder` cop.
28
-
29
- ### Removed
30
-
31
- - Remove unnecessary method names check on `Sevencop/OrderField`.
32
-
33
- ## 0.5.1 - 2022-06-28
34
-
35
- ### Fixed
36
-
37
- - Fix `Sevencop/BelongsToOptional` scope bug.
38
-
39
- ## 0.5.0 - 2022-06-27
40
-
41
- ### Added
42
-
43
- - Add `Sevencop/BelongsToOptional` cop.
44
-
45
- ### Changed
46
-
47
- - Make `Sevencop/RedundantExistenceCheck` cop disabled by default.
48
-
49
- ### Removed
50
-
51
- - Remove rubocop version dependency.
52
-
53
- ## 0.4.1 - 2022-06-23
54
-
55
- ### Fixed
56
-
57
- - Fix `Sevencop/OrderField` on dstr.
58
-
59
- ## 0.4.0 - 2022-06-18
60
-
61
- ### Added
62
-
63
- - Add `Sevencop/OrderField` cop.
64
-
65
- ### Changed
66
-
67
- - Improve performance of `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
68
- - Improve offense location of `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
69
-
70
- ## 0.3.0 - 2022-06-18
71
-
72
- ### Added
73
-
74
- - Add `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
75
-
76
- ## 0.2.0 - 2022-06-07
77
-
78
- ### Changed
79
-
80
- - Correct to enforced operation at `Sevencop/RedundantExistenceCheck`.
81
-
82
- ## 0.1.0 - 2022-06-07
83
-
84
- ### Added
85
-
86
- - Initial release.
87
- - Add `Sevencop/RedundantExistenceCheck` cop.