class RuboCop::Cop::Style::TrailingUnderscoreVariable
a, *b, _ = foo() => The correction ‘a, *b, = foo()` is a syntax error
*a, b, _ = foo() => We need to know to not include 2 variables in a
a, = foo()
a, b, = foo()
#good
a, _, _, = foo()
a, _, _ = foo()
a, b, _, = foo()
a, b, _ = foo()
# bad
@example
This cop checks for extra underscores in variable assignment.
def allow_named_underscore_variables
def allow_named_underscore_variables @allow_named_underscore_variables ||= cop_config['AllowNamedUnderscoreVariables'] end
def autocorrect(node)
def autocorrect(node) lambda do |corrector| range = unneeded_range(node) corrector.remove(range) if range end end
def find_first_offense(variables)
def find_first_offense(variables) first_offense = nil variables.reverse_each do |variable| var, = *variable var, = *var if allow_named_underscore_variables break unless var == :_ else break unless var.to_s.start_with?(UNDERSCORE) end first_offense = variable end return nil if first_offense.nil? first_offense_index = variables.index(first_offense) 0.upto(first_offense_index - 1).each do |index| return nil if variables[index].splat_type? end first_offense end
def on_masgn(node)
def on_masgn(node) return unless (range = unneeded_range(node)) good_code = node.source offset = range.begin_pos - node.source_range.begin_pos good_code[offset, range.size] = '' add_offense(node, range, format(MSG, good_code)) end
def unneeded_range(node)
def unneeded_range(node) left, right = *node variables = *left first_offense = find_first_offense(variables) return unless first_offense end_position = if first_offense.source_range == variables.first.source_range right.source_range.begin_pos else node.loc.operator.begin_pos end range = Parser::Source::Range.new(node.source_range.source_buffer, first_offense.source_range.begin_pos, end_position) range_with_surrounding_space(range, :right) end