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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(statements:, location:) ⇒ StringEmbExpr

Returns a new instance of StringEmbExpr.



10252
10253
10254
10255
10256
# File 'lib/syntax_tree/node.rb', line 10252

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10250
10251
10252
# File 'lib/syntax_tree/node.rb', line 10250

def comments
  @comments
end

#statementsObject (readonly)

Statements

the expressions to be interpolated



10247
10248
10249
# File 'lib/syntax_tree/node.rb', line 10247

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



10309
10310
10311
# File 'lib/syntax_tree/node.rb', line 10309

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

#accept(visitor) ⇒ Object



10258
10259
10260
# File 'lib/syntax_tree/node.rb', line 10258

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

#child_nodesObject Also known as: deconstruct



10262
10263
10264
# File 'lib/syntax_tree/node.rb', line 10262

def child_nodes
  [statements]
end

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



10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
# File 'lib/syntax_tree/node.rb', line 10266

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



10279
10280
10281
# File 'lib/syntax_tree/node.rb', line 10279

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

#format(q) ⇒ Object



10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
# File 'lib/syntax_tree/node.rb', line 10283

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