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



10112
10113
10114
10115
10116
# File 'lib/syntax_tree/node.rb', line 10112

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10110
10111
10112
# File 'lib/syntax_tree/node.rb', line 10110

def comments
  @comments
end

#statementsObject (readonly)

Statements

the expressions to be interpolated



10107
10108
10109
# File 'lib/syntax_tree/node.rb', line 10107

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



10169
10170
10171
# File 'lib/syntax_tree/node.rb', line 10169

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

#accept(visitor) ⇒ Object



10118
10119
10120
# File 'lib/syntax_tree/node.rb', line 10118

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

#child_nodesObject Also known as: deconstruct



10122
10123
10124
# File 'lib/syntax_tree/node.rb', line 10122

def child_nodes
  [statements]
end

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



10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
# File 'lib/syntax_tree/node.rb', line 10126

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



10139
10140
10141
# File 'lib/syntax_tree/node.rb', line 10139

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

#format(q) ⇒ Object



10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
# File 'lib/syntax_tree/node.rb', line 10143

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