Skip to content

Commit

Permalink
fix lexer parsing ID 'contains' as comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmichaelgo committed Oct 30, 2024
1 parent 1943441 commit 002be2b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/liquid/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class Lexer2
COMPARISON_LESS_THAN = [:comparison, "<"].freeze
COMPARISON_LESS_THAN_OR_EQUAL = [:comparison, "<="].freeze
COMPARISON_NOT_EQUAL_ALT = [:comparison, "<>"].freeze
CONTAINS = /contains(?=\s)/
DASH = [:dash, "-"].freeze
DOT = [:dot, "."].freeze
DOTDOT = [:dotdot, ".."].freeze
Expand Down Expand Up @@ -211,7 +210,7 @@ def tokenize

if type && (t = @ss.scan(pattern))
# Special case for "contains"
@output << if type == :id && t == "contains"
@output << if type == :id && t == "contains" && @output.last&.first != :dot
COMPARISON_CONTAINS
else
[type, t]
Expand Down
7 changes: 7 additions & 0 deletions test/unit/lexer_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,11 @@ def test_greater_than_two_digits
tokens = Lexer.new("foo > 12").tokenize
assert_equal([[:id, 'foo'], [:comparison, '>'], [:number, '12'], [:end_of_string]], tokens)
end

def test_contains_as_attribute_name
assert_equal(
[[:id, "a"], [:dot, "."], [:id, "contains"], [:dot, "."], [:id, "b"], [:end_of_string]],
Lexer.new("a.contains.b").tokenize,
)
end
end

0 comments on commit 002be2b

Please sign in to comment.