Skip to content

Commit e0d3790

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

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
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-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ It should format the param description with a dash separator
1919
{% enddoc %}
2020

2121
It should respect the liquidDocParamDash option liquidDocParamDash: false
22+
liquidDocParamDash: false
2223
{% doc %}
2324
@param paramName param with description
2425
{% enddoc %}
@@ -28,6 +29,19 @@ It should normalize the param description
2829
@param paramName - param with description
2930
{% enddoc %}
3031

32+
It should strip whitespace between optional param delimiters
33+
{% doc %}
34+
@param [paramName] - param with description
35+
{% enddoc %}
36+
37+
It should not break params with malformed optional delimiters
38+
{% doc %}
39+
@param [missingTail - param with description
40+
@param missingHead - ] - param with description
41+
@param [too many words no desc]
42+
@param [too many words] - param with description
43+
{% enddoc %}
44+
3145
It should push example content to the next line
3246
{% doc %}
3347
@example
@@ -37,20 +51,16 @@ It should push example content to the next line
3751
It should allow single empty lines between content
3852
{% doc %}
3953
@example
40-
4154
This is a valid example
4255
43-
So is this without a newline
44-
See?
56+
So is this without a newline See?
4557
{% enddoc %}
4658

4759
It should allow multiple empty lines between content
4860
{% doc %}
4961
@example
50-
5162
This is a valid example
5263
53-
5464
Here is another example
5565
5666
with a single empty line
@@ -59,14 +69,12 @@ It should allow multiple empty lines between content
5969
It should remove empty lines at the end of the content
6070
{% doc %}
6171
@example
62-
6372
Here is my content and a newline
6473
{% enddoc %}
6574

6675
It should respect example content with param and description
6776
{% doc %}
6877
@param paramName - param with description
6978
@example
70-
7179
This is a valid example
7280
{% enddoc %}

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

+22-15
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,36 @@ 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
22+
liquidDocParamDash: false
2223
{% doc %}
23-
@param paramName param with description
24+
@param paramName - param with description
2425
{% enddoc %}
2526

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

3145
It should push example content to the next line
@@ -36,20 +50,16 @@ It should push example content to the next line
3650
It should allow single empty lines between content
3751
{% doc %}
3852
@example
39-
4053
This is a valid example
4154
42-
So is this without a newline
43-
See?
55+
So is this without a newline See?
4456
{% enddoc %}
4557

4658
It should allow multiple empty lines between content
4759
{% doc %}
4860
@example
49-
5061
This is a valid example
5162
52-
5363
Here is another example
5464
5565
with a single empty line
@@ -58,15 +68,12 @@ It should allow multiple empty lines between content
5868
It should remove empty lines at the end of the content
5969
{% doc %}
6070
@example
61-
6271
Here is my content and a newline
63-
6472
{% enddoc %}
6573

6674
It should respect example content with param and description
6775
{% doc %}
68-
@param paramName - param with description
69-
@example
70-
71-
This is a valid example
76+
@param paramName - param with description
77+
@example
78+
This is a valid example
7279
{% enddoc %}

0 commit comments

Comments
 (0)