Module: Utils::AST Private

Includes:
Kernel
Included in:
CaskAST, FormulaAST
Defined in:
utils/ast.rb,
utils/ast.rbi

Overview

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

Helper functions for editing Ruby files.

Defined Under Namespace

Classes: CaskAST, FormulaAST

Constant Summary collapse

Node =

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.

RuboCop::AST::Node
SendNode =

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.

RuboCop::AST::SendNode
BlockNode =

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.

RuboCop::AST::BlockNode
DefNode =

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.

RuboCop::AST::DefNode
ProcessedSource =

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.

RuboCop::AST::ProcessedSource
TreeRewriter =

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.

Parser::Source::TreeRewriter

Constants included from Kernel

Kernel::IGNORE_INTERRUPTS_MUTEX

Class Method Summary collapse

Methods included from Kernel

#ensure_executable!, #exec_browser, #exec_editor, #ignore_interrupts, #interactive_shell, #quiet_system, #redirect_stdout, #safe_system, #which, #which_editor, #with_env, #with_homebrew_path

Class Method Details

.body_children(body_node) ⇒ Array<Node>

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.

Parameters:

  • body_node (Node, nil)

Returns:



20
21
22
23
24
25
26
27
28
# File 'utils/ast.rb', line 20

def body_children(body_node)
  if body_node.blank?
    []
  elsif body_node.begin_type?
    body_node.children.compact
  else
    [body_node]
  end
end

.call_node_match?(node, name:, type: nil) ⇒ Boolean

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.

Parameters:

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
86
# File 'utils/ast.rb', line 75

def call_node_match?(node, name:, type: nil)
  node_type = case node
  when SendNode then :method_call
  when BlockNode then :block_call
  else return false
  end

  component_match?(component_name: node.method_name,
                   component_type: node_type,
                   target_name:    name,
                   target_type:    type)
end

.component_match?(component_name:, component_type:, target_name:, target_type: nil) ⇒ Boolean

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.

Parameters:

Returns:

  • (Boolean)


70
71
72
# File 'utils/ast.rb', line 70

def component_match?(component_name:, component_type:, target_name:, target_type: nil)
  component_name == target_name && (target_type.nil? || component_type == target_type)
end

.literal_value(node) ⇒ T.untyped

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.

Parameters:

Returns:

  • (T.untyped)


47
48
49
50
51
52
# File 'utils/ast.rb', line 47

def literal_value(node)
  return node.str_content if node.str_type?
  return T.unsafe(node).value if node.sym_type? || node.numeric_type?

  nil
end

.process_source(source) ⇒ Array<(ProcessedSource, Node)>

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.

Parameters:

Returns:



55
56
57
58
59
60
# File 'utils/ast.rb', line 55

def process_source(source)
  ruby_version = Version.new(HOMEBREW_REQUIRED_RUBY_VERSION).major_minor.to_f
  processed_source = ProcessedSource.new(source, ruby_version)
  root_node = processed_source.ast
  [processed_source, root_node]
end

.ruby_literal(value) ⇒ String

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.

Parameters:

Returns:



42
43
44
# File 'utils/ast.rb', line 42

def ruby_literal(value)
  value.inspect
end

.stanza_text(name, value, indent: nil) ⇒ String

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.

Parameters:

Returns:



31
32
33
34
35
36
37
38
39
# File 'utils/ast.rb', line 31

def stanza_text(name, value, indent: nil)
  text = if value.is_a?(String)
    _, node = process_source(value)
    value if (node.is_a?(SendNode) || node.is_a?(BlockNode)) && node.method_name == name
  end
  text ||= "#{name} #{value.inspect}"
  text = text.gsub(/^(?!$)/, " " * indent) if indent && !text.match?(/\A\n* +/)
  text
end