theme-check icon indicating copy to clipboard operation
theme-check copied to clipboard

New check for `for` loops used for filtering by handle

Open macournoyer opened this issue 3 years ago • 2 comments

# bad
{% for product in all_products %}
  {% if product.handle == 'foobar' %}
    {% assign p = product %}
   {% endif %}
{% endif %}

# good
{% assign p = all_products.foobar %}

Also, potentially other checks for recommending https://shopify.dev/docs/themes/liquid/reference/objects/product#product-options_by_name, and such.

macournoyer avatar Mar 15 '21 16:03 macournoyer

Not to detract from the main reason for this issue, but am wondering if it's also worth looking for nested liquid loops...

  {% for product in collection.products %}
    {% for tag in product.tags %}
      {% for variant in product.variants %}
        {% for collection in product.collections %}
        
        {% endfor %}
      {% endfor %}
    {% endfor %}
  {% endfor %}

It seems there are checks in place for nested snippets, but not for loops.

If this warrants a separate issue just say the word :)

darryn avatar Jun 30 '21 05:06 darryn

Oh yeah this is a very good idea @darryn! Please create an issue 😄

macournoyer avatar Jun 30 '21 12:06 macournoyer