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 +4 -4
- data/CHANGELOG.md +7 -0
- data/lib/rubocop/cop/rspec/inverse_of_matcher.rb +27 -13
- data/lib/rubocop/fourshark/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6c048d884d1fbe9ba0f351c1bdf7e93e70410136084f72bb700dacb04d773cf8
|
|
4
|
+
data.tar.gz: 2459ffac3431fce8bcba699eea337e61146dedac826ea854f262b96aa01139d7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
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
|
-
#
|
|
67
|
-
def
|
|
68
|
-
node.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
111
|
-
|
|
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
|
|