Skip to content

Commit 5426ddb

Browse files
committed
Update stage-2 to include required field on liquid doc params
1 parent 421f570 commit 5426ddb

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

.changeset/smart-onions-pump.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/liquid-html-parser': minor
3+
---
4+
5+
Add parser support for optional liquiddoc parameters

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

+5
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,7 @@ describe('Unit: Stage 1 (CST)', () => {
10331033
const testStr = `{% doc %}
10341034
@param [paramWithNoDescription]
10351035
@param [ paramWithWhitespace ]
1036+
@param {String} [optionalParam] - The optional param
10361037
{% enddoc %}`;
10371038
cst = toCST(testStr);
10381039

@@ -1059,6 +1060,10 @@ describe('Unit: Stage 1 (CST)', () => {
10591060
expectPath(cst, '0.children.1.paramName.locEnd').to.equal(
10601061
testStr.indexOf('paramWithWhitespace') + 'paramWithWhitespace'.length,
10611062
);
1063+
1064+
expectPath(cst, '0.children.2.type').to.equal('LiquidDocParamNode');
1065+
expectPath(cst, '0.children.2.required').to.equal(false);
1066+
expectPath(cst, '0.children.2.paramType.value').to.equal('String');
10621067
});
10631068

10641069
it('should parse @param with missing optional delimiters as required', () => {

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

+22-7
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,8 @@ describe('Unit: Stage 2 (AST)', () => {
12311231

12321232
ast = toLiquidAST(`
12331233
{% doc -%}
1234-
@param asdf
1234+
@param requiredParameter
1235+
@param [optionalParameter] - optional parameter description
12351236
@param {String} paramWithDescription - param with description and \`punctation\`. This is still a valid param description.
12361237
@unsupported this node falls back to a text node
12371238
{%- enddoc %}
@@ -1240,22 +1241,36 @@ describe('Unit: Stage 2 (AST)', () => {
12401241
expectPath(ast, 'children.0.name').to.eql('doc');
12411242
expectPath(ast, 'children.0.body.nodes.0.type').to.eql('LiquidDocParamNode');
12421243
expectPath(ast, 'children.0.body.nodes.0.name').to.eql('param');
1244+
expectPath(ast, 'children.0.body.nodes.0.required').to.eql(true);
12431245
expectPath(ast, 'children.0.body.nodes.0.paramName.type').to.eql('TextNode');
1244-
expectPath(ast, 'children.0.body.nodes.0.paramName.value').to.eql('asdf');
1246+
expectPath(ast, 'children.0.body.nodes.0.paramName.value').to.eql('requiredParameter');
12451247
expectPath(ast, 'children.0.body.nodes.0.paramDescription.type').to.eql('TextNode');
12461248
expectPath(ast, 'children.0.body.nodes.0.paramDescription.value').to.eql('');
1249+
12471250
expectPath(ast, 'children.0.body.nodes.1.type').to.eql('LiquidDocParamNode');
12481251
expectPath(ast, 'children.0.body.nodes.1.name').to.eql('param');
1252+
expectPath(ast, 'children.0.body.nodes.1.required').to.eql(false);
12491253
expectPath(ast, 'children.0.body.nodes.1.paramName.type').to.eql('TextNode');
1250-
expectPath(ast, 'children.0.body.nodes.1.paramName.value').to.eql('paramWithDescription');
1254+
expectPath(ast, 'children.0.body.nodes.1.paramName.value').to.eql('optionalParameter');
12511255
expectPath(ast, 'children.0.body.nodes.1.paramDescription.type').to.eql('TextNode');
12521256
expectPath(ast, 'children.0.body.nodes.1.paramDescription.value').to.eql(
1257+
'optional parameter description',
1258+
);
1259+
1260+
expectPath(ast, 'children.0.body.nodes.2.type').to.eql('LiquidDocParamNode');
1261+
expectPath(ast, 'children.0.body.nodes.2.name').to.eql('param');
1262+
expectPath(ast, 'children.0.body.nodes.2.required').to.eql(true);
1263+
expectPath(ast, 'children.0.body.nodes.2.paramName.type').to.eql('TextNode');
1264+
expectPath(ast, 'children.0.body.nodes.2.paramName.value').to.eql('paramWithDescription');
1265+
expectPath(ast, 'children.0.body.nodes.2.paramDescription.type').to.eql('TextNode');
1266+
expectPath(ast, 'children.0.body.nodes.2.paramDescription.value').to.eql(
12531267
'param with description and `punctation`. This is still a valid param description.',
12541268
);
1255-
expectPath(ast, 'children.0.body.nodes.1.paramType.type').to.eql('TextNode');
1256-
expectPath(ast, 'children.0.body.nodes.1.paramType.value').to.eql('String');
1257-
expectPath(ast, 'children.0.body.nodes.2.type').to.eql('TextNode');
1258-
expectPath(ast, 'children.0.body.nodes.2.value').to.eql(
1269+
expectPath(ast, 'children.0.body.nodes.2.paramType.type').to.eql('TextNode');
1270+
expectPath(ast, 'children.0.body.nodes.2.paramType.value').to.eql('String');
1271+
1272+
expectPath(ast, 'children.0.body.nodes.3.type').to.eql('TextNode');
1273+
expectPath(ast, 'children.0.body.nodes.3.value').to.eql(
12591274
'@unsupported this node falls back to a text node',
12601275
);
12611276
});

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ export interface TextNode extends ASTNode<NodeTypes.TextNode> {
755755
value: string;
756756
}
757757

758-
/** Represents a `@param` node in a LiquidDoc comment - `@param paramName {paramType} - paramDescription` */
758+
/** Represents a `@param` node in a LiquidDoc comment - `@param {paramType} [paramName] - paramDescription` */
759759
export interface LiquidDocParamNode extends ASTNode<NodeTypes.LiquidDocParamNode> {
760760
name: 'param';
761761
/** The name of the parameter (e.g. "product") */
@@ -764,6 +764,8 @@ export interface LiquidDocParamNode extends ASTNode<NodeTypes.LiquidDocParamNode
764764
paramDescription: TextNode | null;
765765
/** Optional type annotation for the parameter (e.g. "{string}", "{number}") */
766766
paramType: TextNode | null;
767+
/** Whether this parameter must be passed when using the snippet */
768+
required: boolean;
767769
}
768770
export interface ASTNode<T> {
769771
/**
@@ -1293,6 +1295,7 @@ function buildAst(
12931295
},
12941296
paramDescription: toNullableTextNode(node.paramDescription),
12951297
paramType: toNullableTextNode(node.paramType),
1298+
required: node.required,
12961299
});
12971300
break;
12981301
}

0 commit comments

Comments
 (0)