sevencop 0.3.0 → 0.5.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: 18546d00ceb4ff7f756715ecdbe89e7941a51855df542b4ad2a8a05843ebc46a
4
- data.tar.gz: b968132f755f723217b47fd81126bb11242e0702b267fcc9ef49eac054c9c22a
3
+ metadata.gz: 319d9ad3cd53f54122dd36a4d53d4ba2b1e496831c3c26022b2a8494f5a95fdc
4
+ data.tar.gz: b54dd12e0177a48827cb87f35fa6af902e256f996c96a990933c08c22e8b91f5
5
5
  SHA512:
6
- metadata.gz: d1e87a57863fe1881b6fc0c923213d9d563cdeba24c1ab92715bab58004efbdaf3b011319b1ddc3215b5b20f443a40c99cd515754312a0ba920ff601cd6eb7ab
7
- data.tar.gz: ae482542aa0d259384cba3bfd04932ae1811b9ed686245ebd3de5f1e1fa7deac48a90fcea0b2f0276afcd9244846b7b59fd4ba1ceef846d1217b505b183877c4
6
+ metadata.gz: 07d68c578fa6807867b0caa213706484ff8c04475e1f17ce0f3121a37307f0cb48962a1ee7c1782dcbaedbff34748a075a41522e8d9c79dcdf24e9d4b8aa581a
7
+ data.tar.gz: e47791134b21b41a4d9fcae6503db43cefb2d377391b7e3c5a7537bd5944fecd444164931df15035d628b939ce6fe5e6fac50694aef76c856c66c3a97763b04a
data/CHANGELOG.md CHANGED
@@ -2,6 +2,37 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.5.0 - 2022-06-27
6
+
7
+ ### Added
8
+
9
+ - Add `Sevencop/BelongsToOptional` cop.
10
+
11
+ ### Changed
12
+
13
+ - Make `Sevencop/RedundantExistenceCheck` cop disabled by default.
14
+
15
+ ### Removed
16
+
17
+ - Remove rubocop version dependency.
18
+
19
+ ## 0.4.1 - 2022-06-23
20
+
21
+ ### Fixed
22
+
23
+ - Fix `Sevencop/OrderField` on dstr.
24
+
25
+ ## 0.4.0 - 2022-06-18
26
+
27
+ ### Added
28
+
29
+ - Add `Sevencop/OrderField` cop.
30
+
31
+ ### Changed
32
+
33
+ - Improve performance of `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
34
+ - Improve offense location of `Sevencop/UniquenessValidatorExplicitCaseSensitivity` cop.
35
+
5
36
  ## 0.3.0 - 2022-06-18
6
37
 
7
38
  ### Added
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sevencop (0.3.0)
5
- rubocop (>= 1.27)
4
+ sevencop (0.5.0)
5
+ rubocop
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -35,7 +35,47 @@ require:
35
35
 
36
36
  ## Cops
37
37
 
38
- ### `Sevencop/RedundantExistenceCheck`
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
+
60
+ ### Sevencop/OrderField
61
+
62
+ Identifies a String including "field" is passed to `order` or `reorder`.
63
+
64
+ ```ruby
65
+ # bad
66
+ articles.order('field(id, ?)', a)
67
+
68
+ # good
69
+ articles.order(Arel.sql('field(id, ?)'), a)
70
+
71
+ # bad
72
+ reorder('field(id, ?)', a)
73
+
74
+ # good
75
+ reorder(Arel.sql('field(id, ?)'), a)
76
+ ```
77
+
78
+ ### Sevencop/RedundantExistenceCheck
39
79
 
40
80
  Identifies redundant existent check before file operation.
41
81
 
@@ -53,7 +93,7 @@ FileUtils.rm(a) if FileTest.exist?(a)
53
93
  FileUtils.rm_f(a)
54
94
  ```
55
95
 
56
- ### `Sevencop/UniquenessValidatorExplicitCaseSensitivity`
96
+ ### Sevencop/UniquenessValidatorExplicitCaseSensitivity
57
97
 
58
98
  Identifies use of UniquenessValidator without :case_sensitive option.
59
99
 
@@ -75,5 +115,3 @@ validates :name, uniqueness: { allow_nil: true, scope: :user_id, case_sensitive:
75
115
  ```
76
116
 
77
117
  Useful to keep the same behavior between Rails 6.0 and 6.1 where case insensitive collation is used in MySQL.
78
-
79
- Note that this cop is `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
 
@@ -24,7 +24,7 @@ task :new_cop, [:cop] do |_task, args|
24
24
 
25
25
  generator.write_source
26
26
  generator.write_spec
27
- generator.inject_require(root_file_path: 'lib/rubocop/cop/rails_deprecation_cops.rb')
27
+ generator.inject_require(root_file_path: 'lib/sevencop.rb')
28
28
  generator.inject_config(config_file_path: 'config/default.yml')
29
29
 
30
30
  puts generator.todo
data/config/default.yml CHANGED
@@ -1,6 +1,19 @@
1
+ Sevencop/BelongsToOptional:
2
+ Description: |
3
+ Force `belongs_to` with `optional: true` option.
4
+ Enabled: false
5
+ Safe: false
6
+ VersionAdded: '0.5'
7
+
8
+ Sevencop/OrderField:
9
+ Description: Wrap safe SQL String by `Arel.sql`.
10
+ Enabled: false
11
+ Safe: false
12
+ VersionAdded: '0.4'
13
+
1
14
  Sevencop/RedundantExistenceCheck:
2
15
  Description: Avoid redundant existent check before file operation.
3
- Enabled: true
16
+ Enabled: false
4
17
  Safe: false
5
18
  VersionAdded: '0.1'
6
19
 
@@ -0,0 +1,58 @@
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 _ _ _)
33
+ PATTERN
34
+
35
+ def_node_matcher :with_hash_options?, <<~PATTERN
36
+ (send _ _ _ (hash ...))
37
+ PATTERN
38
+
39
+ def_node_matcher :with_optional?, <<~PATTERN
40
+ (send _ _ _ (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
+ if node.arguments[-1].hash_type?
49
+ corrector.insert_after(node.arguments[-1], ', optional: true')
50
+ elsif node.arguments.length == 1
51
+ corrector.insert_after(node.arguments.first, ', optional: true')
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sevencop
6
+ # Identifies a String including "field" is passed to `order` or `reorder`.
7
+ #
8
+ # @safety
9
+ # This cop is unsafe because it can register a false positive.
10
+ #
11
+ # @example
12
+ #
13
+ # # bad
14
+ # articles.order('field(id, ?)', a)
15
+ #
16
+ # # good
17
+ # articles.order(Arel.sql('field(id, ?)'), a)
18
+ #
19
+ # # bad
20
+ # reorder('field(id, ?)', a)
21
+ #
22
+ # # good
23
+ # reorder(Arel.sql('field(id, ?)'), a)
24
+ #
25
+ class OrderField < Base
26
+ extend AutoCorrector
27
+
28
+ MSG = 'Wrap safe SQL String by `Arel.sql`.'
29
+
30
+ RESTRICT_ON_SEND = %i[
31
+ order
32
+ reorder
33
+ ].freeze
34
+
35
+ ORDER_METHOD_NAMES = RESTRICT_ON_SEND.to_set.freeze
36
+
37
+ def_node_matcher :order_with_field?, <<~PATTERN
38
+ (send
39
+ _ ORDER_METHOD_NAMES
40
+ {
41
+ (str /field\(.+\)/) |
42
+ (dstr <(str /field\(.+\)/) ...>)
43
+ }
44
+ ...
45
+ )
46
+ PATTERN
47
+
48
+ # @param [RuboCop::AST::SendNode] node
49
+ def on_send(node)
50
+ return unless order_with_field?(node)
51
+
52
+ first_argument_node = node.arguments.first
53
+ add_offense(first_argument_node) do |corrector|
54
+ corrector.replace(
55
+ node.arguments.first,
56
+ "Arel.sql(#{first_argument_node.source})"
57
+ )
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -31,6 +31,8 @@ module RuboCop
31
31
 
32
32
  MSG = 'Specify :case_sensitivity option on use of UniquenessValidator.'
33
33
 
34
+ RESTRICT_ON_SEND = %i[validates].freeze
35
+
34
36
  def_node_matcher :validates_uniqueness?, <<~PATTERN
35
37
  (send nil? :validates
36
38
  _
@@ -65,8 +67,8 @@ module RuboCop
65
67
  def on_send(node)
66
68
  return unless validates_uniqueness?(node) && !validates_uniqueness_with_case_sensitivity?(node)
67
69
 
68
- add_offense(node) do |corrector|
69
- uniqueness_value = find_uniqueness_value(node)
70
+ uniqueness_value = find_uniqueness_value(node)
71
+ add_offense(uniqueness_value) do |corrector|
70
72
  if uniqueness_value.true_type?
71
73
  corrector.replace(
72
74
  uniqueness_value.source_range,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sevencop
4
- VERSION = '0.3.0'
4
+ VERSION = '0.5.0'
5
5
  end
data/lib/sevencop.rb CHANGED
@@ -6,6 +6,8 @@ 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'
10
+ require_relative 'rubocop/cop/sevencop/order_field'
9
11
  require_relative 'rubocop/cop/sevencop/redundant_existence_check'
10
12
  require_relative 'rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity'
11
13
 
@@ -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.3.0
4
+ version: 0.5.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-17 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,12 +41,14 @@ files:
41
41
  - README.md
42
42
  - Rakefile
43
43
  - config/default.yml
44
+ - lib/rubocop/cop/sevencop/belongs_to_optional.rb
45
+ - lib/rubocop/cop/sevencop/order_field.rb
44
46
  - lib/rubocop/cop/sevencop/redundant_existence_check.rb
45
47
  - lib/rubocop/cop/sevencop/uniqueness_validator_explicit_case_sensitivity.rb
46
48
  - lib/sevencop.rb
47
49
  - lib/sevencop/inject.rb
48
50
  - lib/sevencop/version.rb
49
- - rubocop-rails_deprecation.gemspec
51
+ - sevencop.gemspec
50
52
  homepage: https://github.com/r7kamura/sevencop
51
53
  licenses:
52
54
  - MIT
@@ -70,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
72
  - !ruby/object:Gem::Version
71
73
  version: '0'
72
74
  requirements: []
73
- rubygems_version: 3.3.7
75
+ rubygems_version: 3.2.32
74
76
  signing_key:
75
77
  specification_version: 4
76
78
  summary: Custom cops for RuboCop.