Class: ERBLint::Linters::ArgumentMappers::SystemArguments

Inherits:
Object
  • Object
show all
Defined in:
lib/primer/view_components/linters/argument_mappers/system_arguments.rb

Overview

Maps element attributes to system arguments.

Constant Summary collapse

STRING_PARAMETERS =
%w[aria- data-].freeze
TEST_SELECTOR_REGEX =
/test_selector\((?<selector>.+)\)$/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ SystemArguments

Returns a new instance of SystemArguments.



14
15
16
# File 'lib/primer/view_components/linters/argument_mappers/system_arguments.rb', line 14

def initialize(attribute)
  @attribute = attribute
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



13
14
15
# File 'lib/primer/view_components/linters/argument_mappers/system_arguments.rb', line 13

def attribute
  @attribute
end

Instance Method Details

#attr_nameObject



41
42
43
# File 'lib/primer/view_components/linters/argument_mappers/system_arguments.rb', line 41

def attr_name
  attribute.name
end

#to_argsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/primer/view_components/linters/argument_mappers/system_arguments.rb', line 18

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: attribute.value.to_json }
  elsif attr_name.start_with?(*STRING_PARAMETERS)
    raise ConversionError, "Cannot convert attribute \"#{attr_name}\" because its value contains an erb block" if attribute.value_node&.children&.any? { |n| n.try(:type) == :erb }

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