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.



4554
4555
4556
4557
4558
# File 'lib/syntax_tree.rb', line 4554

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



4552
4553
4554
# File 'lib/syntax_tree.rb', line 4552

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



4549
4550
4551
# File 'lib/syntax_tree.rb', line 4549

def location
  @location
end

#valueObject (readonly)

untyped

the value being sent to the keyword



4546
4547
4548
# File 'lib/syntax_tree.rb', line 4546

def value
  @value
end

Instance Method Details

#child_nodesObject



4560
4561
4562
# File 'lib/syntax_tree.rb', line 4560

def child_nodes
  [value]
end

#format(q) ⇒ Object



4564
4565
4566
4567
4568
4569
4570
4571
4572
# File 'lib/syntax_tree.rb', line 4564

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



4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
# File 'lib/syntax_tree.rb', line 4574

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



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

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