Module: Utils::AST Private

Includes:
Kernel
Included in:
FormulaAST
Defined in:
utils/ast.rb,
utils/ast.rbi,
sorbet/rbi/parlour.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: 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
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

#disk_usage_readable, #ensure_executable!, #ensure_formula_installed!, #exec_browser, #exec_editor, #ignore_interrupts, #interactive_shell, #number_readable, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #paths, #pretty_duration, #pretty_installed, #pretty_outdated, #pretty_uninstalled, #quiet_system, #redact_secrets, #redirect_stdout, #require?, #safe_system, #tap_and_name_comparison, #truncate_text_to_approximate_size, #which, #which_all, #which_editor, #with_custom_locale, #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:

Returns:



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

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)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'utils/ast.rb', line 61

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)


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

def component_match?(component_name:, component_type:, target_name:, target_type: nil)
  component_name == target_name && (target_type.nil? || component_type == target_type)
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:



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

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

.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:



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

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