Class: RuboCop::Cop::Homebrew::NoBase64 Private

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
rubocops/no_base64.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Enforces the use of String#unpack1 and Array#pack over the base64 gem, which Homebrew no longer includes.

Example

# bad
require "base64"
Base64.decode64(encoded)
Base64.strict_encode64(decoded)

# good
encoded.unpack1("m")
[decoded].pack("m0")

Constant Summary collapse

MSG =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

"Homebrew no longer includes the `base64` gem; " \
"use `String#unpack1` or `Array#pack` instead."

Instance Method Summary collapse

Instance Method Details

#on_const(node) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::ConstNode)


47
48
49
50
51
52
53
54
55
56
# File 'rubocops/no_base64.rb', line 47

def on_const(node)
  return unless top_level_const?(node, :Base64)

  parent = node.parent
  return if parent.is_a?(RuboCop::AST::SendNode) && parent.receiver == node
  # Formulae for base64 tools are legitimately named `Base64`.
  return if parent.is_a?(RuboCop::AST::ClassNode) && parent.identifier == node

  add_offense(node)
end

#on_send(node) ⇒ void Also known as: on_csend

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'rubocops/no_base64.rb', line 30

def on_send(node)
  if require_base64?(node)
    add_offense(node) do |corrector|
      parent = node.parent
      next if parent && !parent.begin_type?

      corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
    end
  elsif top_level_const?(node.receiver, :Base64)
    add_offense(node) do |corrector|
      autocorrect_base64_call(corrector, node)
    end
  end
end