class ERBLint::Linters::ArgumentMappers::Helpers::ErbBlock

provides helpers to identify and deal with ERB blocks.

Constants

INTERPOLATION_REGEX

Public Instance Methods

convert(attribute) click to toggle source
# File lib/primer/view_components/linters/argument_mappers/helpers/erb_block.rb, line 17
def convert(attribute)
  raise_error(attribute) unless interpolation?(attribute)

  if any?(attribute)
    convert_interpolation(attribute)
  else
    attribute.value.to_json
  end
end
raise_if_erb_block(attribute) click to toggle source
# File lib/primer/view_components/linters/argument_mappers/helpers/erb_block.rb, line 13
def raise_if_erb_block(attribute)
  raise_error(attribute) if any?(attribute)
end

Private Instance Methods

any?(attribute) click to toggle source
# File lib/primer/view_components/linters/argument_mappers/helpers/erb_block.rb, line 40
def any?(attribute)
  erb_blocks(attribute).any?
end
basic?(attribute) click to toggle source
# File lib/primer/view_components/linters/argument_mappers/helpers/erb_block.rb, line 44
def basic?(attribute)
  return false if erb_blocks(attribute).size != 1

  attribute.value.match?(INTERPOLATION_REGEX)
end
convert_interpolation(attribute) click to toggle source
# File lib/primer/view_components/linters/argument_mappers/helpers/erb_block.rb, line 54
def convert_interpolation(attribute)
  if basic?(attribute)
    m = attribute.value.match(INTERPOLATION_REGEX)
    return m[:rb].strip
  end

  # we use `source` instead of `value` because it does not convert encoded HTML entities.
  attribute.value_node.loc.source.gsub("<%=", '#{').gsub("%>", "}")
end
erb_blocks(attribute) click to toggle source
# File lib/primer/view_components/linters/argument_mappers/helpers/erb_block.rb, line 50
def erb_blocks(attribute)
  (attribute.value_node&.children || []).select { |n| n.try(:type) == :erb }
end
interpolation?(attribute) click to toggle source
# File lib/primer/view_components/linters/argument_mappers/helpers/erb_block.rb, line 29
def interpolation?(attribute)
  erb_blocks(attribute).all? do |erb|
    # If the blocks does not have an indicator, it's not an interpolation.
    erb.children.to_a.compact.any? { |node| node.type == :indicator }
  end
end
raise_error(attribute) click to toggle source
# File lib/primer/view_components/linters/argument_mappers/helpers/erb_block.rb, line 36
def raise_error(attribute)
  raise ERBLint::Linters::ArgumentMappers::ConversionError, "Cannot convert attribute \"#{attribute.name}\" because its value contains an erb block"
end