Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit c54c32e

Browse files
authored
Merge pull request #155 from Shopify/fix/154-paginate
Fix paginate parsing error
2 parents c2c8827 + b3e8b60 commit c54c32e

File tree

6 files changed

+57
-10
lines changed

6 files changed

+57
-10
lines changed

grammar/liquid-html.ohm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Liquid <: Helpers {
164164

165165
liquidTagOpenPaginate = liquidTagOpenRule<"paginate", liquidTagOpenPaginateMarkup>
166166
liquidTagOpenPaginateMarkup =
167-
liquidExpression space+ "by" space+ liquidExpression (argumentSeparatorOptionalComma tagArguments)? space*
167+
liquidExpression space+ "by" space+ liquidExpression argumentSeparatorOptionalComma tagArguments space*
168168

169169
liquidDrop = "{{" "-"? space* liquidDropCases "-"? "}}"
170170
liquidDropCases = liquidVariable | liquidDropBaseCase

src/parser/grammar.spec.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { expect } from 'chai';
2-
import { liquidHtmlGrammar } from '~/parser/grammar';
2+
import { liquidHtmlGrammar, liquidStatementsGrammar } from '~/parser/grammar';
33

44
describe('Unit: liquidHtmlGrammar', () => {
5-
it('should succeed at parsing valid HTML+Liquid', () => {
5+
it('should parse or not parse HTML+Liquid', () => {
66
expectMatchSucceeded('<h6 data-src="hello world">').to.be.true;
77
expectMatchSucceeded('<a src="https://product"></a>').to.be.true;
88
expectMatchSucceeded('<a src="https://google.com"></b>').to.be.true;
@@ -51,14 +51,32 @@ describe('Unit: liquidHtmlGrammar', () => {
5151
expectMatchSucceeded(`<div data-popup-{{ section.id }}="size-{{ section.id }}">`).to.be.true;
5252
expectMatchSucceeded('<img {% if aboveFold %} loading="lazy"{% endif %} />').to.be.true;
5353
expectMatchSucceeded('<svg><use></svg>').to.be.true;
54-
});
55-
56-
it('should fail at parsing invalid HTML+Liquid', () => {
57-
// Not valid HTML tag
5854
expectMatchSucceeded('<6h>').to.be.false;
55+
56+
function expectMatchSucceeded(text: string) {
57+
const match = liquidHtmlGrammar.match(text, 'Node');
58+
return expect(match.succeeded());
59+
}
5960
});
6061

61-
function expectMatchSucceeded(text: string) {
62-
return expect(liquidHtmlGrammar.match(text, 'Node').succeeded());
63-
}
62+
it('should parse or not parse {% liquid %} lines', () => {
63+
expectMatchSucceeded(`
64+
layout none
65+
66+
paginate search.results by 28
67+
for item in search.results
68+
if item.object_type != 'product'
69+
continue
70+
endif
71+
72+
render 'product-item', product: item
73+
endfor
74+
endpaginate
75+
`).to.be.true;
76+
77+
function expectMatchSucceeded(text: string) {
78+
const match = liquidStatementsGrammar.match(text.trimStart(), 'Node');
79+
return expect(match.succeeded());
80+
}
81+
});
6482
});

src/parser/grammar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const liquidHtmlGrammars = ohm.grammars(
55
);
66

77
export const liquidHtmlGrammar = liquidHtmlGrammars['LiquidHTML'];
8+
export const liquidStatementsGrammar = liquidHtmlGrammars['LiquidStatement'];
89

910
// see ../../grammar/liquid-html.ohm for full list
1011
export const BLOCKS = (

test/issue-154/fixed.liquid

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{%- liquid
2+
layout none
3+
4+
paginate search.results by 28
5+
render 'product-item', product: item
6+
endpaginate
7+
8+
paginate search.results by 28, window_size: 1
9+
render 'product-item', product: item
10+
endpaginate
11+
-%}

test/issue-154/index.liquid

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{%- liquid
2+
layout none
3+
4+
paginate search.results by 28
5+
render 'product-item', product: item
6+
endpaginate
7+
8+
paginate search.results by 28 window_size:1
9+
render 'product-item', product: item
10+
endpaginate
11+
-%}

test/issue-154/index.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { assertFormattedEqualsFixed } from '../test-helpers';
2+
import * as path from 'path';
3+
4+
describe(`Unit: ${path.basename(__dirname)}`, () => {
5+
assertFormattedEqualsFixed(__dirname);
6+
});

0 commit comments

Comments
 (0)