rubocop-vicenzo 0.9.0 → 0.11.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/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +36 -8
- data/README.md +1 -1
- data/config/default.yml +30 -1
- data/config/style.yml +19 -1
- data/lib/rubocop/cop/vicenzo/naming/short_name.rb +27 -1
- data/lib/rubocop/cop/vicenzo/rspec/let_aliasing_subject.rb +121 -0
- data/lib/rubocop/cop/vicenzo/rspec/let_is_method_result.rb +11 -30
- data/lib/rubocop/cop/vicenzo/rspec/mixin/described_method.rb +7 -21
- data/lib/rubocop/cop/vicenzo/rspec/mixin/subject_reference.rb +61 -0
- data/lib/rubocop/cop/vicenzo/rspec/nested_context_improper_start.rb +3 -4
- data/lib/rubocop/cop/vicenzo/rspec/returned_chain.rb +71 -0
- data/lib/rubocop/cop/vicenzo/rspec/scenario_describe.rb +184 -0
- data/lib/rubocop/cop/vicenzo/rspec/subject_is_method_result.rb +37 -5
- data/lib/rubocop/cop/vicenzo/rspec/unnested_context_improper_start.rb +3 -3
- data/lib/rubocop/cop/vicenzo/style/prefer_many_over_size_comparison.rb +77 -0
- data/lib/rubocop/cop/vicenzo_cops.rb +3 -0
- data/lib/rubocop/vicenzo/version.rb +1 -1
- metadata +6 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9bcae934be9ffca6f86007a710cdd35a9c610a7d5610b5c83960b12905c1a25b
|
|
4
|
+
data.tar.gz: a2b2cb6db0f9d177099b845caf9de7b87d71381efbc37041ca2f01f7c1f62e8b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3568dc18ce54dc425af8fad2e06f0d2fb23e52c190c886a8781d879f4f46f0120dd949eab9ead4f05510b280a97c2d7513a01f95264f8a8c9c9facf3fa03d09b
|
|
7
|
+
data.tar.gz: 93c229f5feb3bfc0b5ea551b57afe5910e48f94d3e44f9da82fc054297d7499936e22f1ff84a7e5f45a2c9b1464b8204f6d38af5f99cdb625d8cf2b51bf99c60
|
data/CHANGELOG.md
CHANGED
|
@@ -1,36 +1,64 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.11.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.10.0...v0.11.0) (2026-11-18)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add Vicenzo/RSpec/ScenarioDescribe cop ([#54](https://github.com/bvicenzo/rubocop-vicenzo/pull/54)) ([df050f7](https://github.com/bvicenzo/rubocop-vicenzo/commit/df050f7e013b029a009c7445b30de739132de4b4)), closes [#48](https://github.com/bvicenzo/rubocop-vicenzo/issues/48)
|
|
9
|
+
* allow whole short words in Vicenzo/Naming/ShortName via AllowedNames ([#51](https://github.com/bvicenzo/rubocop-vicenzo/pull/51)) ([972ff35](https://github.com/bvicenzo/rubocop-vicenzo/commit/972ff35e376b4364876b7638d2838d5593e69ea9)), closes [#47](https://github.com/bvicenzo/rubocop-vicenzo/issues/47)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
* remove 'however' from the context prefix vocabulary ([#49](https://github.com/bvicenzo/rubocop-vicenzo/pull/49)) ([0c46397](https://github.com/bvicenzo/rubocop-vicenzo/commit/0c46397b7fd04c943ae0f974d26f48cb80de17f0)), closes [#46](https://github.com/bvicenzo/rubocop-vicenzo/issues/46)
|
|
15
|
+
* report nested contexts whose forbidden first word is followed by punctuation ([#52](https://github.com/bvicenzo/rubocop-vicenzo/pull/52)) ([e103531](https://github.com/bvicenzo/rubocop-vicenzo/commit/e1035314d769f9e3a68cb6ba3d5ce98eab588bf4)), closes [#50](https://github.com/bvicenzo/rubocop-vicenzo/issues/50)
|
|
16
|
+
|
|
17
|
+
## [0.10.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.9.0...v0.10.0) (2026-10-23)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* add Vicenzo/RSpec/LetAliasingSubject cop ([#44](https://github.com/bvicenzo/rubocop-vicenzo/pull/44)) ([a8cebd3](https://github.com/bvicenzo/rubocop-vicenzo/commit/a8cebd3c0237e8805a58ff9efa7ca34f6beb34b0)), closes [#42](https://github.com/bvicenzo/rubocop-vicenzo/issues/42)
|
|
23
|
+
* add Vicenzo/Style/PreferManyOverSizeComparison cop ([#38](https://github.com/bvicenzo/rubocop-vicenzo/pull/38)) ([910ea02](https://github.com/bvicenzo/rubocop-vicenzo/commit/910ea026f7e4c46f9de27db580e6330cf845bfe7))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
### Bug Fixes
|
|
27
|
+
|
|
28
|
+
* report subjects that derive from an allowed method in SubjectIsMethodResult ([#41](https://github.com/bvicenzo/rubocop-vicenzo/pull/41)) ([a5efd33](https://github.com/bvicenzo/rubocop-vicenzo/commit/a5efd3330b32dd24b4291121a64e378091297a57)), closes [#40](https://github.com/bvicenzo/rubocop-vicenzo/issues/40)
|
|
29
|
+
* see subjects declared in shared groups in LetIsMethodResult and LetAliasingSubject ([#45](https://github.com/bvicenzo/rubocop-vicenzo/pull/45)) ([a98039e](https://github.com/bvicenzo/rubocop-vicenzo/commit/a98039e2cbea568267d8b7832b4717b763678d14)), closes [#43](https://github.com/bvicenzo/rubocop-vicenzo/issues/43)
|
|
30
|
+
|
|
3
31
|
## [0.9.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.8.0...v0.9.0) (2026-09-23)
|
|
4
32
|
|
|
5
33
|
|
|
6
34
|
### Features
|
|
7
35
|
|
|
8
|
-
* add Vicenzo/Naming/AnonymousUnusedName cop ([#33](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
9
|
-
* add Vicenzo/Naming/ShortName cop and an opinionated config/style.yml ([#35](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
10
|
-
* add Vicenzo/RSpec/DescribeInsideContext cop ([#37](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
11
|
-
* add Vicenzo/RSpec/UnnestedContextImproperStart cop ([#36](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
36
|
+
* add Vicenzo/Naming/AnonymousUnusedName cop ([#33](https://github.com/bvicenzo/rubocop-vicenzo/pull/33)) ([fabbad6](https://github.com/bvicenzo/rubocop-vicenzo/commit/fabbad655dac0c362924dd0aaf245abd2c7023e1))
|
|
37
|
+
* add Vicenzo/Naming/ShortName cop and an opinionated config/style.yml ([#35](https://github.com/bvicenzo/rubocop-vicenzo/pull/35)) ([1b67f46](https://github.com/bvicenzo/rubocop-vicenzo/commit/1b67f4643b4348f5768a8fbb3b88cea35046e5e2))
|
|
38
|
+
* add Vicenzo/RSpec/DescribeInsideContext cop ([#37](https://github.com/bvicenzo/rubocop-vicenzo/pull/37)) ([3f3d6ae](https://github.com/bvicenzo/rubocop-vicenzo/commit/3f3d6aeb95d087e32594e8ded3d1ce0e3715dee7))
|
|
39
|
+
* add Vicenzo/RSpec/UnnestedContextImproperStart cop ([#36](https://github.com/bvicenzo/rubocop-vicenzo/pull/36)) ([0a8fb0f](https://github.com/bvicenzo/rubocop-vicenzo/commit/0a8fb0fec62370ac331b9a3275b95a0b22998b1d))
|
|
12
40
|
|
|
13
41
|
## [0.8.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.7.0...v0.8.0) (2026-09-09)
|
|
14
42
|
|
|
15
43
|
|
|
16
44
|
### Features
|
|
17
45
|
|
|
18
|
-
* add RSpec cops against top-level declarations ([#31](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
46
|
+
* add RSpec cops against top-level declarations ([#31](https://github.com/bvicenzo/rubocop-vicenzo/pull/31)) ([ef5e207](https://github.com/bvicenzo/rubocop-vicenzo/commit/ef5e20728c0c4bc94dbb5eec54a48ff5197d1b25))
|
|
19
47
|
|
|
20
48
|
## [0.7.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.6.0...v0.7.0) (2026-08-31)
|
|
21
49
|
|
|
22
50
|
|
|
23
51
|
### Features
|
|
24
52
|
|
|
25
|
-
* add RSpec cops against subjects hidden in lets ([#30](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
26
|
-
* add Vicenzo/RSpec/SubjectIsMethodResult cop ([#28](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
53
|
+
* add RSpec cops against subjects hidden in lets ([#30](https://github.com/bvicenzo/rubocop-vicenzo/pull/30)) ([9daf259](https://github.com/bvicenzo/rubocop-vicenzo/commit/9daf25972252fdfdb92a51bba9cfe70e63446d17))
|
|
54
|
+
* add Vicenzo/RSpec/SubjectIsMethodResult cop ([#28](https://github.com/bvicenzo/rubocop-vicenzo/pull/28)) ([b69245a](https://github.com/bvicenzo/rubocop-vicenzo/commit/b69245af973f3341bbe62bca628333c5acb909d7))
|
|
27
55
|
|
|
28
56
|
## [0.6.0](https://github.com/bvicenzo/rubocop-vicenzo/compare/v0.5.0...v0.6.0) (2026-08-19)
|
|
29
57
|
|
|
30
58
|
|
|
31
59
|
### Features
|
|
32
60
|
|
|
33
|
-
* add RSpec cops against mutated and derived premises ([#26](https://github.com/bvicenzo/rubocop-vicenzo/
|
|
61
|
+
* add RSpec cops against mutated and derived premises ([#26](https://github.com/bvicenzo/rubocop-vicenzo/pull/26)) ([b138dd0](https://github.com/bvicenzo/rubocop-vicenzo/commit/b138dd00a76e1bcd62e95c1f2c51dca5b4232319))
|
|
34
62
|
|
|
35
63
|
## [0.5.0] - 2026-06-29
|
|
36
64
|
|
data/README.md
CHANGED
|
@@ -11,7 +11,7 @@ Add it to the `development`/`test` group of your `Gemfile`:
|
|
|
11
11
|
<!-- x-release-please-start-version -->
|
|
12
12
|
```ruby
|
|
13
13
|
group :development, :test do
|
|
14
|
-
gem 'rubocop-vicenzo', '~> 0.
|
|
14
|
+
gem 'rubocop-vicenzo', '~> 0.11.0', require: false
|
|
15
15
|
end
|
|
16
16
|
```
|
|
17
17
|
<!-- x-release-please-end-version -->
|
data/config/default.yml
CHANGED
|
@@ -16,6 +16,7 @@ Vicenzo/Naming/ShortName:
|
|
|
16
16
|
Enabled: true
|
|
17
17
|
Severity: convention
|
|
18
18
|
MinNameLength: 3
|
|
19
|
+
AllowedNames: []
|
|
19
20
|
VersionAdded: '0.9.0'
|
|
20
21
|
|
|
21
22
|
Vicenzo/Rails/EnumInclusionOfValidation:
|
|
@@ -127,6 +128,17 @@ Vicenzo/RSpec/LeakyDefinition:
|
|
|
127
128
|
- '**/spec/support/**/*'
|
|
128
129
|
- '**/spec/factories/**/*'
|
|
129
130
|
|
|
131
|
+
Vicenzo/RSpec/LetAliasingSubject:
|
|
132
|
+
Description: 'A let must not hold the subject itself under another name.'
|
|
133
|
+
Enabled: true
|
|
134
|
+
Severity: convention
|
|
135
|
+
Include:
|
|
136
|
+
- '**/spec/**/*_spec.rb'
|
|
137
|
+
Exclude:
|
|
138
|
+
- '**/spec/support/**/*'
|
|
139
|
+
- '**/spec/factories/**/*'
|
|
140
|
+
VersionAdded: '0.10.0'
|
|
141
|
+
|
|
130
142
|
Vicenzo/RSpec/LetIsMethodResult:
|
|
131
143
|
Description: 'A let under an example group describing a method must not hold the result of calling that method on the subject.'
|
|
132
144
|
Enabled: true
|
|
@@ -139,6 +151,17 @@ Vicenzo/RSpec/LetIsMethodResult:
|
|
|
139
151
|
- '**/spec/factories/**/*'
|
|
140
152
|
VersionAdded: '0.7.0'
|
|
141
153
|
|
|
154
|
+
Vicenzo/RSpec/ScenarioDescribe:
|
|
155
|
+
Description: 'A describe exercising an entry point a sibling describe already names is a scenario of it, and belongs in a context inside that describe.'
|
|
156
|
+
Enabled: true
|
|
157
|
+
Severity: warning
|
|
158
|
+
Include:
|
|
159
|
+
- '**/spec/**/*_spec.rb'
|
|
160
|
+
Exclude:
|
|
161
|
+
- '**/spec/support/**/*'
|
|
162
|
+
- '**/spec/factories/**/*'
|
|
163
|
+
VersionAdded: '0.11.0'
|
|
164
|
+
|
|
142
165
|
Vicenzo/RSpec/SubjectDefinedAsLet:
|
|
143
166
|
Description: 'A let the expectations assert on, in a group declaring no subject, must be declared as the subject.'
|
|
144
167
|
Enabled: true
|
|
@@ -225,7 +248,6 @@ Vicenzo/RSpec/UnnestedContextImproperStart:
|
|
|
225
248
|
ForbiddenPrefixes:
|
|
226
249
|
- and
|
|
227
250
|
- but
|
|
228
|
-
- however
|
|
229
251
|
Include:
|
|
230
252
|
- '**/spec/**/*_spec.rb'
|
|
231
253
|
Exclude:
|
|
@@ -244,3 +266,10 @@ Vicenzo/Style/MultilineMethodCallParentheses:
|
|
|
244
266
|
Enabled: true
|
|
245
267
|
AllowedMethods: []
|
|
246
268
|
VersionAdded: '0.3.0'
|
|
269
|
+
|
|
270
|
+
Vicenzo/Style/PreferManyOverSizeComparison:
|
|
271
|
+
Description: 'Use many? instead of comparing size or length with one.'
|
|
272
|
+
Enabled: true
|
|
273
|
+
Severity: convention
|
|
274
|
+
SafeAutoCorrect: false
|
|
275
|
+
VersionAdded: '0.10.0'
|
data/config/style.yml
CHANGED
|
@@ -130,11 +130,29 @@ Naming/RescuedExceptionsVariableName:
|
|
|
130
130
|
Enabled: true
|
|
131
131
|
PreferredName: error
|
|
132
132
|
|
|
133
|
+
# Short names that are whole English words, such as `to` as the upper bound of a
|
|
134
|
+
# range. Only whole words belong here: the abbreviations in the core
|
|
135
|
+
# Naming/MethodParameterName default (`db`, `io`, `ip`, `os`, `pp`, `cc`) are left
|
|
136
|
+
# out on purpose, since they are exactly what the rule is meant to catch.
|
|
137
|
+
Vicenzo/Naming/ShortName:
|
|
138
|
+
AllowedNames:
|
|
139
|
+
- to
|
|
140
|
+
- at
|
|
141
|
+
- by
|
|
142
|
+
- of
|
|
143
|
+
- on
|
|
144
|
+
|
|
133
145
|
# Vicenzo/Naming/ShortName covers local variables. Block parameters are the core
|
|
134
146
|
# cop's job, but it ships with MinNameLength: 1 and therefore never fires — raise
|
|
135
|
-
# it so both halves of the rule agree.
|
|
147
|
+
# it, and allow the same whole words, so both halves of the rule agree.
|
|
136
148
|
Naming/BlockParameterName:
|
|
137
149
|
MinNameLength: 3
|
|
150
|
+
AllowedNames:
|
|
151
|
+
- to
|
|
152
|
+
- at
|
|
153
|
+
- by
|
|
154
|
+
- of
|
|
155
|
+
- on
|
|
138
156
|
|
|
139
157
|
###### METRICS #####
|
|
140
158
|
|
|
@@ -26,6 +26,12 @@ module RuboCop
|
|
|
26
26
|
# chosen, and it is that signature the parameter cops point at. The fix
|
|
27
27
|
# cascades from there.
|
|
28
28
|
#
|
|
29
|
+
# `AllowedNames` accepts short names that are whole words of the domain,
|
|
30
|
+
# such as `to` as the upper bound of a range. It is not a place for
|
|
31
|
+
# abbreviations (`db`, `io`): those are exactly what the cop is meant to
|
|
32
|
+
# catch. Leading underscores are ignored when matching, so `_to` is
|
|
33
|
+
# allowed along with `to`.
|
|
34
|
+
#
|
|
29
35
|
# @example
|
|
30
36
|
# # bad
|
|
31
37
|
# v = shelf.capacity
|
|
@@ -71,6 +77,14 @@ module RuboCop
|
|
|
71
77
|
#
|
|
72
78
|
# # good
|
|
73
79
|
# book_title = build
|
|
80
|
+
#
|
|
81
|
+
# @example AllowedNames: [to]
|
|
82
|
+
# # bad
|
|
83
|
+
# at = schedule.first
|
|
84
|
+
#
|
|
85
|
+
# # good
|
|
86
|
+
# from = options.fetch(:from)
|
|
87
|
+
# to = options.fetch(:to)
|
|
74
88
|
class ShortName < Base
|
|
75
89
|
MSG = 'Name `%<name>s` spells %<length>d letter(s); at least %<minimum>d are required. ' \
|
|
76
90
|
'A letter or an abbreviation carries no meaning to the next reader — write the ' \
|
|
@@ -81,11 +95,13 @@ module RuboCop
|
|
|
81
95
|
|
|
82
96
|
ANONYMOUS_NAME_PATTERN = /\A_+\z/
|
|
83
97
|
|
|
98
|
+
LEADING_UNDERSCORES_PATTERN = /\A_+/
|
|
99
|
+
|
|
84
100
|
SCOPE_TYPES = %i[def defs block numblock itblock].freeze
|
|
85
101
|
|
|
86
102
|
def on_lvasgn(node)
|
|
87
103
|
name = node.children.first
|
|
88
|
-
return if anonymous?(name) || rescued_error?(node) || forwarded_by_shorthand?(node, name)
|
|
104
|
+
return if anonymous?(name) || allowed?(name) || rescued_error?(node) || forwarded_by_shorthand?(node, name)
|
|
89
105
|
|
|
90
106
|
length = spelled_length(name)
|
|
91
107
|
return if length >= min_name_length
|
|
@@ -105,6 +121,12 @@ module RuboCop
|
|
|
105
121
|
ANONYMOUS_NAME_PATTERN.match?(name.to_s)
|
|
106
122
|
end
|
|
107
123
|
|
|
124
|
+
# Leading underscores are stripped before the lookup, as the core
|
|
125
|
+
# `Naming/MethodParameterName` does, so `_to` is allowed along with `to`.
|
|
126
|
+
def allowed?(name)
|
|
127
|
+
allowed_names.include?(name.to_s.sub(LEADING_UNDERSCORES_PATTERN, ''))
|
|
128
|
+
end
|
|
129
|
+
|
|
108
130
|
def rescued_error?(node)
|
|
109
131
|
parent = node.parent
|
|
110
132
|
parent&.resbody_type? && parent.exception_variable == node
|
|
@@ -126,6 +148,10 @@ module RuboCop
|
|
|
126
148
|
def min_name_length
|
|
127
149
|
cop_config.fetch('MinNameLength', DEFAULT_MIN_NAME_LENGTH)
|
|
128
150
|
end
|
|
151
|
+
|
|
152
|
+
def allowed_names
|
|
153
|
+
cop_config.fetch('AllowedNames', [])
|
|
154
|
+
end
|
|
129
155
|
end
|
|
130
156
|
end
|
|
131
157
|
end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'returned_chain'
|
|
4
|
+
require_relative 'mixin/premise_tracking'
|
|
5
|
+
require_relative 'mixin/subject_reference'
|
|
6
|
+
|
|
7
|
+
module RuboCop
|
|
8
|
+
module Cop
|
|
9
|
+
module Vicenzo
|
|
10
|
+
module RSpec
|
|
11
|
+
# A `let` whose value is the subject itself is the subject under another name. Refer to the subject by
|
|
12
|
+
# its own name instead.
|
|
13
|
+
#
|
|
14
|
+
# The subject is who the sentences are about. A second name for it splits that across two
|
|
15
|
+
# declarations: the examples reading `hunter` no longer say they are about the subject, and every cop
|
|
16
|
+
# that recognises the subject by its name stops recognising it there. In "the cat caught the ball",
|
|
17
|
+
# calling the cat `hunter` in half of the sentences does not add a character - it hides one.
|
|
18
|
+
#
|
|
19
|
+
# A subject that was declared without a name and is then given one by a `let` is the same thing the
|
|
20
|
+
# other way round: the name belongs to the subject's declaration, `subject(:hunter) { ... }`. Inside a
|
|
21
|
+
# shared group that declares no named subject, the declaration lives where the group is included, so
|
|
22
|
+
# the subject is referred to as `subject`.
|
|
23
|
+
#
|
|
24
|
+
# This composes with `Vicenzo/RSpec/LetIsMethodResult`: once the alias is gone, a `let` that called
|
|
25
|
+
# the described method through it calls it on the subject, and that cop reports it.
|
|
26
|
+
#
|
|
27
|
+
# Only a value that is the subject and nothing more counts. A `let` deriving something from the
|
|
28
|
+
# subject, such as `cat.name`, is a premise, not an alias.
|
|
29
|
+
#
|
|
30
|
+
# @example
|
|
31
|
+
# # bad - `hunter` is the cat
|
|
32
|
+
# describe '#catch' do
|
|
33
|
+
# subject(:cat) { Cat.new(name: 'Bixano') }
|
|
34
|
+
#
|
|
35
|
+
# let(:hunter) { cat }
|
|
36
|
+
#
|
|
37
|
+
# it { expect(hunter.catch(object: Ball.new)).to eq(:success) }
|
|
38
|
+
# end
|
|
39
|
+
#
|
|
40
|
+
# # good - one name for the subject
|
|
41
|
+
# describe '#catch' do
|
|
42
|
+
# subject(:cat) { Cat.new(name: 'Bixano') }
|
|
43
|
+
#
|
|
44
|
+
# it { expect(cat.catch(object: Ball.new)).to eq(:success) }
|
|
45
|
+
# end
|
|
46
|
+
#
|
|
47
|
+
# @example an anonymous subject named by a let
|
|
48
|
+
# # bad - the name belongs to the subject's declaration
|
|
49
|
+
# describe '#catch' do
|
|
50
|
+
# subject { Cat.new(name: 'Bixano') }
|
|
51
|
+
#
|
|
52
|
+
# let(:hunter) { subject }
|
|
53
|
+
# end
|
|
54
|
+
#
|
|
55
|
+
# # good
|
|
56
|
+
# describe '#catch' do
|
|
57
|
+
# subject(:hunter) { Cat.new(name: 'Bixano') }
|
|
58
|
+
# end
|
|
59
|
+
#
|
|
60
|
+
# @example a shared group naming the subject of whoever includes it
|
|
61
|
+
# # bad - the subject is declared where the group is included, under a name it cannot know
|
|
62
|
+
# shared_examples 'a hunter' do
|
|
63
|
+
# let(:hunter) { subject }
|
|
64
|
+
#
|
|
65
|
+
# it { expect(hunter.catch(object: Ball.new)).to eq(:success) }
|
|
66
|
+
# end
|
|
67
|
+
#
|
|
68
|
+
# # good - `subject` holds wherever the group is included
|
|
69
|
+
# shared_examples 'a hunter' do
|
|
70
|
+
# it { expect(subject.catch(object: Ball.new)).to eq(:success) }
|
|
71
|
+
# end
|
|
72
|
+
class LetAliasingSubject < RuboCop::Cop::RSpec::Base
|
|
73
|
+
include PremiseTracking
|
|
74
|
+
include SubjectReference
|
|
75
|
+
|
|
76
|
+
MSG = 'Let `:%<name>s` is the subject under another name. Refer to the subject as `%<subject>s`.'
|
|
77
|
+
UNNAMED_MSG = 'Let `:%<name>s` is the subject under another name. Name the subject `%<name>s` instead.'
|
|
78
|
+
|
|
79
|
+
def on_block(node)
|
|
80
|
+
return unless let?(node)
|
|
81
|
+
|
|
82
|
+
reference = sole_call(node.body)
|
|
83
|
+
return unless reference
|
|
84
|
+
|
|
85
|
+
names = subject_names(node)
|
|
86
|
+
return unless subject_reference?(reference, names)
|
|
87
|
+
|
|
88
|
+
add_offense(node.send_node, message: message(node, reference.method_name, names))
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
alias on_numblock on_block
|
|
92
|
+
alias on_itblock on_block
|
|
93
|
+
|
|
94
|
+
private
|
|
95
|
+
|
|
96
|
+
def sole_call(body)
|
|
97
|
+
calls = ReturnedChain.new(body).to_a
|
|
98
|
+
|
|
99
|
+
calls.first if calls.one?
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# `subject` points at the nearest named subject when there is one; without it, the alias is the name
|
|
103
|
+
# the subject was missing. Inside a shared group the subject is declared where the group is included,
|
|
104
|
+
# under a name this group cannot know, so `subject` is the one name that holds everywhere.
|
|
105
|
+
def message(node, reference_name, names)
|
|
106
|
+
name = premise_name(node)
|
|
107
|
+
subject = reference_name == :subject ? names.find { |candidate| candidate != :subject } : reference_name
|
|
108
|
+
return format(MSG, name:, subject:) if subject
|
|
109
|
+
return format(MSG, name:, subject: :subject) if inside_shared_group?(node)
|
|
110
|
+
|
|
111
|
+
format(UNNAMED_MSG, name:)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def inside_shared_group?(node)
|
|
115
|
+
node.each_ancestor(:any_block).any? { |ancestor| shared_group?(ancestor) }
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative 'mixin/described_method'
|
|
4
4
|
require_relative 'mixin/premise_tracking'
|
|
5
|
+
require_relative 'mixin/subject_reference'
|
|
5
6
|
|
|
6
7
|
module RuboCop
|
|
7
8
|
module Cop
|
|
@@ -21,8 +22,10 @@ module RuboCop
|
|
|
21
22
|
#
|
|
22
23
|
# Only calls on the subject count, which is what separates the result from the setup: a `let`
|
|
23
24
|
# calling the described method on some other object is building a premise, not the outcome the
|
|
24
|
-
# example is about. `
|
|
25
|
-
#
|
|
25
|
+
# example is about. A `let` that only renames the subject is `Vicenzo/RSpec/LetAliasingSubject`'s
|
|
26
|
+
# to report; once it is gone, the call goes through the subject's own name and is reported here.
|
|
27
|
+
# `AllowedMethods` is shared with the sibling cop and ships empty, for the layers whose convention
|
|
28
|
+
# is a single entry point.
|
|
26
29
|
#
|
|
27
30
|
# @example
|
|
28
31
|
# # bad - the expectation asserts on a name; the cat never catches anything in the example
|
|
@@ -51,6 +54,7 @@ module RuboCop
|
|
|
51
54
|
class LetIsMethodResult < RuboCop::Cop::RSpec::Base
|
|
52
55
|
include DescribedMethod
|
|
53
56
|
include PremiseTracking
|
|
57
|
+
include SubjectReference
|
|
54
58
|
|
|
55
59
|
MSG = 'Let `:%<name>s` holds what `%<method>s` returned. Undo it and call `%<method>s` on the ' \
|
|
56
60
|
'subject inside the expectation.'
|
|
@@ -72,39 +76,16 @@ module RuboCop
|
|
|
72
76
|
|
|
73
77
|
private
|
|
74
78
|
|
|
79
|
+
# The receiver is read from the chain itself, the call right after the invocation, so that a subject
|
|
80
|
+
# passed along through a local variable or parentheses is still the subject.
|
|
75
81
|
def invocation_on_subject(node, method_name)
|
|
76
82
|
names = subject_names(node)
|
|
77
83
|
|
|
78
|
-
|
|
79
|
-
|
|
84
|
+
call, = ReturnedChain.new(node.body).each_cons(2).find do |invocation, receiver|
|
|
85
|
+
invocation.method?(method_name) && subject_reference?(receiver, names)
|
|
80
86
|
end
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
# `subject` always refers to the subject, declared or implicit; a named one answers to its name
|
|
84
|
-
# as well.
|
|
85
|
-
def subject_names(node)
|
|
86
|
-
names = Set[:subject]
|
|
87
|
-
|
|
88
|
-
node.each_ancestor(:any_block).each do |ancestor|
|
|
89
|
-
next unless example_group?(ancestor)
|
|
90
|
-
|
|
91
|
-
each_child_block(ancestor) { |child| names << subject_name(child) if subject?(child) }
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
names
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
# An anonymous `subject!` is still read as `subject`.
|
|
98
|
-
def subject_name(node)
|
|
99
|
-
name = premise_name(node).to_sym
|
|
100
|
-
|
|
101
|
-
name == :subject! ? :subject : name
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
def subject_reference?(receiver, names)
|
|
105
|
-
return false unless receiver&.send_type? && receiver.receiver.nil? && receiver.arguments.empty?
|
|
106
87
|
|
|
107
|
-
|
|
88
|
+
call
|
|
108
89
|
end
|
|
109
90
|
end
|
|
110
91
|
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative '../returned_chain'
|
|
4
|
+
|
|
3
5
|
module RuboCop
|
|
4
6
|
module Cop
|
|
5
7
|
module Vicenzo
|
|
@@ -42,27 +44,11 @@ module RuboCop
|
|
|
42
44
|
cop_config.fetch('AllowedMethods', [])
|
|
43
45
|
end
|
|
44
46
|
|
|
45
|
-
#
|
|
46
|
-
# `Cat.new.catch(ball).to_s` is
|
|
47
|
-
#
|
|
48
|
-
def call_in_chain(body)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
while node
|
|
52
|
-
node = node.send_node if node.any_block_type?
|
|
53
|
-
break unless node.type?(:call)
|
|
54
|
-
return node if yield(node)
|
|
55
|
-
|
|
56
|
-
node = node.receiver
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
nil
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def returned_expression(body)
|
|
63
|
-
return if body.nil?
|
|
64
|
-
|
|
65
|
-
body.begin_type? ? body.children.last : body
|
|
47
|
+
# The first call the value of a definition is built on that satisfies the block, seen from the outside in:
|
|
48
|
+
# `Cat.new.catch(ball).to_s` is built on `catch` while `Cat.new(catches: other.catch)` is not - there,
|
|
49
|
+
# nothing of `catch` reaches the value. See `ReturnedChain`.
|
|
50
|
+
def call_in_chain(body, &)
|
|
51
|
+
ReturnedChain.new(body).find(&)
|
|
66
52
|
end
|
|
67
53
|
end
|
|
68
54
|
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Vicenzo
|
|
6
|
+
module RSpec
|
|
7
|
+
# Shared helpers for the cops that ask whether a definition reads the subject: which names answer to it
|
|
8
|
+
# where the definition sits, and whether a call is a bare reference to one of them.
|
|
9
|
+
#
|
|
10
|
+
# Meant for cops inheriting from `RuboCop::Cop::RSpec::Base` and including `PremiseTracking`:
|
|
11
|
+
# `spec_group?` and `subject?` come from the former, `each_child_block` and `premise_name` from the
|
|
12
|
+
# latter.
|
|
13
|
+
module SubjectReference
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
# `subject` always refers to the subject, declared or implicit; a named one answers to its name
|
|
17
|
+
# as well. A definition is read in every group below the one declaring it, so the subjects
|
|
18
|
+
# declared down there - the "abstract let" shape - answer too. Names come nearest group first, after
|
|
19
|
+
# `subject`.
|
|
20
|
+
#
|
|
21
|
+
# Shared groups count as groups, around the definition and below it: RSpec only lets a shared group
|
|
22
|
+
# be included under the group defining it, and a subject declared in one is the subject wherever it
|
|
23
|
+
# is included.
|
|
24
|
+
def subject_names(node)
|
|
25
|
+
names = Set[:subject]
|
|
26
|
+
groups = node.each_ancestor(:any_block).select { |ancestor| spec_group?(ancestor) }
|
|
27
|
+
|
|
28
|
+
collect_subject_names(groups.first, names, nested: true) if groups.any?
|
|
29
|
+
groups.drop(1).each { |group| collect_subject_names(group, names) }
|
|
30
|
+
|
|
31
|
+
names
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def collect_subject_names(group, names, nested: false)
|
|
35
|
+
each_child_block(group) do |child|
|
|
36
|
+
if subject?(child)
|
|
37
|
+
names << subject_name(child)
|
|
38
|
+
elsif nested && spec_group?(child)
|
|
39
|
+
collect_subject_names(child, names, nested:)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# An anonymous `subject!` is still read as `subject`, so it never passes for a name the subject was
|
|
45
|
+
# given.
|
|
46
|
+
def subject_name(node)
|
|
47
|
+
name = premise_name(node).to_sym
|
|
48
|
+
|
|
49
|
+
name == :subject! ? :subject : name
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def subject_reference?(receiver, names)
|
|
53
|
+
return false unless receiver.send_type? && receiver.receiver.nil? && receiver.arguments.empty?
|
|
54
|
+
|
|
55
|
+
names.include?(receiver.method_name)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -5,7 +5,7 @@ module RuboCop
|
|
|
5
5
|
module Vicenzo
|
|
6
6
|
module RSpec
|
|
7
7
|
# Checks for nested `context` blocks where the inner `context`
|
|
8
|
-
# starts with "when", "with", or "without". It suggests replacing it with "and"
|
|
8
|
+
# starts with "when", "with", or "without". It suggests replacing it with "and" or "but".
|
|
9
9
|
#
|
|
10
10
|
# @example
|
|
11
11
|
# # bad
|
|
@@ -22,7 +22,7 @@ module RuboCop
|
|
|
22
22
|
# end
|
|
23
23
|
# end
|
|
24
24
|
class NestedContextImproperStart < RuboCop::Cop::RSpec::Base
|
|
25
|
-
MSG = 'Nested `context` should start with `and
|
|
25
|
+
MSG = 'Nested `context` should start with `and` or `but`, not `when`, `with`, or `without`.'
|
|
26
26
|
|
|
27
27
|
FORBIDDEN_PREFIXES = %w[when with without].freeze
|
|
28
28
|
|
|
@@ -59,8 +59,7 @@ module RuboCop
|
|
|
59
59
|
|
|
60
60
|
return unless description_node&.str_type?
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
first_word = text.split.first&.downcase
|
|
62
|
+
first_word = description_node.value.lstrip[/\A[[:alpha:]]+/]&.downcase
|
|
64
63
|
|
|
65
64
|
return unless FORBIDDEN_PREFIXES.include?(first_word)
|
|
66
65
|
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Vicenzo
|
|
6
|
+
module RSpec
|
|
7
|
+
# The calls the value of a block body is built on, outermost first: `Cat.new.catch(ball).to_s` yields `to_s`,
|
|
8
|
+
# `catch` and `new`, while `Cat.new(catches: other.catch)` yields only `new` - nothing of `catch` reaches the
|
|
9
|
+
# value.
|
|
10
|
+
#
|
|
11
|
+
# The walk starts at the statement the body returns and follows its receivers, seeing through what only
|
|
12
|
+
# passes a value along: parentheses, an assignment, and a local variable, which answers to its latest
|
|
13
|
+
# assignment among the statements before the one reading it.
|
|
14
|
+
class ReturnedChain
|
|
15
|
+
include Enumerable
|
|
16
|
+
|
|
17
|
+
attr_reader :expression
|
|
18
|
+
|
|
19
|
+
def initialize(body)
|
|
20
|
+
@statements = statements_of(body)
|
|
21
|
+
@expression = @statements.last
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def each
|
|
25
|
+
node = expression
|
|
26
|
+
reach = @statements.size - 1
|
|
27
|
+
|
|
28
|
+
while node
|
|
29
|
+
node, reach = value_of(node, reach)
|
|
30
|
+
node = node.send_node if node&.any_block_type?
|
|
31
|
+
break unless node&.type?(:call)
|
|
32
|
+
|
|
33
|
+
yield node
|
|
34
|
+
node = node.receiver
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def statements_of(body)
|
|
41
|
+
return [] if body.nil?
|
|
42
|
+
|
|
43
|
+
body.begin_type? ? body.children : [body]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# `reach` is the index of the statement the node is read in: a variable only answers to an assignment made
|
|
47
|
+
# before it, which keeps `caught = caught.to_s` from answering to itself.
|
|
48
|
+
def value_of(node, reach)
|
|
49
|
+
loop do
|
|
50
|
+
case node&.type
|
|
51
|
+
when :begin then node = node.children.last
|
|
52
|
+
when :lvasgn then node = node.expression
|
|
53
|
+
when :lvar
|
|
54
|
+
reach = assignment_before(node.name, reach)
|
|
55
|
+
node = reach && @statements[reach]
|
|
56
|
+
else return [node, reach]
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def assignment_before(name, reach)
|
|
62
|
+
(reach - 1).downto(0).find do |index|
|
|
63
|
+
statement = @statements[index]
|
|
64
|
+
statement.lvasgn_type? && statement.name == name
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'describe_inside_context'
|
|
4
|
+
require_relative 'mixin/described_method'
|
|
5
|
+
require_relative 'mixin/premise_tracking'
|
|
6
|
+
require_relative 'mixin/subject_reference'
|
|
7
|
+
|
|
8
|
+
module RuboCop
|
|
9
|
+
module Cop
|
|
10
|
+
module Vicenzo
|
|
11
|
+
module RSpec
|
|
12
|
+
# A describe that exercises an entry point a sibling describe already names is a scenario of that entry
|
|
13
|
+
# point, escaped into a group of its own. Make it a context inside the describe naming the entry point.
|
|
14
|
+
#
|
|
15
|
+
# A describe names *what* is exercised, never *when*. A scenario usually escapes because the premises of
|
|
16
|
+
# the entry point's describe - a temporary directory, a `before` writing a file - did not fit it. Those
|
|
17
|
+
# premises belong in the contexts that read them: move them down, and the scenario fits back in. Left
|
|
18
|
+
# outside, the tree claims the entry point has fewer cases than it has, and the escaped one never states
|
|
19
|
+
# its premise as a context sentence.
|
|
20
|
+
#
|
|
21
|
+
# Only describes that name nothing are checked: a describe naming a class, an instance method
|
|
22
|
+
# (`'#name'`) or a class method (`'.name'`) states what it exercises, even when it calls another entry
|
|
23
|
+
# point to build its premises. Groups such as `describe 'GET /users'`, `describe 'enums'` or
|
|
24
|
+
# `describe 'callbacks'` are reported only when a sibling names the entry point they call.
|
|
25
|
+
#
|
|
26
|
+
# An entry point is reached by `described_class.name` (or `described_class.(...)` for `.call`) for a
|
|
27
|
+
# class method, and by `subject.name`, the named subject or `described_class.new(...).name` for an
|
|
28
|
+
# instance method. Calls inside a nested describe that names a class or an entry point belong to that
|
|
29
|
+
# describe, so they do not count.
|
|
30
|
+
#
|
|
31
|
+
# @example
|
|
32
|
+
# # bad - the reference file is a scenario of `.from_file`, outside its describe
|
|
33
|
+
# RSpec.describe ImageLoader do
|
|
34
|
+
# describe '.from_file' do
|
|
35
|
+
# let(:directory) { Dir.mktmpdir }
|
|
36
|
+
#
|
|
37
|
+
# after { FileUtils.remove_entry(directory) }
|
|
38
|
+
#
|
|
39
|
+
# context 'when the file does not exist' do
|
|
40
|
+
# it { expect { described_class.from_file(File.join(directory, 'missing.bin')) }.to raise_error(Errno::ENOENT) }
|
|
41
|
+
# end
|
|
42
|
+
# end
|
|
43
|
+
#
|
|
44
|
+
# describe 'loading the real reference file', :local_file do
|
|
45
|
+
# it { expect(described_class.from_file(reference_path)).to have_attributes(size: 393_232) }
|
|
46
|
+
# end
|
|
47
|
+
# end
|
|
48
|
+
#
|
|
49
|
+
# # good - every scenario of `.from_file` is a context inside it, carrying its own premises
|
|
50
|
+
# RSpec.describe ImageLoader do
|
|
51
|
+
# describe '.from_file' do
|
|
52
|
+
# context 'when the file does not exist' do
|
|
53
|
+
# let(:directory) { Dir.mktmpdir }
|
|
54
|
+
#
|
|
55
|
+
# after { FileUtils.remove_entry(directory) }
|
|
56
|
+
#
|
|
57
|
+
# it { expect { described_class.from_file(File.join(directory, 'missing.bin')) }.to raise_error(Errno::ENOENT) }
|
|
58
|
+
# end
|
|
59
|
+
#
|
|
60
|
+
# context 'when the file is the real reference file', :local_file do
|
|
61
|
+
# it { expect(described_class.from_file(reference_path)).to have_attributes(size: 393_232) }
|
|
62
|
+
# end
|
|
63
|
+
# end
|
|
64
|
+
# end
|
|
65
|
+
class ScenarioDescribe < RuboCop::Cop::RSpec::Base
|
|
66
|
+
include PremiseTracking
|
|
67
|
+
include SubjectReference
|
|
68
|
+
|
|
69
|
+
MSG = 'This `%<group>s` exercises `%<entry_point>s`, which the sibling `%<sibling>s` already names: ' \
|
|
70
|
+
'make it a `context` inside that group.'
|
|
71
|
+
|
|
72
|
+
CLASS_METHOD = '.'
|
|
73
|
+
INSTANCE_METHOD = '#'
|
|
74
|
+
|
|
75
|
+
# @!method describe_definition?(node)
|
|
76
|
+
def_node_matcher :describe_definition?, <<~PATTERN
|
|
77
|
+
(any_block (send #rspec? {#{DescribeInsideContext::DESCRIBES.map(&:inspect).join(' ')}} ...) ...)
|
|
78
|
+
PATTERN
|
|
79
|
+
|
|
80
|
+
# @!method described_class_reference?(node)
|
|
81
|
+
def_node_matcher :described_class_reference?, '(send nil? :described_class)'
|
|
82
|
+
|
|
83
|
+
# @!method new_instance?(node)
|
|
84
|
+
def_node_matcher :new_instance?, '(send (send nil? :described_class) :new ...)'
|
|
85
|
+
|
|
86
|
+
def on_block(node)
|
|
87
|
+
return unless spec_group?(node)
|
|
88
|
+
|
|
89
|
+
describes = child_describes(node)
|
|
90
|
+
named = named_entry_points(describes)
|
|
91
|
+
|
|
92
|
+
return if named.empty?
|
|
93
|
+
|
|
94
|
+
describes.each { |describe| check_candidate(describe, named) }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
alias on_numblock on_block
|
|
98
|
+
|
|
99
|
+
private
|
|
100
|
+
|
|
101
|
+
def child_describes(group)
|
|
102
|
+
describes = []
|
|
103
|
+
each_child_block(group) { |child| describes << child if describe_definition?(child) }
|
|
104
|
+
describes
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# The first sibling naming each entry point, keyed by the entry point as written: `.name` or `#name`.
|
|
108
|
+
def named_entry_points(describes)
|
|
109
|
+
describes.each_with_object({}) do |describe, named|
|
|
110
|
+
entry_point = entry_point_named_by(describe)
|
|
111
|
+
named[entry_point] ||= describe if entry_point
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def check_candidate(describe, named)
|
|
116
|
+
return if names_something?(describe)
|
|
117
|
+
|
|
118
|
+
entry_point = first_reached_entry_point(describe, named)
|
|
119
|
+
return unless entry_point
|
|
120
|
+
|
|
121
|
+
add_offense(describe.send_node, message: message(describe, entry_point, named[entry_point]))
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# `'.from_file'`, `'#size'` and the forms carrying a signature. A bare name, such as `'call'`, could be
|
|
125
|
+
# either, so it names nothing.
|
|
126
|
+
def entry_point_named_by(describe)
|
|
127
|
+
description = describe.send_node.first_argument
|
|
128
|
+
return unless description&.str_type?
|
|
129
|
+
|
|
130
|
+
match = DescribedMethod::PREFIXED_METHOD_DESCRIPTION.match(description.value)
|
|
131
|
+
"#{description.value[0]}#{match[:name]}" if match
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# A class changes what `described_class` is below it, and an entry point is what the group exercises:
|
|
135
|
+
# either way, the group states what it is about.
|
|
136
|
+
def names_something?(describe)
|
|
137
|
+
describe.send_node.first_argument&.const_type? || !entry_point_named_by(describe).nil?
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Walks the group in source order, leaving out the nested describes that name something: what is
|
|
141
|
+
# called there belongs to them.
|
|
142
|
+
def first_reached_entry_point(node, named)
|
|
143
|
+
node.each_child_node do |child|
|
|
144
|
+
next if describe_definition?(child) && names_something?(child)
|
|
145
|
+
|
|
146
|
+
entry_point = reached_entry_point(child, named) || first_reached_entry_point(child, named)
|
|
147
|
+
return entry_point if entry_point
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
nil
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def reached_entry_point(node, named)
|
|
154
|
+
return unless node.call_type? && node.receiver
|
|
155
|
+
|
|
156
|
+
class_method = "#{CLASS_METHOD}#{node.method_name}"
|
|
157
|
+
instance_method = "#{INSTANCE_METHOD}#{node.method_name}"
|
|
158
|
+
|
|
159
|
+
if named.has_key?(class_method) && described_class_reference?(node.receiver)
|
|
160
|
+
class_method
|
|
161
|
+
elsif named.has_key?(instance_method) && instance_receiver?(node)
|
|
162
|
+
instance_method
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def instance_receiver?(node)
|
|
167
|
+
new_instance?(node.receiver) || subject_reference?(node.receiver, subject_names(node))
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def message(describe, entry_point, sibling)
|
|
171
|
+
sibling_send = sibling.send_node
|
|
172
|
+
|
|
173
|
+
format(
|
|
174
|
+
MSG,
|
|
175
|
+
group: describe.method_name,
|
|
176
|
+
entry_point:,
|
|
177
|
+
sibling: "#{sibling_send.method_name} #{sibling_send.first_argument.source}"
|
|
178
|
+
)
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|
|
@@ -24,7 +24,11 @@ module RuboCop
|
|
|
24
24
|
# (a service object always invoked through `call`, for instance). `AllowedMethods` exists
|
|
25
25
|
# for those and ships empty: the convention belongs to the project that adopts it, not to
|
|
26
26
|
# this gem. Declaring it there - or excluding a whole directory through the standard
|
|
27
|
-
# `Exclude` - keeps the exemption visible and reviewable in `.rubocop.yml`.
|
|
27
|
+
# `Exclude` - keeps the exemption visible and reviewable in `.rubocop.yml`. The exemption
|
|
28
|
+
# covers the result itself, not what is derived from it: a subject that chains anything
|
|
29
|
+
# after the allowed call - a projection such as `map`, or unwrapping a monad through
|
|
30
|
+
# `value` - is still reported, since it drops the data the examples end up calling the
|
|
31
|
+
# method again to reach.
|
|
28
32
|
#
|
|
29
33
|
# An inline disable directive is not that: it is an exemption nobody reviewed. Dropping
|
|
30
34
|
# the `#` from the description is not either - the cop reads bare descriptions too, and the
|
|
@@ -59,26 +63,54 @@ module RuboCop
|
|
|
59
63
|
# describe '.call' do
|
|
60
64
|
# subject(:result) { described_class.(object: Ball.new) }
|
|
61
65
|
# end
|
|
66
|
+
#
|
|
67
|
+
# # bad - the subject is derived from the result, not the result itself
|
|
68
|
+
# describe '.call' do
|
|
69
|
+
# subject(:ids) { described_class.().map(&:id) }
|
|
70
|
+
# end
|
|
71
|
+
#
|
|
72
|
+
# # bad - unwrapping the monad keeps only a part of the result
|
|
73
|
+
# describe '.call' do
|
|
74
|
+
# subject(:value) { described_class.(object: Ball.new).value }
|
|
75
|
+
# end
|
|
62
76
|
class SubjectIsMethodResult < RuboCop::Cop::RSpec::Base
|
|
63
77
|
include DescribedMethod
|
|
64
78
|
|
|
65
79
|
MSG = 'Subject holds what `%<method>s` returned, not the object under test. ' \
|
|
66
80
|
'Make the subject the object that receives `%<method>s`, and call it inside the example.'
|
|
81
|
+
DERIVED_MSG = 'Subject derives from what `%<method>s` returned. ' \
|
|
82
|
+
'Keep the subject as the result of `%<method>s`, and derive from it inside the example.'
|
|
67
83
|
|
|
68
84
|
def on_block(node)
|
|
69
85
|
return unless subject?(node)
|
|
70
86
|
|
|
71
87
|
method_name = described_method_name(node)
|
|
72
|
-
return if method_name.nil?
|
|
88
|
+
return if method_name.nil?
|
|
73
89
|
|
|
74
|
-
|
|
75
|
-
|
|
90
|
+
chain = ReturnedChain.new(node.body)
|
|
91
|
+
invocations = chain.select { |call| call.method?(method_name) }
|
|
92
|
+
return if invocations.empty?
|
|
76
93
|
|
|
77
|
-
|
|
94
|
+
if allowed_methods.include?(method_name)
|
|
95
|
+
check_derivation(chain, invocations.last, method_name)
|
|
96
|
+
else
|
|
97
|
+
add_offense(invocations.first, message: format(MSG, method: method_name))
|
|
98
|
+
end
|
|
78
99
|
end
|
|
79
100
|
|
|
80
101
|
alias on_numblock on_block
|
|
81
102
|
alias on_itblock on_block
|
|
103
|
+
|
|
104
|
+
private
|
|
105
|
+
|
|
106
|
+
# An allowed method may be the subject only as the returned expression itself: its innermost
|
|
107
|
+
# invocation has to be the outermost call of the chain. A block handed to it still belongs to the
|
|
108
|
+
# call; anything chained after it - calling the result again included - does not.
|
|
109
|
+
def check_derivation(chain, innermost_invocation, method_name)
|
|
110
|
+
return if chain.first.equal?(innermost_invocation)
|
|
111
|
+
|
|
112
|
+
add_offense(chain.expression, message: format(DERIVED_MSG, method: method_name))
|
|
113
|
+
end
|
|
82
114
|
end
|
|
83
115
|
end
|
|
84
116
|
end
|
|
@@ -4,7 +4,7 @@ module RuboCop
|
|
|
4
4
|
module Cop
|
|
5
5
|
module Vicenzo
|
|
6
6
|
module RSpec
|
|
7
|
-
# Checks for `context` blocks with no parent scenario that start with "and"
|
|
7
|
+
# Checks for `context` blocks with no parent scenario that start with "and" or "but".
|
|
8
8
|
# It suggests starting them with "when", "with", or "without" instead.
|
|
9
9
|
#
|
|
10
10
|
# A conjunction continues the scenario of the enclosing context. When the closest example group is
|
|
@@ -34,7 +34,7 @@ module RuboCop
|
|
|
34
34
|
# end
|
|
35
35
|
# end
|
|
36
36
|
#
|
|
37
|
-
# @example ForbiddenPrefixes: ['and', 'but', '
|
|
37
|
+
# @example ForbiddenPrefixes: ['and', 'but', 'also']
|
|
38
38
|
# # bad
|
|
39
39
|
# describe '#available_colors' do
|
|
40
40
|
# context 'also when the color pink is not available' do
|
|
@@ -45,7 +45,7 @@ module RuboCop
|
|
|
45
45
|
MSG = 'Unnested `context` should start with `when`, `with`, or `without`, not `%<prefix>s`.'
|
|
46
46
|
|
|
47
47
|
CONTEXTS = %i[context fcontext xcontext].freeze
|
|
48
|
-
DEFAULT_FORBIDDEN_PREFIXES = %w[and but
|
|
48
|
+
DEFAULT_FORBIDDEN_PREFIXES = %w[and but].freeze
|
|
49
49
|
|
|
50
50
|
# @!method context_definition?(node)
|
|
51
51
|
def_node_matcher :context_definition?, <<~PATTERN
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Vicenzo
|
|
6
|
+
module Style
|
|
7
|
+
# Enforces `many?` over comparing `size` or `length` with one.
|
|
8
|
+
#
|
|
9
|
+
# Active Support's `Enumerable#many?` exists precisely to name the
|
|
10
|
+
# "more than one element" question that `size > 1` only spells out.
|
|
11
|
+
# RuboCop's own `Style/CollectionQuerying` already covers `count > 1`, but
|
|
12
|
+
# leaves `size` and `length` alone, which is the form most often written.
|
|
13
|
+
# This cop closes that gap.
|
|
14
|
+
#
|
|
15
|
+
# Only an explicit receiver calling `size` or `length` without arguments is
|
|
16
|
+
# flagged, compared with one through `>` or with two through `>=`, on
|
|
17
|
+
# either side of the operator. Like `Style/CollectionQuerying`, the cop
|
|
18
|
+
# only runs when `AllCops: ActiveSupportExtensionsEnabled` is on, since
|
|
19
|
+
# `many?` comes from Active Support. `rubocop-rails` turns it on.
|
|
20
|
+
#
|
|
21
|
+
# @safety
|
|
22
|
+
# The cop is unsafe because the receiver might not include `Enumerable`.
|
|
23
|
+
# `String`, for instance, responds to `size` and `length` but not to
|
|
24
|
+
# `many?`. The same holds for any object with its own `count`, which is
|
|
25
|
+
# why `Style/CollectionQuerying` is unsafe as well.
|
|
26
|
+
#
|
|
27
|
+
# @example
|
|
28
|
+
# # bad
|
|
29
|
+
# items.size > 1
|
|
30
|
+
# items.length >= 2
|
|
31
|
+
# 1 < items.size
|
|
32
|
+
#
|
|
33
|
+
# # good
|
|
34
|
+
# items.many?
|
|
35
|
+
class PreferManyOverSizeComparison < Base
|
|
36
|
+
extend AutoCorrector
|
|
37
|
+
|
|
38
|
+
MSG = 'Use `many?` instead of `%<comparison>s`.'
|
|
39
|
+
|
|
40
|
+
RESTRICT_ON_SEND = %i[> >= < <=].freeze
|
|
41
|
+
|
|
42
|
+
# @!method size_comparison(node)
|
|
43
|
+
def_node_matcher :size_comparison, <<~PATTERN
|
|
44
|
+
{
|
|
45
|
+
(call $(call !nil? {:size :length}) {:> (int 1) | :>= (int 2)})
|
|
46
|
+
(call {(int 1) :< | (int 2) :<=} $(call !nil? {:size :length}))
|
|
47
|
+
}
|
|
48
|
+
PATTERN
|
|
49
|
+
|
|
50
|
+
def on_send(node)
|
|
51
|
+
return unless active_support_extensions_enabled?
|
|
52
|
+
return unless (size_node = size_comparison(node))
|
|
53
|
+
|
|
54
|
+
message = format(MSG, comparison: comparison_source(node, size_node))
|
|
55
|
+
|
|
56
|
+
add_offense(node, message: message) do |corrector|
|
|
57
|
+
corrector.replace(node, "#{size_node.receiver.source}#{size_node.loc.dot.source}many?")
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
alias on_csend on_send
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def comparison_source(node, size_node)
|
|
65
|
+
size_source = size_node.method_name
|
|
66
|
+
|
|
67
|
+
if node.receiver.equal?(size_node)
|
|
68
|
+
"#{size_source} #{node.method_name} #{node.first_argument.source}"
|
|
69
|
+
else
|
|
70
|
+
"#{node.receiver.source} #{node.method_name} #{size_source}"
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -13,6 +13,8 @@ require_relative 'vicenzo/rspec/nested_context_improper_start'
|
|
|
13
13
|
require_relative 'vicenzo/rspec/nested_let_redefinition'
|
|
14
14
|
require_relative 'vicenzo/rspec/nested_subject_redefinition'
|
|
15
15
|
require_relative 'vicenzo/rspec/leaky_definition'
|
|
16
|
+
require_relative 'vicenzo/rspec/let_aliasing_subject'
|
|
17
|
+
require_relative 'vicenzo/rspec/scenario_describe'
|
|
16
18
|
require_relative 'vicenzo/rspec/subject_defined_as_let'
|
|
17
19
|
require_relative 'vicenzo/rspec/subject_is_method_result'
|
|
18
20
|
require_relative 'vicenzo/rspec/top_level_context'
|
|
@@ -27,3 +29,4 @@ require_relative 'vicenzo/naming/anonymous_unused_name'
|
|
|
27
29
|
require_relative 'vicenzo/naming/short_name'
|
|
28
30
|
require_relative 'vicenzo/style/json_parse_symbolize_names'
|
|
29
31
|
require_relative 'vicenzo/style/multiline_method_call_parentheses'
|
|
32
|
+
require_relative 'vicenzo/style/prefer_many_over_size_comparison'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rubocop-vicenzo
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.11.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bruno Vicenzo
|
|
@@ -86,15 +86,19 @@ files:
|
|
|
86
86
|
- lib/rubocop/cop/vicenzo/rspec/inconsistent_sibling_structure.rb
|
|
87
87
|
- lib/rubocop/cop/vicenzo/rspec/iteration_inside_example.rb
|
|
88
88
|
- lib/rubocop/cop/vicenzo/rspec/leaky_definition.rb
|
|
89
|
+
- lib/rubocop/cop/vicenzo/rspec/let_aliasing_subject.rb
|
|
89
90
|
- lib/rubocop/cop/vicenzo/rspec/let_is_method_result.rb
|
|
90
91
|
- lib/rubocop/cop/vicenzo/rspec/mixin/described_method.rb
|
|
91
92
|
- lib/rubocop/cop/vicenzo/rspec/mixin/expectation_target.rb
|
|
92
93
|
- lib/rubocop/cop/vicenzo/rspec/mixin/premise_tracking.rb
|
|
94
|
+
- lib/rubocop/cop/vicenzo/rspec/mixin/subject_reference.rb
|
|
93
95
|
- lib/rubocop/cop/vicenzo/rspec/mixin/top_level_definition.rb
|
|
94
96
|
- lib/rubocop/cop/vicenzo/rspec/mutated_premises.rb
|
|
95
97
|
- lib/rubocop/cop/vicenzo/rspec/nested_context_improper_start.rb
|
|
96
98
|
- lib/rubocop/cop/vicenzo/rspec/nested_let_redefinition.rb
|
|
97
99
|
- lib/rubocop/cop/vicenzo/rspec/nested_subject_redefinition.rb
|
|
100
|
+
- lib/rubocop/cop/vicenzo/rspec/returned_chain.rb
|
|
101
|
+
- lib/rubocop/cop/vicenzo/rspec/scenario_describe.rb
|
|
98
102
|
- lib/rubocop/cop/vicenzo/rspec/subject_defined_as_let.rb
|
|
99
103
|
- lib/rubocop/cop/vicenzo/rspec/subject_is_method_result.rb
|
|
100
104
|
- lib/rubocop/cop/vicenzo/rspec/top_level_context.rb
|
|
@@ -105,6 +109,7 @@ files:
|
|
|
105
109
|
- lib/rubocop/cop/vicenzo/rspec/unnested_context_improper_start.rb
|
|
106
110
|
- lib/rubocop/cop/vicenzo/style/json_parse_symbolize_names.rb
|
|
107
111
|
- lib/rubocop/cop/vicenzo/style/multiline_method_call_parentheses.rb
|
|
112
|
+
- lib/rubocop/cop/vicenzo/style/prefer_many_over_size_comparison.rb
|
|
108
113
|
- lib/rubocop/cop/vicenzo_cops.rb
|
|
109
114
|
- lib/rubocop/vicenzo.rb
|
|
110
115
|
- lib/rubocop/vicenzo/plugin.rb
|