Class: SyntaxTree::StringEmbExpr

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

Overview

StringEmbExpr represents interpolated content. It can be contained within a couple of different parent nodes, including regular expressions, strings, and dynamic symbols.

"string #{expression}"

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(statements:, location:) ⇒ StringEmbExpr

Returns a new instance of StringEmbExpr.



10077
10078
10079
10080
10081
# File 'lib/syntax_tree/node.rb', line 10077

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10075
10076
10077
# File 'lib/syntax_tree/node.rb', line 10075

def comments
  @comments
end

#statementsObject (readonly)

Statements

the expressions to be interpolated



10072
10073
10074
# File 'lib/syntax_tree/node.rb', line 10072

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



10134
10135
10136
# File 'lib/syntax_tree/node.rb', line 10134

def ===(other)
  other.is_a?(StringEmbExpr) && statements === other.statements
end

#accept(visitor) ⇒ Object



10083
10084
10085
# File 'lib/syntax_tree/node.rb', line 10083

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

#child_nodesObject Also known as: deconstruct



10087
10088
10089
# File 'lib/syntax_tree/node.rb', line 10087

def child_nodes
  [statements]
end

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



10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
# File 'lib/syntax_tree/node.rb', line 10091

def copy(statements: nil, location: nil)
  node =
    StringEmbExpr.new(
      statements: statements || self.statements,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



10104
10105
10106
# File 'lib/syntax_tree/node.rb', line 10104

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

#format(q) ⇒ Object



10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
# File 'lib/syntax_tree/node.rb', line 10108

def format(q)
  if location.start_line == location.end_line
    # If the contents of this embedded expression were originally on the
    # same line in the source, then we're going to leave them in place and
    # assume that's the way the developer wanted this expression
    # represented.
    q.remove_breaks(
      q.group do
        q.text('#{')
        q.format(statements)
        q.text("}")
      end
    )
  else
    q.group do
      q.text('#{')
      q.indent do
        q.breakable_empty
        q.format(statements)
      end
      q.breakable_empty
      q.text("}")
    end
  end
end