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

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(value:, location:) ⇒ EndContent



383
384
385
386
387
# File 'lib/syntax_tree/node.rb', line 383

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



381
382
383
# File 'lib/syntax_tree/node.rb', line 381

def comments
  @comments
end

#valueObject (readonly)

String

the content after the script



378
379
380
# File 'lib/syntax_tree/node.rb', line 378

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



432
433
434
# File 'lib/syntax_tree/node.rb', line 432

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

#accept(visitor) ⇒ Object



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

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

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  []
end

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



397
398
399
400
401
402
403
404
405
406
# File 'lib/syntax_tree/node.rb', line 397

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



410
411
412
# File 'lib/syntax_tree/node.rb', line 410

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

#format(q) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/syntax_tree/node.rb', line 414

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