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

Prettier Liquid: if, unless, elsif, else #62

Closed
Tracked by #5
charlespwd opened this issue Aug 9, 2022 · 0 comments · Fixed by #77
Closed
Tracked by #5

Prettier Liquid: if, unless, elsif, else #62

charlespwd opened this issue Aug 9, 2022 · 0 comments · Fixed by #77
Assignees

Comments

@charlespwd
Copy link
Collaborator

charlespwd commented Aug 9, 2022

https://shopify.dev/api/liquid/tags#conditional-tags
https://shopify.dev/api/liquid/basics#operators

{% if|unless|elsif condition %}
{% else %}
{% endif %}
==
!=
>
<
>=
<=
or
and
contains
  • parens are not allowed
  • evaluated right to left

need to decide

{% # (1) conditions first %}
{% if a > b
  and c < d
  or col != empty
  and col contains string
%}

{% # (2) conditions last %}
{% if a > b and
  c < d or
  col != empty and
  col contains string
%}

I'm definitely a conditional first kind of guy (usually), but I'm not sure that's great considering those don't align as well as || and &&...

In fact, just looking at the above, I think the second one looks better.

Moreover, thinking about the "evaluated right to left," that'd mean that you'd want to consider them "whatever is at the top" + "logical operator" + whatever is down below.

Which I think makes more sense with the second style.

{% if a > b and
  c < d or
  col != empty and
  col contains string
%}
  if a > b AND
  (
    c < d OR
    (
      col != empty AND
      col contains string
    )
  )
{% endif %}

Because then the "sub" conditions are little blocks that can be thought of line by line.

Partner slack and internal slack suggests we want conditions first. So it'll be conditions first.
image

need to decide (2)

What if the conditionals are really really long? e.g.

{% if product.metafields.information.thread_count.value
    > product.metafields.information.lower.value
  and product.metafields.information.thread_count.value
    < product.metafields.information.upper.value
%}

I feel like indenting with operator first is the way to go. I think we can do this with groups. So I'll do it.

@charlespwd charlespwd self-assigned this Aug 12, 2022
charlespwd added a commit that referenced this issue Aug 15, 2022
Decisions:
- We're going logical operators first
- We're maybe breaking on comparator (but grouped)
- Same syntax as others `{% if $condition1`, ..., `%}`

Partial fix of #62
charlespwd added a commit that referenced this issue Aug 15, 2022
Reuse same logic as if/unless.

This commit is bigger because of the LiquidBranch shenanigans.

Fixes #62
charlespwd added a commit that referenced this issue Aug 15, 2022
Decisions:
- We're going logical operators first
- We're maybe breaking on comparator (but grouped)
- Same syntax as others `{% if $condition1`, ..., `%}`

Partial fix of #62
charlespwd added a commit that referenced this issue Aug 15, 2022
Reuse same logic as if/unless.

This commit is bigger because of the LiquidBranch shenanigans.

Fixes #62
@charlespwd charlespwd linked a pull request Aug 15, 2022 that will close this issue
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant