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

Inherits:
Object
  • Object
show all
Defined in:
lib/yattho/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.



16
17
18
19
# File 'lib/yattho/view_components/linters/argument_mappers/system_arguments.rb', line 16

def initialize(attribute)
  @attribute = attribute
  @erb_helper = Helpers::ErbBlock.new
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



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

def attribute
  @attribute
end

#erb_helperObject (readonly)

Returns the value of attribute erb_helper.



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

def erb_helper
  @erb_helper
end

Instance Method Details

#attr_nameObject



42
43
44
# File 'lib/yattho/view_components/linters/argument_mappers/system_arguments.rb', line 42

def attr_name
  attribute.name
end

#to_argsObject



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

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