rubocop-fourshark 0.2.1 → 0.2.2

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: 6c048d884d1fbe9ba0f351c1bdf7e93e70410136084f72bb700dacb04d773cf8
4
+ data.tar.gz: 2459ffac3431fce8bcba699eea337e61146dedac826ea854f262b96aa01139d7
5
5
  SHA512:
6
- metadata.gz: 6cd1bc1df875dc06da1fbe6091e896afe434e99f13e64c3f564e5ac43bcf23ac5cb5a4e3e0ff6792829df9df109ec331066fac1395472b536372ced8dd025d65
7
- data.tar.gz: f62cfe9f582d0ef9108800eef733aa8957852b7183cc3bee53f7182680a51ceda57293d19137f4baf9badb345cf9add64f9a15643734d0b515672188026435a9
6
+ metadata.gz: 76dbe2d05b1dc7471ac29d280a91d5bf9daa7a3da1888b7487b0715e36049d9883e72f15de51736c7ad35a8dac5bcca738797547e3f5549b8661b9034253ef26
7
+ data.tar.gz: c09dcdc4fd7a190a916c9ce199116ad61fcfe2e5d5023139512f23da7418c780a1232b4489e2fb20982b417f93a264c162e2e6af1719255a4875a395c7da5898
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [0.2.2] - 2026-05-30
2
+
3
+ ### Fixed
4
+
5
+ - `RSpec/InverseOfMatcher` no longer demands `.inverse_of` on polymorphic associations — it reads the polymorphic declaration from the model instead of the spec matcher chain
6
+ - `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
7
+
1
8
  ## [0.2.1] - 2026-05-29
2
9
 
3
10
  ### Fixed
@@ -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.2'
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Ribeiro