Sha256: 6e9eec86f0b68cc91821b6a35fff682068999220a47cf68c44e6e9baf6c3df32

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require_relative "conversion_error"
require_relative "helpers/erb_block"

module ERBLint
  module Linters
    module ArgumentMappers
      # Maps element attributes to system arguments.
      class SystemArguments
        STRING_PARAMETERS = %w[aria- data-].freeze
        TEST_SELECTOR_REGEX = /test_selector\((?<selector>.+)\)$/.freeze

        attr_reader :attribute
        def initialize(attribute)
          @attribute = attribute
        end

        def to_args
          if attribute.erb?
            _, _, code_node = *attribute.node

            raise ConversionError, "Cannot convert erb block" if code_node.nil?

            code = code_node.loc.source.strip
            m = code.match(TEST_SELECTOR_REGEX)

            raise ConversionError, "Cannot convert erb block" if m.blank?

            { test_selector: m[:selector].tr("'", '"') }
          elsif attr_name == "data-test-selector"
            Helpers::ErbBlock.raise_if_erb_block(attribute)

            { test_selector: attribute.value.to_json }
          elsif attr_name.start_with?(*STRING_PARAMETERS)
            Helpers::ErbBlock.raise_if_erb_block(attribute)

            { "\"#{attr_name}\"" => attribute.value.to_json }
          else
            raise ConversionError, "Cannot convert attribute \"#{attr_name}\""
          end
        end

        def attr_name
          attribute.name
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
primer_view_components-0.0.51 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.50 lib/primer/view_components/linters/argument_mappers/system_arguments.rb