Skip to content

Commit

Permalink
Update stage-2 to include required field on liquid doc params
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmengo committed Jan 24, 2025
1 parent 7adc3f6 commit 04f5d07
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
29 changes: 22 additions & 7 deletions packages/liquid-html-parser/src/stage-2-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,8 @@ describe('Unit: Stage 2 (AST)', () => {

ast = toLiquidAST(`
{% doc -%}
@param asdf
@param requiredParameter
@param [optionalParameter] - optional parameter description
@param {String} paramWithDescription - param with description and \`punctation\`. This is still a valid param description.
@unsupported this node falls back to a text node
{%- enddoc %}
Expand All @@ -1240,22 +1241,36 @@ describe('Unit: Stage 2 (AST)', () => {
expectPath(ast, 'children.0.name').to.eql('doc');
expectPath(ast, 'children.0.body.nodes.0.type').to.eql('LiquidDocParamNode');
expectPath(ast, 'children.0.body.nodes.0.name').to.eql('param');
expectPath(ast, 'children.0.body.nodes.0.required').to.eql(true);
expectPath(ast, 'children.0.body.nodes.0.paramName.type').to.eql('TextNode');
expectPath(ast, 'children.0.body.nodes.0.paramName.value').to.eql('asdf');
expectPath(ast, 'children.0.body.nodes.0.paramName.value').to.eql('requiredParameter');
expectPath(ast, 'children.0.body.nodes.0.paramDescription.type').to.eql('TextNode');
expectPath(ast, 'children.0.body.nodes.0.paramDescription.value').to.eql('');

expectPath(ast, 'children.0.body.nodes.1.type').to.eql('LiquidDocParamNode');
expectPath(ast, 'children.0.body.nodes.1.name').to.eql('param');
expectPath(ast, 'children.0.body.nodes.1.required').to.eql(false);
expectPath(ast, 'children.0.body.nodes.1.paramName.type').to.eql('TextNode');
expectPath(ast, 'children.0.body.nodes.1.paramName.value').to.eql('paramWithDescription');
expectPath(ast, 'children.0.body.nodes.1.paramName.value').to.eql('optionalParameter');
expectPath(ast, 'children.0.body.nodes.1.paramDescription.type').to.eql('TextNode');
expectPath(ast, 'children.0.body.nodes.1.paramDescription.value').to.eql(
'optional parameter description',
);

expectPath(ast, 'children.0.body.nodes.2.type').to.eql('LiquidDocParamNode');
expectPath(ast, 'children.0.body.nodes.2.name').to.eql('param');
expectPath(ast, 'children.0.body.nodes.2.required').to.eql(true);
expectPath(ast, 'children.0.body.nodes.2.paramName.type').to.eql('TextNode');
expectPath(ast, 'children.0.body.nodes.2.paramName.value').to.eql('paramWithDescription');
expectPath(ast, 'children.0.body.nodes.2.paramDescription.type').to.eql('TextNode');
expectPath(ast, 'children.0.body.nodes.2.paramDescription.value').to.eql(
'param with description and `punctation`. This is still a valid param description.',
);
expectPath(ast, 'children.0.body.nodes.1.paramType.type').to.eql('TextNode');
expectPath(ast, 'children.0.body.nodes.1.paramType.value').to.eql('String');
expectPath(ast, 'children.0.body.nodes.2.type').to.eql('TextNode');
expectPath(ast, 'children.0.body.nodes.2.value').to.eql(
expectPath(ast, 'children.0.body.nodes.2.paramType.type').to.eql('TextNode');
expectPath(ast, 'children.0.body.nodes.2.paramType.value').to.eql('String');

expectPath(ast, 'children.0.body.nodes.3.type').to.eql('TextNode');
expectPath(ast, 'children.0.body.nodes.3.value').to.eql(
'@unsupported this node falls back to a text node',
);
});
Expand Down
5 changes: 4 additions & 1 deletion packages/liquid-html-parser/src/stage-2-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ export interface TextNode extends ASTNode<NodeTypes.TextNode> {
value: string;
}

/** Represents a `@param` node in a LiquidDoc comment - `@param paramName {paramType} - paramDescription` */
/** Represents a `@param` node in a LiquidDoc comment - `@param {paramType} [paramName] - paramDescription` */
export interface LiquidDocParamNode extends ASTNode<NodeTypes.LiquidDocParamNode> {
name: 'param';
/** The name of the parameter (e.g. "product") */
Expand All @@ -764,6 +764,8 @@ export interface LiquidDocParamNode extends ASTNode<NodeTypes.LiquidDocParamNode
paramDescription: TextNode | null;
/** Optional type annotation for the parameter (e.g. "{string}", "{number}") */
paramType: TextNode | null;
/** Whether this parameter must be passed when using the snippet */
required: boolean;
}
export interface ASTNode<T> {
/**
Expand Down Expand Up @@ -1293,6 +1295,7 @@ function buildAst(
},
paramDescription: toNullableTextNode(node.paramDescription),
paramType: toNullableTextNode(node.paramType),
required: node.required,
});
break;
}
Expand Down

0 comments on commit 04f5d07

Please sign in to comment.