Class: ERBLint::Linters::ArgumentMappers::SystemArguments
- Inherits:
-
Object
- Object
- ERBLint::Linters::ArgumentMappers::SystemArguments
- 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
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
Instance Method Summary collapse
- #attr_name ⇒ Object
-
#initialize(attribute) ⇒ SystemArguments
constructor
A new instance of SystemArguments.
- #to_args ⇒ Object
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
#attribute ⇒ Object (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_name ⇒ Object
41 42 43 |
# File 'lib/primer/view_components/linters/argument_mappers/system_arguments.rb', line 41 def attr_name attribute.name end |
#to_args ⇒ Object
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 |