Shopify / prettier-plugin-liquid

Prettier Liquid/HTML plugin
https://npm.im/@shopify/prettier-plugin-liquid
MIT License
93 stars 15 forks source link

Add support for nested block comments #108

Closed charlespwd closed 1 year ago

charlespwd commented 1 year ago

Fixes #74

The language allows us to do this (for convenience). This PR adds fixes support for that.

The technique used is a bit similar to the one I used for Syntax Highlighting.

Before

A "comment" block was considered a "raw" tag block. We'd parse it like this:

This approach didn't work for nested tags, since we'd match over the {% comment %} of the nested comment and stop at the {% endcomment %} of the nested comment. Leaving us with a dangling comment body and {% endcomment %}.

Something like this:

{% comment %}
This is inside the comment
  {% comment %}
    nested
  {% endcomment}
This is outside the comment since we stoped parsing on the previous line
{% endcomment %}

After

Slightly different algorithm. We parse like this:

The "trick" here is that we stop parsing on nested {% comment %} and try to recurse on the rule.

So this will work:

{% comment %}
This is inside the comment
{% comment %}
  this is nested inside a recursed rule
{% endcomment %}
This is inside the top comment
{% endcomment %}