class RuboCop::AST::NodePattern::Compiler::NodePatternSubcompiler

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/rubocop/ast/node_pattern/compiler/node_pattern_subcompiler.rbs

class RuboCop::AST::NodePattern::Compiler::NodePatternSubcompiler < RuboCop::AST::NodePattern::Compiler::Subcompiler
  def access_element: () -> String
  def access_node: () -> String
  def compile_args: (Array[] arg_list, first: nil) -> String
  def compile_guard_clause: () -> String
  def compile_value_match: (String value) -> String
  def initialize: (RuboCop::AST::NodePattern::Compiler compiler, var: String?, access: String, seq_head: bool) -> void
  def multiple_access: (Symbol kind) -> String
  def visit_capture: () -> String
  def visit_node_type: () -> String
  def visit_other_type: () -> String
  def visit_sequence: () -> String
  def visit_union: () -> String
end

/docs/modules/ROOT/pages/node_pattern.adoc
Doc on how this fits in the compiling process:
or it’s ‘node.type` if `seq_head` is true
for a given value `var` (typically a RuboCop::AST::Node)
Compiles code that evalues to true or false

def access_element

Experimental RBS support (using type sampling data from the type_fusion project).

def access_element: () -> String

This signature was generated using 2 samples from 1 application.

def access_element
  seq_head ? "#{access}.type" : access
end

def access_node

Experimental RBS support (using type sampling data from the type_fusion project).

def access_node: () -> String

This signature was generated using 2 samples from 1 application.

def access_node
  return access if seq_head
  "#{compile_guard_clause} && #{access}"
end

def compile_args(arg_list, first: nil)

Experimental RBS support (using type sampling data from the type_fusion project).

def compile_args: ( arg_list, first: nil) -> String

This signature was generated using 1 sample from 1 application.

Returns:
  • (String, nil) -

Parameters:
  • (Array, nil) --
def compile_args(arg_list, first: nil)
  args = arg_list&.map { |arg| compiler.compile_as_atom(arg) }
  args = [first, *args] if first
  "(#{args.join(', ')})" if args
end

def compile_guard_clause

Experimental RBS support (using type sampling data from the type_fusion project).

def compile_guard_clause: () -> String

This signature was generated using 3 samples from 1 application.

def compile_guard_clause
  "#{access}.is_a?(::RuboCop::AST::Node)"
end

def compile_value_match(value)

Experimental RBS support (using type sampling data from the type_fusion project).

def compile_value_match: (String value) -> String

This signature was generated using 1 sample from 1 application.

def compile_value_match(value)
  "#{value} === #{access_element}"
end

def initialize(compiler, var: nil, access: var, seq_head: false)

Experimental RBS support (using type sampling data from the type_fusion project).

def initialize: (RuboCop::AST::NodePattern::Compiler compiler, var: String?, access: String, seq_head: bool) -> void

This signature was generated using 5 samples from 1 application.

def initialize(compiler, var: nil, access: var, seq_head: false)
  super(compiler)
  @var = var
  @access = access
  @seq_head = seq_head
end

def multiple_access(kind)

Experimental RBS support (using type sampling data from the type_fusion project).

def multiple_access: (Symbol kind) -> String

This signature was generated using 1 sample from 1 application.

def multiple_access(kind)
  return yield @var if @var
  compiler.with_temp_variables(kind) do |var|
    memo = "#{var} = #{access}"
    @var = @access = var
    "(#{memo}; #{yield @var})"
  end
end

def visit_ascend

def visit_ascend
  compiler.with_temp_variables do |ascend|
    expr = compiler.compile_as_node_pattern(node.child, var: ascend)
    "(#{ascend} = #{access_node}) && (#{ascend} = #{ascend}.parent) && #{expr}"
  end
end

def visit_capture

Experimental RBS support (using type sampling data from the type_fusion project).

def visit_capture: () -> String

This signature was generated using 3 samples from 1 application.

def visit_capture
  "(#{compiler.next_capture} = #{access_element}; #{compile(node.child)})"
end

def visit_descend

def visit_descend
  compiler.with_temp_variables { |descendant| <<~RUBY.chomp }
    ::RuboCop::AST::NodePattern.descend(#{access}).any? do |#{descendant}|
      #{compiler.compile_as_node_pattern(node.child, var: descendant)}
    end
  RUBY
end

def visit_function_call

def visit_function_call
  "#{node.method_name}#{compile_args(node.arg_list, first: access_element)}"
end

def visit_intersection

def visit_intersection
  multiple_access(:intersection) do
    node.children.map { |child| compile(child) }
        .join(' && ')
  end
end

def visit_negation

def visit_negation
  expr = compile(node.child)
  "!(#{expr})"
end

def visit_node_type

Experimental RBS support (using type sampling data from the type_fusion project).

def visit_node_type: () -> String

This signature was generated using 2 samples from 1 application.

def visit_node_type
  "#{access_node}.#{node.child.to_s.tr('-', '_')}_type?"
end

def visit_other_type

Experimental RBS support (using type sampling data from the type_fusion project).

def visit_other_type: () -> String

This signature was generated using 1 sample from 1 application.

Assumes other types are atoms.
def visit_other_type
  value = compiler.compile_as_atom(node)
  compile_value_match(value)
end

def visit_predicate

def visit_predicate
  "#{access_element}.#{node.method_name}#{compile_args(node.arg_list)}"
end

def visit_sequence

Experimental RBS support (using type sampling data from the type_fusion project).

def visit_sequence: () -> String

This signature was generated using 1 sample from 1 application.

def visit_sequence
  multiple_access(:sequence) do |var|
    term = compiler.compile_sequence(node, var: var)
    "#{compile_guard_clause} && #{term}"
  end
end

def visit_unify

def visit_unify
  name = compiler.bind(node.child) do |unify_name|
    # double assign to avoid "assigned but unused variable"
    return "(#{unify_name} = #{access_element}; #{unify_name} = #{unify_name}; true)"
  end
  compile_value_match(name)
end

def visit_union

Experimental RBS support (using type sampling data from the type_fusion project).

def visit_union: () -> String

This signature was generated using 1 sample from 1 application.

def visit_union
  multiple_access(:union) do
    terms = compiler.each_union(node.children)
                    .map { |child| compile(child) }
    "(#{terms.join(' || ')})"
  end
end

def visit_wildcard

def visit_wildcard
  'true'
end