sevencop 0.4.0 → 0.5.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: b2c9e682c02d43b65dbeefd01c07c467881b43bb8a1344289f85584f21e998c9
4
- data.tar.gz: fd1dd656e25801d5748b28edcb85d0286c042d29d3b2d065761fa030876c492c
3
+ metadata.gz: db281c40f5207dfc9759c52c60d4e2799d772e8d612364c4afa9b72eb935c90a
4
+ data.tar.gz: f84b8ca1a7d8cdb598c170b9c04b98b2358750ced29a8d3b3fb011b8a629449c
5
5
  SHA512:
6
- metadata.gz: 60b0f9acd29d904cc77cc7fbc20023fdc050298ef51022802e815287e9fd8ea5a3823f849a718a2b76fe1a563c3932c5e5e553be9e18cdac16cb22d813ff9e30
7
- data.tar.gz: ada45750a1964ee08629bdac329029b169f77aa68999c8c7bf3cd0d983e8eacadb9925d0140fb418a1e9cd0f071b9c1d75f0f8b4dd36ff07e0984c0e21c0a673
6
+ metadata.gz: 4597d82f8b5dd02a575d4720c10f495e6ebf16bfe47ef88c9557e5cb474c83bc35505861e2a1e5fdbc3b1d062fac6a06d3cfe94a05593ee9638a0aecf1034125
7
+ data.tar.gz: 3e9c4206cec1f67a0ab9176814f4405fe6d7fbe83c8571d0759788206b67569a8ad144f0cb934dbc2ddf30df29d529906f7160c5b029fbc13a60464229f2a8d6
data/CHANGELOG.md CHANGED
@@ -2,6 +2,32 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.5.1 - 2022-06-28
6
+
7
+ ### Fixed
8
+
9
+ - Fix `Sevencop/BelongsToOptional` scope bug.
10
+
11
+ ## 0.5.0 - 2022-06-27
12
+
13
+ ### Added
14
+
15
+ - Add `Sevencop/BelongsToOptional` cop.
16
+
17
+ ### Changed
18
+
19
+ - Make `Sevencop/RedundantExistenceCheck` cop disabled by default.
20
+
21
+ ### Removed
22
+
23
+ - Remove rubocop version dependency.
24
+
25
+ ## 0.4.1 - 2022-06-23
26
+
27
+ ### Fixed
28
+
29
+ - Fix `Sevencop/OrderField` on dstr.
30
+
5
31
  ## 0.4.0 - 2022-06-18
6
32
 
7
33
  ### Added
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sevencop (0.4.0)
5
- rubocop (>= 1.27)
4
+ sevencop (0.5.1)
5
+ rubocop
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -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
@@ -35,6 +35,28 @@ require:
35
35
 
36
36
  ## Cops
37
37
 
38
+ All cops are `Enabled: false` by default.
39
+
40
+ ### Sevencop/BelongsToOptional
41
+
42
+ Force `belongs_to` with `optional: true` option.
43
+
44
+ ```ruby
45
+ # bad
46
+ belongs_to :group
47
+
48
+ # good
49
+ belongs_to :group, optional: true
50
+
51
+ # good
52
+ belongs_to :group, optional: false
53
+
54
+ # good
55
+ belongs_to :group, options
56
+ ```
57
+
58
+ This is useful for migration of `config.active_record.belongs_to_required_by_default`.
59
+
38
60
  ### Sevencop/OrderField
39
61
 
40
62
  Identifies a String including "field" is passed to `order` or `reorder`.
@@ -53,9 +75,7 @@ reorder('field(id, ?)', a)
53
75
  reorder(Arel.sql('field(id, ?)'), a)
54
76
  ```
55
77
 
56
- `Enabled: false` by default.
57
-
58
- ### `Sevencop/RedundantExistenceCheck`
78
+ ### Sevencop/RedundantExistenceCheck
59
79
 
60
80
  Identifies redundant existent check before file operation.
61
81
 
@@ -73,7 +93,7 @@ FileUtils.rm(a) if FileTest.exist?(a)
73
93
  FileUtils.rm_f(a)
74
94
  ```
75
95
 
76
- ### `Sevencop/UniquenessValidatorExplicitCaseSensitivity`
96
+ ### Sevencop/UniquenessValidatorExplicitCaseSensitivity
77
97
 
78
98
  Identifies use of UniquenessValidator without :case_sensitive option.
79
99
 
@@ -95,5 +115,3 @@ validates :name, uniqueness: { allow_nil: true, scope: :user_id, case_sensitive:
95
115
  ```
96
116
 
97
117
  Useful to keep the same behavior between Rails 6.0 and 6.1 where case insensitive collation is used in MySQL.
98
-
99
- `Enabled: false` by default.
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ task :new_cop, [:cop] do |_task, args|
16
16
  require 'rubocop'
17
17
 
18
18
  cop_name = args.fetch(:cop) do
19
- warn 'usage: bundle exec rake new_cop[Department/Name]'
19
+ warn "usage: bundle exec rake 'new_cop[Department/Name]'"
20
20
  exit!
21
21
  end
22
22
 
data/config/default.yml CHANGED
@@ -1,3 +1,10 @@
1
+ Sevencop/BelongsToOptional:
2
+ Description: |
3
+ Force `belongs_to` with `optional: true` option.
4
+ Enabled: false
5
+ Safe: false
6
+ VersionAdded: '0.5'
7
+
1
8
  Sevencop/OrderField:
2
9
  Description: Wrap safe SQL String by `Arel.sql`.
3
10
  Enabled: false
@@ -6,7 +13,7 @@ Sevencop/OrderField:
6
13
 
7
14
  Sevencop/RedundantExistenceCheck:
8
15
  Description: Avoid redundant existent check before file operation.
9
- Enabled: true
16
+ Enabled: false
10
17
  Safe: false
11
18
  VersionAdded: '0.1'
12
19
 
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sevencop
6
+ # Force `belongs_to` with `optional: true` option.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ # belongs_to :group
12
+ #
13
+ # # good
14
+ # belongs_to :group, optional: true
15
+ #
16
+ # # good
17
+ # belongs_to :group, optional: false
18
+ #
19
+ # # good (We cannot identify offenses in this case.)
20
+ # belongs_to :group, options
21
+ #
22
+ class BelongsToOptional < Base
23
+ extend AutoCorrector
24
+
25
+ MSG = 'Specify :optional option.'
26
+
27
+ RESTRICT_ON_SEND = %i[
28
+ belongs_to
29
+ ].freeze
30
+
31
+ def_node_matcher :without_options?, <<~PATTERN
32
+ (send _ _ _ (block ...)?)
33
+ PATTERN
34
+
35
+ def_node_matcher :with_hash_options?, <<~PATTERN
36
+ (send _ _ _ (block ...)? (hash ...))
37
+ PATTERN
38
+
39
+ def_node_matcher :with_optional?, <<~PATTERN
40
+ (send _ _ _ (block ...)? (hash <(pair (sym :optional) _) ...>))
41
+ PATTERN
42
+
43
+ # @param [RuboCop::AST::SendNode] node
44
+ def on_send(node)
45
+ return unless without_options?(node) || (with_hash_options?(node) && !with_optional?(node))
46
+
47
+ add_offense(node) do |corrector|
48
+ corrector.insert_after(node.arguments[-1], ', optional: true')
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -37,7 +37,10 @@ module RuboCop
37
37
  def_node_matcher :order_with_field?, <<~PATTERN
38
38
  (send
39
39
  _ ORDER_METHOD_NAMES
40
- (str /field\(.+\)/)
40
+ {
41
+ (str /field\(.+\)/) |
42
+ (dstr <(str /field\(.+\)/) ...>)
43
+ }
41
44
  ...
42
45
  )
43
46
  PATTERN
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sevencop
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.1'
5
5
  end
data/lib/sevencop.rb CHANGED
@@ -6,6 +6,7 @@ require 'yaml'
6
6
  require_relative 'sevencop/inject'
7
7
  require_relative 'sevencop/version'
8
8
 
9
+ require_relative 'rubocop/cop/sevencop/belongs_to_optional'
9
10
  require_relative 'rubocop/cop/sevencop/order_field'
10
11
  require_relative 'rubocop/cop/sevencop/redundant_existence_check'
11
12
  require_relative 'rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity'
@@ -29,5 +29,5 @@ Gem::Specification.new do |spec|
29
29
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
30
  spec.require_paths = ['lib']
31
31
 
32
- spec.add_runtime_dependency 'rubocop', '>= 1.27'
32
+ spec.add_runtime_dependency 'rubocop'
33
33
  end
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.4.0
4
+ version: 0.5.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-18 00:00:00.000000000 Z
11
+ date: 2022-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.27'
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.27'
26
+ version: '0'
27
27
  description:
28
28
  email:
29
29
  - r7kamura@gmail.com
@@ -41,13 +41,14 @@ files:
41
41
  - README.md
42
42
  - Rakefile
43
43
  - config/default.yml
44
+ - lib/rubocop/cop/sevencop/belongs_to_optional.rb
44
45
  - lib/rubocop/cop/sevencop/order_field.rb
45
46
  - lib/rubocop/cop/sevencop/redundant_existence_check.rb
46
47
  - lib/rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity.rb
47
48
  - lib/sevencop.rb
48
49
  - lib/sevencop/inject.rb
49
50
  - lib/sevencop/version.rb
50
- - rubocop-rails_deprecation.gemspec
51
+ - sevencop.gemspec
51
52
  homepage: https://github.com/r7kamura/sevencop
52
53
  licenses:
53
54
  - MIT