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

Begin/Rescue/End block inside of a Lambda raises Style/RedundantBegin and formatter borks it #6192

Closed
tadiou opened this issue Aug 17, 2018 · 1 comment
Assignees
Labels

Comments

@tadiou
Copy link

tadiou commented Aug 17, 2018

Expected behavior

coerce_input ->(value, _ctx) do
  begin
    value.nil? ? nil : JSON.parse(value)
  rescue JSON::ParserError
    raise GraphQL::CoercionError, "cannot coerce `#{value.inspect}` to Hash value"
  end
end

is valid code (when stabby syntax is preferred in config).

Actual behavior

After format it looks like this

coerce_input ->(value, _ctx) do
  
    value.nil? ? nil : JSON.parse(value)
  rescue JSON::ParserError
    raise GraphQL::CoercionError, "cannot coerce `#{value.inspect}` to Hash value"
  
end 

which is invalid as heck.

Additional Info

Ruby's major GraphQL implementation often uses lambdas in arguments, we ended up sticking a begin/rescue/end block in one (because it works).

When it's separated by braces, a la

coerce_method ->(value, _ctx) {
  begin
    value.nil? ? nil : JSON.parse(value)
  rescue JSON::ParserError
    raise GraphQL::CoercionError, "cannot coerce `#{value.inspect}` to Hash value"
  end
}

It works fine. Or if it was forced to be

coerce_input lambda(value, _ctx) do
  value.nil? ? nil : JSON.parse(value)
rescue JSON::ParseError
  raise GraphQL::CoercionError, "cannot coerce `#{value.inspect}` to Hash value"
end

Basically it tries to handle the stabby syntax in the same way that's allowed by the lambda syntax and ruby doesn't handle them the same way at all.

This exists in the same format whether it's being used as an argument or assigning the lambda to a variable.

RuboCop version

0.58.2 (using Parser 2.5.1.2, running on ruby 2.5.1 x86_64-linux)

@Drenmi Drenmi added the bug label Aug 20, 2018
@Drenmi
Copy link
Collaborator

Drenmi commented Aug 20, 2018

Good catch. Will have a look at this.

@Drenmi Drenmi self-assigned this Aug 20, 2018
Drenmi added a commit to Drenmi/rubocop that referenced this issue Aug 20, 2018
Stabby lambda blocks don't support the implicit rescue syntax.
This change makes the cop ignore such cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants