rubocop-ordered_methods 0.9 → 0.11

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: 2f2d2e3b61a0a11602f8fe67a27acf70a93494ca9cab1356f69b8acd88021f1f
4
- data.tar.gz: bbf8c175b74c26ea08440b0283fab7a97437475757cb9ff0275e1d241777dc75
3
+ metadata.gz: 8136e162c37c297ad3e2eee09364841551f447b6854ada050b165d0286fda95d
4
+ data.tar.gz: 595fd69b9114f9122920cf5c8f15a1cb6a43b22f4c31ab9665a11bf2411ff91d
5
5
  SHA512:
6
- metadata.gz: 8bdb062792695cb201c3fc2311f2ab4f990e0bbc809d1c5930ced67b456661335db31268d283a7e009ede8f872ba41fc601293dd2fb14fab59b3430f80b01bbb
7
- data.tar.gz: 50c1683cd8866ef961612a48be343896da74a5a03c269802fc1ac1e1aae2cee91d360be7bd553d694b72ce8cce9eba32fd8f126afb30e4d4d421820abc8e9794
6
+ metadata.gz: ab8b3c4ae74ac44dc4ee43af04cdb3854f8df847113e499dc2d429843c4276a34084316edc3c0aca41fd4174fdd6c3a508a642ec8dbc19302e6c015bc65ee16e
7
+ data.tar.gz: 96347af9d20819c73c485e5a985702097ab5507ec86774ab144bf8f9fa54363461dc0a53f993429a6321e0e8fea6d0b4bcf244c9159b17a03fcf7f503b41ec75
@@ -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.4
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,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.11] - 2023-12-19
10
+
11
+ ### Fixed
12
+
13
+ - 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
14
+
15
+ ## [0.10] - 2021-03-10
16
+
17
+ ### Removed
18
+
19
+ - Drop support for Ruby 2.4, 2.5, and 2.6
20
+
21
+ ### Added
22
+
23
+ - Support for custom method qualifiers ([#11](https://github.com/shanecav84/rubocop-ordered_methods/pull/11)). Thanks @Darhazer.
24
+ - Setup CI ([#12](https://github.com/shanecav84/rubocop-ordered_methods/pull/12)). Thanks @Darhazer.
25
+
9
26
  ## [0.9] - 2021-03-10
10
27
 
11
28
  ### Added
data/Gemfile CHANGED
@@ -2,4 +2,9 @@
2
2
 
3
3
  source 'https://rubygems.org'
4
4
 
5
+ gem 'bundler'
6
+ gem 'byebug'
7
+ gem 'rake', '~> 12.3.3'
8
+ gem 'rspec', '~> 3.0'
9
+
5
10
  gemspec
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: initialize
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,26 +35,34 @@ module RuboCop
35
35
  include RangeHelp
36
36
 
37
37
  COMPARISONS = {
38
- 'alphabetical' => lambda do |left_method, right_method|
39
- (left_method.method_name <=> right_method.method_name) != 1
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
- @corrector.correct(node, @previous_node)
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(@siblings) do |previous, current|
57
+ start_node = node.children.find(&:class_type?)&.children&.last || node
58
+ siblings, _corrector = cache(start_node)
59
+
60
+ consecutive_methods(siblings) do |previous, current|
52
61
  unless ordered?(previous, current)
53
62
  @previous_node = previous
54
63
  add_offense(
55
64
  current,
56
- message: 'Methods should be sorted in ' \
57
- "#{cop_config['EnforcedStyle']} order."
65
+ message: "Methods should be sorted in #{cop_config['EnforcedStyle']} order."
58
66
  )
59
67
  end
60
68
  end
@@ -68,25 +76,33 @@ module RuboCop
68
76
  (node.send_type? && node.bare_access_modifier?)
69
77
  end
70
78
 
79
+ # rubocop:disable Metrics/MethodLength
71
80
  # Cache to avoid traversing the AST multiple times
72
81
  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)
82
+ @cache ||= Hash.new do |h, key|
83
+ h[key.hash] = begin
84
+ siblings = node.children
85
+
86
+ # Init the corrector with the cache to avoid traversing the AST in
87
+ # the corrector.
88
+ #
89
+ # We always init the @corrector, even if @options[:auto_correct] is
90
+ # nil, because `add_offense` always attempts correction. This
91
+ # correction attempt is how RuboCop knows if the offense can be
92
+ # labeled "[Correctable]".
93
+ comment_locations = ::Parser::Source::Comment.associate_locations(
94
+ processed_source.ast,
95
+ processed_source.comments
96
+ )
97
+ corrector = OrderedMethodsCorrector.new(comment_locations, siblings, cop_config)
98
+
99
+ [siblings, corrector]
100
+ end
88
101
  end
102
+
103
+ @cache[node.hash]
89
104
  end
105
+ # rubocop:enable Metrics/MethodLength
90
106
 
91
107
  # We disable `Style/ExplicitBlockArgument` for performance. See
92
108
  # https://github.com/shanecav84/rubocop-ordered_methods/pull/5#pullrequestreview-562957146
@@ -103,11 +119,9 @@ module RuboCop
103
119
  # rubocop:enable Style/ExplicitBlockArgument
104
120
 
105
121
  def filter_relevant_nodes(nodes)
106
- 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
+ nodes.compact.select do |node|
123
+ next unless node.is_a?(Parser::AST::Node)
124
+ relevant_node?(node) || (node.send_type? && qualifier_macro?(node))
111
125
  end
112
126
  end
113
127
 
@@ -139,6 +153,17 @@ module RuboCop
139
153
 
140
154
  comparison.call(left_method, right_method)
141
155
  end
156
+
157
+ def qualifier_macro?(node)
158
+ return true if node.bare_access_modifier?
159
+
160
+ cop_config['MethodQualifiers'].to_a.include?(node.method_name.to_s) &&
161
+ relevant_node?(node.first_argument)
162
+ end
163
+
164
+ def relevant_node?(node)
165
+ (node.defs_type? || node.def_type?) && !ignored_method?(node.method_name)
166
+ end
142
167
  end
143
168
  end
144
169
  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? {#{QUALIFIERS.map(&:inspect).join(' ')}}
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.9'
8
+ spec.version = '0.11'
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.4'
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.add_development_dependency 'bundler'
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.9'
4
+ version: '0.11'
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: 2021-03-10 00:00:00.000000000 Z
11
+ date: 2023-12-19 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
- - !ruby/object:Gem::Dependency
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
- post_install_message:
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.4'
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.1.4
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: []