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
openproject-primer_view_components-0.15.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
openproject-primer_view_components-0.14.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
openproject-primer_view_components-0.13.1 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.13.2 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.13.1 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
openproject-primer_view_components-0.13.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.13.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.12.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.11.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
openproject-primer_view_components-0.12.1 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.10.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
openproject-primer_view_components-0.11.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
openproject-primer_view_components-0.10.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.9.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
openproject-primer_view_components-0.9.1 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
openproject-primer_view_components-0.9.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
openproject-primer_view_components-0.8.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.8.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
primer_view_components-0.7.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb
openproject-primer_view_components-0.7.0 lib/primer/view_components/linters/argument_mappers/system_arguments.rb