Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/lib/rubocop/cop/style/unpack_first.rb b/lib/rubocop/cop/style/unpack_first.rb
- index c1d4af411..da0a2e221 100644
- --- a/lib/rubocop/cop/style/unpack_first.rb
- +++ b/lib/rubocop/cop/style/unpack_first.rb
- @@ -28,16 +28,23 @@ module RuboCop
- def_node_matcher :unpack_and_first_element?, <<-PATTERN
- {
- - (send (send (...) :unpack (...)) :first)
- - (send (send (...) :unpack (...)) {:[] :slice :at} (int 0))
- - (send (send (...) :unpack (...)) :take (int 1))
- + (send $(send (...) :unpack $(...)) :first)
- + (send $(send (...) :unpack $(...)) {:[] :slice :at} (int 0))
- + (send $(send (...) :unpack $(...)) :take (int 1))
- }
- PATTERN
- def on_send(node)
- - return unless unpack_and_first_element?(node)
- -
- - add_offense(node)
- + unpack_and_first_element?(node) do |unpack_call, unpack_arg|
- + range =
- + Parser::Source::Range.new(node.loc.expression.source_buffer,
- + unpack_call.loc.expression.end_pos,
- + node.loc.expression.end_pos)
- + message = format(MSG,
- + format: unpack_arg.source,
- + method: range.source)
- + add_offense(node, message: message)
- + end
- end
- def autocorrect(node)
- @@ -47,22 +54,6 @@ module RuboCop
- corrector.replace(node.children[0].loc.selector, 'unpack1')
- end
- end
- -
- - private
- -
- - def message(node)
- - format_arg = node.receiver.first_argument.source
- - accessor_method = case node.method_name
- - when :first
- - '.first'
- - when :[]
- - '[0]'
- - else
- - ".#{node.method_name}(#{node.arguments.first.source})"
- - end
- -
- - format(MSG, format: format_arg, method: accessor_method)
- - end
- end
- end
- end
- diff --git a/spec/rubocop/cop/style/unpack_first_spec.rb b/spec/rubocop/cop/style/unpack_first_spec.rb
- index 7550a3f17..015a603a5 100644
- --- a/spec/rubocop/cop/style/unpack_first_spec.rb
- +++ b/spec/rubocop/cop/style/unpack_first_spec.rb
- @@ -22,7 +22,7 @@ RSpec.describe RuboCop::Cop::Style::UnpackFirst, :config do
- it 'when using `#unpack` with dot and square brackets' do
- expect_offense(<<-RUBY.strip_indent)
- ''.unpack(y).[](0)
- - ^^^^^^^^^^^^^^^^^^ Use `String#unpack1(y)` instead of `String#unpack(y)[0]`.
- + ^^^^^^^^^^^^^^^^^^ Use `String#unpack1(y)` instead of `String#unpack(y).[](0)`.
- RUBY
- end
Add Comment
Please, Sign In to add comment