rubocop-ordered_methods 0.9 → 0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +46 -0
- data/.rubocop.yml +4 -1
- data/CHANGELOG.md +11 -0
- data/Gemfile +5 -0
- data/README.md +15 -1
- data/lib/rubocop/cop/layout/ordered_methods.rb +47 -24
- data/lib/rubocop/cop/qualifier_node_matchers.rb +9 -2
- data/rubocop-ordered_methods.gemspec +3 -6
- metadata +11 -65
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c43226dde578a4844ba7c579a7fa5dfb52f05dce2e84d9a68f1e250e272d4e58
|
4
|
+
data.tar.gz: 636542d7c4be5982d3424703ba2062c72c4fdbfcf4f289a5b593050b68c16969
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1eced8a7bb0e4a33bf58cae6b0b0d90fd9814318635689450ee34646905a5a41fbd97189c51d9763706f307d4ed4c3032e3eb02af2aa5ce79c373f12bd5f1019
|
7
|
+
data.tar.gz: 4c528b7368611c20b2ab790f28502f3c44e4e6546bcbff35b199c749932583bdea36cc5d441e598efb7b7e199b63fbac97f50a6534b206052f60474ebe24147a
|
@@ -0,0 +1,46 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
push:
|
6
|
+
branches:
|
7
|
+
- master
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
rspec:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
strategy:
|
13
|
+
matrix:
|
14
|
+
ruby:
|
15
|
+
- "2.7"
|
16
|
+
- "3.0"
|
17
|
+
- "3.1"
|
18
|
+
- "3.2"
|
19
|
+
|
20
|
+
name: "Ruby ${{ matrix.ruby }}: run rspec"
|
21
|
+
steps:
|
22
|
+
- uses: actions/checkout@v3
|
23
|
+
- uses: ruby/setup-ruby@v1
|
24
|
+
with:
|
25
|
+
ruby-version: "${{ matrix.ruby }}"
|
26
|
+
bundler-cache: true
|
27
|
+
- run: bundle exec rspec
|
28
|
+
|
29
|
+
rubocop:
|
30
|
+
runs-on: ubuntu-latest
|
31
|
+
strategy:
|
32
|
+
matrix:
|
33
|
+
ruby:
|
34
|
+
- "2.7"
|
35
|
+
- "3.0"
|
36
|
+
- "3.1"
|
37
|
+
- "3.2"
|
38
|
+
|
39
|
+
name: "Ruby ${{ matrix.ruby }}: run rubocop"
|
40
|
+
steps:
|
41
|
+
- uses: actions/checkout@v3
|
42
|
+
- uses: ruby/setup-ruby@v1
|
43
|
+
with:
|
44
|
+
ruby-version: "${{ matrix.ruby }}"
|
45
|
+
bundler-cache: true
|
46
|
+
- run: bundle exec rubocop
|
data/.rubocop.yml
CHANGED
@@ -5,7 +5,7 @@ require: rubocop-ordered_methods
|
|
5
5
|
AllCops:
|
6
6
|
NewCops: enable
|
7
7
|
SuggestExtensions: false
|
8
|
-
TargetRubyVersion: 2.
|
8
|
+
TargetRubyVersion: 2.7
|
9
9
|
|
10
10
|
# Subtle, left to author's discretion. In a long method with many guard clauses,
|
11
11
|
# a blank line may help. But, in a short method, especially with only a single
|
@@ -13,6 +13,9 @@ AllCops:
|
|
13
13
|
Layout/EmptyLineAfterGuardClause:
|
14
14
|
Enabled: false
|
15
15
|
|
16
|
+
Layout/LineEndStringConcatenationIndentation:
|
17
|
+
EnforcedStyle: indented
|
18
|
+
|
16
19
|
Metrics/BlockLength:
|
17
20
|
Exclude:
|
18
21
|
- spec/**/*
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [0.10] - 2021-03-10
|
10
|
+
|
11
|
+
### Removed
|
12
|
+
|
13
|
+
- Drop support for Ruby 2.4, 2.5, and 2.6
|
14
|
+
|
15
|
+
### Added
|
16
|
+
|
17
|
+
- Support for custom method qualifiers ([#11](https://github.com/shanecav84/rubocop-ordered_methods/pull/11)). Thanks @Darhazer.
|
18
|
+
- Setup CI ([#12](https://github.com/shanecav84/rubocop-ordered_methods/pull/12)). Thanks @Darhazer.
|
19
|
+
|
9
20
|
## [0.9] - 2021-03-10
|
10
21
|
|
11
22
|
### Added
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -76,6 +76,7 @@ Name | Default value | Configurable values
|
|
76
76
|
--- | --- | ---
|
77
77
|
EnforcedStyle | `'alphabetical'` | `'alphabetical'`
|
78
78
|
IgnoredMethods | `['initialize']` | Array
|
79
|
+
MethodQualifiers | `[]` | Array
|
79
80
|
Signature | `nil` | `'sorbet'`, `nil`
|
80
81
|
|
81
82
|
#### Example
|
@@ -84,7 +85,10 @@ Signature | `nil` | `'sorbet'`, `nil`
|
|
84
85
|
# .rubocop.yml
|
85
86
|
Layout/OrderedMethods:
|
86
87
|
EnforcedStyle: alphabetical
|
87
|
-
IgnoredMethods:
|
88
|
+
IgnoredMethods:
|
89
|
+
- initialize
|
90
|
+
MethodQualifiers:
|
91
|
+
- memoize
|
88
92
|
Signature: sorbet
|
89
93
|
```
|
90
94
|
|
@@ -136,6 +140,16 @@ protected :instance_a
|
|
136
140
|
public :instance_a
|
137
141
|
```
|
138
142
|
|
143
|
+
#### Method qualifiers
|
144
|
+
Some gems (like `memery`, `memoist`, etc.) provide a DSL that modifies the method (e.g. for memoization).
|
145
|
+
Those DSL methods can be added to the `MethodQualifiers` configuration, and they will be respected.
|
146
|
+
|
147
|
+
E.g. the following source can be correctly ordered:
|
148
|
+
```ruby
|
149
|
+
def b; end;
|
150
|
+
memoize def a;end
|
151
|
+
```
|
152
|
+
|
139
153
|
#### Method signatures
|
140
154
|
|
141
155
|
Support for (Sorbet) method signatures was added to the corrector by
|
@@ -35,20 +35,27 @@ module RuboCop
|
|
35
35
|
include RangeHelp
|
36
36
|
|
37
37
|
COMPARISONS = {
|
38
|
-
'alphabetical' => lambda do |
|
39
|
-
(
|
38
|
+
'alphabetical' => lambda do |left_node, right_node|
|
39
|
+
(method_name(left_node) <=> method_name(right_node)) != 1
|
40
40
|
end
|
41
41
|
}.freeze
|
42
42
|
ERR_INVALID_COMPARISON = 'Invalid "Comparison" config for ' \
|
43
43
|
"#{cop_name}. Expected one of: #{COMPARISONS.keys.join(', ')}".freeze
|
44
44
|
|
45
|
+
def self.method_name(node)
|
46
|
+
return node.method_name unless node.send_type?
|
47
|
+
|
48
|
+
node.first_argument.method_name
|
49
|
+
end
|
50
|
+
|
45
51
|
def autocorrect(node)
|
46
|
-
|
52
|
+
_siblings, corrector = cache(node)
|
53
|
+
corrector.correct(node, @previous_node)
|
47
54
|
end
|
48
55
|
|
49
56
|
def on_begin(node)
|
50
|
-
cache(node)
|
51
|
-
consecutive_methods(
|
57
|
+
siblings, _corrector = cache(node)
|
58
|
+
consecutive_methods(siblings) do |previous, current|
|
52
59
|
unless ordered?(previous, current)
|
53
60
|
@previous_node = previous
|
54
61
|
add_offense(
|
@@ -68,25 +75,33 @@ module RuboCop
|
|
68
75
|
(node.send_type? && node.bare_access_modifier?)
|
69
76
|
end
|
70
77
|
|
78
|
+
# rubocop:disable Metrics/MethodLength
|
71
79
|
# Cache to avoid traversing the AST multiple times
|
72
80
|
def cache(node)
|
73
|
-
@cache ||=
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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
|
88
100
|
end
|
101
|
+
|
102
|
+
@cache[node.hash]
|
89
103
|
end
|
104
|
+
# rubocop:enable Metrics/MethodLength
|
90
105
|
|
91
106
|
# We disable `Style/ExplicitBlockArgument` for performance. See
|
92
107
|
# https://github.com/shanecav84/rubocop-ordered_methods/pull/5#pullrequestreview-562957146
|
@@ -104,10 +119,7 @@ module RuboCop
|
|
104
119
|
|
105
120
|
def filter_relevant_nodes(nodes)
|
106
121
|
nodes.select do |node|
|
107
|
-
(
|
108
|
-
(node.defs_type? || node.def_type?) &&
|
109
|
-
!ignored_method?(node.method_name)
|
110
|
-
) || (node.send_type? && node.bare_access_modifier?)
|
122
|
+
relevant_node?(node) || (node.send_type? && qualifier_macro?(node))
|
111
123
|
end
|
112
124
|
end
|
113
125
|
|
@@ -139,6 +151,17 @@ module RuboCop
|
|
139
151
|
|
140
152
|
comparison.call(left_method, right_method)
|
141
153
|
end
|
154
|
+
|
155
|
+
def qualifier_macro?(node)
|
156
|
+
return true if node.bare_access_modifier?
|
157
|
+
|
158
|
+
cop_config['MethodQualifiers'].to_a.include?(node.method_name.to_s) &&
|
159
|
+
relevant_node?(node.first_argument)
|
160
|
+
end
|
161
|
+
|
162
|
+
def relevant_node?(node)
|
163
|
+
(node.defs_type? || node.def_type?) && !ignored_method?(node.method_name)
|
164
|
+
end
|
142
165
|
end
|
143
166
|
end
|
144
167
|
end
|
@@ -20,9 +20,16 @@ module RuboCop
|
|
20
20
|
def_node_matcher :alias_method?,
|
21
21
|
'(send nil? {:alias_method} ... (sym $_method_name))'
|
22
22
|
def_node_matcher :qualifier?, <<-PATTERN
|
23
|
-
(send nil?
|
24
|
-
... (sym $_method_name))
|
23
|
+
(send nil? #method_qualifier? ... (sym $_method_name))
|
25
24
|
PATTERN
|
25
|
+
|
26
|
+
def method_qualifier?(name)
|
27
|
+
qualifiers.include?(name)
|
28
|
+
end
|
29
|
+
|
30
|
+
def qualifiers
|
31
|
+
@qualifiers ||= QUALIFIERS + @cop_config['MethodQualifiers'].to_a.map(&:to_sym)
|
32
|
+
end
|
26
33
|
end
|
27
34
|
end
|
28
35
|
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.10'
|
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.7'
|
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
|
@@ -28,8 +28,5 @@ Gem::Specification.new do |spec|
|
|
28
28
|
|
29
29
|
spec.add_runtime_dependency 'rubocop', '>= 1.0'
|
30
30
|
|
31
|
-
spec.
|
32
|
-
spec.add_development_dependency 'byebug'
|
33
|
-
spec.add_development_dependency 'rake', '~> 12.3.3'
|
34
|
-
spec.add_development_dependency 'rspec', '~> 3.0'
|
31
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
35
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.
|
4
|
+
version: '0.10'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shane Cavanaugh
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -24,69 +24,14 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.0'
|
27
|
-
|
28
|
-
name: bundler
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
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'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rake
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 12.3.3
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: 12.3.3
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rspec
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '3.0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - "~>"
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '3.0'
|
83
|
-
description:
|
27
|
+
description:
|
84
28
|
email:
|
85
29
|
- shane@shanecav.net
|
86
30
|
executables: []
|
87
31
|
extensions: []
|
88
32
|
extra_rdoc_files: []
|
89
33
|
files:
|
34
|
+
- ".github/workflows/main.yml"
|
90
35
|
- ".gitignore"
|
91
36
|
- ".rakeTasks"
|
92
37
|
- ".rubocop.yml"
|
@@ -110,8 +55,9 @@ files:
|
|
110
55
|
homepage: https://github.com/shanecav84/rubocop-ordered_methods
|
111
56
|
licenses:
|
112
57
|
- MIT
|
113
|
-
metadata:
|
114
|
-
|
58
|
+
metadata:
|
59
|
+
rubygems_mfa_required: 'true'
|
60
|
+
post_install_message:
|
115
61
|
rdoc_options: []
|
116
62
|
require_paths:
|
117
63
|
- lib
|
@@ -119,15 +65,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
119
65
|
requirements:
|
120
66
|
- - ">="
|
121
67
|
- !ruby/object:Gem::Version
|
122
|
-
version: '2.
|
68
|
+
version: '2.7'
|
123
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
70
|
requirements:
|
125
71
|
- - ">="
|
126
72
|
- !ruby/object:Gem::Version
|
127
73
|
version: '0'
|
128
74
|
requirements: []
|
129
|
-
rubygems_version: 3.
|
130
|
-
signing_key:
|
75
|
+
rubygems_version: 3.2.33
|
76
|
+
signing_key:
|
131
77
|
specification_version: 4
|
132
78
|
summary: Checks that methods are ordered alphabetically.
|
133
79
|
test_files: []
|