rubocop-ordered_methods 0.4 → 0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rakeTasks +7 -0
- data/.rubocop.yml +21 -0
- data/.rubocop_todo.yml +9 -11
- data/.travis.yml +1 -2
- data/CHANGELOG.md +41 -0
- data/README.md +34 -16
- data/config/default.yml +1 -0
- data/lib/rubocop/cop/alias_method_order_verifier.rb +67 -0
- data/lib/rubocop/cop/correctors/ordered_methods_corrector.rb +91 -94
- data/lib/rubocop/cop/layout/ordered_methods.rb +28 -6
- data/lib/rubocop/cop/qualifier_node_matchers.rb +28 -0
- data/rubocop-ordered_methods.gemspec +5 -4
- metadata +25 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f2d2e3b61a0a11602f8fe67a27acf70a93494ca9cab1356f69b8acd88021f1f
|
4
|
+
data.tar.gz: bbf8c175b74c26ea08440b0283fab7a97437475757cb9ff0275e1d241777dc75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8bdb062792695cb201c3fc2311f2ab4f990e0bbc809d1c5930ced67b456661335db31268d283a7e009ede8f872ba41fc601293dd2fb14fab59b3430f80b01bbb
|
7
|
+
data.tar.gz: 50c1683cd8866ef961612a48be343896da74a5a03c269802fc1ac1e1aae2cee91d360be7bd553d694b72ce8cce9eba32fd8f126afb30e4d4d421820abc8e9794
|
data/.gitignore
CHANGED
data/.rakeTasks
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<Settings><!--This file was automatically generated by Ruby plugin.
|
3
|
+
You are allowed to:
|
4
|
+
1. Remove rake task
|
5
|
+
2. Add existing rake tasks
|
6
|
+
To add existing rake tasks automatically delete this file and reload the project.
|
7
|
+
--><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build rubocop-ordered_methods-0.5.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Build and install rubocop-ordered_methods-0.5.gem into system gems" fullCmd="install" taksId="install" /><RakeGroup description="" fullCmd="" taksId="install"><RakeTask description="Build and install rubocop-ordered_methods-0.5.gem into system gems without network access" fullCmd="install:local" taksId="local" /></RakeGroup><RakeTask description="Create tag v0.5 and build and push rubocop-ordered_methods-0.5.gem to rubygems.org" fullCmd="release[remote]" taksId="release[remote]" /><RakeTask description="Run RuboCop" fullCmd="rubocop" taksId="rubocop" /><RakeGroup description="" fullCmd="" taksId="rubocop"><RakeTask description="Auto-correct RuboCop offenses" fullCmd="rubocop:auto_correct" taksId="auto_correct" /></RakeGroup><RakeTask description="Run RSpec code examples" fullCmd="spec" taksId="spec" /><RakeTask description="" fullCmd="default" taksId="default" /><RakeTask description="" fullCmd="release" taksId="release" /><RakeGroup description="" fullCmd="" taksId="release"><RakeTask description="" fullCmd="release:guard_clean" taksId="guard_clean" /><RakeTask description="" fullCmd="release:rubygem_push" taksId="rubygem_push" /><RakeTask description="" fullCmd="release:source_control_push" taksId="source_control_push" /></RakeGroup></RakeGroup></Settings>
|
data/.rubocop.yml
CHANGED
@@ -2,10 +2,31 @@ inherit_from: .rubocop_todo.yml
|
|
2
2
|
|
3
3
|
require: rubocop-ordered_methods
|
4
4
|
|
5
|
+
AllCops:
|
6
|
+
NewCops: enable
|
7
|
+
SuggestExtensions: false
|
8
|
+
TargetRubyVersion: 2.4
|
9
|
+
|
10
|
+
# Subtle, left to author's discretion. In a long method with many guard clauses,
|
11
|
+
# a blank line may help. But, in a short method, especially with only a single
|
12
|
+
# guard clause, a blank line can be disruptive.
|
13
|
+
Layout/EmptyLineAfterGuardClause:
|
14
|
+
Enabled: false
|
15
|
+
|
5
16
|
Metrics/BlockLength:
|
6
17
|
Exclude:
|
7
18
|
- spec/**/*
|
8
19
|
|
20
|
+
Metrics/MethodLength:
|
21
|
+
Exclude:
|
22
|
+
- spec/**/*
|
23
|
+
|
9
24
|
Naming/FileName:
|
10
25
|
Exclude:
|
11
26
|
- lib/rubocop-ordered_methods.rb
|
27
|
+
|
28
|
+
# Use the semantic style. If a block has side effects use `do`, and if it is
|
29
|
+
# pure use `{}`. This style is too nuanced for a linter, so the cop is
|
30
|
+
# disabled.
|
31
|
+
Style/BlockDelimiters:
|
32
|
+
Enabled: false
|
data/.rubocop_todo.yml
CHANGED
@@ -1,26 +1,24 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on
|
3
|
+
# on 2021-01-05 20:47:55 UTC using RuboCop version 1.7.0.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
7
7
|
# versions of RuboCop, may require this file to be generated again.
|
8
8
|
|
9
9
|
# Offense count: 1
|
10
|
-
# Configuration parameters:
|
11
|
-
# Include: **/*.gemspec
|
12
|
-
Gemspec/RequiredRubyVersion:
|
13
|
-
Exclude:
|
14
|
-
- 'rubocop-ordered_methods.gemspec'
|
15
|
-
|
16
|
-
# Offense count: 1
|
17
|
-
# Configuration parameters: CountComments, ExcludedMethods.
|
10
|
+
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
|
18
11
|
Metrics/MethodLength:
|
19
12
|
Max: 11
|
20
13
|
|
21
|
-
# Offense count:
|
14
|
+
# Offense count: 1
|
15
|
+
# Cop supports --auto-correct.
|
16
|
+
Style/IfUnlessModifier:
|
17
|
+
Exclude:
|
18
|
+
- 'lib/rubocop/cop/layout/ordered_methods.rb'
|
19
|
+
|
20
|
+
# Offense count: 1
|
22
21
|
# Cop supports --auto-correct.
|
23
22
|
Style/RedundantFreeze:
|
24
23
|
Exclude:
|
25
|
-
- 'lib/rubocop/cop/correctors/ordered_methods_corrector.rb'
|
26
24
|
- 'lib/rubocop/cop/layout/ordered_methods.rb'
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,47 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [0.9] - 2021-03-10
|
10
|
+
|
11
|
+
### Added
|
12
|
+
|
13
|
+
- Autocorrection support for Sorbet signatures
|
14
|
+
|
15
|
+
## [0.8] - 2021-02-01
|
16
|
+
|
17
|
+
### Fixed
|
18
|
+
|
19
|
+
- Fix NoMethodError and the "\[Correctable\]" label ([#6](https://github.com/shanecav84/rubocop-ordered_methods/pull/6)). Thanks @jaredbeck.
|
20
|
+
|
21
|
+
## [0.7] - 2021-01-11
|
22
|
+
|
23
|
+
### Removed
|
24
|
+
|
25
|
+
- Drop Ruby 2.3 support
|
26
|
+
- Drop support for rubocop < 1.0
|
27
|
+
|
28
|
+
### Changed
|
29
|
+
|
30
|
+
- Support for rubocop >= 1.0 ([#5](https://github.com/shanecav84/rubocop-ordered_methods/pull/5)). Thanks @jaredbeck.
|
31
|
+
|
32
|
+
## [0.6] - 2020-03-01
|
33
|
+
|
34
|
+
### Security
|
35
|
+
|
36
|
+
- Upgrade rake to avoid vulnerability https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-8130
|
37
|
+
- rake is a development dependency for this gem, so shouldn't have been a risk for production
|
38
|
+
|
39
|
+
## [0.5] - 2019-11-05
|
40
|
+
|
41
|
+
### Removed
|
42
|
+
|
43
|
+
- Drop Ruby 2.2 support
|
44
|
+
|
45
|
+
### Changed
|
46
|
+
|
47
|
+
- Nonadjacent qualifiers are now autocorrected (#4). Thanks @adamkiczula.
|
48
|
+
- Cache AST traversals for significant speed up on large files
|
49
|
+
|
9
50
|
## [0.4] - 2019-06-11
|
10
51
|
|
11
52
|
### Changed
|
data/README.md
CHANGED
@@ -3,33 +3,33 @@
|
|
3
3
|
|
4
4
|
# RuboCop OrderedMethods
|
5
5
|
|
6
|
-
Check that methods are defined alphabetically
|
7
|
-
autocorrection.
|
6
|
+
Check that methods are defined alphabetically per access modifier block (class,
|
7
|
+
public, private, protected). Includes [autocorrection](#corrector).
|
8
8
|
|
9
9
|
```ruby
|
10
10
|
# bad
|
11
|
-
def self.
|
12
|
-
def self.
|
11
|
+
def self.b_class; end
|
12
|
+
def self.a_class; end
|
13
13
|
|
14
|
-
def
|
15
|
-
def
|
14
|
+
def b_public; end
|
15
|
+
def a_public; end
|
16
16
|
|
17
17
|
private
|
18
18
|
|
19
|
-
def
|
20
|
-
def
|
19
|
+
def b_private; end
|
20
|
+
def a_private; end
|
21
21
|
|
22
22
|
# good
|
23
|
-
def self.
|
24
|
-
def self.
|
23
|
+
def self.a_class; end
|
24
|
+
def self.b_class; end
|
25
25
|
|
26
|
-
def
|
27
|
-
def
|
26
|
+
def a_public; end
|
27
|
+
def b_public; end
|
28
28
|
|
29
29
|
private
|
30
30
|
|
31
|
-
def
|
32
|
-
def
|
31
|
+
def a_private; end
|
32
|
+
def b_private; end
|
33
33
|
```
|
34
34
|
|
35
35
|
## Installation
|
@@ -74,8 +74,19 @@ rubocop --require rubocop-ordered_methods
|
|
74
74
|
|
75
75
|
Name | Default value | Configurable values
|
76
76
|
--- | --- | ---
|
77
|
-
EnforcedStyle | `alphabetical` | `alphabetical`
|
78
|
-
IgnoredMethods | `initialize` | Array
|
77
|
+
EnforcedStyle | `'alphabetical'` | `'alphabetical'`
|
78
|
+
IgnoredMethods | `['initialize']` | Array
|
79
|
+
Signature | `nil` | `'sorbet'`, `nil`
|
80
|
+
|
81
|
+
#### Example
|
82
|
+
|
83
|
+
```
|
84
|
+
# .rubocop.yml
|
85
|
+
Layout/OrderedMethods:
|
86
|
+
EnforcedStyle: alphabetical
|
87
|
+
IgnoredMethods: initialize
|
88
|
+
Signature: sorbet
|
89
|
+
```
|
79
90
|
|
80
91
|
### Corrector
|
81
92
|
|
@@ -125,6 +136,13 @@ protected :instance_a
|
|
125
136
|
public :instance_a
|
126
137
|
```
|
127
138
|
|
139
|
+
#### Method signatures
|
140
|
+
|
141
|
+
Support for (Sorbet) method signatures was added to the corrector by
|
142
|
+
[#7](https://github.com/shanecav84/rubocop-ordered_methods/pull/7).
|
143
|
+
It is off by default due to performance concerns (not yet benchmarked). Enable
|
144
|
+
with `Signature: sorbet`.
|
145
|
+
|
128
146
|
#### Caveats
|
129
147
|
|
130
148
|
* The corrector will warn and refuse to order a method if it were to be
|
data/config/default.yml
CHANGED
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'qualifier_node_matchers'
|
4
|
+
|
5
|
+
module RuboCop
|
6
|
+
module Cop
|
7
|
+
# This verifies a method is defined before its alias
|
8
|
+
class AliasMethodOrderVerifier
|
9
|
+
class << self
|
10
|
+
include IgnoredNode
|
11
|
+
include QualifierNodeMatchers
|
12
|
+
|
13
|
+
# Disable cop for freezing on Ruby 2.2
|
14
|
+
# rubocop:disable Style/RedundantFreeze
|
15
|
+
ALIAS_BEFORE_METHOD_WARNING_FMT = "Won't reorder " \
|
16
|
+
'%<first_method_name>s and %<second_method_name>s because ' \
|
17
|
+
'alias for %<first_method_name>s would be declared before ' \
|
18
|
+
'its method definition.'.freeze
|
19
|
+
# rubocop:enable Style/RedundantFreeze
|
20
|
+
|
21
|
+
# rubocop:disable Style/GuardClause
|
22
|
+
def verify!(current_node, previous_node)
|
23
|
+
if moving_after_alias?(current_node, previous_node)
|
24
|
+
ignore_node(current_node)
|
25
|
+
raise_warning!(current_node.method_name, previous_node.method_name)
|
26
|
+
end
|
27
|
+
if moving_after_alias?(previous_node, current_node)
|
28
|
+
ignore_node(previous_node)
|
29
|
+
raise_warning!(previous_node.method_name, current_node.method_name)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
# rubocop:enable Style/GuardClause
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def find_aliases(current_node, siblings)
|
37
|
+
siblings.select do |sibling|
|
38
|
+
(alias?(sibling) || alias_method?(sibling)) ==
|
39
|
+
current_node.method_name
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# We don't want a method to be defined after its alias
|
44
|
+
def moving_after_alias?(current_node, previous_node)
|
45
|
+
siblings = current_node.parent.children
|
46
|
+
current_node_aliases = find_aliases(current_node, siblings)
|
47
|
+
filter = current_node_aliases.delete_if do |cna|
|
48
|
+
cna.sibling_index > current_node.sibling_index
|
49
|
+
end
|
50
|
+
return false if filter.empty?
|
51
|
+
|
52
|
+
current_node_aliases.any? do |cna|
|
53
|
+
previous_node.sibling_index > cna.sibling_index
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def raise_warning!(first_method_name, second_method_name)
|
58
|
+
raise Warning, format(
|
59
|
+
ALIAS_BEFORE_METHOD_WARNING_FMT,
|
60
|
+
first_method_name: first_method_name,
|
61
|
+
second_method_name: second_method_name
|
62
|
+
)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -1,120 +1,117 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative '../layout/ordered_methods'
|
4
|
+
require_relative '../alias_method_order_verifier'
|
5
|
+
require_relative '../qualifier_node_matchers'
|
4
6
|
|
5
7
|
module RuboCop
|
6
8
|
module Cop
|
7
9
|
# This auto-corrects method order
|
8
10
|
class OrderedMethodsCorrector
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
].freeze
|
26
|
-
|
27
|
-
def_node_matcher :alias?, '(:alias ... (sym $_method_name))'
|
28
|
-
def_node_matcher :qualifier?, <<-PATTERN
|
29
|
-
(send nil? {#{QUALIFIERS.map(&:inspect).join(' ')}}
|
30
|
-
... (sym $_method_name))
|
31
|
-
PATTERN
|
32
|
-
|
33
|
-
def correct(processed_source, node, previous_node)
|
34
|
-
@processed_source = processed_source
|
35
|
-
@current_node = node
|
36
|
-
@previous_node = previous_node
|
37
|
-
|
38
|
-
verify_alias_method_order
|
39
|
-
current_range = join_surroundings(@current_node)
|
40
|
-
previous_range = join_surroundings(@previous_node)
|
41
|
-
lambda do |corrector|
|
42
|
-
corrector.replace(current_range, previous_range.source)
|
43
|
-
corrector.replace(previous_range, current_range.source)
|
44
|
-
end
|
11
|
+
include QualifierNodeMatchers
|
12
|
+
|
13
|
+
# @param cop_config ::RuboCop::Config
|
14
|
+
def initialize(comment_locations, siblings, cop_config)
|
15
|
+
@comment_locations = comment_locations
|
16
|
+
@siblings = siblings
|
17
|
+
@cop_config = cop_config
|
18
|
+
end
|
19
|
+
|
20
|
+
def correct(node, previous_node)
|
21
|
+
AliasMethodOrderVerifier.verify!(node, previous_node)
|
22
|
+
current_range = join_surroundings(node)
|
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)
|
45
27
|
end
|
28
|
+
end
|
46
29
|
|
47
|
-
|
30
|
+
private
|
48
31
|
|
49
|
-
|
50
|
-
|
32
|
+
def find_last_qualifier_index(node)
|
33
|
+
preceding_qualifier_index = node.sibling_index
|
34
|
+
last_qualifier_index = @siblings.length - 1
|
35
|
+
while preceding_qualifier_index < last_qualifier_index
|
36
|
+
break if found_qualifier?(node, @siblings[last_qualifier_index])
|
51
37
|
|
52
|
-
|
38
|
+
last_qualifier_index -= 1
|
53
39
|
end
|
54
40
|
|
55
|
-
|
56
|
-
|
57
|
-
source_range = source_range.join(comment.loc.expression)
|
58
|
-
end
|
59
|
-
source_range
|
60
|
-
end
|
41
|
+
last_qualifier_index
|
42
|
+
end
|
61
43
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
44
|
+
def found_qualifier?(node, next_sibling)
|
45
|
+
return false if next_sibling.nil?
|
46
|
+
|
47
|
+
(qualifier?(next_sibling) || alias?(next_sibling)) == node.method_name
|
48
|
+
end
|
49
|
+
|
50
|
+
# @param node RuboCop::AST::DefNode
|
51
|
+
# @param source_range Parser::Source::Range
|
52
|
+
# @return Parser::Source::Range
|
53
|
+
def join_comments(node, source_range)
|
54
|
+
@comment_locations[node.loc].each do |comment|
|
55
|
+
source_range = source_range.join(comment.loc.expression)
|
72
56
|
end
|
57
|
+
source_range
|
58
|
+
end
|
73
59
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
60
|
+
# @param node RuboCop::AST::DefNode
|
61
|
+
# @param source_range Parser::Source::Range
|
62
|
+
# @return Parser::Source::Range
|
63
|
+
def join_modifiers_and_aliases(node, source_range)
|
64
|
+
preceding_qualifier_index = node.sibling_index
|
65
|
+
last_qualifier_index = find_last_qualifier_index(node)
|
66
|
+
while preceding_qualifier_index < last_qualifier_index
|
67
|
+
source_range = source_range.join(
|
68
|
+
@siblings[preceding_qualifier_index + 1].source_range
|
78
69
|
)
|
79
|
-
|
70
|
+
preceding_qualifier_index += 1
|
80
71
|
end
|
72
|
+
source_range
|
73
|
+
end
|
81
74
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
previous_node.sibling_index > cna.sibling_index
|
95
|
-
end
|
75
|
+
# @param node RuboCop::AST::DefNode
|
76
|
+
# @param source_range Parser::Source::Range
|
77
|
+
# @return Parser::Source::Range
|
78
|
+
def join_signature(node, source_range)
|
79
|
+
sib = node.left_sibling
|
80
|
+
if signature?(sib)
|
81
|
+
# If there is a comment directly above the sig, first calculate the
|
82
|
+
# range that covers both.
|
83
|
+
with_comment = join_comments(sib, sib.source_range)
|
84
|
+
source_range.join(with_comment)
|
85
|
+
else
|
86
|
+
source_range
|
96
87
|
end
|
88
|
+
end
|
97
89
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
)
|
115
|
-
end
|
90
|
+
def join_signature?
|
91
|
+
@cop_config['Signature'] == 'sorbet'
|
92
|
+
end
|
93
|
+
|
94
|
+
# @param node RuboCop::AST::DefNode
|
95
|
+
# @return Parser::Source::Range
|
96
|
+
def join_surroundings(node)
|
97
|
+
with_modifiers_and_aliases = join_modifiers_and_aliases(
|
98
|
+
node,
|
99
|
+
node.source_range
|
100
|
+
)
|
101
|
+
with_comments = join_comments(node, with_modifiers_and_aliases)
|
102
|
+
if join_signature?
|
103
|
+
join_signature(node, with_comments)
|
104
|
+
else
|
105
|
+
with_comments
|
116
106
|
end
|
117
|
-
|
107
|
+
end
|
108
|
+
|
109
|
+
# https://sorbet.org/docs/sigs
|
110
|
+
# @param node RuboCop::AST::Node
|
111
|
+
def signature?(node)
|
112
|
+
return false unless node&.type == :block
|
113
|
+
child = node.children.first
|
114
|
+
child&.type == :send && child.method_name == :sig
|
118
115
|
end
|
119
116
|
end
|
120
117
|
end
|
@@ -30,6 +30,7 @@ module RuboCop
|
|
30
30
|
# def c; end
|
31
31
|
# def d; end
|
32
32
|
class OrderedMethods < Cop
|
33
|
+
# TODO: Extending Cop is deprecated. Should extend Cop::Base.
|
33
34
|
include IgnoredMethods
|
34
35
|
include RangeHelp
|
35
36
|
|
@@ -42,15 +43,12 @@ module RuboCop
|
|
42
43
|
"#{cop_name}. Expected one of: #{COMPARISONS.keys.join(', ')}".freeze
|
43
44
|
|
44
45
|
def autocorrect(node)
|
45
|
-
|
46
|
-
processed_source,
|
47
|
-
node,
|
48
|
-
@previous_node
|
49
|
-
)
|
46
|
+
@corrector.correct(node, @previous_node)
|
50
47
|
end
|
51
48
|
|
52
49
|
def on_begin(node)
|
53
|
-
|
50
|
+
cache(node)
|
51
|
+
consecutive_methods(@siblings) do |previous, current|
|
54
52
|
unless ordered?(previous, current)
|
55
53
|
@previous_node = previous
|
56
54
|
add_offense(
|
@@ -70,6 +68,29 @@ module RuboCop
|
|
70
68
|
(node.send_type? && node.bare_access_modifier?)
|
71
69
|
end
|
72
70
|
|
71
|
+
# Cache to avoid traversing the AST multiple times
|
72
|
+
def cache(node)
|
73
|
+
@cache ||= begin
|
74
|
+
@siblings = node.children
|
75
|
+
|
76
|
+
# Init the corrector with the cache to avoid traversing the AST in
|
77
|
+
# the corrector.
|
78
|
+
#
|
79
|
+
# We always init the @corrector, even if @options[:auto_correct] is
|
80
|
+
# nil, because `add_offense` always attempts correction. This
|
81
|
+
# correction attempt is how RuboCop knows if the offense can be
|
82
|
+
# labeled "[Correctable]".
|
83
|
+
comment_locations = ::Parser::Source::Comment.associate_locations(
|
84
|
+
processed_source.ast,
|
85
|
+
processed_source.comments
|
86
|
+
)
|
87
|
+
@corrector = OrderedMethodsCorrector.new(comment_locations, @siblings, cop_config)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# We disable `Style/ExplicitBlockArgument` for performance. See
|
92
|
+
# https://github.com/shanecav84/rubocop-ordered_methods/pull/5#pullrequestreview-562957146
|
93
|
+
# rubocop:disable Style/ExplicitBlockArgument
|
73
94
|
def consecutive_methods(nodes)
|
74
95
|
filtered = filter_relevant_nodes(nodes)
|
75
96
|
filtered_and_grouped = group_methods_by_access_modifier(filtered)
|
@@ -79,6 +100,7 @@ module RuboCop
|
|
79
100
|
end
|
80
101
|
end
|
81
102
|
end
|
103
|
+
# rubocop:enable Style/ExplicitBlockArgument
|
82
104
|
|
83
105
|
def filter_relevant_nodes(nodes)
|
84
106
|
nodes.select do |node|
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# defines matchers for qualifier nodes
|
6
|
+
module QualifierNodeMatchers
|
7
|
+
extend NodePattern::Macros
|
8
|
+
|
9
|
+
QUALIFIERS = %i[
|
10
|
+
alias_method
|
11
|
+
module_function
|
12
|
+
private_class_method
|
13
|
+
public_class_method
|
14
|
+
private
|
15
|
+
protected
|
16
|
+
public
|
17
|
+
].freeze
|
18
|
+
|
19
|
+
def_node_matcher :alias?, '(:alias ... (sym $_method_name))'
|
20
|
+
def_node_matcher :alias_method?,
|
21
|
+
'(send nil? {:alias_method} ... (sym $_method_name))'
|
22
|
+
def_node_matcher :qualifier?, <<-PATTERN
|
23
|
+
(send nil? {#{QUALIFIERS.map(&:inspect).join(' ')}}
|
24
|
+
... (sym $_method_name))
|
25
|
+
PATTERN
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -5,14 +5,14 @@ $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.
|
8
|
+
spec.version = '0.9'
|
9
9
|
spec.authors = ['Shane Cavanaugh']
|
10
10
|
spec.email = ['shane@shanecav.net']
|
11
11
|
|
12
12
|
spec.summary = 'Checks that methods are ordered alphabetically.'
|
13
13
|
spec.homepage = 'https://github.com/shanecav84/rubocop-ordered_methods'
|
14
14
|
spec.license = 'MIT'
|
15
|
-
spec.required_ruby_version = '>= 2.
|
15
|
+
spec.required_ruby_version = '>= 2.4'
|
16
16
|
|
17
17
|
# Specify which files should be added to the gem when it is released.
|
18
18
|
# The `git ls-files -z` loads the files in the RubyGem that have been added
|
@@ -26,9 +26,10 @@ 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', '>= 0
|
29
|
+
spec.add_runtime_dependency 'rubocop', '>= 1.0'
|
30
30
|
|
31
31
|
spec.add_development_dependency 'bundler'
|
32
|
-
spec.add_development_dependency '
|
32
|
+
spec.add_development_dependency 'byebug'
|
33
|
+
spec.add_development_dependency 'rake', '~> 12.3.3'
|
33
34
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
34
35
|
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.
|
4
|
+
version: '0.9'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shane Cavanaugh
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0
|
19
|
+
version: '1.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0
|
26
|
+
version: '1.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,20 +38,34 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rake
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
59
|
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
61
|
+
version: 12.3.3
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
68
|
+
version: 12.3.3
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rspec
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -74,6 +88,7 @@ extensions: []
|
|
74
88
|
extra_rdoc_files: []
|
75
89
|
files:
|
76
90
|
- ".gitignore"
|
91
|
+
- ".rakeTasks"
|
77
92
|
- ".rubocop.yml"
|
78
93
|
- ".rubocop_todo.yml"
|
79
94
|
- ".travis.yml"
|
@@ -86,8 +101,10 @@ files:
|
|
86
101
|
- bin/console
|
87
102
|
- config/default.yml
|
88
103
|
- lib/rubocop-ordered_methods.rb
|
104
|
+
- lib/rubocop/cop/alias_method_order_verifier.rb
|
89
105
|
- lib/rubocop/cop/correctors/ordered_methods_corrector.rb
|
90
106
|
- lib/rubocop/cop/layout/ordered_methods.rb
|
107
|
+
- lib/rubocop/cop/qualifier_node_matchers.rb
|
91
108
|
- lib/rubocop/ordered_methods.rb
|
92
109
|
- rubocop-ordered_methods.gemspec
|
93
110
|
homepage: https://github.com/shanecav84/rubocop-ordered_methods
|
@@ -102,14 +119,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
102
119
|
requirements:
|
103
120
|
- - ">="
|
104
121
|
- !ruby/object:Gem::Version
|
105
|
-
version: 2.
|
122
|
+
version: '2.4'
|
106
123
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
124
|
requirements:
|
108
125
|
- - ">="
|
109
126
|
- !ruby/object:Gem::Version
|
110
127
|
version: '0'
|
111
128
|
requirements: []
|
112
|
-
rubygems_version: 3.
|
129
|
+
rubygems_version: 3.1.4
|
113
130
|
signing_key:
|
114
131
|
specification_version: 4
|
115
132
|
summary: Checks that methods are ordered alphabetically.
|