rubocop-canon 0.2.0 → 0.2.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: bdf44fc516121d61d209401a8c2b58f4603ce3d3c7951ec9a232a4acc8cc5175
4
- data.tar.gz: 7881f90e36d55ba4bc3a7cd940640827ab91973e53c13d494da72058ba837c43
3
+ metadata.gz: 24d224c2ecf5e361f702c300a79778baf8ad63247fb726cb8c39b89534d7dd3f
4
+ data.tar.gz: 8e4e0ca31f844a61bdba984c2efd2ccadb2d0ddea11693f38dc452c9b356e40c
5
5
  SHA512:
6
- metadata.gz: 9903f67032ee8161d257aee6f9a773def334c7537eb2e9f066ac0a90bf1fa3dabe21f96a9e7208575ead7baabbef0bb7013ad22502f8a76c2ebbd6a7487f5569
7
- data.tar.gz: fcf0cd9d4304f5dee4bb21d73d914a58e8dc240c17f3d55dfedfca2a8ee766368437871a899dd76eb607f81ef616cb7c31fe7b6221d23119b9dba02ccf020c7b
6
+ metadata.gz: b1eea01a2c5652d7513ccfe08b048cd61817f4f20c3dcb757c5e7b0e96ac99c92d0b52f1840d40b69a9c3c5b2eced42444c22ae29ed3461eedc83c1dbd4b5081
7
+ data.tar.gz: 98f0a6237ae0b71995af7bf03e8d8b46bcf83ebd92f3fa0ddf81f607a503ab71061b365f2e19a05279702791998be00d7cd1d621224dcdd965ae79d2d3fc2d84
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Canon
5
- VERSION = '0.2.0'
5
+ VERSION = '0.2.1'
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 block is a group of one whatever it calls, so a DSL block always
16
+ # stands apart from the declarations around it. Everything else the cop
17
+ # has not been told about — an unlisted method name, a constant — it
18
+ # 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,30 @@ 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?
105
-
106
- grouped_key(node.method_name)
114
+ def standalone?(node)
115
+ STANDALONE_TYPES.include?(node.type)
107
116
  end
108
117
 
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?
118
+ def group_key(node)
119
+ return unless node.send_type?
113
120
 
114
- group_index
121
+ grouped_methods.index { |group| group.include?(node.method_name.to_s) }
115
122
  end
116
123
 
117
124
  def declaration_context?(node)
@@ -123,7 +130,9 @@ module RuboCop
123
130
  end
124
131
 
125
132
  def declaration?(node)
126
- return true if DECLARATION_TYPES.include?(node.type)
133
+ return true if node.casgn_type?
134
+ return true if DEFINITION_TYPES.include?(node.type)
135
+ return node.send_node.receiver.nil? if BLOCK_TYPES.include?(node.type)
127
136
 
128
137
  node.send_type? && node.receiver.nil?
129
138
  end
@@ -139,10 +148,6 @@ module RuboCop
139
148
  ACCESS_MODIFIERS.include?(node.method_name)
140
149
  end
141
150
 
142
- def multiline?(node)
143
- node.first_line != node.last_line
144
- end
145
-
146
151
  def grouped_methods
147
152
  @grouped_methods ||= Array(cop_config['GroupedMethods']).map { |group| Array(group).map(&:to_s) }
148
153
  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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - skiftle