Compare commits

..
Author SHA1 Message Date
Ian Ker-Seymer 19e287dda2 Gracefully empty property in map filter 2025-04-04 09:59:04 -04:00
Ian Ker-Seymer 7900043ffc Do not raise property error 2025-04-02 22:18:09 -04:00
Marco Concetto Rudilosso a7ad5f4282 also do it for map 2025-04-02 12:16:38 +02:00
Marco Concetto Rudilosso 2998ae38ba fix ordering of checks 2025-04-02 12:09:54 +02:00
Marco Concetto Rudilosso 09f9a87ea3 up version 2025-04-02 11:02:08 +02:00
Marco Concetto Rudilosso 035a0db6ab Make all array filters that use filter_array util process empty string and nil correctly 2025-04-02 11:01:51 +02:00
aa1640035f Always stringify properties in all array filters (#1936)
* Always stringify sum property.

* Add test

* always stringify properties in all array filters

* fix syntax error

* up version

---------

Co-authored-by: Dominic Petrick <[email protected]>
2025-03-19 13:50:01 -04:00
Ian Ker-SeymerandGitHub f5d6a36574 Stringify properties before filtering (#1929) 2025-03-17 17:43:57 -04:00
Ian Ker-SeymerandGitHub c5711c095f Improve docs of date filter (#1920) 2025-03-13 17:36:29 -04:00
James MengandGitHub 284f5fb647 Merge pull request #1928 from Shopify/jm/add_ld_link_doc_tag
Add a link to the LiquidDoc tooling reference in `doc` tag documentation
2025-03-13 09:48:40 -07:00
James MengandGitHub 21432928d0 Use full relative path for hyperlink 2025-03-13 09:47:57 -07:00
James Meng 1783c0c084 Add a link to the LiquidDoc reference in doc tag documentation comment 2025-03-12 15:31:28 -07:00
Guilherme CarreiroandGuilherme Carreiro e38f730c00 Update LiquidDoc documentation 2025-03-07 18:59:59 +01:00
2d0442798b chore: Add RUBYOPT configuration to the tests (#1859)
Co-authored-by: Ian Ker-Seymer <[email protected]>
2025-02-26 14:28:17 -05:00
Guilherme CarreiroandGuilherme Carreiro 6453a0ea48 Implement nodelist in the Doc tag so it may be visited 2025-02-26 13:14:39 +01:00
Guilherme CarreiroandGuilherme Carreiro a398b4cc74 Fix History.md 2025-02-25 08:50:46 +01:00
Guilherme CarreiroandGuilherme Carreiro cca9fe99cf Bump version to 5.8.0 2025-02-25 08:50:46 +01:00
Guilherme CarreiroandGuilherme Carreiro 17d327988d Rename {% doc %} constant strictly validates the abscense of args 2025-02-20 12:37:09 +01:00
Guilherme CarreiroandGuilherme Carreiro f643af4bac Update the implementation to make {% doc %} as strict as {% raw %} 2025-02-20 12:37:09 +01:00
Guilherme CarreiroandGuilherme Carreiro ae8a0a86ac Remove misleading unit test (thank you, @EvilGenius13) 2025-02-20 12:37:09 +01:00
Guilherme CarreiroandGuilherme Carreiro b439d0da53 Update {% doc %} to no longer support nested tags (as {% comment %} does) 2025-02-20 12:37:09 +01:00
Guilherme CarreiroandGuilherme Carreiro 16592cfb8f Add support to LiquidDoc with the new {% doc %} tag 2025-02-20 12:37:09 +01:00
Chris AtLeeandGitHub da4afd4156 Merge pull request #1905 from Shopify/catlee/invalid_utf8
Raise SyntaxError on invalid UTF8 strings in lexer/tokenizer
2025-02-13 09:24:11 -05:00
Chris AtLeeandGitHub 1bb3091208 Merge pull request #1909 from Shopify/catlee/5.7.3
Bump version to 5.7.3
2025-02-13 09:22:47 -05:00
Max StoiberandGitHub 040801b32c Fix array has filters referring to some (#1910) 2025-02-12 17:37:49 +01:00
Chris AtLee 550135c0b9 Raise SyntaxError on invalid UTF8 strings in lexer/tokenizer 2025-02-11 14:23:15 -05:00
Chris AtLee aec966eed7 Bump version to 5.7.3 2025-02-11 14:21:14 -05:00
11 changed files with 204 additions and 20 deletions
+15
View File
@@ -14,8 +14,21 @@ jobs:
- { ruby: 3.0, allowed-failure: false } # minimum supported - { ruby: 3.0, allowed-failure: false } # minimum supported
- { ruby: 3.2, allowed-failure: false } - { ruby: 3.2, allowed-failure: false }
- { ruby: 3.3, allowed-failure: false } - { ruby: 3.3, allowed-failure: false }
- { ruby: 3.3, allowed-failure: false }
- { ruby: 3.4, allowed-failure: false } # latest - { ruby: 3.4, allowed-failure: false } # latest
- {
ruby: 3.4,
allowed-failure: false,
rubyopt: "--enable-frozen-string-literal",
}
- { ruby: 3.4, allowed-failure: false, rubyopt: "--yjit" }
- { ruby: ruby-head, allowed-failure: false } - { ruby: ruby-head, allowed-failure: false }
- {
ruby: ruby-head,
allowed-failure: false,
rubyopt: "--enable-frozen-string-literal",
}
- { ruby: ruby-head, allowed-failure: false, rubyopt: "--yjit" }
name: Test Ruby ${{ matrix.entry.ruby }} name: Test Ruby ${{ matrix.entry.ruby }}
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
@@ -26,6 +39,8 @@ jobs:
bundler: latest bundler: latest
- run: bundle exec rake - run: bundle exec rake
continue-on-error: ${{ matrix.entry.allowed-failure }} continue-on-error: ${{ matrix.entry.allowed-failure }}
env:
RUBYOPT: ${{ matrix.entry.rubyopt }}
memory_profile: memory_profile:
runs-on: ubuntu-latest runs-on: ubuntu-latest
+18 -6
View File
@@ -1,22 +1,34 @@
# Liquid Change Log # Liquid Change Log
## 5.8.0 (unreleased) ## 5.8.1 (unreleased)
## 5.8.1
* Fix `{% doc %}` tag to be visitable [Guilherme Carreiro]
## 5.8.0
* Introduce the new `{% doc %}` tag [Guilherme Carreiro]
## 5.7.3
* Raise Liquid::SyntaxError when parsing invalidly encoded strings [Chris AtLee]
## 5.7.2 2025-01-31 ## 5.7.2 2025-01-31
* Fix array filters to not support nested properties * Fix array filters to not support nested properties [Guilherme Carreiro]
## 5.7.1 2025-01-24 ## 5.7.1 2025-01-24
* Fix the `find` and `find_index`filters to return `nil` when filtering empty arrays * Fix the `find` and `find_index`filters to return `nil` when filtering empty arrays [Guilherme Carreiro]
* Fix the `has` filter to return `false` when filtering empty arrays * Fix the `has` filter to return `false` when filtering empty arrays [Guilherme Carreiro]
## 5.7.0 2025-01-16 ## 5.7.0 2025-01-16
### Features ### Features
* Add `find`, `find_index`, `has`, and `reject` filters to arrays * Add `find`, `find_index`, `has`, and `reject` filters to arrays [Guilherme Carreiro]
* Compatibility with Ruby 3.4 * Compatibility with Ruby 3.4 [Ian Ker-Seymer]
## 5.6.4 2025-01-14 ## 5.6.4 2025-01-14
+6
View File
@@ -161,6 +161,12 @@ module Liquid
end end
# rubocop:enable Metrics/BlockNesting # rubocop:enable Metrics/BlockNesting
output << EOS output << EOS
rescue ::ArgumentError => e
if e.message == "invalid byte sequence in #{ss.string.encoding}"
raise SyntaxError, "Invalid byte sequence in #{ss.string.encoding}"
else
raise
end
end end
def raise_syntax_error(start_pos, ss) def raise_syntax_error(start_pos, ss)
+26 -6
View File
@@ -3,7 +3,6 @@
require 'cgi' require 'cgi'
require 'base64' require 'base64'
require 'bigdecimal' require 'bigdecimal'
module Liquid module Liquid
module StandardFilters module StandardFilters
MAX_I32 = (1 << 31) - 1 MAX_I32 = (1 << 31) - 1
@@ -387,6 +386,7 @@ module Liquid
end end
elsif ary.all? { |el| el.respond_to?(:[]) } elsif ary.all? { |el| el.respond_to?(:[]) }
begin begin
property = Utils.to_s(property)
ary.sort { |a, b| nil_safe_compare(a[property], b[property]) } ary.sort { |a, b| nil_safe_compare(a[property], b[property]) }
rescue TypeError rescue TypeError
raise_property_error(property) raise_property_error(property)
@@ -416,6 +416,7 @@ module Liquid
end end
elsif ary.all? { |el| el.respond_to?(:[]) } elsif ary.all? { |el| el.respond_to?(:[]) }
begin begin
property = Utils.to_s(property)
ary.sort { |a, b| nil_safe_casecmp(a[property], b[property]) } ary.sort { |a, b| nil_safe_casecmp(a[property], b[property]) }
rescue TypeError rescue TypeError
raise_property_error(property) raise_property_error(property)
@@ -456,7 +457,7 @@ module Liquid
# Tests if any item in an array has a specific property value. # Tests if any item in an array has a specific property value.
# @liquid_description # @liquid_description
# This requires you to provide both the property name and the associated value. # This requires you to provide both the property name and the associated value.
# @liquid_syntax array | some: string, string # @liquid_syntax array | has: string, string
# @liquid_return [boolean] # @liquid_return [boolean]
def has(input, property, target_value = nil) def has(input, property, target_value = nil)
filter_array(input, property, target_value, false) { |ary, &block| ary.any?(&block) } filter_array(input, property, target_value, false) { |ary, &block| ary.any?(&block) }
@@ -503,6 +504,7 @@ module Liquid
elsif ary.empty? # The next two cases assume a non-empty array. elsif ary.empty? # The next two cases assume a non-empty array.
[] []
else else
property = Utils.to_s(property)
ary.uniq do |item| ary.uniq do |item|
item[property] item[property]
rescue TypeError rescue TypeError
@@ -534,6 +536,11 @@ module Liquid
# @liquid_syntax array | map: string # @liquid_syntax array | map: string
# @liquid_return [array[untyped]] # @liquid_return [array[untyped]]
def map(input, property) def map(input, property)
property = Utils.to_s(property)
# Return the input array if property is empty (no-op)
return InputIterator.new(input, context).to_a if property.empty?
InputIterator.new(input, context).map do |e| InputIterator.new(input, context).map do |e|
e = e.call if e.is_a?(Proc) e = e.call if e.is_a?(Proc)
@@ -563,6 +570,7 @@ module Liquid
elsif ary.empty? # The next two cases assume a non-empty array. elsif ary.empty? # The next two cases assume a non-empty array.
[] []
else else
property = Liquid::Utils.to_s(property)
ary.reject do |item| ary.reject do |item|
item[property].nil? item[property].nil?
rescue TypeError rescue TypeError
@@ -712,7 +720,16 @@ module Liquid
input.gsub(/\r?\n/, "<br />\n") input.gsub(/\r?\n/, "<br />\n")
end end
# Reformat a date using Ruby's core Time#strftime( string ) -> string # @liquid_public_docs
# @liquid_type filter
# @liquid_category date
# @liquid_summary
# Formats a date according to a specified format string.
# @liquid_description
# This filter formats a date using various format specifiers. If the format string is empty,
# the original input is returned. If the input cannot be converted to a date, the original input is returned.
#
# The following format specifiers can be used:
# #
# %a - The abbreviated weekday name (``Sun'') # %a - The abbreviated weekday name (``Sun'')
# %A - The full weekday name (``Sunday'') # %A - The full weekday name (``Sunday'')
@@ -741,8 +758,8 @@ module Liquid
# %Y - Year with century # %Y - Year with century
# %Z - Time zone name # %Z - Time zone name
# %% - Literal ``%'' character # %% - Literal ``%'' character
# # @liquid_syntax date | date: string
# See also: http://www.ruby-doc.org/core/Time.html#method-i-strftime # @liquid_return [string]
def date(input, format) def date(input, format)
str_format = Utils.to_s(format) str_format = Utils.to_s(format)
return input if str_format.empty? return input if str_format.empty?
@@ -943,6 +960,8 @@ module Liquid
# @liquid_syntax array | sum # @liquid_syntax array | sum
# @liquid_return [number] # @liquid_return [number]
def sum(input, property = nil) def sum(input, property = nil)
property = property.nil? ? nil : Utils.to_s(property)
ary = InputIterator.new(input, context) ary = InputIterator.new(input, context)
return 0 if ary.empty? return 0 if ary.empty?
@@ -971,9 +990,10 @@ module Liquid
def filter_array(input, property, target_value, default_value = [], &block) def filter_array(input, property, target_value, default_value = [], &block)
ary = InputIterator.new(input, context) ary = InputIterator.new(input, context)
return default_value if ary.empty? return default_value if ary.empty?
property = Utils.to_s(property)
block.call(ary) do |item| block.call(ary) do |item|
if target_value.nil? if target_value.nil?
item[property] item[property]
+10 -2
View File
@@ -13,12 +13,16 @@ module Liquid
# Liquid code inside will be parsed but not executed. This facilitates # Liquid code inside will be parsed but not executed. This facilitates
# tooling support for features like code completion, linting, and inline # tooling support for features like code completion, linting, and inline
# documentation. # documentation.
#
# For detailed documentation syntax and examples, see the
# [`LiquidDoc` reference](/docs/storefronts/themes/tools/liquid-doc).
#
# @liquid_syntax # @liquid_syntax
# {% doc %} # {% doc %}
# Renders a message. # Renders a message.
# #
# @param {string} foo - A foo value. # @param {string} foo - A string value.
# @param {string} [bar] - An optional bar value. # @param {string} [bar] - An optional string value.
# #
# @example # @example
# {% render 'message', foo: 'Hello', bar: 'World' %} # {% render 'message', foo: 'Hello', bar: 'World' %}
@@ -55,6 +59,10 @@ module Liquid
true true
end end
def nodelist
[]
end
private private
def ensure_valid_markup(tag_name, markup, parse_context) def ensure_valid_markup(tag_name, markup, parse_context)
+6
View File
@@ -103,6 +103,12 @@ module Liquid
pos = @ss.pos -= 2 pos = @ss.pos -= 2
@source.byteslice(start, pos - start) @source.byteslice(start, pos - start)
rescue ::ArgumentError => e
if e.message == "invalid byte sequence in #{@ss.string.encoding}"
raise SyntaxError, "Invalid byte sequence in #{@ss.string.encoding}"
else
raise
end
end end
def next_variable_token def next_variable_token
+1 -1
View File
@@ -2,5 +2,5 @@
# frozen_string_literal: true # frozen_string_literal: true
module Liquid module Liquid
VERSION = "5.7.2" VERSION = "5.8.3"
end end
+77 -5
View File
@@ -560,15 +560,47 @@ class StandardFiltersTest < Minitest::Test
end end
end end
def test_map_returns_empty_with_no_property def test_map_with_nil_property
foo = [ array = [
{ "handle" => "alpha", "value" => "A" },
{ "handle" => "beta", "value" => "B" },
{ "handle" => "gamma", "value" => "C" }
]
assert_template_result("alpha beta gamma", "{{ array | map: nil | map: 'handle' | join: ' ' }}", { "array" => array })
end
def test_map_with_empty_string_property
array = [
{ "handle" => "alpha", "value" => "A" },
{ "handle" => "beta", "value" => "B" },
{ "handle" => "gamma", "value" => "C" }
]
assert_template_result("alpha beta gamma", "{{ array | map: '' | map: 'handle' | join: ' ' }}", { "array" => array })
end
def test_map_with_value_property
array = [
{ "handle" => "alpha", "value" => "A" },
{ "handle" => "beta", "value" => "B" },
{ "handle" => "gamma", "value" => "C" }
]
assert_template_result("A B C", "{{ array | map: 'value' | join: ' ' }}", { "array" => array })
end
def test_map_returns_input_with_no_property
input = [
[1], [1],
[2], [2],
[3], [3],
] ]
assert_raises(Liquid::ArgumentError) do result = @filters.map(input, nil)
@filters.map(foo, nil) assert_equal(input.flatten, result)
end
result = @filters.map(input, '')
assert_equal(input.flatten, result)
end end
def test_sort_works_on_enumerables def test_sort_works_on_enumerables
@@ -1033,6 +1065,22 @@ class StandardFiltersTest < Minitest::Test
assert_template_result(expected_output, template, { "array" => array }) assert_template_result(expected_output, template, { "array" => array })
end end
def test_where_with_empty_string_is_a_no_op
environment = { "array" => ["alpha", "beta", "gamma"] }
expected_output = "alpha beta gamma"
template = "{{ array | where: '' | join: ' ' }}"
assert_template_result(expected_output, template, environment)
end
def test_where_with_nil_is_a_no_op
environment = { "array" => ["alpha", "beta", "gamma"] }
expected_output = "alpha beta gamma"
template = "{{ array | where: nil | join: ' ' }}"
assert_template_result(expected_output, template, environment)
end
def test_where_with_value def test_where_with_value
array = [ array = [
{ "handle" => "alpha", "ok" => true }, { "handle" => "alpha", "ok" => true },
@@ -1061,6 +1109,19 @@ class StandardFiltersTest < Minitest::Test
assert_template_result(expected_output, template, { "array" => array }) assert_template_result(expected_output, template, { "array" => array })
end end
def test_where_with_non_string_property
array = [
{ "handle" => "alpha", "{}" => true },
{ "handle" => "beta", "{}" => false },
{ "handle" => "gamma", "{}" => false },
{ "handle" => "delta", "{}" => true },
]
template = "{{ array | where: some_property, true | map: 'handle' | join: ' ' }}"
expected_output = "alpha delta"
assert_template_result(expected_output, template, { "array" => array, "some_property" => {} })
end
def test_where_string_keys def test_where_string_keys
input = [ input = [
"alpha", "beta", "gamma", "delta" "alpha", "beta", "gamma", "delta"
@@ -1268,6 +1329,17 @@ class StandardFiltersTest < Minitest::Test
assert_template_result("0", "{{ input | sum: 'subtotal' }}", { "input" => input }) assert_template_result("0", "{{ input | sum: 'subtotal' }}", { "input" => input })
end end
def test_sum_with_non_string_property
input = [{ "true" => 1 }, { "1.0" => 0.2, "1" => -0.3 }, { "1..5" => 0.4 }]
assert_equal(1, @filters.sum(input, true))
assert_equal(0.2, @filters.sum(input, 1.0))
assert_equal(-0.3, @filters.sum(input, 1))
assert_equal(0.4, @filters.sum(input, (1..5)))
assert_equal(0, @filters.sum(input, nil))
assert_equal(0, @filters.sum(input, ""))
end
private private
def with_timezone(tz) def with_timezone(tz)
+10
View File
@@ -131,6 +131,16 @@ class LexerUnitTest < Minitest::Test
assert_equal([[:id, "false"], [:number, "1"], [:end_of_string]], tokenize("false 1")) assert_equal([[:id, "false"], [:number, "1"], [:end_of_string]], tokenize("false 1"))
end end
def test_error_with_invalid_utf8
error = assert_raises(SyntaxError) do
tokenize("\x00\xff")
end
assert_equal(
'Liquid syntax error: Invalid byte sequence in UTF-8',
error.message,
)
end
private private
def tokenize(input) def tokenize(input)
+24
View File
@@ -157,4 +157,28 @@ class DocTagUnitTest < Minitest::Test
assert_template_result('', "{% doc %}123{% enddoc\nxyz %}") assert_template_result('', "{% doc %}123{% enddoc\nxyz %}")
assert_template_result('', "{% doc %}123{% enddoc\n xyz enddoc %}") assert_template_result('', "{% doc %}123{% enddoc\n xyz enddoc %}")
end end
def test_doc_tag_visitor
template_source = '{% doc %}{% enddoc %}'
assert_equal(
[Liquid::Doc],
visit(template_source),
)
end
private
def traversal(template)
ParseTreeVisitor
.for(Template.parse(template).root)
.add_callback_for(Liquid::Doc) do |tag|
tag_class = tag.class
tag_class
end
end
def visit(template)
traversal(template).visit.flatten.compact
end
end end
+11
View File
@@ -35,4 +35,15 @@ class TemplateUnitTest < Minitest::Test
def test_template_inheritance def test_template_inheritance
assert_equal("foo", TemplateSubclass.parse("foo").render) assert_equal("foo", TemplateSubclass.parse("foo").render)
end end
def test_invalid_utf8
input = "\xff\x00"
error = assert_raises(SyntaxError) do
Liquid::Tokenizer.new(source: input, string_scanner: StringScanner.new(input))
end
assert_equal(
'Liquid syntax error: Invalid byte sequence in UTF-8',
error.message,
)
end
end end