rubocop-ordered_methods 0.10 → 0.12

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: c43226dde578a4844ba7c579a7fa5dfb52f05dce2e84d9a68f1e250e272d4e58
4
- data.tar.gz: 636542d7c4be5982d3424703ba2062c72c4fdbfcf4f289a5b593050b68c16969
3
+ metadata.gz: a73cfd25f43f431101b9822358b1c59180d41a56f170bf614d7ec0149f4f2c3e
4
+ data.tar.gz: 3c470834560d76327fc38f4163284ab4131214d8b944f0ffe34ede2e4164caab
5
5
  SHA512:
6
- metadata.gz: 1eced8a7bb0e4a33bf58cae6b0b0d90fd9814318635689450ee34646905a5a41fbd97189c51d9763706f307d4ed4c3032e3eb02af2aa5ce79c373f12bd5f1019
7
- data.tar.gz: 4c528b7368611c20b2ab790f28502f3c44e4e6546bcbff35b199c749932583bdea36cc5d441e598efb7b7e199b63fbac97f50a6534b206052f60474ebe24147a
6
+ metadata.gz: a2600cc2de26566766baf34796941c5056340e3bd9ccc1e9eb30ee73b6d57aa315a2f290e641c2128e9089956415fc0f79d0a0b8dd300df02af5a73b238037e9
7
+ data.tar.gz: f7c14e935cbfae83760f4724292db502f9bed264e3c77c1d99c51d5d3ca4e6c948b46fedc8dbbbd32906d721dcaf310597160b2bc148b4bee06725f1c59119d3
data/CHANGELOG.md CHANGED
@@ -6,6 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.12] - 2024-07-16
10
+
11
+ ### Fixed
12
+
13
+ - Add MethodQualifiers to the default config [#13](https://github.com/shanecav84/rubocop-ordered_methods/pull/13). Thanks, @Darhazer.
14
+ - Fix rubocop 1.65 compatability [#16](https://github.com/shanecav84/rubocop-ordered_methods/pull/16). Thanks, @Darhazer.
15
+
16
+ ## [0.11] - 2023-12-19
17
+
18
+ ### Fixed
19
+
20
+ - Fix handling require at the top of the file, nil nodes, and nodes that aren't of type AST::node ([#15](https://github.com/shanecav84/rubocop-ordered_methods/pull/15)). Thanks @rohitpaulk and @libmartinito
21
+
9
22
  ## [0.10] - 2021-03-10
10
23
 
11
24
  ### Removed
data/config/default.yml CHANGED
@@ -4,4 +4,5 @@ Layout/OrderedMethods:
4
4
  EnforcedStyle: 'alphabetical'
5
5
  IgnoredMethods:
6
6
  - initialize
7
+ MethodQualifiers: []
7
8
  Signature: ~
@@ -17,14 +17,11 @@ module RuboCop
17
17
  @cop_config = cop_config
18
18
  end
19
19
 
20
- def correct(node, previous_node)
20
+ def correct(node, previous_node, corrector)
21
21
  AliasMethodOrderVerifier.verify!(node, previous_node)
22
22
  current_range = join_surroundings(node)
23
23
  previous_range = join_surroundings(previous_node)
24
- lambda do |corrector|
25
- corrector.replace(current_range, previous_range.source)
26
- corrector.replace(previous_range, current_range.source)
27
- end
24
+ corrector.swap(current_range, previous_range)
28
25
  end
29
26
 
30
27
  private
@@ -51,7 +48,7 @@ module RuboCop
51
48
  # @param source_range Parser::Source::Range
52
49
  # @return Parser::Source::Range
53
50
  def join_comments(node, source_range)
54
- @comment_locations[node.loc].each do |comment|
51
+ @comment_locations[node].each do |comment|
55
52
  source_range = source_range.join(comment.loc.expression)
56
53
  end
57
54
  source_range
@@ -29,9 +29,9 @@ module RuboCop
29
29
  #
30
30
  # def c; end
31
31
  # def d; end
32
- class OrderedMethods < Cop
33
- # TODO: Extending Cop is deprecated. Should extend Cop::Base.
34
- include IgnoredMethods
32
+ class OrderedMethods < Base
33
+ extend AutoCorrector
34
+ include AllowedMethods
35
35
  include RangeHelp
36
36
 
37
37
  COMPARISONS = {
@@ -48,21 +48,15 @@ module RuboCop
48
48
  node.first_argument.method_name
49
49
  end
50
50
 
51
- def autocorrect(node)
52
- _siblings, corrector = cache(node)
53
- corrector.correct(node, @previous_node)
54
- end
55
-
56
51
  def on_begin(node)
57
- siblings, _corrector = cache(node)
58
- consecutive_methods(siblings) do |previous, current|
59
- unless ordered?(previous, current)
60
- @previous_node = previous
61
- add_offense(
62
- current,
63
- message: 'Methods should be sorted in ' \
64
- "#{cop_config['EnforcedStyle']} order."
65
- )
52
+ start_node = node.children.find(&:class_type?)&.children&.last || node
53
+ consecutive_methods(start_node.children) do |previous, current|
54
+ next if ordered?(previous, current)
55
+
56
+ add_offense(current, message: message) do |corrector|
57
+ OrderedMethodsCorrector.new(
58
+ processed_source.ast_with_comments, start_node.children, cop_config
59
+ ).correct(current, previous, corrector)
66
60
  end
67
61
  end
68
62
  end
@@ -75,34 +69,6 @@ module RuboCop
75
69
  (node.send_type? && node.bare_access_modifier?)
76
70
  end
77
71
 
78
- # rubocop:disable Metrics/MethodLength
79
- # Cache to avoid traversing the AST multiple times
80
- def cache(node)
81
- @cache ||= Hash.new do |h, key|
82
- h[key.hash] = begin
83
- siblings = node.children
84
-
85
- # Init the corrector with the cache to avoid traversing the AST in
86
- # the corrector.
87
- #
88
- # We always init the @corrector, even if @options[:auto_correct] is
89
- # nil, because `add_offense` always attempts correction. This
90
- # correction attempt is how RuboCop knows if the offense can be
91
- # labeled "[Correctable]".
92
- comment_locations = ::Parser::Source::Comment.associate_locations(
93
- processed_source.ast,
94
- processed_source.comments
95
- )
96
- corrector = OrderedMethodsCorrector.new(comment_locations, siblings, cop_config)
97
-
98
- [siblings, corrector]
99
- end
100
- end
101
-
102
- @cache[node.hash]
103
- end
104
- # rubocop:enable Metrics/MethodLength
105
-
106
72
  # We disable `Style/ExplicitBlockArgument` for performance. See
107
73
  # https://github.com/shanecav84/rubocop-ordered_methods/pull/5#pullrequestreview-562957146
108
74
  # rubocop:disable Style/ExplicitBlockArgument
@@ -118,7 +84,8 @@ module RuboCop
118
84
  # rubocop:enable Style/ExplicitBlockArgument
119
85
 
120
86
  def filter_relevant_nodes(nodes)
121
- nodes.select do |node|
87
+ nodes.compact.select do |node|
88
+ next unless node.is_a?(Parser::AST::Node)
122
89
  relevant_node?(node) || (node.send_type? && qualifier_macro?(node))
123
90
  end
124
91
  end
@@ -145,6 +112,10 @@ module RuboCop
145
112
  end
146
113
  end
147
114
 
115
+ def message
116
+ "Methods should be sorted in #{cop_config['EnforcedStyle']} order."
117
+ end
118
+
148
119
  def ordered?(left_method, right_method)
149
120
  comparison = COMPARISONS[cop_config['EnforcedStyle']]
150
121
  raise Error, ERR_INVALID_COMPARISON if comparison.nil?
@@ -160,7 +131,7 @@ module RuboCop
160
131
  end
161
132
 
162
133
  def relevant_node?(node)
163
- (node.defs_type? || node.def_type?) && !ignored_method?(node.method_name)
134
+ (node.defs_type? || node.def_type?) && !allowed_method?(node.method_name)
164
135
  end
165
136
  end
166
137
  end
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'rubocop-ordered_methods'
8
- spec.version = '0.10'
8
+ spec.version = '0.12'
9
9
  spec.authors = ['Shane Cavanaugh']
10
10
  spec.email = ['shane@shanecav.net']
11
11
 
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
27
  spec.require_paths = ['lib']
28
28
 
29
- spec.add_runtime_dependency 'rubocop', '>= 1.0'
29
+ spec.add_dependency 'rubocop', '>= 1.0'
30
30
 
31
31
  spec.metadata['rubygems_mfa_required'] = 'true'
32
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-ordered_methods
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.10'
4
+ version: '0.12'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Cavanaugh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-14 00:00:00.000000000 Z
11
+ date: 2024-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  - !ruby/object:Gem::Version
73
73
  version: '0'
74
74
  requirements: []
75
- rubygems_version: 3.2.33
75
+ rubygems_version: 3.5.11
76
76
  signing_key:
77
77
  specification_version: 4
78
78
  summary: Checks that methods are ordered alphabetically.