Liquid Snippets by ALSEL
🧰 ユーティリティ上級

Prettier Liquid プラグインの HTML 属性フォーマットテスト

Prettier 向け Liquid プラグインの HTML 属性フォーマット機能をテストするコード集。ダブルクォート・シングルクォート、属性の折り返し、Liquid タグ埋め込み属性などの整形ルールを検証する。

用途
Shopify テーマの開発環境で Prettier による自動整形を導入する際に、HTML 属性がどのように整形されるかを事前に確認・検証したいとき。
設置場所
このファイル自体はテストケースであり、テーマに貼り付けるものではない。Prettier Liquid プラグインの開発・検証時に packages/prettier-plugin-liquid/src/test/html-attributes/ ディレクトリ配下に保持し、テストスイート実行時に参照される。
注意点
このコードはテスト用のモック HTML・Liquid 混在コードであり、そのまま実動作するテンプレートではない。printWidth オプションやシングルクォート設定など、Prettier 設定によって整形結果が異なるため、プロジェクトの .prettierrc または package.json で設定値を確認してから利用する。Liquid タグ内の属性複数指定時の改行判定は printWidth に依存するため、ツール更新時に検証結果が変わる可能性がある。
タグ:prettierpluginhtml-formattingliquid-tagtest-case

コード

110 行 / liquid
It should use double quotes for all attributes, unless boolean attribute
<div id=1234 class='long string of classes' style="color: blue;" content="" disabled></div>

It should use single quotes for all attributes, unless boolean attribute when singleQuote is true
singleQuote: true
<div id=1234 class='long string of classes' style="color: blue;" content="" disabled></div>

It should break LiquidTags attribute nodes and consider the insides _not_ whitespace sensitive
<div src="https://content.instructables.com/ORIG/F23/YMO0/FPIUQOJF/F23YMO0FPIUQOJF.jpg"
   width="320" height="240" loading="lazy" {{ block.shopify_attributes }} {% if A %}disabled{% else %}checked{% endif %}></div>

It should not try to format HTML attributes (yet) (1)
<img class="image {% if sticky %}image--sticky{% endif %} {% if wrapped %}image--wrapped{% endif %}" id="img--{{ product.id }}" src="{{ product.featured_image }}" width="{{ product.featured_image.width }}" height="{{ product.featured_image.height }}" loading="lazy">

It should break and indent attributes that are long af
<img
srcset="{% if media.preview_image.width >= 493 %}{{ media.preview_image | image_url: width: 493 }} 493w,{% endif %}
{% if media.preview_image.width >= 600 %}{{ media.preview_image | image_url: width: 600 }} 600w,{% endif %}
{% if media.preview_image.width >= 713 %}{{ media.preview_image | image_url: width: 713 }} 713w,{% endif %}
{% if media.preview_image.width >= 823 %}{{ media.preview_image | image_url: width: 823 }} 823w,{% endif %}
{% if media.preview_image.width >= 990 %}{{ media.preview_image | image_url: width: 990 }} 990w,{% endif %}
{% if media.preview_image.width >= 1100 %}{{ media.preview_image | image_url: width: 1100 }} 1100w,{% endif %}
{% if media.preview_image.width >= 1206 %}{{ media.preview_image | image_url: width: 1206 }} 1206w,{% endif %}
{% if media.preview_image.width >= 1346 %}{{ media.preview_image | image_url: width: 1346 }} 1346w,{% endif %}
{% if media.preview_image.width >= 1426 %}{{ media.preview_image | image_url: width: 1426 }} 1426w,{% endif %}
{% if media.preview_image.width >= 1646 %}{{ media.preview_image | image_url: width: 1646 }} 1646w,{% endif %}
{% if media.preview_image.width >= 1946 %}{{ media.preview_image | image_url: width: 1946 }} 1946w,{% endif %}
{{ media.preview_image | image_url }} {{ media.preview_image.width }}w"
src="{{ media | image_url: width: 1946 }}"
sizes="(min-width: {{ settings.page_width }}px) {{ settings.page_width | minus: 100 | times: media_width | round }}px, (min-width: 990px) calc({{ media_width | times: 100 }}vw - 10rem), (min-width: 750px) calc((100vw - 11.5rem) / 2), calc(100vw - 4rem)"
{% unless lazy_load == false %}loading="lazy"{% endunless %}
width="973"
height="{{ 973 | divided_by: media.preview_image.aspect_ratio | ceil }}"
alt="{{ media.preview_image.alt | escape }}"
>

If there's only one attribute and self-closing, don't break when the line is too long.
<img class="Lorem ipsum dolor sit amet consectetur adipiscing elit Sed feugiat semper sem eu ultrices nisl interdum sit amet">

If there's only one attribute, not self-closing and no children, break the attr and close the tag on a different line.
<div class="Lorem ipsum dolor sit amet consectetur adipiscing elit Sed feugiat semper sem eu ultrices nisl interdum sit amet">
</div>

If there's only one attribute and not self-closing, but with children, do not break the attr and close the tag on a different line.
<div class="Lorem ipsum dolor sit amet consectetur adipiscing elit Sed feugiat semper sem eu ultrices nisl interdum sit amet">
Hello
</div>

If there's only one attribute, not self-closing, with children, do not break the LiquidTag attr and close the tag on a different line.
printWidth: 30
<div {% echo 'disabled' %}>
  Hello
</div>

If there's only one attribute, not self-closing, with children, do not break the LiquidNode attr and close the tag on a different line.
printWidth: 30
<div {{ block_attributes }}>
  Hello
</div>

If there's only one attribute, not self closing, with children, but the attribute is a liquid tag that has children, break it normally.
printWidth: 40
<div {% if some_condition %} attribute=value {% endif %}>hello world</div>

If there's only one attribute, not self closing, without children, but the attribute is a liquid tag that has children, break it normally.
printWidth: 40
<div {% if some_condition %} attribute=value {% endif %}></div>

If there's multiple attributes inside an if tag attribute node, print until print width
<div {% if some_condition %}attribute="abstractNode" ok=value{% endif %}></div>

If there's multiple attributes inside an if tag attribute node, print until print width
printWidth: 1
<div {% if some_condition %}attribute="abstractNode" ok=value{% endif %}></div>

If there's multiple attributes inside an if tag attribute node with a newline, maintain newline
<div {% if some_condition %}
       attribute="abstractNode"
       ok=value{% endif %}></div>

If singleQuote is true, and the attribute value contained single quotes, use double quotes
singleQuote: true
<link onload="this.media='all'">

If singleQuote is false, and the attribute value contained double quotes, use single quotes
singleQuote: false
<link onload='this.media="all"'>

If singleQuote is true, and the attribute value has liquid tags that contain single quotes, don't care.
singleQuote: true
<script src="{{ 'styles.css' | asset_url }}"></script>

If singleQuote is false, and the attribute value has liquid tags that contain double quotes, don't care.
singleQuote: false
<script src="{{ "styles.css" | asset_url }}"></script>

It should pretty print attribute names with Liquid in them
singleQuote: true
<div data-popup-{{section.id}}="size-{{ section.id }}"></div>

It should pretty print attribute names with Liquid in them without new lines
printWidth: 1
<div data-popup-{{section.id|upcase}}="size-{{ section.id }}"></div>

It should pretty unquoted liquid drop attributes
<div id={{ section.id }}--omg></div>

It should pretty print smart quotes into dumb quotes to prevent copy pasting errors
<h1 class=“section-header__title h2id=‘abc’></h1>

出典・ライセンス

License:
MIT

このコードは Shopify 著作の MIT ライセンスソースです。 原本の著作権は Shopify が保有します。日本語訳は ALSEL によるものです。

関連項目

🧰 ユーティリティ初級

コメント後の空白フォーマット

Liquid コメントタグの直後に改行や空白がある場合の正しいフォーマット例。prettier-plugin-liquid によるコード整形テストケース。

📁 theme-tools·MIT·6
🧰 ユーティリティ初級

Liquidコメントのフォーマット

Liquid のコメントタグ直後に改行がある場合の正しいフォーマット例。prettier-plugin-liquid による自動整形テストケース。

📁 theme-tools·MIT·6
🧰 ユーティリティ初級

セクション呼び出しの改行制御

{% sections %} タグの前後の空白制御(ホワイトスペース削除演算子 `{%-` と `-%}` の使い方)を示すサンプル。Prettier による Liquid コードフォーマットで改行が崩れないことを確認するテストケース。

📁 theme-tools·MIT·7
🧰 ユーティリティ初級

Prettier フォーマッタの HTML リスト整形テスト

Prettier Liquid プラグインが HTML のリスト要素(ul / ol)をどのように整形・改行するかをテストするサンプルコード。短い行は1行、長い行は複数行に自動で分割される動作を検証する。

📁 theme-tools·MIT·8
🧰 ユーティリティ上級

Vue.js属性のLiquid整形

Vue.js のディレクティブ(@click、:class など)を含む Liquid コードを Prettier で正しく整形するテストケース。spec 準拠の属性名を保持したまま、改行・インデント処理を行う。

📁 theme-tools·MIT·9
🧰 ユーティリティ上級

sections タグのフォーマット

Liquid の sections タグ構文と、Prettier によるフォーマット時の動作を示すテストファイル。タグ名の直前・直後の空白や改行が保持される仕様を確認するサンプル。

📁 theme-tools·MIT·9