Skip to content

Commit fa9a764

Browse files
committed
Add formatting support for optional liquidDoc params
---- - Preserve `[]` delimiteres - Strips whitespace between delimiter and param name
1 parent d5e8ec9 commit fa9a764

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

.changeset/olive-cougars-float.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@shopify/prettier-plugin-liquid': minor
3+
---
4+
5+
Add formatting support for optional liquidDoc parameters (e.g. `@param [optional-parameter] - paramDescription`).
6+
Whitespace is now stripped around the parameter name and the optional delimiters.

packages/prettier-plugin-liquid/src/printer/print/liquid.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,10 @@ export function printLiquidDocParam(
520520
parts.push(' ', `{${node.paramType.value}}`);
521521
}
522522

523-
if (node.paramName.value) {
523+
if (node.required) {
524524
parts.push(' ', node.paramName.value);
525+
} else {
526+
parts.push(' ', `[${node.paramName.value}]`);
525527
}
526528

527529
if (node.paramDescription?.value) {

packages/prettier-plugin-liquid/src/test/liquid-doc/fixed.liquid

+15-8
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,27 @@ It should format the param description with a dash separator
2020

2121
It should respect the liquidDocParamDash option liquidDocParamDash: false
2222
{% doc %}
23-
@param paramName param with description
23+
@param paramName - param with description
2424
{% enddoc %}
2525

2626
It should normalize the param description
2727
{% doc %}
2828
@param paramName - param with description
2929
{% enddoc %}
3030

31+
It should strip whitespace between optional param delimiters
32+
{% doc %}
33+
@param [paramName] - param with description
34+
{% enddoc %}
35+
36+
It should not break params with malformed optional delimiters
37+
{% doc %}
38+
@param [missingTail - param with description
39+
@param missingHead - ] - param with description
40+
@param [too many words no desc]
41+
@param [too many words] - param with description
42+
{% enddoc %}
43+
3144
It should push example content to the next line
3245
{% doc %}
3346
@example
@@ -37,20 +50,16 @@ It should push example content to the next line
3750
It should allow single empty lines between content
3851
{% doc %}
3952
@example
40-
4153
This is a valid example
4254
43-
So is this without a newline
44-
See?
55+
So is this without a newline See?
4556
{% enddoc %}
4657

4758
It should allow multiple empty lines between content
4859
{% doc %}
4960
@example
50-
5161
This is a valid example
5262
53-
5463
Here is another example
5564
5665
with a single empty line
@@ -59,14 +68,12 @@ It should allow multiple empty lines between content
5968
It should remove empty lines at the end of the content
6069
{% doc %}
6170
@example
62-
6371
Here is my content and a newline
6472
{% enddoc %}
6573

6674
It should respect example content with param and description
6775
{% doc %}
6876
@param paramName - param with description
6977
@example
70-
7178
This is a valid example
7279
{% enddoc %}

packages/prettier-plugin-liquid/src/test/liquid-doc/index.liquid

+21-15
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,35 @@ It should trim whitespace between nodes
1010

1111
It should normalize the param type
1212
{% doc %}
13-
@param { string } paramName - param with description
13+
@param {string} paramName - param with description
1414
{% enddoc %}
1515

1616
It should format the param description with a dash separator
1717
{% doc %}
18-
@param paramName - param with description
18+
@param paramName - param with description
1919
{% enddoc %}
2020

2121
It should respect the liquidDocParamDash option liquidDocParamDash: false
2222
{% doc %}
23-
@param paramName param with description
23+
@param paramName - param with description
2424
{% enddoc %}
2525

2626
It should normalize the param description
2727
{% doc %}
28-
@param paramName - param with description
28+
@param paramName - param with description
29+
{% enddoc %}
30+
31+
It should strip whitespace between optional param delimiters
32+
{% doc %}
33+
@param [ paramName ] - param with description
34+
{% enddoc %}
35+
36+
It should handle params with malformed optional delimiters
37+
{% doc %}
38+
@param [missingTail - param with description
39+
@param missingHead - ] - param with description
40+
@param [too many words no desc]
41+
@param [too many words] - param with description
2942
{% enddoc %}
3043

3144
It should push example content to the next line
@@ -36,20 +49,16 @@ It should push example content to the next line
3649
It should allow single empty lines between content
3750
{% doc %}
3851
@example
39-
4052
This is a valid example
4153
42-
So is this without a newline
43-
See?
54+
So is this without a newline See?
4455
{% enddoc %}
4556

4657
It should allow multiple empty lines between content
4758
{% doc %}
4859
@example
49-
5060
This is a valid example
5161
52-
5362
Here is another example
5463
5564
with a single empty line
@@ -58,15 +67,12 @@ It should allow multiple empty lines between content
5867
It should remove empty lines at the end of the content
5968
{% doc %}
6069
@example
61-
6270
Here is my content and a newline
63-
6471
{% enddoc %}
6572

6673
It should respect example content with param and description
6774
{% doc %}
68-
@param paramName - param with description
69-
@example
70-
71-
This is a valid example
75+
@param paramName - param with description
76+
@example
77+
This is a valid example
7278
{% enddoc %}

0 commit comments

Comments
 (0)