Guest User

Untitled

a guest
Mar 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. diff --git a/lib/rubocop/cop/style/unpack_first.rb b/lib/rubocop/cop/style/unpack_first.rb
  2. index c1d4af411..da0a2e221 100644
  3. --- a/lib/rubocop/cop/style/unpack_first.rb
  4. +++ b/lib/rubocop/cop/style/unpack_first.rb
  5. @@ -28,16 +28,23 @@ module RuboCop
  6.  
  7. def_node_matcher :unpack_and_first_element?, <<-PATTERN
  8. {
  9. - (send (send (...) :unpack (...)) :first)
  10. - (send (send (...) :unpack (...)) {:[] :slice :at} (int 0))
  11. - (send (send (...) :unpack (...)) :take (int 1))
  12. + (send $(send (...) :unpack $(...)) :first)
  13. + (send $(send (...) :unpack $(...)) {:[] :slice :at} (int 0))
  14. + (send $(send (...) :unpack $(...)) :take (int 1))
  15. }
  16. PATTERN
  17.  
  18. def on_send(node)
  19. - return unless unpack_and_first_element?(node)
  20. -
  21. - add_offense(node)
  22. + unpack_and_first_element?(node) do |unpack_call, unpack_arg|
  23. + range =
  24. + Parser::Source::Range.new(node.loc.expression.source_buffer,
  25. + unpack_call.loc.expression.end_pos,
  26. + node.loc.expression.end_pos)
  27. + message = format(MSG,
  28. + format: unpack_arg.source,
  29. + method: range.source)
  30. + add_offense(node, message: message)
  31. + end
  32. end
  33.  
  34. def autocorrect(node)
  35. @@ -47,22 +54,6 @@ module RuboCop
  36. corrector.replace(node.children[0].loc.selector, 'unpack1')
  37. end
  38. end
  39. -
  40. - private
  41. -
  42. - def message(node)
  43. - format_arg = node.receiver.first_argument.source
  44. - accessor_method = case node.method_name
  45. - when :first
  46. - '.first'
  47. - when :[]
  48. - '[0]'
  49. - else
  50. - ".#{node.method_name}(#{node.arguments.first.source})"
  51. - end
  52. -
  53. - format(MSG, format: format_arg, method: accessor_method)
  54. - end
  55. end
  56. end
  57. end
  58. diff --git a/spec/rubocop/cop/style/unpack_first_spec.rb b/spec/rubocop/cop/style/unpack_first_spec.rb
  59. index 7550a3f17..015a603a5 100644
  60. --- a/spec/rubocop/cop/style/unpack_first_spec.rb
  61. +++ b/spec/rubocop/cop/style/unpack_first_spec.rb
  62. @@ -22,7 +22,7 @@ RSpec.describe RuboCop::Cop::Style::UnpackFirst, :config do
  63. it 'when using `#unpack` with dot and square brackets' do
  64. expect_offense(<<-RUBY.strip_indent)
  65. ''.unpack(y).[](0)
  66. - ^^^^^^^^^^^^^^^^^^ Use `String#unpack1(y)` instead of `String#unpack(y)[0]`.
  67. + ^^^^^^^^^^^^^^^^^^ Use `String#unpack1(y)` instead of `String#unpack(y).[](0)`.
  68. RUBY
  69. end
Add Comment
Please, Sign In to add comment