Class: SyntaxTree::Defined

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

Defined represents the use of the defined? operator. It can be used with and without parentheses.

defined?(variable)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, location:, comments: []) ⇒ Defined

Returns a new instance of Defined.



4591
4592
4593
4594
4595
# File 'lib/syntax_tree.rb', line 4591

def initialize(value:, location:, comments: [])
  @value = value
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4589
4590
4591
# File 'lib/syntax_tree.rb', line 4589

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4586
4587
4588
# File 'lib/syntax_tree.rb', line 4586

def location
  @location
end

#valueObject (readonly)

untyped

the value being sent to the keyword



4583
4584
4585
# File 'lib/syntax_tree.rb', line 4583

def value
  @value
end

Instance Method Details

#child_nodesObject



4597
4598
4599
# File 'lib/syntax_tree.rb', line 4597

def child_nodes
  [value]
end

#format(q) ⇒ Object



4601
4602
4603
4604
4605
4606
4607
4608
4609
# File 'lib/syntax_tree.rb', line 4601

def format(q)
  q.group(0, "defined?(", ")") do
    q.indent do
      q.breakable("")
      q.format(value)
    end
    q.breakable("")
  end
end

#pretty_print(q) ⇒ Object



4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
# File 'lib/syntax_tree.rb', line 4611

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("defined")

    q.breakable
    q.pp(value)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



4622
4623
4624
4625
4626
# File 'lib/syntax_tree.rb', line 4622

def to_json(*opts)
  { type: :defined, value: value, loc: location, cmts: comments }.to_json(
    *opts
  )
end