Quote non-strictly valid identifiers in variable lookups

This commit is contained in:
Dylan Thacker-Smith
2022-11-17 09:51:23 -05:00
parent 7d2a9da8bc
commit 106ca1bd69
2 changed files with 10 additions and 8 deletions
+5 -3
View File
@@ -20,12 +20,14 @@ module Liquid
markup.scan(VariableParser) do |lookup|
last_match = Regexp.last_match
first_match ||= last_match
new_markup ||= +""
if lookup&.start_with?('[') && lookup&.end_with?(']')
new_markup ||= +""
new_markup << "[" << Expression.lax_migrate(lookup[1..-2]) << "]"
elsif !lookup.match?(/\A#{Liquid::Lexer::IDENTIFIER}\z/)
# quote non-strictly valid identifiers
new_markup << "['" << lookup << "']"
else
new_markup << "." if new_markup
new_markup ||= +""
new_markup << "." unless new_markup.empty?
new_markup << lookup
end
end
+5 -5
View File
@@ -38,7 +38,7 @@ class MigrateUnitTest < Minitest::Test
"{{ -1 | ! abs }}" => "{{ -1 | abs }}", # ignored non-word characters preceding filter name
"{{ 'a' | append WAT: 'b' }}" => "{{ 'a' | append : 'b' }}", # FilterArgsRegex skipped characters
"{{ '!' | replace, '!': '?' }}" => "{{ '!' | replace: '!', '?' }}", # FilterArgsRegex unexpected separators
"{{!-a-!}}" => "{{ -a- }}", # preserve separators when removing ignored characters
"{{ -a.1b }}" => "{{ ['-a']['1b'] }}", # quote separators when removing ignored characters
})
end
end
@@ -57,7 +57,7 @@ class MigrateUnitTest < Minitest::Test
def test_migrate_variable_lookup
with_error_mode(:lax) do
assert_migration({
"{{@-a[b].c-@}}" => "{{ -a[b].c- }}", # VariableParser skipped characters
"{{@a[b].c@}}" => "{{ a[b].c }}", # VariableParser skipped characters
"{{ a!b$c }}" => "{{ a.b.c }}", # VariableParser unexpected separators
})
end
@@ -66,7 +66,7 @@ class MigrateUnitTest < Minitest::Test
def test_migrate_assign
with_error_mode(:lax) do
assert_migration({
"{% assign!a = b-!%}" => "{% assign a = b- %}", # Syntax skipped characters
"{% assign!a = b!%}" => "{% assign a = b %}", # Syntax skipped characters
"{% assign a = @b ! %}" => "{% assign a = b %}", # Variable skipped characters
"{% assign|x=1 %}" => "{% assign x=1 %}", # ensure tag name separated from markup
})
@@ -76,12 +76,12 @@ class MigrateUnitTest < Minitest::Test
def test_lax_migrate_if
with_error_mode(:lax) do
assert_migration({
"{% if@a-@%}Y{% endif %}" => "{% if a- %}Y{% endif %}", # Syntax skipped character
"{% if@a@%}Y{% endif %}" => "{% if a %}Y{% endif %}", # Syntax skipped character
"{% if &a contains^b and *c %}A{% elsif %d or$e %}B{% endif %}" =>
"{% if a contains b and c %}A{% elsif d or e %}B{% endif %}", # test more expressions
"{% if b 1 %}Y{% endif %}" => "{% if b %}Y{% endif %}", # missing operator with right operand
"{% if c == %}Y{% endif %}" => "{% if c == nil %}Y{% endif %}", # operator with missing right operand
"{% if!a-!%}T{% endif %}" => "{% if a- %}T{% endif %}", # VariableParser skipped characters
"{% if!a!%}T{% endif %}" => "{% if a %}T{% endif %}", # VariableParser skipped characters
"{% if!%}T{% endif %}" => "{% if nil %}T{% endif %}", # VariableParser skipping all characters
})
end