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
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Disallows `return` and `next` inside a transaction block.
|
|
4
|
+
#
|
|
5
|
+
# Abort the transaction with `raise ActiveRecord::Rollback` instead.
|
|
6
|
+
#
|
|
7
|
+
# @example
|
|
8
|
+
# # bad
|
|
9
|
+
# ApplicationRecord.transaction do
|
|
10
|
+
# return false
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# # bad
|
|
14
|
+
# record.with_lock do
|
|
15
|
+
# next if skip?
|
|
16
|
+
# end
|
|
17
|
+
#
|
|
18
|
+
# # good
|
|
19
|
+
# ApplicationRecord.transaction do
|
|
20
|
+
# raise ActiveRecord::Rollback
|
|
21
|
+
# end
|
|
22
|
+
class RuboCop::Cop::Munra::NoTransactionExit < RuboCop::Cop::Base
|
|
23
|
+
include RuboCop::Cop::Munra::TransactionBlockMixin
|
|
24
|
+
|
|
25
|
+
MSG = 'Do not use `%<keyword>s` in a transaction block; use `raise ActiveRecord::Rollback` to abort.'
|
|
26
|
+
|
|
27
|
+
def on_return(node)
|
|
28
|
+
register_offense(node)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def on_next(node)
|
|
32
|
+
register_offense(node)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def register_offense(node)
|
|
38
|
+
return unless exit_in_direct_transaction_body?(node)
|
|
39
|
+
|
|
40
|
+
add_offense(node, message: format(MSG, keyword: node.type))
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# When a group has a `when success` or `when error` context, sibling
|
|
4
|
+
# `context` blocks must use only those two names.
|
|
5
|
+
#
|
|
6
|
+
# @example
|
|
7
|
+
# # bad
|
|
8
|
+
# RSpec.describe FooService do
|
|
9
|
+
# describe '.call' do
|
|
10
|
+
# context 'when success' do
|
|
11
|
+
# it { }
|
|
12
|
+
# end
|
|
13
|
+
#
|
|
14
|
+
# context 'when validation fails' do
|
|
15
|
+
# it { }
|
|
16
|
+
# end
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# # good
|
|
21
|
+
# RSpec.describe FooService do
|
|
22
|
+
# describe '.call' do
|
|
23
|
+
# context 'when success' do
|
|
24
|
+
# it { }
|
|
25
|
+
# end
|
|
26
|
+
#
|
|
27
|
+
# context 'when error' do
|
|
28
|
+
# it { }
|
|
29
|
+
# end
|
|
30
|
+
# end
|
|
31
|
+
# end
|
|
32
|
+
#
|
|
33
|
+
# # good
|
|
34
|
+
# RSpec.describe FooService do
|
|
35
|
+
# context 'when logged in' do
|
|
36
|
+
# it { }
|
|
37
|
+
# end
|
|
38
|
+
#
|
|
39
|
+
# context 'when logged out' do
|
|
40
|
+
# it { }
|
|
41
|
+
# end
|
|
42
|
+
# end
|
|
43
|
+
class RuboCop::Cop::Munra::SuccessErrorSiblings < RuboCop::Cop::Base
|
|
44
|
+
include RuboCop::Cop::Munra::RSpecHelpers
|
|
45
|
+
|
|
46
|
+
MSG = 'Sibling `context` blocks must be `when success` or `when error` only.'
|
|
47
|
+
|
|
48
|
+
SUCCESS_ERROR = ['when success', 'when error'].freeze
|
|
49
|
+
|
|
50
|
+
def on_block(node)
|
|
51
|
+
return unless example_group?(node)
|
|
52
|
+
return unless restricted_sibling_contexts?(node)
|
|
53
|
+
|
|
54
|
+
block_statement_children(node).each do |child|
|
|
55
|
+
next unless context_block?(child)
|
|
56
|
+
next if SUCCESS_ERROR.include?(context_name(child))
|
|
57
|
+
|
|
58
|
+
add_offense(child)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def restricted_sibling_contexts?(node)
|
|
65
|
+
block_statement_children(node).any? do |child|
|
|
66
|
+
context_block?(child) && SUCCESS_ERROR.include?(context_name(child))
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def context_block?(node)
|
|
71
|
+
return false unless node.block_type?
|
|
72
|
+
|
|
73
|
+
send = node.send_node
|
|
74
|
+
return false unless send
|
|
75
|
+
|
|
76
|
+
send.method_name == :context
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def context_name(node)
|
|
80
|
+
node.send_node.arguments.first&.value&.to_s
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Loads every file under `lib/rubocop/cop/munra/`.
|
|
4
|
+
#
|
|
5
|
+
# Opens the department namespace first so those files can use compact constant
|
|
6
|
+
# paths (`module RuboCop::Cop::Munra::...`). Nested form is required here:
|
|
7
|
+
# compact style needs the parent constants to exist already.
|
|
8
|
+
# `Dir[]` returns paths in alphabetical order, so shared mixins (`00_*`) are
|
|
9
|
+
# defined before the cops that include them. Adding a cop is a new file in
|
|
10
|
+
# that directory; this loader does not need an edit.
|
|
11
|
+
|
|
12
|
+
module RuboCop
|
|
13
|
+
module Cop
|
|
14
|
+
# Department namespace for Munra cops and shared mixins.
|
|
15
|
+
module Munra
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
Dir[File.expand_path('munra/*.rb', __dir__)].each { |file| require file }
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'lint_roller'
|
|
4
|
+
require 'pathname'
|
|
5
|
+
|
|
6
|
+
# Registers rubocop-munra with RuboCop through the LintRoller plugin API.
|
|
7
|
+
#
|
|
8
|
+
# RuboCop calls {#about}, {#supported?}, and {#rules} when `.rubocop.yml`
|
|
9
|
+
# lists `rubocop-munra` under `plugins`. The rules file is
|
|
10
|
+
# `config/default.yml`, which holds the Munra department and each cop's
|
|
11
|
+
# default options. Apps override those keys in their own `.rubocop.yml`.
|
|
12
|
+
class RuboCop::Munra::Plugin < LintRoller::Plugin
|
|
13
|
+
# @return [LintRoller::About] gem name, version, and a short description
|
|
14
|
+
def about
|
|
15
|
+
LintRoller::About.new(
|
|
16
|
+
name: 'rubocop-munra',
|
|
17
|
+
version: RuboCop::Munra::STRING,
|
|
18
|
+
description: 'RuboCop cops for Rails application conventions.'
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# @param context [Object] object responding to `#engine` (for example `:rubocop`)
|
|
23
|
+
# @return [Boolean] true when this plugin should load for the given engine
|
|
24
|
+
def supported?(context)
|
|
25
|
+
context.engine == :rubocop
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @param _context [Object] unused; present for the LintRoller API
|
|
29
|
+
# @return [LintRoller::Rules] path to `config/default.yml`
|
|
30
|
+
def rules(_context)
|
|
31
|
+
project_root = Pathname.new(__dir__).join('../../..')
|
|
32
|
+
|
|
33
|
+
LintRoller::Rules.new(
|
|
34
|
+
type: :path,
|
|
35
|
+
config_format: :rubocop,
|
|
36
|
+
value: project_root.join('config/default.yml')
|
|
37
|
+
)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
# Namespace for the rubocop-munra plugin.
|
|
5
|
+
#
|
|
6
|
+
# Nested so the gemspec can load this file before RuboCop is on the load path.
|
|
7
|
+
# Compact style (`module RuboCop::Munra`) needs that constant to exist already.
|
|
8
|
+
module Munra
|
|
9
|
+
# Gem version. Keep in sync with the changelog.
|
|
10
|
+
VERSION = '0.1.0'
|
|
11
|
+
# String form required by LintRoller::About.
|
|
12
|
+
STRING = VERSION
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Entrypoint for the rubocop-munra gem.
|
|
4
|
+
#
|
|
5
|
+
# Loads RuboCop, the Munra department (helpers and cops), and the LintRoller
|
|
6
|
+
# plugin. Consumer apps should depend on this file via Bundler and list the
|
|
7
|
+
# gem under `plugins` in `.rubocop.yml`. See docs/usage.md.
|
|
8
|
+
|
|
9
|
+
require 'rubocop'
|
|
10
|
+
|
|
11
|
+
require_relative 'rubocop/munra/version'
|
|
12
|
+
require_relative 'rubocop/cop/munra'
|
|
13
|
+
require_relative 'rubocop/munra/plugin'
|
metadata
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rubocop-munra
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- ramonsantos
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-12-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: lint_roller
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.1'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.1'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rubocop
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.72'
|
|
34
|
+
- - "<"
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '2.0'
|
|
37
|
+
type: :runtime
|
|
38
|
+
prerelease: false
|
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '1.72'
|
|
44
|
+
- - "<"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '2.0'
|
|
47
|
+
description: RuboCop cops for Rails application conventions (Munra department).
|
|
48
|
+
email:
|
|
49
|
+
- ramonsantos.pe@gmail.com
|
|
50
|
+
executables: []
|
|
51
|
+
extensions: []
|
|
52
|
+
extra_rdoc_files: []
|
|
53
|
+
files:
|
|
54
|
+
- ".ruby-version"
|
|
55
|
+
- AGENTS.md
|
|
56
|
+
- CHANGELOG.md
|
|
57
|
+
- CODE_OF_CONDUCT.md
|
|
58
|
+
- LICENSE.txt
|
|
59
|
+
- README.md
|
|
60
|
+
- Rakefile
|
|
61
|
+
- config/default.yml
|
|
62
|
+
- docs/modules/ROOT/pages/cops.adoc
|
|
63
|
+
- docs/modules/ROOT/pages/cops_munra.adoc
|
|
64
|
+
- docs/usage.md
|
|
65
|
+
- lib/rubocop-munra.rb
|
|
66
|
+
- lib/rubocop/cop/munra.rb
|
|
67
|
+
- lib/rubocop/cop/munra/00_class_body_helper.rb
|
|
68
|
+
- lib/rubocop/cop/munra/00_rspec_helpers.rb
|
|
69
|
+
- lib/rubocop/cop/munra/00_transaction_block_mixin.rb
|
|
70
|
+
- lib/rubocop/cop/munra/let_layout.rb
|
|
71
|
+
- lib/rubocop/cop/munra/named_subject_layout.rb
|
|
72
|
+
- lib/rubocop/cop/munra/no_human_attribute_name.rb
|
|
73
|
+
- lib/rubocop/cop/munra/no_leading_dot.rb
|
|
74
|
+
- lib/rubocop/cop/munra/no_spec_method.rb
|
|
75
|
+
- lib/rubocop/cop/munra/no_transaction_exit.rb
|
|
76
|
+
- lib/rubocop/cop/munra/success_error_siblings.rb
|
|
77
|
+
- lib/rubocop/munra/plugin.rb
|
|
78
|
+
- lib/rubocop/munra/version.rb
|
|
79
|
+
- sig/rubocop/munra.rbs
|
|
80
|
+
homepage: https://github.com/ramonsantos/rubocop-munra
|
|
81
|
+
licenses:
|
|
82
|
+
- MIT
|
|
83
|
+
metadata:
|
|
84
|
+
homepage_uri: https://github.com/ramonsantos/rubocop-munra
|
|
85
|
+
source_code_uri: https://github.com/ramonsantos/rubocop-munra
|
|
86
|
+
default_lint_roller_plugin: RuboCop::Munra::Plugin
|
|
87
|
+
rubygems_mfa_required: 'true'
|
|
88
|
+
post_install_message:
|
|
89
|
+
rdoc_options: []
|
|
90
|
+
require_paths:
|
|
91
|
+
- lib
|
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 3.2.0
|
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
|
+
requirements:
|
|
99
|
+
- - ">="
|
|
100
|
+
- !ruby/object:Gem::Version
|
|
101
|
+
version: '0'
|
|
102
|
+
requirements: []
|
|
103
|
+
rubygems_version: 3.4.1
|
|
104
|
+
signing_key:
|
|
105
|
+
specification_version: 4
|
|
106
|
+
summary: RuboCop cops for Rails application conventions (Munra department).
|
|
107
|
+
test_files: []
|