rubocop-canon 0.2.0 → 0.2.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: bdf44fc516121d61d209401a8c2b58f4603ce3d3c7951ec9a232a4acc8cc5175
4
- data.tar.gz: 7881f90e36d55ba4bc3a7cd940640827ab91973e53c13d494da72058ba837c43
3
+ metadata.gz: c0d98429626224c644766d7ef86c951721e1cacb1ed1f2ca8399088dc7e83b26
4
+ data.tar.gz: 7f07b6c9c8a8005d3403a2ec8fbdd01e9048a142bf2cbd16fd0274166f3808bf
5
5
  SHA512:
6
- metadata.gz: 9903f67032ee8161d257aee6f9a773def334c7537eb2e9f066ac0a90bf1fa3dabe21f96a9e7208575ead7baabbef0bb7013ad22502f8a76c2ebbd6a7487f5569
7
- data.tar.gz: fcf0cd9d4304f5dee4bb21d73d914a58e8dc240c17f3d55dfedfca2a8ee766368437871a899dd76eb607f81ef616cb7c31fe7b6221d23119b9dba02ccf020c7b
6
+ metadata.gz: 59e81cc7a968cdc8bb061621f7526ee386df07b354d75b305010c5290d52286e8d5a4906f24c41aaad28cba648288d6e675c45d415edd2d2569ad6d53de4337a
7
+ data.tar.gz: 00a20f3e22afc400098761be958ffc1d1bbc8eddffbb9738969037b42dc4ca0babcaa0d9c54a21ba093142d5c64b4a40b0572affa30a17d8b0a0d2cbf6ac1ed9
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Canon
5
- VERSION = '0.2.0'
5
+ VERSION = '0.2.2'
6
6
  end
7
7
  end
@@ -15,6 +15,10 @@ module RuboCop
15
15
  # against the leftmost call in a chain. Both are empty by default, so
16
16
  # the cop does nothing until it is configured.
17
17
  #
18
+ # A body carrying more phases than `MaxPhases` allows keeps the last
19
+ # separator before the trailing phase and loses the earlier ones —
20
+ # everything ahead of the last leading statement is one phase.
21
+ #
18
22
  # @example Blocks: ['step'], TrailingMethods: ['report']
19
23
  # # bad — no blank line opens the trailing phase
20
24
  # step 'totals the order' do
@@ -91,12 +95,22 @@ module RuboCop
91
95
  end
92
96
 
93
97
  def check_phase_count(statements)
94
- separators = statements.each_cons(2).count do |previous, following|
95
- blank_lines_between(previous, following).positive?
98
+ separators = leading_separators(statements)
99
+ return if separators.size < max_phases - 1
100
+
101
+ surplus = separators[0..-(max_phases - 1)]
102
+
103
+ surplus.each do |previous|
104
+ register_extra_blank_line(previous, format(MSG_TOO_MANY_PHASES, max_phases:))
96
105
  end
97
- return if separators < max_phases - 1
106
+ end
98
107
 
99
- add_offense(statements.first, message: format(MSG_TOO_MANY_PHASES, max_phases:))
108
+ def leading_separators(statements)
109
+ statements.each_cons(2).filter_map do |previous, following|
110
+ next if comments_between?(previous, following)
111
+
112
+ previous if blank_lines_between(previous, following) == 1
113
+ end
100
114
  end
101
115
 
102
116
  def detect_trailing_start(statements)
@@ -6,31 +6,26 @@ module RuboCop
6
6
  # Enforces blank lines between declaration groups in a class, module or
7
7
  # DSL block body.
8
8
  #
9
- # Two declarations belong to the same group when they call the same
10
- # method and both fit on one line. A declaration spanning several lines,
11
- # and any block, is a group of one. Groups are separated by exactly one
12
- # blank line; declarations inside a group are not separated at all.
9
+ # `GroupedMethods` is the whole specification: two declarations calling
10
+ # methods listed in the same group take no blank line between them, and
11
+ # two calling methods listed in different groups take exactly one. The
12
+ # cop says nothing about a method name it has not been given, so a DSL
13
+ # it does not know stays as its author wrote it.
13
14
  #
14
- # `GroupedMethods` merges method names that belong to one group.
15
+ # A declaration spanning several lines, and any block, is a group of one
16
+ # whatever it calls, so it always stands apart from its neighbours.
17
+ # Everything else the cop has not been told about — an unlisted method
18
+ # name on one line, a constant — it leaves alone.
15
19
  #
16
- # @example
20
+ # @example GroupedMethods: [[belongs_to, has_many], [validate, validates]]
17
21
  # # bad
18
22
  # class Shift < ApplicationRecord
19
- # belongs_to :service
20
- #
21
- # belongs_to :site
22
- # validates :starts_at, presence: true
23
- # end
24
- #
25
- # # good
26
- # class Shift < ApplicationRecord
27
- # belongs_to :service
28
23
  # belongs_to :site
29
24
  #
25
+ # has_many :shift_assignments, dependent: :destroy
30
26
  # validates :starts_at, presence: true
31
27
  # end
32
28
  #
33
- # @example GroupedMethods: [[belongs_to, has_many]]
34
29
  # # good
35
30
  # class Shift < ApplicationRecord
36
31
  # belongs_to :site
@@ -38,6 +33,12 @@ module RuboCop
38
33
  #
39
34
  # validates :starts_at, presence: true
40
35
  # end
36
+ #
37
+ # # good — neither name is listed, so the cop leaves the body alone
38
+ # class Filtering < Capability::Base
39
+ # request_transformer RequestTransformer
40
+ # api_builder APIBuilder
41
+ # end
41
42
  class DeclarationGroups < Base
42
43
  extend AutoCorrector
43
44
  include BlankLineHelp
@@ -46,7 +47,8 @@ module RuboCop
46
47
  MSG_MISSING = 'Add a blank line between declaration groups.'
47
48
  ACCESS_MODIFIERS = %i[private protected public module_function private_class_method].freeze
48
49
  DEFINITION_TYPES = %i[def defs].freeze
49
- DECLARATION_TYPES = %i[casgn block numblock def defs].freeze
50
+ BLOCK_TYPES = %i[block numblock].freeze
51
+ STANDALONE_TYPES = %i[block numblock def defs].freeze
50
52
 
51
53
  def on_class(node)
52
54
  check_declarations(node)
@@ -75,6 +77,7 @@ module RuboCop
75
77
  next if definitions?(previous, following)
76
78
  next if access_modifier?(previous)
77
79
  next if access_modifier?(following)
80
+ next unless grouped?(previous, following)
78
81
 
79
82
  check_gap(previous, following, expected_blank_lines(previous, following))
80
83
  end
@@ -92,26 +95,36 @@ module RuboCop
92
95
  register_missing_blank_line(previous, following, MSG_MISSING)
93
96
  end
94
97
 
98
+ def grouped?(previous, following)
99
+ return true if standalone?(previous)
100
+ return true if standalone?(following)
101
+ return false if group_key(previous).nil?
102
+
103
+ !group_key(following).nil?
104
+ end
105
+
95
106
  def expected_blank_lines(previous, following)
107
+ return 1 if standalone?(previous)
108
+ return 1 if standalone?(following)
96
109
  return 0 if group_key(previous) == group_key(following)
97
110
 
98
111
  1
99
112
  end
100
113
 
101
- def group_key(node)
102
- return node.object_id if multiline?(node)
103
- return :constant if node.casgn_type?
104
- return node.object_id unless node.send_type?
114
+ def standalone?(node)
115
+ return true if STANDALONE_TYPES.include?(node.type)
105
116
 
106
- grouped_key(node.method_name)
117
+ multiline?(node)
107
118
  end
108
119
 
109
- def grouped_key(method_name)
110
- name = method_name.to_s
111
- group_index = grouped_methods.index { |group| group.include?(name) }
112
- return name if group_index.nil?
120
+ def multiline?(node)
121
+ node.first_line != node.last_line
122
+ end
113
123
 
114
- group_index
124
+ def group_key(node)
125
+ return unless node.send_type?
126
+
127
+ grouped_methods.index { |group| group.include?(node.method_name.to_s) }
115
128
  end
116
129
 
117
130
  def declaration_context?(node)
@@ -123,7 +136,9 @@ module RuboCop
123
136
  end
124
137
 
125
138
  def declaration?(node)
126
- return true if DECLARATION_TYPES.include?(node.type)
139
+ return true if node.casgn_type?
140
+ return true if DEFINITION_TYPES.include?(node.type)
141
+ return node.send_node.receiver.nil? if BLOCK_TYPES.include?(node.type)
127
142
 
128
143
  node.send_type? && node.receiver.nil?
129
144
  end
@@ -139,10 +154,6 @@ module RuboCop
139
154
  ACCESS_MODIFIERS.include?(node.method_name)
140
155
  end
141
156
 
142
- def multiline?(node)
143
- node.first_line != node.last_line
144
- end
145
-
146
157
  def grouped_methods
147
158
  @grouped_methods ||= Array(cop_config['GroupedMethods']).map { |group| Array(group).map(&:to_s) }
148
159
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-canon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - skiftle