Skip to content
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

Fix crash in Style/ExplicitBlockArgument #8826

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [#8809](https://github.com/rubocop-hq/rubocop/pull/8809): Fix multiple offense detection for `Style/For`. ([@pbernays][])
* [#8801](https://github.com/rubocop-hq/rubocop/issues/8801): Fix `Layout/SpaceAroundEqualsInParameterDefault` only registered once in a line. ([@rdunlop][])
* [#8514](https://github.com/rubocop-hq/rubocop/issues/8514): Correct multiple `Style/MethodDefParentheses` per file. ([@rdunlop][])
* [#8825](https://github.com/rubocop-hq/rubocop/issues/8825): Fix crash in `Style/ExplicitBlockArgument` when code is called outside of a method. ([@ghiculescu][])

### Changes

Expand Down Expand Up @@ -4942,3 +4943,4 @@
[@tleish]: https://github.com/tleish
[@pbernays]: https://github.com/pbernays
[@rdunlop]: https://github.com/rdunlop
[@ghiculescu]: https://github.com/ghiculescu
8 changes: 6 additions & 2 deletions lib/rubocop/cop/style/explicit_block_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ def on_yield(node)
yielding_block?(block_node) do |send_node, block_args, yield_args|
return unless yielding_arguments?(block_args, yield_args)

def_node = block_node.each_ancestor(:def, :defs).first
# if `yield` is being called outside of a method context, ignore
# this is not a valid ruby pattern, but can happen in haml or erb,
# so this can cause crashes in haml_lint
return unless def_node

add_offense(block_node) do |corrector|
corrector.remove(block_body_range(block_node, send_node))

add_block_argument(send_node, corrector)

def_node = block_node.each_ancestor(:def, :defs).first
add_block_argument(def_node, corrector) if @def_nodes.add?(def_node)
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/style/explicit_block_argument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,12 @@ def m
end
RUBY
end

it 'does not register an offense when code is called outside of a method' do
expect_no_offenses(<<~RUBY)
render("partial") do
yield
end
RUBY
end
end