Class: SyntaxTree::EndContent

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

Overview

EndContent represents the use of __END__ syntax, which allows individual scripts to keep content after the main ruby code that can be read through the DATA constant.

puts DATA.read

__END__

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(value:, location:) ⇒ EndContent

Returns a new instance of EndContent.



393
394
395
396
397
# File 'lib/syntax_tree/node.rb', line 393

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



391
392
393
# File 'lib/syntax_tree/node.rb', line 391

def comments
  @comments
end

#valueObject (readonly)

String

the content after the script



388
389
390
# File 'lib/syntax_tree/node.rb', line 388

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



442
443
444
# File 'lib/syntax_tree/node.rb', line 442

def ===(other)
  other.is_a?(EndContent) && value === other.value
end

#accept(visitor) ⇒ Object



399
400
401
# File 'lib/syntax_tree/node.rb', line 399

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

#child_nodesObject Also known as: deconstruct



403
404
405
# File 'lib/syntax_tree/node.rb', line 403

def child_nodes
  []
end

#copy(value: nil, location: nil) ⇒ Object



407
408
409
410
411
412
413
414
415
416
# File 'lib/syntax_tree/node.rb', line 407

def copy(value: nil, location: nil)
  node =
    EndContent.new(
      value: value || self.value,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



420
421
422
# File 'lib/syntax_tree/node.rb', line 420

def deconstruct_keys(_keys)
  { value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/syntax_tree/node.rb', line 424

def format(q)
  q.text("__END__")
  q.breakable_force

  first = true
  value.each_line(chomp: true) do |line|
    if first
      first = false
    else
      q.breakable_return
    end

    q.text(line)
  end

  q.breakable_return if value.end_with?("\n")
end