fix: report error for malformed else/elsif/endif/endfor, #713

This commit is contained in:
Harttle
2024-07-05 01:23:33 +08:00
parent d141c4bdd2
commit 22b5a12333
8 changed files with 41 additions and 47 deletions
+2 -10
View File
@@ -471,19 +471,11 @@ describe('Issues', function () {
})
it('#670 Should not render anything after an else branch', () => {
const engine = new Liquid()
const result = engine.parseAndRenderSync('{% assign value = "this" %}' +
'{% if false %}don\'t show' +
'{% else %}show {{ value }}' +
'{% else %}don\'t show{% endif %}', {})
expect(result).toEqual('show this')
expect(() => engine.parseAndRenderSync('{% assign value = "this" %}{% if false %}{% else %}{% else %}{% endif %}')).toThrow('duplicated else')
})
it('#672 Should not render an elseif after an else branch', () => {
const engine = new Liquid()
const result = engine.parseAndRenderSync('{% if false %}don\'t show' +
'{% else %}show' +
'{% elsif true %}don\'t show' +
'{% endif %}', {})
expect(result).toEqual('show')
expect(() => engine.parseAndRenderSync('{% if false %}{% else %}{% elsif true %}{% endif %}')).toThrow('unexpected elsif after else')
})
it('#675 10.10.1 Operator: contains regression', () => {
const engine = new Liquid()
+3 -3
View File
@@ -78,10 +78,10 @@ describe('tags/for', function () {
.rejects.toThrow('illegal tag: {%for c alpha%}, line:1, col:1')
})
it('should reject when inner templates rejected', function () {
const src = '{%for c in alpha%}{%throwingTag%}{%endfor%}'
it('should throw for additional args', function () {
const src = "{% for f in foo %} foo {% else foo = 'blah' %} {% endfor %}"
return expect(liquid.parseAndRender(src, scope))
.rejects.toThrow(/intended render error/)
.rejects.toThrow(`unexpected "foo = 'blah'", line:1, col:1`)
})
})
+13 -7
View File
@@ -26,6 +26,12 @@ describe('tags/if', function () {
return expect(html).toBe('')
})
it('should throw for additional args', function () {
const src = "{% if foo %} foo {% else foo = 'blah' %} {% endif %}"
return expect(liquid.parseAndRender(src, scope))
.rejects.toThrow(`unexpected "foo = 'blah'", line:1, col:1`)
})
describe('single value as condition', function () {
it('should support boolean', async function () {
const src = '{% if false %}1{%elsif true%}2{%else%}3{%endif%}'
@@ -155,12 +161,12 @@ describe('tags/if', function () {
const html = await liquid.parseAndRender(src, scope)
return expect(html).toBe('no')
})
it('should not render anything after an else branch even when first else branch is empty', () => {
const engine = new Liquid()
const result = engine.parseAndRenderSync('{% if false %}don\'t show' +
'{% else %}' +
'{% else %}don\'t show' +
'%{% endif %}', {})
expect(result).toEqual('')
it('should throw for duplicated else', () => {
expect(() => liquid.parseAndRenderSync('{% if false %}{% else %}{% else %}{% endif %}'))
.toThrow(`duplicated else`)
})
it('should throw for unexpected elsif', () => {
expect(() => liquid.parseAndRenderSync('{% if false %}{% else %}{% elsif true %}{% endif %}'))
.toThrow(`unexpected elsif after else`)
})
})