sevencop 0.8.1 → 0.9.2

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: 7ed10e2f4687671a6868a81d9c550490d309bc0f604c7b2f596db6efa9ef7ef9
4
- data.tar.gz: a097023dd73739464bc3c55f0d30cc89166eb8caa22201b3419a1ccaa778295c
3
+ metadata.gz: 2be2d44c67e1057b48a80a65e3bcdaaf1663d00b4cfd40a780e5524279be89e8
4
+ data.tar.gz: 577fe7a5e296a27b002814584a2716b6a445b6013d8759233a8ca1bb31183a32
5
5
  SHA512:
6
- metadata.gz: 3037aad57ef04821ea70c518a5d1cbc4bd69bbfb8460ac22887bee480ae9d599c871ed77226944ca29f1cdd24060558b3358513919c486af4bdce7e5d8980e08
7
- data.tar.gz: 956951c7a21d5c1315bee2e6a77ce21307c8889cbdeebca5931a40c5f8a21bdb8c89703054e2304ca2760f5878b0d211ed92613a27b2825ab514a9a9657192b4
6
+ metadata.gz: c0acba59ece08f76e4c937542c5579e2352582d32dffd6e07f584466f8d386cfe863af58c6b480c0e9db675584b4a6a977c9babb18b7c83676b5f8112c63a885
7
+ data.tar.gz: 5bb840ce34348e557cc9512b9f4ffcb6f8da0cf680e35cb77f11a2858aa68fc2a51b4229e8feadafda21507f699d5977d5ec9906e109652d29d9333ba0522adc
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.1)
4
+ sevencop (0.9.2)
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`.
data/config/default.yml CHANGED
@@ -11,6 +11,12 @@ Sevencop/HashLiteralOrder:
11
11
  Enabled: false
12
12
  VersionAdded: '0.6'
13
13
 
14
+ Sevencop/InferredSpecType:
15
+ Description: |
16
+ Identifies redundant spec type.
17
+ Enabled: false
18
+ VersionAdded: '0.9'
19
+
14
20
  Sevencop/OrderField:
15
21
  Description: |
16
22
  Wrap safe SQL String by `Arel.sql`.
@@ -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,6 +32,7 @@ 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
  _ _
@@ -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 _)
@@ -20,6 +20,7 @@ module RuboCop
20
20
 
21
21
  RESTRICT_ON_SEND = %i[to_s].freeze
22
22
 
23
+ # @!method to_s_with_any_argument?(node)
23
24
  def_node_matcher :to_s_with_any_argument?, <<~PATTERN
24
25
  (call _ :to_s _+)
25
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.1'
4
+ VERSION = '0.9.2'
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.1
4
+ version: 0.9.2
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-08-11 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,91 +0,0 @@
1
- # Changelog
2
-
3
- ## Unreleased
4
-
5
- ### Changed
6
-
7
- - Improve `Sevencop/ToSWithArgument` performance.
8
-
9
- ## 0.8.0 - 2022-07-20
10
-
11
- ### Added
12
-
13
- - Add `Sevencop/ToSWithArgument` cop.
14
-
15
- ## 0.7.0 - 2022-07-15
16
-
17
- ### Added
18
-
19
- - Add `Sevencop/WhereNot` cop.
20
-
21
- ## 0.6.1 - 2022-07-04
22
-
23
- ### Fixed
24
-
25
- - Fix `Sevencop/HashLiteralOrder` on surrounding-space-less Hash.
26
-
27
- ## 0.6.0 - 2022-07-04
28
-
29
- ### Added
30
-
31
- - Add `Sevencop/HashLiteralOrder` cop.
32
-
33
- ### Removed
34
-
35
- - Remove unnecessary method names check on `Sevencop/OrderField`.
36
-
37
- ## 0.5.1 - 2022-06-28
38
-
39
- ### Fixed
40
-
41
- - Fix `Sevencop/BelongsToOptional` scope bug.
42
-
43
- ## 0.5.0 - 2022-06-27
44
-
45
- ### Added
46
-
47
- - Add `Sevencop/BelongsToOptional` cop.
48
-
49
- ### Changed
50
-
51
- - Make `Sevencop/RedundantExistenceCheck` cop disabled by default.
52
-
53
- ### Removed
54
-
55
- - Remove rubocop version dependency.
56
-
57
- ## 0.4.1 - 2022-06-23
58
-
59
- ### Fixed
60
-
61
- - Fix `Sevencop/OrderField` on dstr.
62
-
63
- ## 0.4.0 - 2022-06-18
64
-
65
- ### Added
66
-
67
- - Add `Sevencop/OrderField` cop.
68
-
69
- ### Changed
70
-
71
- - Improve performance of `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
72
- - Improve offense location of `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
73
-
74
- ## 0.3.0 - 2022-06-18
75
-
76
- ### Added
77
-
78
- - Add `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
79
-
80
- ## 0.2.0 - 2022-06-07
81
-
82
- ### Changed
83
-
84
- - Correct to enforced operation at `Sevencop/RedundantExistenceCheck`.
85
-
86
- ## 0.1.0 - 2022-06-07
87
-
88
- ### Added
89
-
90
- - Initial release.
91
- - Add `Sevencop/RedundantExistenceCheck` cop.