Class: Riml::AST_Rewriter::DefaultParamToIfNode

Inherits:
Riml::AST_Rewriter show all
Defined in:
lib/riml/ast_rewriter.rb

Defined Under Namespace

Classes: SplatVarToCopiedSplatVar

Constant Summary

Constants included from Constants

Constants::BUILTIN_COMMANDS, Constants::BUILTIN_FUNCTIONS, Constants::COMPARISON_OPERATORS, Constants::COMPILED_STRING_LOCATION, Constants::DEFINE_KEYWORDS, Constants::END_KEYWORDS, Constants::IGNORECASE_CAPABLE_OPERATORS, Constants::KEYWORDS, Constants::REGISTERS, Constants::RIML_CLASS_COMMANDS, Constants::RIML_COMMANDS, Constants::RIML_END_KEYWORDS, Constants::RIML_FILE_COMMANDS, Constants::RIML_KEYWORDS, Constants::SPECIAL_VARIABLE_PREFIXES, Constants::SPLAT_LITERAL, Constants::UNKNOWN_LOCATION_INFO, Constants::VIML_COMMANDS, Constants::VIML_END_KEYWORDS, Constants::VIML_KEYWORDS

Instance Attribute Summary

Attributes inherited from Riml::AST_Rewriter

#ast, #classes, #options

Instance Method Summary collapse

Methods inherited from Riml::AST_Rewriter

#add_SID_function!, #add_SID_function?, #do_establish_parents, #do_rewrite_on_match, #establish_parents, #initialize, #max_recursion_lvl, #reorder_includes_based_on_class_dependencies!, #resolve_class_dependencies!, #resolve_class_dependencies?, #rewrite, #rewrite_included_and_sourced_files!, #rewrite_on_match, #watch_for_class_pickup

Constructor Details

This class inherits a constructor from Riml::AST_Rewriter

Instance Method Details

#match?(node) ⇒ Boolean

Returns:

  • (Boolean)


899
900
901
# File 'lib/riml/ast_rewriter.rb', line 899

def match?(node)
  DefaultParamNode === node
end

#replace(node) ⇒ Object



903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
# File 'lib/riml/ast_rewriter.rb', line 903

def replace(node)
  def_node = node.parent
  param_idx = def_node.parameters.index(node)
  first_default_param = def_node.parameters.detect(&DefNode::DEFAULT_PARAMS)
  first_default_param_idx = def_node.parameters.index(first_default_param)

  last_default_param = def_node.parameters.reverse.detect(&DefNode::DEFAULT_PARAMS)
  insert_idx = param_idx - first_default_param_idx

  while param = def_node.parameters[param_idx += 1]
    unless param == def_node.splat || DefaultParamNode === param
      error_msg = "can't have regular parameter after default parameter in function #{def_node.name.inspect}"
      error = UserArgumentError.new(error_msg, def_node)
      raise error
    end
  end

  if_expression = construct_if_expression(node)
  if insert_idx.zero?
    def_node.expressions.nodes.unshift(construct_copy_splat_var_assignment)
    SplatVarToCopiedSplatVar.new(def_node, classes).rewrite_on_match
  end

  if last_default_param == node
    def_node.parameters.delete_if(&DefNode::DEFAULT_PARAMS)
    def_node.parameters << SPLAT_LITERAL unless def_node.splat
  end
  def_node.expressions.nodes.insert(insert_idx + 1, if_expression)
  reestablish_parents(def_node)
end