Skip to content

Commit dcb46c0

Browse files
committed
Update stage-2 to include required field on liquid doc params
1 parent 23871e5 commit dcb46c0

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
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-12
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 paramWithNoType
1234+
@param requiredParamWithNoType
1235+
@param [optionalParameter] - optional parameter description
12351236
@param {String} paramWithDescription - param with description and \`punctation\`. This is still a valid param description.
12361237
@param {String} paramWithNoDescription
12371238
@unsupported this node falls back to a text node
@@ -1242,34 +1243,43 @@ describe('Unit: Stage 2 (AST)', () => {
12421243

12431244
expectPath(ast, 'children.0.body.nodes.0.type').to.eql('LiquidDocParamNode');
12441245
expectPath(ast, 'children.0.body.nodes.0.name').to.eql('param');
1246+
expectPath(ast, 'children.0.body.nodes.0.required').to.eql(true);
12451247
expectPath(ast, 'children.0.body.nodes.0.paramName.type').to.eql('TextNode');
1246-
expectPath(ast, 'children.0.body.nodes.0.paramName.value').to.eql('paramWithNoType');
1248+
expectPath(ast, 'children.0.body.nodes.0.paramName.value').to.eql('requiredParamWithNoType');
12471249
expectPath(ast, 'children.0.body.nodes.0.paramType').to.be.null;
12481250
expectPath(ast, 'children.0.body.nodes.0.paramDescription').to.be.null;
12491251

12501252
expectPath(ast, 'children.0.body.nodes.1.type').to.eql('LiquidDocParamNode');
12511253
expectPath(ast, 'children.0.body.nodes.1.name').to.eql('param');
1254+
expectPath(ast, 'children.0.body.nodes.1.required').to.eql(false);
12521255
expectPath(ast, 'children.0.body.nodes.1.paramName.type').to.eql('TextNode');
1253-
expectPath(ast, 'children.0.body.nodes.1.paramName.value').to.eql('paramWithDescription');
1256+
expectPath(ast, 'children.0.body.nodes.1.paramName.value').to.eql('optionalParameter');
12541257
expectPath(ast, 'children.0.body.nodes.1.paramDescription.type').to.eql('TextNode');
12551258
expectPath(ast, 'children.0.body.nodes.1.paramDescription.value').to.eql(
1256-
'param with description and `punctation`. This is still a valid param description.',
1259+
'optional parameter description',
12571260
);
1258-
expectPath(ast, 'children.0.body.nodes.1.paramType.type').to.eql('TextNode');
1259-
expectPath(ast, 'children.0.body.nodes.1.paramType.value').to.eql('String');
1261+
expectPath(ast, 'children.0.body.nodes.1.paramType').to.be.null;
1262+
expectPath(ast, 'children.0.body.nodes.1.paramType').to.be.null;
12601263

12611264
expectPath(ast, 'children.0.body.nodes.2.type').to.eql('LiquidDocParamNode');
12621265
expectPath(ast, 'children.0.body.nodes.2.name').to.eql('param');
1266+
expectPath(ast, 'children.0.body.nodes.2.required').to.eql(true);
12631267
expectPath(ast, 'children.0.body.nodes.2.paramName.type').to.eql('TextNode');
1264-
expectPath(ast, 'children.0.body.nodes.2.paramName.value').to.eql('paramWithNoDescription');
1265-
expectPath(ast, 'children.0.body.nodes.2.paramDescription').to.be.null;
1268+
expectPath(ast, 'children.0.body.nodes.2.paramName.value').to.eql('paramWithDescription');
1269+
expectPath(ast, 'children.0.body.nodes.2.paramDescription.type').to.eql('TextNode');
1270+
expectPath(ast, 'children.0.body.nodes.2.paramDescription.value').to.eql(
1271+
'param with description and `punctation`. This is still a valid param description.',
1272+
);
12661273
expectPath(ast, 'children.0.body.nodes.2.paramType.type').to.eql('TextNode');
12671274
expectPath(ast, 'children.0.body.nodes.2.paramType.value').to.eql('String');
12681275

1269-
expectPath(ast, 'children.0.body.nodes.3.type').to.eql('TextNode');
1270-
expectPath(ast, 'children.0.body.nodes.3.value').to.eql(
1271-
'@unsupported this node falls back to a text node',
1272-
);
1276+
expectPath(ast, 'children.0.body.nodes.3.type').to.eql('LiquidDocParamNode');
1277+
expectPath(ast, 'children.0.body.nodes.3.name').to.eql('param');
1278+
expectPath(ast, 'children.0.body.nodes.2.paramName.type').to.eql('TextNode');
1279+
expectPath(ast, 'children.0.body.nodes.3.paramName.value').to.eql('paramWithNoDescription');
1280+
expectPath(ast, 'children.0.body.nodes.3.paramDescription').to.be.null;
1281+
expectPath(ast, 'children.0.body.nodes.3.paramType.type').to.eql('TextNode');
1282+
expectPath(ast, 'children.0.body.nodes.3.paramType.value').to.eql('String');
12731283
});
12741284

12751285
it('should parse unclosed tables with assignments', () => {

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)