rubocop-obsession 0.1.6 → 0.1.8

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: 4b1e6edfacbf86e2ac264de49f2ac818af6ee693c6cabe9865894b556953e3ad
4
- data.tar.gz: 9d523c4878734ef93c3b0052622ffe49e1a3913505963a91fe10327ddac17660
3
+ metadata.gz: d393d8af056f32d108c9c0f272cf6a6ccf2158d33caf91a950ca317f9da2a9cb
4
+ data.tar.gz: 822e9f0697a1559eb91755d9f2702a9d121727c89e9715e9c48ab447ff702220
5
5
  SHA512:
6
- metadata.gz: eed54a03536307d0bfdda7b43b8346306420d1037068921b238f67fbd938c276d84f5802f70ce42bc0a26f3d46f399a5db9e3864ac73adfc01e6e5911f8f5f83
7
- data.tar.gz: bc7e712ec48bbe05f8419754a9b9d1f443973e154ca4525dc0712a3c0576be840b66c65e67c8a206a12a628af97d703c7b84d50a15951dc8446dc1bda5c5461e
6
+ metadata.gz: 9dc017470f824de21567bf7ec06ea9e372f5edc147932f715e8bdb3a8b60ef6cdfb1a84905b33c9ae0e340cf8515455e59374ec22121c6b4b36296204527918d
7
+ data.tar.gz: d4a5f84c5714d34ae2733e4f13fb58a089955d83c546ff411a34614bf2178a749f8acd1fc31bd4cbacb3d6fd5d85b24a0648fb7545f44a7a60110d1aa194494f
@@ -11,18 +11,21 @@ module RuboCop
11
11
  # Private/protected methods should follow that rule.
12
12
  #
13
13
  # Note 1: public methods do not have to follow that rule, and can be
14
- # defined in any order the developer wants, like by order of
15
- # importance. This is because they are usually called outside of the
16
- # class and often not called within the class at all. If possible though,
17
- # developers should still try to order their public methods from top to
18
- # bottom when it makes sense.
14
+ # defined in any order the developer wants, like by order of importance.
15
+ # This is because they are usually called outside of the class and often
16
+ # not called within the class at all. If possible though, developers
17
+ # should still try to order their public methods from top to bottom when
18
+ # it makes sense.
19
19
  #
20
20
  # Note 2: method order cannot be computed for methods called by `send`,
21
21
  # metaprogramming, private methods called by superclasses or modules,
22
22
  # etc. This cop's suggestions and autocorrections may be slightly off for
23
23
  # these kinds of edge cases.
24
24
  #
25
- # Note 3: for more information on this style of method ordering, see
25
+ # Note 3: for simplicity, protected methods do not have to follow that
26
+ # rule if there are both a protected section and a private section.
27
+ #
28
+ # Note 4: for more information on this style of method ordering, see
26
29
  # Robert C. Martin's "Clean Code" book > "Chapter 3: Functions" > "One
27
30
  # level of abstraction per function" > "Reading Code from Top to Bottom:
28
31
  # The Stepdown Rule" chapter.
@@ -62,7 +65,7 @@ module RuboCop
62
65
 
63
66
  MSG = 'Method `%<after>s` should appear below `%<previous>s`.'
64
67
 
65
- def_node_search :private_node, <<~PATTERN
68
+ def_node_search :private_nodes, <<~PATTERN
66
69
  (send nil? {:private :protected})
67
70
  PATTERN
68
71
 
@@ -103,7 +106,24 @@ module RuboCop
103
106
  private
104
107
 
105
108
  def find_private_node
106
- @private_node = private_node(@class_node)&.first
109
+ private_nodes = private_nodes(@class_node).to_a
110
+ return nil if private_nodes.empty?
111
+
112
+ visibilities = private_nodes.map(&:method_name)
113
+ @ignore_protected = visibilities.include?(:protected) && visibilities.include?(:private)
114
+
115
+ @private_node = private_nodes.find { |node| !ignore_visibility?(node.method_name) }
116
+ end
117
+
118
+ def ignore_visibility?(visibility)
119
+ case visibility
120
+ when :public
121
+ true
122
+ when :protected
123
+ @ignore_protected
124
+ when :private
125
+ false
126
+ end
107
127
  end
108
128
 
109
129
  def build_methods
@@ -174,7 +194,7 @@ module RuboCop
174
194
  end
175
195
 
176
196
  def should_ignore?(ast_node)
177
- ast_node.nil? || node_visibility(ast_node) == :public ||
197
+ ast_node.nil? || ignore_visibility?(node_visibility(ast_node)) ||
178
198
  !@called_methods.include?(ast_node)
179
199
  end
180
200
 
@@ -213,7 +233,7 @@ module RuboCop
213
233
  previous_method_range = previous_method_range.adjust(end_pos: 1)
214
234
  end
215
235
 
216
- method_range = source_range_with_comment(method)
236
+ method_range = source_range_with_signature(method)
217
237
  if buffer.source[method_range.begin_pos - 1] == "\n"
218
238
  method_range = method_range.adjust(end_pos: 1)
219
239
  end
@@ -221,6 +241,20 @@ module RuboCop
221
241
  corrector.insert_after(previous_method_range, method_range.source)
222
242
  corrector.remove(method_range)
223
243
  end
244
+
245
+ def source_range_with_signature(method)
246
+ previous_node = method.left_sibling
247
+ begin_node = sorbet_signature?(previous_node) ? previous_node : method
248
+
249
+ begin_pos = begin_pos_with_comment(begin_node)
250
+ end_pos = end_position_for(method)
251
+
252
+ Parser::Source::Range.new(buffer, begin_pos, end_pos)
253
+ end
254
+
255
+ def sorbet_signature?(node)
256
+ node && node.method_name == :sig && node.type == :block
257
+ end
224
258
  end
225
259
  end
226
260
  end
@@ -11,8 +11,8 @@ module RuboCop
11
11
  # - For small loops, you can just use normal conditions instead of `next`.
12
12
  # `break` is allowed.
13
13
  #
14
- # Note: Sometimes loops can also be rethought, like transforming a `loop`
15
- # + `break` into a `while`.
14
+ # Note: Sometimes loops can also be rethought, like transforming a
15
+ # `loop` + `break` into a `while`.
16
16
  #
17
17
  # @example
18
18
  #
@@ -11,6 +11,19 @@ module RuboCop
11
11
  # stale TODOs. Sometimes developers really mean to work on their TODOs
12
12
  # soon, but then Product re-prioritizes their work, or the developer
13
13
  # leaves the company, and never gets a chance to tackle them.
14
+ #
15
+ # @example
16
+ #
17
+ # # bad
18
+ # # TODO: remove this method when we ship the new signup flow
19
+ # def my_method
20
+ # ...
21
+ # end
22
+ #
23
+ # # good
24
+ # def my_method
25
+ # ...
26
+ # end
14
27
  class NoTodos < Base
15
28
  MSG = 'Avoid TODO comment, create a task in your project management tool instead.'
16
29
  KEYWORD_REGEX = /(^|[^\w])(TODO|FIXME|OPTIMIZE|HACK)($|[^\w])/i
@@ -18,14 +18,18 @@ module RuboCop
18
18
  # @example
19
19
  #
20
20
  # # bad
21
- # add_column :languages, :items, :jsonb
21
+ # def change
22
+ # add_column :languages, :items, :jsonb
23
+ # end
22
24
  #
23
25
  # # good
24
- # add_column :languages,
25
- # :items,
26
- # :jsonb,
27
- # default: [],
28
- # comment: "Example: [{ 'name': 'ruby' }, { 'name': 'python' }]"
26
+ # def change
27
+ # add_column :languages,
28
+ # :items,
29
+ # :jsonb,
30
+ # default: [],
31
+ # comment: "Example: [{ 'name': 'ruby' }, { 'name': 'python' }]"
32
+ # end
29
33
  class FullyDefinedJsonField < Base
30
34
  def_node_matcher :json_field?, <<~PATTERN
31
35
  (send nil? :add_column _ _ (sym {:json :jsonb}) ...)
@@ -19,12 +19,14 @@ module RuboCop
19
19
  #
20
20
  # # bad
21
21
  # after_update_commit :crawl_rss, if: :rss_changed?
22
+ #
22
23
  # def crawl_rss
23
24
  # ...
24
25
  # end
25
26
  #
26
27
  # # good
27
28
  # after_update_commit :crawl_rss
29
+ #
28
30
  # def crawl_rss
29
31
  # return if !rss_changed?
30
32
  # ...
@@ -13,6 +13,23 @@ module RuboCop
13
13
  # of the DB migration. The reason should be detailed and reviewed by a
14
14
  # knowledgeable PR reviewer. Failure to follow instructions may bring your
15
15
  # app down.
16
+ #
17
+ # @example
18
+ #
19
+ # # bad
20
+ # class RemoveSourceUrlFromBlogPosts < ActiveRecord::Migration[8.0]
21
+ # def change
22
+ # safety_assured { remove_column :blog_posts, :source_url }
23
+ # end
24
+ # end
25
+ #
26
+ # # good
27
+ # class RemoveSourceUrlFromBlogPosts < ActiveRecord::Migration[8.0]
28
+ # # Safe because this column was ignored with self.ignored_columns in PR #1234
29
+ # def change
30
+ # safety_assured { remove_column :blog_posts, :source_url }
31
+ # end
32
+ # end
16
33
  class SafetyAssuredComment < Base
17
34
  MSG =
18
35
  'Add `# Safe because <reason>` comment above safety_assured. ' \
@@ -8,15 +8,17 @@ if defined?(RuboCop::RSpec)
8
8
  #
9
9
  # @example
10
10
  #
11
- # describe '#domain' do
12
- # context do
13
- # let(:url) { Url.new('http://www.some-site.com/some-page') }
14
- # it { expect(url.domain).to eq 'some-site.com' }
15
- # end
11
+ # describe Url do
12
+ # describe '#domain' do
13
+ # context do
14
+ # let(:url) { Url.new('http://www.some-site.com/some-page') }
15
+ # it { expect(url.domain).to eq 'some-site.com' }
16
+ # end
16
17
  #
17
- # context do
18
- # let(:url) { Url.new('some-site.com') }
19
- # it { expect(url.domain).to eq 'some-site.com' }
18
+ # context do
19
+ # let(:url) { Url.new('some-site.com') }
20
+ # it { expect(url.domain).to eq 'some-site.com' }
21
+ # end
20
22
  # end
21
23
  # end
22
24
  class EmptyLineAfterFinalLet < RSpec::EmptyLineAfterFinalLet
@@ -1,5 +1,5 @@
1
1
  module Rubocop
2
2
  module Obsession
3
- VERSION = '0.1.6'
3
+ VERSION = '0.1.8'
4
4
  end
5
5
  end
@@ -1,5 +1,6 @@
1
1
  require 'active_support'
2
2
  require 'active_support/core_ext/object/blank'
3
+ require 'active_support/core_ext/string/inflections'
3
4
  require 'rubocop'
4
5
 
5
6
  require_relative 'cop/mixin/helpers'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-obsession
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jerome Dalbert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-02 00:00:00.000000000 Z
11
+ date: 2024-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport