Class: YARP::UnlessNode

Inherits:
YARPNode
  • Object
show all
Defined in:
lib/yarp/node.rb,
ext/yarp/api_node.c

Overview

Represents the use of the ‘unless` keyword, either in the block form or the modifier form.

bar unless foo
^^^^^^^^^^^^^^

unless foo then bar end
^^^^^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword_loc, predicate, statements, consequent, end_keyword_loc, location) ⇒ UnlessNode

def initialize: (keyword_loc: Location, predicate: Node, statements: StatementsNode?, consequent: ElseNode?, end_keyword_loc: Location?, location: Location) -> void



10018
10019
10020
10021
10022
10023
10024
10025
# File 'lib/yarp/node.rb', line 10018

def initialize(keyword_loc, predicate, statements, consequent, end_keyword_loc, location)
  @keyword_loc = keyword_loc
  @predicate = predicate
  @statements = statements
  @consequent = consequent
  @end_keyword_loc = end_keyword_loc
  @location = location
end

Instance Attribute Details

#consequentObject (readonly)

attr_reader consequent: ElseNode?



10012
10013
10014
# File 'lib/yarp/node.rb', line 10012

def consequent
  @consequent
end

#end_keyword_locObject (readonly)

attr_reader end_keyword_loc: Location?



10015
10016
10017
# File 'lib/yarp/node.rb', line 10015

def end_keyword_loc
  @end_keyword_loc
end

#keyword_locObject (readonly)

attr_reader keyword_loc: Location



10003
10004
10005
# File 'lib/yarp/node.rb', line 10003

def keyword_loc
  @keyword_loc
end

#predicateObject (readonly)

attr_reader predicate: Node



10006
10007
10008
# File 'lib/yarp/node.rb', line 10006

def predicate
  @predicate
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



10009
10010
10011
# File 'lib/yarp/node.rb', line 10009

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



10028
10029
10030
# File 'lib/yarp/node.rb', line 10028

def accept(visitor)
  visitor.visit_unless_node(self)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



10037
10038
10039
# File 'lib/yarp/node.rb', line 10037

def child_nodes
  [predicate, statements, consequent]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



10042
10043
10044
# File 'lib/yarp/node.rb', line 10042

def comment_targets
  [keyword_loc, predicate, *statements, *consequent, *end_keyword_loc]
end

#copy(**params) ⇒ Object

def copy: (**params) -> UnlessNode



10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
# File 'lib/yarp/node.rb', line 10047

def copy(**params)
  UnlessNode.new(
    params.fetch(:keyword_loc) { keyword_loc },
    params.fetch(:predicate) { predicate },
    params.fetch(:statements) { statements },
    params.fetch(:consequent) { consequent },
    params.fetch(:end_keyword_loc) { end_keyword_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



10062
10063
10064
# File 'lib/yarp/node.rb', line 10062

def deconstruct_keys(keys)
  { keyword_loc: keyword_loc, predicate: predicate, statements: statements, consequent: consequent, end_keyword_loc: end_keyword_loc, location: location }
end

#end_keywordObject

def end_keyword: () -> String?



10072
10073
10074
# File 'lib/yarp/node.rb', line 10072

def end_keyword
  end_keyword_loc&.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object



10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
# File 'lib/yarp/node.rb', line 10076

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── keyword_loc: #{inspector.location(keyword_loc)}\n"
  inspector << "├── predicate:\n"
  inspector << inspector.child_node(predicate, "")
  if (statements = self.statements).nil?
    inspector << "├── statements: ∅\n"
  else
    inspector << "├── statements:\n"
    inspector << statements.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  if (consequent = self.consequent).nil?
    inspector << "├── consequent: ∅\n"
  else
    inspector << "├── consequent:\n"
    inspector << consequent.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "└── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n"
  inspector.to_str
end

#keywordObject

def keyword: () -> String



10067
10068
10069
# File 'lib/yarp/node.rb', line 10067

def keyword
  keyword_loc.slice
end

#set_newline_flag(newline_marked) ⇒ Object



10032
10033
10034
# File 'lib/yarp/node.rb', line 10032

def set_newline_flag(newline_marked)
  predicate.set_newline_flag(newline_marked)
end