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.



10267
10268
10269
10270
10271
# File 'lib/syntax_tree/node.rb', line 10267

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



10265
10266
10267
# File 'lib/syntax_tree/node.rb', line 10265

def comments
  @comments
end

#statementsObject (readonly)

Statements

the expressions to be interpolated



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

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



10324
10325
10326
# File 'lib/syntax_tree/node.rb', line 10324

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

#accept(visitor) ⇒ Object



10273
10274
10275
# File 'lib/syntax_tree/node.rb', line 10273

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

#child_nodesObject Also known as: deconstruct



10277
10278
10279
# File 'lib/syntax_tree/node.rb', line 10277

def child_nodes
  [statements]
end

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



10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
# File 'lib/syntax_tree/node.rb', line 10281

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



10294
10295
10296
# File 'lib/syntax_tree/node.rb', line 10294

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

#format(q) ⇒ Object



10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
# File 'lib/syntax_tree/node.rb', line 10298

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