rubocop-fourshark 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 202b492f9486b827fd67079c3a7bb113bb194a87fd968646eebbf30b881140cc
4
- data.tar.gz: 20f9b022eb67bad4c30fe2295c3d56a30c027594c15142303dcfe707c5390f85
3
+ metadata.gz: 4f2644c550944bc0f11e7b4222b430e7cecc1ed4bf97f2650eb6f60e9f588a03
4
+ data.tar.gz: 5e2cbafe91b1b52c7d8de97d8d45c8a7e39a65921d3040bd07d48b5ccfbcd0e4
5
5
  SHA512:
6
- metadata.gz: 6cd1bc1df875dc06da1fbe6091e896afe434e99f13e64c3f564e5ac43bcf23ac5cb5a4e3e0ff6792829df9df109ec331066fac1395472b536372ced8dd025d65
7
- data.tar.gz: f62cfe9f582d0ef9108800eef733aa8957852b7183cc3bee53f7182680a51ceda57293d19137f4baf9badb345cf9add64f9a15643734d0b515672188026435a9
6
+ metadata.gz: 7564a809a8d3e84320ac5de4e597cdc610c8568dbbc30eafcfa8b908955a38d022e68778fce30a35a8391a4576b98f965976e03234adb985c64dc26ae46c7ca8
7
+ data.tar.gz: fc105db26052f84221d17282382d867aa3d72a12d8a8b91d900061b84b01fe58d552e5365755af15f1a72df2036bb523574b28ff6311d2ffd14c0de5295e35dc
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ ## [0.2.3] - 2026-05-30
2
+
3
+ ### Fixed
4
+
5
+ - `Rails/OrderedMacros` sorts `:through` associations as a separate trailing group instead of interleaving them alphabetically — a `:through` association declared after its target association is no longer flagged
6
+
7
+ ## [0.2.2] - 2026-05-30
8
+
9
+ ### Fixed
10
+
11
+ - `RSpec/InverseOfMatcher` no longer demands `.inverse_of` on polymorphic associations — it reads the polymorphic declaration from the model instead of the spec matcher chain
12
+ - `RSpec/InverseOfMatcher` classifies a nested class by its own superclass instead of the first `class` line in the file — a nested root model is no longer misread as an STI subclass
13
+
1
14
  ## [0.2.1] - 2026-05-29
2
15
 
3
16
  ### Fixed
@@ -9,9 +9,13 @@ module RuboCop
9
9
  # alphabetically by their first symbol argument. Checked per macro name:
10
10
  # all `belongs_to` sorted among themselves, all `validates` sorted, etc.
11
11
  #
12
- # Exceptions (lifecycle callbacks, dependency-ordered items, logical
13
- # grouping) are NOT modelled this is an experimental cop; if it flags
14
- # more legitimate cases than it helps, drop it.
12
+ # `:through` associations are sorted as a separate trailing group (a
13
+ # `:through` association must be declared after its target association),
14
+ # not interleaved with the regular declarations.
15
+ #
16
+ # Other exceptions (lifecycle callbacks, other dependency-ordered items)
17
+ # are NOT modelled — this is an experimental cop; if it flags more
18
+ # legitimate cases than it helps, drop it.
15
19
  #
16
20
  # @example
17
21
  # # bad
@@ -38,12 +42,24 @@ module RuboCop
38
42
  statements = body.begin_type? ? body.children : [body]
39
43
 
40
44
  MACROS.each do |macro|
41
- flag_unsorted(statements.select { |statement| macro_call?(statement, macro) })
45
+ calls = statements.select { |statement| macro_call?(statement, macro) }
46
+ regular, through = calls.partition { |call| !through_option?(call) }
47
+ flag_unsorted(regular)
48
+ flag_unsorted(through)
42
49
  end
43
50
  end
44
51
 
45
52
  private
46
53
 
54
+ # A `:through` association must be declared after its target association,
55
+ # so it forms a separate trailing group sorted among itself — never
56
+ # interleaved with (or compared against) the regular declarations.
57
+ def through_option?(node)
58
+ node.arguments.any? do |argument|
59
+ argument.hash_type? && argument.keys.any? { |key| key.value == :through }
60
+ end
61
+ end
62
+
47
63
  def flag_unsorted(calls)
48
64
  calls.each_cons(2) do |previous, current|
49
65
  previous_name = macro_name(previous)
@@ -12,7 +12,7 @@ module RuboCop
12
12
  # Rules:
13
13
  # - Root models (direct superclass = `ApplicationRecord`) must include `.inverse_of`.
14
14
  # - STI subclasses (superclass is another model) must NOT include `.inverse_of` (it belongs to the parent).
15
- # - Associations marked as polymorphic or through are ignored.
15
+ # - Associations the model declares as polymorphic are ignored (they have no single inverse).
16
16
  # - Classes whose model file is missing or whose superclass is not a model are ignored.
17
17
  #
18
18
  # The root/subclass decision is made **statically** — by reading the model
@@ -31,12 +31,13 @@ module RuboCop
31
31
 
32
32
  def on_send(node)
33
33
  return unless node.method?(:belong_to)
34
- return if chain_has_option?(node, :polymorphic)
35
- return if chain_has_option?(node, :through)
36
34
 
37
35
  model_name = model_class_from_spec
38
36
  return unless model_name
39
37
 
38
+ association_name = association_name_from(node)
39
+ return if association_name && polymorphic_in_model?(model_name, association_name)
40
+
40
41
  classification = classify_model(model_name)
41
42
  return if classification == :non_ar
42
43
 
@@ -63,13 +64,22 @@ module RuboCop
63
64
  node.each_ancestor(:send).any? { |ancestor| ancestor.method?(:inverse_of) }
64
65
  end
65
66
 
66
- # Check if association has a given option (e.g., :polymorphic, :through)
67
- def chain_has_option?(node, option_name)
68
- node.each_ancestor(:send).any? do |ancestor|
69
- ancestor.arguments.any? do |arg|
70
- arg.hash_type? && arg.keys.any? { |k| k.value == option_name }
71
- end
72
- end
67
+ # The association name passed to the matcher, e.g. belong_to(:user) -> :user
68
+ def association_name_from(node)
69
+ argument = node.first_argument
70
+ return nil unless argument && argument.sym_type?
71
+
72
+ argument.value
73
+ end
74
+
75
+ # Whether the model declares this association as polymorphic. Polymorphic
76
+ # associations have no single inverse, so `.inverse_of` does not apply —
77
+ # and the declaration lives in the model, not in the spec matcher chain.
78
+ def polymorphic_in_model?(model_name, association_name)
79
+ content = model_source(model_name)
80
+ return false unless content
81
+
82
+ content.match?(/belongs_to\s+:#{Regexp.escape(association_name.to_s)}\b[^\n]*polymorphic:\s*true/)
73
83
  end
74
84
 
75
85
  # Convert spec file path into model class name
@@ -92,7 +102,7 @@ module RuboCop
92
102
  content = model_source(model_name)
93
103
  return :non_ar unless content
94
104
 
95
- superclass = superclass_of(content)
105
+ superclass = superclass_of(content, model_name)
96
106
  return :non_ar unless superclass
97
107
  return :root if superclass == 'ApplicationRecord'
98
108
  return :subclass if model_source(superclass)
@@ -107,8 +117,12 @@ module RuboCop
107
117
  File.exist?(path) ? File.read(path) : nil
108
118
  end
109
119
 
110
- def superclass_of(content)
111
- match = content.match(/^\s*class\s+[\w:]+\s*<\s*([\w:]+)/)
120
+ # The declared superclass of the specific class named by the spec — not the
121
+ # first `class` line in the file. A nested `class Row < ApplicationRecord`
122
+ # inside `class Wrapper < Parent` must resolve to its own superclass.
123
+ def superclass_of(content, model_name)
124
+ class_name = model_name.split('::').last
125
+ match = content.match(/^\s*class\s+(?:[\w:]*::)?#{Regexp.escape(class_name)}\s*<\s*([\w:]+)/)
112
126
  match && match[1]
113
127
  end
114
128
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Fourshark
5
- VERSION = '0.2.1'
5
+ VERSION = '0.2.3'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-fourshark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Ribeiro