rubocop-pixelforce 0.3 → 0.4

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: f7183c2bd36b9ded637e732f10a442180c1d631946009f0e7b18947cc26c2b91
4
- data.tar.gz: 767a3f22c46a45d9192211fe56f5ba0848ee10bc0a2ab1eb36e089b50ac60692
3
+ metadata.gz: c39237d29ba2cb32b80ca7537a40b943a217099d499cbf65dfe40da2e07e3483
4
+ data.tar.gz: d7d8fd1807d26e2581b871c46cdde1672058300d2bbb206ab46b4f184b1adf1d
5
5
  SHA512:
6
- metadata.gz: a28e3971ac5717957d9ccf62aac47932c4b87b532c6367dbad4b4d985c183944d6559542f0553d47b5667471f2f8341f88466907c5f36afee3df91f0dfd5c1fc
7
- data.tar.gz: eedf613e54c8903a8453fb11e657d93dc5500983864cf2a0407fd98db5169a52970d763fbdd86b707a8a1952adc4a9a34d561fa8d95b80daedbb70a4f70c5544
6
+ metadata.gz: d17544bd7582c2031329d6969ee1631ea973be725b0ba7c5b05f4c8875684ce0a8b07601204519bb499c0d976857489d323c268485340322a7883015c0faea33
7
+ data.tar.gz: 2e93ca8bb8f766cd4ba52adc19ffea77de99cc65274102e06d7f33b4c5b890b315045145dee5a2b8ebaef112bd4a53931aec4c47e00330af8abace65a63cfcc7
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop-pixelforce (0.3)
4
+ rubocop-pixelforce (0.4)
5
5
  rubocop (~> 0.76)
6
6
  rubocop-performance
7
7
  rubocop-rails
@@ -22,7 +22,7 @@ Rails/HasManyOrHasOneDependent:
22
22
  Rails/FilePath:
23
23
  Enabled: false
24
24
 
25
- Pixelforce/ClassStructure:
25
+ Layout/ClassStructure: &class_structures
26
26
  Enabled: true
27
27
  ExpectedOrder:
28
28
  - module_inclusion
@@ -76,6 +76,10 @@ Pixelforce/ClassStructure:
76
76
  - prepend
77
77
  - extend
78
78
 
79
+ Pixelforce/EmptyLineBwteenCategories:
80
+ <<: *class_structures
81
+ Enabled: true
82
+
79
83
  Layout/AlignHash:
80
84
  EnforcedColonStyle: table
81
85
 
@@ -1,3 +1,3 @@
1
1
  require 'rubocop'
2
2
 
3
- require 'rubocop/cop/pixelforce/class_structure'
3
+ require 'rubocop/cop/pixelforce/empty_line_between_categories'
@@ -1,7 +1,9 @@
1
1
  module RuboCop
2
2
  module Cop
3
3
  module Pixelforce
4
- class ClassStructure < Cop
4
+ class EmptyLineBwteenCategories < Cop
5
+ include RangeHelp
6
+
5
7
  HUMANIZED_NODE_TYPE = {
6
8
  casgn: :constants,
7
9
  defs: :class_methods,
@@ -10,8 +12,7 @@ module RuboCop
10
12
  }.freeze
11
13
 
12
14
  VISIBILITY_SCOPES = %i[private protected public].freeze
13
- MSG = '`%<category>s` is supposed to appear before ' \
14
- '`%<previous>s`.'
15
+ MSG = 'Use empty lines between categories.'
15
16
 
16
17
  def_node_matcher :visibility_block?, <<~PATTERN
17
18
  (send nil? { :private :protected :public })
@@ -20,22 +21,13 @@ module RuboCop
20
21
  # Validates code style on class declaration.
21
22
  # Add offense when find a node out of expected order.
22
23
  def on_class(class_node)
23
- previous = -1
24
24
  previous_category = nil
25
25
  previous_node_end_line = -1
26
26
  previous_method_name = nil
27
27
  walk_over_nested_class_definition(class_node) do |node, category|
28
- index = expected_order.index(category)
29
- if index < previous
30
- message = format(MSG, category: category,
31
- previous: expected_order[previous])
32
- add_offense(node, message: message)
33
- end
34
- previous = index
35
-
36
28
  next unless node.respond_to?(:method_name)
37
29
  if previous_category && category != previous_category && node.loc.first_line - previous_node_end_line < 2
38
- add_offense(node, message: "Use empty lines between categories.")
30
+ add_offense(node, message: MSG)
39
31
  end
40
32
 
41
33
  if previous_method_name && previous_method_name == node.method_name && node.loc.first_line - previous_node_end_line > 1
@@ -45,22 +37,20 @@ module RuboCop
45
37
  previous_node_end_line = node.loc.last_line
46
38
  previous_method_name = node.method_name
47
39
  end
48
- end
40
+ end
49
41
 
50
- # Autocorrect by swapping between two nodes autocorrecting them
51
42
  def autocorrect(node)
52
- node_classification = classify(node)
53
- previous = left_siblings_of(node).find do |sibling|
54
- classification = classify(sibling)
55
- !ignore?(classification) && node_classification != classification
56
- end
43
+ prev_category = prev_node(node)
44
+ # finds position of first newline
45
+ end_pos = end_position_for(prev_category)
46
+ newline_pos = buffer.source.index("\n", end_pos)
57
47
 
58
- current_range = source_range_with_comment(node)
59
- previous_range = source_range_with_comment(previous)
48
+ count = blank_lines_count_between(prev_category, node)
60
49
 
61
- lambda do |corrector|
62
- corrector.insert_before(previous_range, current_range.source)
63
- corrector.remove(current_range)
50
+ if count > 1
51
+ autocorrect_remove_lines(newline_pos, count)
52
+ else
53
+ autocorrect_insert_lines(newline_pos, count)
64
54
  end
65
55
  end
66
56
 
@@ -209,6 +199,16 @@ module RuboCop
209
199
  processed_source.buffer
210
200
  end
211
201
 
202
+ def prev_node(node)
203
+ return nil unless node.sibling_index.positive?
204
+
205
+ node.parent.children[node.sibling_index - 1]
206
+ end
207
+
208
+ def blank_lines_count_between(prev_category, node)
209
+ node.loc.first_line - prev_category.loc.last_line
210
+ end
211
+
212
212
  # Load expected order from `ExpectedOrder` config.
213
213
  # Define new terms in the expected order by adding new {categories}.
214
214
  def expected_order
@@ -220,6 +220,20 @@ module RuboCop
220
220
  def categories
221
221
  cop_config['Categories']
222
222
  end
223
+
224
+ def autocorrect_remove_lines(newline_pos, count)
225
+ range_to_remove = range_between(newline_pos, newline_pos + 1)
226
+ lambda do |corrector|
227
+ corrector.remove(range_to_remove)
228
+ end
229
+ end
230
+
231
+ def autocorrect_insert_lines(newline_pos, count)
232
+ where_to_insert = range_between(newline_pos, newline_pos + 1)
233
+ lambda do |corrector|
234
+ corrector.insert_after(where_to_insert, "\n")
235
+ end
236
+ end
223
237
  end
224
238
  end
225
239
  end
@@ -1,5 +1,5 @@
1
1
  module Rubocop
2
2
  module Pixelforce
3
- VERSION = "0.3"
3
+ VERSION = "0.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-pixelforce
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '0.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Zhang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-08 00:00:00.000000000 Z
11
+ date: 2019-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -113,7 +113,7 @@ files:
113
113
  - bin/setup
114
114
  - default.yml
115
115
  - lib/rubocop-pixelforce.rb
116
- - lib/rubocop/cop/pixelforce/class_structure.rb
116
+ - lib/rubocop/cop/pixelforce/empty_line_between_categories.rb
117
117
  - lib/rubocop/pixelforce.rb
118
118
  - lib/rubocop/pixelforce/version.rb
119
119
  - rubocop-pixelforce.gemspec