Skip to content

Commit 9251a0f

Browse files
committed
Make LiquidDoc @param description dash seperator a configurable prettier option and simplify types
Now that this is a configurable option, we don't need to capture this information in the type. This means we can replace the description node with a simple text node. Simplify types: Remove Replace LiquidDocParamDescription with TextNode Now that the param description is configurable, this isn't needed
1 parent 71b8392 commit 9251a0f

File tree

8 files changed

+40
-68
lines changed

8 files changed

+40
-68
lines changed

packages/liquid-html-parser/src/stage-1-cst.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,10 @@ export interface ConcreteLiquidDocParamNode
111111
extends ConcreteBasicNode<ConcreteNodeTypes.LiquidDocParamNode> {
112112
name: 'param';
113113
paramName: ConcreteTextNode;
114-
paramDescription: ConcreteLiquidDocParamDescription | null;
114+
paramDescription: ConcreteTextNode | null;
115115
paramType: ConcreteTextNode | null;
116116
}
117117

118-
export interface ConcreteLiquidDocParamDescription
119-
extends ConcreteBasicNode<ConcreteNodeTypes.TextNode> {
120-
dashSeparated: boolean;
121-
value: string;
122-
}
123-
124118
export interface ConcreteHtmlNodeBase<T> extends ConcreteBasicNode<T> {
125119
attrList?: ConcreteAttributeNode[];
126120
}

packages/liquid-html-parser/src/stage-2-ast.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1244,13 +1244,11 @@ describe('Unit: Stage 2 (AST)', () => {
12441244
expectPath(ast, 'children.0.body.nodes.0.paramName.value').to.eql('asdf');
12451245
expectPath(ast, 'children.0.body.nodes.0.paramDescription.type').to.eql('TextNode');
12461246
expectPath(ast, 'children.0.body.nodes.0.paramDescription.value').to.eql('');
1247-
expectPath(ast, 'children.0.body.nodes.0.paramDescription.dashSeparated').to.eql(false);
12481247
expectPath(ast, 'children.0.body.nodes.1.type').to.eql('LiquidDocParamNode');
12491248
expectPath(ast, 'children.0.body.nodes.1.name').to.eql('@param');
12501249
expectPath(ast, 'children.0.body.nodes.1.paramName.type').to.eql('TextNode');
12511250
expectPath(ast, 'children.0.body.nodes.1.paramName.value').to.eql('paramWithDescription');
12521251
expectPath(ast, 'children.0.body.nodes.1.paramDescription.type').to.eql('TextNode');
1253-
expectPath(ast, 'children.0.body.nodes.1.paramDescription.dashSeparated').to.eql(true);
12541252
expectPath(ast, 'children.0.body.nodes.1.paramDescription.value').to.eql(
12551253
'param with description and `punctation`. This is still a valid param description.',
12561254
);

packages/liquid-html-parser/src/stage-2-ast.ts

+8-36
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ import {
7373
LiquidHtmlConcreteNode,
7474
ConcreteLiquidTagBaseCase,
7575
ConcreteLiquidTagContentForMarkup,
76-
ConcreteLiquidDocParamDescription,
7776
} from './stage-1-cst';
7877
import { Comparators, NamedTags, NodeTypes, nonTraversableProperties, Position } from './types';
7978
import { assertNever, deepGet, dropLast } from './utils';
@@ -762,19 +761,10 @@ export interface LiquidDocParamNode extends ASTNode<NodeTypes.LiquidDocParamNode
762761
/** The name of the parameter (e.g. "product") */
763762
paramName: TextNode;
764763
/** Optional description of the parameter in a Liquid doc comment (e.g. "The product title") */
765-
paramDescription: LiquidDocParamDescription | null;
764+
paramDescription: TextNode | null;
766765
/** Optional type annotation for the parameter (e.g. "{string}", "{number}") */
767766
paramType: TextNode | null;
768767
}
769-
770-
export interface LiquidDocParamDescription extends ASTNode<NodeTypes.TextNode> {
771-
/** Whether the parameter description is dash-separated (e.g. "paramName - paramDescription") */
772-
dashSeparated: boolean;
773-
774-
/** The body of the description */
775-
value: string;
776-
}
777-
778768
export interface ASTNode<T> {
779769
/**
780770
* The type of the node, as a string.
@@ -1301,8 +1291,8 @@ function buildAst(
13011291
position: position(node.paramName),
13021292
source: node.paramName.source,
13031293
},
1304-
paramDescription: toParamDescription(node.paramDescription),
1305-
paramType: toParamType(node.paramType),
1294+
paramDescription: toNullableTextNode(node.paramDescription),
1295+
paramType: toNullableTextNode(node.paramType),
13061296
});
13071297
break;
13081298
}
@@ -1983,6 +1973,11 @@ function toHtmlSelfClosingElement(
19831973
};
19841974
}
19851975

1976+
function toNullableTextNode(node: ConcreteTextNode | null): TextNode | null {
1977+
if (!node) return null;
1978+
return toTextNode(node);
1979+
}
1980+
19861981
function toTextNode(node: ConcreteTextNode): TextNode {
19871982
return {
19881983
type: NodeTypes.TextNode,
@@ -2087,26 +2082,3 @@ function getUnclosed(node?: ParentNode, parentNode?: ParentNode): UnclosedNode |
20872082
blockStartPosition: 'blockStartPosition' in node ? node.blockStartPosition : node.position,
20882083
};
20892084
}
2090-
2091-
function toParamDescription(
2092-
node: ConcreteLiquidDocParamDescription | null,
2093-
): LiquidDocParamDescription | null {
2094-
if (!node) return null;
2095-
return {
2096-
type: NodeTypes.TextNode,
2097-
value: node.value,
2098-
position: position(node),
2099-
source: node.source,
2100-
dashSeparated: node.dashSeparated,
2101-
};
2102-
}
2103-
2104-
function toParamType(node: ConcreteTextNode | null): TextNode | null {
2105-
if (!node) return null;
2106-
return {
2107-
type: NodeTypes.TextNode,
2108-
value: node.value,
2109-
position: position(node),
2110-
source: node.source,
2111-
};
2112-
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ const options: SupportOptions = {
6161
description: 'Indent the contents of the {% schema %} tag',
6262
since: '0.1.0',
6363
},
64+
liquidDocParamDash: {
65+
type: 'boolean',
66+
category: 'LIQUID',
67+
default: true,
68+
description: 'Append a dash (-) to separate descriptions in {% doc %} @param annotations',
69+
since: '1.6.4',
70+
},
6471
};
6572

6673
const defaultOptions = {

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ export function printLiquidDoc(
508508

509509
export function printLiquidDocParam(
510510
path: AstPath<LiquidDocParamNode>,
511-
_options: LiquidParserOptions,
511+
options: LiquidParserOptions,
512512
_print: LiquidPrinter,
513513
_args: LiquidPrinterArgs,
514514
): Doc {
@@ -525,7 +525,8 @@ export function printLiquidDocParam(
525525

526526
if (node.paramDescription?.value) {
527527
const normalizedDescription = node.paramDescription.value.replace(/\s+/g, ' ').trim();
528-
if (node.paramDescription.dashSeparated) {
528+
529+
if (options.liquidDocParamDash) {
529530
parts.push(' - ', normalizedDescription);
530531
} else {
531532
parts.push(' ', normalizedDescription);
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
It should indent the body
22
{% doc %}
3-
@param paramName param with description
4-
{% enddoc %}
5-
6-
It should not dedent to root
7-
{% doc %}
8-
@param paramName param with description
3+
@param paramName - param with description
94
{% enddoc %}
105

116
It should trim whitespace between nodes
127
{% doc %}
13-
@param paramName param with description
8+
@param paramName - param with description
149
{% enddoc %}
1510

16-
It should format the param type
11+
It should normalize the param type
1712
{% doc %}
18-
@param {string} paramName param with description
13+
@param {string} paramName - param with description
1914
{% enddoc %}
2015

2116
It should format the param description with a dash separator
2217
{% doc %}
2318
@param paramName - param with description
2419
{% enddoc %}
2520

26-
It should normalize the param description
21+
It should respect the liquidDocParamDash option liquidDocParamDash: false
2722
{% doc %}
2823
@param paramName param with description
2924
{% enddoc %}
25+
26+
It should normalize the param description
27+
{% doc %}
28+
@param paramName - param with description
29+
{% enddoc %}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
It should indent the body
22
{% doc %}
3-
@param paramName param with description
3+
@param paramName - param with description
44
{% enddoc %}
55

6-
It should not dedent to root
6+
It should trim whitespace between nodes
77
{% doc %}
8-
@param paramName param with description
8+
@param paramName - param with description
99
{% enddoc %}
1010

11-
It should trim whitespace between nodes
11+
It should normalize the param type
1212
{% doc %}
13-
@param paramName param with description
13+
@param { string } paramName - param with description
1414
{% enddoc %}
1515

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

21-
It should format the param description with a dash separator
21+
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
2929
{% enddoc %}
30-

packages/prettier-plugin-liquid/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type LiquidParserOptions = ParserOptions<LiquidHtmlNode> & {
2424
embeddedSingleQuote: boolean;
2525
indentSchema: boolean;
2626
captureWhitespaceSensitivity: 'strict' | 'ignore';
27+
liquidDocParamDash: boolean;
2728
};
2829
export type LiquidPrinterArgs = {
2930
leadingSpaceGroupId?: symbol[] | symbol;

0 commit comments

Comments
 (0)