Sha256: fc02d186190641c3c24d04885d93c48fe1af440ab1946a984ee04aef4f962892

Contents?: true

Size: 1.36 KB

Versions: 212

Compression:

Stored size: 1.36 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, :erb_helper

        def initialize(attribute)
          @attribute = attribute
          @erb_helper = Helpers::ErbBlock.new
        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"
            { test_selector: erb_helper.convert(attribute) }
          elsif attr_name.start_with?(*STRING_PARAMETERS)
            { "\"#{attr_name}\"" => erb_helper.convert(attribute) }
          else
            raise ConversionError, "Cannot convert attribute \"#{attr_name}\""
          end
        end

        def attr_name
          attribute.name
        end
      end
    end
  end
end

Version data entries

212 entries across 212 versions & 3 rubygems

Version Path
primer_view_components-0.0.83 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.82 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.81 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.80 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.79 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.78 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.77 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.76 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.75 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.74 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.73 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.72 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.71 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.70 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.69 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.68 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.67 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.66 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.65 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.0.64 lib/primer/view_components/linters/argument_mappers/system_arguments.rb