Skip to content

Support itblock #2077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

Earlopain
Copy link
Contributor

All cops that handle numblock now also handle itblock. itblock is relatively new and the required rubocop-ast version is required via RuboCop 1.75


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

@Earlopain Earlopain requested a review from a team as a code owner April 11, 2025 10:31
return unless example_group_with_body?(node)

check_hooks(node.body) if multiline_block?(node.body)
end

alias on_numblock on_block
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This had no effect, so I removed it. It relies on a previous block and then simply uses next_sibling together with any_block (which includes itblock). I only added tests to show it already works.

@@ -38,7 +38,6 @@ def on_block(node)
on_metadata_arguments(metadata_arguments)
end
end
alias on_numblock on_block
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module is included in 4 cops and none have tests for numblocks. So I simply removed this one.

I don't think handling these blocks makes sense in context anyways and it was only added here because of the internal affairs cop

@@ -39,5 +39,5 @@ Gem::Specification.new do |spec|
}

spec.add_dependency 'lint_roller', '~> 1.1'
spec.add_dependency 'rubocop', '~> 1.72', '>= 1.72.1'
spec.add_dependency 'rubocop', '~> 1.75'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.75 transitively depends on rubocop-ast >= 1.43.0 which is good enough

@Earlopain
Copy link
Contributor Author

I guess I should clarify: itblock is foo { it }, where it is the same as _1 for numblock. It's a separate node type.

@@ -44,6 +44,11 @@ module SpecHelper

config.include_context 'with default RSpec/Language config', :config
config.include_context 'smoke test', type: :cop_spec

# whitequark/parser is syntax compatible with 2.0 to 3.3.
if ENV['PARSER_ENGINE'] != 'parser_prism'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parser gem doesn't support 3.4-specific syntax. RuboCop now always uses prism for these versions, so some tests that use newer syntax may fail with parser.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we remove this, what fails?

The ultimate goal is to be running those new specs at least in one CI job. What job would it be?

We can still check against 3.4 syntax and use prism even if we run ‘rubocop’ in 3.1, right?

How do we set the “Ruby version of the code being investigated”? Same as the runtime Ruby?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we remove this, what fails?

I was a bit overeager with adding the excludes. There are only two failures for two cops, so I removed it from the others. The others pass by chance because itblock is simply block with zero block arguments from the perspective of parser.

The ultimate goal is to be running those new specs at least in one CI job. What job would it be?

It will run only in the prism job from workflows/main.yml since it will fail on the parser one. It sets PARSER_ENGINE=parser_prism which is used by the specs.

We can still check against 3.4 syntax and use prism even if we run ‘rubocop’ in 3.1, right?
How do we set the “Ruby version of the code being investigated”? Same as the runtime Ruby?

These two points I don't understand what you are asking, can you elaborate?

@Earlopain
Copy link
Contributor Author

Hm, coverage is reduced because when using the parser gem on_itblock will never get called (rather, its interpreted as a plain block node). Any tips on how to solve this?


context 'when Ruby 3.4', :ruby34, unsupported_on: :parser do
it 'does not flag :example for hooks' do
expect_no_offenses(<<~RUBY)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please change this to ‘it.run’ not to offend the other cop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copy-pasted the current numblock specs, will change these as well.

expect_offense(<<~RUBY)
RSpec.describe User do
it { is_expected.to be_after_around_hook }
around { it }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this would work anyway without making a special case for an it-block? Looks like a regular no-args block call with an ‘it’ call inside. No surprise, this may or may not be an it-block, hard to tell.

Side note, how does Ruby knows ‘describe { it { foo } }’ is not an it-block?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By what I propose here to keep the spec, and attempt to remove special handling in the cop. Chances are it will work anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that's what I did: #2077 (comment)

Side note, how does Ruby knows ‘describe { it { foo } }’ is not an it-block?

it with block had special considerations from ruby itself, it only points to the block argument when it doesn't take a block itself. https://bugs.ruby-lang.org/issues/18980

The following cases have been discussed:

  • it method, most famously in RSpec: You almost always pass a positional and/or block argument to RSpec's it, so the conflict is avoided with my proposal. You virtually never use a completely naked it

All cops that handle numblock now also handle itblock.
`itblock` is relatively new and the required `rubocop-ast` version is required via RuboCop 1.75
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants