sevencop 0.18.4 → 0.19.1

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: ddb1ab8850e168dd10c98cf0954d8e9df26cf1b0d43c1019780c2ce76d84d8d6
4
- data.tar.gz: d159188fae4618097806c22ef26825993b821f2277671011f9c89997056fb364
3
+ metadata.gz: 5664628e10057bbf8aed265ce915891333cdb0236e5c2ba3816e46d929d566ac
4
+ data.tar.gz: 52160da1f19f3621bdc623c5d840020a1da412a98011b58f3703e455a67715c8
5
5
  SHA512:
6
- metadata.gz: 449aff2e37cf2abd954b38db5ee587401657f261b584097d4390ab23a76df991505561e7462e8683d950a126fb7675a2b0445a868fc0802ab7711d9a10cc9d2d
7
- data.tar.gz: 5171d0b62826dd0d85371a7b81fcae4917a49a3cf1de2f4d19ed7e61f38e670c8bacc0e7afd2934aac060e889a44d7475013d031eaaed68d9d7006ab0b85f03f
6
+ metadata.gz: 50120fa7354c41fe5228a3f252ce2c38dc041d005b1694efba98603c3db2ee33a42e49c7dd2d2b3dc5bf8dbc406fa948de312d20c5f9c68beafd7a075c2e752e
7
+ data.tar.gz: 751cda89e7bee353e85ecc990375ea70abca1db3fcf09d52f016be522767121c1015a7a5461767d666f3803f7ea3462355b66a8b5386fef5158ec61bccc0fc11
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sevencop (0.18.4)
4
+ sevencop (0.19.1)
5
5
  rubocop
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -31,6 +31,7 @@ Choose the cops you want to use and enable them on your .rubocop.yml.
31
31
  - [Sevencop/AutoloadOrdered](lib/rubocop/cop/sevencop/autoload_ordered.rb)
32
32
  - [Sevencop/HashElementOrdered](lib/rubocop/cop/sevencop/hash_element_ordered.rb)
33
33
  - [Sevencop/MethodDefinitionArgumentsMultiline](lib/rubocop/cop/sevencop/method_definition_arguments_multiline.rb)
34
+ - [Sevencop/MethodDefinitionInIncluded](lib/rubocop/cop/sevencop/method_definition_in_included.rb)
34
35
  - [Sevencop/MethodDefinitionKeywordArgumentOrdered](lib/rubocop/cop/sevencop/method_definition_keyword_argument_ordered.rb)
35
36
  - [Sevencop/MethodDefinitionOrdered](lib/rubocop/cop/sevencop/method_definition_ordered.rb)
36
37
  - [Sevencop/RailsBelongsToOptional](lib/rubocop/cop/sevencop/rails_belongs_to_optional.rb)
data/config/default.yml CHANGED
@@ -16,6 +16,12 @@ Sevencop/MethodDefinitionArgumentsMultiline:
16
16
  Enabled: false
17
17
  VersionAdded: '0.11'
18
18
 
19
+ Sevencop/MethodDefinitionInIncluded:
20
+ Description: |
21
+ Do not define methods in `included` blocks.
22
+ Enabled: false
23
+ VersionAdded: '0.19'
24
+
19
25
  Sevencop/MethodDefinitionOrdered:
20
26
  Description: |
21
27
  Sort method definitions in alphabetical order.
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Sevencop
6
+ # Do not define methods in `included` blocks.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # module A
11
+ # extend ::ActiveSupport::Concern
12
+ #
13
+ # included do
14
+ # def foo
15
+ # end
16
+ # end
17
+ # end
18
+ #
19
+ # # good
20
+ # module A
21
+ # extend ::ActiveSupport::Concern
22
+ #
23
+ # def foo
24
+ # end
25
+ # end
26
+ class MethodDefinitionInIncluded < RuboCop::Cop::Base
27
+ extend AutoCorrector
28
+
29
+ include RangeHelp
30
+ include VisibilityHelp
31
+
32
+ MSG = 'Do not define methods in `included` blocks.'
33
+
34
+ # @param node [RuboCop::AST::DefNode]
35
+ # @return [void]
36
+ def on_def(node)
37
+ block = find_ancestor_included_block(node)
38
+ return unless block
39
+
40
+ add_offense(node) do |corrector|
41
+ corrector.insert_before(
42
+ range_with_comments_and_lines(block),
43
+ "#{range_with_comments_and_lines(node).source}\n".sub(
44
+ /\bdef /, "#{node_visibility(node)} def "
45
+ )
46
+ )
47
+ corrector.remove(
48
+ range_with_comments_and_lines(node)
49
+ )
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ # @param range1 [Parser::Source::Range]
56
+ # @param range2 [Parser::Source::Range]
57
+ # @return [Parser::Source::Range]
58
+ def add_range(
59
+ range1,
60
+ range2
61
+ )
62
+ range1.with(
63
+ begin_pos: [range1.begin_pos, range2.begin_pos].min,
64
+ end_pos: [range1.end_pos, range2.end_pos].max
65
+ )
66
+ end
67
+
68
+ # @param node [RuboCop::AST::DefNode]
69
+ # @return [RuboCop::AST::BlockNode, nil]
70
+ def find_ancestor_included_block(node)
71
+ if node.parent&.block_type?
72
+ node.parent if node.parent.method?(:included)
73
+ elsif node.parent&.begin_type?
74
+ node.parent.parent if node.parent.parent&.block_type? && node.parent.parent&.method?(:included)
75
+ end
76
+ end
77
+
78
+ # @param node [RuboCop::AST::DefNode]
79
+ # @return [Boolean]
80
+ def private?(node)
81
+ node_visibility(node) == :private
82
+ end
83
+
84
+ # @param node [RuboCop::AST::Node]
85
+ # @return [Paresr::Source::Range]
86
+ def range_with_comments(node)
87
+ ranges = [
88
+ node,
89
+ *processed_source.ast_with_comments[node]
90
+ ].map do |element|
91
+ element.location.expression
92
+ end
93
+ ranges.reduce do |result, range|
94
+ add_range(result, range)
95
+ end
96
+ end
97
+
98
+ # @param node [RuboCop::AST::Node]
99
+ # @return [Parser::Source::Range]
100
+ def range_with_comments_and_lines(node)
101
+ range_by_whole_lines(
102
+ range_with_comments(node),
103
+ include_final_newline: true
104
+ )
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Sevencop
4
- VERSION = '0.18.4'
4
+ VERSION = '0.19.1'
5
5
  end
data/lib/sevencop.rb CHANGED
@@ -7,6 +7,7 @@ require_relative 'sevencop/version'
7
7
  require_relative 'rubocop/cop/sevencop/autoload_ordered'
8
8
  require_relative 'rubocop/cop/sevencop/hash_element_ordered'
9
9
  require_relative 'rubocop/cop/sevencop/method_definition_arguments_multiline'
10
+ require_relative 'rubocop/cop/sevencop/method_definition_in_included'
10
11
  require_relative 'rubocop/cop/sevencop/method_definition_keyword_argument_ordered'
11
12
  require_relative 'rubocop/cop/sevencop/method_definition_ordered'
12
13
  require_relative 'rubocop/cop/sevencop/rails_belongs_to_optional'
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.18.4
4
+ version: 0.19.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-10-11 00:00:00.000000000 Z
11
+ date: 2022-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -44,6 +44,7 @@ files:
44
44
  - lib/rubocop/cop/sevencop/autoload_ordered.rb
45
45
  - lib/rubocop/cop/sevencop/hash_element_ordered.rb
46
46
  - lib/rubocop/cop/sevencop/method_definition_arguments_multiline.rb
47
+ - lib/rubocop/cop/sevencop/method_definition_in_included.rb
47
48
  - lib/rubocop/cop/sevencop/method_definition_keyword_argument_ordered.rb
48
49
  - lib/rubocop/cop/sevencop/method_definition_ordered.rb
49
50
  - lib/rubocop/cop/sevencop/rails_belongs_to_optional.rb