class RuboCop::Cop::Lint::AmbiguousBlockAssociation
expect { do_something }.to not_change { object.attribute }
expect { do_something }.to change { object.attribute }
# good
@example AllowedPatterns: [‘change’]
expect { do_something }.to change { object.attribute }
# bad
@example AllowedPatterns: [] (default)
expect { do_something }.to change { object.attribute }
# good
@example AllowedMethods: [change]
expect { do_something }.to change { object.attribute }
# bad
@example AllowedMethods: [] (default)
foo = ->(bar) { bar.baz }
# Lambda arguments require no disambiguation
# good
foo == bar { |b| b.baz }
# Operator methods require no disambiguation
# good
some_method(a) { |val| puts val }
# or (different meaning)
some_method(a { |val| puts val })
# With parentheses, there’s no ambiguity.
# good
some_method a { |val| puts val }
# bad
@example
By default, there are no methods to allowed.
This cop can customize allowed methods with ‘AllowedMethods`.
when param passed without parentheses.
Checks for ambiguous block association with method
def allowed_method_pattern?(node)
def allowed_method_pattern?(node) node.assignment? || node.operator_method? || node.method?(:[]) || allowed_method?(node.last_argument.method_name) || matches_allowed_pattern?(node.last_argument.send_node.source) end
def ambiguous_block_association?(send_node)
def ambiguous_block_association?(send_node) send_node.last_argument.any_block_type? && !send_node.last_argument.send_node.arguments? end
def message(send_node)
def message(send_node) block_param = send_node.last_argument format(MSG, param: block_param.source, method: block_param.send_node.source) end
def on_send(node)
def on_send(node) return unless node.arguments? return unless ambiguous_block_association?(node) return if node.parenthesized? || node.last_argument.lambda_or_proc? || allowed_method_pattern?(node) message = message(node) add_offense(node, message: message) do |corrector| wrap_in_parentheses(corrector, node) end end
def wrap_in_parentheses(corrector, node)
def wrap_in_parentheses(corrector, node) range = node.loc.selector.end.join(node.first_argument.source_range.begin) corrector.remove(range) corrector.insert_before(range, '(') corrector.insert_after(node.last_argument, ')') end