rubocop_method_order 0.1.0.beta.2 → 0.1.0

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: 65922ea8abc76bcda56fb4e87a976c6365411f1830737b5cf20f5fe087342888
4
- data.tar.gz: 514a70a5c948377d6aa37983ba2b0b66c369d3d2c54c2fbbb8c246a4e3a34353
3
+ metadata.gz: ab19c825958da68b876b86e287411591d1656885ad6add3df83d6893e586cc0f
4
+ data.tar.gz: 276d8f2e12003d75b283312aad1f1cc7a8e84166f8cf5332d9c537b95f77cdeb
5
5
  SHA512:
6
- metadata.gz: 21f2f14717003a01794a81b8df81a1e67e4c06ce1806dddf9ddfe275ef0b912cb2e43c48ad0d166b5e8891509e38b2d8167b409da6366bda9e2f9bcab238c936
7
- data.tar.gz: 8afb97c663d3cd4ed382511eee0c698785b6ef6842c87d1d566f0185117981c26e7f1901440f2215e21c60901bf0cecaf6a508a7d388f55c6b55ed1c3ad4d5f1
6
+ metadata.gz: 55fd84404e00fc9bb72cad2fe6bc6a93d1316df805f0cea41947fb02c05ee56c6db941cf07c6694379a929b03ce26515d64ae66275b78defff1359821071e5c5
7
+ data.tar.gz: 9e2964b8653047536814e91c514dfb398fcd2c7329725a2eedf143b9f7cd6dc411bf27898e923398a2f6ef802582620a4a091e8c88f1c4f3c6081706e8224f9f
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -5,8 +5,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
5
5
 
6
6
  ## [Unreleased]
7
7
 
8
+ ## [0.1.0] - 2018-03-21
9
+
8
10
  ### Added
9
11
 
10
12
  * Initial version of the extension.
11
13
 
12
14
  [unreleased]: https://github.com/CoffeeAndCode/rubocop_method_order
15
+ [0.1.0]: https://github.com/CoffeeAndCode/rubocop_method_order/releases/tag/v0.1.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rubocop_method_order (0.1.0.beta.2)
4
+ rubocop_method_order (0.1.0)
5
5
  rubocop (~> 0.53)
6
6
 
7
7
  GEM
@@ -15,7 +15,7 @@ GEM
15
15
  powerpack (0.1.1)
16
16
  rainbow (3.0.0)
17
17
  rake (12.3.0)
18
- rubocop (0.53.0)
18
+ rubocop (0.54.0)
19
19
  parallel (~> 1.10)
20
20
  parser (>= 2.5)
21
21
  powerpack (~> 0.1)
data/README.md CHANGED
@@ -54,14 +54,14 @@ require:
54
54
  ## Usage
55
55
 
56
56
  Here are a few desirable, already ordered examples and you can find more
57
- examples in the [test/fixtures/](test/fixtures/) directory of this project.
57
+ examples in the [test/fixtures/files/](test/fixtures/files/) directory of this project.
58
58
 
59
59
  ```ruby
60
60
  # In this example, `foo` should be listed after `bar` with both after the
61
- # `initialize` method. If in the incorrect order, both methods will
62
- # actually show a linter error with a message indicating if they should
63
- # show 'before' or 'after' a comparision method. The private methods will
64
- # also each show errors in relation to each other if in the opposite order.
61
+ # `initialize` method. If in the incorrect order, each method that should show
62
+ # up before another will show a linter error indicating which method they should
63
+ # be defined before. The private methods will work the same way in the context
64
+ # of just the private methods.
65
65
  class ExampleClass
66
66
  def initialize
67
67
  end
data/Rakefile CHANGED
@@ -13,9 +13,14 @@ RuboCop::RakeTask.new
13
13
 
14
14
  Rake::Task['release'].enhance do
15
15
  require 'digest/sha2'
16
- require_relative '../lib/rubocop_method_order/version'
16
+ require_relative './lib/rubocop_method_order/version'
17
17
 
18
18
  built_gem_path = "pkg/rubocop_method_order-#{RuboCopMethodOrder.gem_version}.gem"
19
+
20
+ checksum = Digest::SHA256.new.hexdigest(File.read(built_gem_path))
21
+ checksum_path = "checksums/rubocop_method_order-#{RuboCopMethodOrder.gem_version}.gem.sha256"
22
+ File.open(checksum_path, 'w') { |file| file.write(checksum) }
23
+
19
24
  checksum = Digest::SHA512.new.hexdigest(File.read(built_gem_path))
20
25
  checksum_path = "checksums/rubocop_method_order-#{RuboCopMethodOrder.gem_version}.gem.sha512"
21
26
  File.open(checksum_path, 'w') { |file| file.write(checksum) }
@@ -15,10 +15,10 @@ module RuboCop
15
15
  #
16
16
  # # bad
17
17
  #
18
- # # `foo` should be listed after `bar`. Both methods will actually
19
- # # show a linter error with a message indicating if they should show
20
- # # before or after the comparision method. The private methods will
21
- # # also each show errors in relation to each other.
18
+ # # `bar` should be listed before `foo`. The method `bar` will show
19
+ # # a linter error with a message indicating which method is should show
20
+ # # before. The private method `another_method` will also an error
21
+ # # saying it should be before `private_method`.
22
22
  # class ExampleClass
23
23
  # def foo
24
24
  # end
@@ -103,18 +103,27 @@ module RuboCop
103
103
  class MethodOrder < Cop
104
104
  include RangeHelp
105
105
 
106
- MSG = 'Method `%<method>s` should come %<direction>s the method `%<other_method>s`.'
106
+ MSG = 'Methods should be sorted in alphabetical order within their section' \
107
+ ' of the code. Method `%<method>s` should come before `%<previous_method>s`.'
107
108
 
108
109
  def autocorrect(node)
110
+ return if @autocorrect_replacements[node].nil?
111
+
109
112
  lambda do |corrector|
110
- source_range = range_with_method_comments(node)
111
- replace_range = range_with_method_comments(@autocorrect_replacements[node])
112
- corrector.replace(replace_range, source_range.source)
113
+ @autocorrect_replacements[node].each do |node_to_move, node_to_replace|
114
+ next if @autocorrected_nodes.include?(node_to_move)
115
+
116
+ @autocorrected_nodes << node_to_move
117
+ node_source = range_with_method_comments(node_to_move).source
118
+ replacement_range = range_with_method_comments(node_to_replace)
119
+ corrector.replace(replacement_range, node_source)
120
+ end
113
121
  end
114
122
  end
115
123
 
116
124
  def investigate(_processed_source)
117
125
  @autocorrect_replacements = {}
126
+ @autocorrected_nodes = []
118
127
  @method_collector = RuboCopMethodOrder::MethodCollector.new(
119
128
  should_skip_method: ->(node) { !cop_enabled_for_node?(node) }
120
129
  )
@@ -168,8 +177,7 @@ module RuboCop
168
177
  method_collection.offenses.each do |offense|
169
178
  add_offense(offense[:node], location: :expression, message: message(
170
179
  offense[:node].method_name,
171
- offense[:other_node].method_name,
172
- offense[:direction]
180
+ offense[:other_node].method_name
173
181
  ))
174
182
  end
175
183
  end
@@ -185,11 +193,10 @@ module RuboCop
185
193
  range.end_pos + last_line.length - range.last_column + 1 # account for \n char
186
194
  end
187
195
 
188
- def message(method_name, following_method_name, direction)
196
+ def message(method_name, following_method_name)
189
197
  format(MSG,
190
- direction: direction,
191
198
  method: method_name,
192
- other_method: following_method_name)
199
+ previous_method: following_method_name)
193
200
  end
194
201
 
195
202
  def range_with_method_comments(node)
@@ -10,13 +10,10 @@ module RuboCopMethodOrder
10
10
  end
11
11
 
12
12
  def offenses
13
- nodes.reject { |node| method_order_correct?(node) }.map do |node|
14
- is_after = expected_method_index(node) > nodes.index(node)
15
-
13
+ nodes.reject { |node| definition_order_correct?(node) }.map do |node|
16
14
  {
17
- direction: is_after ? 'after' : 'before',
18
15
  node: node,
19
- other_node: is_after ? previous_method_node(node) : next_method_node(node)
16
+ other_node: previous_node_from_definition_order(node)
20
17
  }
21
18
  end
22
19
  end
@@ -26,14 +23,25 @@ module RuboCopMethodOrder
26
23
  self
27
24
  end
28
25
 
26
+ # Build a hash for every node that is not at the correct, final position
27
+ # which includes any nodes that need to be moved. Used for autocorrecting.
29
28
  def replacements
30
29
  nodes.reject { |node| method_order_correct?(node) }.each_with_object({}) do |node, obj|
31
- obj[node] = nodes[expected_method_index(node)]
30
+ node_to_replace = nodes[expected_method_index(node)]
31
+
32
+ obj[node] = {
33
+ node => node_to_replace,
34
+ node_to_replace => nodes[expected_method_index(node_to_replace)]
35
+ }
32
36
  end
33
37
  end
34
38
 
35
39
  private
36
40
 
41
+ def definition_order_correct?(method_node)
42
+ previous_node_from_definition_order(method_node).nil?
43
+ end
44
+
37
45
  def expected_method_index(method_node)
38
46
  ordered_nodes.index(method_node)
39
47
  end
@@ -42,22 +50,17 @@ module RuboCopMethodOrder
42
50
  nodes.index(method_node) == ordered_nodes.index(method_node)
43
51
  end
44
52
 
45
- def next_method_node(method_node)
46
- expected_index = expected_method_index(method_node)
47
- return if expected_index.nil?
48
- return if expected_index + 1 >= nodes.size
49
- ordered_nodes[expected_index + 1]
53
+ def ordered_nodes
54
+ nodes.sort(&method(:sort))
50
55
  end
51
56
 
52
- def ordered_nodes
53
- nodes.sort_by(&:method_name)
57
+ def previous_node_from_definition_order(method_node)
58
+ index = nodes.index(method_node)
59
+ nodes[0..index].find { |comp_node| sort(comp_node, method_node).positive? }
54
60
  end
55
61
 
56
- def previous_method_node(method_node)
57
- expected_index = expected_method_index(method_node)
58
- return if expected_index.nil?
59
- return if (expected_index - 1).negative?
60
- ordered_nodes[expected_index - 1]
62
+ def sort(one, two)
63
+ one.method_name <=> two.method_name
61
64
  end
62
65
  end
63
66
  end
@@ -8,17 +8,18 @@ module RuboCopMethodOrder
8
8
  super
9
9
  end
10
10
 
11
- def ordered_nodes
12
- if @initialize_node
13
- [@initialize_node] + (super - [@initialize_node])
14
- else
15
- super
16
- end
17
- end
18
-
19
11
  def push(method_node)
20
12
  @initialize_node = method_node if method_node.method_name == :initialize
21
13
  super(method_node)
22
14
  end
15
+
16
+ def sort(one, two)
17
+ if @initialize_node
18
+ return 0 if one == @initialize_node && two == @initialize_node
19
+ return -1 if one == @initialize_node
20
+ return 1 if two == @initialize_node
21
+ end
22
+ super
23
+ end
23
24
  end
24
25
  end
@@ -12,7 +12,7 @@ module RuboCopMethodOrder
12
12
  MAJOR = 0
13
13
  MINOR = 1
14
14
  PATCH = 0
15
- PRE = 'beta.2'
15
+ PRE = nil
16
16
 
17
17
  STRING = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
18
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop_method_order
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.beta.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Knapp
@@ -34,7 +34,7 @@ cert_chain:
34
34
  XxvXYAfIUSbb6/CHtemZT4zsEprRuk8eADIYUTMyAt9d2roTcsRKgq20m+PZO+b+
35
35
  DVnSbPSTRxaM+9Hds4CAzdoo5hIiLyvSXSqSRGgQEMT0yiFdK+6kRw==
36
36
  -----END CERTIFICATE-----
37
- date: 2018-03-20 00:00:00.000000000 Z
37
+ date: 2018-03-21 00:00:00.000000000 Z
38
38
  dependencies:
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: rubocop
@@ -133,9 +133,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
133
133
  version: 2.3.0
134
134
  required_rubygems_version: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - ">"
136
+ - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: 1.3.1
138
+ version: '0'
139
139
  requirements: []
140
140
  rubyforge_project:
141
141
  rubygems_version: 2.7.3
metadata.gz.sig CHANGED
Binary file