rubocop-munra 0.1.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 +7 -0
- data/.ruby-version +1 -0
- data/AGENTS.md +99 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +42 -0
- data/config/default.yml +27 -0
- data/docs/modules/ROOT/pages/cops.adoc +2 -0
- data/docs/modules/ROOT/pages/cops_munra.adoc +336 -0
- data/docs/usage.md +160 -0
- data/lib/rubocop/cop/munra/00_class_body_helper.rb +27 -0
- data/lib/rubocop/cop/munra/00_rspec_helpers.rb +66 -0
- data/lib/rubocop/cop/munra/00_transaction_block_mixin.rb +49 -0
- data/lib/rubocop/cop/munra/let_layout.rb +155 -0
- data/lib/rubocop/cop/munra/named_subject_layout.rb +165 -0
- data/lib/rubocop/cop/munra/no_human_attribute_name.rb +31 -0
- data/lib/rubocop/cop/munra/no_leading_dot.rb +25 -0
- data/lib/rubocop/cop/munra/no_spec_method.rb +28 -0
- data/lib/rubocop/cop/munra/no_transaction_exit.rb +42 -0
- data/lib/rubocop/cop/munra/success_error_siblings.rb +82 -0
- data/lib/rubocop/cop/munra.rb +20 -0
- data/lib/rubocop/munra/plugin.rb +39 -0
- data/lib/rubocop/munra/version.rb +14 -0
- data/lib/rubocop-munra.rb +13 -0
- data/sig/rubocop/munra.rbs +6 -0
- metadata +107 -0
data/docs/usage.md
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Using rubocop-munra
|
|
2
|
+
|
|
3
|
+
`rubocop-munra` is a RuboCop plugin. It adds the `Munra` department: cops for Rails application conventions used by Munra apps. It runs on Ruby 3.2.0 and newer. Defaults live in this gem. Each app turns cops on or off and overrides options in its own `.rubocop.yml`. Do not copy cop source into the app.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
From a path checkout:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
# Gemfile
|
|
11
|
+
gem 'rubocop-munra', path: '../../rubocop-munra'
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
From Git:
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
gem 'rubocop-munra', git: 'https://github.com/ramonsantos/rubocop-munra.git', branch: 'main'
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
From RubyGems, once the gem is published:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
gem 'rubocop-munra'
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Then:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
bundle install
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Enable the plugin
|
|
33
|
+
|
|
34
|
+
In the app's `.rubocop.yml`:
|
|
35
|
+
|
|
36
|
+
```yaml
|
|
37
|
+
plugins:
|
|
38
|
+
- rubocop-munra
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
RuboCop loads `RuboCop::Munra::Plugin`, which points at this gem's [`config/default.yml`](../config/default.yml). That file enables the `Munra` department and, as cops are added, each cop's default options.
|
|
42
|
+
|
|
43
|
+
Check that the plugin is visible:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
bundle exec rubocop --show-cops Munra
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Before any cop is merged, the department exists and the cop list is empty. After a cop is added, it shows up here with its description and default configuration.
|
|
50
|
+
|
|
51
|
+
## Department and cop switches
|
|
52
|
+
|
|
53
|
+
Disable the whole department:
|
|
54
|
+
|
|
55
|
+
```yaml
|
|
56
|
+
Munra:
|
|
57
|
+
Enabled: false
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Disable one cop and keep the rest:
|
|
61
|
+
|
|
62
|
+
```yaml
|
|
63
|
+
Munra/NoLeadingDot:
|
|
64
|
+
Enabled: false
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Run a single cop while you adopt it:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
bundle exec rubocop --only Munra/NoLeadingDot
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Ignore it in one file:
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
# rubocop:disable Munra/NoLeadingDot
|
|
77
|
+
chain
|
|
78
|
+
.call
|
|
79
|
+
# rubocop:enable Munra/NoLeadingDot
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Limit where a cop runs
|
|
83
|
+
|
|
84
|
+
`Include` and `Exclude` are relative to the app, not to this gem.
|
|
85
|
+
|
|
86
|
+
```yaml
|
|
87
|
+
Munra/NoLeadingDot:
|
|
88
|
+
Include:
|
|
89
|
+
- app/**/*.rb
|
|
90
|
+
Exclude:
|
|
91
|
+
- db/schema.rb
|
|
92
|
+
- vendor/**/*
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`Include` replaces the cop's default include list when you set it. `Exclude` is merged with the default excludes. Prefer `Exclude` when you only want to skip a few paths.
|
|
96
|
+
|
|
97
|
+
## Override cop options
|
|
98
|
+
|
|
99
|
+
Domain values (attribute names, superclasses, allowed files) are keys in `config/default.yml`, not constants inside the cop. Override the keys you need. Omitted keys keep the gem default.
|
|
100
|
+
|
|
101
|
+
Example shape, once `Munra/AdminPolicy` is part of the gem:
|
|
102
|
+
|
|
103
|
+
```yaml
|
|
104
|
+
Munra/AdminPolicy:
|
|
105
|
+
Enabled: true
|
|
106
|
+
ExpectedSuperclass: Admin::ApplicationPolicy
|
|
107
|
+
AllowedResolveFiles:
|
|
108
|
+
- app/policies/admin/account_policy.rb
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
The generated cop page lists every key, its type, and the default. Read that page before inventing a key. Unknown keys are reported by RuboCop as configuration errors.
|
|
112
|
+
|
|
113
|
+
## What each cop accepts
|
|
114
|
+
|
|
115
|
+
Cop behavior is documented on the cop class (`@example` in the source) and generated into AsciiDoc:
|
|
116
|
+
|
|
117
|
+
[`docs/modules/ROOT/pages/cops_munra.adoc`](modules/ROOT/pages/cops_munra.adoc)
|
|
118
|
+
|
|
119
|
+
Regenerate it after changing a cop comment or `config/default.yml`:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
bundle exec rake generate_cops_documentation
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
The page is produced by RuboCop's `CopsDocumentationGenerator`. Do not edit it by hand. Change the cop's comment or the YAML default, then run the task again.
|
|
126
|
+
|
|
127
|
+
`DocumentationBaseURL` in `config/default.yml` is the prefix RuboCop uses for "see documentation" links. It points at that generated page on GitHub. `DocumentationExtension` is `.adoc`.
|
|
128
|
+
|
|
129
|
+
## Examples of a cop comment
|
|
130
|
+
|
|
131
|
+
A cop that ships later should look like this so the generator can pick up the text and the samples:
|
|
132
|
+
|
|
133
|
+
```ruby
|
|
134
|
+
# Flags a method chain broken with a leading dot.
|
|
135
|
+
#
|
|
136
|
+
# @example
|
|
137
|
+
# # bad
|
|
138
|
+
# user
|
|
139
|
+
# .name
|
|
140
|
+
#
|
|
141
|
+
# # good
|
|
142
|
+
# user.
|
|
143
|
+
# name
|
|
144
|
+
class RuboCop::Cop::Munra::NoLeadingDot < RuboCop::Cop::Base
|
|
145
|
+
end
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Configurable cops document each non-obvious key in the comment and set the default in `config/default.yml`.
|
|
149
|
+
|
|
150
|
+
## Develop this gem
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
bundle install
|
|
154
|
+
bundle exec rubocop
|
|
155
|
+
bundle exec rake test
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
`rake test` fails if line or branch coverage of `lib/` drops below 100%. `rake` runs the tests and RuboCop.
|
|
159
|
+
|
|
160
|
+
See [AGENTS.md](../AGENTS.md) for how new cops, tests, and docs are added.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Shared AST helpers for cops that walk a class or module body.
|
|
4
|
+
#
|
|
5
|
+
# Not a cop. Loaded before cop files because of the `00_` prefix.
|
|
6
|
+
# Include this module and call {#class_body_statements}.
|
|
7
|
+
module RuboCop::Cop::Munra::ClassBodyHelper
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
# Statements directly inside a class or module node.
|
|
11
|
+
#
|
|
12
|
+
# An empty body is `[]`. A single statement is a one-element array.
|
|
13
|
+
# Several statements are the children of the implicit `begin` node.
|
|
14
|
+
#
|
|
15
|
+
# @param class_node [RuboCop::AST::Node] a `class` or `module` node
|
|
16
|
+
# @return [Array<RuboCop::AST::Node>]
|
|
17
|
+
def class_body_statements(class_node)
|
|
18
|
+
body = class_node.body
|
|
19
|
+
return [] unless body
|
|
20
|
+
|
|
21
|
+
if body.begin_type?
|
|
22
|
+
body.children
|
|
23
|
+
else
|
|
24
|
+
[body]
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Predicates for RSpec example groups, examples, and `let` blocks.
|
|
4
|
+
#
|
|
5
|
+
# Not a cop. Cops that inspect spec structure include this module
|
|
6
|
+
# instead of reimplementing the same node checks.
|
|
7
|
+
module RuboCop::Cop::Munra::RSpecHelpers
|
|
8
|
+
# Example-group method names treated as a group.
|
|
9
|
+
EXAMPLE_GROUP_METHODS = %i[describe context].freeze
|
|
10
|
+
# Example method names treated as one example.
|
|
11
|
+
EXAMPLE_METHODS = %i[it specify example].freeze
|
|
12
|
+
# Memoized helper method names.
|
|
13
|
+
LET_METHODS = %i[let let!].freeze
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
# @param node [RuboCop::AST::Node]
|
|
18
|
+
# @return [Boolean] true for a `describe` or `context` block
|
|
19
|
+
def example_group?(node)
|
|
20
|
+
return false unless node.block_type?
|
|
21
|
+
|
|
22
|
+
send = node.send_node
|
|
23
|
+
return false unless send
|
|
24
|
+
|
|
25
|
+
EXAMPLE_GROUP_METHODS.include?(send.method_name)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @param node [RuboCop::AST::Node]
|
|
29
|
+
# @return [Boolean] true for an `it`, `specify`, or `example` block with no receiver
|
|
30
|
+
def example?(node)
|
|
31
|
+
return false unless node.block_type?
|
|
32
|
+
|
|
33
|
+
send = node.send_node
|
|
34
|
+
return false unless send
|
|
35
|
+
return false unless send.receiver.nil?
|
|
36
|
+
|
|
37
|
+
EXAMPLE_METHODS.include?(send.method_name)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @param node [RuboCop::AST::Node]
|
|
41
|
+
# @return [Boolean] true for a `let` or `let!` block with no receiver
|
|
42
|
+
def let?(node)
|
|
43
|
+
return false unless node.block_type?
|
|
44
|
+
|
|
45
|
+
send = node.send_node
|
|
46
|
+
return false unless send
|
|
47
|
+
return false unless send.receiver.nil?
|
|
48
|
+
|
|
49
|
+
LET_METHODS.include?(send.method_name)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Direct children of a block body.
|
|
53
|
+
#
|
|
54
|
+
# @param node [RuboCop::AST::Node] a block node
|
|
55
|
+
# @return [Array<RuboCop::AST::Node>]
|
|
56
|
+
def block_statement_children(node)
|
|
57
|
+
body = node.body
|
|
58
|
+
return [] unless body
|
|
59
|
+
|
|
60
|
+
if body.begin_type?
|
|
61
|
+
body.children
|
|
62
|
+
else
|
|
63
|
+
[body]
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Detects `return` and `next` written directly inside a transaction block.
|
|
4
|
+
#
|
|
5
|
+
# Not a cop. `transaction` and `with_lock` blocks are recognized when the
|
|
6
|
+
# receiver is omitted, a constant, or a method call.
|
|
7
|
+
module RuboCop::Cop::Munra::TransactionBlockMixin
|
|
8
|
+
# Method names that open a transaction block.
|
|
9
|
+
TRANSACTION_METHODS = %i[transaction with_lock].freeze
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
# @param block_node [RuboCop::AST::Node]
|
|
14
|
+
# @return [Boolean] true when the block is a transaction or `with_lock`
|
|
15
|
+
def transaction_block?(block_node)
|
|
16
|
+
send = block_node.send_node
|
|
17
|
+
return false unless send
|
|
18
|
+
|
|
19
|
+
TRANSACTION_METHODS.include?(send.method_name) && transaction_receiver?(send.receiver)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# @param receiver [RuboCop::AST::Node, nil]
|
|
23
|
+
# @return [Boolean]
|
|
24
|
+
def transaction_receiver?(receiver)
|
|
25
|
+
return true if receiver.nil?
|
|
26
|
+
|
|
27
|
+
receiver.const_type? || receiver.send_type?
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Nearest ancestor block that is a transaction, if any.
|
|
31
|
+
#
|
|
32
|
+
# @param node [RuboCop::AST::Node]
|
|
33
|
+
# @return [RuboCop::AST::Node, nil]
|
|
34
|
+
def direct_transaction_block_for(node)
|
|
35
|
+
node.each_ancestor(:block).find { |block| transaction_block?(block) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# @param node [RuboCop::AST::Node] a `return` or `next` node
|
|
39
|
+
# @return [Boolean] true when that node sits in the transaction body, not inside a nested block
|
|
40
|
+
def exit_in_direct_transaction_body?(node)
|
|
41
|
+
return false unless %i[return next].include?(node.type)
|
|
42
|
+
|
|
43
|
+
transaction_block = direct_transaction_block_for(node)
|
|
44
|
+
return false unless transaction_block
|
|
45
|
+
|
|
46
|
+
innermost_block = node.each_ancestor(:block).first
|
|
47
|
+
innermost_block == transaction_block
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Enforces blank lines and ordering for single-line versus multi-line `let` blocks.
|
|
4
|
+
#
|
|
5
|
+
# Single-line `let` declarations stay together without blank lines between them,
|
|
6
|
+
# and they come before multi-line `let` blocks. A multi-line `let` has a blank
|
|
7
|
+
# line before and after it. The blank line before it is unnecessary when the
|
|
8
|
+
# previous line is empty or already opens the surrounding block.
|
|
9
|
+
#
|
|
10
|
+
# @example Single-line lets stay together
|
|
11
|
+
# # bad
|
|
12
|
+
# RSpec.describe Foo do
|
|
13
|
+
# let(:name) { 'Ada' }
|
|
14
|
+
#
|
|
15
|
+
# let(:role) { 'admin' }
|
|
16
|
+
# end
|
|
17
|
+
#
|
|
18
|
+
# # good
|
|
19
|
+
# RSpec.describe Foo do
|
|
20
|
+
# let(:name) { 'Ada' }
|
|
21
|
+
# let(:role) { 'admin' }
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# @example Multi-line lets are surrounded by blank lines
|
|
25
|
+
# # bad
|
|
26
|
+
# RSpec.describe Foo do
|
|
27
|
+
# let(:name) { 'Ada' }
|
|
28
|
+
# let(:profile) do
|
|
29
|
+
# { role: 'admin' }
|
|
30
|
+
# end
|
|
31
|
+
# end
|
|
32
|
+
#
|
|
33
|
+
# # good
|
|
34
|
+
# RSpec.describe Foo do
|
|
35
|
+
# let(:name) { 'Ada' }
|
|
36
|
+
#
|
|
37
|
+
# let(:profile) do
|
|
38
|
+
# { role: 'admin' }
|
|
39
|
+
# end
|
|
40
|
+
#
|
|
41
|
+
# end
|
|
42
|
+
#
|
|
43
|
+
# @example Single-line lets come before multi-line lets
|
|
44
|
+
# # bad
|
|
45
|
+
# RSpec.describe Foo do
|
|
46
|
+
# let(:profile) do
|
|
47
|
+
# { role: 'admin' }
|
|
48
|
+
# end
|
|
49
|
+
#
|
|
50
|
+
# let(:name) { 'Ada' }
|
|
51
|
+
# end
|
|
52
|
+
#
|
|
53
|
+
# # good
|
|
54
|
+
# RSpec.describe Foo do
|
|
55
|
+
# let(:name) { 'Ada' }
|
|
56
|
+
#
|
|
57
|
+
# let(:profile) do
|
|
58
|
+
# { role: 'admin' }
|
|
59
|
+
# end
|
|
60
|
+
#
|
|
61
|
+
# end
|
|
62
|
+
class RuboCop::Cop::Munra::LetLayout < RuboCop::Cop::Base
|
|
63
|
+
include RuboCop::Cop::Munra::RSpecHelpers
|
|
64
|
+
|
|
65
|
+
MSG_SINGLE_GAP = 'Do not add blank lines between single-line `let` declarations.'
|
|
66
|
+
MSG_MULTILINE_PAD = 'Add a blank line before and after multi-line `let` declarations.'
|
|
67
|
+
MSG_SINGLE_BEFORE_MULTILINE = 'Single-line `let` declarations must come before multi-line `let` blocks.'
|
|
68
|
+
|
|
69
|
+
def on_block(node)
|
|
70
|
+
return unless example_group?(node)
|
|
71
|
+
|
|
72
|
+
lets = let_declarations(node)
|
|
73
|
+
return if lets.size < 2
|
|
74
|
+
|
|
75
|
+
check_single_line_grouping(lets)
|
|
76
|
+
check_single_before_multiline(lets)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def let_declarations(node)
|
|
82
|
+
return [] unless node.body
|
|
83
|
+
|
|
84
|
+
block_statement_children(node).select { |child| let?(child) }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def single_line_let?(node)
|
|
88
|
+
return false unless node.loc.begin && node.loc.end
|
|
89
|
+
|
|
90
|
+
node.loc.begin.line == node.loc.end.line
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def check_single_line_grouping(lets)
|
|
94
|
+
lets.each_cons(2) do |left, right|
|
|
95
|
+
next unless single_line_let?(left) && single_line_let?(right)
|
|
96
|
+
next unless empty_line_between?(left, right)
|
|
97
|
+
|
|
98
|
+
add_offense(right, message: MSG_SINGLE_GAP)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def empty_line_between?(left, right)
|
|
103
|
+
((left.loc.end.line + 1)...right.loc.line).any? do |line_number|
|
|
104
|
+
processed_source.lines[line_number - 1].strip.empty?
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def check_single_before_multiline(lets)
|
|
109
|
+
seen_multiline = false
|
|
110
|
+
lets.each do |let_node|
|
|
111
|
+
if single_line_let?(let_node)
|
|
112
|
+
add_offense(let_node, message: MSG_SINGLE_BEFORE_MULTILINE) if seen_multiline
|
|
113
|
+
else
|
|
114
|
+
seen_multiline = true
|
|
115
|
+
check_multiline_padding(let_node)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def check_multiline_padding(let_node)
|
|
121
|
+
return unless let_node.loc.begin && let_node.loc.end
|
|
122
|
+
|
|
123
|
+
add_leading_multiline_padding_offense(let_node)
|
|
124
|
+
add_trailing_multiline_padding_offense(let_node)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def add_leading_multiline_padding_offense(let_node)
|
|
128
|
+
previous_line = let_node.loc.line - 1
|
|
129
|
+
return unless previous_line.positive?
|
|
130
|
+
return unless multiline_let_needs_leading_padding?(let_node, previous_line, processed_source)
|
|
131
|
+
|
|
132
|
+
add_offense(let_node, message: MSG_MULTILINE_PAD)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def add_trailing_multiline_padding_offense(let_node)
|
|
136
|
+
next_line = let_node.loc.end.line + 1
|
|
137
|
+
return if next_line > processed_source.lines.length
|
|
138
|
+
return if processed_source.lines[next_line - 1].strip.empty?
|
|
139
|
+
|
|
140
|
+
add_offense(let_node, message: MSG_MULTILINE_PAD)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def multiline_let_needs_leading_padding?(_let_node, previous_line, source)
|
|
144
|
+
previous_source = source.lines[previous_line - 1]
|
|
145
|
+
return false if previous_source.strip.empty?
|
|
146
|
+
return false if block_opener_line?(previous_source)
|
|
147
|
+
|
|
148
|
+
true
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def block_opener_line?(line)
|
|
152
|
+
stripped = line.strip
|
|
153
|
+
stripped.end_with?(' do') || stripped == 'do'
|
|
154
|
+
end
|
|
155
|
+
end
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Named `subject` must be the first declaration in its example group;
|
|
4
|
+
# do not repeat the subject body inline inside `expect`.
|
|
5
|
+
#
|
|
6
|
+
# @example
|
|
7
|
+
# # bad
|
|
8
|
+
# RSpec.describe FooService do
|
|
9
|
+
# let(:name) { 'x' }
|
|
10
|
+
# subject(:result) { described_class.call(name:) }
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# # bad
|
|
14
|
+
# RSpec.describe FooService do
|
|
15
|
+
# subject { described_class.call }
|
|
16
|
+
# end
|
|
17
|
+
#
|
|
18
|
+
# # bad
|
|
19
|
+
# RSpec.describe FooService do
|
|
20
|
+
# subject(:result) { described_class.call(name: 'x') }
|
|
21
|
+
#
|
|
22
|
+
# it 'works' do
|
|
23
|
+
# expect(described_class.call(name: 'x')).to be(true)
|
|
24
|
+
# end
|
|
25
|
+
# end
|
|
26
|
+
#
|
|
27
|
+
# # good
|
|
28
|
+
# RSpec.describe FooService do
|
|
29
|
+
# subject(:result) { described_class.call(name: 'x') }
|
|
30
|
+
#
|
|
31
|
+
# it 'works' do
|
|
32
|
+
# expect(result).to be(true)
|
|
33
|
+
# end
|
|
34
|
+
# end
|
|
35
|
+
class RuboCop::Cop::Munra::NamedSubjectLayout < RuboCop::Cop::Base
|
|
36
|
+
include RuboCop::Cop::Munra::RSpecHelpers
|
|
37
|
+
|
|
38
|
+
MSG_NAMED = 'Use a named `subject(:name)` instead of an anonymous `subject`.'
|
|
39
|
+
MSG_FIRST = '`subject` must be the first declaration in the example group.'
|
|
40
|
+
MSG_REPEAT = 'Use the named subject `%<name>s` in the expectation instead of repeating its body.'
|
|
41
|
+
|
|
42
|
+
SUBJECT_METHODS = %i[subject subject!].freeze
|
|
43
|
+
HTTP_METHODS = %i[get post put patch delete].freeze
|
|
44
|
+
|
|
45
|
+
def on_block(node)
|
|
46
|
+
check_example_group(node) if example_group?(node)
|
|
47
|
+
check_example_expects(node) if example?(node)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def check_example_group(node)
|
|
53
|
+
statements = block_statement_children(node)
|
|
54
|
+
subject_nodes = statements.select { |statement| subject_declaration?(statement) }
|
|
55
|
+
return if subject_nodes.empty?
|
|
56
|
+
|
|
57
|
+
subject_nodes.each do |subject_node|
|
|
58
|
+
add_offense(subject_node, message: MSG_NAMED) unless named_subject?(subject_node)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
return if subject_declaration?(statements.first)
|
|
62
|
+
|
|
63
|
+
add_offense(subject_nodes.first, message: MSG_FIRST)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def check_example_expects(node)
|
|
67
|
+
subjects = ancestral_named_subjects(node)
|
|
68
|
+
return if subjects.empty?
|
|
69
|
+
|
|
70
|
+
each_expect_expression(node) do |expression|
|
|
71
|
+
matching = subjects.find { |subject_node| same_body?(subject_node, expression) }
|
|
72
|
+
next unless matching
|
|
73
|
+
|
|
74
|
+
body = subject_body_node(matching)
|
|
75
|
+
# Multiple evaluations of the same call need the raw body (memoized subject would lie).
|
|
76
|
+
next if body_occurrences_in_example(node, body) > 1
|
|
77
|
+
|
|
78
|
+
add_offense(expression, message: format(MSG_REPEAT, name: subject_name(matching)))
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def body_occurrences_in_example(example_node, body_node)
|
|
83
|
+
target = normalize(body_node.source)
|
|
84
|
+
example_node.each_descendant(:send).count { |send_node| normalize(send_node.source) == target }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def subject_declaration?(node)
|
|
88
|
+
return subject_send?(node) if node.send_type?
|
|
89
|
+
return false unless node.block_type?
|
|
90
|
+
|
|
91
|
+
subject_send?(node.send_node)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def subject_send?(node) = node.send_type? && node.receiver.nil? && SUBJECT_METHODS.include?(node.method_name)
|
|
95
|
+
|
|
96
|
+
def named_subject?(node) = subject_send_node(node).first_argument&.sym_type?
|
|
97
|
+
|
|
98
|
+
def subject_name(node) = subject_send_node(node).first_argument.value
|
|
99
|
+
|
|
100
|
+
def subject_send_node(node) = (node.block_type? ? node.send_node : node)
|
|
101
|
+
|
|
102
|
+
def subject_body_node(node) = (node.block_type? ? node.body : nil)
|
|
103
|
+
|
|
104
|
+
def ancestral_named_subjects(node)
|
|
105
|
+
subjects = []
|
|
106
|
+
|
|
107
|
+
node.each_ancestor(:block) do |ancestor|
|
|
108
|
+
next unless example_group?(ancestor)
|
|
109
|
+
|
|
110
|
+
block_statement_children(ancestor).each do |statement|
|
|
111
|
+
next unless subject_declaration?(statement) && named_subject?(statement)
|
|
112
|
+
|
|
113
|
+
subjects << statement
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
subjects
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def each_expect_expression(example_node)
|
|
121
|
+
example_node.each_descendant(:send) do |send_node|
|
|
122
|
+
next unless send_node.method_name == :expect && send_node.receiver.nil?
|
|
123
|
+
|
|
124
|
+
block = expect_block(send_node)
|
|
125
|
+
if block
|
|
126
|
+
yield(block.body) if block.body
|
|
127
|
+
elsif send_node.first_argument
|
|
128
|
+
yield(send_node.first_argument)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def expect_block(send_node)
|
|
134
|
+
parent = send_node.parent
|
|
135
|
+
parent if parent.block_type? && parent.send_node == send_node
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def same_body?(subject_node, expression)
|
|
139
|
+
body = subject_body_node(subject_node)
|
|
140
|
+
return false unless body
|
|
141
|
+
return false unless actionable_expression?(expression)
|
|
142
|
+
|
|
143
|
+
normalize(body.source) == normalize(expression.source)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def actionable_expression?(node)
|
|
147
|
+
return false unless node.send_type?
|
|
148
|
+
|
|
149
|
+
described_class_call?(node) ||
|
|
150
|
+
(node.receiver.nil? && HTTP_METHODS.include?(node.method_name)) ||
|
|
151
|
+
(node.method_name == :call && node.receiver&.const_type?)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def described_class_call?(node)
|
|
155
|
+
return true if node.method_name == :call && described_class_receiver?(node.receiver)
|
|
156
|
+
|
|
157
|
+
%i[perform_now perform_later].include?(node.method_name) && described_class_receiver?(node.receiver)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def described_class_receiver?(receiver)
|
|
161
|
+
receiver&.send_type? && receiver.receiver.nil? && receiver.method_name == :described_class
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def normalize(source) = source.gsub(/\s+/, ' ').strip
|
|
165
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Prefer `I18n.t('activerecord.attributes...')` over `human_attribute_name`.
|
|
4
|
+
#
|
|
5
|
+
# @example
|
|
6
|
+
# # bad
|
|
7
|
+
# Student.human_attribute_name(:name)
|
|
8
|
+
#
|
|
9
|
+
# # good
|
|
10
|
+
# I18n.t('activerecord.attributes.student.name')
|
|
11
|
+
class RuboCop::Cop::Munra::NoHumanAttributeName < RuboCop::Cop::Base
|
|
12
|
+
MSG = 'Use `I18n.t(\'activerecord.attributes...\')` instead of `human_attribute_name`.'
|
|
13
|
+
|
|
14
|
+
RESTRICT_ON_SEND = %i[human_attribute_name].freeze
|
|
15
|
+
|
|
16
|
+
def on_send(node)
|
|
17
|
+
return if defining_human_attribute_name?(node)
|
|
18
|
+
|
|
19
|
+
add_offense(node)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def defining_human_attribute_name?(node)
|
|
25
|
+
node.each_ancestor(:defs) do |defs|
|
|
26
|
+
return true if defs.method_name == :human_attribute_name
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
false
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Flags a method chain broken with a leading dot.
|
|
4
|
+
#
|
|
5
|
+
# @example
|
|
6
|
+
# # bad
|
|
7
|
+
# user
|
|
8
|
+
# .name
|
|
9
|
+
#
|
|
10
|
+
# # good
|
|
11
|
+
# user.
|
|
12
|
+
# name
|
|
13
|
+
class RuboCop::Cop::Munra::NoLeadingDot < RuboCop::Cop::Base
|
|
14
|
+
MSG = 'Do not break method chains with a leading dot on a new line.'
|
|
15
|
+
|
|
16
|
+
def on_new_investigation
|
|
17
|
+
processed_source.lines.each_with_index do |line, index|
|
|
18
|
+
next if line.strip.start_with?('#')
|
|
19
|
+
next unless line.match?(/\A\s+\./)
|
|
20
|
+
|
|
21
|
+
range = processed_source.buffer.line_range(index + 1)
|
|
22
|
+
add_offense(range)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Disallows `def` in a spec.
|
|
4
|
+
#
|
|
5
|
+
# Use `let` or a helper instead of defining a method in the spec file.
|
|
6
|
+
#
|
|
7
|
+
# @example
|
|
8
|
+
# # bad
|
|
9
|
+
# def helper
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# # bad
|
|
13
|
+
# def self.helper
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# # good
|
|
17
|
+
# let(:user) { build_user }
|
|
18
|
+
class RuboCop::Cop::Munra::NoSpecMethod < RuboCop::Cop::Base
|
|
19
|
+
MSG = 'Do not define methods in spec files; use `let` and helpers.'
|
|
20
|
+
|
|
21
|
+
def on_def(node)
|
|
22
|
+
add_offense(node)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def on_defs(node)
|
|
26
|
+
add_offense(node)
|
|
27
|
+
end
|
|
28
|
+
end
|