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.

@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

@Earlopain Earlopain force-pushed the itblock-support branch 2 times, most recently from ba0f9df to 89a75bf Compare May 21, 2025 12:36
@Earlopain
Copy link
Contributor Author

Earlopain commented May 21, 2025

Coverage is no longer an issue (rubocop/rubocop#14119) but now there's a gnarly-looking ruby-head failure I'm 100% certain is not my fault (: (https://bugs.ruby-lang.org/issues/21357)

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