Module: Utils::AST Private

Extended by:
T::Sig
Included in:
FormulaAST
Defined in:
utils/ast.rb

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

Class Method Summary collapse

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:



23
24
25
26
27
28
29
30
31
# File 'utils/ast.rb', line 23

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)


65
66
67
68
69
70
71
72
73
74
75
76
# File 'utils/ast.rb', line 65

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)


60
61
62
# File 'utils/ast.rb', line 60

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:



45
46
47
48
49
50
# File 'utils/ast.rb', line 45

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:



34
35
36
37
38
39
40
41
42
# File 'utils/ast.rb', line 34

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.indent(indent) if indent && !text.match?(/\A\n* +/)
  text
end