Skip to content

Commit

Permalink
stage2 tests and prettier support functioning
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilGenius13 committed Jan 15, 2025
1 parent 1b4763c commit 8db22b5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
37 changes: 37 additions & 0 deletions packages/liquid-html-parser/src/stage-2-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,43 @@ describe('Unit: Stage 2 (AST)', () => {
expectPath(ast, 'children.0.body.nodes.2.value').to.eql(
'@unsupported this node falls back to a text node',
);

ast = toLiquidAST(`
{% doc -%}
@example
This is a valid example
{%- enddoc %}
`);
expectPath(ast, 'children.0.type').to.eql('LiquidRawTag');
expectPath(ast, 'children.0.name').to.eql('doc');
expectPath(ast, 'children.0.body.nodes.0.name').to.eql('example');
expectPath(ast, 'children.0.body.nodes.0.type').to.eql('LiquidDocExampleNode');
expectPath(ast, 'children.0.body.nodes.0.exampleContent.type').to.eql('TextNode');
expectPath(ast, 'children.0.body.nodes.0.exampleContent.value').to.eql(
'This is a valid example',
);

ast = toLiquidAST(`
{% doc -%}
@example
This is a valid example
It can have multiple lines
@param {String} paramWithDescription - param with description
{% enddoc %}
`);
expectPath(ast, 'children.0.type').to.eql('LiquidRawTag');
expectPath(ast, 'children.0.name').to.eql('doc');
expectPath(ast, 'children.0.body.nodes.0.type').to.eql('LiquidDocExampleNode');
expectPath(ast, 'children.0.body.nodes.0.name').to.eql('example');
expectPath(ast, 'children.0.body.nodes.0.exampleContent.value').to.eql(
'This is a valid example\nIt can have multiple lines',
);
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.paramName.value').to.eql('paramWithDescription');
expectPath(ast, 'children.0.body.nodes.1.paramDescription.value').to.eql(
'param with description',
);
});

it('should parse unclosed tables with assignments', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/liquid-html-parser/src/stage-2-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,11 @@ function buildAst(
source: node.source,
exampleContent: {
type: NodeTypes.TextNode,
value: node.exampleContent.value,
value: node.exampleContent.value
.split('\n')
.map(line => line.trim())
.filter(Boolean)
.join('\n'),
position: position(node.exampleContent),
source: node.exampleContent.source,
},
Expand Down
7 changes: 6 additions & 1 deletion packages/prettier-plugin-liquid/src/printer/print/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,12 @@ export function printLiquidDocExample(
_args: LiquidPrinterArgs,
): Doc {
const node = path.getValue();
const parts: Doc[] = ['@example', 'asdf'];
const parts: Doc[] = ['@example'];

if (node.exampleContent?.value.trim()) {
parts.push(hardline);
parts.push(join(hardline, node.exampleContent.value.split('\n')));
}

return parts;
}
Expand Down

0 comments on commit 8db22b5

Please sign in to comment.