harttle / liquidjs

A simple, expressive, safe and Shopify compatible template engine in pure JavaScript.
https://liquidjs.com
MIT License
1.51k stars 240 forks source link

fix: in conditionals, don't render anything after an else branch #671

Closed joel-hamilton closed 7 months ago

joel-hamilton commented 8 months ago

This PR enforces a simple rule: don't render anything after an else branch.

There are several bugs currently where if, unless and case conditionals can have branches that occur after an else render, or that branches after an else can actually cause the else branch itself not to render.

Fixes: #670 and #672

Example

<p>If</p>
{% if false %}
  don't show
  {%- else -%}
  show
  {%- else -%}
  don't show
{% endif %}

<p>If with Elsif</p>
{% if false %}
  don't show
  {%- else -%}
  show
  {%- elsif true -%}
  don't show
{% endif %}

<p>Unless</p>
{% unless true %}
  don't show
  {%- else -%}
  show
  {%- else -%}
  don't show
{% endunless %}

<p>Case</p>
{% case true %}
  {%- when false- %}
    don't show
  {%- else -%}
    show
  {%- else -%}
    don't show
{% endcase %}

<p>Case with When</p>
{% case true -%}
  {% when false -%}
    don't show (false)
  {%- else -%}
    show
  {%- else -%}
    don't show (second else)
  {%- when true -%}
    don't show (truthy when condition after else)
  {%- when true -%}
    don't show (truthy when condition after else)
{%- endcase %}

master Behaviour:

<p>If</p>
showdon't show

<p>If with Elsif</p>
don't show

<p>Unless</p>
showdon't show

<p>Case</p>
showdon't show

<p>Case with When</p>
don't show (truthy when condition after else)don't show (truthy when condition after else)

PR Behaviour:

<p>If</p>
show

<p>If with Elsif</p>
show

<p>Unless</p>
show

<p>Case</p>
show

<p>Case with When</p>
show
coveralls commented 8 months ago

Pull Request Test Coverage Report for Build 7946818540

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
src/tags/if.ts 8 10 80.0%
<!-- Total: 25 27 92.59% -->
Totals Coverage Status
Change from base Build 7895611436: -0.09%
Covered Lines: 2261
Relevant Lines: 2270

💛 - Coveralls
harttle commented 7 months ago

@all-contributors please add @joel-hamilton for code

allcontributors[bot] commented 7 months ago

@harttle

I've put up a pull request to add @joel-hamilton! :tada:

github-actions[bot] commented 7 months ago

:tada: This PR is included in version 10.10.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: