Class: SyntaxTree::QWords

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

Overview

QWords represents a string literal array without interpolation.

%w[one two three]

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(beginning:, elements:, location:, comments: []) ⇒ QWords

Returns a new instance of QWords.



6951
6952
6953
6954
6955
6956
# File 'lib/syntax_tree/node.rb', line 6951

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

Instance Attribute Details

#beginning ⇒ Object (readonly)

[QWordsBeg] the token that opens this array literal



6943
6944
6945
# File 'lib/syntax_tree/node.rb', line 6943

def beginning
  @beginning
end

#comments ⇒ Object (readonly)

[Array[ Comment | EmbDoc ]] the comments attached to this node



6949
6950
6951
# File 'lib/syntax_tree/node.rb', line 6949

def comments
  @comments
end

#elements ⇒ Object (readonly)

[Array[ TStringContent ]] the elements of the array



6946
6947
6948
# File 'lib/syntax_tree/node.rb', line 6946

def elements
  @elements
end

Instance Method Details

#accept(visitor) ⇒ Object



6958
6959
6960
# File 'lib/syntax_tree/node.rb', line 6958

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

#child_nodes ⇒ Object Also known as: deconstruct



6962
6963
6964
# File 'lib/syntax_tree/node.rb', line 6962

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



6968
6969
6970
6971
6972
6973
6974
6975
# File 'lib/syntax_tree/node.rb', line 6968

def deconstruct_keys(keys)
  {
    beginning: beginning,
    elements: elements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
# File 'lib/syntax_tree/node.rb', line 6977

def format(q)
  opening, closing = "%w[", "]"

  if elements.any? { |element| element.match?(/[\[\]]/) }
    opening = beginning.value
    closing = Quotes.matching(opening[2])
  end

  q.group(0, opening, closing) do
    q.indent do
      q.breakable("")
      q.seplist(elements, -> { q.breakable }) do |element|
        q.format(element)
      end
    end
    q.breakable("")
  end
end