Class: SyntaxTree::ERB::CharData

Inherits:
Element show all
Defined in:
lib/syntax_tree/erb/nodes.rb

Overview

A CharData contains either plain text or whitespace within an element. It wraps a single token value.

Instance Attribute Summary collapse

Attributes inherited from Element

#location, #new_line

Instance Method Summary collapse

Methods inherited from Node

#format, #pretty_print

Constructor Details

#initialize(value:, new_line:, location:) ⇒ CharData

Returns a new instance of CharData.



633
634
635
636
# File 'lib/syntax_tree/erb/nodes.rb', line 633

def initialize(value:, new_line:, location:)
  super(new_line: new_line, location: location)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



631
632
633
# File 'lib/syntax_tree/erb/nodes.rb', line 631

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



638
639
640
# File 'lib/syntax_tree/erb/nodes.rb', line 638

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

#child_nodesObject Also known as: deconstruct



642
643
644
# File 'lib/syntax_tree/erb/nodes.rb', line 642

def child_nodes
  [value]
end

#deconstruct_keys(keys) ⇒ Object



648
649
650
# File 'lib/syntax_tree/erb/nodes.rb', line 648

def deconstruct_keys(keys)
  super.merge(value: value)
end

#skip?Boolean

Returns:

  • (Boolean)


652
653
654
# File 'lib/syntax_tree/erb/nodes.rb', line 652

def skip?
  value.value.strip.empty?
end

#without_new_lineObject

Also remove trailing whitespace



657
658
659
660
661
662
663
664
665
666
667
668
669
# File 'lib/syntax_tree/erb/nodes.rb', line 657

def without_new_line
  self.class.new(
    **deconstruct_keys([]).merge(
      new_line: nil,
      value:
        Token.new(
          type: value.type,
          location: value.location,
          value: value.value.rstrip
        )
    )
  )
end