sevencop 0.7.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 435ef77ac8fd3b1e598c538068a02cf91bce3e1d3143bd1e48b45ed80b9a8cd1
4
- data.tar.gz: 8bc5d65cde82f92a7f6457041cd49a1448af80d1915841a44a4fc2c22b7b0877
3
+ metadata.gz: 823af30bd6e2d7b7c9b9a433125915e5fcd002dfb9b332206dea852d8fc8c711
4
+ data.tar.gz: 59c81c197e486e596a43402497555718cc660f08d63e7827ca98f82bb351798c
5
5
  SHA512:
6
- metadata.gz: f53c512de8a6c32b2c4a409dc274dc22149c3ef15b680c8223e58a74bcac0015ccb3dd2d2e397fbb410bdf6f949e9f3d1de645c8332204a88dce7ab6ca5194da
7
- data.tar.gz: 2fd6cc8caf282c894b41744ae7df09d54017ce02d82ac3ab4925c85cde00a1aa0b8fd0ebc0fac5ec70f25dd8ab5fbfa79160102456a2a58ffafd3da1ffb92689
6
+ metadata.gz: 0de80f55882209fc927e0db5840e74a970af9d49504cf11013cd3e6da8f9a0195eaa7875fb794f2f8823402264b506ca7d77b0af7ac99cee9b15412d93d67805
7
+ data.tar.gz: 27c7ff5b059a95ca7b6dde3e3bc6814767e7d10db67c17504e1953fe4db6ae6ac1f3cc3b1d2ec91415630688d6614b7d53b138e622eb688c0e4745bebbb22236
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.7.0)
4
+ sevencop (0.9.0)
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
@@ -25,6 +25,13 @@ Sevencop/RedundantExistenceCheck:
25
25
  Safe: false
26
26
  VersionAdded: '0.1'
27
27
 
28
+ Sevencop/ToSWithArgument:
29
+ Description: |
30
+ Identifies passing any argument to `#to_s`.
31
+ Enabled: false
32
+ Safe: false
33
+ VersionAdded: '0.8'
34
+
28
35
  Sevencop/UniquenessValidatorExplicitCaseSensitivity:
29
36
  Description: |
30
37
  Specify :case_sensitivity option on use of UniquenessValidator.
@@ -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.start_with?(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 _)
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sevencop
6
+ # Identifies passing any argument to `#to_s`.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ # to_s(:delimited)
12
+ #
13
+ # # good
14
+ # to_formatted_s(:delimited)
15
+ #
16
+ class ToSWithArgument < Base
17
+ extend AutoCorrector
18
+
19
+ MSG = 'Use `to_formatted_s(...)` instead of `to_s(...)`.'
20
+
21
+ RESTRICT_ON_SEND = %i[to_s].freeze
22
+
23
+ # @!method to_s_with_any_argument?(node)
24
+ def_node_matcher :to_s_with_any_argument?, <<~PATTERN
25
+ (call _ :to_s _+)
26
+ PATTERN
27
+
28
+ def on_send(node)
29
+ return unless to_s_with_any_argument?(node)
30
+
31
+ add_offense(node) do |rewriter|
32
+ rewriter.replace(
33
+ node.loc.selector,
34
+ 'to_formatted_s'
35
+ )
36
+ end
37
+ end
38
+ alias on_csend on_send
39
+ end
40
+ end
41
+ end
42
+ end
@@ -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.7.0'
4
+ VERSION = '0.9.0'
5
5
  end
data/lib/sevencop.rb CHANGED
@@ -8,8 +8,10 @@ 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'
14
+ require_relative 'rubocop/cop/sevencop/to_s_with_argument'
13
15
  require_relative 'rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity'
14
16
  require_relative 'rubocop/cop/sevencop/where_not'
15
17
 
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.7.0
4
+ version: 0.9.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-07-15 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,8 +43,10 @@ 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
49
+ - lib/rubocop/cop/sevencop/to_s_with_argument.rb
48
50
  - lib/rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity.rb
49
51
  - lib/rubocop/cop/sevencop/where_not.rb
50
52
  - lib/sevencop.rb
data/CHANGELOG.md DELETED
@@ -1,81 +0,0 @@
1
- # Changelog
2
-
3
- ## Unreleased
4
-
5
- ## 0.7.0 - 2022-07-15
6
-
7
- ### Added
8
-
9
- - Add `Sevencop/WhereNot` cop.
10
-
11
- ## 0.6.1 - 2022-07-04
12
-
13
- ### Fixed
14
-
15
- - Fix `Sevencop/HashLiteralOrder` on surrounding-space-less Hash.
16
-
17
- ## 0.6.0 - 2022-07-04
18
-
19
- ### Added
20
-
21
- - Add `Sevencop/HashLiteralOrder` cop.
22
-
23
- ### Removed
24
-
25
- - Remove unnecessary method names check on `Sevencop/OrderField`.
26
-
27
- ## 0.5.1 - 2022-06-28
28
-
29
- ### Fixed
30
-
31
- - Fix `Sevencop/BelongsToOptional` scope bug.
32
-
33
- ## 0.5.0 - 2022-06-27
34
-
35
- ### Added
36
-
37
- - Add `Sevencop/BelongsToOptional` cop.
38
-
39
- ### Changed
40
-
41
- - Make `Sevencop/RedundantExistenceCheck` cop disabled by default.
42
-
43
- ### Removed
44
-
45
- - Remove rubocop version dependency.
46
-
47
- ## 0.4.1 - 2022-06-23
48
-
49
- ### Fixed
50
-
51
- - Fix `Sevencop/OrderField` on dstr.
52
-
53
- ## 0.4.0 - 2022-06-18
54
-
55
- ### Added
56
-
57
- - Add `Sevencop/OrderField` cop.
58
-
59
- ### Changed
60
-
61
- - Improve performance of `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
62
- - Improve offense location of `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
63
-
64
- ## 0.3.0 - 2022-06-18
65
-
66
- ### Added
67
-
68
- - Add `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
69
-
70
- ## 0.2.0 - 2022-06-07
71
-
72
- ### Changed
73
-
74
- - Correct to enforced operation at `Sevencop/RedundantExistenceCheck`.
75
-
76
- ## 0.1.0 - 2022-06-07
77
-
78
- ### Added
79
-
80
- - Initial release.
81
- - Add `Sevencop/RedundantExistenceCheck` cop.