rubocop-vicenzo 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 93a0df32e2fd3e164d0b7380c341fe12eb61a46143f4ec15f990961d72d1a611
4
+ data.tar.gz: ff138db061137acee6d7067d4cb9e3301d36f6ea33dac8cd494d9a616c8c2ac6
5
+ SHA512:
6
+ metadata.gz: 0b5ddd5178c5866f85d86624f5709cb52a51444b2077363e2a73242feaeb80933e435e8009edbff123e786f01a671e7b7e0d8946e1e06096cb7dc747967f9ab1
7
+ data.tar.gz: d79145ff57444fa82a3a4dd89375821b77755841eeac1b373698784bab36a7c4448f609c4733de6b1206460684d3db3ab423b234ef661ad423ac47ea4df38c65
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ plugins:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
5
+ - rubocop-vicenzo
6
+ - rubocop-internal_affairs
7
+
8
+ AllCops:
9
+ NewCops: enable
10
+
11
+ Naming/FileName:
12
+ Exclude:
13
+ - lib/rubocop-vicenzo.rb
14
+
15
+ RSpec/ContextWording:
16
+ Prefixes:
17
+ - and
18
+ - but
19
+ - when
20
+ - with
21
+ - without
22
+
23
+ RSpec/ExampleLength:
24
+ Enabled: false
25
+
26
+ RSpec/NestedGroups:
27
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-04-02
4
+
5
+ - Initial release;
6
+ - Add RoboCop::Cop::Vicenzo::RSpec::NestedLetRedefinition #1;
7
+ - Add RoboCop::Cop::Vicenzo::RSpec::NestedSubjectRedefinition #2;
8
+ - Add RuboCop::Cop::Vicenzo::Rails::EnumInclusionOfValidation #3;
9
+ - Add RuboCop::Cop::Vicenzo::RSpec::NestedContextImproperStart #4;
10
+ - Add RuboCop::Cop::Vicenzo::RSpec::MixedExampleGroups #6;
11
+
12
+ - Change RuboCop::Cop::Vicenzo::RSpec::NestedContextImproperStart inherits from Rspec::Base #5;
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Bruno Vicenzo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # RuboCop::Vicenzo
2
+
3
+ [![Ruby](https://github.com/bvicenzo/rubocop-vicenzo/actions/workflows/main.yml/badge.svg)](https://github.com/bvicenzo/rubocop-vicenzo/actions/workflows/main.yml)
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ ```bash
10
+ bundle add rubocop-vicenzo --group=development --require=false
11
+ ```
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ ```bash
16
+ gem install rubocop-vicenzo
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ You need to tell RuboCop to load the Vicenzo extension. There are three
22
+ ways to do this:
23
+
24
+ ### RuboCop configuration file
25
+
26
+ Put this into your `.rubocop.yml`.
27
+
28
+ ```yaml
29
+ plugins: rubocop-vicenzo
30
+ ```
31
+
32
+ Alternatively, use the following array notation when specifying multiple extensions.
33
+
34
+ ```yaml
35
+ plugins:
36
+ - rubocop-other-extension
37
+ - rubocop-vicenzo
38
+ ```
39
+
40
+ Now you can run `rubocop` and it will automatically load the RuboCop Vicenzo
41
+ cops together with the standard cops.
42
+
43
+ > [!NOTE]
44
+ > The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`.
45
+
46
+ ### Command line
47
+
48
+ ```bash
49
+ rubocop --plugin rubocop-vicenzo
50
+ ```
51
+
52
+ ## Development
53
+
54
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
55
+
56
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
57
+
58
+ ### Creating a new cop
59
+
60
+ ```bash
61
+ bundle exec rake 'new_cop[Vicenzo/OptionalNamespace/CopName]'
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rubocop-vicenzo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rubocop-vicenzo/blob/master/CODE_OF_CONDUCT.md).
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
71
+
72
+ ## Code of Conduct
73
+
74
+ Everyone interacting in the Rubocop::Vicenzo project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rubocop-vicenzo/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/setup'
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |t|
8
+ t.rspec_opts = '--format progress'
9
+ end
10
+
11
+ require 'rubocop/rake_task'
12
+
13
+ RuboCop::RakeTask.new
14
+
15
+ task default: %i[spec rubocop]
16
+
17
+ desc 'Generate a new cop with a template'
18
+ task :new_cop, [:cop] do |_task, args|
19
+ require 'rubocop'
20
+
21
+ cop_name = args.fetch(:cop) do
22
+ warn 'usage: bundle exec rake new_cop[Department/Name]'
23
+ exit!
24
+ end
25
+
26
+ generator = RuboCop::Cop::Generator.new(cop_name)
27
+
28
+ generator.write_source
29
+ generator.write_spec
30
+ generator.inject_require(root_file_path: 'lib/rubocop/cop/vicenzo_cops.rb')
31
+ generator.inject_config(config_file_path: 'config/default.yml')
32
+
33
+ puts generator.todo
34
+ end
@@ -0,0 +1,25 @@
1
+ Vicenzo/RSpec/MixedExampleGroups:
2
+ Description: 'Check if there are example and groups at same level'
3
+ Enabled: warning
4
+ VersionAdded: '0.1.0'
5
+
6
+ Vicenzo/RSpec/NestedContextImproperStart:
7
+ Description: 'Check if the nested context does not start as a root one.'
8
+ Enabled: warning
9
+ VersionAdded: '0.1.0'
10
+
11
+ Vicenzo/RSpec/NestedLetRedefinition:
12
+ Description: 'Check if a let is redefined in a nested example group.'
13
+ Enabled: true
14
+ Severity: warning
15
+ VersionAdded: '0.1.0'
16
+
17
+ Vicenzo/RSpec/NestedSubjectRedefinition:
18
+ Description: 'Check if a subject is redefined in a nested example group.'
19
+ Enabled: warning
20
+ VersionAdded: '0.1.0'
21
+
22
+ Vicenzo/Rails/EnumInclusionOfValidation:
23
+ Description: 'Check if the enum has the inclusion of validation defined.'
24
+ Enabled: convention
25
+ VersionAdded: '0.1.0'
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Vicenzo
6
+ module Rails
7
+ # Ensures that enums using the new syntax include the
8
+ # `validate: { allow_nil: true }` option.
9
+ #
10
+ # ## Bad usage
11
+ #
12
+ # ```ruby
13
+ # enum :status, { active: 1, inactive: 0 }, suffix: true
14
+ # ```
15
+ #
16
+ # ```ruby
17
+ # enum :status, { active: 1, inactive: 0 }, validate: true, suffix: true
18
+ # ```
19
+ #
20
+ # ## Good usage
21
+ #
22
+ # ```ruby
23
+ # enum :status, { active: 1, inactive: 0 }, validate: { allow_nil: true }, suffix: true
24
+ # ```
25
+ #
26
+ # This cop does not enforce validation on enums using the old syntax:
27
+ #
28
+ # ```ruby
29
+ # enum status: { active: 1, inactive: 0 }
30
+ # ```
31
+ class EnumInclusionOfValidation < Base
32
+ extend AutoCorrector
33
+
34
+ MSG_MISSING_VALIDATE = 'Add `validate: { allow_nil: true }` to the enum.'
35
+ MSG_INVALID_VALIDATE = 'The `validate` option for the enum must be `validate: { allow_nil: true }`.'
36
+
37
+ RESTRICT_ON_SEND = [:enum].freeze
38
+
39
+ def on_send(node)
40
+ return unless node.command?(:enum)
41
+
42
+ # Ignore old-style enums
43
+ first_argument = node.first_argument
44
+ return if first_argument&.hash_type?
45
+
46
+ validate_kwarg = find_validate_option(node)
47
+
48
+ register_offence_for(node, validate_kwarg)
49
+ end
50
+ alias on_csend on_send
51
+
52
+ private
53
+
54
+ def find_validate_option(enum_node)
55
+ enum_node.last_argument.each_pair.find { |pair| pair.key.value == :validate }
56
+ end
57
+
58
+ def valid_validate_option?(validate_kwarg)
59
+ validate_kwarg.value.hash_type? &&
60
+ validate_kwarg.value.each_pair.any? do |pair|
61
+ pair.key.value == :allow_nil && pair.value.true_type?
62
+ end
63
+ end
64
+
65
+ def last_hash_node(enum_node)
66
+ enum_node.last_argument if enum_node.last_argument.hash_type?
67
+ end
68
+
69
+ def register_offence_for(enum_node, validate_kwarg)
70
+ if validate_kwarg.nil?
71
+ add_offense(enum_node, message: MSG_MISSING_VALIDATE) do |corrector|
72
+ corrector.insert_after(last_hash_node(enum_node), ', validate: { allow_nil: true }')
73
+ end
74
+ elsif !valid_validate_option?(validate_kwarg)
75
+ add_offense(validate_kwarg, message: MSG_INVALID_VALIDATE) do |corrector|
76
+ corrector.replace(validate_kwarg, 'validate: { allow_nil: true }')
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Vicenzo
6
+ module RSpec
7
+ # Ensures that examples (`it`, `specify`, `example`)
8
+ # are not mixed with groups (`describe`, `context`) at the same level.
9
+ #
10
+ # @example
11
+ # # bad
12
+ # RSpec.describe User do
13
+ # it { is_expected.to validate_presence_of(:name) }
14
+ # describe '#admin?' do
15
+ # it { expect(true).to eq(true) }
16
+ # end
17
+ # end
18
+ #
19
+ # # bad
20
+ # RSpec.describe User do
21
+ # describe '#admin?' do
22
+ # it { expect(true).to eq(true) }
23
+ # context 'when email starts with' do
24
+ # end
25
+ # end
26
+ # end
27
+ #
28
+ # # good
29
+ # RSpec.describe User do
30
+ # describe '#admin?' do
31
+ # context 'when email starts with' do
32
+ # it { expect(true).to eq(true) }
33
+ # end
34
+ # end
35
+ # end
36
+ class MixedExampleGroups < RuboCop::Cop::RSpec::Base
37
+ MSG = 'Do not mix examples (`it`, `specify`, `example`) with groups (`describe`, `context`) ' \
38
+ 'at the same level.'
39
+
40
+ def on_block(node)
41
+ return unless example_or_group?(node)
42
+
43
+ parent = node.parent
44
+ return unless parent
45
+
46
+ children = parent.children.select { |child| example_or_group?(child) }
47
+ example_nodes, group_nodes = children.partition { |n| example?(n) }
48
+
49
+ return if example_nodes.empty? || group_nodes.empty?
50
+
51
+ add_offense(node)
52
+ end
53
+
54
+ alias on_numblock on_block
55
+
56
+ private
57
+
58
+ def example_or_group?(node)
59
+ example?(node) || example_group?(node)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Vicenzo
6
+ module RSpec
7
+ # Checks for nested `context` blocks where the inner `context`
8
+ # starts with "when", "with", or "without". It suggests replacing it with "and", "but", or "however".
9
+ #
10
+ # @example
11
+ # # bad
12
+ # context 'when the product is for sale' do
13
+ # context 'when the color pink is not available' do
14
+ # it 'does not show the pink option'
15
+ # end
16
+ # end
17
+ #
18
+ # # good
19
+ # context 'when the product is for sale' do
20
+ # context 'but the color pink is not available' do
21
+ # it 'does not show the pink option'
22
+ # end
23
+ # end
24
+ class NestedContextImproperStart < RuboCop::Cop::RSpec::Base
25
+ MSG = 'Nested `context` should start with `and`, `but`, or `however`, not `when`, `with`, or `without`.'
26
+
27
+ FORBIDDEN_PREFIXES = %w[when with without].freeze
28
+
29
+ def on_block(node)
30
+ return unless context_block?(node) && context_block?(node.parent)
31
+
32
+ context_description = description_for(node)
33
+ return unless context_description
34
+
35
+ first_word = context_description.split.first&.downcase
36
+ return unless FORBIDDEN_PREFIXES.include?(first_word)
37
+
38
+ add_offense(node.send_node)
39
+ end
40
+
41
+ alias on_numblock on_block
42
+
43
+ private
44
+
45
+ def context_block?(node)
46
+ !node.nil? && node.block_type? && node.send_node.command?(:context)
47
+ end
48
+
49
+ def description_for(context_node)
50
+ description = context_node.send_node.first_argument
51
+
52
+ return if description.nil?
53
+
54
+ description.source.delete_prefix("'").delete_suffix("'")
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Vicenzo
6
+ module RSpec
7
+ # Do not define the same let in nested example groups.
8
+ #
9
+ # It makes the tests more dificult to read and indicates that exists hidden scenarios (contexts)
10
+ #
11
+ # @example `Hidden context`
12
+ #
13
+ # # bad
14
+ #
15
+ # describe '#authorized?' do
16
+ # subject(:action) { create(:action) }
17
+ # let(:user) { create(:user, :admin) }
18
+ #
19
+ # it { expect(action).to be_authorized(user) }
20
+ #
21
+ # context 'when user is not admin' do
22
+ # let(:user) { create(:user, :analyst) }
23
+ #
24
+ # it { expect(action).not_to be_authorized(user) }
25
+ # end
26
+ # end
27
+ #
28
+ # # good
29
+ #
30
+ # describe '#authorized?' do
31
+ # subject(:action) { create(:action) }
32
+ #
33
+ # context 'when user is not admin' do
34
+ # let(:user) { create(:user, :analyst) }
35
+ #
36
+ # it { expect(action).not_to be_authorized(user) }
37
+ # end
38
+ #
39
+ # context 'when user is admin' do # this context was hidden
40
+ # let(:user) { create(:user, :admin) }
41
+ #
42
+ # it { expect(action).to be_authorized(user) }
43
+ # end
44
+ # end
45
+ class NestedLetRedefinition < RuboCop::Cop::RSpec::Base
46
+ MSG = 'Let `:%<name>s` is already defined in ancestor(s) block(s) at: %<definitions>s.'
47
+
48
+ # @!method let_name(node)
49
+ def_node_matcher :let_name, <<~PATTERN
50
+ {
51
+ (block (send nil? #Helpers.all ({str sym} $_) ...) ...)
52
+ (send nil? #Helpers.all ({str sym} $_) block_pass)
53
+ }
54
+ PATTERN
55
+
56
+ # @!method let_it_be_name(node)
57
+ def_node_matcher :let_it_be_name, <<~PATTERN
58
+ {
59
+ (block (send nil? {:let_it_be :let_it_be!} ({str sym} $_) ...) ...)
60
+ (send nil? {:let_it_be :let_it_be!} ({str sym} $_) block_pass)
61
+ }
62
+ PATTERN
63
+
64
+ # @!method let_it_be?(node)
65
+ def_node_matcher :let_it_be?, <<~PATTERN
66
+ {
67
+ (block (send nil? {:let_it_be :let_it_be!} ...) ...)
68
+ (send nil? {:let_it_be :let_it_be!} _ block_pass)
69
+ }
70
+ PATTERN
71
+
72
+ def on_block(node)
73
+ check_let_redefinitions(node, {}) if example_group?(node)
74
+ end
75
+
76
+ alias on_numblock on_block
77
+
78
+ private
79
+
80
+ def check_let_redefinitions(node, let_definitions)
81
+ node.body.each_child_node do |child|
82
+ if child.block_type?
83
+ if example_group?(child)
84
+ check_let_redefinitions(child, let_definitions.dup)
85
+ elsif let?(child) || let_it_be?(child)
86
+ check_let(child, let_definitions)
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ def check_let(let_node, let_definitions)
93
+ name = (let_name(let_node) || let_it_be_name(let_node)).to_s.to_sym
94
+
95
+ if let_definitions.key?(name)
96
+ add_offense(let_node, message: redefined_let_message(name, let_definitions))
97
+ let_definitions[name] << line_location(let_node)
98
+ else
99
+ let_definitions[name] = [line_location(let_node)]
100
+ end
101
+ end
102
+
103
+ def redefined_let_message(name, let_definitions)
104
+ format(MSG, name:, definitions: let_definitions[name].join(', '))
105
+ end
106
+
107
+ def line_location(node)
108
+ node.source_range.line
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Vicenzo
6
+ module RSpec
7
+ # Do not define the same let in nested example groups.
8
+ #
9
+ # It makes the tests more dificult to read and indicates that exists hidden scenarios (contexts)
10
+ #
11
+ # @example `Hidden context`
12
+ #
13
+ # # bad
14
+ #
15
+ # describe '#can_access?' do
16
+ # subject(:user) { create(:user, :admin) }
17
+ #
18
+ # it { expect(user).to can_access(:products) }
19
+ #
20
+ # context 'when user is not admin' do
21
+ # subject(:user) { create(:user, :analyst) }
22
+ #
23
+ # it { expect(user).not_to can_access(:products) }
24
+ # end
25
+ # end
26
+ #
27
+ # # good
28
+ #
29
+ # describe '#can_access?' do
30
+ # context 'when user is not admin' do
31
+ # subject(:user) { create(:user, :analyst) }
32
+ #
33
+ # it { expect(user).not_to can_access(:products) }
34
+ # end
35
+ #
36
+ # context 'when user is admin' do # this context was hidden
37
+ # subject(:user) { create(:user, :admin) }
38
+ #
39
+ # it { expect(action).to can_access(:products) }
40
+ # end
41
+ # end
42
+ class NestedSubjectRedefinition < RuboCop::Cop::RSpec::Base
43
+ MSG = 'Subject `:%<name>s` is already defined in ancestor(s) block(s) at: %<definitions>s.'
44
+
45
+ # @!method subject_name(node)
46
+ # Find a named or unnamed subject definition
47
+ #
48
+ # @example anonymous subject
49
+ # subject_name(parse('subject { foo }').ast) do |name|
50
+ # name # => :subject
51
+ # end
52
+ #
53
+ # @example named subject
54
+ # subject_name(parse('subject(:thing) { foo }').ast) do |name|
55
+ # name # => :thing
56
+ # end
57
+ #
58
+ # @param node [RuboCop::AST::Node]
59
+ #
60
+ # @yield [Symbol] subject name
61
+ def_node_matcher :subject_name, <<-PATTERN
62
+ (block
63
+ (send nil?
64
+ { #Subjects.all (sym $_) | $#Subjects.all }
65
+ ) args ...)
66
+ PATTERN
67
+
68
+ def on_block(node)
69
+ check_subject_redefinitions(node, {}) if example_group?(node)
70
+ end
71
+
72
+ alias on_numblock on_block
73
+
74
+ private
75
+
76
+ def name_for(subject_node)
77
+ name = subject_name(subject_node)
78
+
79
+ name == :subject ? :anonymous : name
80
+ end
81
+
82
+ def check_subject_redefinitions(node, subject_definitions)
83
+ node.body.each_child_node do |child|
84
+ if child.block_type?
85
+ if example_group?(child)
86
+ check_subject_redefinitions(child, subject_definitions.dup)
87
+ elsif subject?(child)
88
+ check_subject(child, subject_definitions)
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ def check_subject(subject_node, subject_definitions)
95
+ name = name_for(subject_node)
96
+
97
+ if subject_definitions.key?(name)
98
+ add_offense(subject_node, message: redefined_subject_message(name, subject_definitions))
99
+ subject_definitions[name] << line_location(subject_node)
100
+ else
101
+ subject_definitions[name] = [line_location(subject_node)]
102
+ end
103
+ end
104
+
105
+ def redefined_subject_message(name, subject_definitions)
106
+ format(MSG, name:, definitions: subject_definitions[name].join(', '))
107
+ end
108
+
109
+ def line_location(node)
110
+ node.source_range.line
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'vicenzo/rspec/mixed_example_groups'
4
+ require_relative 'vicenzo/rspec/nested_context_improper_start'
5
+ require_relative 'vicenzo/rspec/nested_let_redefinition'
6
+ require_relative 'vicenzo/rspec/nested_subject_redefinition'
7
+ require_relative 'vicenzo/rails/enum_inclusion_of_validation'
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lint_roller'
4
+
5
+ module RuboCop
6
+ module Vicenzo
7
+ # A plugin that integrates rubocop-vicenzo with RuboCop's plugin system.
8
+ class Plugin < LintRoller::Plugin
9
+ def about
10
+ LintRoller::About.new(
11
+ name: 'rubocop-vicenzo',
12
+ version: VERSION,
13
+ homepage: 'https://github.com/bvicenzo/rubocop-vicenzo/',
14
+ description: 'Cops created for sharing years of experiments of good practices.'
15
+ )
16
+ end
17
+
18
+ def supported?(context)
19
+ context.engine == :rubocop
20
+ end
21
+
22
+ def rules(_context)
23
+ LintRoller::Rules.new(
24
+ type: :path,
25
+ config_format: :rubocop,
26
+ value: Pathname.new(__dir__).join('../../../config/default.yml')
27
+ )
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Vicenzo
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'vicenzo/version'
4
+
5
+ module RuboCop
6
+ # Vicenzo's Cop
7
+ module Vicenzo
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubocop'
4
+ require 'rubocop-rspec'
5
+
6
+ require_relative 'rubocop/vicenzo'
7
+ require_relative 'rubocop/vicenzo/version'
8
+ require_relative 'rubocop/vicenzo/plugin'
9
+
10
+ require_relative 'rubocop/cop/vicenzo_cops'
@@ -0,0 +1,6 @@
1
+ module Rubocop
2
+ module Vicenzo
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-vicenzo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Vicenzo
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-04-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: lint_roller
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.1'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.1'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rubocop
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.72.2
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 1.72.2
40
+ - !ruby/object:Gem::Dependency
41
+ name: rubocop-rspec
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.5.0
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 3.5.0
54
+ description: Cops with good pratices I have been learning
55
+ email:
56
+ - bruno@alumni.usp.br
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".rspec"
62
+ - ".rubocop.yml"
63
+ - CHANGELOG.md
64
+ - CODE_OF_CONDUCT.md
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - config/default.yml
69
+ - lib/rubocop-vicenzo.rb
70
+ - lib/rubocop/cop/vicenzo/rails/enum_inclusion_of_validation.rb
71
+ - lib/rubocop/cop/vicenzo/rspec/mixed_example_groups.rb
72
+ - lib/rubocop/cop/vicenzo/rspec/nested_context_improper_start.rb
73
+ - lib/rubocop/cop/vicenzo/rspec/nested_let_redefinition.rb
74
+ - lib/rubocop/cop/vicenzo/rspec/nested_subject_redefinition.rb
75
+ - lib/rubocop/cop/vicenzo_cops.rb
76
+ - lib/rubocop/vicenzo.rb
77
+ - lib/rubocop/vicenzo/plugin.rb
78
+ - lib/rubocop/vicenzo/version.rb
79
+ - sig/rubocop/vicenzo.rbs
80
+ homepage: https://github.com/bvicenzo/rubocop-vicenzo
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ allowed_push_host: https://rubygems.org
85
+ homepage_uri: https://github.com/bvicenzo/rubocop-vicenzo
86
+ source_code_uri: https://github.com/bvicenzo/rubocop-vicenzo
87
+ changelog_uri: https://github.com/bvicenzo/rubocop-vicenzo/blob/master/CHANGELOG.md
88
+ rubygems_mfa_required: 'true'
89
+ default_lint_roller_plugin: RuboCop::Vicenzo::Plugin
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 3.1.0
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.6.2
105
+ specification_version: 4
106
+ summary: Cops of Bruno Vicenzo
107
+ test_files: []