class SyntaxTree::DynaSymbol
{ “#{key}”: value }
They can also be used as a special kind of dynamic hash key, as in:
:“#{variable}”
define its value.
DynaSymbol represents a symbol literal that uses quotes to dynamically
def ===(other)
def ===(other) other.is_a?(DynaSymbol) && ArrayMatch.call(parts, other.parts) && quote === other.quote end
def accept(visitor)
def accept(visitor) visitor.visit_dyna_symbol(self) end
def child_nodes
def child_nodes parts end
def copy(parts: nil, quote: nil, location: nil)
def copy(parts: nil, quote: nil, location: nil) node = DynaSymbol.new( parts: parts || self.parts, quote: quote || self.quote, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end
def deconstruct_keys(_keys)
def deconstruct_keys(_keys) { parts: parts, quote: quote, location: location, comments: comments } end
def format(q)
def format(q) opening_quote, closing_quote = quotes(q) q.text(opening_quote) q.group do parts.each do |part| if part.is_a?(TStringContent) value = Quotes.normalize(part.value, closing_quote) first = true value.each_line(chomp: true) do |line| if first first = false else q.breakable_return end q.text(line) end q.breakable_return if value.end_with?("\n") else q.format(part) end end end q.text(closing_quote) end
def initialize(parts:, quote:, location:)
def initialize(parts:, quote:, location:) @parts = parts @quote = quote @location = location @comments = [] end
def quotes(q)
lot of rules because it could be in many different contexts with many
Here we determine the quotes to use for a dynamic symbol. It's bound by a
def quotes(q) # If we're inside of an assoc node as the key, then it will handle # printing the : on its own since it could change sides. parent = q.parent hash_key = parent.is_a?(Assoc) && parent.key == self if quote.start_with?("%s") # Here we're going to check if there is a closing character, a new line, # or a quote in the content of the dyna symbol. If there is, then # quoting could get weird, so just bail out and stick to the original # quotes in the source. matching = Quotes.matching(quote[2]) pattern = /[\n#{Regexp.escape(matching)}'"]/ # This check is to ensure we don't find a matching quote inside of the # symbol that would be confusing. matched = parts.any? do |part| part.is_a?(TStringContent) && part.value.match?(pattern) end if matched [quote, matching] elsif Quotes.locked?(self, q.quote) ["#{":" unless hash_key}'", "'"] else ["#{":" unless hash_key}#{q.quote}", q.quote] end elsif Quotes.locked?(self, q.quote) if quote.start_with?(":") [hash_key ? quote[1..] : quote, quote[1..]] else [hash_key ? quote : ":#{quote}", quote] end else [hash_key ? q.quote : ":#{q.quote}", q.quote] end end