Write symbols instead of hash rockets in HAML

This commit is contained in:
Maikel Linke
2024-02-22 14:47:58 +11:00
parent e2ec3a642c
commit 1ad58b214b
2 changed files with 14 additions and 6 deletions

View File

@@ -102,7 +102,15 @@ class HamlUp
entries = hash.map do |key, value|
value = stringify(value) if value.is_a? Hash
"#{key.inspect} => #{value}"
# We prefer the Ruby 1.9 hash syntax with symbols followed by a colon
# like this:
#
# %button{ disabled: true, "ng-class": "primary-button" }
#
# Symbols start with `:` which we slice off. It gets appended below.
key = key.to_sym.inspect.slice(1..-1)
"#{key}: #{value}"
end
"{ #{entries.join(', ')} }"

View File

@@ -14,7 +14,7 @@ describe HamlUp, skip: !Gem::Dependency.new("", "~> 5.2").match?("", Haml::VERSI
it "rewrites non-standard attribute hashes" do
original = "%p{ng: {click: 'action', show: 'condition'}} label"
template = call(original)
expect(template).to eq "%p{ \"ng-click\" => 'action', \"ng-show\" => 'condition' } label"
expect(template).to eq "%p{ \"ng-click\": 'action', \"ng-show\": 'condition' } label"
end
it "preserves standard attribute hashes" do
@@ -26,18 +26,18 @@ describe HamlUp, skip: !Gem::Dependency.new("", "~> 5.2").match?("", Haml::VERSI
it "preserves standard attribute hashes while rewriting others" do
original = "%p{data: {click: 'standard'}, ng: {click: 'not'}} label"
template = call(original)
expect(template).to eq "%p{ \"data\" => {click: 'standard'}, \"ng-click\" => 'not' } label"
expect(template).to eq "%p{ data: {click: 'standard'}, \"ng-click\": 'not' } label"
end
it "rewrites multi-line attributes" do
original = <<~HAML
%li{ ng: { class: "{active: selector.active}" } }
%a{ "tooltip" => "{{selector.object.value}}", "tooltip-placement" => "bottom",
%a{ tooltip: "{{selector.object.value}}", "tooltip-placement": "bottom",
ng: { transclude: true, class: "{active: selector.active, 'has-tip': selector.object.value}" } }
HAML
expected = <<~HAML
%li{ "ng-class" => "{active: selector.active}" }
%a{ "tooltip" => "{{selector.object.value}}", "tooltip-placement" => "bottom", "ng-transclude" => true, "ng-class" => "{active: selector.active, 'has-tip': selector.object.value}" }
%li{ "ng-class": "{active: selector.active}" }
%a{ tooltip: "{{selector.object.value}}", "tooltip-placement": "bottom", "ng-transclude": true, "ng-class": "{active: selector.active, 'has-tip': selector.object.value}" }
HAML
template = call(original)
expect(template).to eq expected