rubocop-metz 0.5.3 → 0.6.0
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/config/default.yml +62 -0
- data/lib/metz/file_classifier.rb +8 -0
- data/lib/rubocop/cop/metz/demeter_train_wreck/type_inference.rb +3 -9
- data/lib/rubocop/cop/metz/god_service_class.rb +82 -0
- data/lib/rubocop/cop/metz/operations_too_many_public_methods.rb +71 -0
- data/lib/rubocop/cop/metz/operator_methods.rb +24 -0
- data/lib/rubocop/cop/metz/public_api_methods.rb +67 -0
- data/lib/rubocop/cop/metz/test_asserts_on_internals.rb +81 -0
- data/lib/rubocop/cop/metz/test_reaches_private.rb +101 -0
- data/lib/rubocop/cop/metz/test_stubs_subject.rb +138 -0
- data/lib/rubocop/cop/metz/views_deep_navigation.rb +1 -2
- data/lib/rubocop/cop/metz_cops.rb +5 -0
- data/lib/rubocop/metz/version.rb +1 -1
- data/rubocop-metz.gemspec +10 -12
- metadata +13 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7a70996428c4b11c07460d7eaf12494f2e675d1b574e4f92af1ddbc245e8ecfc
|
|
4
|
+
data.tar.gz: af4ae4bd83391bf13da58d62c3a87c5a07be076713167fa34e349449ebf6c756
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 47086b1fb0e8244ab9f249b69b76e722cce8685647a938ca7a6387815a315d4e28da0870cf848eadd10b70150c8cc3100f327a53d4861b74e681d19ff7f29816
|
|
7
|
+
data.tar.gz: f5f13b5c4ffdf5e9a72e9baede758c77ab2d460a7137d0ddc658116279eabc37f4aa35555166b7436744189facc2f0b65aa2000c69e617d4b3ffd864dc86db37
|
data/config/default.yml
CHANGED
|
@@ -57,6 +57,43 @@ Metz/DemeterTrainWreck:
|
|
|
57
57
|
- 'db/migrate/**/*'
|
|
58
58
|
VersionAdded: '0.1'
|
|
59
59
|
|
|
60
|
+
Metz/TestReachesPrivate:
|
|
61
|
+
Description: 'Flags tests that reach past the public interface with send/__send__ and a literal method name.'
|
|
62
|
+
Enabled: false
|
|
63
|
+
Severity: refactor
|
|
64
|
+
Include:
|
|
65
|
+
- '**/*_test.rb'
|
|
66
|
+
- '**/test_*.rb'
|
|
67
|
+
- '**/*_spec.rb'
|
|
68
|
+
AllowedMethods:
|
|
69
|
+
- define_method
|
|
70
|
+
- remove_const
|
|
71
|
+
- include
|
|
72
|
+
- extend
|
|
73
|
+
- prepend
|
|
74
|
+
- alias_method
|
|
75
|
+
AllowedReceivers: []
|
|
76
|
+
VersionAdded: '0.6'
|
|
77
|
+
|
|
78
|
+
Metz/TestAssertsOnInternals:
|
|
79
|
+
Description: 'Flags tests that assert on internal state via instance_variable_get/_set or Rails controller-test assigns.'
|
|
80
|
+
Enabled: false
|
|
81
|
+
Severity: refactor
|
|
82
|
+
Include:
|
|
83
|
+
- '**/*_test.rb'
|
|
84
|
+
- '**/test_*.rb'
|
|
85
|
+
- '**/*_spec.rb'
|
|
86
|
+
AllowedReceivers: []
|
|
87
|
+
VersionAdded: '0.6'
|
|
88
|
+
|
|
89
|
+
Metz/TestStubsSubject:
|
|
90
|
+
Description: 'Flags RSpec tests that stub or mock the subject under test instead of its collaborators.'
|
|
91
|
+
Enabled: false
|
|
92
|
+
Severity: refactor
|
|
93
|
+
Include:
|
|
94
|
+
- '**/*_spec.rb'
|
|
95
|
+
VersionAdded: '0.6'
|
|
96
|
+
|
|
60
97
|
Metz/ControllersTooManyDirectCollaborators:
|
|
61
98
|
Description: 'Flags Rails controller actions that reach into more than MaxCollaborators distinct collaborator constants.'
|
|
62
99
|
Enabled: true
|
|
@@ -66,6 +103,31 @@ Metz/ControllersTooManyDirectCollaborators:
|
|
|
66
103
|
- 'app/controllers/**/*.rb'
|
|
67
104
|
VersionAdded: '0.1'
|
|
68
105
|
|
|
106
|
+
Metz/OperationsTooManyPublicMethods:
|
|
107
|
+
Description: >-
|
|
108
|
+
Flags classes under app/services or app/operations that expose more than
|
|
109
|
+
MaxPublicMethods public methods (excluding AllowedMethods such as initialize).
|
|
110
|
+
Enabled: true
|
|
111
|
+
Severity: refactor
|
|
112
|
+
MaxPublicMethods: 1
|
|
113
|
+
AllowedMethods:
|
|
114
|
+
- initialize
|
|
115
|
+
Include:
|
|
116
|
+
- 'app/services/**/*.rb'
|
|
117
|
+
- 'app/operations/**/*.rb'
|
|
118
|
+
VersionAdded: '0.6'
|
|
119
|
+
|
|
120
|
+
Metz/GodServiceClass:
|
|
121
|
+
Description: >-
|
|
122
|
+
Flags *Service classes that expose more than MaxPublicMethods public methods
|
|
123
|
+
(excluding AllowedMethods such as initialize) — god-service / multi-use bags.
|
|
124
|
+
Enabled: true
|
|
125
|
+
Severity: refactor
|
|
126
|
+
MaxPublicMethods: 1
|
|
127
|
+
AllowedMethods:
|
|
128
|
+
- initialize
|
|
129
|
+
VersionAdded: '0.6'
|
|
130
|
+
|
|
69
131
|
Metz/ViewsDeepNavigation:
|
|
70
132
|
Description: 'Flags Rails view templates whose ERB/HAML/SLIM expressions traverse the object graph deeper than MaxChainLength.'
|
|
71
133
|
Enabled: true
|
data/lib/metz/file_classifier.rb
CHANGED
|
@@ -21,6 +21,7 @@ module Metz
|
|
|
21
21
|
CONTROLLER_PATTERN = %r{(?:\A|/)app/controllers/[^\0]+\.rb\z}
|
|
22
22
|
VIEW_PATTERN = %r{(?:\A|/)app/views/[^\0]+\.(?:erb|haml|slim)\z}
|
|
23
23
|
MODEL_PATTERN = %r{(?:\A|/)app/models/[^\0]+\.rb\z}
|
|
24
|
+
OPERATION_PATTERN = %r{(?:\A|/)app/(?:services|operations)/[^\0]+\.rb\z}
|
|
24
25
|
|
|
25
26
|
module_function
|
|
26
27
|
|
|
@@ -36,6 +37,13 @@ module Metz
|
|
|
36
37
|
normalize(path).match?(MODEL_PATTERN)
|
|
37
38
|
end
|
|
38
39
|
|
|
40
|
+
# Application-operation homes (legacy `app/services` and preferred
|
|
41
|
+
# `app/operations`). Path alone does not legitimize a class; it only
|
|
42
|
+
# scopes shape cops such as OperationsTooManyPublicMethods.
|
|
43
|
+
def operation?(path)
|
|
44
|
+
normalize(path).match?(OPERATION_PATTERN)
|
|
45
|
+
end
|
|
46
|
+
|
|
39
47
|
# Windows-style paths arrive with backslash separators when the caller is
|
|
40
48
|
# an editor running on Windows or a CI step that shells out via `cmd.exe`.
|
|
41
49
|
# Normalize to forward slashes so the regexes below stay POSIX-shaped.
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "../demeter_train_wreck" unless defined?(RuboCop::Cop::Metz::DemeterTrainWreck)
|
|
4
|
+
require_relative "../operator_methods"
|
|
4
5
|
|
|
5
6
|
module RuboCop
|
|
6
7
|
module Cop
|
|
@@ -18,13 +19,7 @@ module RuboCop
|
|
|
18
19
|
|
|
19
20
|
PASS_THROUGH = %i[tap then yield_self itself dup clone freeze].to_set.freeze
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
%i[+ - * / % **] +
|
|
23
|
-
%i[== != < > <= >= <=> === =~ !~] +
|
|
24
|
-
%i[& | ^ << >>]
|
|
25
|
-
).to_set.freeze
|
|
26
|
-
|
|
27
|
-
# rubocop:disable Lint/BooleanSymbol
|
|
22
|
+
# rubocop:disable-next Lint/BooleanSymbol
|
|
28
23
|
LITERAL_MAP = {
|
|
29
24
|
str: :string, dstr: :string, xstr: :string,
|
|
30
25
|
sym: :symbol, dsym: :symbol,
|
|
@@ -33,7 +28,6 @@ module RuboCop
|
|
|
33
28
|
true: :boolean, false: :boolean, nil: :nil_value,
|
|
34
29
|
regexp: :regexp, irange: :range, erange: :range
|
|
35
30
|
}.freeze
|
|
36
|
-
# rubocop:enable Lint/BooleanSymbol
|
|
37
31
|
|
|
38
32
|
METHOD_RETURN_TYPES = {
|
|
39
33
|
string: {
|
|
@@ -225,7 +219,7 @@ module RuboCop
|
|
|
225
219
|
end
|
|
226
220
|
|
|
227
221
|
def self.operator?(method)
|
|
228
|
-
|
|
222
|
+
RuboCop::Cop::Metz::OperatorMethods.operator?(method)
|
|
229
223
|
end
|
|
230
224
|
end
|
|
231
225
|
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rubocop"
|
|
4
|
+
require_relative "../../../metz/cop_metadata"
|
|
5
|
+
require_relative "public_api_methods"
|
|
6
|
+
|
|
7
|
+
module RuboCop
|
|
8
|
+
module Cop
|
|
9
|
+
module Metz
|
|
10
|
+
# Flags `*Service` classes that expose multiple public methods — the classic
|
|
11
|
+
# god-service / procedural module-with-a-class-wrapper shape
|
|
12
|
+
# (`UserService#create/#update/#notify`).
|
|
13
|
+
#
|
|
14
|
+
# Applies by class name (suffix `Service`), not by path.
|
|
15
|
+
# Full standard: docs/application-operations.md (rule R2) in this repo.
|
|
16
|
+
class GodServiceClass < RuboCop::Cop::Base
|
|
17
|
+
extend ::Metz::CopMetadata
|
|
18
|
+
|
|
19
|
+
why_it_matters "A *Service class with many public methods is a procedural " \
|
|
20
|
+
"namespace, not an object — it scatters use cases and weakens domain modeling."
|
|
21
|
+
fix_safety :manual
|
|
22
|
+
suggested_next_moves [
|
|
23
|
+
"Split each use case into its own thin operation or a method on the owning domain object.",
|
|
24
|
+
"If one real workflow remains, keep a single public entry and privatize helpers.",
|
|
25
|
+
"Drop the Service suffix when the type is a real domain concept with a clear interface."
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
MSG = "%<const>s defines %<count>d public methods (%<names>s); " \
|
|
29
|
+
"god *Service classes should not host multiple use cases " \
|
|
30
|
+
"(max %<max>d public, excluding %<allowed>s)."
|
|
31
|
+
|
|
32
|
+
def on_class(node)
|
|
33
|
+
const_name = class_basename(node)
|
|
34
|
+
return unless service_class_name?(const_name)
|
|
35
|
+
|
|
36
|
+
public_names = api_names(node)
|
|
37
|
+
return if public_names.size <= max_public_methods
|
|
38
|
+
|
|
39
|
+
add_offense(node.identifier, message: offense_message(const_name, public_names))
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def service_class_name?(name)
|
|
45
|
+
return false if name.nil? || name.empty?
|
|
46
|
+
|
|
47
|
+
name == "Service" || name.end_with?("Service")
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def class_basename(node)
|
|
51
|
+
const = node.identifier
|
|
52
|
+
return unless const&.const_type?
|
|
53
|
+
|
|
54
|
+
const.const_name.to_s.split("::").last
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def api_names(node)
|
|
58
|
+
PublicApiMethods.new(node, allowed_methods: allowed_methods).names
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def offense_message(const_name, public_names)
|
|
62
|
+
format(MSG, **message_fields(const_name, public_names))
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def message_fields(const_name, public_names)
|
|
66
|
+
names = public_names.map(&:inspect).join(", ")
|
|
67
|
+
allowed = allowed_methods.map(&:inspect).join(", ")
|
|
68
|
+
{ const: const_name, count: public_names.size, names: names,
|
|
69
|
+
max: max_public_methods, allowed: allowed }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def max_public_methods
|
|
73
|
+
cop_config.fetch("MaxPublicMethods", 1).to_i
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def allowed_methods
|
|
77
|
+
Array(cop_config["AllowedMethods"]).map(&:to_sym)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rubocop"
|
|
4
|
+
require_relative "../../../metz/file_classifier"
|
|
5
|
+
require_relative "../../../metz/cop_metadata"
|
|
6
|
+
require_relative "public_api_methods"
|
|
7
|
+
|
|
8
|
+
module RuboCop
|
|
9
|
+
module Cop
|
|
10
|
+
module Metz
|
|
11
|
+
# Flags application-operation classes that expose more than one public
|
|
12
|
+
# entry method. Legitimate operations are thin commands with a single
|
|
13
|
+
# intentional API (typically `call` / `perform`), not multi-use bags.
|
|
14
|
+
#
|
|
15
|
+
# Scoped to operation-shaped paths (`app/services/**`, `app/operations/**`).
|
|
16
|
+
# Full standard: docs/application-operations.md (rule R1) in this repo.
|
|
17
|
+
class OperationsTooManyPublicMethods < RuboCop::Cop::Base
|
|
18
|
+
extend ::Metz::CopMetadata
|
|
19
|
+
|
|
20
|
+
why_it_matters "Multiple public methods on an operation turn a use-case " \
|
|
21
|
+
"command into a procedural bag of behaviors and hide missing domain concepts."
|
|
22
|
+
fix_safety :manual
|
|
23
|
+
suggested_next_moves [
|
|
24
|
+
"Keep a single public entry (`call` / `perform`) and make helpers private.",
|
|
25
|
+
"Split unrelated use cases into separate operation classes or domain methods.",
|
|
26
|
+
"Move invariants and calculations onto the objects that own the data."
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
MSG = "Operation defines %<count>d public methods (%<names>s); " \
|
|
30
|
+
"keep a single public entry (max %<max>d, excluding %<allowed>s)."
|
|
31
|
+
|
|
32
|
+
def on_class(node)
|
|
33
|
+
return unless operation_file?
|
|
34
|
+
|
|
35
|
+
public_names = api_names(node)
|
|
36
|
+
return if public_names.size <= max_public_methods
|
|
37
|
+
|
|
38
|
+
add_offense(node.identifier, message: offense_message(public_names))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def operation_file?
|
|
44
|
+
::Metz::FileClassifier.operation?(processed_source.file_path)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def api_names(node)
|
|
48
|
+
PublicApiMethods.new(node, allowed_methods: allowed_methods).names
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def offense_message(public_names)
|
|
52
|
+
format(MSG, **message_fields(public_names))
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def message_fields(public_names)
|
|
56
|
+
names = public_names.map(&:inspect).join(", ")
|
|
57
|
+
allowed = allowed_methods.map(&:inspect).join(", ")
|
|
58
|
+
{ count: public_names.size, names: names, max: max_public_methods, allowed: allowed }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def max_public_methods
|
|
62
|
+
cop_config.fetch("MaxPublicMethods", 1).to_i
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def allowed_methods
|
|
66
|
+
Array(cop_config["AllowedMethods"]).map(&:to_sym)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Metz
|
|
6
|
+
# Provides a canonical predicate for operator method symbols.
|
|
7
|
+
# Extracted from Metz/DemeterTrainWreck's TypeInference module
|
|
8
|
+
# so testing cops can use it without coupling to DemeterTrainWreck.
|
|
9
|
+
module OperatorMethods
|
|
10
|
+
OPERATOR_METHODS = (
|
|
11
|
+
%i[+ - * / % **] +
|
|
12
|
+
%i[== != < > <= >= <=> === =~ !~] +
|
|
13
|
+
%i[& | ^ << >>]
|
|
14
|
+
).to_set.freeze
|
|
15
|
+
|
|
16
|
+
module_function
|
|
17
|
+
|
|
18
|
+
def operator?(method)
|
|
19
|
+
OPERATOR_METHODS.include?(method)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Metz
|
|
6
|
+
# Collects public method definition nodes on a class body (instance,
|
|
7
|
+
# singleton, and `class << self`), honoring visibility macros via
|
|
8
|
+
# RuboCop's VisibilityHelp. Used by operation / service shape cops.
|
|
9
|
+
class PublicApiMethods
|
|
10
|
+
include VisibilityHelp
|
|
11
|
+
|
|
12
|
+
def initialize(class_node, allowed_methods: %i[initialize])
|
|
13
|
+
@class_node = class_node
|
|
14
|
+
@allowed_methods = allowed_methods.map(&:to_sym).freeze
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def names
|
|
18
|
+
definition_nodes.filter_map { |node| public_api_name(node) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def count
|
|
22
|
+
names.size
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
attr_reader :class_node, :allowed_methods
|
|
28
|
+
|
|
29
|
+
def public_api_name(node)
|
|
30
|
+
return unless node_visibility(node) == :public
|
|
31
|
+
|
|
32
|
+
name = node.method_name
|
|
33
|
+
return if allowed_methods.include?(name)
|
|
34
|
+
|
|
35
|
+
name
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def definition_nodes
|
|
39
|
+
direct_children(class_node).flat_map { |child| definitions_from(child) }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def definitions_from(node)
|
|
43
|
+
return [node] if node.def_type? || node.defs_type?
|
|
44
|
+
return inline_visibility_defs(node) if visibility_inline_on_def?(node)
|
|
45
|
+
return sclass_definitions(node) if node.sclass_type?
|
|
46
|
+
|
|
47
|
+
[]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def inline_visibility_defs(node)
|
|
51
|
+
node.arguments.select { |argument| argument.def_type? || argument.defs_type? }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def sclass_definitions(sclass_node)
|
|
55
|
+
direct_children(sclass_node).flat_map { |child| definitions_from(child) }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def direct_children(node)
|
|
59
|
+
body = node.body
|
|
60
|
+
return [] unless body
|
|
61
|
+
|
|
62
|
+
body.begin_type? ? body.children.compact : [body]
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rubocop"
|
|
4
|
+
require_relative "../../../metz/cop_metadata"
|
|
5
|
+
require_relative "on_send_csend_bridge"
|
|
6
|
+
|
|
7
|
+
module RuboCop
|
|
8
|
+
module Cop
|
|
9
|
+
module Metz
|
|
10
|
+
# Flags tests that bind expectations to internal state instead of
|
|
11
|
+
# observable behavior.
|
|
12
|
+
class TestAssertsOnInternals < RuboCop::Cop::Base
|
|
13
|
+
extend ::Metz::CopMetadata
|
|
14
|
+
include OnSendCsendBridge
|
|
15
|
+
|
|
16
|
+
INTERNAL_STATE_METHODS = %i[instance_variable_get instance_variable_set].freeze
|
|
17
|
+
|
|
18
|
+
MSG = "Test asserts on internal state via `%<method>s`; assert on observable " \
|
|
19
|
+
"behavior through the public interface instead."
|
|
20
|
+
|
|
21
|
+
why_it_matters "Tests that bind to implementation state break when safe refactors " \
|
|
22
|
+
"preserve behavior but change internal representation."
|
|
23
|
+
fix_safety :manual
|
|
24
|
+
suggested_next_moves [
|
|
25
|
+
"Assert on observable behavior through the public interface.",
|
|
26
|
+
"If callers need the value, expose a public reader designed for them.",
|
|
27
|
+
"For controller tests, assert on response or rendered output instead of assigned ivars."
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
def on_send(node)
|
|
31
|
+
return unless internal_state_access?(node)
|
|
32
|
+
return if allowed_receiver?(node)
|
|
33
|
+
|
|
34
|
+
add_offense(node.loc.selector, message: message(node))
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def internal_state_access?(node)
|
|
40
|
+
internal_state_method?(node) || receiverless_literal_assigns?(node)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def internal_state_method?(node)
|
|
44
|
+
INTERNAL_STATE_METHODS.include?(node.method_name)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def receiverless_literal_assigns?(node)
|
|
48
|
+
receiverless_assigns?(node) && literal_assigns_name?(node.arguments.first)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def receiverless_assigns?(node)
|
|
52
|
+
node.method_name == :assigns && !node.receiver
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def literal_assigns_name?(arg)
|
|
56
|
+
arg&.sym_type? || arg&.str_type?
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def allowed_receiver?(node)
|
|
60
|
+
receiver = receiver_name(node)
|
|
61
|
+
receiver && allowed_receivers.include?(receiver)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def message(node)
|
|
65
|
+
format(MSG, method: node.method_name)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def receiver_name(node)
|
|
69
|
+
receiver = node.receiver
|
|
70
|
+
return nil unless receiver
|
|
71
|
+
|
|
72
|
+
receiver.const_type? ? receiver.const_name : receiver.source
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def allowed_receivers
|
|
76
|
+
@allowed_receivers ||= Array(cop_config["AllowedReceivers"]).map(&:to_s)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rubocop"
|
|
4
|
+
require_relative "../../../metz/cop_metadata"
|
|
5
|
+
require_relative "on_send_csend_bridge"
|
|
6
|
+
require_relative "operator_methods"
|
|
7
|
+
|
|
8
|
+
module RuboCop
|
|
9
|
+
module Cop
|
|
10
|
+
module Metz
|
|
11
|
+
# Flags tests that bypass Ruby visibility with literal `send` calls. This
|
|
12
|
+
# is a mechanism signal only: AST inspection cannot prove the target
|
|
13
|
+
# method is private, so the message stays about interface reach-through.
|
|
14
|
+
class TestReachesPrivate < RuboCop::Cop::Base
|
|
15
|
+
extend ::Metz::CopMetadata
|
|
16
|
+
include OnSendCsendBridge
|
|
17
|
+
|
|
18
|
+
BYPASS_METHODS = %i[send __send__].freeze
|
|
19
|
+
|
|
20
|
+
MSG = "Test reaches past the public interface with `%<method>s(:%<name>s)`; " \
|
|
21
|
+
"`send` bypasses method visibility — exercise the object through its public API."
|
|
22
|
+
|
|
23
|
+
why_it_matters "Tests that reach around the public interface couple to implementation " \
|
|
24
|
+
"details instead of documenting observable behavior."
|
|
25
|
+
fix_safety :manual
|
|
26
|
+
suggested_next_moves [
|
|
27
|
+
"Exercise the object through its public API.",
|
|
28
|
+
"If the private method deserves its own test, extract it into its own public object.",
|
|
29
|
+
"Add legitimate metaprogramming names to `AllowedMethods`."
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
def on_send(node)
|
|
33
|
+
return unless literal_bypass?(node)
|
|
34
|
+
return if allowed?(node)
|
|
35
|
+
|
|
36
|
+
add_offense(node.loc.selector, message: message(node))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def literal_bypass?(node)
|
|
42
|
+
bypass_send?(node) && literal_name?(method_name_arg(node))
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def bypass_send?(node)
|
|
46
|
+
BYPASS_METHODS.include?(node.method_name)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def literal_name?(arg)
|
|
50
|
+
arg&.sym_type? || arg&.str_type?
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def allowed?(node)
|
|
54
|
+
allowed_method?(method_name(node)) ||
|
|
55
|
+
operator_method?(method_name(node)) ||
|
|
56
|
+
allowed_receiver?(node)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def allowed_method?(name)
|
|
60
|
+
allowed_methods.include?(name)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def operator_method?(name)
|
|
64
|
+
OperatorMethods.operator?(name.to_sym)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def allowed_receiver?(node)
|
|
68
|
+
receiver = receiver_name(node)
|
|
69
|
+
receiver && allowed_receivers.include?(receiver)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def message(node)
|
|
73
|
+
format(MSG, method: node.method_name, name: method_name(node))
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def method_name(node)
|
|
77
|
+
method_name_arg(node).value.to_s
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def method_name_arg(node)
|
|
81
|
+
node.arguments.first
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def receiver_name(node)
|
|
85
|
+
receiver = node.receiver
|
|
86
|
+
return nil unless receiver
|
|
87
|
+
|
|
88
|
+
receiver.const_type? ? receiver.const_name : receiver.source
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def allowed_methods
|
|
92
|
+
@allowed_methods ||= Array(cop_config["AllowedMethods"]).map(&:to_s)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def allowed_receivers
|
|
96
|
+
@allowed_receivers ||= Array(cop_config["AllowedReceivers"]).map(&:to_s)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rubocop"
|
|
4
|
+
require_relative "../../../metz/cop_metadata"
|
|
5
|
+
require_relative "on_send_csend_bridge"
|
|
6
|
+
|
|
7
|
+
module RuboCop
|
|
8
|
+
module Cop
|
|
9
|
+
module Metz
|
|
10
|
+
# Flags RSpec expectations that stub or mock the subject under test.
|
|
11
|
+
class TestStubsSubject < RuboCop::Cop::Base
|
|
12
|
+
extend ::Metz::CopMetadata
|
|
13
|
+
include OnSendCsendBridge
|
|
14
|
+
|
|
15
|
+
EXAMPLE_GROUP_METHODS = %i[describe context feature example_group xdescribe xcontext
|
|
16
|
+
fdescribe fcontext shared_examples shared_examples_for
|
|
17
|
+
shared_context].freeze
|
|
18
|
+
SUBJECT_DEFINERS = %i[subject subject!].freeze
|
|
19
|
+
LET_DEFINERS = %i[let let!].freeze
|
|
20
|
+
EXPECTATION_METHODS = %i[expect allow].freeze
|
|
21
|
+
RUNNERS = %i[to to_not not_to].freeze
|
|
22
|
+
STUB_MATCHERS = %i[receive receive_messages receive_message_chain have_received].freeze
|
|
23
|
+
|
|
24
|
+
MSG = "Test stubs the subject under test; stub the subject's collaborators, " \
|
|
25
|
+
"not the object whose behavior the test verifies."
|
|
26
|
+
|
|
27
|
+
why_it_matters "Tests that stub the subject replace the behavior they claim " \
|
|
28
|
+
"to verify, so they document implementation setup instead " \
|
|
29
|
+
"of observable design."
|
|
30
|
+
fix_safety :manual
|
|
31
|
+
suggested_next_moves [
|
|
32
|
+
"Exercise the subject's real behavior; stub only its collaborators.",
|
|
33
|
+
"If a dependency needs controlling, inject and stub the collaborator, not the subject.",
|
|
34
|
+
"If the method you want to stub is doing too much, extract it into a collaborator you can stub honestly."
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
def on_send(node)
|
|
38
|
+
return unless subject_stub_expectation?(node)
|
|
39
|
+
|
|
40
|
+
add_offense(node.loc.selector, message: MSG)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def subject_stub_expectation?(node)
|
|
46
|
+
return false unless RUNNERS.include?(node.method_name)
|
|
47
|
+
|
|
48
|
+
subject_name = expectation_subject_name(node.receiver)
|
|
49
|
+
subject_name && in_scope_subject_names(node).include?(subject_name) &&
|
|
50
|
+
node.arguments.any? { |argument| contains_stub_matcher?(argument) }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def expectation_subject_name(receiver)
|
|
54
|
+
return unless receiver&.send_type?
|
|
55
|
+
return :subject if receiverless_call?(receiver, :is_expected) && receiver.arguments.empty?
|
|
56
|
+
return unless EXPECTATION_METHODS.any? { |method| receiverless_call?(receiver, method) }
|
|
57
|
+
|
|
58
|
+
bare_call_name(receiver.arguments.first)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def bare_call_name(node)
|
|
62
|
+
return unless node&.send_type?
|
|
63
|
+
return if node.receiver
|
|
64
|
+
|
|
65
|
+
node.method_name
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def receiverless_call?(node, method_name)
|
|
69
|
+
node.method_name == method_name && !node.receiver
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def contains_stub_matcher?(node)
|
|
73
|
+
node && (stub_matcher?(node) ||
|
|
74
|
+
node.each_descendant(:send).any? { |descendant| stub_matcher?(descendant) })
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def stub_matcher?(node)
|
|
78
|
+
node.send_type? && !node.receiver && STUB_MATCHERS.include?(node.method_name)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def in_scope_subject_names(node)
|
|
82
|
+
groups = node.each_ancestor(:block).select { |ancestor| example_group_block?(ancestor) }.reverse
|
|
83
|
+
names = groups.reduce([]) { |subject_names, group| fold_subject_names(subject_names, group) }
|
|
84
|
+
names | [:subject]
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def fold_subject_names(names, group)
|
|
88
|
+
(names | subject_names_defined_directly_in(group)) -
|
|
89
|
+
let_override_names_defined_directly_in(group)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def example_group_block?(node)
|
|
93
|
+
send_node = node.send_node
|
|
94
|
+
|
|
95
|
+
EXAMPLE_GROUP_METHODS.include?(send_node.method_name) && example_group_receiver?(send_node.receiver)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def example_group_receiver?(receiver)
|
|
99
|
+
!receiver || (receiver.const_type? && receiver.const_name == "RSpec")
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def subject_names_defined_directly_in(group)
|
|
103
|
+
group.each_descendant(:block).filter_map do |block|
|
|
104
|
+
next unless nearest_example_group(block) == group
|
|
105
|
+
next unless definer_block?(block, SUBJECT_DEFINERS)
|
|
106
|
+
|
|
107
|
+
subject_name(block.send_node)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def let_override_names_defined_directly_in(group)
|
|
112
|
+
group.each_descendant(:block).filter_map do |block|
|
|
113
|
+
next unless nearest_example_group(block) == group
|
|
114
|
+
next unless definer_block?(block, LET_DEFINERS)
|
|
115
|
+
|
|
116
|
+
literal_symbol_name(block.send_node.arguments.first)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def nearest_example_group(node)
|
|
121
|
+
node.each_ancestor(:block).find { |ancestor| example_group_block?(ancestor) }
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def definer_block?(block, methods)
|
|
125
|
+
methods.include?(block.send_node.method_name) && !block.send_node.receiver
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def subject_name(send_node)
|
|
129
|
+
send_node.arguments.empty? ? :subject : literal_symbol_name(send_node.arguments.first)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def literal_symbol_name(node)
|
|
133
|
+
node.value if node&.sym_type?
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
@@ -33,11 +33,10 @@ module RuboCop
|
|
|
33
33
|
|
|
34
34
|
# RuboCop's dispatcher needs the subclass to own the callback, while the
|
|
35
35
|
# inherited implementation remains the shared chain analyzer.
|
|
36
|
-
# rubocop:disable Lint/UselessMethodDefinition -- required for subclass callback registration.
|
|
36
|
+
# rubocop:disable-next Lint/UselessMethodDefinition -- required for subclass callback registration.
|
|
37
37
|
def on_send(node)
|
|
38
38
|
super
|
|
39
39
|
end
|
|
40
|
-
# rubocop:enable Lint/UselessMethodDefinition
|
|
41
40
|
|
|
42
41
|
def relevant_file?(file)
|
|
43
42
|
return super if file.nil? || file.empty? || file == "(string)"
|
|
@@ -9,4 +9,9 @@ require_relative "metz/methods_too_long"
|
|
|
9
9
|
require_relative "metz/methods_too_many_parameters"
|
|
10
10
|
require_relative "metz/demeter_train_wreck"
|
|
11
11
|
require_relative "metz/controllers_too_many_direct_collaborators"
|
|
12
|
+
require_relative "metz/god_service_class"
|
|
13
|
+
require_relative "metz/operations_too_many_public_methods"
|
|
14
|
+
require_relative "metz/test_asserts_on_internals"
|
|
15
|
+
require_relative "metz/test_reaches_private"
|
|
16
|
+
require_relative "metz/test_stubs_subject"
|
|
12
17
|
require_relative "metz/views_deep_navigation"
|
data/lib/rubocop/metz/version.rb
CHANGED
data/rubocop-metz.gemspec
CHANGED
|
@@ -17,26 +17,24 @@ Gem::Specification.new do |spec|
|
|
|
17
17
|
spec.required_ruby_version = ">= 3.3"
|
|
18
18
|
|
|
19
19
|
spec.metadata["source_code_uri"] = spec.homepage
|
|
20
|
+
spec.metadata["documentation_uri"] = "#{spec.homepage}/blob/main/README.md"
|
|
20
21
|
spec.metadata["changelog_uri"] = "#{spec.homepage}/releases"
|
|
21
22
|
spec.metadata["bug_tracker_uri"] = "#{spec.homepage}/issues"
|
|
22
23
|
spec.metadata["github_repo"] = "ssh://github.com/fuentesjr/metz-scan"
|
|
23
24
|
spec.metadata["github_package_uri"] = "https://github.com/users/fuentesjr/packages/rubygems/package/rubocop-metz"
|
|
24
25
|
spec.metadata["default_lint_roller_plugin"] = "RuboCop::Metz::Plugin"
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
raise "rubocop-metz.gemspec: lib/rubocop-metz.rb missing from packaged files; " \
|
|
35
|
-
"build from rubocop-metz/ (cd rubocop-metz && gem build rubocop-metz.gemspec)."
|
|
36
|
-
end
|
|
27
|
+
# Resolve files from the gemspec directory so evaluation is CWD-independent.
|
|
28
|
+
# Do not raise when the entrypoint is absent: Dependabot evaluates gemspecs in
|
|
29
|
+
# a sparse tree that only materializes require_relative targets (#41). Package
|
|
30
|
+
# completeness is checked by release_metadata_test against the full checkout.
|
|
31
|
+
gem_root = __dir__
|
|
32
|
+
spec.files = Dir.glob("{lib,config}/**/*", File::FNM_DOTMATCH, base: gem_root)
|
|
33
|
+
.reject { |f| File.directory?(File.join(gem_root, f)) } +
|
|
34
|
+
["LICENSE", "rubocop-metz.gemspec"].select { |f| File.exist?(File.join(gem_root, f)) }
|
|
37
35
|
spec.require_paths = ["lib"]
|
|
38
36
|
|
|
39
37
|
spec.add_dependency "lint_roller", "~> 1.1"
|
|
40
|
-
spec.add_dependency "rubocop", "~> 1.
|
|
38
|
+
spec.add_dependency "rubocop", "~> 1.90"
|
|
41
39
|
spec.add_dependency "rubocop-ast", "~> 1.49"
|
|
42
40
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rubocop-metz
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Salvador Fuentes Jr
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 2026-10-04 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: lint_roller
|
|
@@ -29,14 +29,14 @@ dependencies:
|
|
|
29
29
|
requirements:
|
|
30
30
|
- - "~>"
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: '1.
|
|
32
|
+
version: '1.90'
|
|
33
33
|
type: :runtime
|
|
34
34
|
prerelease: false
|
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
37
|
- - "~>"
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: '1.
|
|
39
|
+
version: '1.90'
|
|
40
40
|
- !ruby/object:Gem::Dependency
|
|
41
41
|
name: rubocop-ast
|
|
42
42
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -72,9 +72,16 @@ files:
|
|
|
72
72
|
- lib/rubocop/cop/metz/controllers_too_many_direct_collaborators.rb
|
|
73
73
|
- lib/rubocop/cop/metz/demeter_train_wreck.rb
|
|
74
74
|
- lib/rubocop/cop/metz/demeter_train_wreck/type_inference.rb
|
|
75
|
+
- lib/rubocop/cop/metz/god_service_class.rb
|
|
75
76
|
- lib/rubocop/cop/metz/methods_too_long.rb
|
|
76
77
|
- lib/rubocop/cop/metz/methods_too_many_parameters.rb
|
|
77
78
|
- lib/rubocop/cop/metz/on_send_csend_bridge.rb
|
|
79
|
+
- lib/rubocop/cop/metz/operations_too_many_public_methods.rb
|
|
80
|
+
- lib/rubocop/cop/metz/operator_methods.rb
|
|
81
|
+
- lib/rubocop/cop/metz/public_api_methods.rb
|
|
82
|
+
- lib/rubocop/cop/metz/test_asserts_on_internals.rb
|
|
83
|
+
- lib/rubocop/cop/metz/test_reaches_private.rb
|
|
84
|
+
- lib/rubocop/cop/metz/test_stubs_subject.rb
|
|
78
85
|
- lib/rubocop/cop/metz/views_deep_navigation.rb
|
|
79
86
|
- lib/rubocop/cop/metz_cops.rb
|
|
80
87
|
- lib/rubocop/formatter/metz_json_formatter.rb
|
|
@@ -86,6 +93,7 @@ licenses:
|
|
|
86
93
|
- MIT
|
|
87
94
|
metadata:
|
|
88
95
|
source_code_uri: https://github.com/fuentesjr/metz-scan
|
|
96
|
+
documentation_uri: https://github.com/fuentesjr/metz-scan/blob/main/README.md
|
|
89
97
|
changelog_uri: https://github.com/fuentesjr/metz-scan/releases
|
|
90
98
|
bug_tracker_uri: https://github.com/fuentesjr/metz-scan/issues
|
|
91
99
|
github_repo: ssh://github.com/fuentesjr/metz-scan
|
|
@@ -105,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
105
113
|
- !ruby/object:Gem::Version
|
|
106
114
|
version: '0'
|
|
107
115
|
requirements: []
|
|
108
|
-
rubygems_version:
|
|
116
|
+
rubygems_version: 3.6.2
|
|
109
117
|
specification_version: 4
|
|
110
118
|
summary: Sandi-Metz-inspired RuboCop cops.
|
|
111
119
|
test_files: []
|